Here is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | require "hpricot" # HTML Outliner 0.4 # Copyright Christoffer Sawicki 2006 # Licensed under the same terms as Ruby # Please send bug reports and improvements to # christoffer.sawicki@gmail.com # See the test cases at the bottom for usage # Changes # * 0.4: An element can only have one id, # so pass the current id to the slugifier. class HTMLOutliner class SimpleTree attr_accessor :root, :subtrees def initialize(root, subtrees = []) @root = root @subtrees = subtrees end end end class HTMLOutliner def initialize(hpricot_doc) @doc = hpricot_doc end def add_header_anchors!(slugifier = method(:default_slugifier)) headers.each_with_index do |header, index| header[:id] = slugifier.call(header.inner_html, index, header[:id]) end end def headers @headers ||= find_headers end def outline @outline ||= build_outline end protected def default_slugifier(string, index, current_id) # Just overwrite the current id string.downcase.gsub(/s+/, "_") + "_section" end private def find_headers find_headers_helper(@doc.children) end def find_headers_helper(nodes) nodes.inject([]) do |sum, node| if node.is_a? Hpricot::Text sum elsif node.name.match(/^h[1-6]$/i) sum + [ node ] + find_headers_helper(node.children) else sum + find_headers_helper(node.children) end end end def build_outline # TODO: This code needs some lovin' :-) result = [] tree_stack = [] headers.each do |header| new_tree = SimpleTree.new(header) until tree_stack.empty? || tree_stack.last.root.name < header.name tree_stack.pop end if tree_stack.empty? result.push(new_tree) tree_stack.push(new_tree) else tree_stack.last.subtrees.push(new_tree) tree_stack.push(new_tree) end end return result end end if __FILE__ == $0 require "test/unit" class HTMLOutlinerTest < Test::Unit::TestCase def test_construction_with_root assert_nothing_raised do HTMLOutliner.new(Hpricot("<a></a>")).headers end end def test_construction_without_root assert_nothing_raised do HTMLOutliner.new(Hpricot("<a></a><b></b>")).headers end end def test_headers input = <<-END <body> <h1 id="toc-hello">Hello</h1> <h1 id="toc-world">World</h1> </body> END doc = Hpricot(input) outliner = HTMLOutliner.new(doc) assert_equal 2, outliner.headers.size assert_equal "Hello", outliner.headers[0].inner_html assert_equal "World", outliner.headers[1].inner_html end def test_add_anchors input = <<-END <body> <h1 id="toc-hello1">Hello</h1> <h1 id="toc-world1">World</h1> </body> END expected_output = <<-END <body> <h1 id="toc-hello2" id="hello_section">Hello</h1> <h1 id="toc-world2" id="world_section">World</h1> </body> END doc = Hpricot(input) outliner = HTMLOutliner.new(doc) outliner.add_header_anchors! assert_equal(expected_output, doc.to_s) end def test_add_anchors_with_existing_id input = <<-END <body> <h1 id="toc-hello3" id="moo">Hello</h1> </body> END expected_output = <<-END <body> <h1 id="toc-hello4" id="hello_section">Hello</h1> </body> END doc = Hpricot(input) outliner = HTMLOutliner.new(doc) outliner.add_header_anchors! assert_equal(expected_output, doc.to_s) end def test_add_anchors_with_custom_slugifier input = <<-END <body> <h1 id="toc-hello5">Hello</h1> </body> END expected_output = <<-END <body> <h1 id="toc-hello6" id="chapter1">Hello</h1> </body> END doc = Hpricot(input) outliner = HTMLOutliner.new(doc) slugifier = lambda { |x, i, _| "chapter#{i + 1}" } outliner.add_header_anchors!(slugifier) assert_equal(expected_output, doc.to_s) end def test_outline doc = Hpricot(<<-END) <body> <h1 id="toc-a">A</h1> <h1 id="toc-b">B</h1> <h2 id="toc-c">C</h2> <h3 id="toc-d">D</h3> <h2 id="toc-e">E</h2> </body> END outline = HTMLOutliner.new(doc).outline assert_equal 2, outline.size assert_equal 0, outline[0].subtrees.size assert_equal 2, outline[1].subtrees.size assert_equal 1, outline[1].subtrees[0].subtrees.size assert_equal 0, outline[1].subtrees[0].subtrees[0].subtrees.size assert_equal 0, outline[1].subtrees[1].subtrees.size assert_equal "A", outline[0].root.inner_html assert_equal "B", outline[1].root.inner_html assert_equal "C", outline[1].subtrees[0].root.inner_html assert_equal "D", outline[1].subtrees[0].subtrees[0].root.inner_html assert_equal "E", outline[1].subtrees[1].root.inner_html end &nbs |