Skip to content
This repository
Browse code

Try to use actionpack gem to generate guide when Rails is not vendored

  • Loading branch information...
commit dc88847e5ce392eed210b97525c14fca55852867 1 parent 18eb80c
Pratik authored March 17, 2009
28  railties/guides/rails_guides.rb
... ...
@@ -1,17 +1,28 @@
1 1
 pwd = File.dirname(__FILE__)
2 2
 $: << pwd
3  
-$: << File.join(pwd, "../../activesupport/lib")
4  
-$: << File.join(pwd, "../../actionpack/lib")
5 3
 
6  
-require "action_controller"
7  
-require "action_view"
  4
+begin
  5
+  as_lib = File.join(pwd, "../../activesupport/lib")
  6
+  ap_lib = File.join(pwd, "../../actionpack/lib")
  7
+
  8
+  $: << as_lib if File.directory?(as_lib)
  9
+  $: << ap_lib if File.directory?(ap_lib)
  10
+  
  11
+  require "action_controller"
  12
+  require "action_view"
  13
+rescue LoadError
  14
+  require 'rubygems'
  15
+  gem "actionpack", '>= 2.3'
  16
+
  17
+  require "action_controller"
  18
+  require "action_view"
  19
+end
8 20
 
9  
-# Require rubygems after loading Action View
10  
-require 'rubygems'
11 21
 begin
12  
-  gem 'RedCloth', '>= 4.1.1'# Need exactly 4.1.1
  22
+  require 'rubygems'
  23
+  gem 'RedCloth', '>= 4.1.1'
13 24
 rescue Gem::LoadError
14  
-  $stderr.puts %(Missing the RedCloth 4.1.1 gem.\nPlease `gem install -v=4.1.1 RedCloth` to generate the guides.)
  25
+  $stderr.puts %(Generating Guides requires RedCloth 4.1.1+)
15 26
   exit 1
16 27
 end
17 28
 
@@ -22,7 +33,6 @@ module RailsGuides
22 33
   autoload :Indexer, "rails_guides/indexer"
23 34
   autoload :Helpers, "rails_guides/helpers"
24 35
   autoload :TextileExtensions, "rails_guides/textile_extensions"
25  
-  autoload :Levenshtein, "rails_guides/levenshtein"
26 36
 end
27 37
 
28 38
 RedCloth.send(:include, RailsGuides::TextileExtensions)
34  railties/guides/rails_guides/generator.rb
@@ -57,7 +57,6 @@ def generate_guide(guide)
57 57
 
58 58
           result = view.render(:layout => 'layout', :text => textile(body))
59 59
           f.write result
60  
-          warn_about_broken_links(result)
61 60
         end
62 61
       end
63 62
     end
@@ -135,38 +134,5 @@ def with_workaround_for_notextile(body)
135 134
         code_blocks[$1.to_i]
136 135
       end
137 136
     end
138  
-
139  
-    def warn_about_broken_links(html)
140  
-      anchors = extract_anchors(html)
141  
-      check_fragment_identifiers(html, anchors)
142  
-    end
143  
-    
144  
-    def extract_anchors(html)
145  
-      # Textile generates headers with IDs computed from titles.
146  
-      anchors = Set.new
147  
-      html.scan(/<h\d\s+id="([^"]+)/).flatten.each do |anchor|
148  
-        if anchors.member?(anchor)
149  
-          puts "*** DUPLICATE HEADER ID: #{anchor}, please consider rewording"
150  
-        else
151  
-          anchors << anchor
152  
-        end
153  
-      end
154  
-
155  
-      # Also, footnotes are rendered as paragraphs this way.
156  
-      anchors += Set.new(html.scan(/<p\s+class="footnote"\s+id="([^"]+)/).flatten)
157  
-      return anchors
158  
-    end
159  
-    
160  
-    def check_fragment_identifiers(html, anchors)
161  
-      html.scan(/<a\s+href="#([^"]+)/).flatten.each do |fragment_identifier|
162  
-        next if fragment_identifier == 'mainCol' # in layout, jumps to some DIV
163  
-        unless anchors.member?(fragment_identifier)
164  
-          guess = anchors.min { |a, b|
165  
-            Levenshtein.distance(fragment_identifier, a) <=> Levenshtein.distance(fragment_identifier, b)
166  
-          }
167  
-          puts "*** BROKEN LINK: ##{fragment_identifier}, perhaps you meant ##{guess}."
168  
-        end
169  
-      end
170  
-    end
171 137
   end
172 138
 end
