From 044f6348957f702a5569003a2ac7bc2d2b6b81dc Mon Sep 17 00:00:00 2001 From: Nick Nicholas Date: Fri, 7 Apr 2023 20:55:01 +1000 Subject: [PATCH] sourcecode keys instead of annotations: https://github.com/metanorma/metanorma-standoc/issues/764 --- lib/isodoc/function/blocks.rb | 21 +- lib/isodoc/html_function/html.rb | 1 + .../presentation_function/sourcecode.rb | 32 ++- spec/isodoc/sourcecode_spec.rb | 185 ++++++++++++++---- 4 files changed, 191 insertions(+), 48 deletions(-) diff --git a/lib/isodoc/function/blocks.rb b/lib/isodoc/function/blocks.rb index ae16185b..57f86e6a 100644 --- a/lib/isodoc/function/blocks.rb +++ b/lib/isodoc/function/blocks.rb @@ -76,6 +76,7 @@ def sourcecode_parse(node, out) out.p **sourcecode_attrs(node) do |div| sourcecode_parse1(node, div) end + annotation_parse(node, out) sourcecode_name_parse(node, out, name) end @@ -84,9 +85,10 @@ def sourcecode_parse1(node, div) node.at(ns(".//table[@class = 'rouge-line-table']")) || node.at("./ancestor::xmlns:table[@class = 'rouge-line-table']") and @sourcecode = "table" - # !node.ancestors("table").empty? and - # @sourcecode = "table" - node.children.each { |n| parse(n, div) unless n.name == "name" } + node.children.each do |n| + %w(name dl).include?(n.name) and next + parse(n, div) + end @sourcecode = false end @@ -95,17 +97,12 @@ def pre_parse(node, out) end def annotation_parse(node, out) + dl = node.at(ns("./dl")) or return @sourcecode = false - @annotation = true + # @annotation = true out.div class: "annotation" do |div| - # node.at("./preceding-sibling::*[local-name() = 'annotation']") or - # div << "
" - callout = node.at(ns("//callout[@target='#{node['id']}']")) - div << "<#{callout.text}> " - div << "#{node.children&.text&.strip}" - node.at("./following-sibling::*[local-name() = 'annotation']") and - div << "
" - @annotation = false + parse(dl, div) + # @annotation = false end end diff --git a/lib/isodoc/html_function/html.rb b/lib/isodoc/html_function/html.rb index 9e2d775f..78cef941 100644 --- a/lib/isodoc/html_function/html.rb +++ b/lib/isodoc/html_function/html.rb @@ -96,6 +96,7 @@ def sourcecode_parse(node, out) out.send tag, **attr do |div| sourcecode_parse1(node, div) end + annotation_parse(node, out) sourcecode_name_parse(node, out, name) end diff --git a/lib/isodoc/presentation_function/sourcecode.rb b/lib/isodoc/presentation_function/sourcecode.rb index e2782f86..47f0108c 100644 --- a/lib/isodoc/presentation_function/sourcecode.rb +++ b/lib/isodoc/presentation_function/sourcecode.rb @@ -28,9 +28,16 @@ def sourcehighlighter { formatter: f, formatter_line: f1 } end + def callouts(elem) + elem.xpath(ns(".//callout")).each do |c| + @callouts[c["target"]] = c.children.to_xml + end + end + def sourcecode(docxml) sourcehighlighter_css(docxml) @highlighter = sourcehighlighter + @callouts = {} docxml.xpath(ns("//sourcecode")).each do |f| sourcecode1(f) end @@ -39,6 +46,21 @@ def sourcecode(docxml) def sourcecode1(elem) source_highlight(elem) source_label(elem) + callouts(elem) + annotations(elem) + end + + def annotations(elem) + elem.at(ns("./annotation")) or return + ret = "" + elem.xpath(ns("./annotation")).each do |a| + a.remove + ret += <<~OUT +
#{@callouts[a['id']]}
+
#{a.children.to_xml}
+ OUT + end + elem << "
#{@i18n.key}#{ret}
" end def source_highlight(elem) @@ -64,7 +86,7 @@ def source_remove_markup(elem) def source_remove_annotations(ret, elem) ret[:ann] = elem.xpath(ns("./annotation")).each(&:remove) ret[:call] = elem.xpath(ns("./callout")).each_with_object([]) do |c, m| - m << { xml: c.remove.to_xml, line: c.line - elem.line } + m << { xml: c.remove, line: c.line - elem.line } end ret end @@ -84,7 +106,7 @@ def source_restore_callouts(code, callouts) text = to_xml(code) text.split(/[\n\r]/).each_with_index do |c, i| while !callouts.empty? && callouts[0][:line] == i - c.sub!(/\s+$/, " #{callouts[0][:xml]} ") + c.sub!(/\s+$/, " #{reinsert_callout(callouts[0][:xml])} ") callouts.shift end end.join("\n") @@ -94,12 +116,16 @@ def source_restore_callouts_table(table, callouts) table.xpath(".//td[@class = 'rouge-code']/sourcecode") .each_with_index do |c, i| while !callouts.empty? && callouts[0][:line] == i - c << " #{callouts[0][:xml]} " + c << " #{reinsert_callout(callouts[0][:xml])} " callouts.shift end end end + def reinsert_callout(xml) + "#{to_xml(xml)}" + end + def sourcecode_table_to_elem(elem, tokens) r = Nokogiri::XML(@highlighter[:formatter_line].format(tokens)).root r.xpath(".//td[@class = 'rouge-code']/pre").each do |pre| diff --git a/spec/isodoc/sourcecode_spec.rb b/spec/isodoc/sourcecode_spec.rb index 66671622..bf6aaffd 100644 --- a/spec/isodoc/sourcecode_spec.rb +++ b/spec/isodoc/sourcecode_spec.rb @@ -516,10 +516,10 @@ - Figure 1puts "Hello, world." 12 + Figure 1puts "Hello, world." 12 %w{a b c}.each do |x| puts x 3 - end

