aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/rails_guides/indexer.rb
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-04-06 15:20:27 -0700
committerXavier Noria <fxn@hashref.com>2010-04-06 15:20:44 -0700
commit03cb74b9461293b96ae0add8ff5efda132dabba0 (patch)
treeee7e7843a5efc03827103e6ea5921a332689bf96 /railties/guides/rails_guides/indexer.rb
parent92eab845a422436aaba4abc9de90f937a91c6a4e (diff)
downloadrails-03cb74b9461293b96ae0add8ff5efda132dabba0.tar.gz
rails-03cb74b9461293b96ae0add8ff5efda132dabba0.tar.bz2
rails-03cb74b9461293b96ae0add8ff5efda132dabba0.zip
guides: adds support in the indexer for custom header IDs, and some refactors
Diffstat (limited to 'railties/guides/rails_guides/indexer.rb')
-rw-r--r--railties/guides/rails_guides/indexer.rb35
1 files changed, 24 insertions, 11 deletions
diff --git a/railties/guides/rails_guides/indexer.rb b/railties/guides/rails_guides/indexer.rb
index 939404c85f..bd817786ed 100644
--- a/railties/guides/rails_guides/indexer.rb
+++ b/railties/guides/rails_guides/indexer.rb
@@ -1,10 +1,14 @@
+require 'active_support/core_ext/object/blank'
+require 'active_support/ordered_hash'
+
module RailsGuides
class Indexer
- attr_reader :body, :result, :level_hash
+ attr_reader :body, :result, :warnings, :level_hash
- def initialize(body)
- @body = body
- @result = @body.dup
+ def initialize(body, warnings)
+ @body = body
+ @result = @body.dup
+ @warnings = warnings
end
def index
@@ -13,29 +17,30 @@ module RailsGuides
private
- def process(string, current_level= 3, counters = [1])
+ 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]\..*$/)
+ re = %r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$}
+ s.match?(re)
if matched = s.matched
- matched =~ /h([0-9])\.(.*)$/
- level, title = $1.to_i, $2
+ 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(".")
- bookmark = '#' + title.strip.downcase.gsub(/\s+|_/, '-').delete('^a-z0-9-')
+ idx ||= '#' + title_to_idx(title)
- raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}")
+ raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{idx}). #{index} #{title}")
key = {
:title => title,
- :id => bookmark
+ :id => idx
}
# Recurse
counters << 1
@@ -51,5 +56,13 @@ module RailsGuides
end
level_hash
end
+
+ def title_to_idx(title)
+ idx = title.strip.downcase.gsub(/\s+|_/, '-').delete('^a-z0-9-').sub(/^[^a-z]*/, '')
+ if warnings && idx.blank?
+ puts "BLANK ID: please put an explicit ID for section #{title}, as in h5(#my-id)"
+ end
+ idx
+ end
end
end