Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocop #155

Merged
merged 3 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions lib/asciidoctor/ietf/cleanup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module Asciidoctor
module Ietf
class Converter < ::Asciidoctor::Standoc::Converter
def cleanup(xmldoc)
bcp14_cleanup(xmldoc)
abstract_cleanup(xmldoc)
super
rfc_anchor_cleanup(xmldoc)
end

BCP_KEYWORDS =
["MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", "OPTIONAL"].freeze

def abstract_cleanup(xmldoc)
xmldoc.xpath("//abstract[not(text())]").each do |x|
x.remove
warn "Empty abstract section removed"
end
end

def bcp14_cleanup(xmldoc)
return unless @bcp_bold

xmldoc.xpath("//strong").each do |s|
next unless BCP_KEYWORDS.include?(s.text)

s.name = "bcp14"
end
end

def rfc_anchor_cleanup(xmldoc)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for rfc_anchor_cleanup is too high. [<6, 17, 4> 18.47/15]

map = {}
xmldoc.xpath("//bibitem[docidentifier/@type = 'rfc-anchor']").each do |b|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [81/80]

next if b.at("./ancestor::bibdata")

map[b["id"]] = b.at("./docidentifier[@type = 'rfc-anchor']").text
b["id"] = b.at("./docidentifier[@type = 'rfc-anchor']").text
end
xmldoc.xpath("//eref | //origin").each do |x|
map[x["bibitemid"]] and x["bibitemid"] = map[x["bibitemid"]]
end
xmldoc
end

def smartquotes_cleanup(xmldoc)
xmldoc.traverse do |n|
next unless n.text?

n.replace(HTMLEntities.new.encode(
n.text.gsub(/\u2019|\u2018|\u201a|\u201b/, "'")
.gsub(/\u201c|\u201d|\u201e|\u201f/, '"'), :basic
))
end
xmldoc
end

def xref_to_eref(xref)
super
xref.delete("format")
end

def xref_cleanup(xmldoc)
super
xmldoc.xpath("//xref").each do |x|
x.delete("displayFormat")
x.delete("relative")
end
end

def quotesource_cleanup(xmldoc)
xmldoc.xpath("//quote/source | //terms/source").each do |x|
if x["target"]&.match?(URI::DEFAULT_PARSER.make_regexp)
x["uri"] = x["target"]
x.delete("target")
else
xref_to_eref(x)
end
end
end

def section_names_refs_cleanup(xml); end
end
end
end
43 changes: 17 additions & 26 deletions lib/asciidoctor/ietf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative "./front"
require_relative "./blocks"
require_relative "./validate"
require_relative "./cleanup"

module Asciidoctor
module Ietf
Expand All @@ -22,13 +23,13 @@ def makexml(node)
@draft = node.attributes.has_key?("draft")
@workgroups = cache_workgroup(node)
@bcp_bold = !node.attr?("no-rfc-bold-bcp14")
@xinclude = node.attr?("use-xinclude")
@xinclude = node.attr?("use-xinclude")
super
end

def outputs(node, ret)
File.open(@filename + ".xml", "w:UTF-8") { |f| f.write(ret) }
rfc_converter(node).convert(@filename + ".xml")
File.open("#{@filename}.xml", "w:UTF-8") { |f| f.write(ret) }
rfc_converter(node).convert("#{@filename}.xml")
end

def doctype(node)
Expand Down Expand Up @@ -63,12 +64,12 @@ def inline_anchor_xref(node)
f, c = xref_text(node)
f1, c = eref_text(node) if f.nil?
t, rel = xref_rel(node)
attrs = { target: t, type: "inline", displayFormat: f1, format: f,
relative: rel }
noko do |xml|
xml.xref **attr_code(target: t, type: "inline",
displayFormat: f1, format: f,
relative: rel ) do |x|
x << c
end
xml.xref **attr_code(attrs) do |x|
x << c
end
end.join
end

Expand Down Expand Up @@ -195,34 +196,24 @@ def introduction_parse(attrs, xml, node)
clause_parse(attrs, xml, node)
end

def quotesource_cleanup(xmldoc)
xmldoc.xpath("//quote/source | //terms/source").each do |x|
if x["target"] =~ URI::DEFAULT_PARSER.make_regexp
x["uri"] = x["target"]
x.delete("target")
else
xref_to_eref(x)
end
end
end

def inline_indexterm(node)
noko do |xml|
node.type == :visible and xml << node.text.sub(/^primary:(?=\S)/, "")
terms = (node.attr("terms") || [node.text]).map { |x| xml_encode(x) }
if /^primary:\S/.match(terms[0])
if /^primary:\S/.match?(terms[0])
terms[0].sub!(/^primary:/, "")
has_primary = true
end
xml.index **attr_code(primary: has_primary) do |i|
i.primary { |x| x << terms[0] }
a = terms.dig(1) and i.secondary { |x| x << a }
a = terms.dig(2) and i.tertiary { |x| x << a }
end
inline_indexterm1(has_primary, terms, xml)
end.join
end

def section_names_refs_cleanup(x)
def inline_indexterm1(has_primary, terms, xml)
xml.index **attr_code(primary: has_primary) do |i|
i.primary { |x| x << terms[0] }
a = terms[1] and i.secondary { |x| x << a }
a = terms[2] and i.tertiary { |x| x << a }
end
end

def html_extract_attributes(node)
Expand Down
Loading