let the guides generator warn about broken links

This commit is contained in:
Xavier Noria 2009-02-28 03:24:23 +01:00
parent 626c724b62
commit da5549b83f

@ -1,3 +1,5 @@
require 'set'
module RailsGuides
class Generator
attr_reader :output, :view_path, :view, :guides_dir
@ -55,6 +57,7 @@ def generate_guide(guide)
result = view.render(:layout => 'layout', :text => textile(body))
f.write result
warn_about_broken_links(result)
end
end
end
@ -110,5 +113,20 @@ def textile(body)
t.hard_breaks = false
t.to_html(:notestuff, :plusplus, :code, :tip)
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