From 18eb80ccc7e932f9a6c00462ceaeea648631b120 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 16 Mar 2009 11:28:36 +0000 Subject: Merge docrails --- railties/guides/rails_guides/generator.rb | 36 ++++++--- railties/guides/rails_guides/indexer.rb | 2 +- railties/guides/rails_guides/levenshtein.rb | 112 ++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 railties/guides/rails_guides/levenshtein.rb (limited to 'railties/guides/rails_guides') diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 18fdb81810..8e69af5bde 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -109,8 +109,8 @@ module RailsGuides end def textile(body) - # If the issue with nontextile is fixed just remove the wrapper. - with_workaround_for_nontextile(body) do |body| + # If the issue with notextile is fixed just remove the wrapper. + with_workaround_for_notextile(body) do |body| t = RedCloth.new(body) t.hard_breaks = false t.to_html(:notestuff, :plusplus, :code, :tip) @@ -120,33 +120,51 @@ module RailsGuides # 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) + def with_workaround_for_notextile(body) code_blocks = [] body.gsub!(%r{<(yaml|shell|ruby|erb|html|sql|plain)>(.*?)}m) do |m| es = ERB::Util.h($2) css_class = ['erb', 'shell'].include?($1) ? 'html' : $1 code_blocks << %{
#{es}
} - "dirty_workaround_for_nontextile_#{code_blocks.size - 1}" + "\ndirty_workaround_for_notextile_#{code_blocks.size - 1}\n" end body = yield body - body.gsub(%r{

dirty_workaround_for_nontextile_(\d+)

}) do |_| + body.gsub(%r{

dirty_workaround_for_notextile_(\d+)

}) do |_| 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(/ Levenshtein.distance(fragment_identifier, b) + } + puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}." end end end diff --git a/railties/guides/rails_guides/indexer.rb b/railties/guides/rails_guides/indexer.rb index 7cb254d0b0..5b5ad3fee1 100644 --- a/railties/guides/rails_guides/indexer.rb +++ b/railties/guides/rails_guides/indexer.rb @@ -29,7 +29,7 @@ module RailsGuides return level_hash elsif level == current_level index = counters.join(".") - bookmark = '#' + title.gsub(/[^a-z0-9\-_]+/i, '').underscore.dasherize + bookmark = '#' + title.strip.downcase.gsub(/\s+|_/, '-').delete('^a-z0-9-') raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}") diff --git a/railties/guides/rails_guides/levenshtein.rb b/railties/guides/rails_guides/levenshtein.rb new file mode 100644 index 0000000000..02e35f60d2 --- /dev/null +++ b/railties/guides/rails_guides/levenshtein.rb @@ -0,0 +1,112 @@ +# +# Levenshtein distance algorithm implementation for Ruby, with UTF-8 support +# +# Author:: Paul BATTLEY (pbattley @ gmail.com) +# Version:: 1.3 +# Date:: 2005-04-19 +# +# == About +# +# The Levenshtein distance is a measure of how similar two strings s and t are, +# calculated as the number of deletions/insertions/substitutions needed to +# transform s into t. The greater the distance, the more the strings differ. +# +# The Levenshtein distance is also sometimes referred to as the +# easier-to-pronounce-and-spell 'edit distance'. +# +# == Revision history +# +# * 2005-05-19 1.3 Repairing an oversight, distance can now be called via +# Levenshtein.distance(s, t) +# * 2005-05-04 1.2 Now uses just one 1-dimensional array. I think this is as +# far as optimisation can go. +# * 2005-05-04 1.1 Now storing only the current and previous rows of the matrix +# instead of the whole lot. +# +# == Licence +# +# Copyright (c) 2005 Paul Battley +# +# Usage of the works is permitted provided that this instrument is retained +# with the works, so that any entity that uses the works is notified of this +# instrument. +# +# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. +# + +module Levenshtein + + # + # Calculate the Levenshtein distance between two strings +str1+ and +str2+. + # +str1+ and +str2+ should be ASCII or UTF-8. + # + def distance(str1, str2) + s = str1.unpack('U*') + t = str2.unpack('U*') + n = s.length + m = t.length + return m if (0 == n) + return n if (0 == m) + + d = (0..m).to_a + x = nil + + (0...n).each do |i| + e = i+1 + (0...m).each do |j| + cost = (s[i] == t[j]) ? 0 : 1 + x = [ + d[j+1] + 1, # insertion + e + 1, # deletion + d[j] + cost # substitution + ].min + d[j] = e + e = x + end + d[m] = x + end + + return x + end + + extend self +end + +if (__FILE__ == $0) + require 'test/unit' + + class LevenshteinTest < Test::Unit::TestCase + include Levenshtein + + EXPECTED = [ + # Easy ones + ['test', 'test', 0], + ['test', 'tent', 1], + ['gumbo', 'gambol', 2], + ['kitten', 'sitting', 3], + # Empty strings + ['foo', '', 3], + ['', '', 0], + ['a', '', 1], + # UTF-8 + ["f\303\266o", 'foo', 1], + ["fran\303\247ais", 'francais', 1], + ["fran\303\247ais", "fran\303\246ais", 1], + ["\347\247\201\343\201\256\345\220\215\345\211\215\343\201\257"<< + "\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231", + "\343\201\274\343\201\217\343\201\256\345\220\215\345\211\215\343\201"<< + "\257\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231", + 2], # Japanese + # Edge cases + ['a', 'a', 0], + ['0123456789', 'abcdefghijklmnopqrstuvwxyz', 26] + ] + + def test_known_distances + EXPECTED.each do |a,b,x| + assert_equal(x, distance(a, b)) + assert_equal(x, distance(b, a)) + end + end + end +end -- cgit v1.2.3 From dc88847e5ce392eed210b97525c14fca55852867 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 17 Mar 2009 12:26:34 +0000 Subject: Try to use actionpack gem to generate guide when Rails is not vendored --- railties/guides/rails_guides/generator.rb | 34 --------- railties/guides/rails_guides/levenshtein.rb | 112 ---------------------------- 2 files changed, 146 deletions(-) delete mode 100644 railties/guides/rails_guides/levenshtein.rb (limited to 'railties/guides/rails_guides') diff --git a/railties/guides/rails_guides/generator.rb b/railties/guides/rails_guides/generator.rb index 8e69af5bde..6c0d9f3c3b 100644 --- a/railties/guides/rails_guides/generator.rb +++ b/railties/guides/rails_guides/generator.rb @@ -57,7 +57,6 @@ module RailsGuides result = view.render(:layout => 'layout', :text => textile(body)) f.write result - warn_about_broken_links(result) end end end @@ -135,38 +134,5 @@ 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(/ Levenshtein.distance(fragment_identifier, b) - } - puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}." - end - end - end end end diff --git a/railties/guides/rails_guides/levenshtein.rb b/railties/guides/rails_guides/levenshtein.rb deleted file mode 100644 index 02e35f60d2..0000000000 --- a/railties/guides/rails_guides/levenshtein.rb +++ /dev/null @@ -1,112 +0,0 @@ -# -# Levenshtein distance algorithm implementation for Ruby, with UTF-8 support -# -# Author:: Paul BATTLEY (pbattley @ gmail.com) -# Version:: 1.3 -# Date:: 2005-04-19 -# -# == About -# -# The Levenshtein distance is a measure of how similar two strings s and t are, -# calculated as the number of deletions/insertions/substitutions needed to -# transform s into t. The greater the distance, the more the strings differ. -# -# The Levenshtein distance is also sometimes referred to as the -# easier-to-pronounce-and-spell 'edit distance'. -# -# == Revision history -# -# * 2005-05-19 1.3 Repairing an oversight, distance can now be called via -# Levenshtein.distance(s, t) -# * 2005-05-04 1.2 Now uses just one 1-dimensional array. I think this is as -# far as optimisation can go. -# * 2005-05-04 1.1 Now storing only the current and previous rows of the matrix -# instead of the whole lot. -# -# == Licence -# -# Copyright (c) 2005 Paul Battley -# -# Usage of the works is permitted provided that this instrument is retained -# with the works, so that any entity that uses the works is notified of this -# instrument. -# -# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY. -# - -module Levenshtein - - # - # Calculate the Levenshtein distance between two strings +str1+ and +str2+. - # +str1+ and +str2+ should be ASCII or UTF-8. - # - def distance(str1, str2) - s = str1.unpack('U*') - t = str2.unpack('U*') - n = s.length - m = t.length - return m if (0 == n) - return n if (0 == m) - - d = (0..m).to_a - x = nil - - (0...n).each do |i| - e = i+1 - (0...m).each do |j| - cost = (s[i] == t[j]) ? 0 : 1 - x = [ - d[j+1] + 1, # insertion - e + 1, # deletion - d[j] + cost # substitution - ].min - d[j] = e - e = x - end - d[m] = x - end - - return x - end - - extend self -end - -if (__FILE__ == $0) - require 'test/unit' - - class LevenshteinTest < Test::Unit::TestCase - include Levenshtein - - EXPECTED = [ - # Easy ones - ['test', 'test', 0], - ['test', 'tent', 1], - ['gumbo', 'gambol', 2], - ['kitten', 'sitting', 3], - # Empty strings - ['foo', '', 3], - ['', '', 0], - ['a', '', 1], - # UTF-8 - ["f\303\266o", 'foo', 1], - ["fran\303\247ais", 'francais', 1], - ["fran\303\247ais", "fran\303\246ais", 1], - ["\347\247\201\343\201\256\345\220\215\345\211\215\343\201\257"<< - "\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231", - "\343\201\274\343\201\217\343\201\256\345\220\215\345\211\215\343\201"<< - "\257\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231", - 2], # Japanese - # Edge cases - ['a', 'a', 0], - ['0123456789', 'abcdefghijklmnopqrstuvwxyz', 26] - ] - - def test_known_distances - EXPECTED.each do |a,b,x| - assert_equal(x, distance(a, b)) - assert_equal(x, distance(b, a)) - end - end - end -end -- cgit v1.2.3