Skip to content

Commit

Permalink
Merge pull request #825 from metanorma/features/private-charset
Browse files Browse the repository at this point in the history
Features/private charset
  • Loading branch information
opoudjis authored Nov 8, 2023
2 parents 6e67dc3 + f4edd9d commit 0c38d5a
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 177 deletions.
112 changes: 112 additions & 0 deletions lib/metanorma/standoc/anchor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require "asciidoctor/extensions"

module Metanorma
module Standoc
module Inline
def inline_anchor(node)
case node.type
when :ref then inline_anchor_ref node
when :xref then inline_anchor_xref node
when :link then inline_anchor_link node
when :bibref then inline_anchor_bibref node
end
end

def inline_anchor_ref(node)
noko do |xml|
xml.bookmark nil, **attr_code(id: node.id)
end.join
end

def inline_anchor_xref(node)
noko do |xml|
attrs = inline_anchor_xref_attrs(node)
c = attrs[:text]
attrs.delete(:text) unless c.nil?
xml.xref **attr_code(attrs) do |x|
x << c
end
end.join
end

def inline_anchor_xref_attrs(node)
text = concatenate_attributes_to_xref_text(node)
m = inline_anchor_xref_match(text)
t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2")
m.nil? and return { target: t, type: "inline", text: text }
inline_anchor_xref_attrs1(m, t, text)
end

def concatenate_attributes_to_xref_text(node)
node.attributes.each_with_object([]) do |(k, v), m|
%w(path fragment refid).include?(k) and next
m << "#{k}=#{v}%"
end.map { |x| x.sub(/%+/, "%") }.join + (node.text || "")
end

def inline_anchor_xref_attrs1(match, target, text)
{ target: target, hidden: match[:hidden],
type: match[:fn].nil? ? "inline" : "footnote",
case: match[:case]&.sub(/%$/, ""),
style: match[:style]&.sub(/^style=/, "")&.sub(/%$/, "") || @xrefstyle,
droploc: match[:drop].nil? && match[:drop2].nil? ? nil : true,
text: inline_anchor_xref_text(match, text) }
end

def inline_anchor_xref_match(text)
/^(?:hidden%(?<hidden>[^,]+),?)?
(?<style>style=[^%]+%)?
(?<drop>droploc%)?(?<case>capital%|lowercase%)?(?<drop2>droploc%)?
(?<fn>fn:?\s*)?(?<text>.*)$/x.match text
end

def inline_anchor_xref_text(match, text)
if %i[case fn drop drop2 hidden style].any? { |x| !match[x].nil? }
match[:text]
else text
end
end

def inline_anchor_link(node)
contents = node.text
contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text
attributes = { target: node.target, alt: node.attr("title"),
"update-type": node.attr("updatetype") ||
node.attr("update-type") }
noko do |xml|
xml.link **attr_code(attributes) do |l|
l << contents
end
end.join
end

def inline_anchor_bibref(node)
eref_contents =
@c.decode(node.text || node.target || node.id)
&.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]")
@refids << (node.target || node.id)
noko do |xml|
xml.ref **attr_code(id: node.target || node.id) do |r|
r << eref_contents
end
end.join
end

def inline_callout(node)
noko do |xml|
xml.callout node.text
end.join
end

def inline_footnote(node)
@fn_number ||= 0
noko do |xml|
@fn_number += 1
xml.fn reference: @fn_number do |fn|
fn.p { |p| p << node.text }
end
end.join
end
end
end
end
3 changes: 1 addition & 2 deletions lib/metanorma/standoc/cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ def variant_space_cleanup(xmldoc)
end

def metadata_cleanup(xmldoc)
return if @metadata_attrs.nil? || @metadata_attrs.empty?

(@metadata_attrs.nil? || @metadata_attrs.empty?) and return
ins = add_misc_container(xmldoc)
ins << @metadata_attrs
end
Expand Down
3 changes: 1 addition & 2 deletions lib/metanorma/standoc/cleanup_toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ def toc_cleanup_clause_entry(xmldoc, list)
end

def toc_metadata(xmldoc)
return unless @htmltoclevels || @doctoclevels || @toclevels

@htmltoclevels || @doctoclevels || @toclevels or return
ins = add_misc_container(xmldoc)
toc_metadata1(ins)
end
Expand Down
1 change: 1 addition & 0 deletions lib/metanorma/standoc/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative "lists"
require_relative "ref"
require_relative "inline"
require_relative "anchor"
require_relative "blocks"
require_relative "section"
require_relative "table"
Expand Down
133 changes: 19 additions & 114 deletions lib/metanorma/standoc/inline.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "asciidoctor/extensions"
require "unicode2latex"
require "mime/types"
require "base64"
Expand All @@ -8,115 +7,6 @@
module Metanorma
module Standoc
module Inline
def refid?(ref)
@refids.include? ref
end

def inline_anchor(node)
case node.type
when :ref then inline_anchor_ref node
when :xref then inline_anchor_xref node
when :link then inline_anchor_link node
when :bibref then inline_anchor_bibref node
end
end

def inline_anchor_ref(node)
noko do |xml|
xml.bookmark nil, **attr_code(id: node.id)
end.join
end

def inline_anchor_xref(node)
noko do |xml|
attrs = inline_anchor_xref_attrs(node)
c = attrs[:text]
attrs.delete(:text) unless c.nil?
xml.xref **attr_code(attrs) do |x|
x << c
end
end.join
end

