2009-02-27 21:24:23 -05:00
|
|
|
require 'set'
|
|
|
|
|
2009-02-03 20:44:58 -05:00
|
|
|
module RailsGuides
|
|
|
|
class Generator
|
|
|
|
attr_reader :output, :view_path, :view, :guides_dir
|
|
|
|
|
|
|
|
def initialize(output = nil)
|
|
|
|
@guides_dir = File.join(File.dirname(__FILE__), '..')
|
|
|
|
|
|
|
|
@output = output || File.join(@guides_dir, "output")
|
|
|
|
|
|
|
|
unless ENV["ONLY"]
|
|
|
|
FileUtils.rm_r(@output) if File.directory?(@output)
|
|
|
|
FileUtils.mkdir(@output)
|
|
|
|
end
|
|
|
|
|
|
|
|
@view_path = File.join(@guides_dir, "source")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
|
|
|
guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
|
|
|
|
|
|
|
|
if ENV["ONLY"]
|
|
|
|
only = ENV["ONLY"].split(",").map{|x| x.strip }.map {|o| "#{o}.textile" }
|
|
|
|
guides = guides.find_all {|g| only.include?(g) }
|
|
|
|
puts "GENERATING ONLY #{guides.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
guides.each do |guide|
|
2009-02-21 18:29:11 -05:00
|
|
|
generate_guide(guide)
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Copy images and css files to html directory
|
|
|
|
FileUtils.cp_r File.join(guides_dir, 'images'), File.join(output, 'images')
|
|
|
|
FileUtils.cp_r File.join(guides_dir, 'files'), File.join(output, 'files')
|
|
|
|
end
|
|
|
|
|
2009-02-21 18:29:11 -05:00
|
|
|
def generate_guide(guide)
|
|
|
|
guide =~ /(.*?)(\.erb)?\.textile/
|
|
|
|
name = $1
|
|
|
|
|
|
|
|
puts "Generating #{name}"
|
|
|
|
|
|
|
|
file = File.join(output, "#{name}.html")
|
|
|
|
File.open(file, 'w') do |f|
|
|
|
|
@view = ActionView::Base.new(view_path)
|
|
|
|
@view.extend(Helpers)
|
|
|
|
|
|
|
|
if guide =~ /\.erb\.textile/
|
|
|
|
# Generate the erb pages with textile formatting - e.g. index/authors
|
2009-04-05 18:07:38 -04:00
|
|
|
result = view.render(:layout => 'layout', :file => guide)
|
2009-02-21 18:29:11 -05:00
|
|
|
f.write textile(result)
|
|
|
|
else
|
|
|
|
body = File.read(File.join(view_path, guide))
|
|
|
|
body = set_header_section(body, @view)
|
|
|
|
body = set_index(body, @view)
|
|
|
|
|
|
|
|
result = view.render(:layout => 'layout', :text => textile(body))
|
|
|
|
f.write result
|
2009-03-24 08:10:38 -04:00
|
|
|
warn_about_broken_links(result) if ENV.key?("WARN_BROKEN_LINKS")
|
2009-02-21 18:29:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-03 20:44:58 -05:00
|
|
|
def set_header_section(body, view)
|
|
|
|
new_body = body.gsub(/(.*?)endprologue\./m, '').strip
|
|
|
|
header = $1
|
|
|
|
|
|
|
|
header =~ /h2\.(.*)/
|
|
|
|
page_title = $1.strip
|
|
|
|
|
|
|
|
header = textile(header)
|
|
|
|
|
|
|
|
view.content_for(:page_title) { page_title }
|
|
|
|
view.content_for(:header_section) { header }
|
|
|
|
new_body
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_index(body, view)
|
|
|
|
index = <<-INDEX
|
|
|
|
<div id="subCol">
|
2009-02-21 18:29:11 -05:00
|
|
|
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
2009-02-03 20:44:58 -05:00
|
|
|
<ol class="chapters">
|
2009-02-21 18:29:11 -05:00
|
|
|
INDEX
|
2009-02-03 20:44:58 -05:00
|
|
|
|
|
|
|
i = Indexer.new(body)
|
|
|
|
i.index
|
|
|
|
|
|
|
|
# Set index for 2 levels
|
|
|
|
i.level_hash.each do |key, value|
|
2009-02-21 18:29:11 -05:00
|
|
|
link = view.content_tag(:a, :href => key[:id]) { textile(key[:title]) }
|
2009-02-03 20:44:58 -05:00
|
|
|
|
|
|
|
children = value.keys.map do |k|
|
2009-02-21 18:29:11 -05:00
|
|
|
l = view.content_tag(:a, :href => k[:id]) { textile(k[:title]) }
|
2009-02-03 20:44:58 -05:00
|
|
|
view.content_tag(:li, l)
|
|
|
|
end
|
|
|
|
|
|
|
|
children_ul = view.content_tag(:ul, children)
|
|
|
|
|
|
|
|
index << view.content_tag(:li, link + children_ul)
|
|
|
|
end
|
|
|
|
|
|
|
|
index << '</ol>'
|
|
|
|
index << '</div>'
|
|
|
|
|
|
|
|
view.content_for(:index_section) { index }
|
|
|
|
|
|
|
|
i.result
|
|
|
|
end
|
|
|
|
|
|
|
|
def textile(body)
|
2009-03-01 19:06:54 -05:00
|
|
|
# If the issue with notextile is fixed just remove the wrapper.
|
|
|
|
with_workaround_for_notextile(body) do |body|
|
2009-02-28 11:07:46 -05:00
|
|
|
t = RedCloth.new(body)
|
|
|
|
t.hard_breaks = false
|
|
|
|
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-01 19:06:54 -05:00
|
|
|
def with_workaround_for_notextile(body)
|
2009-02-28 11:07:46 -05: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-01 19:06:54 -05:00
|
|
|
"\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n"
|
2009-02-28 11:07:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
body = yield body
|
|
|
|
|
2009-03-01 19:06:54 -05:00
|
|
|
body.gsub(%r{<p>dirty_workaround_for_notextile_(\d+)</p>}) do |_|
|
2009-02-28 11:07:46 -05:00
|
|
|
code_blocks[$1.to_i]
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
2009-02-28 11:07:46 -05:00
|
|
|
|
2009-02-27 21:24:23 -05:00
|
|
|
def warn_about_broken_links(html)
|
2009-03-15 17:16:27 -04:00
|
|
|
anchors = extract_anchors(html)
|
|
|
|
check_fragment_identifiers(html, anchors)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_anchors(html)
|
2009-02-27 21:24:23 -05:00
|
|
|
# Textile generates headers with IDs computed from titles.
|
2009-03-15 17:16:27 -04:00
|
|
|
anchors = Set.new
|
|
|
|
html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
|
|
|
|
if anchors.member?(anchor)
|
2009-03-21 19:43:56 -04:00
|
|
|
puts "*** DUPLICATE HEADER ID: #{anchor}, please consider rewording" if ENV.key?("WARN_DUPLICATE_HEADERS")
|
2009-03-15 17:16:27 -04:00
|
|
|
else
|
|
|
|
anchors << anchor
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-27 21:24:23 -05:00
|
|
|
# Also, footnotes are rendered as paragraphs this way.
|
|
|
|
anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
|
2009-03-15 17:16:27 -04:00
|
|
|
return anchors
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_fragment_identifiers(html, anchors)
|
2009-02-27 21:24:23 -05:00
|
|
|
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)
|
2009-03-15 15:03:37 -04:00
|
|
|
guess = anchors.min { |a, b|
|
|
|
|
Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
|
|
|
|
}
|
2009-03-24 08:10:38 -04:00
|
|
|
puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
|
2009-02-27 21:24:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 20:44:58 -05:00
|
|
|
end
|
|
|
|
end
|