aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/rails_guides/generator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/rails_guides/generator.rb')
-rw-r--r--railties/guides/rails_guides/generator.rb46
1 files changed, 43 insertions, 3 deletions
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)>(.*?)</\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>}
+ "dirty_workaround_for_nontextile_#{code_blocks.size - 1}"
+ end
+
+ body = yield body
+
+ body.gsub(%r{<p>dirty_workaround_for_nontextile_(\d+)</p>}) 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(/<h\d\s+id="([^"]+)/).flatten)
+ # Also, footnotes are rendered as paragraphs this way.
+ anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
+
+ # Check fragment identifiers.
+ 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)
+ puts "BROKEN LINK: ##{fragment_identifier}"
+ end
+ end
end
end
end