History

Add Python documentation

Thibaut committed 12 years ago

Commit 36d6a101 parent 587855115 files · +220 −0Browse files

Showing the changes to lib/docs/filters/python/clean_html.rb (1 of 15 files). Show all

Unified · Split

lib/docs/filters/python/clean_html.rb+68−0
@@ -0,0 +1,68 @@
1+module Docs
2+ class Python
3+ class CleanHtmlFilter < Filter
4+ def call
5+ @doc = at_css '.body > .section'
6+
7+ # Clean inline code elements
8+
9+ css('tt.literal').each do |node|
10+ node.before(node.children).remove
11+ end
12+
13+ css('tt', 'span.pre').each do |node|
14+ node.name = 'code'
15+ node.remove_attribute 'class'
16+ end
17+
18+ root_page? ? root : other
19+
20+ doc
21+ end
22+
23+ def root
24+ at_css('h1').content = 'Python'
25+ css('> p').remove
26+ end
27+
28+ def other
29+ css('.headerlink', 'hr').remove
30+
31+ # Clean headings
32+
33+ at_css('h1').tap do |node|
34+ node.content = node.content.sub!(/\A[\d\.]+/) { |str| @levelRegexp = /\A#{str}/; '' }
35+ end
36+
37+ css('h2', 'h3', 'h4').each do |node|
38+ node.css('a').each do |link|
39+ link.before(link.children).remove
40+ end
41+ node.child.content = node.child.content.sub @levelRegexp, ''
42+ end
43+
44+ css('dt').each do |node|
45+ node.content = node.content
46+ end
47+
48+ # Remove blockquotes
49+ css('blockquote').each do |node|
50+ node.before(node.children).remove
51+ end
52+
53+ # Remove code highlighting
54+ css('.highlight-python3').each do |node|
55+ pre = node.at_css('pre')
56+ pre.content = pre.content
57+ pre['class'] = 'python'
58+ node.replace(pre)
59+ end
60+
61+ # Remove <table> border attribute
62+ css('table[border]').each do |node|
63+ node.remove_attribute 'border'
64+ end
65+ end
66+ end
67+ end
68+end