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

To attribute #2465

Merged
merged 7 commits into from
Jan 8, 2025
10 changes: 8 additions & 2 deletions lib/LaTeXML/Core/Document.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,14 @@ sub makeError {
# [xml:id and namespaced attributes are always allowed]
sub setAttribute {
my ($self, $node, $key, $value) = @_;
return if (ref $value) && ((!blessed($value)) || !$value->can('toAttribute'));
$value = $value->toAttribute if ref $value;
return if (ref $value) && ((!blessed($value)) || !$value->can('toAttribute'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be useful to emit a Warning if this unusual case is encountered, where $value was e.g. a perl array ref or hash ref? Could be some rare mistake that gets caught quickly with a warning message.

Copy link
Owner Author

Choose a reason for hiding this comment

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

good idea; done!

if (ref $value) {
if ($key eq '_box') {
return $self->setNodeBox($node, $value); }
elsif ($key eq '_font') {
return $self->setNodeFont($node, $value); }
else {
$value = $value->toAttribute; } }
if ((defined $value) && ($value ne '')) { # Skip if `empty'; but 0 is OK!
if ($key eq 'xml:id') { # If it's an ID attribute
$value = recordID($self, $value, $node); # Do id book keeping
Expand Down
6 changes: 5 additions & 1 deletion lib/LaTeXML/Core/List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use LaTeXML::Common::Object;
use LaTeXML::Common::Error;
use LaTeXML::Common::Dimension;
use List::Util qw(min max);
use base qw(Exporter LaTeXML::Core::Box);
use base qw(Exporter LaTeXML::Core::Box);
our @EXPORT = (qw(&List));

# Tricky; don't really want a separate constructor for a Math List,
Expand Down Expand Up @@ -71,6 +71,10 @@ sub toString {
my ($self) = @_;
return join('', grep { defined $_ } map { $_->toString } $self->unlist); }

sub toAttribute {
my ($self) = @_;
return join('', grep { defined $_ } map { $_->toAttribute } $self->unlist); }

# Methods for overloaded operators
sub stringify {
no warnings 'recursion';
Expand Down
25 changes: 25 additions & 0 deletions lib/LaTeXML/Core/Whatsit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,31 @@ sub beAbsorbed {
LaTeXML::Core::Definition::stopProfiling($profiled, 'absorb') if $profiled;
return @result; }

# Similar to ->revert, but converts to pure string for use in an attribute value
sub toAttribute {
my ($self) = @_;
my $props = $$self{properties};
my $defn = $self->getDefinition;
my $spec = $$props{toAttribute} || $$defn{toAttribute};
if (!defined $spec) {
return $self->toString; } # Default
elsif (ref $spec eq 'CODE') { # If handled by CODE, call it
$spec = &$spec($self, $self->getArgs); }
# Now, similar to substituteParameters, but creating a string.
$spec =~ s/#(#|[1-9]|\w+)/ toAttribute_aux($self,$1)/eg;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, I think I understand the code here, but I am wondering if we should have a an example DefConstructor that uses this argument-based toAttribute string form, and a test that exercises it.

Copy link
Owner Author

Choose a reason for hiding this comment

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

The conceit is that it should work just like reversion.

return $spec; }

sub toAttribute_aux {
my ($self, $code) = @_;
my $value;
if ($code eq '#') { return $code; }
elsif ((ord($code) > ord('0')) && (ord($code) <= ord('9'))) {
$value = $self->getArg(ord($code) - ord('0')); }
else {
$value = $self->getProperty($code); }
$value = $value->toAttribute if (defined $value) && (ref $value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor, but ref undef works fine, so defined $value is redundnat.

Copy link
Owner Author

Choose a reason for hiding this comment

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

indeed; fixed.

return $value; }

# See discussion in Box.pm
sub computeSize {
my ($self, %options) = @_;
Expand Down
8 changes: 4 additions & 4 deletions lib/LaTeXML/Engine/LaTeX.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -1211,9 +1211,9 @@ DefConstructor('\lx@author@prefix', sub {
my $i = scalar(@{ $document->findnodes('//ltx:creator[@role="author"]') });
if ($i <= 1) { }
elsif ($i == $nauthors) {
$document->setAttribute($node, before => ToString(Digest(T_CS('\lx@author@conj')))); }
$document->setAttribute($node, before => Digest(T_CS('\lx@author@conj'))); }
else {
$document->setAttribute($node, before => ToString(Digest(T_CS('\lx@author@sep')))); }
$document->setAttribute($node, before => Digest(T_CS('\lx@author@sep'))); }
});

DefMacro('\@author', '\@empty');
Expand Down Expand Up @@ -4376,8 +4376,8 @@ DefConstructor('\@@bibref Semiverbatim Semiverbatim {}{}',
"<ltx:bibref show='#1' bibrefs='#bibrefs' inlist='#bibunit'"
. " separator='#separator' yyseparator='#yyseparator'>#3#4</ltx:bibref>",
properties => sub { (bibrefs => CleanBibKey($_[2]),
separator => ToString(Digest(LookupValue('CITE_SEPARATOR'))),
yyseparator => ToString(Digest(LookupValue('CITE_YY_SEPARATOR'))),
separator => Digest(LookupValue('CITE_SEPARATOR')),
yyseparator => Digest(LookupValue('CITE_YY_SEPARATOR')),
bibunit => LookupValue('CITE_UNIT')); });

# Simple container for any phrases used in the bibref
Expand Down
5 changes: 3 additions & 2 deletions lib/LaTeXML/Engine/TeX_Glue.pool.ltxml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ DefConstructor('\hskip Glue', sub {
else {
# $document->openText(DimensionToSpaces($length), $props{font}); } },
$document->absorb(DimensionToSpaces($length)); } },
reversion => sub { revertSkip(T_CS('\hskip'), $_[1]); },
properties => sub {
reversion => sub { revertSkip(T_CS('\hskip'), $_[1]); },
toAttribute => sub { DimensionToSpaces($_[1]); },
properties => sub {
my ($stomach, $length) = @_;
(width => $length, isSpace => 1, isSkip => 1); });

Expand Down
24 changes: 18 additions & 6 deletions lib/LaTeXML/Package.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ sub flatten {
my $constructor_options = { # [CONSTANT]
mode => 1, requireMath => 1, forbidMath => 1, font => 1,
alias => 1, reversion => 1, sizer => 1, properties => 1,
nargs => 1,
nargs => 1, toAttribute => 1,
beforeDigest => 1, afterDigest => 1, beforeConstruct => 1, afterConstruct => 1,
captureBody => 1, scope => 1, bounded => 1, locked => 1,
outer => 1, long => 1, robust => 1 };
Expand Down Expand Up @@ -1425,6 +1425,7 @@ sub DefConstructorI {
alias => (defined $options{alias} ? $options{alias}
: ($options{robust} ? $cs : undef)),
reversion => $options{reversion},
toAttribute => $options{toAttribute},
sizer => inferSizer($options{sizer}, $options{reversion}),
captureBody => $options{captureBody},
properties => $options{properties} || {},
Expand Down Expand Up @@ -1827,7 +1828,8 @@ my $environment_options = { # [CONSTANT]
beforeDigest => 1, afterDigest => 1,
afterDigestBegin => 1, beforeDigestEnd => 1, afterDigestBody => 1,
beforeConstruct => 1, afterConstruct => 1,
reversion => 1, sizer => 1, scope => 1, locked => 1 };
reversion => 1, sizer => 1, scope => 1, locked => 1,
toAttribute => 1 };

sub DefEnvironment {
my ($proto, $replacement, %options) = @_;
Expand Down Expand Up @@ -1873,8 +1875,9 @@ sub DefEnvironmentI {
nargs => $options{nargs},
captureBody => 1,
properties => $options{properties} || {},
(defined $options{reversion} ? (reversion => $options{reversion}) : ()),
(defined $sizer ? (sizer => $sizer) : ()),
(defined $options{reversion} ? (reversion => $options{reversion}) : ()),
(defined $options{toAttribute} ? (toAttribute => $options{toAttribute}) : ()),
(defined $sizer ? (sizer => $sizer) : ()),
), $options{scope});
$STATE->installDefinition(LaTeXML::Core::Definition::Constructor
->new(T_CS("\\end{$name}"), "", "",
Expand Down Expand Up @@ -1914,8 +1917,9 @@ sub DefEnvironmentI {
nargs => $options{nargs},
captureBody => T_CS("\\end$name"), # Required to capture!!
properties => $options{properties} || {},
(defined $options{reversion} ? (reversion => $options{reversion}) : ()),
(defined $sizer ? (sizer => $sizer) : ()),
(defined $options{reversion} ? (reversion => $options{reversion}) : ()),
(defined $options{toAttribute} ? (toAttribute => $options{toAttribute}) : ()),
(defined $sizer ? (sizer => $sizer) : ()),
), $options{scope});
$STATE->installDefinition(LaTeXML::Core::Definition::Constructor
->new(T_CS("\\end$name"), "", "",
Expand Down Expand Up @@ -3682,6 +3686,14 @@ the one defined in the C<prototype>. This is a convenient alternative for
reversion when a 'public' command conditionally expands into
an internal one, but the reversion should be for the public command.

=item C<toAttribute=E<gt>I<texstring> | I<code>($whatsit,#1,#2,...)>

specifies the conversion of the invocation back into plain text for an attribute value
(the default is C<toString>).
The I<textstring> string can include C<#1>, C<#2>...
The I<code> is called with the C<$whatsit> and digested arguments
and must return a string.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Possibly a doc string like this is also needed for the DefEnvironment addition.

Copy link
Owner Author

Choose a reason for hiding this comment

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

yep, done.


=item C<sizer=E<gt>I<string> | I<code>($whatsit)>

specifies how to compute (approximate) the displayed size of the object,
Expand Down
Loading