112  railties/guides/rails_guides/levenshtein.rb
... ...
@@ -1,112 +0,0 @@
1  
-#
2  
-# Levenshtein distance algorithm implementation for Ruby, with UTF-8 support
3  
-#
4  
-# Author::  Paul BATTLEY (pbattley @ gmail.com)
5  
-# Version:: 1.3
6  
-# Date::    2005-04-19
7  
-#
8  
-# == About
9  
-#
10  
-# The Levenshtein distance is a measure of how similar two strings s and t are,
11  
-# calculated as the number of deletions/insertions/substitutions needed to
12  
-# transform s into t.  The greater the distance, the more the strings differ.
13  
-#
14  
-# The Levenshtein distance is also sometimes referred to as the
15  
-# easier-to-pronounce-and-spell 'edit distance'.
16  
-#
17  
-# == Revision history
18  
-#
19  
-# * 2005-05-19 1.3 Repairing an oversight, distance can now be called via
20  
-#   Levenshtein.distance(s, t)
21  
-# * 2005-05-04 1.2 Now uses just one 1-dimensional array.  I think this is as
22  
-#   far as optimisation can go.
23  
-# * 2005-05-04 1.1 Now storing only the current and previous rows of the matrix
24  
-#   instead of the whole lot.
25  
-#
26  
-# == Licence
27  
-#
28  
-# Copyright (c) 2005 Paul Battley
29  
-#
30  
-# Usage of the works is permitted provided that this instrument is retained
31  
-# with the works, so that any entity that uses the works is notified of this
32  
-# instrument.
33  
-#
34  
-# DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.  
35  
-#
36  
-
37  
-module Levenshtein
38  
-
39  
-    #
40  
-    # Calculate the Levenshtein distance between two strings +str1+ and +str2+.
41  
-    # +str1+ and +str2+ should be ASCII or UTF-8.
42  
-    #
43  
-    def distance(str1, str2)
44  
-        s = str1.unpack('U*')
45  
-        t = str2.unpack('U*')
46  
-        n = s.length
47  
-        m = t.length
48  
-        return m if (0 == n)
49  
-        return n if (0 == m)
50  
-        
51  
-        d = (0..m).to_a
52  
-        x = nil
53  
-
54  
-        (0...n).each do |i|
55  
-            e = i+1
56  
-            (0...m).each do |j|
57  
-                cost = (s[i] == t[j]) ? 0 : 1
58  
-                x = [
59  
-                    d[j+1] + 1, # insertion
60  
-                    e + 1,      # deletion
61  
-                    d[j] + cost # substitution
62  
-                ].min
63  
-                d[j] = e
64  
-                e = x
65  
-            end
66  
-            d[m] = x
67  
-        end
68  
-
69  
-        return x
70  
-    end
71  
-
72  
-    extend self
73  
-end
74  
-
75  
-if (__FILE__ == $0)
76  
-    require 'test/unit'
77  
-
78  
-    class LevenshteinTest < Test::Unit::TestCase
79  
-        include Levenshtein
80  
-
81  
-        EXPECTED = [
82  
-            # Easy ones
83  
-            ['test', 'test', 0],
84  
-            ['test', 'tent', 1],
85  
-            ['gumbo', 'gambol', 2],
86  
-            ['kitten', 'sitting', 3],
87  
-            # Empty strings
88  
-            ['foo', '', 3],
89  
-            ['', '', 0],
90  
-            ['a', '', 1],
91  
-            # UTF-8
92  
-            ["f\303\266o", 'foo', 1],
93  
-            ["fran\303\247ais", 'francais', 1],
94  
-            ["fran\303\247ais", "fran\303\246ais", 1],
95  
-            ["\347\247\201\343\201\256\345\220\215\345\211\215\343\201\257"<<
96  
-             "\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231",
97  
-             "\343\201\274\343\201\217\343\201\256\345\220\215\345\211\215\343\201"<<
98  
-             "\257\343\203\235\343\203\274\343\203\253\343\201\247\343\201\231", 
99  
-             2], # Japanese
100  
-            # Edge cases
101  
-            ['a', 'a', 0],
102  
-            ['0123456789', 'abcdefghijklmnopqrstuvwxyz', 26]
103  
-        ]
104  
-
105  
-        def test_known_distances
106  
-            EXPECTED.each do |a,b,x|
107  
-                assert_equal(x, distance(a, b))
108  
-                assert_equal(x, distance(b, a))
109  
-            end
110  
-        end
111  
-    end
112  
-end

0 notes on commit dc88847

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