|
| 1 | +module Docs |
| 2 | + class Reactivex |
| 3 | + class CleanHtmlFilter < Filter |
| 4 | + def call |
| 5 | + # Can't use options[:container] because then the navigation bar wouldn't be scraped |
| 6 | + @doc = at_css(root_page? ? '.col-md-8' : '.col-sm-8') |
| 7 | + |
| 8 | + # Remove breadcrumbs |
| 9 | + css('.breadcrumb').remove |
| 10 | + |
| 11 | + # Replace interactive demo's with links to them |
| 12 | + css('rx-marbles').each do |node| |
| 13 | + node.name = 'a' |
| 14 | + node.content = 'Open interactive diagram on rxmarbles.com' |
| 15 | + node['href'] = "https://rxmarbles.com/##{node['key']}" |
| 16 | + node.remove_attribute('key') |
| 17 | + end |
| 18 | + |
| 19 | + # Syntax-highlighted code blocks |
| 20 | + css('.code').each do |node| |
| 21 | + language = node['class'].gsub('code', '').strip |
| 22 | + |
| 23 | + pre = node.at_css('pre') |
| 24 | + pre['data-language'] = language |
| 25 | + pre.content = pre.content.strip |
| 26 | + |
| 27 | + node.replace(pre) |
| 28 | + end |
| 29 | + |
| 30 | + # Assume JavaScript syntax for code blocks not surrounded by a div.code |
| 31 | + css('pre').each do |node| |
| 32 | + next if node['data-language'] |
| 33 | + |
| 34 | + node.content = node.content.strip |
| 35 | + node['data-language'] = 'javascript' |
| 36 | + end |
| 37 | + |
| 38 | + # Make language specific implementation titles prettier |
| 39 | + css('.panel-title').each do |node| |
| 40 | + # Remove the link, keep the text |
| 41 | + node.content = node.content |
| 42 | + |
| 43 | + # Transform it into a header for better styling |
| 44 | + node.name = 'h3' |
| 45 | + end |
| 46 | + |
| 47 | + # Remove language specific implementations that are TBD |
| 48 | + css('span').each do |node| |
| 49 | + next unless node.content == 'TBD' |
| 50 | + node.xpath('./ancestor::div[contains(@class, "panel-default")][1]').remove |
| 51 | + end |
| 52 | + |
| 53 | + # Remove the : at the end of "Language-Specific Information:" |
| 54 | + css('h2').each do |node| |
| 55 | + node.inner_html = node.inner_html.gsub('Information:', 'Information') |
| 56 | + end |
| 57 | + |
| 58 | + # Remove attributes to reduce file size |
| 59 | + css('div').remove_attr('class') |
| 60 | + |
| 61 | + doc |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments