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

prune XMDuals with mirrored single tokens #1327

Merged
merged 7 commits into from
Aug 23, 2020
Merged
28 changes: 24 additions & 4 deletions lib/LaTeXML/Core/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1443,11 +1443,31 @@ sub pruneXMDuals {
$self->compactXMDual($dual, $content, $presentation); } }
return; }

our @CONTENT_TRANSFER_ATTRS = qw(decl_id meaning name omcd);

sub compactXMDual {
my ($self, $dual, $content, $presentation) = @_;
# For now only handle compacting mirror applications
return if ($self->getNodeQName($content) ne 'ltx:XMApp') ||
($self->getNodeQName($presentation) ne 'ltx:XMApp');
my $c_name = $self->getNodeQName($content);
my $p_name = $self->getNodeQName($presentation);
# 1.Quick fix: merge two tokens
if (($c_name eq 'ltx:XMTok') && ($p_name eq 'ltx:XMTok')) {
for my $attr_key (@CONTENT_TRANSFER_ATTRS) {
if (my $attr_val = $content->getAttribute($attr_key)) {
$content->removeAttribute($attr_key);
$presentation->setAttribute($attr_key, $attr_val); } }
# if the dual has any attributes migrate them to the new XMTok
my %transfer_attrs = ();
for my $attr ($dual->attributes) {
if ($attr->nodeType == XML_ATTRIBUTE_NODE) {
$transfer_attrs{ $attr->nodeName } = $attr->getValue; } }
$self->replaceNode($dual, $presentation);
# transfer the attributes after replacing, so that the bookkeeping has been undone
for my $key (keys %transfer_attrs) {
$self->setAttribute($presentation, $key, $transfer_attrs{$key}); }
return; }

# 2.For now, only main use case is compacting mirror XMApp nodes
return if ($c_name ne 'ltx:XMApp') || ($p_name ne 'ltx:XMApp');
my @content_args = element_nodes($content);
my @pres_args = element_nodes($presentation);
return if scalar(@content_args) != scalar(@pres_args);
Expand Down Expand Up @@ -1483,7 +1503,7 @@ sub compactXMDual {
if (ref $n_arg eq 'ARRAY') {
my ($c_arg, $p_arg) = @$n_arg;
# Transfer all c_arg attributes over, it should be primary?
for my $attr_key (qw(decl_id meaning name omcd)) {
for my $attr_key (@CONTENT_TRANSFER_ATTRS) {
if (my $attr_val = $c_arg->getAttribute($attr_key)) {
$c_arg->removeAttribute($attr_key);
$p_arg->setAttribute($attr_key, $attr_val); } }
Expand Down
95 changes: 19 additions & 76 deletions t/complex/physics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1117,10 +1117,7 @@
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMDual>
<XMTok meaning="gradient"/>
<XMTok name="nabla" role="OPERATOR">∇</XMTok>
</XMDual>
<XMTok font="italic" meaning="gradient" name="nabla" role="OPERATOR">∇</XMTok>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I noticed that font="italic" gets added to the tokens when merged, I was wondering what causes that and how to avoid it - since I am pretty sure we don't want it added for the cases in the tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, figured it out, simply do not overwrite the presentation XMTok's attributes with attributes of the parent XMDual, in the cases where both have the same attribute. With a special exception where xml:id gets overwritten to be the XMDual id, in case it was being referenced from somewhere else.

</XMApp>
</XMath>
</Math></td>
Expand Down Expand Up @@ -1719,10 +1716,7 @@
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMDual>
<XMTok meaning="sine"/>
<XMTok role="OPFUNCTION" scriptpos="post">sin</XMTok>
</XMDual>
<XMTok font="italic" meaning="sine" role="OPFUNCTION" scriptpos="post">sin</XMTok>
<XMTok font="italic" role="UNKNOWN">x</XMTok>
</XMApp>
</XMApp>
Expand Down Expand Up @@ -1754,10 +1748,7 @@
<XMRef idref="S1.Ex1.m1.5"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex1.m1.4">
<XMTok meaning="sine"/>
<XMTok role="OPFUNCTION" scriptpos="post">sin</XMTok>
</XMDual>
<XMTok font="italic" meaning="sine" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex1.m1.4">sin</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex1.m1.5">
Expand All @@ -1782,10 +1773,7 @@
<XMRef idref="S1.Ex1.m1.7"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex1.m1.6">
<XMTok meaning="sine"/>
<XMTok role="OPFUNCTION" scriptpos="post">sin</XMTok>
</XMDual>
<XMTok font="italic" meaning="sine" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex1.m1.6">sin</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMTok font="italic" role="UNKNOWN" xml:id="S1.Ex1.m1.7">x</XMTok>
Expand Down Expand Up @@ -1851,10 +1839,7 @@
<XMRef idref="S1.Ex1.m1.10"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex1.m1.9">
<XMTok meaning="sine"/>
<XMTok role="OPFUNCTION" scriptpos="post">sin</XMTok>
</XMDual>
<XMTok font="italic" meaning="sine" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex1.m1.9">sin</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">{</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex1.m1.10">
Expand All @@ -1879,10 +1864,7 @@
<XMRef idref="S1.Ex1.m1.12"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex1.m1.11">
<XMTok meaning="sine"/>
<XMTok role="OPFUNCTION" scriptpos="post">sin</XMTok>
</XMDual>
<XMTok font="italic" meaning="sine" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex1.m1.11">sin</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMTok font="italic" role="UNKNOWN" xml:id="S1.Ex1.m1.12">x</XMTok>
Expand Down Expand Up @@ -2657,10 +2639,7 @@
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMDual>
<XMTok meaning="trace"/>
<XMTok role="OPFUNCTION" scriptpos="post">tr</XMTok>
</XMDual>
<XMTok font="italic" meaning="trace" role="OPFUNCTION" scriptpos="post">tr</XMTok>
<XMTok font="italic" name="rho" role="UNKNOWN">ρ</XMTok>
</XMApp>
</XMApp>
Expand Down Expand Up @@ -2709,10 +2688,7 @@
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMDual>
<XMTok meaning="trace"/>
<XMTok role="OPFUNCTION" scriptpos="post">Tr</XMTok>
</XMDual>
<XMTok font="italic" meaning="trace" role="OPFUNCTION" scriptpos="post">Tr</XMTok>
<XMTok font="italic" name="rho" role="UNKNOWN">ρ</XMTok>
</XMApp>
</XMApp>
Expand All @@ -2728,10 +2704,7 @@
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMApp>
<XMDual>
<XMTok meaning="rank"/>
<XMTok role="OPFUNCTION" scriptpos="post">rank</XMTok>
</XMDual>
<XMTok font="italic" meaning="rank" role="OPFUNCTION" scriptpos="post">rank</XMTok>
<XMTok font="italic" role="UNKNOWN">M</XMTok>
</XMApp>
</XMApp>
Expand Down Expand Up @@ -2778,10 +2751,7 @@
<XMRef idref="S1.T3.m6.3"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.T3.m6.2">
<XMTok meaning="residue"/>
<XMTok role="OPFUNCTION" scriptpos="post">Res</XMTok>
</XMDual>
<XMTok font="italic" meaning="residue" role="OPFUNCTION" scriptpos="post" xml:id="S1.T3.m6.2">Res</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMApp xml:id="S1.T3.m6.3">
Expand Down Expand Up @@ -2988,10 +2958,7 @@
<XMRef idref="S1.Ex2.m1.2"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex2.m1.1">
<XMTok meaning="realport"/>
<XMTok role="OPFUNCTION" scriptpos="post">Re</XMTok>
</XMDual>
<XMTok font="italic" meaning="realport" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex2.m1.1">Re</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">(</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex2.m1.2">
Expand All @@ -3014,10 +2981,7 @@
<XMRef idref="S1.Ex2.m1.4"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex2.m1.3">
<XMTok meaning="realport"/>
<XMTok role="OPFUNCTION" scriptpos="post">Re</XMTok>
</XMDual>
<XMTok font="italic" meaning="realport" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex2.m1.3">Re</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex2.m1.4">
Expand All @@ -3040,10 +3004,7 @@
<XMRef idref="S1.Ex2.m1.6"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex2.m1.5">
<XMTok meaning="imagport"/>
<XMTok role="OPFUNCTION" scriptpos="post">Im</XMTok>
</XMDual>
<XMTok font="italic" meaning="imagport" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex2.m1.5">Im</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">(</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex2.m1.6">
Expand All @@ -3066,10 +3027,7 @@
<XMRef idref="S1.Ex2.m1.8"/>
</XMApp>
<XMApp>
<XMDual xml:id="S1.Ex2.m1.7">
<XMTok meaning="imagport"/>
<XMTok role="OPFUNCTION" scriptpos="post">Im</XMTok>
</XMDual>
<XMTok font="italic" meaning="imagport" role="OPFUNCTION" scriptpos="post" xml:id="S1.Ex2.m1.7">Im</XMTok>
<XMWrap>
<XMTok role="OPEN" stretchy="false">[</XMTok>
<XMApp backgroundcolor="#00FFFF" xml:id="S1.Ex2.m1.8">
Expand Down Expand Up @@ -3307,10 +3265,7 @@
<XMApp>
<XMTok name="rightarrow" role="ARROW">→</XMTok>
<XMTok meaning="absent"/>
<XMDual>
<XMTok meaning="differential"/>
<XMTok role="UNKNOWN">d</XMTok>
</XMDual>
<XMTok font="italic" meaning="differential" role="UNKNOWN">d</XMTok>
</XMApp>
</XMath>
</Math></td>
Expand All @@ -3325,10 +3280,7 @@
<XMTok meaning="absent"/>
<XMApp>
<XMTok meaning="times" role="MULOP">⁢</XMTok>
<XMDual>
<XMTok meaning="differential"/>
<XMTok role="UNKNOWN">d</XMTok>
</XMDual>
<XMTok font="italic" meaning="differential" role="UNKNOWN">d</XMTok>
<XMTok font="italic" role="UNKNOWN">x</XMTok>
</XMApp>
</XMApp>
Expand Down Expand Up @@ -3399,10 +3351,7 @@
<XMWrap>
<XMTok role="OPEN" stretchy="true">(</XMTok>
<XMApp xml:id="S1.T6.m6.1">
<XMDual>
<XMTok meaning="cosine"/>
<XMTok role="OPFUNCTION" scriptpos="post">cos</XMTok>
</XMDual>
<XMTok font="italic" meaning="cosine" role="OPFUNCTION" scriptpos="post">cos</XMTok>
<XMTok font="italic" name="theta" role="UNKNOWN">θ</XMTok>
</XMApp>
<XMTok role="CLOSE" stretchy="true">)</XMTok>
Expand Down Expand Up @@ -6616,19 +6565,13 @@
<XMCell align="center">
<XMApp>
<XMTok fontsize="70%" meaning="minus" role="ADDOP">-</XMTok>
<XMDual role="UNKNOWN">
<XMTok meaning="imaginary-unit" name="lx@physics@iunit" role="UNKNOWN"/>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">i</XMTok>
</XMDual>
<XMTok font="italic" fontsize="70%" meaning="imaginary-unit" name="lx@physics@iunit" role="UNKNOWN">i</XMTok>
</XMApp>
</XMCell>
</XMRow>
<XMRow>
<XMCell align="center">
<XMDual role="UNKNOWN">
<XMTok meaning="imaginary-unit" name="lx@physics@iunit" role="UNKNOWN"/>
<XMTok font="italic" fontsize="70%" role="UNKNOWN">i</XMTok>
</XMDual>
<XMTok font="italic" fontsize="70%" meaning="imaginary-unit" name="lx@physics@iunit" role="UNKNOWN">i</XMTok>
</XMCell>
<XMCell align="center">
<XMTok fontsize="70%" meaning="0" role="NUMBER">0</XMTok>
Expand Down
Loading