def inline_anchor_xref_attrs(node)
text = concatenate_attributes_to_xref_text(node)
m = inline_anchor_xref_match(text)
t = node.target.gsub(/^#/, "").gsub(%r{(\.xml|\.adoc)(#.*$)}, "\\2")
m.nil? and return { target: t, type: "inline", text: text }
inline_anchor_xref_attrs1(m, t, text)
end

def concatenate_attributes_to_xref_text(node)
node.attributes.each_with_object([]) do |(k, v), m|
%w(path fragment refid).include?(k) and next
m << "#{k}=#{v}%"
end.map { |x| x.sub(/%+/, "%") }.join + (node.text || "")
end

def inline_anchor_xref_attrs1(match, target, text)
{ target: target, hidden: match[:hidden],
type: match[:fn].nil? ? "inline" : "footnote",
case: match[:case]&.sub(/%$/, ""),
style: match[:style]&.sub(/^style=/, "")&.sub(/%$/, "") || @xrefstyle,
droploc: match[:drop].nil? && match[:drop2].nil? ? nil : true,
text: inline_anchor_xref_text(match, text) }
end

def inline_anchor_xref_match(text)
/^(?:hidden%(?<hidden>[^,]+),?)?
(?<style>style=[^%]+%)?
(?<drop>droploc%)?(?<case>capital%|lowercase%)?(?<drop2>droploc%)?
(?<fn>fn:?\s*)?(?<text>.*)$/x.match text
end

def inline_anchor_xref_text(match, text)
if %i[case fn drop drop2 hidden style].any? { |x| !match[x].nil? }
match[:text]
else text
end
end

def inline_anchor_link(node)
contents = node.text
contents = "" if node.target.gsub(%r{^mailto:}, "") == node.text
attributes = { target: node.target, alt: node.attr("title"),
"update-type": node.attr("updatetype") ||
node.attr("update-type") }
noko do |xml|
xml.link **attr_code(attributes) do |l|
l << contents
end
end.join
end

def inline_anchor_bibref(node)
eref_contents =
@c.decode(node.text || node.target || node.id)
&.sub(/^\[?([^\[\]]+?)\]?$/, "[\\1]")
@refids << (node.target || node.id)
noko do |xml|
xml.ref **attr_code(id: node.target || node.id) do |r|
r << eref_contents
end
end.join
end

def inline_callout(node)
noko do |xml|
xml.callout node.text
end.join
end

def inline_footnote(node)
@fn_number ||= 0
noko do |xml|
@fn_number += 1
xml.fn reference: @fn_number do |fn|
fn.p { |p| p << node.text }
end
end.join
end

def inline_break(node)
noko do |xml|
xml << node.text
Expand All @@ -137,8 +27,8 @@ def thematic_break(_node)

def latex_parse1(text, block)
lxm_input = Unicode2LaTeX.unicode2latex(@c.decode(text))
results = Plurimath::Math.parse(lxm_input, "latex")
.to_mathml(display_style: block)
results = Plurimath::Math.parse(lxm_input, "latex").
to_mathml(display_style: block)
if results.nil?
@log.add("Math", nil,
"latexmlmath failed to process equation:\n#{lxm_input}")
Expand All @@ -164,8 +54,8 @@ def latex_parse(text, xml, block)
latex = latex_parse1(text, block) or
return xml.stem type: "MathML", block: block
xml.stem type: "MathML", block: block do |s|
math = Nokogiri::XML.fragment(latex.sub(/<\?[^>]+>/, ""))
.elements[0]
math = Nokogiri::XML.fragment(latex.sub(/<\?[^>]+>/, "")).
elements[0]
math.delete("alttext")
s.parent.children = math
s << "<latexmath>#{text}</latexmath>"
Expand Down Expand Up @@ -202,13 +92,28 @@ def inline_quoted(node)
xml.span style: node.role.sub(/^css /, "") do |s|
s << node.text
end
when /:/
xml.span **attr_code(hash2styles(node.role)) do |s|
s << node.text
end
else
xml << node.text
end
end
end.join
end

def hash2styles(role)
CSV.parse_line(role, liberal_parsing: true).
each_with_object({}) do |r, m|
kv = r.split(":", 2).map(&:strip)
case kv[0]
when "custom-charset"
m[kv[0]] = kv[1]
end
end
end

def image_attributes(node)
nodetarget = node.attr("target") || node.target
if Gem.win_platform? && /^[a-zA-Z]:/.match?(nodetarget)
Expand Down
14 changes: 9 additions & 5 deletions lib/metanorma/standoc/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ def link_unwrap(para)
end

def xml_encode(text)
@c.encode(text, :basic, :hexadecimal)
.gsub("&amp;gt;", ">").gsub("&amp;lt;", "<").gsub("&amp;amp;", "&")
.gsub("&gt;", ">").gsub("&lt;", "<").gsub("&amp;", "&")
.gsub("&quot;", '"').gsub("&#xa;", "\n").gsub("&amp;#", "&#")
.gsub("&apos;", "'")
@c.encode(text, :basic, :hexadecimal).
gsub("&amp;gt;", ">").gsub("&amp;lt;", "<").gsub("&amp;amp;", "&").
gsub("&gt;", ">").gsub("&lt;", "<").gsub("&amp;", "&").
gsub("&quot;", '"').gsub("&#xa;", "\n").gsub("&amp;#", "&#").
gsub("&apos;", "'")
end

# wrapped in <sections>
Expand Down Expand Up @@ -104,6 +104,10 @@ def grkletters(text)
psi|omega)\b/xi, "&\\1;")
end

def refid?(ref)
@refids.include? ref
end

module_function :adoc2xml

class EmptyAttr
Expand Down
Loading

0 comments on commit 0c38d5a

Please sign in to comment.