Repository navigation
Expand file tree
/
Copy pathclean_html.rb
More file actions
85 lines (73 loc) · 2.49 KB
/
Copy pathclean_html.rb
File metadata and controls
85 lines (73 loc) · 2.49 KB
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
module Docs
class Gnuplot
class CleanHtmlFilter < Filter
def call
# remove some anchors nested inside headers: <hX><a name="">...</a></hX>
css('h1, h2, h3, h4, h5').each do |heading|
anchor = heading.css('a')[0]
heading['id'] = anchor['id'] || anchor['name']
heading.content = anchor.content.strip
end
# make the title on the front page which is in some weird tags
if root_page?
title = css('.HUGE')[0]
title.name = 'h1'
subtitle = css('.XLARGE')[0]
title.content = title.content + ' − ' + subtitle.content
css('> *:first-child')[0].before(title)
subtitle.remove
css('p:contains("TableOfContents")').remove
end
# remove nav, empty items, and any useless horizontal rules as well
# as the subsection table of contents (.ChildLinks)
css('.navigation').remove
css('#CHILD_LINKS, ul.ChildLinks').remove
css('hr').remove
css('br').remove
# Anchors that use only names are some numerical IDs that latex2html distributes through the document
css('a[name]:not([href]):not([id])').remove
# spacing
css('> div, p').each do |node|
node.remove if node.content.strip.empty?
end
css('pre').each do |node|
node.content = dedent(node.content)
end
# links generated are of the form (NB: some might have been removed):
# <B>{text} (p. <A HREF="{target}"><IMG ALT="[*]" SRC="crossref.png"></A>)<A NAME="{anchor}"></A></B>
# transform to <b><a href="{target}>{text}</a></b>
css('b:contains(" (p. ")').each do |node|
text = node.content.gsub /\(p\. (\[\*\])?\)/, ''
link = node.css('a[href]')[0]
if link
link.content = text.strip
node.children.each do |child|
child.remove if child != link
end
else
node.content = text.strip
end
end
doc
end
private
def dedent string
lines = string.split "\n"
indent = lines.reduce Float::INFINITY do |least, line|
if line == ''
least
else
[least, line.index(line.lstrip)].min
end
end
if indent == Float::INFINITY
string
else
lines
.map { |line| line[indent..] || '' }
.join("\n")
end
end
end
end
end