Skip to content

Commit e2c719a

Browse files
Merge pull request #116 from relaton/115-collections
template: Render collections with appropriate fields
2 parents 1c5170d + f4f5a2f commit e2c719a

7 files changed

Lines changed: 202 additions & 30 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gem "rake"
1010
gem "rspec", "~> 3.0"
1111
gem "rspec-command", "~> 1.0.3"
1212
gem "rspec-core", "~> 3.4"
13+
gem "rspec-html", "~> 0.3.0"
1314
gem "simplecov"
1415
gem "vcr"
1516
gem "webmock"

lib/relaton/cli/relaton_file.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def bibcollection
8989
)
9090
end
9191

92+
# Turn an XML string into a Nokogiri XML document.
93+
#
9294
# @param document [String] XML
9395
# @param file [String, nil] path to file
9496
# @return [Nokogiri::XML::Document]
@@ -119,7 +121,7 @@ def extract_and_write_to_files # rubocop:disable Metrics/AbcSize, Metrics/Method
119121
elsif (rfc = xml.at("//rfc"))
120122
require "relaton_ietf/bibxml_parser"
121123
ietf = RelatonIetf::BibXMLParser.fetch_rfc rfc
122-
bib = nokogiri_document ietf.to_xml(bibdata: true)
124+
bib = nokogiri_document(ietf.to_xml(bibdata: true))
123125
else
124126
next
125127
end
@@ -136,6 +138,9 @@ def extract_and_write_to_files # rubocop:disable Metrics/AbcSize, Metrics/Method
136138
end
137139
end
138140

141+
# Map all relevant files to the corresponding bibdata instances
142+
#
143+
# @return [Array<Relaton::Bibdata>]
139144
def concatenate_files
140145
xml_files = [convert_rxl_to_xml, convert_yamls_to_xml, convert_xml_to_xml]
141146

@@ -188,6 +193,10 @@ def output_type(ext = options[:extension])
188193
end
189194
end
190195

196+
# Create a bibdata instance from the XML document,
197+
# while also adding file paths of related artifacts
198+
# as URI elements to the bibdata instance.
199+
#
191200
# @param document [Nokogiri::XML::Document]
192201
# @param file [String] path to file
193202
# @return [Relaton::Bibdata]
@@ -199,6 +208,11 @@ def bibdata_instance(document, file)
199208
bibdata
200209
end
201210

211+
# For each file type, add to the bibdata:
212+
# - a URI element that points to the file of that file type
213+
#
214+
# The URI element generation is skipped if the file does not exist.
215+
#
202216
# @param bibdata [Relaton::Bibdata]
203217
# @param file [String] path to file
204218
def build_bibdata_relaton(bibdata, file)

