public
Rubygem
Description: A lightweight and flexible website management system.
Homepage: http://webby.rubyforge.org/
Clone URL: git://github.com/TwP/webby.git
updates for outline filter
TwP (author)
Fri Feb 15 13:09:47 -0800 2008
commit  4a2502cc4fc3e3bdd22767415827e4c3a6a6d96f
tree    17bfb7f55ae0953c3876fa589c70914e02d8339a
parent  1ec1da8f7a37a376c87ec79bb34164a4410d374f
...
1
 
2
3
 
4
5
 
 
6
7
8
...
 
1
2
 
3
4
5
6
7
8
9
10
0
@@ -1,8 +1,10 @@
0
-== 0.7.4 / 2008-
0
+== 0.7.4 / 2008-02-15
0
 
0
-* 2 minor enhancements
0
+* 3 minor enhancements
0
   - Added "limit" and "sort_by" options to the pages find method
0
   - Removed dependency on RedCloth for outline generation
0
+ - Added some more options for outline generation, numbering, and
0
+ table of contents generation
0
 * 1 bug fix
0
   - Fixed incompatibility with the new logging library
0
 
...
8
9
10
 
11
12
13
...
8
9
10
11
12
13
14
0
@@ -8,6 +8,7 @@ task :deploy => [:build, 'deploy:rsync']
0
 
0
 SITE.host = 'tim_pease@rubyforge.org'
0
 SITE.remote_dir = '/var/www/gforge-projects/webby/'
0
+SITE.create_mode = 'directory'
0
 
0
 task :autobuild => :growl
0
 
...
15
16
17
18
 
19
20
21
...
29
30
31
32
33
34
35
36
37
38
39
...
46
47
48
 
 
 
 
49
50
 
 
 
51
52
53
...
65
66
67
68
 
69
70
71
...
78
79
80
81
82
 
 
 
 
 
 
 
 
83
84
 
 
 
 
85
86
87
 
88
89
90
91
92
93
94
95
96
97
98
99
100
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
103
104
...
114
115
116
117
 
118
119
120
...
131
132
133
134
135
 
 
 
 
136
137
138
...
150
151
152
 
 
 
 
 
 
153
154
155
...
178
179
180
181
182
 
 
183
184
185
...
191
192
193
194
 
195
196
197
...
223
224
225
226
227
 
 
228
229
230
...
15
16
17
 
18
19
20
21
...
29
30
31
 
 
 
 
 
32
33
34
...
41
42
43
44
45
46
47
48
 
49
50
51
52
53
54
...
66
67
68
 
69
70
71
72
...
79
80
81
 
 
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 
98
99
100
 
 
101
 
 
 
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
134
135
136
 
137
138
139
140
...
151
152
153
 
 
154
155
156
157
158
159
160
...
172
173
174
175
176
177
178
179
180
181
182
183
...
206
207
208
 
 
209
210
211
212
213
...
219
220
221
 
222
223
224
225
...
251
252
253
 
 
254
255
256
257
258
0
@@ -15,7 +15,7 @@ module Filters
0
 # into the heading tags, this can be specified in the attibutes of the
0
 # <toc /> tag itself.
0
 #
0
-# <toc outline_numbering="off" />
0
+# <toc numbering="off" />
0
 #
0
 # This will generate a table of contents, but not insert outline numbering
0
 # into the heading tags.
0
@@ -29,11 +29,6 @@ class Outline
0
 
0
   class Error < StandardError; end # :nodoc:
0
 
0
- # TODO: options for table of contents
0
- # - where to start numbering
0
- # - list style (numbered or unordered)
0
- # - header range to select (h2-h3)
0
-
0
   # call-seq:
0
   # Outline.new( html )
0
   #
0
@@ -46,8 +41,14 @@ class Outline
0
     @cur_level, @base_level, @cur_depth = nil
0
     @level = [0] * 6
0
     @h_rgxp = %r/^h(\d)$/o
0
+
0
+ @numbering = true
0
+ @numbering_start = 1
0
+
0
     @toc = []
0
- @outline_numbering = true
0
+ @toc_style = 'ol'
0
+ @toc_range = 'h1-h6'
0
+ @list_opening = nil
0
   end
0
 
0
   # call-seq:
0
@@ -65,7 +66,7 @@ class Outline
0
   #
0
   # somewhere in a page about comic strips, the tag might be altered as such
0
   #
0
- # <h3 id="h2_2_1"><span class="heading-num>2.2.1</span>Get Fuzzy</h3>
0
+ # <h3 id="h2_2_1"><span class="heading-num">2.2.1</span>Get Fuzzy</h3>
0
   #
