|
| 1 | +module Docs |
| 2 | + class Rxjs |
| 3 | + class CleanHtmlFilter < Filter |
| 4 | + def call |
| 5 | + if root_page? |
| 6 | + css('.card-container').remove |
| 7 | + at_css('h1').content = 'RxJS Documentation' |
| 8 | + end |
| 9 | + |
| 10 | + if at_css('h1').nil? |
| 11 | + title = subpath.rpartition('/').last.titleize |
| 12 | + doc.prepend_child("<h1>#{title}</h1>") |
| 13 | + end |
| 14 | + |
| 15 | + css('br', 'hr', '.material-icons', '.header-link', '.breadcrumb').remove |
| 16 | + |
| 17 | + css('.content', 'article', '.api-header', 'section', '.instance-member').each do |node| |
| 18 | + node.before(node.children).remove |
| 19 | + end |
| 20 | + |
| 21 | + css('label', 'h2 > em', 'h3 > em').each do |node| |
| 22 | + node.name = 'code' |
| 23 | + end |
| 24 | + |
| 25 | + css('h1 + code').each do |node| |
| 26 | + node.before('<p></p>') |
| 27 | + while node.next_element.name == 'code' |
| 28 | + node.previous_element << ' ' |
| 29 | + node.previous_element << node.next_element |
| 30 | + end |
| 31 | + node.previous_element.prepend_child(node) |
| 32 | + end |
| 33 | + |
| 34 | + css('td h3', '.l-sub-section > h3', '.alert h3', '.row-margin > h3', '.api-heading ~ h3', '.api-heading + h2', '.metadata-member h3').each do |node| |
| 35 | + node.name = 'h4' |
| 36 | + end |
| 37 | + |
| 38 | + css('.l-sub-section', '.alert', '.banner').each do |node| |
| 39 | + node.name = 'blockquote' |
| 40 | + end |
| 41 | + |
| 42 | + css('.file').each do |node| |
| 43 | + node.content = node.content.strip |
| 44 | + end |
| 45 | + |
| 46 | + css('.filetree .children').each do |node| |
| 47 | + node.css('.file').each do |n| |
| 48 | + n.content = " #{n.content}" |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + css('.filetree').each do |node| |
| 53 | + node.content = node.css('.file').map(&:inner_html).join("\n") |
| 54 | + node.name = 'pre' |
| 55 | + node.remove_attribute('class') |
| 56 | + end |
| 57 | + |
| 58 | + css('pre').each do |node| |
| 59 | + node.content = node.content.strip |
| 60 | + |
| 61 | + node['data-language'] = 'typescript' if node['path'].try(:ends_with?, '.ts') |
| 62 | + node['data-language'] = 'html' if node['path'].try(:ends_with?, '.html') |
| 63 | + node['data-language'] = 'css' if node['path'].try(:ends_with?, '.css') |
| 64 | + node['data-language'] = 'js' if node['path'].try(:ends_with?, '.js') |
| 65 | + node['data-language'] = 'json' if node['path'].try(:ends_with?, '.json') |
| 66 | + node['data-language'] = node['language'].sub(/\Ats/, 'typescript').strip if node['language'] |
| 67 | + node['data-language'] ||= 'typescript' if node.content.start_with?('@') |
| 68 | + |
| 69 | + node.before(%(<div class="pre-title">#{node['title']}</div>)) if node['title'] |
| 70 | + |
| 71 | + if node['class'] && node['class'].include?('api-heading') |
| 72 | + node.name = 'h3' |
| 73 | + |
| 74 | + unless node.ancestors('.instance-method').empty? |
| 75 | + matches = node.inner_html.scan(/([^(& ]+)[(&]/) |
| 76 | + |
| 77 | + unless matches.empty? || matches[0][0] == 'constructor' |
| 78 | + node['name'] = matches[0][0] |
| 79 | + node['id'] = node['name'].downcase + '-' |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + node.inner_html = "<code>#{node.inner_html}</code>" |
| 84 | + end |
| 85 | + |
| 86 | + node.remove_attribute('path') |
| 87 | + node.remove_attribute('region') |
| 88 | + node.remove_attribute('linenums') |
| 89 | + node.remove_attribute('title') |
| 90 | + node.remove_attribute('language') |
| 91 | + node.remove_attribute('hidecopy') |
| 92 | + node.remove_attribute('class') |
| 93 | + end |
| 94 | + |
| 95 | + css('td > .overloads').each do |node| |
| 96 | + node.replace node.at_css('.detail-contents') |
| 97 | + end |
| 98 | + |
| 99 | + css('td.short-description p').each do |node| |
| 100 | + signature = node.parent.parent.next_element.at_css('h3[id]') |
| 101 | + signature.after(node) unless signature.nil? |
| 102 | + end |
| 103 | + |
| 104 | + css('.method-table').each do |node| |
| 105 | + node.replace node.at_css('tbody') |
| 106 | + end |
| 107 | + |
| 108 | + css('.api-body > table > caption').each do |node| |
| 109 | + node.name = 'center' |
| 110 | + lift_out_of_table node |
| 111 | + end |
| 112 | + |
| 113 | + css('.api-body > table > tbody > tr:not([class]) > td > *').each do |node| |
| 114 | + lift_out_of_table node |
| 115 | + end |
| 116 | + |
| 117 | + css('.api-body > table').each do |node| |
| 118 | + node.remove if node.content.strip.blank? |
| 119 | + end |
| 120 | + |
| 121 | + css('h1[class]').remove_attr('class') |
| 122 | + css('table[class]').remove_attr('class') |
| 123 | + css('table[width]').remove_attr('width') |
| 124 | + css('tr[style]').remove_attr('style') |
| 125 | + |
| 126 | + css('code code').each do |node| |
| 127 | + node.before(node.children).remove |
| 128 | + end |
| 129 | + |
| 130 | + doc |
| 131 | + end |
| 132 | + |
| 133 | + def lift_out_of_table(node) |
| 134 | + table = node.ancestors('table').first |
| 135 | + table.previous_element.after(node) |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | +end |
0 commit comments