spec/assets/with-collections.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<relaton-collection xmlns="https://open.ribose.com/relaton-xml"><title>ZZZ examples</title><contributor><role type='author'/><organization><name>Zzz Zzz Zzz</name></organization></contributor><relation type='partOf'><bibdata type="standard" schema-version="v1.2.9">
2+
<title type="main" format="text/plain" language="en">ZZZ Document</title>
3+
<uri type="xml">./documents/doc-1/document.xml</uri>
4+
<uri type="pdf">./documents/doc-1/document.pdf</uri>
5+
<uri type="doc">./documents/doc-1/document.doc</uri>
6+
<uri type="html">./documents/doc-1/document.html</uri>
7+
<uri type="rxl">./documents/doc-1/document.rxl</uri>
8+
<docidentifier type="ZZZ" primary="true">SG 1-A80</docidentifier>
9+
<docidentifier type="ZZZ-lang">SG 1-A80-E</docidentifier>
10+
<docnumber>80</docnumber>
11+
<date type="published">
12+
<on>2133-07-07</on>
13+
</date>
14+
<contributor>
15+
<role type="author"/>
16+
<organization>
17+
<name>Zzz Zzz Zzz</name>
18+
<abbreviation>ZZZ</abbreviation>
19+
</organization>
20+
</contributor>
21+
<version>
22+
<draft>2</draft>
23+
</version>
24+
<language>en</language>
25+
<script>Latn</script>
26+
<abstract/>
27+
<status>
28+
<stage>draft</stage>
29+
</status>
30+
<copyright>
31+
<from>2025</from>
32+
<owner>
33+
<organization>
34+
<name>Zzz Zzz Zzz</name>
35+
<abbreviation>ZZZ</abbreviation>
36+
</organization>
37+
</owner>
38+
</copyright>
39+
<keyword>Authoring</keyword>
40+
<keyword>metanorma</keyword>
41+
<keyword>technical-report</keyword>
42+
<keyword>template</keyword>
43+
<ext schema-version="v1.0.0">
44+
<doctype>contribution</doctype>
45+
<editorialgroup>
46+
<bureau>A</bureau>
47+
<group type="study-group">
48+
<name>Sample Group 1</name>
49+
<acronym>SG 1</acronym>
50+
<period>
51+
<start>2020</start>
52+
<end>2025</end>
53+
</period>
54+
</group>
55+
</editorialgroup>
56+
<structuredidentifier>
57+
<bureau>A</bureau>
58+
<docnumber>80</docnumber>
59+
</structuredidentifier>
60+
</ext>
61+
</bibdata></relation>
62+
<relation type='partOf'><bibdata type="collection" schema-version="v1.2.9">
63+
<title format="text/plain" language="en">Sample ZZZ Collection 1</title>
64+
<uri type="xml">./documents/collections/collection-1/collection.xml</uri>
65+
<uri type="pdf">./documents/collections/collection-1/collection.pdf</uri>
66+
<uri type="doc">./documents/collections/collection-1/collection.doc</uri>
67+
<uri type="rxl">./documents/collections/collection-1/collection.rxl</uri>
68+
<docidentifier type="ZZZ">SG1-0001</docidentifier>
69+
<copyright>
70+
<from>2019</from>
71+
<owner>
72+
<organization>
73+
<name>Zzz Zzz Zzz</name>
74+
<abbreviation>ZZZ</abbreviation>
75+
</organization>
76+
</owner>
77+
</copyright>
78+
</bibdata></relation>
79+
<relation type='partOf'><bibdata type="collection" schema-version="v1.2.9">
80+
<title format="text/plain" language="en">Sample ZZZ Collection 2</title>
81+
<uri type="xml">./documents/collections/collection-2/collection.xml</uri>
82+
<uri type="pdf">./documents/collections/collection-2/collection.pdf</uri>
83+
<uri type="doc">./documents/collections/collection-2/collection.doc</uri>
84+
<uri type="rxl">./documents/collections/collection-2/collection.rxl</uri>
85+
<docidentifier type="ZZZ">SG1-0002</docidentifier>
86+
<copyright>
87+
<from>2020</from>
88+
<owner>
89+
<organization>
90+
<name>Zzz Zzz Zzz</name>
91+
<abbreviation>ZZZ</abbreviation>
92+
</organization>
93+
</owner>
94+
</copyright>
95+
</bibdata></relation>
96+
</relaton-collection>

spec/relaton/cli/xml_convertor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
Relaton::Cli::XMLConvertor.to_html(file)
6565
expect(buffer).to include '<a href="spec/fixtures/documents/example.html">1149</a>'
6666
expect(buffer).to include '<a href="spec/fixtures/documents/example.rxl">Relaton XML</a>'
67-
expect(buffer).to include '<a href="">draft-camelot-holy-grenade-01</a>'
67+
expect(buffer).to include '<a href="spec/fixtures/documents/antioch.rfc.xml">draft-camelot-holy-grenade-01</a>'
6868
expect(buffer).to include '<a href="spec/fixtures/documents/antioch.rxl">Relaton XML</a>'
6969
end
7070
end

spec/relaton/cli/xml_to_html_renderer_spec.rb

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,78 @@
11
RSpec.describe Relaton::Cli::XmlToHtmlRenderer do
2+
3+
let(:document) do
4+
parse_html(html)
5+
end
6+
7+
let(:renderer) do
8+
Relaton::Cli::XmlToHtmlRenderer.new(
9+
liquid_dir: "templates",
10+
stylesheet: "spec/assets/index-style.css",
11+
)
12+
end
13+
214
describe ".render" do
3-
it "generates the HTML output" do
4-
html = Relaton::Cli::XmlToHtmlRenderer.new(
5-
liquid_dir: "templates",
6-
stylesheet: "spec/assets/index-style.css",
7-
).render(File.read("spec/assets/index.xml"))
8-
9-
expect(html).to include("<html")
10-
expect(html).to include("<div class=\"document\"")
11-
expect(html).to include("CC/Amd 86003")
12-
expect(html).to include("CalConnect Inc\.")
13-
expect(html).to include("I AM A SAMPLE STYLESHEET")
14-
expect(html).to include("CalConnect Standards Registry")
15-
expect(html).to match(/<h3>[^{]+Date and time -- Timezone -- Timezone Manageme[^}]+</)
16-
expect(html).to match(/<h3>[^{]+Date and time -- Calendars -- Gregorian calendar[^}]+</)
17-
expect(html).to match(/<div class="doc-stage proposal">[\R\s]+proposal[\R\s]+<\/div>/)
18-
expect(html).to include("http://calconnect.org/pubdocs/CD0507%20CalDAV")
15+
context "with a document containing a stylesheet" do
16+
let(:html) do
17+
renderer.render(File.read("spec/assets/index.xml"))
18+
end
19+
20+
it "generates the HTML output" do
21+
expect(html).to include("<html")
22+
expect(html).to include("<div class=\"document\"")
23+
expect(html).to include("CC/Amd 86003")
24+
expect(html).to include("CalConnect Inc\.")
25+
expect(html).to include("I AM A SAMPLE STYLESHEET")
26+
expect(html).to include("CalConnect Standards Registry")
27+
expect(html).to match(/<h3>[^{]+Date and time -- Timezone -- Timezone Manageme[^}]+</)
28+
expect(html).to match(/<h3>[^{]+Date and time -- Calendars -- Gregorian calendar[^}]+</)
29+
expect(html).to match(/<div class="doc-stage proposal">[\R\s]+proposal[\R\s]+<\/div>/)
30+
expect(html).to include("http://calconnect.org/pubdocs/CD0507%20CalDAV")
31+
end
1932
end
33+
34+
context "with a document containing other collections" do
35+
let(:html) do
36+
renderer.render(File.read("spec/assets/with-collections.xml"))
37+
end
38+
39+
it "renders links with relative paths" do
40+
document.div(class: "document").a.all.each do |a|
41+
expect(Pathname.new(a[:href])).to be_relative
42+
end
43+
end
44+
45+
context "for the only rendered document" do
46+
subject(:section) do
47+
document.div(class: 'document')[1]
48+
end
49+
50+
it "renders with stage" do
51+
expect(section).to have_css(".doc-stage")
52+
expect(section.div(class: "doc-info")[:class]).to match 'draft'
53+
expect(section.div(class: "doc-stage")[:class]).to match 'draft'
54+
end
55+
end
56+
57+
context "for the rendered collections" do
58+
it "renders without stage" do
59+
(2..3).map do |i|
60+
document.div(class: 'document')[i]
61+
end.each do |e|
62+
expect(e).to_not have_css(".doc-info")
63+
expect(e).to_not have_css(".doc-stage")
64+
end
65+
end
66+
end
67+
68+
end
69+
2070
end
2171

