From eec013dcbce55a4ad6a2efc79e18a4372379a173 Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Wed, 8 Nov 2023 23:17:15 +1100 Subject: [PATCH 1/2] custom-charset for Private Use character codepoints: https://github.com/metanorma/metanorma/issues/339: --- lib/metanorma/standoc/anchor.rb | 112 ++++++++++++++++++++++ lib/metanorma/standoc/cleanup.rb | 3 +- lib/metanorma/standoc/cleanup_toc.rb | 3 +- lib/metanorma/standoc/converter.rb | 1 + lib/metanorma/standoc/inline.rb | 133 ++++----------------------- lib/metanorma/standoc/utils.rb | 14 ++- spec/metanorma/inline_spec.rb | 2 + 7 files changed, 145 insertions(+), 123 deletions(-) create mode 100644 lib/metanorma/standoc/anchor.rb diff --git a/lib/metanorma/standoc/anchor.rb b/lib/metanorma/standoc/anchor.rb new file mode 100644 index 00000000..f43607d6 --- /dev/null +++ b/lib/metanorma/standoc/anchor.rb @@ -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%(?[^,]+),?)? + (?