Skip to content
Browse files

Cleanup to Indexer

  • Loading branch information...
1 parent a8a3a9e commit 0d9706dc360032f02563a55a6092005f0e7f2100 @wagenet wagenet committed Apr 27, 2011
Showing with 24 additions and 30 deletions.
  1. +24 −30 lib/guides/indexer.rb
View
54 lib/guides/indexer.rb
@@ -23,37 +23,31 @@ def process(string, current_level=3, counters=[1])
level_hash = ActiveSupport::OrderedHash.new
- while !s.eos?
- re = %r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$}
- s.match?(re)
- if matched = s.matched
- matched =~ re
- level, idx, title = $1.to_i, $2, $3.strip
-
- if level < current_level
- # This is needed. Go figure.
- return level_hash
- elsif level == current_level
- index = counters.join(".")
- idx ||= '#' + title_to_idx(title)
-
- raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{idx}). #{index} #{title}")
-
- key = {
- :title => title,
- :id => idx
- }
- # Recurse
- counters << 1
- level_hash[key] = process(s.post_match, current_level + 1, counters)
- counters.pop
-
- # Increment the current level
- last = counters.pop
- counters << last + 1
- end
+ while s.scan_until(%r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$})
+ level, idx, title = s[1].to_i, s[2], s[3].strip
+
+ if level < current_level
+ # This is needed. Go figure.
+ return level_hash
+ elsif level == current_level
+ index = counters.join(".")
+ idx ||= '#' + title_to_idx(title)
+
+ raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{idx}). #{index} #{title}")
+
+ key = {
+ :title => title,
+ :id => idx
+ }
+ # Recurse
+ counters << 1
+ level_hash[key] = process(s.post_match, current_level + 1, counters)
+ counters.pop
+
+ # Increment the current level
+ last = counters.pop
+ counters << last + 1
end
- s.getch
end
level_hash
end

0 comments on commit 0d9706d

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