This is one callout

This is another callout

This is yet another callout

+ end
Key
1

This is one callout

2

This is another callout

3

This is yet another callout

INPUT @@ -528,7 +528,30 @@

Foreword

-
puts "Hello, world." <1> <2>
   %w{a b c}.each do |x|
     puts x <3>
     end
<1> This is one callout
<2> This is another callout
<3> This is yet another callout
+
puts "Hello, world."  <1> <2> 
   %w{a b c}.each do |x|
     puts x <3>
   end
+
+

Key

+
+
+ 1 +
+
+

This is one callout

+
+
+ 2 +
+
+

This is another callout

+
+
+ 3 +
+
+

This is yet another callout

+
+
+

Figure 1

@@ -552,7 +575,42 @@

Foreword

-

puts "Hello, world." <1><2>
   %w{a b c}.each do |x|
     puts x <3>
     end

<1>This is one callout
<2>This is another callout
<3>This is yet another callout

+

puts "Hello, world." <1> <2>
   %w{a b c}.each do |x|
     puts x <3>
   end

+
+

Key

+ + + + + + + + + + + + + +
+

+ 1 +

+
+

This is one callout

+
+

+ 2 +

+
+

This is another callout

+
+

+ 3 +

+
+

This is yet another callout

+
+

Figure 1

 

@@ -640,15 +698,21 @@ - -

This is one callout

-
- -

This is another callout

-
- -

This is yet another callout

-
+
+ Key +
1
+
+

This is one callout

+
+
2
+
+

This is another callout

+
+
3
+
+

This is yet another callout

+
+
@@ -665,11 +729,30 @@                 
1
                               
puts "Hello, world."  <1>   <2> 
-
2
   %w{a b c}.each do |x|
3
     puts x  <3> 
4
   end
-
<1>This is one callout
-
<2>This is another callout
-
<3>This is yet another callout
- +
2
   %w{a b c}.each do |x|
3
     puts x  <3> 
4
   end
+
+

Key

+
+
+ 1 +
+
+

This is one callout

+
+
+ 2 +
+
+

This is another callout

+
+
+ 3 +
+
+

This is yet another callout

+
+
+

Figure 1

@@ -700,10 +783,42 @@                                

puts "Hello, world." <1>  <2>

2

   %w{a b c}.each do |x|

3

     puts x <3>

4

   end

-
<1>This is one callout
-
<2>This is another callout
-
<3>This is yet another callout

+
+

Key

+ + + + + + + + + + + + + +
+

+ 1 +

+
+

This is one callout

+
+

+ 2 +

+
+

This is another callout

+
+

+ 3 +

+
+

This is yet another callout

+
+

Figure 1

 

@@ -762,12 +877,12 @@ puts "Hello, world." - - 1 - - - 2 - + + 1 + + + 2 + @@ -799,9 +914,9 @@ x - - 3 - + + 3 + @@ -825,9 +940,13 @@ - -

This is one callout

-
+
+ Key +
1
+
+

This is one callout

+
+