Skip to content
Browse files

let the guides generator warn about broken links

  • Loading branch information...
1 parent 626c724 commit da5549b83fe7bf2c36e695e570a12d2fc9e8bbbd @fxn fxn committed Feb 28, 2009
Showing with 18 additions and 0 deletions.
  1. +18 −0 railties/guides/rails_guides/generator.rb
View
18 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 @@ 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

0 comments on commit da5549b

Please sign in to comment.
Something went wrong with that request. Please try again.