From 55ec2b6302eb2efee92de716c0fb98186afcba61 Mon Sep 17 00:00:00 2001 From: Nicolas Ettlin Date: Tue, 15 Feb 2022 19:43:25 +0100 Subject: [PATCH] Format attributes as a table --- assets/stylesheets/pages/_scala.scss | 7 +++++- lib/docs/filters/scala/clean_html_v3.rb | 33 ++++++++++++++++++++++--- lib/docs/scrapers/scala.rb | 9 ++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/assets/stylesheets/pages/_scala.scss b/assets/stylesheets/pages/_scala.scss index 6ddb5dc0..94b8f7b4 100644 --- a/assets/stylesheets/pages/_scala.scss +++ b/assets/stylesheets/pages/_scala.scss @@ -2,10 +2,15 @@ @extend %simple; .deprecated { @extend %label-red; } + + .attributes dl, + .attributes pre { + margin: 0; + } .related-types { @extend %pre; - margin-top: 0; + margin: 0; white-space: normal; } diff --git a/lib/docs/filters/scala/clean_html_v3.rb b/lib/docs/filters/scala/clean_html_v3.rb index f2d4c793..207d3ccd 100644 --- a/lib/docs/filters/scala/clean_html_v3.rb +++ b/lib/docs/filters/scala/clean_html_v3.rb @@ -102,7 +102,6 @@ module Docs end attributes = at_css('.attributes') - attributes.add_previous_sibling('

Metadata

') tabs_names = css('.tabs.single .names .tab') tabs_contents = css('.tabs.single .contents .tab') @@ -112,7 +111,11 @@ module Docs attributes.add_child("
#{name.content}
") attributes.add_child("
#{contents.inner_html.strip}
") end - at_css('.tabs').remove + + convert_dl_to_table(attributes) + + tabs = at_css('.tabs') + tabs.remove unless tabs.nil? || tabs.parent['class'] == 'membersList' end def format_members @@ -138,7 +141,7 @@ module Docs id = element['id'] element.remove_attribute('id') - header['id'] = id + header['id'] = id unless id.nil? annotations = element.at_css('.annotations') annotations.name = 'small' @@ -158,6 +161,10 @@ module Docs dd.remove end + # Format attributes as a table + dl = element.at_css('.attributes') + convert_dl_to_table(dl) unless dl.nil? + # Remove the unnecessary wrapper element element.replace(element.inner_html) end @@ -176,6 +183,26 @@ module Docs end end + def convert_dl_to_table(dl) + table = Nokogiri::XML::Node.new('table', doc) + table['class'] = 'attributes' + + dl.css('> dt').each do |dt| + dd = dt.next_element + has_dd = dd.name == 'dd' rescue false + + tr = Nokogiri::XML::Node.new('tr', doc) + colspan = has_dd ? '' : ' colspan="2"' # handle
without following
+ tr.add_child("#{dt.inner_html.sub(/:$/, '')}") + + tr.add_child("#{dd.inner_html}") if has_dd + + table.add_child(tr) + end + + dl.replace(table) + end + end end end diff --git a/lib/docs/scrapers/scala.rb b/lib/docs/scrapers/scala.rb index 39e95121..dcf28b63 100644 --- a/lib/docs/scrapers/scala.rb +++ b/lib/docs/scrapers/scala.rb @@ -26,7 +26,14 @@ module Docs self.release = '3.1.1' self.base_url = 'https://scala-lang.org/api/3.1.1/' self.root_path = 'index.html' - # options[:container] = '#main-content' + + options[:skip_patterns] = [ + # Ignore class names with include “#”, which cause issues with the scraper + /%23/, + + # Ignore local links to the Java documentation created by a Scaladoc bug + /java\/lang/, + ] html_filters.push 'scala/entries_v3', 'scala/clean_html_v3' end