Skip to content

Commit

Permalink
sourcecode keys instead of annotations: metanorma/metanorma-standoc#764
Browse files Browse the repository at this point in the history
  • Loading branch information
opoudjis committed Apr 7, 2023
1 parent 6af80c4 commit 044f634
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 48 deletions.
21 changes: 9 additions & 12 deletions lib/isodoc/function/blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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 << "<br/>"
callout = node.at(ns("//callout[@target='#{node['id']}']"))
div << "<span class='c'>&lt;#{callout.text}&gt;</span> "
div << "<span class='c'>#{node.children&.text&.strip}</span>"
node.at("./following-sibling::*[local-name() = 'annotation']") and
div << "<br/>"
@annotation = false
parse(dl, div)
# @annotation = false
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/isodoc/html_function/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 29 additions & 3 deletions lib/isodoc/presentation_function/sourcecode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<dt id='#{a['id']}'><span class='c'>#{@callouts[a['id']]}</span></dt>
<dd>#{a.children.to_xml}</dd>
OUT
end
elem << "<dl><name>#{@i18n.key}</name>#{ret}</dl>"
end

def source_highlight(elem)
Expand All @@ -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
Expand All @@ -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+$/, " <span class='c'>#{callouts[0][:xml]}</span> ")
c.sub!(/\s+$/, " #{reinsert_callout(callouts[0][:xml])} ")
callouts.shift
end
end.join("\n")
Expand All @@ -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 << " <span class='c'>#{callouts[0][:xml]}</span> "
c << " #{reinsert_callout(callouts[0][:xml])} "
callouts.shift
end
end
end

