2010-09-05 22:24:15 +02:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# This script generates the guides. It can be invoked either directly or via the
|
|
|
|
# generate_guides rake task within the railties directory.
|
|
|
|
#
|
|
|
|
# Guides are taken from the source directory, and the resulting HTML goes into the
|
|
|
|
# output directory. Assets are stored under files, and copied to output/files as
|
|
|
|
# part of the generation process.
|
|
|
|
#
|
|
|
|
# Some arguments may be passed via environment variables:
|
|
|
|
#
|
|
|
|
# WARNINGS
|
|
|
|
# If you are writing a guide, please work always with WARNINGS=1. Users can
|
|
|
|
# generate the guides, and thus this flag is off by default.
|
|
|
|
#
|
|
|
|
# Internal links (anchors) are checked. If a reference is broken levenshtein
|
|
|
|
# distance is used to suggest an existing one. This is useful since IDs are
|
|
|
|
# generated by Textile from headers and thus edits alter them.
|
|
|
|
#
|
|
|
|
# Also detects duplicated IDs. They happen if there are headers with the same
|
|
|
|
# text. Please do resolve them, if any, so guides are valid XHTML.
|
|
|
|
#
|
|
|
|
# ALL
|
|
|
|
# Set to "1" to force the generation of all guides.
|
|
|
|
#
|
|
|
|
# ONLY
|
|
|
|
# Use ONLY if you want to generate only one or a set of guides. Prefixes are
|
|
|
|
# enough:
|
|
|
|
#
|
|
|
|
# # generates only association_basics.html
|
|
|
|
# ONLY=assoc ruby rails_guides.rb
|
|
|
|
#
|
|
|
|
# Separate many using commas:
|
|
|
|
#
|
|
|
|
# # generates only
|
|
|
|
# ONLY=assoc,migrations ruby rails_guides.rb
|
|
|
|
#
|
|
|
|
# Note that if you are working on a guide generation will by default process
|
|
|
|
# only that one, so ONLY is rarely used nowadays.
|
|
|
|
#
|
|
|
|
# EDGE
|
|
|
|
# Set to "1" to indicate generated guides should be marked as edge. This
|
|
|
|
# inserts a badge and changes the preamble of the home page.
|
|
|
|
#
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2009-03-02 09:32:25 +01:00
|
|
|
require 'set'
|
2010-09-05 22:24:15 +02:00
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
require 'active_support/core_ext/string/output_safety'
|
|
|
|
require 'active_support/core_ext/object/blank'
|
|
|
|
require 'action_controller'
|
|
|
|
require 'action_view'
|
|
|
|
|
|
|
|
require 'rails_guides/indexer'
|
|
|
|
require 'rails_guides/helpers'
|
|
|
|
require 'rails_guides/levenshtein'
|
2009-03-02 09:32:25 +01:00
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
module RailsGuides
|
|
|
|
class Generator
|
2010-09-05 22:24:15 +02:00
|
|
|
attr_reader :guides_dir, :source_dir, :output_dir, :edge, :warnings, :all
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
GUIDES_RE = /\.(?:textile|html\.erb)$/
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def initialize(output=nil)
|
|
|
|
initialize_dirs(output)
|
|
|
|
create_output_dir_if_needed
|
|
|
|
set_flags_from_environment
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def generate
|
|
|
|
generate_guides
|
|
|
|
copy_assets
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def initialize_dirs(output)
|
|
|
|
@guides_dir = File.join(File.dirname(__FILE__), '..')
|
|
|
|
@source_dir = File.join(@guides_dir, "source")
|
|
|
|
@output_dir = output || File.join(@guides_dir, "output")
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def create_output_dir_if_needed
|
|
|
|
FileUtils.mkdir_p(output_dir)
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def set_flags_from_environment
|
|
|
|
@edge = ENV['EDGE'] == '1'
|
|
|
|
@warnings = ENV['WARNINGS'] == '1'
|
|
|
|
@all = ENV['ALL'] == '1'
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def generate_guides
|
|
|
|
guides_to_generate.each do |guide|
|
|
|
|
output_file = output_file_for(guide)
|
|
|
|
generate_guide(guide, output_file) if generate?(guide, output_file)
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def guides_to_generate
|
|
|
|
guides = Dir.entries(source_dir).grep(GUIDES_RE)
|
|
|
|
ENV.key?('ONLY') ? select_only(guides) : guides
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_only(guides)
|
|
|
|
prefixes = ENV['ONLY'].split(",").map(&:strip)
|
|
|
|
guides.select do |guide|
|
|
|
|
prefixes.any? {|p| guide.start_with?(p)}
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def copy_assets
|
|
|
|
FileUtils.cp_r(Dir.glob("#{guides_dir}/images"), output_dir)
|
|
|
|
FileUtils.cp_r(Dir.glob("#{guides_dir}/files"), output_dir)
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def output_file_for(guide)
|
|
|
|
guide.sub(GUIDES_RE, '.html')
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def generate?(source_file, output_file)
|
|
|
|
fin = File.join(source_dir, source_file)
|
|
|
|
fout = File.join(output_dir, output_file)
|
|
|
|
all || !File.exists?(fout) || File.mtime(fout) < File.mtime(fin)
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def generate_guide(guide, output_file)
|
|
|
|
puts "Generating #{output_file}"
|
|
|
|
File.open(File.join(output_dir, output_file), 'w') do |f|
|
|
|
|
view = ActionView::Base.new(source_dir, :edge => edge)
|
|
|
|
view.extend(Helpers)
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
if guide =~ /\.html\.erb$/
|
|
|
|
# Generate the special pages like the home.
|
|
|
|
result = view.render(:layout => 'layout', :file => guide)
|
2009-02-28 02:23:00 +01:00
|
|
|
else
|
2010-09-05 22:24:15 +02:00
|
|
|
body = File.read(File.join(source_dir, guide))
|
|
|
|
body = set_header_section(body, view)
|
|
|
|
body = set_index(body, view)
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
result = view.render(:layout => 'layout', :text => textile(body))
|
2010-09-05 22:24:15 +02:00
|
|
|
|
|
|
|
warn_about_broken_links(result) if @warnings
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
|
|
|
|
f.write result
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_header_section(body, view)
|
|
|
|
new_body = body.gsub(/(.*?)endprologue\./m, '').strip
|
|
|
|
header = $1
|
|
|
|
|
|
|
|
header =~ /h2\.(.*)/
|
2010-09-05 22:24:15 +02:00
|
|
|
page_title = "Ruby on Rails Guides: #{$1.strip}"
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
header = textile(header)
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
view.content_for(:page_title) { page_title.html_safe }
|
|
|
|
view.content_for(:header_section) { header.html_safe }
|
2009-02-28 02:23:00 +01:00
|
|
|
new_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_index(body, view)
|
|
|
|
index = <<-INDEX
|
|
|
|
<div id="subCol">
|
|
|
|
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
|
|
|
<ol class="chapters">
|
|
|
|
INDEX
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
i = Indexer.new(body, warnings)
|
2009-02-28 02:23:00 +01:00
|
|
|
i.index
|
|
|
|
|
|
|
|
# Set index for 2 levels
|
|
|
|
i.level_hash.each do |key, value|
|
2010-09-05 22:24:15 +02:00
|
|
|
link = view.content_tag(:a, :href => key[:id]) { textile(key[:title], true).html_safe }
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
children = value.keys.map do |k|
|
2010-09-05 22:24:15 +02:00
|
|
|
view.content_tag(:li,
|
|
|
|
view.content_tag(:a, :href => k[:id]) { textile(k[:title], true).html_safe })
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
children_ul = children.empty? ? "" : view.content_tag(:ul, children.join(" ").html_safe)
|
2009-02-28 02:23:00 +01:00
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
index << view.content_tag(:li, link.html_safe + children_ul.html_safe)
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
index << '</ol>'
|
|
|
|
index << '</div>'
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
view.content_for(:index_section) { index.html_safe }
|
2009-02-28 02:23:00 +01:00
|
|
|
|
|
|
|
i.result
|
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:15 +02:00
|
|
|
def textile(body, lite_mode=false)
|
2009-03-16 15:55:30 +01:00
|
|
|
# If the issue with notextile is fixed just remove the wrapper.
|
|
|
|
with_workaround_for_notextile(body) do |body|
|
2009-03-02 09:32:25 +01:00
|
|
|
t = RedCloth.new(body)
|
|
|
|
t.hard_breaks = false
|
2010-09-05 22:24:15 +02:00
|
|
|
t.lite_mode = lite_mode
|
2009-03-02 09:32:25 +01:00
|
|
|
t.to_html(:notestuff, :plusplus, :code, :tip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# For some reason the notextile tag does not always turn off textile. See
|
|
|
|
# LH ticket of the security guide (#7). As a temporary workaround we deal
|
|
|
|
# with code blocks by hand.
|
2009-03-16 15:55:30 +01:00
|
|
|
def with_workaround_for_notextile(body)
|
2009-03-02 09:32:25 +01:00
|
|
|
code_blocks = []
|
|
|
|
body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)</\1>}m) do |m|
|
|
|
|
es = ERB::Util.h($2)
|
|
|
|
css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
|
|
|
|
code_blocks << %{<div class="code_container"><code class="#{css_class}">#{es}</code></div>}
|
2009-03-16 15:55:30 +01:00
|
|
|
"\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
|
2009-03-02 09:32:25 +01:00
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
|
2009-03-02 09:32:25 +01:00
|
|
|
body = yield body
|
2010-09-05 22:24:15 +02:00
|
|
|
|
2009-03-16 15:55:30 +01:00
|
|
|
body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
|
2009-03-02 09:32:25 +01:00
|
|
|
code_blocks[$1.to_i]
|
|
|
|
end
|
|
|
|
end
|
2010-09-05 22:24:15 +02:00
|
|
|
|
|
|
|
def warn_about_broken_links(html)
|
|
|
|
anchors = extract_anchors(html)
|
|
|
|
check_fragment_identifiers(html, anchors)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_anchors(html)
|
|
|
|
# Textile generates headers with IDs computed from titles.
|
|
|
|
anchors = Set.new
|
|
|
|
html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
|
|
|
|
if anchors.member?(anchor)
|
|
|
|
puts "*** DUPLICATE ID: #{anchor}, please put and explicit ID, e.g. h4(#explicit-id), or consider rewording"
|
|
|
|
else
|
|
|
|
anchors << anchor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Also, footnotes are rendered as paragraphs this way.
|
|
|
|
anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
|
|
|
|
return anchors
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_fragment_identifiers(html, anchors)
|
|
|
|
html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
|
|
|
|
next if fragment_identifier == 'mainCol' # in layout, jumps to some DIV
|
|
|
|
unless anchors.member?(fragment_identifier)
|
|
|
|
guess = anchors.min { |a, b|
|
|
|
|
Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
|
|
|
|
}
|
|
|
|
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-28 02:23:00 +01:00
|
|
|
end
|
|
|
|
end
|