Class: Docs::D3::CleanHtmlFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/docs/filters/d3/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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/docs/filters/d3/clean_html.rb', line 4

def call
  # Remove links inside <h2> and add "id" attributes
  css('h2 > a').each do |node|
    node.parent['id'] = node['name'].remove('user-content-') if node['name']
    node.before(node.children).remove
  end

  css('.markdown-body > blockquote:first-child', '.anchor').remove

  # Replace .gh-header with <h1>
  css('.gh-header-title').each do |node|
    node.parent.parent.before(node).remove
    node.content = 'D3.js' if root_page?
  end

  # Move content to the root-level
  css('#wiki-content').each do |node|
    node.before(node.at_css('.markdown-body').children).remove
  end

  # Make headings for function definitions and add "id" attributes
  css('p > a:first-child').each do |node|
    next unless node['name'] || node.content == '#'
    parent = node.parent
    parent.name = 'h6'
    parent['id'] = (node['name'] || node['href'].remove(/\A.+#/)).remove('user-content-')
    parent.css('a[name]').remove
    node.remove
  end

  # Fix internal links
  css('a[href]').each do |node|
    node['href'] = node['href'].sub(/#user\-content\-(\w+?)\z/, '#\1').sub(/#wiki\-(\w+?)\z/, '#\1')
  end

  # Remove code highlighting
  css('.highlight > pre').each do |node|
    node.content = node.content
  end

  doc
end