|
| 1 | +module Docs |
| 2 | + class Bootstrap |
| 3 | + class CleanHtmlV6Filter < Filter |
| 4 | + def call |
| 5 | + |
| 6 | + @doc = at_css('main') |
| 7 | + at_css('.bd-content').prepend_child(at_css('h1').remove) |
| 8 | + @doc = at_css('.bd-content') |
| 9 | + |
| 10 | + at_css('h1').content = 'Bootstrap' if root_page? |
| 11 | + |
| 12 | + # Live examples and demo dialogs |
| 13 | + css('.bd-example', 'dialog', 'script', 'button[data-bs-toggle]').remove |
| 14 | + |
| 15 | + # Interactive playgrounds, swatches and prev/next navigation |
| 16 | + css('.bd-links-nav', '.bd-colors-grid', '.bd-playground', '.bd-scrollspy-debug', 'svg:has(symbol)').remove |
| 17 | + |
| 18 | + css('.anchor-link', '.bd-details-icon').remove |
| 19 | + |
| 20 | + css('.bd-code-snippet').each do |node| |
| 21 | + label = node.at_css('.highlight-toolbar .font-monospace')&.content&.strip&.downcase |
| 22 | + tabs = node.css('.code-tab-btn').map { |btn| btn.content.strip } |
| 23 | + |
| 24 | + html = node.css('pre').each_with_index.map do |pre, i| |
| 25 | + out = Nokogiri::XML::Node.new('pre', doc) |
| 26 | + lang = tabs.empty? ? language(label) : 'shell' |
| 27 | + out['data-language'] = lang if lang |
| 28 | + out.content = pre.content |
| 29 | + tabs[i] ? "<p><strong>#{tabs[i]}</strong></p>#{out.to_html}" : out.to_html |
| 30 | + end.join |
| 31 | + |
| 32 | + node.replace(html) |
| 33 | + end |
| 34 | + |
| 35 | + # Remaining live demos (option pickers etc.) |
| 36 | + doc.element_children.each do |node| |
| 37 | + node.remove if node.name == 'div' && node.at_css('button, input, select') |
| 38 | + end |
| 39 | + |
| 40 | + css('.bd-callout').each do |node| |
| 41 | + node.name = 'blockquote' |
| 42 | + end |
| 43 | + |
| 44 | + css('table').each do |node| |
| 45 | + node.remove_attribute('class') |
| 46 | + end |
| 47 | + |
| 48 | + css('td[data-cell]').remove_attr('data-cell') |
| 49 | + |
| 50 | + doc |
| 51 | + |
| 52 | + end |
| 53 | + |
| 54 | + def language(label) |
| 55 | + label = File.extname(label)[1..] if label.include?('.') |
| 56 | + case label |
| 57 | + when 'js', 'javascript' then 'javascript' |
| 58 | + when 'ts', 'typescript' then 'typescript' |
| 59 | + when 'sh', 'bash', 'shell', 'console' then 'shell' |
| 60 | + when 'text', 'plaintext' then nil |
| 61 | + else label |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments