Class: Webby::Filters::Outline
- Inherits:
-
Object
- Object
- Webby::Filters::Outline
- Defined in:
- lib/webby/filters/outline.rb
Overview
The Outline filter is used to insert outline numbering into HTML heading tags (h1, h2, h3, etc.) and to generate a table of contents based on the heading tags. The table of contents is inserted into the page at the location of the <toc /> tag. If there is no <toc /> tag, then a table of contents will not be created but outline numbering will still take place.
If a table of contents is desired without outline number being inserted into the heading tags, this can be specified in the attibutes of the <toc /> tag itself.
<toc outline_numbering="off" />
This will generate a table of contents, but not insert outline numbering into the heading tags.
The Outline filter will only work on valid HTML or XHTML pages. Therefore it should be used after any markup langauge filters (textile, markdown, etc.).
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
-
#filter ⇒ Object
call-seq: filter => html.
-
#initialize(str) ⇒ Outline
constructor
call-seq: Outline.new( html ).
Constructor Details
#initialize(str) ⇒ Outline
call-seq:
Outline.new( html )
Creates a new outline filter that will operate on the given html string.
37 38 39 40 41 42 43 44 45 |
# File 'lib/webby/filters/outline.rb', line 37 def initialize( str ) @str = str @cur_level, @base_level, @cur_depth = nil @level = [0] * 6 @h_rgxp = %r/^h(\d)$/o @toc = [] @outline_numbering = true end |
Instance Method Details
#filter ⇒ Object
call-seq:
filter => html
Process the original html document passed to the filter when it was created. The document will be scanned for heading tags (h1, h2, etc.) and outline numbering and id attributes will be inserted. A table of contents will also be created and inserted into the page if a <toc /> tag is found.
For example, if there is a heading tag
<h3>Get Fuzzy</h3>
somewhere in a page about comic strips, the tag might be altered as such
<h3 id="h2_2_1"><span class="heading-num>2.2.1</span>Get Fuzzy</h3>
The id attribute is used to generate a linke from the table of contents to this particular heading tag. The original text of the tag is used in the table of contents – “Get Fuzzy” in this example.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/webby/filters/outline.rb', line 68 def filter doc = Hpricot(@str) # extract directives from the "toc" tag toc_elem = doc.search('toc').first unless toc_elem.nil? @outline_numbering = toc_elem['outline_numbering'] !~ %r/off/i end doc.traverse_element(*%w[h1 h2 h3 h4 h5 h6]) do |elem| text, id = heading_info(elem) add_to_toc(text, id) end # create the TOC ordered list toc_elem.swap(toc) unless toc_elem.nil? # replace the "toc" tag with the ordered list doc.to_html end |