Class: Docs::Http::CleanHtmlFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/docs/filters/http/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
# File 'lib/docs/filters/http/clean_html.rb', line 4

def call
  root_page? ? root : other
  doc
end

#otherObject


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

def other
  at_css('address').remove

  # Change title
  title = at_css 'h2'
  title.name = 'h1'
  title.at_css('a').remove
  title.content = "HTTP #{title.content}"

  # Update headings
  css('h3').each do |node|
    link = node.at_css('a')
    node.name = "h#{link.content.count('.') + 1}"
    node['id'] = link['id']
    link.remove
  end

  # Merge adjacent <pre> tags and remove indentation
  css('pre').each do |node|
    while (sibling = node.next_element) && sibling.name == 'pre'
      node.inner_html += "\n#{sibling.inner_html}"
      sibling.remove
    end
    node.inner_html = node.inner_html.strip_heredoc
  end
end

#rootObject


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/docs/filters/http/clean_html.rb', line 9

def root
  # Change title
  title = at_css 'h2'
  title.name = 'h1'
  title.inner_html = 'Hypertext Transfer Protocol &mdash; HTTP/1.1'

  # Remove "..." following each link
  css('span').each do |node|
    node.inner_html = node.first_element_child if node.first_element_child
  end
end