0
   # The id attribute is used to generate a linke from the table of contents
0
   # to this particular heading tag. The original text of the tag is used in
0
@@ -78,27 +79,46 @@ class Outline
0
     toc_elem = doc.search('toc').first
0
 
0
     unless toc_elem.nil?
0
- @outline_numbering = toc_elem['outline_numbering'] !~ %r/off/i
0
- @list_style = toc_elem['list'] || 'ol'
0
+ @numbering = toc_elem['numbering'] !~ %r/off/i
0
+ @numbering_start = Integer(toc_elem['numbering_start']) if toc_elem.has_attribute? 'numbering_start'
0
+ @toc_style = toc_elem['toc_style'] if toc_elem.has_attribute? 'toc_style'
0
+ @toc_range = toc_elem['toc_range'] if toc_elem.has_attribute? 'toc_range'
0
+ end
0
+
0
+ unless %w[ul ol].include? @toc_style
0
+ raise ArgumentError, "unknown ToC list type '#{@toc_style}'"
0
     end
0
 
0
+ m = %r/h(\d)\s*-\s*h(\d)/i.match @toc_range
0
+ @toc_range = Integer(m[1])..Integer(m[2])
0
+ @list_opening = build_list_opening(toc_elem)
0
+
0
     doc.traverse_element(*%w[h1 h2 h3 h4 h5 h6]) do |elem|
0
       text, id = heading_info(elem)
0
- add_to_toc(text, id)
0
+ add_to_toc(text, id) if @toc_range.include? current_level
0
     end
0
 
0
- # create the TOC ordered list
0
-
0
     toc_elem.swap(toc) unless toc_elem.nil?
0
-
0
- # replace the "toc" tag with the ordered list
0
-
0
     doc.to_html
0
   end
0
 
0
 
0
   private
0
 
0
+ def build_list_opening( elem )
0
+ lo = "<#{@toc_style}"
0
+ unless elem.nil?
0
+ %w[class style id].each do |atr|
0
+ next unless elem.has_attribute? atr
0
+ lo << " %s=\"%s\"" % [atr, elem[atr]]
0
+ end
0
+ end
0
+ if @toc_style == 'ol' and @numbering_start != 1
0
+ lo << " start=\"#{@numbering_start}\""
0
+ end
0
+ lo << ">"
0
+ end
0
+
0
   # Returns information for the given heading element. The information is
0
   # returned as a two element array: [text, id].
0
   #
0
@@ -114,7 +134,7 @@ class Outline
0
     text = elem.inner_text
0
 
0
     lbl = label
0
- if outline_numbering?
0
+ if numbering?
0
       elem.children.first.before {tag!(:span, lbl, :class => 'heading-num')}
0
     end
0
     elem['id'] = "h#{lbl.tr('.','_')}" if elem['id'].nil?
0
@@ -131,8 +151,10 @@ class Outline
0
   # heading level.
0
   #
0
   def current_level=( level )
0
- @base_level ||= level
0
- @cur_level ||= level
0
+ if @base_level.nil?
0
+ @base_level = @cur_level = level
0
+ @level[@base_level-1] = @numbering_start-1
0
+ end
0
 
0
     if level < @base_level
0
       raise Error, "heading tags are not in order, cannot outline"
0
@@ -150,6 +172,12 @@ class Outline
0
     @cur_level = level
0
   end
0
 
0
+ # Returns the current heading level number.
0
+ #
0
+ def current_level
0
+ @cur_level
0
+ end
0
+
0
   # Return the label string for the current heading level.
0
   #
0
   def label
0
@@ -178,8 +206,8 @@ class Outline
0
   def toc
0
     ary = []
0
 
0
- lopen = "<#@list_style>"
0
- lclose = "</#@list_style>"
0
+ lopen = "<#@toc_style>"
0
+ lclose = "</#@toc_style>"
0
     prev_depth = open = 0
0
 
0
     @toc.each do |a|
0
@@ -191,7 +219,7 @@ class Outline
0
 
0
       # if we are increasing the level, then start a new list
0
       elsif cur > prev_depth
0
- ary << lopen
0
+ ary << if ary.empty? then @list_opening else lopen end
0
         open += 1
0
 
0
       # we are decreasing the level; close out tags but ensure we don't
0
@@ -223,8 +251,8 @@ class Outline
0
   # Returns +true+ if outline numbering should be inserted into the heading
0
   # tags. Returns +false+ otherwise.
0
   #
0
- def outline_numbering?
0
- @outline_numbering
0
+ def numbering?
0
+ @numbering
0
   end
0
 end # class Outline
0
 

Comments

    No one has commented yet.