def reinsert_callout(xml)
"<span class='c'>#{to_xml(xml)}</span>"
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|
Expand Down
185 changes: 152 additions & 33 deletions spec/isodoc/sourcecode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,10 @@
<iso-standard xmlns="http://riboseinc.com/isoxml" type="presentation">
<bibdata/>
<preface><foreword displayorder="1">
<sourcecode id="_"><name>Figure 1</name>puts "Hello, world." <span class="c"><callout target="A">1</callout></span><span class="c"><callout target="B">2</callout></span>
<sourcecode id="_"><name>Figure 1</name>puts "Hello, world." <span class="c"><callout target="A">1</callout></span><span class="c"><callout target="B">2</callout></span>
%w{a b c}.each do |x|
puts x <span class="c"><callout target="C">3</callout></span>
end<annotation id="A"><p id="_">This is <em>one</em> callout</p></annotation><annotation id="B"><p id="_">This is another callout</p></annotation><annotation id="C"><p id="_">This is yet another callout</p></annotation></sourcecode>
end<dl><name>Key</name><dt id="A"><span class="c">1</span></dt><dd><p id="_">This is <em>one</em> callout</p></dd><dt id="B"><span class="c">2</span></dt><dd><p id="_">This is another callout</p></dd><dt id="C"><span class="c">3</span></dt><dd><p id="_">This is yet another callout</p></dd></dl></sourcecode>
</foreword></preface>
</iso-standard>
INPUT
Expand All @@ -528,7 +528,30 @@
<br/>
<div>
<h1 class="ForewordTitle">Foreword</h1>
<pre id="_" class="sourcecode">puts "Hello, world." <span class="c">&lt;1&gt;</span> <span class="c">&lt;2&gt;</span><br/>   %w{a b c}.each do |x|<br/>     puts x <span class="c">&lt;3&gt;</span> <br/>     end<div class="annotation"><span class="c">&lt;1&gt;</span> <span class="c">This is one callout</span><br/></div><div class="annotation"><span class="c">&lt;2&gt;</span> <span class="c">This is another callout</span><br/></div><div class="annotation"><span class="c">&lt;3&gt;</span> <span class="c">This is yet another callout</span></div></pre>
<pre id="_" class="sourcecode">puts "Hello, world." <span class="c"> &lt;1&gt;</span><span class="c"> &lt;2&gt;</span> <br/>   %w{a b c}.each do |x|<br/>     puts x <span class="c"> &lt;3&gt;</span><br/>   end</pre>
<div class="annotation">
<p class="ListTitle">Key</p>
<dl>
<dt id="A">
<span class="c">1</span>
</dt>
<dd>
<p id="_">This is <i>one</i> callout</p>
</dd>
<dt id="B">
<span class="c">2</span>
</dt>
<dd>
<p id="_">This is another callout</p>
</dd>
<dt id="C">
<span class="c">3</span>
</dt>
<dd>
<p id="_">This is yet another callout</p>
</dd>
</dl>
</div>
<p class="SourceTitle" style="text-align:center;">Figure 1</p>
</div>
<p class="zzSTDTitle1"/>
Expand All @@ -552,7 +575,42 @@
</p>
<div>
<h1 class="ForewordTitle">Foreword</h1>
<p id="_" class="Sourcecode">puts "Hello, world." <span class="c">&lt;1&gt;</span><span class="c">&lt;2&gt;</span><br/>   %w{a b c}.each do |x|<br/>     puts x <span class="c">&lt;3&gt;</span> <br/>     end<div class="annotation"><span class="c">&lt;1&gt;</span><span class="c">This is one callout</span><br/></div><div class="annotation"><span class="c">&lt;2&gt;</span><span class="c">This is another callout</span><br/></div><div class="annotation"><span class="c">&lt;3&gt;</span><span class="c">This is yet another callout</span></div></p>
<p id="_" class="Sourcecode">puts "Hello, world." <span class="c"> &lt;1&gt;</span><span class="c"> &lt;2&gt;</span> <br/>   %w{a b c}.each do |x|<br/>     puts x <span class="c"> &lt;3&gt;</span><br/>   end</p>
<div class="annotation">
<p class="ListTitle">Key</p>
<table class="dl">
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">1</span>
</p>
</td>
<td valign="top">
<p id="_">This is <i>one</i> callout</p>
</td>
</tr>
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">2</span>
</p>
</td>
<td valign="top">
<p id="_">This is another callout</p>
</td>
</tr>
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">3</span>
</p>
</td>
<td valign="top">
<p id="_">This is yet another callout</p>
</td>
</tr>
</table>
</div>
<p class="SourceTitle" style="text-align:center;">Figure 1</p>
</div>
<p> </p>
Expand Down Expand Up @@ -640,15 +698,21 @@
</tr>
</tbody>
</table>
<annotation id="A">
<p id="_">This is <em>one</em> callout</p>
</annotation>
<annotation id="B">
<p id="_">This is another callout</p>
</annotation>
<annotation id="C">
<p id="_">This is yet another callout</p>
</annotation>
<dl>
<name>Key</name>
<dt id="A"><span class="c">1</span></dt>
<dd>
<p id="_">This is <em>one</em> callout</p>
</dd>
<dt id="B"><span class="c">2</span></dt>
<dd>
<p id="_">This is another callout</p>
</dd>
<dt id="C"><span class="c">3</span></dt>
<dd>
<p id="_">This is yet another callout</p>
</dd>
</dl>
</sourcecode>
</foreword>
</preface>
Expand All @@ -665,11 +729,30 @@
                 <pre>1</pre>
               </td><td style="" class="rouge-code">
                 <pre class="sourcecode">puts "Hello, world." <span class="c"> &lt;1&gt;</span>  <span class="c"> &lt;2&gt;</span> </pre>
