Class: Docs::Python::CleanHtmlFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/docs/filters/python/clean_html.rb

Constant Summary

Constants inherited from Filter

Filter::SCHEME_RGX

Instance Method Summary collapse

Methods inherited from Filter

#absolute_url_string?, #at_css, #at_xpath, #base_url, #css, #current_url, #fragment_url_string?, #initial_page?, #parse_html, #relative_url_string?, #root_page?, #root_path, #root_url, #slug, #subpath, #subpath_to, #xpath

Instance Method Details

#callObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/docs/filters/python/clean_html.rb', line 4

def call
  @doc = at_css '.body > .section'

  # 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

#otherObject


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
# File 'lib/docs/filters/python/clean_html.rb', line 28

def other
  css('.headerlink', 'hr').remove

  # Clean headings

  at_css('h1').tap do |node|
    node.content = node.content.sub!(/\A[\d\.]+/) { |str| @levelRegexp = /\A#{str}/; '' }
  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('.highlight-python3').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

#rootObject


23
24
25
26
# File 'lib/docs/filters/python/clean_html.rb', line 23

def root
  at_css('h1').content = 'Python'
  css('> p').remove
end