diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-02-04 01:44:58 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-02-04 01:44:58 +0000 |
commit | 03bb1ebec513de779908af110c46e16b9c882673 (patch) | |
tree | 4df5643c61501de1b8e4101746e324a183042262 /railties/guides/rails_guides/indexer.rb | |
parent | e4094e23f9e3740599e9eaac527fbef86ff4b4c4 (diff) | |
download | rails-03bb1ebec513de779908af110c46e16b9c882673.tar.gz rails-03bb1ebec513de779908af110c46e16b9c882673.tar.bz2 rails-03bb1ebec513de779908af110c46e16b9c882673.zip |
Convert the guides from asciidoc to textile and integrate with the new design.
If you're a guide writer and want to generate the guides, Run :
ruby railties/guides/rails_guides.rb
And guides HTML will get generated inside railties/guides/output directory.
Diffstat (limited to 'railties/guides/rails_guides/indexer.rb')
-rw-r--r-- | railties/guides/rails_guides/indexer.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/railties/guides/rails_guides/indexer.rb b/railties/guides/rails_guides/indexer.rb new file mode 100644 index 0000000000..7f1c55a980 --- /dev/null +++ b/railties/guides/rails_guides/indexer.rb @@ -0,0 +1,51 @@ +module RailsGuides + class Indexer + attr_reader :body, :result, :level_hash + + def initialize(body) + @body = body + @result = @body.dup + end + + def index + @level_hash = process(body) + end + + private + + def process(string, current_level= 3, counters = [1]) + s = StringScanner.new(string) + + level_hash = ActiveSupport::OrderedHash.new + + while !s.eos? + s.match?(/\h[0-9]\..*$/) + if matched = s.matched + matched =~ /\h([0-9])\.(.*)$/ + level, title = $1.to_i, $2 + + if level < current_level + # This is needed. Go figure. + return level_hash + elsif level == current_level + index = counters.join(".") + bookmark = '#' + title.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize + + raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}") + + # Recurse + counters << 1 + level_hash[title.strip] = process(s.post_match, current_level + 1, counters) + counters.pop + + # Increment the current level + last = counters.pop + counters << last + 1 + end + end + s.getch + end + level_hash + end + end +end |