diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-03-24 12:15:43 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-03-24 12:15:43 +0000 |
commit | f97832b1e4406a76d268a96b521d2297adec0ae3 (patch) | |
tree | ec54e4d91113e8d3b6a481912a687aa97e3c63e2 /railties/guides/rails_guides/generator.rb | |
parent | 6ed42ebdff05f9d28a60e91093d8f9afad03a958 (diff) | |
download | rails-f97832b1e4406a76d268a96b521d2297adec0ae3.tar.gz rails-f97832b1e4406a76d268a96b521d2297adec0ae3.tar.bz2 rails-f97832b1e4406a76d268a96b521d2297adec0ae3.zip |
Merge docrails
Diffstat (limited to 'railties/guides/rails_guides/generator.rb')
-rw-r--r-- | railties/guides/rails_guides/generator.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 6c0d9f3c3b..f93282db2e 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -57,6 +57,7 @@ module RailsGuides result = view.render(:layout => 'layout', :text => textile(body)) f.write result + warn_about_broken_links(result) if ENV.key?("WARN_BROKEN_LINKS") end end end @@ -134,5 +135,38 @@ module RailsGuides code_blocks[$1.to_i] end end + + def warn_about_broken_links(html) + anchors = extract_anchors(html) + check_fragment_identifiers(html, anchors) + end + + def extract_anchors(html) + # Textile generates headers with IDs computed from titles. + anchors = Set.new + html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor| + if anchors.member?(anchor) + puts "*** DUPLICATE HEADER ID: #{anchor}, please consider rewording" if ENV.key?("WARN_DUPLICATE_HEADERS") + else + anchors << anchor + end + end + + # Also, footnotes are rendered as paragraphs this way. + anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten) + return anchors + end + + def check_fragment_identifiers(html, anchors) + 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) + guess = anchors.min { |a, b| + Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b) + } + puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}." + end + end + end end end |