|
| 1 | +module Docs |
| 2 | + class Dart |
| 3 | + class CleanHtmlFilter < Filter |
| 4 | + def call |
| 5 | + # Move the title into the main content node in the v1 docs |
| 6 | + title = at_css('h1.title') |
| 7 | + unless title.nil? |
| 8 | + name = title.children.last.content.strip |
| 9 | + kind = title.at_css('.kind').content |
| 10 | + at_css('.main-content').prepend_child("<h1>#{name} #{kind}</h1>") |
| 11 | + end |
| 12 | + |
| 13 | + # Add a title to the homepage of the v2 docs |
| 14 | + if subpath == 'index.html' && at_css('.main-content > h1').nil? |
| 15 | + at_css('.main-content').prepend_child('<h1>Dart SDK</h1>') |
| 16 | + end |
| 17 | + |
| 18 | + # Add the library to the main content (it is not always visible in the menu entry) |
| 19 | + breadcrumbs = at_css('.breadcrumbs').css('li:not(.self-crumb) > a') |
| 20 | + if breadcrumbs.length > 1 |
| 21 | + library = breadcrumbs[1].content |
| 22 | + |
| 23 | + # Generate the link to the homepage of the library |
| 24 | + with_hypens = library.gsub(/:/, '-') |
| 25 | + location = "#{'../' * subpath.count('/')}#{with_hypens}/#{with_hypens}-library" |
| 26 | + link = "<a href=\"#{location}\" class=\"_links-link\">#{library}</span>" |
| 27 | + |
| 28 | + # Add the link to the main title, just like how the "Homepage" and "Source code" links appear |
| 29 | + at_css('.main-content').prepend_child("<p class=\"_links\">#{link}</p>") |
| 30 | + end |
| 31 | + |
| 32 | + # Extract the actual content |
| 33 | + # We can't use options[:container] here because the entries filter uses the breadcrumbs node |
| 34 | + @doc = at_css('.main-content') |
| 35 | + |
| 36 | + # Move the features (i.e. "read-only, inherited") into the blue header |
| 37 | + css('.features').each do |node| |
| 38 | + header = node.xpath('parent::dd/preceding::dt').last |
| 39 | + header.add_child node unless header.nil? |
| 40 | + end |
| 41 | + |
| 42 | + # Make code blocks detectable by Prism |
| 43 | + css('pre').each do |node| |
| 44 | + node['data-language'] = 'dart' |
| 45 | + end |
| 46 | + |
| 47 | + doc |
| 48 | + end |
| 49 | + end |
| 50 | + end |
| 51 | +end |
0 commit comments