class Conversio::HTMLTableOfContent
Attributes
numbering[RW]
table_of_content_div_class[RW]
Public Class Methods
new(html_input)
click to toggle source
# File lib/conversio/htmltoc.rb 7 def initialize(html_input) 8 @numbering = true 9 @table_of_content_div_class = 'toc' 10 # Variables 11 @html_input = Array.new 12 @heading_elements = Array.new 13 html_input.split("\n").each { |l| @html_input << l } 14 scan_for_heading_elements() 15 numbers() 16 end
Public Instance Methods
get_html()
click to toggle source
# File lib/conversio/htmltoc.rb 40 def get_html() 41 return get_html_table_of_content() << "\n" << get_html_with_anchors 42 end
get_html_table_of_content()
click to toggle source
# File lib/conversio/htmltoc.rb 23 def get_html_table_of_content() 24 output = String.new 25 @heading_elements.each do |heading| 26 index, level, content, anchor, number = heading 27 level = level.to_i 28 next if level > 3 # only h1,h2, and h3 tags are used 29 space = ' ' 30 case level 31 when 2 then output << space 32 when 3 then output << space << space 33 end 34 content = "#{number} #{content}" if numbering? 35 output << %{<a href="##{anchor}">#{content}</a><br/>\n} 36 end 37 return %{<div class="#{@table_of_content_div_class}">\n#{output}</div>\n} 38 end
get_html_with_anchors()
click to toggle source
# File lib/conversio/htmltoc.rb 18 def get_html_with_anchors() 19 inject_anchors() 20 return @html_input.join("\n") 21 end
Protected Instance Methods
anchor(string)
click to toggle source
Transforms the input string into a valid XHTML anchor (ID attribute).
anchor("Text with spaces") # textwithspaces anchor("step 1 step 2 step: 3") # step1step2step3
# File lib/conversio/htmltoc.rb 65 def anchor(string) 66 alnum = String.new 67 string.gsub(/[[:alnum:]]/) { |c| alnum << c } 68 return alnum.downcase 69 end
inject_anchors()
click to toggle source
# File lib/conversio/htmltoc.rb 94 def inject_anchors() 95 @heading_elements.each do |heading| 96 line = String.new 97 index = heading[0] 98 level = heading[1].to_i 99 content = heading[2] 100 anchor = heading[3] 101 next if level > 3 # only h1,h2, and h3 tags are used 102 if numbering? then 103 number = heading[4] 104 line = %{<h#{level}><a name="#{anchor}"></a>#{number} #{content}</h#{level}>} 105 else 106 line = %{<h#{level}><a name="#{anchor}"></a>#{content}</h#{level}>} 107 end 108 @html_input[index] = line 109 end 110 end
numbering?()
click to toggle source
# File lib/conversio/htmltoc.rb 46 def numbering? 47 return @numbering 48 end
numbers()
click to toggle source
# File lib/conversio/htmltoc.rb 71 def numbers() 72 chapters = 0 73 sections = 0 74 subsections = 0 75 @heading_elements.each_index do |index| 76 level = @heading_elements[index][1].to_i 77 case level 78 when 1 79 chapters = chapters.next 80 @heading_elements[index] << "#{chapters}" 81 sections = 0 82 subsections = 0 83 when 2 84 sections = sections.next 85 @heading_elements[index] << "#{chapters}.#{sections}" 86 subsections = 0 87 when 3 88 subsections = subsections.next 89 @heading_elements[index] << "#{chapters}.#{sections}.#{subsections}" 90 end 91 end 92 end
scan_for_heading_elements()
click to toggle source
# File lib/conversio/htmltoc.rb 50 def scan_for_heading_elements() 51 @html_input.each_index do |index| 52 if @html_input[index] =~ %r{<h(\d)(.*?)>(.*?)</h\1>$}m then 53 # Pattern match values: 54 # $1 -- header tag level, e.g. <h2>...</h2> will be 2 55 # $3 -- content between the tags 56 @heading_elements << [index, $1, $3, anchor($3)] 57 end 58 end 59 end