2272
describe "#uri_for_extension" do
2373
it "replace file extension with the provided one" do
24-
xmltohtml = Relaton::Cli::XmlToHtmlRenderer.new(
25-
stylesheet: "spec/assets/index-style.css", liquid_dir: "templates",
26-
)
27-
2874
expect(
29-
xmltohtml.uri_for_extension("index.xml", "html"),
75+
renderer.uri_for_extension("index.xml", "html"),
3076
).to eq("index.html")
3177
end
3278
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
55

66
require "relaton/cli"
7+
require "rspec/html"
78

89
RSpec.configure do |config|
910
# Enable flags like --only-failures and --next-failure

templates/_document.liquid

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,25 @@
44
<div class="document">
55
{%- endif -%}
66

7+
{%- if document.html != blank and document.html != nil -%}
8+
{%- assign default_link = document.html -%}
9+
{%- elsif document.pdf != blank and document.pdf != nil -%}
10+
{%- assign default_link = document.pdf -%}
11+
{%- elsif document.doc != blank and document.doc != nil -%}
12+
{%- assign default_link = document.doc -%}
13+
{%- elsif document.xml != blank and document.xml != nil -%}
14+
{%- assign default_link = document.xml -%}
15+
{%- else -%}
16+
{%- assign default_link = nil -%}
17+
{%- endif -%}
18+
719
<div class="doc-line">
820
<div class="doc-identifier">
921
<h{{ depth }}>
10-
{% if document.html == "" %}
11-
{{ document.docid.id }}
22+
{% if default_link %}
23+
<a href="{{ default_link }}">{{ document.docid.id }}</a>
1224
{% else %}
13-
<a href="{{ document.html }}">{{ document.docid.id }}</a>
25+
{{ document.docid.id }}
1426
{% endif %}
1527
</h{{ depth }}>
1628
</div>
@@ -25,19 +37,20 @@
2537
<div class="doc-identifier">
2638
Edition: {{ document.edition.content }}
2739
</div>
28-
{% endif %}
40+
{% endif %}
2941
</div>
3042

3143
<div class="doc-title">
3244
<h{{ depth | plus: 1 }}>
33-
{% if document.html == blank or document.html == nil %}
34-
{{ document.title }}
45+
{% if default_link %}
46+
<a href="{{ default_link }}">{{ document.title }}</a>
3547
{% else %}
36-
<a href="{{ document.html }}">{{ document.title }}</a>
48+
{{ document.title }}
3749
{% endif %}
3850
</h{{ depth | plus: 1 }}>
3951
</div>
4052

53+
{% if document.docstatus %}
4154
<div class="doc-info {{ document.docstatus.stage.value | downcase }}">
4255
<div class="doc-stage {{ document.docstatus.stage.value | downcase }}">
4356
{{ document.docstatus.stage.abbreviation }}
@@ -57,6 +70,7 @@
5770
</div>
5871
</div>
5972
</div>
73+
{% endif %}
6074

6175
<div class="doc-bib">
6276
<div class="doc-bib-relaton">

0 commit comments

Comments
 (0)