Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added config options for indexes

  • Loading branch information...
commit e296e52b799d461562bdd2bdf893974aa9c5ab2e 1 parent 125f22e
Peter Wagenet authored September 28, 2011

Showing 1 changed file with 17 additions and 9 deletions. Show diff stats Hide diff stats

  1. 26  lib/guides/indexer.rb
26  lib/guides/indexer.rb
@@ -30,21 +30,29 @@ def process(string, current_level=3, counters=[1])
30 30
       level_hash = ActiveSupport::OrderedHash.new
31 31
 
32 32
       while s.scan_until(%r{^h(\d)(?:\((#.*?)\))?\s*\.\s*(.*)$})
33  
-        level, idx, title = s[1].to_i, s[2], s[3].strip
  33
+        level, element_id, title = s[1].to_i, s[2], s[3].strip
34 34
 
35 35
         if level < current_level
36 36
           # This is needed. Go figure.
37 37
           return level_hash
38 38
         elsif level == current_level
39 39
           index = counters.dup
40  
-          index[0] = sprintf('%02d', index[0])
41  
-          idx ||= '#' + title_to_idx(title)
  40
+          if format = Guides.config['index_format']
  41
+            index[0] = sprintf(format, index[0])
  42
+          end
  43
+          index = index.join('.')
42 44
 
43  
-          raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{idx}). <span class='header_index'>#{index.join('.')}.</span> #{title}")
  45
+          element_id ||= '#' + title_to_element_id(title)
  46
+
  47
+          header_html = Guides.config['index_header'] || "{{index}} {{title}}"
  48
+          values = { 'index' => index, 'title' => title }
  49
+          header = header_html.gsub(/{{(.*?)}}/){ values[$1].to_str }
  50
+
  51
+          raise "Parsing Fail" unless @result.sub!(s.matched, "h#{level}(#{element_id}). #{header}")
44 52
 
45 53
           key = {
46 54
             :title => title,
47  
-            :id => idx
  55
+            :id => element_id
48 56
           }
49 57
           # Recurse
50 58
           counters << 1
@@ -59,12 +67,12 @@ def process(string, current_level=3, counters=[1])
59 67
       level_hash
60 68
     end
61 69
 
62  
-    def title_to_idx(title)
63  
-      idx = title.strip.parameterize.sub(/^\d+/, '')
64  
-      if warnings && idx.blank?
  70
+    def title_to_element_id(title)
  71
+      element_id = title.strip.parameterize.sub(/^\d+/, '')
  72
+      if warnings && element_id.blank?
65 73
         puts "BLANK ID: please put an explicit ID for section #{title}, as in h5(#my-id)"
66 74
       end
67  
-      idx
  75
+      element_id
68 76
     end
69 77
   end
70 78
 end

0 notes on commit e296e52

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