From 92a0d28e48d4353b8e591fd0065da4b926c781ca Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Wed, 8 Jan 2020 15:18:00 -0500 Subject: [PATCH] Improve Mongoose docs --- lib/docs/filters/mongoose/clean_html.rb | 39 ++++++++++++------------- lib/docs/filters/mongoose/entries.rb | 29 +++++++++++------- lib/docs/scrapers/mongoose.rb | 3 +- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/lib/docs/filters/mongoose/clean_html.rb b/lib/docs/filters/mongoose/clean_html.rb index dbbb3c8c..7364649b 100644 --- a/lib/docs/filters/mongoose/clean_html.rb +++ b/lib/docs/filters/mongoose/clean_html.rb @@ -4,28 +4,12 @@ module Docs def call css('hr', '.showcode', '.sourcecode').remove - if slug == 'api' - at_css('.controls').after('

Mongoose API

') - - css('.private', '.controls').remove - - css('a + .method').each do |node| - node.previous_element.replace("

#{node.previous_element.to_html}

") - end - else - css('h2:empty + p').each do |node| # /customschematypes.html - node.previous_element.content = node.content - node.remove - end - - at_css('h2').name = 'h1' - - css('h3').each do |node| - node.name = 'h2' - end + css('h2:empty + p').each do |node| # /customschematypes.html + node.previous_element.content = node.content + node.remove end - css('pre > code', 'h1 + ul', '.module', '.item', 'h3 > a', 'h3 code').each do |node| + css('pre > code', 'h1 + ul', '.module', '.item', 'h1 > a', 'h2 > a', 'h3 > a', 'h3 code').each do |node| node.before(node.children).remove end @@ -33,6 +17,21 @@ module Docs node['data-language'] = 'javascript' end + css('.native-inline', '.api-nav', '.toc', '.api-content ul:first-child').remove + + if !at_css('h1') + if css('h2').count > 1 + # Mongoose vX.Y.Z: [title here] + title = doc.document.at_css('title').content.split(': ')[1] + doc.prepend_child("

#{title}") + else + at_css('h2').name = 'h1' + css('h3').each do |el| + el.name = 'h2' + end + end + end + doc end end diff --git a/lib/docs/filters/mongoose/entries.rb b/lib/docs/filters/mongoose/entries.rb index b2466e58..d4918d94 100644 --- a/lib/docs/filters/mongoose/entries.rb +++ b/lib/docs/filters/mongoose/entries.rb @@ -2,23 +2,30 @@ module Docs class Mongoose class EntriesFilter < Docs::EntriesFilter def get_name - if slug == 'api' - 'Mongoose' - else - at_css('h1').content - end + at_css('h1').content end def get_type - if slug == 'api' - 'Mongoose' + if slug.start_with? 'api' + for heading in css('h3[id]') + type = get_type_from heading + return type if type + end + end + 'Guides' + end + + def get_type_from node + name = node.content.strip + type = name.split(/[#\.\(]/).first + if type.empty? || name.include?(' ') + nil else - 'Guides' + type end end def additional_entries - return [] unless slug == 'api' entries = [] css('h3[id]').each do |node| @@ -32,8 +39,8 @@ module Docs name.sub! %r{\(.+\)}, '()' next if name.include?(' ') - type = name.split(/[#\.\(]/).first - next if type.empty? + type = get_type_from node + next if type.nil? entries << [name, id, type] end diff --git a/lib/docs/scrapers/mongoose.rb b/lib/docs/scrapers/mongoose.rb index 36ea82c5..af7d56ff 100644 --- a/lib/docs/scrapers/mongoose.rb +++ b/lib/docs/scrapers/mongoose.rb @@ -5,7 +5,7 @@ module Docs self.release = '5.7.7' self.base_url = 'https://mongoosejs.com/docs/' self.root_path = 'index.html' - self.initial_paths = %w(guide.html api.html) + self.initial_paths = %w(guide.html) self.force_gzip = true self.links = { home: 'http://mongoosejs.com/', @@ -17,6 +17,7 @@ module Docs options[:container] = '#content' options[:skip] = %w( + api.html faq.html prior.html migration.html