Repository navigation
Expand file tree
/
Copy pathclean_html.rb
More file actions
76 lines (61 loc) · 1.75 KB
/
Copy pathclean_html.rb
File metadata and controls
76 lines (61 loc) · 1.75 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
module Docs
class Python
class CleanHtmlFilter < Filter
def call
@doc = at_css '.body'
css('> .section').each do |node|
node.before(node.children).remove
end
# Clean inline code elements
css('tt.literal').each do |node|
node.before(node.children).remove
end
css('tt', 'span.pre').each do |node|
node.name = 'code'
node.remove_attribute 'class'
end
root_page? ? root : other
doc
end
def root
at_css('h1').content = 'Python'
css('> p').remove
end
def other
css('.headerlink', 'hr').remove
# Clean headings
css('h1').each do |node|
node.content = node.content.sub!(/\A[\d\.]+/) do |str|
rgx = /\A#{str}/
@levelRegexp = @levelRegexp ? Regexp.union(@levelRegexp, rgx) : rgx
''
end
end
css('h2', 'h3', 'h4').each do |node|
node.css('a').each do |link|
link.before(link.children).remove
end
node.child.content = node.child.content.remove @levelRegexp
end
css('dt').each do |node|
node.content = node.content
end
# Remove blockquotes
css('blockquote').each do |node|
node.before(node.children).remove
end
# Remove code highlighting
css('[class*="highlight-python"]').each do |node|
pre = node.at_css('pre')
pre.content = pre.content
pre['class'] = 'python'
node.replace(pre)
end
# Remove <table> border attribute
css('table[border]').each do |node|
node.remove_attribute 'border'
end
end
end
end
end