| @@ -2,100 +2,46 @@ module Docs · 1 unmodified line |
| 2 | class Pygame | 2 | class Pygame |
| 3 | class CleanHtmlFilter < Filter | 3 | class CleanHtmlFilter < Filter |
| 4 | def call | 4 | def call |
| 5 | @doc = at_css '.body' | 5 | @doc = at_css('.body') |
| 6 | | 6 | root_page? ? root : other |
| 7 | if root_page? | 7 | doc |
| 8 | # remove unneeded stuff | 8 | end |
| 9 | at_css('.modindex-jumpbox').remove | | |
| 10 | css('[role="navigation"],.pcap, .cap, .footer').remove | | |
| 11 | # table -> list | | |
| 12 | list = at_css('table') | | |
| 13 | list.replace(list.children) | | |
| 14 | list.name = 'ul' | | |
| 15 | css('tr').each do |row| | | |
| 16 | row.name = 'li' | | |
| 17 | row.remove_attribute('class') | | |
| 18 | end | | |
| 19 | at_css('h1').content = 'Pygame' | | |
| 20 | return doc | | |
| 21 | end | | |
| 22 | | 9 | |
| 23 | # remove unwanted stuff | 10 | def root |
| 24 | # .headerlink => ¶ after links | 11 | at_css('h1').content = 'Pygame' |
| 25 | # .toc => table of content | | |
| 26 | # .tooltip-content => tooltips after links to functions | | |
| 27 | css('table.toc.docutils, .headerlink, .tooltip-content').remove | | |
| 28 | | 12 | |
| 29 | # Remove wrapper .section | 13 | # remove unneeded stuff |
| 30 | section = at_css('.section') | 14 | at_css('.modindex-jumpbox').remove |
| 31 | definition = at_css('.definition') | 15 | css('[role="navigation"], .pcap, .cap, .footer').remove |
| 32 | definition['id'] = section['id'] | 16 | css('tr > td:first-child').remove |
| 33 | section.replace(section.children) | | |
| 34 | | 17 | |
| 35 | # Format code for it be highlighted | 18 | # Unitalicize package descriptions |
| 36 | css('.highlight-default.notranslate').each do |node| | 19 | css('td > em').each do |node| |
| 37 | pre = node.at_css('pre') | 20 | node.parent.content = node.content |
| 38 | node.replace(pre) | | |
| 39 | # gets rid of the already existing syntax highlighting | | |
| 40 | pre.content = pre.content | | |
| 41 | pre['class'] = 'language-python' | | |
| 42 | pre['data-language'] = "python" | | |
| 43 | end | | |
| 44 | | | |
| 45 | # change descriptions of functions/attributes to blockquote | | |
| 46 | css('.line-block').each do |node| | | |
| 47 | node.name = 'blockquote' | | |
| 48 | end | 21 | end |
| | 22 | end |
| 49 | | 23 | |
| 50 | # change functions | 24 | def other |
| 51 | css('.definition').each do |d| | 25 | css('> :not(.section), table.toc.docutils, .tooltip-content').remove |
| 52 | | 26 | |
| 53 | # the header is the function/attribute name. It might look something like | 27 | # Remove code tag from function, class, method, module, etc. |
| 54 | # this: | 28 | css('dl > dt').each do |node| |
| 55 | # pygame.image.load() | 29 | node.content = node.content |
| 56 | # It'll end up being something like this: | 30 | end |
| 57 | # pygame.image.load(filename) -> Surface | | |
| 58 | # pygame.image.load(fileobj, namehint="") -> Surface | | |
| 59 | | 31 | |
| 60 | header = d.at_css('dt.title') | 32 | css('> .section > dl > dt').each do |node| |
| 61 | if d['class'].include?('class') or d['class'].include?('module') | 33 | node.name = 'h1' |
| 62 | header.name = 'h1' | 34 | node.parent.parent.before(node) |
| 63 | @section = header.content.strip | | |
| 64 | else | | |
| 65 | header.name = 'h3' | | |
| 66 | end | | |
| 67 | # save the original header | | |
| 68 | initial_header = header.content.strip | | |
| 69 | # save the real name for the entries | | |
| 70 | header['data-name'] = initial_header | | |
| 71 | # empty the header | | |
| 72 | if header.name == 'h3' | | |
| 73 | header.inner_html = '' | | |
| 74 | end | | |
| 75 | # to replace it with the signatures | | |
| 76 | next_el = header.next_element | | |
| 77 | signatures = next_el.css('.signature') | | |
| 78 | signatures.each do |sig| | | |
| 79 | sig.name = 'code' | | |
| 80 | if header.name == 'h3' | | |
| 81 | sig.parent = header | | |
| 82 | # the signature don't contain pygame.module. I think it's better | | |
| 83 | # to display them, as it avoids confusion with methods (have a | | |
| 84 | # look at the pygame.Rect page) | | |
| 85 | if initial_header.start_with?(@section) | | |
| 86 | sig.content = @section + '.' + sig.text | | |
| 87 | end | | |
| 88 | # separate the signatures on different lines. | | |
| 89 | header.add_child "<br>" | | |
| 90 | end | | |
| 91 | end | | |
| 92 | end | 35 | end |
| 93 | | 36 | |
| 94 | css('> dl', '> dl > dd', 'h1 code').each do |node| | 37 | # Format code for it be highlighted |
| 95 | node.before(node.children).remove | 38 | css('.highlight-default.notranslate').each do |node| |
| | 39 | node.name = 'pre' |
| | 40 | node.content = node.content.strip |
| | 41 | node['class'] = 'language-python' |
| | 42 | node['data-language'] = 'python' |
| 96 | end | 43 | end |
| 97 | | 44 | |
| 98 | doc | | |
| 99 | end | 45 | end |
| 100 | end | 46 | end |
| 101 | end | 47 | end |