From f9440d110ed11148f711e7458e87189ee5b11dff Mon Sep 17 00:00:00 2001 From: Jeffrey Lau Date: Tue, 8 Apr 2025 14:36:19 +0800 Subject: [PATCH 1/4] docs(xml to html): Add comments to certain methods --- lib/relaton/cli/relaton_file.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/relaton/cli/relaton_file.rb b/lib/relaton/cli/relaton_file.rb index 35d87b1..22bff8a 100644 --- a/lib/relaton/cli/relaton_file.rb +++ b/lib/relaton/cli/relaton_file.rb @@ -89,6 +89,8 @@ def bibcollection ) end + # Turn an XML string into a Nokogiri XML document. + # # @param document [String] XML # @param file [String, nil] path to file # @return [Nokogiri::XML::Document] @@ -136,6 +138,9 @@ def extract_and_write_to_files # rubocop:disable Metrics/AbcSize, Metrics/Method end end + # Map all relevant files to the corresponding bibdata instances + # + # @return [Array] def concatenate_files xml_files = [convert_rxl_to_xml, convert_yamls_to_xml, convert_xml_to_xml] @@ -188,6 +193,10 @@ def output_type(ext = options[:extension]) end end + # Create a bibdata instance from the XML document, + # while also adding file paths of related artifacts + # as URI elements to the bibdata instance. + # # @param document [Nokogiri::XML::Document] # @param file [String] path to file # @return [Relaton::Bibdata] @@ -199,6 +208,11 @@ def bibdata_instance(document, file) bibdata end + # For each file type, add to the bibdata: + # - a URI element that points to the file of that file type + # + # The URI element generation is skipped if the file does not exist. + # # @param bibdata [Relaton::Bibdata] # @param file [String] path to file def build_bibdata_relaton(bibdata, file) From 3966282e3b5ae7480ac3596a1aefa155623cc98d Mon Sep 17 00:00:00 2001 From: Jeffrey Lau Date: Tue, 8 Apr 2025 14:37:33 +0800 Subject: [PATCH 2/4] style: Fix some formatting --- lib/relaton/cli/relaton_file.rb | 2 +- spec/relaton/cli/xml_to_html_renderer_spec.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/relaton/cli/relaton_file.rb b/lib/relaton/cli/relaton_file.rb index 22bff8a..3add3be 100644 --- a/lib/relaton/cli/relaton_file.rb +++ b/lib/relaton/cli/relaton_file.rb @@ -121,7 +121,7 @@ def extract_and_write_to_files # rubocop:disable Metrics/AbcSize, Metrics/Method elsif (rfc = xml.at("//rfc")) require "relaton_ietf/bibxml_parser" ietf = RelatonIetf::BibXMLParser.fetch_rfc rfc - bib = nokogiri_document ietf.to_xml(bibdata: true) + bib = nokogiri_document(ietf.to_xml(bibdata: true)) else next end diff --git a/spec/relaton/cli/xml_to_html_renderer_spec.rb b/spec/relaton/cli/xml_to_html_renderer_spec.rb index 42438cf..8fbae8e 100644 --- a/spec/relaton/cli/xml_to_html_renderer_spec.rb +++ b/spec/relaton/cli/xml_to_html_renderer_spec.rb @@ -22,7 +22,8 @@ describe "#uri_for_extension" do it "replace file extension with the provided one" do xmltohtml = Relaton::Cli::XmlToHtmlRenderer.new( - stylesheet: "spec/assets/index-style.css", liquid_dir: "templates", + stylesheet: "spec/assets/index-style.css", + liquid_dir: "templates", ) expect( From d8e7cd9b73f3f8d0941b3f413748afedaf1c2f3b Mon Sep 17 00:00:00 2001 From: Jeffrey Lau Date: Tue, 8 Apr 2025 14:38:44 +0800 Subject: [PATCH 3/4] chore(xml to html): Make title + description link to a default link This makes the titles of individual entries in `metanorma site generate` without a generated HTML (e.g., collections) that only have generated PDF/DOC/XML, still contain a valid link (to PDF/DOC/XML), instead of just being plain text. Related: #115 --- spec/relaton/cli/xml_convertor_spec.rb | 2 +- templates/_document.liquid | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/spec/relaton/cli/xml_convertor_spec.rb b/spec/relaton/cli/xml_convertor_spec.rb index 2555149..ffe794d 100644 --- a/spec/relaton/cli/xml_convertor_spec.rb +++ b/spec/relaton/cli/xml_convertor_spec.rb @@ -64,7 +64,7 @@ Relaton::Cli::XMLConvertor.to_html(file) expect(buffer).to include '1149' expect(buffer).to include 'Relaton XML' - expect(buffer).to include 'draft-camelot-holy-grenade-01' + expect(buffer).to include 'draft-camelot-holy-grenade-01' expect(buffer).to include 'Relaton XML' end end diff --git a/templates/_document.liquid b/templates/_document.liquid index fd1f160..a380f29 100644 --- a/templates/_document.liquid +++ b/templates/_document.liquid @@ -4,13 +4,25 @@
{%- endif -%} + {%- if document.html != blank and document.html != nil -%} + {%- assign default_link = document.html -%} + {%- elsif document.pdf != blank and document.pdf != nil -%} + {%- assign default_link = document.pdf -%} + {%- elsif document.doc != blank and document.doc != nil -%} + {%- assign default_link = document.doc -%} + {%- elsif document.xml != blank and document.xml != nil -%} + {%- assign default_link = document.xml -%} + {%- else -%} + {%- assign default_link = nil -%} + {%- endif -%} +
- {% if document.html == "" %} - {{ document.docid.id }} + {% if default_link %} + {{ document.docid.id }} {% else %} - {{ document.docid.id }} + {{ document.docid.id }} {% endif %}
@@ -25,15 +37,15 @@
Edition: {{ document.edition.content }}
- {% endif %} + {% endif %}
- {% if document.html == blank or document.html == nil %} - {{ document.title }} + {% if default_link %} + {{ document.title }} {% else %} - {{ document.title }} + {{ document.title }} {% endif %}
From f4f5a2f4a7bba458f5ba0e7fec4d08cec7c47020 Mon Sep 17 00:00:00 2001 From: Jeffrey Lau Date: Tue, 8 Apr 2025 16:48:44 +0800 Subject: [PATCH 4/4] chore(xml to html): Only render STAGE: if such attribute exists in bibdata Fixes #115 --- Gemfile | 1 + spec/assets/with-collections.xml | 96 +++++++++++++++++++ spec/relaton/cli/xml_to_html_renderer_spec.rb | 89 ++++++++++++----- spec/spec_helper.rb | 1 + templates/_document.liquid | 2 + 5 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 spec/assets/with-collections.xml diff --git a/Gemfile b/Gemfile index f893ab3..ccfd993 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ gem "rake" gem "rspec", "~> 3.0" gem "rspec-command", "~> 1.0.3" gem "rspec-core", "~> 3.4" +gem "rspec-html", "~> 0.3.0" gem "simplecov" gem "vcr" gem "webmock" diff --git a/spec/assets/with-collections.xml b/spec/assets/with-collections.xml new file mode 100644 index 0000000..7d7305b --- /dev/null +++ b/spec/assets/with-collections.xml @@ -0,0 +1,96 @@ +ZZZ examplesZzz Zzz Zzz + ZZZ Document + ./documents/doc-1/document.xml + ./documents/doc-1/document.pdf + ./documents/doc-1/document.doc + ./documents/doc-1/document.html + ./documents/doc-1/document.rxl + SG 1-A80 + SG 1-A80-E + 80 + + 2133-07-07 + + + + + Zzz Zzz Zzz + ZZZ + + + + 2 + + en + + + + draft + + + 2025 + + + Zzz Zzz Zzz + ZZZ + + + + Authoring + metanorma + technical-report + template + + contribution + + A + + Sample Group 1 + SG 1 + + 2020 + 2025 + + + + + A + 80 + + + + + Sample ZZZ Collection 1 + ./documents/collections/collection-1/collection.xml + ./documents/collections/collection-1/collection.pdf + ./documents/collections/collection-1/collection.doc + ./documents/collections/collection-1/collection.rxl + SG1-0001 + + 2019 + + + Zzz Zzz Zzz + ZZZ + + + + + + Sample ZZZ Collection 2 + ./documents/collections/collection-2/collection.xml + ./documents/collections/collection-2/collection.pdf + ./documents/collections/collection-2/collection.doc + ./documents/collections/collection-2/collection.rxl + SG1-0002 + + 2020 + + + Zzz Zzz Zzz + ZZZ + + + + + diff --git a/spec/relaton/cli/xml_to_html_renderer_spec.rb b/spec/relaton/cli/xml_to_html_renderer_spec.rb index 8fbae8e..500a6d4 100644 --- a/spec/relaton/cli/xml_to_html_renderer_spec.rb +++ b/spec/relaton/cli/xml_to_html_renderer_spec.rb @@ -1,33 +1,78 @@ RSpec.describe Relaton::Cli::XmlToHtmlRenderer do + + let(:document) do + parse_html(html) + end + + let(:renderer) do + Relaton::Cli::XmlToHtmlRenderer.new( + liquid_dir: "templates", + stylesheet: "spec/assets/index-style.css", + ) + end + describe ".render" do - it "generates the HTML output" do - html = Relaton::Cli::XmlToHtmlRenderer.new( - liquid_dir: "templates", - stylesheet: "spec/assets/index-style.css", - ).render(File.read("spec/assets/index.xml")) - - expect(html).to include("[^{]+Date and time -- Timezone -- Timezone Manageme[^}]+[^{]+Date and time -- Calendars -- Gregorian calendar[^}]+[\R\s]+proposal[\R\s]+<\/div>/) - expect(html).to include("http://calconnect.org/pubdocs/CD0507%20CalDAV") + context "with a document containing a stylesheet" do + let(:html) do + renderer.render(File.read("spec/assets/index.xml")) + end + + it "generates the HTML output" do + expect(html).to include("[^{]+Date and time -- Timezone -- Timezone Manageme[^}]+[^{]+Date and time -- Calendars -- Gregorian calendar[^}]+[\R\s]+proposal[\R\s]+<\/div>/) + expect(html).to include("http://calconnect.org/pubdocs/CD0507%20CalDAV") + end end + + context "with a document containing other collections" do + let(:html) do + renderer.render(File.read("spec/assets/with-collections.xml")) + end + + it "renders links with relative paths" do + document.div(class: "document").a.all.each do |a| + expect(Pathname.new(a[:href])).to be_relative + end + end + + context "for the only rendered document" do + subject(:section) do + document.div(class: 'document')[1] + end + + it "renders with stage" do + expect(section).to have_css(".doc-stage") + expect(section.div(class: "doc-info")[:class]).to match 'draft' + expect(section.div(class: "doc-stage")[:class]).to match 'draft' + end + end + + context "for the rendered collections" do + it "renders without stage" do + (2..3).map do |i| + document.div(class: 'document')[i] + end.each do |e| + expect(e).to_not have_css(".doc-info") + expect(e).to_not have_css(".doc-stage") + end + end + end + + end + end describe "#uri_for_extension" do it "replace file extension with the provided one" do - xmltohtml = Relaton::Cli::XmlToHtmlRenderer.new( - stylesheet: "spec/assets/index-style.css", - liquid_dir: "templates", - ) - expect( - xmltohtml.uri_for_extension("index.xml", "html"), + renderer.uri_for_extension("index.xml", "html"), ).to eq("index.html") end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index db22666..c3daff2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ Dir["./spec/support/**/*.rb"].sort.each { |f| require f } require "relaton/cli" +require "rspec/html" RSpec.configure do |config| # Enable flags like --only-failures and --next-failure diff --git a/templates/_document.liquid b/templates/_document.liquid index a380f29..138c376 100644 --- a/templates/_document.liquid +++ b/templates/_document.liquid @@ -50,6 +50,7 @@
+ {% if document.docstatus %}
{{ document.docstatus.stage.abbreviation }} @@ -69,6 +70,7 @@
+ {% endif %}