@@ -4,16 +4,16 @@ class CleanHtmlFilter < Filter
44 def call
55 @doc = at_css ( '> div[id]' ) if at_css ( '> div[id]' )
66 # Remove the navigation header and footer and the lines underneath and above it
7- at_css ( '.header + hr' ) . remove
7+ at_css ( '.nav-panel + hr' ) . try ( : remove)
88 line_above = at_xpath ( '//div[@class="header"]/preceding::hr[1]' )
99 line_above . remove unless line_above . nil?
10- css ( '.header ' ) . remove
10+ css ( '.nav-panel ' ) . remove
1111
12- css ( '.copiable-anchor' ) . remove
12+ css ( '.copiable-anchor' , '.copiable-link' ) . remove
1313
1414 # Remove chapter and section numbers from title
1515 title_node = at_css ( 'h1, h2, h3, h4, h5, h6' )
16- title_node . content = title_node . content . gsub ( /(\d +\. ?)+/ , '' ) . strip
16+ title_node . content = title_node . content . gsub ( /(\d +\. ?)+/ , '' ) . gsub ( '¶' , '' ) . strip
1717 title_node . name = 'h1'
1818
1919 # Remove the "D. " from names like "D. Concept Index" and "D. Function Index"
@@ -23,39 +23,32 @@ def call
2323 # In the original reference they are used to add width between two columns
2424 xpath ( '//td[text()=" " and not(descendant::*)]' ) . remove
2525
26- # Add id's to additional entry nodes
27- css ( 'dl > dt > code' ) . each do |node |
28- # Only take the direct text (i.e. "<div>Hello <span>World</span></div>" becomes "Hello")
29- node [ 'id' ] = node . xpath ( 'text()' ) . to_s . strip
30- end
31-
32- # Fix hashes of index entries so they link to the correct hash on the linked page
33- css ( 'table[class^=index-] td[valign=top] > a' ) . each do |node |
34- path = node [ 'href' ] . split ( '#' ) [ 0 ]
35- hash = node . content
36-
37- # Fix the index entries linking to the Special Parameters page
38- # There are multiple index entries that should link to the same paragraph on that page
39- # Example: the documentation for "$!" is equal to the documentation for "!"
40- if path . downcase . include? ( 'special-parameters' )
41- if hash . size > 1 && hash [ 0 ] == '$'
42- hash = hash [ 1 ..-1 ]
43- end
26+ # The manual marks index targets with empty anchors (<a id="index-…"></a>), which the
27+ # clean_text filter strips. Move their ids onto the enclosing node so the index entries
28+ # keep linking to the right paragraph.
29+ css ( 'a[id]' ) . each do |node |
30+ next unless node . content . strip . empty?
31+ parent = node . parent
32+ if parent && parent [ 'id' ] . blank?
33+ parent [ 'id' ] = node [ 'id' ]
34+ else
35+ # Several anchors can share a parent; keep the extra ones alive by giving
36+ # them content the clean_text filter doesn't consider empty
37+ node . content = ""
4438 end
45-
46- node [ 'href' ] = path + '#' + hash
47- end
48-
49- # Fix index table letter hashes (the "Jump to" hashes)
50- css ( 'table[class^=index-] th > a' ) . each do |node |
51- node [ 'id' ] = node [ 'name' ]
5239 end
5340
5441 # Remove the rows with a horizontal line in them from the index tables
55- css ( 'td[colspan="4"]' ) . remove
42+ css ( 'td[colspan="3"]' ) . remove
43+
44+ # Remove the empty spacer cells of the index tables
45+ css ( 'td.printindex-index-entry' ) . each do |node |
46+ previous = node . previous_element
47+ previous . remove if previous && previous . name == 'td' && previous . content . strip . empty?
48+ end
5649
5750 # Remove additional text from menu entry and index entry cells
58- css ( 'td[valign=top] ' ) . each do |node |
51+ css ( 'td.printindex-index-entry, td.printindex-index-section ' ) . each do |node |
5952 link = node . at_css ( 'a' )
6053 node . children = link unless link . nil?
6154 end
0 commit comments