</td></tr><tr><td style="" class="rouge-gutter gl"><pre>2</pre></td><td style="" class="rouge-code"><pre class="sourcecode">   %w{a b c}.each do |x|</pre></td></tr><tr><td style="" class="rouge-gutter gl"><pre>3</pre></td><td style="" class="rouge-code"><pre class="sourcecode">     puts x <span class="c"> &lt;3&gt;</span> </pre></td></tr><tr><td style="" class="rouge-gutter gl"><pre>4</pre></td><td style="" class="rouge-code"><pre class="sourcecode">   end</pre></td></tr></tbody></table>
<div class="annotation"><span class="c">&lt;1&gt;</span><span class="c">This is one callout</span><br/></div>
<div class="annotation"><span class="c">&lt;2&gt;</span><span class="c">This is another callout</span><br/></div>
<div class="annotation"><span class="c">&lt;3&gt;</span><span class="c">This is yet another callout</span></div>
</div>
</td></tr><tr><td style="" class="rouge-gutter gl"><pre>2</pre></td><td style="" class="rouge-code"><pre class="sourcecode">   %w{a b c}.each do |x|</pre></td></tr><tr><td style="" class="rouge-gutter gl"><pre>3</pre></td><td style="" class="rouge-code"><pre class="sourcecode">     puts x <span class="c"> &lt;3&gt;</span> </pre></td></tr><tr><td style="" class="rouge-gutter gl"><pre>4</pre></td><td style="" class="rouge-code"><pre class="sourcecode">   end</pre></td></tr></tbody></table></div>
<div class="annotation">
<p class="ListTitle">Key</p>
<dl>
<dt id="A">
<span class="c">1</span>
</dt>
<dd>
<p id="_">This is <i>one</i> callout</p>
</dd>
<dt id="B">
<span class="c">2</span>
</dt>
<dd>
<p id="_">This is another callout</p>
</dd>
<dt id="C">
<span class="c">3</span>
</dt>
<dd>
<p id="_">This is yet another callout</p>
</dd>
</dl>
</div>
<p class="SourceTitle" style="text-align:center;">Figure 1</p>
</div>
<p class="zzSTDTitle1"/>
Expand Down Expand Up @@ -700,10 +783,42 @@
               </td><td style="page-break-after:avoid;" class="rouge-code">
                 <p class="Sourcecode">puts "Hello, world." <span class="c"> &lt;1&gt;</span>  <span class="c"> &lt;2&gt;</span> </p>
</td></tr><tr><td style="page-break-after:avoid;" class="rouge-gutter gl"><pre>2</pre></td><td style="page-break-after:avoid;" class="rouge-code"><p class="Sourcecode">   %w{a b c}.each do |x|</p></td></tr><tr><td style="page-break-after:avoid;" class="rouge-gutter gl"><pre>3</pre></td><td style="page-break-after:avoid;" class="rouge-code"><p class="Sourcecode">     puts x <span class="c"> &lt;3&gt;</span> </p></td></tr><tr><td style="page-break-after:auto;" class="rouge-gutter gl"><pre>4</pre></td><td style="page-break-after:auto;" class="rouge-code"><p class="Sourcecode">   end</p></td></tr></tbody></table></div>
<div class="annotation"><span class="c">&lt;1&gt;</span><span class="c">This is one callout</span><br/></div>
<div class="annotation"><span class="c">&lt;2&gt;</span><span class="c">This is another callout</span><br/></div>
<div class="annotation"><span class="c">&lt;3&gt;</span><span class="c">This is yet another callout</span></div>
</p>
<div class="annotation">
<p class="ListTitle">Key</p>
<table class="dl">
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">1</span>
</p>
</td>
<td valign="top">
<p id="_">This is <i>one</i> callout</p>
</td>
</tr>
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">2</span>
</p>
</td>
<td valign="top">
<p id="_">This is another callout</p>
</td>
</tr>
<tr>
<td valign="top" align="left">
<p align="left" style="margin-left:0pt;text-align:left;">
<span class="c">3</span>
</p>
</td>
<td valign="top">
<p id="_">This is yet another callout</p>
</td>
</tr>
</table>
</div>
<p class="SourceTitle" style="text-align:center;">Figure 1</p>
</div>
<p> </p>
Expand Down Expand Up @@ -762,12 +877,12 @@
<sourcecode>
<span class="nb">puts</span>
<span class="s2">"Hello, world."</span>
<span class="c">
<callout target="A">1</callout>
</span>
<span class="c">
<callout target="B">2</callout>
</span>
<span class="c">
<callout target="A">1</callout>
</span>
<span class="c">
<callout target="B">2</callout>
</span>
</sourcecode>
</td>
</tr>
Expand Down Expand Up @@ -799,9 +914,9 @@
<xref target="X">
<span class="n">x</span>
</xref>
<span class="c">
<callout target="C">3</callout>
</span>
<span class="c">
<callout target="C">3</callout>
</span>
</sourcecode>
</td>
</tr>
Expand All @@ -825,9 +940,13 @@
</tr>
</tbody>
</table>
<annotation id="A">
<p id="_">This is <em>one</em> callout</p>
</annotation>
<dl>
<name>Key</name>
<dt id="A"><span class="c">1</span></dt>
<dd>
<p id="_">This is <em>one</em> callout</p>
</dd>
</dl>
</sourcecode>
</foreword>
</preface>
Expand Down

0 comments on commit 044f634

Please sign in to comment.