Skip to content

Commit

Permalink
metadata dl for reqts: metanorma/ogcapi-processes#2
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Aug 13, 2021
1 parent b4a4efa commit dc99921
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 88 deletions.
66 changes: 35 additions & 31 deletions lib/asciidoctor/standoc/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ def id_attr(node = nil)
end

def id_unnum_attrs(node)
attr_code( id: Metanorma::Utils::anchor_or_uuid(node),
attr_code(id: Metanorma::Utils::anchor_or_uuid(node),
unnumbered: node.option?("unnumbered") ? "true" : nil,
number: node.attr("number"),
subsequence: node.attr("subsequence") )
subsequence: node.attr("subsequence"))
end

def formula_attrs(node)
attr_code(id_unnum_attrs(node).merge(keep_attrs(node).merge(
inequality: node.option?("inequality") ? "true" : nil)))
attr_code(id_unnum_attrs(node)
.merge(keep_attrs(node).merge(
inequality: node.option?("inequality") ? "true" : nil,
)))
end

def keep_attrs(node)
Expand Down Expand Up @@ -58,8 +60,8 @@ def literal(node)
xml.figure **literal_attrs(node) do |f|
figure_title(node, f)
f.pre node.lines.join("\n"),
**attr_code(id: Metanorma::Utils::anchor_or_uuid,
alt: node.attr("alt"))
**attr_code(id: Metanorma::Utils::anchor_or_uuid,
alt: node.attr("alt"))
end
end
end
Expand Down Expand Up @@ -95,20 +97,21 @@ def example(node)
end

def svgmap_attrs(node)
attr_code( { id: node.id,
unnumbered: node.option?("unnumbered") ? "true" : nil,
number: node.attr("number"),
subsequence: node.attr("subsequence") }.
merge(keep_attrs(node)))
attr_code({ id: node.id,
unnumbered: node.option?("unnumbered") ? "true" : nil,
number: node.attr("number"),
subsequence: node.attr("subsequence") }
.merge(keep_attrs(node)))
end

def svgmap_example(node)
noko do |xml|
xml.svgmap **attr_code(svgmap_attrs(node).merge(
src: node.attr("src"), alt: node.attr("alt"))) do |ex|
figure_title(node, ex)
ex << node.content
end
src: node.attr("src"), alt: node.attr("alt"),
)) do |ex|
figure_title(node, ex)
ex << node.content
end
end.join("\n")
end

Expand Down Expand Up @@ -136,9 +139,10 @@ def example_proper(node)
end.join("\n")
end

def figure_title(node, f)
def figure_title(node, out)
return if node.title.nil?
f.name { |name| name << node.title }

out.name { |name| name << node.title }
end

def figure_attrs(node)
Expand All @@ -149,15 +153,15 @@ def image(node)
noko do |xml|
xml.figure **figure_attrs(node) do |f|
figure_title(node, f)
f.image **(image_attributes(node))
f.image **image_attributes(node)
end
end
end

def para_attrs(node)
attr_code(keep_attrs(node)
.merge(align: node.attr("align"),
id: Metanorma::Utils::anchor_or_uuid(node)))
.merge(align: node.attr("align"),
id: Metanorma::Utils::anchor_or_uuid(node)))
end

def paragraph(node)
Expand All @@ -172,8 +176,8 @@ def paragraph(node)

def quote_attrs(node)
attr_code(keep_attrs(node)
.merge(align: node.attr("align"),
id: Metanorma::Utils::anchor_or_uuid(node)))
.merge(align: node.attr("align"),
id: Metanorma::Utils::anchor_or_uuid(node)))
end

def quote_attribution(node, out)
Expand All @@ -189,26 +193,26 @@ def quote_attribution(node, out)

def quote(node)
noko do |xml|
xml.quote **(quote_attrs(node)) do |q|
xml.quote **quote_attrs(node) do |q|
quote_attribution(node, q)
wrap_in_para(node, q)
end
end.join("\n")
end

def listing_attrs(node)
attr_code(keep_attrs(node).
merge(lang: node.attr("language"),
id: Metanorma::Utils::anchor_or_uuid(node),
unnumbered: node.option?("unnumbered") ? "true" : nil,
number: node.attr("number"),
filename: node.attr("filename")))
attr_code(keep_attrs(node)
.merge(lang: node.attr("language"),
id: Metanorma::Utils::anchor_or_uuid(node),
unnumbered: node.option?("unnumbered") ? "true" : nil,
number: node.attr("number"),
filename: node.attr("filename")))
end

# NOTE: html escaping is performed by Nokogiri
def listing(node)
fragment = ::Nokogiri::XML::Builder.new do |xml|
xml.sourcecode **(listing_attrs(node)) do |s|
xml.sourcecode **listing_attrs(node) do |s|
figure_title(node, s)
s << node.content
end
Expand All @@ -219,7 +223,7 @@ def listing(node)

def pass(node)
noko do |xml|
xml.passthrough **attr_code(formats:
xml.passthrough **attr_code(formats:
node.attr("format") || "metanorma") do |p|
p << HTMLEntities.new.encode(node.content, :basic, :hexadecimal)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/asciidoctor/standoc/cleanup_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module Cleanup
def para_cleanup(xmldoc)
["//p[not(ancestor::bibdata)]", "//ol[not(ancestor::bibdata)]",
"//ul[not(ancestor::bibdata)]", "//quote[not(ancestor::bibdata)]",
"//note[not(ancestor::bibitem or ancestor::table or ancestor::bibdata)]"
].each { |w| inject_id(xmldoc, w) }
"//note[not(ancestor::bibitem or ancestor::table or ancestor::bibdata)]"].each do |w|
inject_id(
xmldoc, w
)
end
end

