From 0db6c3f51828e1a37e2c7b9245ffa8c12ac59c83 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 28 Feb 2009 19:44:25 +0000 Subject: Merge docrails and update the release notes --- railties/guides/rails_guides/generator.rb | 46 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'railties/guides/rails_guides/generator.rb') diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 41d22e37fd..18fdb81810 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -1,3 +1,5 @@ +require 'set' + module RailsGuides class Generator attr_reader :output, :view_path, :view, :guides_dir @@ -55,6 +57,7 @@ module RailsGuides result = view.render(:layout => 'layout', :text => textile(body)) f.write result + warn_about_broken_links(result) end end end @@ -106,9 +109,46 @@ module RailsGuides end def textile(body) - t = RedCloth.new(body) - t.hard_breaks = false - t.to_html(:notestuff, :plusplus, :code, :tip) + # If the issue with nontextile is fixed just remove the wrapper. + with_workaround_for_nontextile(body) do |body| + 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. + def with_workaround_for_nontextile(body) + code_blocks = [] + body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)}m) do |m| + es = ERB::Util.h($2) + css_class = ['erb', 'shell'].include?($1) ? 'html' : $1 + code_blocks << %{
#{es}
} + "dirty_workaround_for_nontextile_#{code_blocks.size - 1}" + end + + body = yield body + + body.gsub(%r{

dirty_workaround_for_nontextile_(\d+)

}) do |_| + code_blocks[$1.to_i] + end + end + + def warn_about_broken_links(html) + # Textile generates headers with IDs computed from titles. + anchors = Set.new(html.scan(/