Showing the changes to lib/docs/filters/python (2 of 15 files). Show all
Unified · Split
| @@ -0,0 +1,68 @@ | ||
| 1 | +module Docs | |
| 2 | + class Python | |
| 3 | + class CleanHtmlFilter < Filter | |
| 4 | + def call | |
| 5 | + @doc = at_css '.body > .section' | |
| 6 | + | |
| 7 | + # Clean inline code elements | |
| 8 | + | |
| 9 | + css('tt.literal').each do |node| | |
| 10 | + node.before(node.children).remove | |
| 11 | + end | |
| 12 | + | |
| 13 | + css('tt', 'span.pre').each do |node| | |
| 14 | + node.name = 'code' | |
| 15 | + node.remove_attribute 'class' | |
| 16 | + end | |
| 17 | + | |
| 18 | + root_page? ? root : other | |
| 19 | + | |
| 20 | + doc | |
| 21 | + end | |
| 22 | + | |
| 23 | + def root | |
| 24 | + at_css('h1').content = 'Python' | |
| 25 | + css('> p').remove | |
| 26 | + end | |
| 27 | + | |
| 28 | + def other | |
| 29 | + css('.headerlink', 'hr').remove | |
| 30 | + | |
| 31 | + # Clean headings | |
| 32 | + | |
| 33 | + at_css('h1').tap do |node| | |
| 34 | + node.content = node.content.sub!(/\A[\d\.]+/) { |str| @levelRegexp = /\A#{str}/; '' } | |
| 35 | + end | |
| 36 | + | |
| 37 | + css('h2', 'h3', 'h4').each do |node| | |
| 38 | + node.css('a').each do |link| | |
| 39 | + link.before(link.children).remove | |
| 40 | + end | |
| 41 | + node.child.content = node.child.content.sub @levelRegexp, '' | |
| 42 | + end | |
| 43 | + | |
| 44 | + css('dt').each do |node| | |
| 45 | + node.content = node.content | |
| 46 | + end | |
| 47 | + | |
| 48 | + # Remove blockquotes | |
| 49 | + css('blockquote').each do |node| | |
| 50 | + node.before(node.children).remove | |
| 51 | + end | |
| 52 | + | |
| 53 | + # Remove code highlighting | |
| 54 | + css('.highlight-python3').each do |node| | |
| 55 | + pre = node.at_css('pre') | |
| 56 | + pre.content = pre.content | |
| 57 | + pre['class'] = 'python' | |
| 58 | + node.replace(pre) | |
| 59 | + end | |
| 60 | + | |
| 61 | + # Remove <table> border attribute | |
| 62 | + css('table[border]').each do |node| | |
| 63 | + node.remove_attribute 'border' | |
| 64 | + end | |
| 65 | + end | |
| 66 | + end | |
| 67 | + end | |
| 68 | +end | |
| @@ -0,0 +1,82 @@ | ||
| 1 | +module Docs | |
| 2 | + class Python | |
| 3 | + class EntriesFilter < Docs::EntriesFilter | |
| 4 | + REPLACE_TYPES = { | |
| 5 | + 'Cryptographic' => 'Cryptography', | |
| 6 | + 'Custom Interpreters' => 'Interpreters', | |
| 7 | + 'Data Compression & Archiving' => 'Data Compression', | |
| 8 | + 'Generic Operating System' => 'Operating System', | |
| 9 | + 'Graphical User Interfaces with Tk' => 'Tk', | |
| 10 | + 'Internet Data Handling' => 'Internet Data', | |
| 11 | + 'Internet Protocols & Support' => 'Internet', | |
| 12 | + 'Interprocess Communication & Networking' => 'Networking', | |
| 13 | + 'Program Frameworks' => 'Frameworks', | |
| 14 | + 'Structured Markup Processing Tools' => 'Structured Markup' } | |
| 15 | + | |
| 16 | + def get_name | |
| 17 | + name = at_css('h1').content | |
| 18 | + name.sub! %r{\A[\d\.]+ }, '' # remove list number | |
| 19 | + name.sub! %r{ \u{2014}.+\z}, '' # remove text after em dash | |
| 20 | + name | |
| 21 | + end | |
| 22 | + | |
| 23 | + def get_type | |
| 24 | + return 'Logging' if slug.start_with? 'library/logging' | |
| 25 | + | |
| 26 | + type = at_css('.related a[accesskey="U"]').content | |
| 27 | + | |
| 28 | + if type == 'The Python Standard Library' | |
| 29 | + type = at_css('h1').content | |
| 30 | + elsif type.start_with? '19' | |
| 31 | + type = 'Internet Data Handling' | |
| 32 | + end | |
| 33 | + | |
| 34 | + type.sub! %r{\A\d+\.\s+}, '' # remove list number | |
| 35 | + type.sub! "\u{00b6}", '' # remove paragraph character | |
| 36 | + type.sub! ' and ', ' & ' | |
| 37 | + [' Services', ' Modules', ' Specific', 'Python '].each { |str| type.sub! str, '' } | |
| 38 | + | |
| 39 | + REPLACE_TYPES[type] || type | |
| 40 | + end | |
| 41 | + | |
| 42 | + def include_default_entry? | |
| 43 | + name !~ /[A-Z]/ && !skip? # skip non-module names | |
| 44 | + end | |
| 45 | + | |
| 46 | + def additional_entries | |
| 47 | + return [] if root_page? || skip? || name == 'errno' | |
| 48 | + clean_id_attributes | |
| 49 | + entries = [] | |
| 50 | + | |
| 51 | + css('.class > dt[id]', '.exception > dt[id]', '.attribute > dt[id]').each do |node| | |
| 52 | + entries << [node['id'], node['id']] | |
| 53 | + end | |
| 54 | + | |
| 55 | + css('.data > dt[id]').each do |node| | |
| 56 | + if node['id'].split('.').last.upcase! # skip constants | |
| 57 | + entries << [node['id'], node['id']] | |
| 58 | + end | |
| 59 | + end | |
| 60 | + | |
| 61 | + css('.function > dt[id]', '.method > dt[id]', '.classmethod > dt[id]').each do |node| | |
| 62 | + entries << [node['id'] + '()', node['id']] | |
| 63 | + end | |
| 64 | + | |
| 65 | + entries | |
| 66 | + end | |
| 67 | + | |
| 68 | + def skip? | |
| 69 | + type == 'Language' | |
| 70 | + end | |
| 71 | + | |
| 72 | + def clean_id_attributes | |
| 73 | + css('.section > .target[id]').each do |node| | |
| 74 | + if dt = node.at_css('+ dl > dt') | |
| 75 | + dt['id'] ||= node['id'].sub(/\w+\-/, '') | |
| 76 | + end | |
| 77 | + node.remove | |
| 78 | + end | |
| 79 | + end | |
| 80 | + end | |
| 81 | + end | |
| 82 | +end | |