def inject_id(xmldoc, path)
Expand Down Expand Up @@ -205,7 +208,7 @@ def sourcecode_cleanup(xmldoc)
def safe_noko(text, doc)
Nokogiri::XML::Text.new(text, doc).to_xml(
encoding: "US-ASCII",
save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION,
)
end

Expand Down
73 changes: 59 additions & 14 deletions lib/asciidoctor/standoc/cleanup_reqt.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,92 @@
module Asciidoctor
module Standoc
module Cleanup
def requirement_cleanup(reqt)
requirement_descriptions(reqt)
requirement_inherit(reqt)
def requirement_cleanup(xmldoc)
requirement_metadata(xmldoc)
requirement_descriptions(xmldoc)
requirement_inherit(xmldoc)
end

def requirement_inherit(reqt)
reqt.xpath("//requirement | //recommendation | //permission")
.each do |r|
REQRECPER = "//requirement | //recommendation | //permission".freeze

def requirement_inherit(xmldoc)
xmldoc.xpath(REQRECPER).each do |r|
ins = r.at("./classification") ||
r.at("./description | ./measurementtarget | ./specification | "\
"./verification | ./import | ./description | ./requirement | "\
"./recommendation | ./permission")
"./recommendation | ./permission | ./component")
r.xpath("./*//inherit").each { |i| ins.previous = i }
end
end

def requirement_descriptions(reqt)
reqt.xpath("//requirement | //recommendation | //permission")
.each do |r|
def requirement_descriptions(xmldoc)
xmldoc.xpath(REQRECPER).each do |r|
r.children.each do |e|
unless e.element? && (reqt_subpart(e.name) ||
%w(requirement recommendation permission).include?(e.name))
t = Nokogiri::XML::Element.new("description", reqt)
t = Nokogiri::XML::Element.new("description", r)
e.before(t)
t.children = e.remove
end
end
requirement_cleanup1(r)
requirement_description_cleanup1(r)
end
end

def requirement_cleanup1(reqt)
def requirement_description_cleanup1(reqt)
while d = reqt.at("./description[following-sibling::*[1]"\
"[self::description]]")
"[self::description]]")
n = d.next.remove
d << n.children
end
reqt.xpath("./description[normalize-space(.)='']").each do |r|
r.replace("\n")
end
end

def requirement_metadata(xmldoc)
xmldoc.xpath(REQRECPER).each do |r|
dl = r&.at("./dl[@metadata = 'true']")&.remove or next
requirement_metadata1(r, dl)
end
end

def requirement_metadata1(reqt, dlist)
unless ins = reqt.at("./title")
reqt.children.first.previous = " "
ins = reqt.children.first
end
%w(label subject inherit).each do |a|
ins = reqt_dl_to_elems(ins, reqt, dlist, a)
end
reqt_dl_to_classif(ins, reqt, dlist)
end

def reqt_dl_to_elems(ins, reqt, dlist, name)
if a = reqt.at("./#{name}[last()]")
ins = a
end
dlist.xpath("./dt[text()='#{name}']").each do |e|
val = e.at("./following::dd/p") || e.at("./following::dd")
val.name = name
ins.next = val
ins = ins.next
end
ins
end

def reqt_dl_to_classif(ins, reqt, dlist)
if a = reqt.at("./classification[last()]")
ins = a
end
dlist.xpath("./dt[text()='classification']").each do |e|
val = e.at("./following::dd/p") || e.at("./following::dd")
req_classif_parse(val.text).each do |r|
ins.next = "<classification><tag>#{r[0]}</tag>"\
"<value>#{r[1]}</value></classification>"
end
end
end
end
end
end
15 changes: 9 additions & 6 deletions lib/asciidoctor/standoc/lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def ul_li_attrs(node)
c = node.attr?("checked")
attr_code(
uncheckedcheckbox: node.attr?("checkbox") ? !c : nil,
checkedcheckbox: node.attr?("checkbox") ? c : nil
checkedcheckbox: node.attr?("checkbox") ? c : nil,
)
end

Expand All @@ -52,12 +52,14 @@ def olist_style(style)
return "roman" if style == "lowerroman"
return "roman_upper" if style == "upperroman"
return "alphabet_upper" if style == "upperalpha"

style
end

def ol_attrs(node)
attr_code(keep_attrs(node).merge(id: Metanorma::Utils::anchor_or_uuid(node),
type: olist_style(node.style)))
attr_code(keep_attrs(node)
.merge(id: Metanorma::Utils::anchor_or_uuid(node),
type: olist_style(node.style)))
end

def olist(node)
Expand Down Expand Up @@ -89,9 +91,10 @@ def dd(dd, xml_dl)
end

def dl_attrs(node)
attr_code(keep_attrs(node).
merge(id: Metanorma::Utils::anchor_or_uuid(node),
key: node.option?("key") ? "true" : nil))
attr_code(keep_attrs(node)
.merge(id: Metanorma::Utils::anchor_or_uuid(node),
metadata: node.option?("metadata") ? "true" : nil,
key: node.option?("key") ? "true" : nil))
end

def dlist(node)
Expand Down
12 changes: 8 additions & 4 deletions lib/asciidoctor/standoc/reqt.rng
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
<optional>
<ref name="label"/>
</optional>
<optional>
<zeroOrMore>
<ref name="subject"/>
</optional>
</zeroOrMore>
<zeroOrMore>
<ref name="reqinherit"/>
</zeroOrMore>
Expand Down Expand Up @@ -106,12 +106,16 @@
</define>
<define name="subject">
<element name="subject">
<text/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="reqinherit">
<element name="inherit">
<text/>
<oneOrMore>
<ref name="TextElement"/>
</oneOrMore>
</element>
</define>
<define name="measurementtarget">
Expand Down
Loading

0 comments on commit dc99921

Please sign in to comment.