lib/guides/indexer.rb in guides-0.6.10 vs lib/guides/indexer.rb in guides-0.7.0
- old
+ new
@@ -28,24 +28,33 @@
s = StringScanner.new(string)
level_hash = ActiveSupport::OrderedHash.new
while s.scan_until(%r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$})
- level, idx, title = s[1].to_i, s[2], s[3].strip
+ level, element_id, 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)
+ index = counters.dup
+ if format = Guides.config['index_format']
+ index[0] = sprintf(format, index[0])
+ end
+ index = index.join('.')
- raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{idx}). #{index} #{title}")
+ element_id ||= '#' + title_to_element_id(title)
+ header_html = Guides.config['index_header'] || "{{index}} {{title}}"
+ values = { 'index' => index, 'title' => title }
+ header = header_html.gsub(/{{(.*?)}}/){ values[$1].to_str }
+
+ raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{element_id}). #{header}")
+
key = {
:title => title,
- :id => idx
+ :id => element_id
}
# Recurse
counters << 1
level_hash[key] = process(s.post_match, current_level + 1, counters)
counters.pop
@@ -56,14 +65,14 @@
end
end
level_hash
end
- def title_to_idx(title)
- idx = title.strip.parameterize.sub(/^\d+/, '')
- if warnings && idx.blank?
+ def title_to_element_id(title)
+ element_id = title.strip.parameterize.sub(/^\d+/, '')
+ if warnings && element_id.blank?
puts "BLANK ID: please put an explicit ID for section #{title}, as in h5(#my-id)"
end
- idx
+ element_id
end
end
end