|
| 1 | +module Docs |
| 2 | + class R |
| 3 | + class EntriesFilter < Docs::EntriesFilter |
| 4 | + |
| 5 | + PKG_INDEX_ENTRIES = Hash.new [] |
| 6 | + |
| 7 | + def call |
| 8 | + if slug_parts[-1] == '00Index' |
| 9 | + dir = File.dirname(result[:subpath]) |
| 10 | + css('tr a').each do |link| |
| 11 | + PKG_INDEX_ENTRIES[link['href']] += [link.text] |
| 12 | + next if link['href'] == link.text |
| 13 | + context[:replace_paths][File.join(dir, "#{link.text}.html")] = File.join(dir, "#{link['href']}.html") |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + super |
| 18 | + end |
| 19 | + |
| 20 | + def slug_parts |
| 21 | + slug.split('/') |
| 22 | + end |
| 23 | + |
| 24 | + def is_package? |
| 25 | + slug_parts[0] == 'library' |
| 26 | + end |
| 27 | + |
| 28 | + def is_manual? |
| 29 | + slug_parts[1] == 'manual' |
| 30 | + end |
| 31 | + |
| 32 | + def get_name |
| 33 | + return at_css('h2').content if is_package? |
| 34 | + title = at_css('h1.settitle') |
| 35 | + title ? title.content : at_css('h1, h2').content |
| 36 | + end |
| 37 | + |
| 38 | + def get_type |
| 39 | + return slug_parts[1] if is_package? |
| 40 | + return at_css('h1.settitle').content if is_manual? |
| 41 | + end |
| 42 | + |
| 43 | + def include_default_entry? |
| 44 | + is_package? and not slug_parts[-1] == '00Index' |
| 45 | + end |
| 46 | + |
| 47 | + def manual_section(node) |
| 48 | + title = node.content.sub /^((Appendix )?[A-Z]|[0-9]+)(\.[0-9]+)* /, '' |
| 49 | + title unless ['References', 'Preface', 'Acknowledgements'].include?(title) or title.end_with?(' index') |
| 50 | + end |
| 51 | + |
| 52 | + def additional_entries |
| 53 | + if is_package? and slug_parts[-1] != '00Index' |
| 54 | + page = slug_parts[-1] |
| 55 | + return [page] + PKG_INDEX_ENTRIES.fetch(page, []) |
| 56 | + end |
| 57 | + |
| 58 | + return [] unless is_manual? |
| 59 | + |
| 60 | + entries = [] |
| 61 | + unless slug_parts[-1].downcase == 'r-intro' |
| 62 | + # Single top-level category |
| 63 | + css('div.contents > ul a').each do |link| |
| 64 | + link_name = manual_section(link) |
| 65 | + entries << [link_name, link['href'].split('#')[1], name] unless link_name.nil? |
| 66 | + end |
| 67 | + else |
| 68 | + # Split 1st level of manual into different categories |
| 69 | + css('div.contents > ul > li').each do |node| |
| 70 | + type = manual_section(node.at_css('a')) |
| 71 | + next if type.nil? |
| 72 | + node.css('> ul a').each do |link| |
| 73 | + link_name = link.content.sub /^[0-9A-Z]+(\.[0-9]+)* /, '' |
| 74 | + entries << [link_name, link['href'].split('#')[1], type] |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + return entries |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments