diff options
Diffstat (limited to 'railties/guides/rails_guides/generator.rb')
-rw-r--r-- | railties/guides/rails_guides/generator.rb | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index dd147c4d5f..7807c2c565 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -137,16 +137,34 @@ module RailsGuides 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) + 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) - - # Check fragment identifiers. + 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) - puts "BROKEN LINK: ##{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}." if ENV.key?("WARN_BROKEN_LINKS") end end end |