Skip to content

Commit 8784919

Browse files
committed
Various bits of code cleanup; Thanks dginev
1 parent e8ca26d commit 8784919

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

lib/LaTeXML/Engine/Base_Utility.pool.ltxml

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ sub andSplit {
136136
return map { ($cs, T_BEGIN, @$_, T_END) } SplitTokens($tokens, T_CS('\and')); }
137137

138138
DefMacro('\lx@splitting{}{}{}', sub {
139-
my ($gullet, $op, $delimiters, $tokens) = @_;
140-
my @delims = ($delimiters ? $delimiters->unlist : (T_CS('\and')));
141-
return map { ($op, T_BEGIN, @$_, T_END) } SplitTokens($tokens, @delims); });
139+
my ($gullet, $op, $delimiters, $tokens) = @_;
140+
return map { ($op, T_BEGIN, @$_, T_END) }
141+
($delimiters ? SplitTokens($tokens, $delimiters->unlist) : ($tokens)); });
142142

143143
sub orNull {
144144
return (grep { defined } @_) ? @_ : undef; }
@@ -183,11 +183,11 @@ our %ReplaceableFrontmatterTags = (
183183
# Digestion is deferred until \maketitle, or something similar,
184184
# to avoid extra side-effects, particularly when entries
185185
# (eg. \title, \author) get redefined & replaced.
186-
sub saveFrontMatter {
186+
sub queueFrontMatter {
187187
my ($stomach, $tag, $attr, $command) = @_;
188188
$tag = ToString($tag);
189189
AssignValue('frontmatter_raw', [], 'global') unless LookupValue('frontmatter_raw');
190-
unsaveFrontMatter($tag) if $ReplaceableFrontmatterTags{$tag};
190+
dequeueFrontMatter($tag) if $ReplaceableFrontmatterTags{$tag};
191191

192192
# Convert KeyVals to a hash, but be concerned about multiple values!?!?
193193
if ($attr && (ref $attr eq 'LaTeXML::Core::KeyVals')) {
@@ -200,7 +200,7 @@ sub saveFrontMatter {
200200
return; }
201201

202202
# This removes previously stored (but deferred) frontmatter that is being overridden.
203-
sub unsaveFrontMatter {
203+
sub dequeueFrontMatter {
204204
my ($tag, %attr) = @_;
205205
if (my $frontmatter = LookupValue('frontmatter_raw')) {
206206
my @keep = ();
@@ -218,13 +218,13 @@ DefPrimitive('\lx@clear@frontmatter {} OptionalKeyVals:Frontmatter', sub {
218218
my ($stomach, $tag, $kv) = @_;
219219
my %attr = ($kv ? $kv->getHash : ());
220220
map { $attr{$_} = ToString($kv->getValue($_)); } keys %attr;
221-
unsaveFrontMatter(ToString($tag), %attr);
221+
dequeueFrontMatter(ToString($tag), %attr);
222222
return; });
223223

224224
# Remove all creators with given role (default author)
225225
DefPrimitive('\lx@clear@creators OptionalKeyVals:Frontmatter', sub {
226226
my ($stomach, $kv) = @_;
227-
unsaveFrontMatter('ltx:creator', role => ($kv && ToString($kv->getValue('role'))) || 'author');
227+
dequeueFrontMatter('ltx:creator', role => ($kv && ToString($kv->getValue('role'))) || 'author');
228228
return; });
229229
#DefMacro('\lx@clear@creators OptionalKeyVals:Frontmatter', '\lx@clear@frontmatter{ltx:creator)[#1]');
230230
DefMacro('\lx@clear@creators []', '\lx@clear@frontmatter{ltx:creator}[#1]');
@@ -259,18 +259,16 @@ Tag('ltx:contact', afterClose => \&cleanTrailingBreak);
259259
# ifnew (only add if no previous entry)
260260
DefPrimitive('\@add@frontmatter OptionalKeyVals {} OptionalKeyVals {}', sub {
261261
my ($stomach, $keys, $tag, $attr, $tokens) = @_;
262-
saveFrontMatter($stomach, $tag, $attr,
262+
queueFrontMatter($stomach, $tag, $attr,
263263
Invocation(T_CS('\lx@add@frontmatter@now'), $keys, $tag, $attr, $tokens));
264264
return; });
265265

266266
sub cleanFrontmatterMarks {
267267
my ($marks, $prefix) = @_;
268268
$marks = ToString($marks);
269269
$prefix = ToString($prefix) || '';
270-
### return ($marks ? map { CleanLabel($_, $prefix); } split(',', $marks) : ()); }
271270
my @marks = ();
272271
foreach my $mark (split(',', $marks)) {
273-
$mark = ToString($mark);
274272
$mark =~ s/^\s+//; $mark =~ s/\s+$//;
275273
if ($mark =~ /^\\ref\{\s*([^\}]*)\s*\}$/) {
276274
$mark = 'LABEL:'. $1; }
@@ -285,14 +283,18 @@ DefPrimitive('\lx@add@frontmatter@now OptionalKeyVals {} OptionalKeyVals:Frontma
285283
$tag = ToString($tag);
286284
$kv = $kv->beDigested($stomach) if $kv;
287285
my $frontmatter = LookupValue('frontmatter');
288-
my $role = ($kv && ToString($kv->getValue('role'))) || '';
289-
my $n = LookupMapping('num_' . $tag, $role); $n++;
290-
AssignMapping('num_' . $tag, $role => $n);
286+
my $role = ($kv && ToString($kv->getValue('role')));
287+
my $n = 0;
291288
# extract (possibly multiple!) marks
292289
my @marks = cleanFrontmatterMarks(($kv && $kv->getValues('mark')) || '', '');
293-
push(@marks, CleanLabel($n, $role)); # record sequence position as potential contact attachment mark
290+
if ($role) {
291+
$n = LookupMapping('num_' . $tag, $role); $n++;
292+
AssignMapping('num_' . $tag, $role => $n);
293+
# record sequence position as potential contact attachment mark
294+
push(@marks, CleanLabel($n, $role)); }
294295
my %attr = (($kv ? $kv->getHash : ()),
295-
role => $role, _num => $n, _marks => join(',', @marks)); # replace these...
296+
($role ? (role => $role, _num => $n) : ()),
297+
_marks => join(',', @marks)); # replace these...
296298
my $entry = [$tag, {%attr}, 'place_keeper']; # (in case embedded)
297299
push(@{ $$frontmatter{$tag} }, $entry);
298300
Debug("ADD $tag: [" . join(',', map { $_ . '=' . ToString($attr{$_}); } sort keys %attr) . "] "
@@ -319,23 +321,24 @@ DefPrimitive('\lx@add@frontmatter@now OptionalKeyVals {} OptionalKeyVals:Frontma
319321
# <default> : attach to preceding creator.
320322
DefPrimitive('\lx@add@contact OptionalKeyVals:Frontmatter {}', sub {
321323
my ($stomach, $kv, $content) = @_;
322-
saveFrontMatter($stomach, 'ltx:contact', $kv, Invocation(T_CS('\lx@add@contact@now'), $kv, $content));
324+
queueFrontMatter($stomach, 'ltx:contact', $kv, Invocation(T_CS('\lx@add@contact@now'), $kv, $content));
323325
return; });
324326

325327
DefPrimitive('\lx@add@contact@now OptionalKeyVals:Frontmatter {}', sub {
326328
##DefPrimitive('\lx@add@contact OptionalKeyVals:Frontmatter {}', sub {
327329
my ($stomach, $kv, $content) = @_;
328330
my $frontmatter = LookupValue('frontmatter');
329331
my %options = ($kv ? $kv->beDigested($stomach)->getHash : ());
330-
my $role = ToString($options{role}) || '';
331-
my $n = LookupMapping('num_contacts', $role); $n++;
332-
my $marks = join(',',
333-
cleanFrontmatterMarks($options{mark} || '', ''),
334-
($options{markseq} ? CleanLabel($n, ToString($options{markseq})) : ()));
335-
AssignMapping('num_contacts', $role => $n);
336-
if (!$options{name} || ! scalar($options{name}->unlist)) {
337-
my $cs = T_CS('\lx@contact@' . $role . '@name');
338-
$options{name} = DigestText($cs) if LookupDefinition($cs); }
332+
my $role = ToString($options{role});
333+
my @marks = cleanFrontmatterMarks($options{mark} || '', '');
334+
if ($role) {
335+
my $n = LookupMapping('num_contacts', $role); $n++;
336+
AssignMapping('num_contacts', $role => $n);
337+
push(@marks, CleanLabel($n, ToString($options{markseq}))) if $options{markseq};
338+
if (!$options{name} || ! scalar($options{name}->unlist)) {
339+
my $cs = T_CS('\lx@contact@' . $role . '@name');
340+
$options{name} = DigestText($cs) if LookupDefinition($cs); } }
341+
my $marks = join(',', @marks);
339342
my @allcreators = @{ $$frontmatter{'ltx:creator'} || [] };
340343
my @creators = grep { ($$_[1]{role} || '') ne 'pending'; } @allcreators;
341344
my $ncreators = scalar(@creators);
@@ -372,8 +375,10 @@ DefPrimitive('\lx@add@contact@now OptionalKeyVals:Frontmatter {}', sub {
372375
Debug("...adding to $nprev previous" . ($newonly ? ' new' : ''));
373376
while (my $creator = pop(@creators)) {
374377
push(@$creator, $datum);
375-
$$creator[1]{"_has$role"} = 1;
376-
last if (--$nprev <= 0) || ($newonly && $creators[-1][1]{"_has$role"}); } }
378+
if ($role) {
379+
$$creator[1]{"_has$role"} = 1;
380+
last if $newonly && @creators && $creators[-1][1]{"_has$role"}; }
381+
last if (--$nprev <= 0); } }
377382
return; });
378383

379384
DefConstructor('\lx@make@contact@@ Undigested Undigested {} Undigested',
@@ -429,7 +434,7 @@ DefPrimitive('\lx@set@contact@label Semiverbatim', sub {
429434
# \@add@to@frontmatter{tag}[label]{content}
430435
DefPrimitive('\@add@to@frontmatter {} [] {}', sub {
431436
my ($stomach, $tag, $label, $tokens) = @_;
432-
saveFrontMatter($stomach, $tag, undef, Invocation(T_CS('\@add@to@frontmatter@now'), $tag, $label, $tokens));
437+
queueFrontMatter($stomach, $tag, undef, Invocation(T_CS('\@add@to@frontmatter@now'), $tag, $label, $tokens));
433438
return; });
434439
DefPrimitive('\@add@to@frontmatter@now {} [] {}', sub {
435440
##DefPrimitive('\@add@to@frontmatter {} [] {}', sub {
@@ -476,7 +481,7 @@ DefMacro('\lx@add@authors{}',
476481
# Note: must keep affiliations (after \\) embedded, to simplify clear@author removal
477482
. '\lx@splitting{\lx@author@and@affil}{\and,\And}{#1}');
478483
DefMacro('\lx@author@and@affil{}', '\lx@author@and@affil@#1\\\\\done');
479-
DefMacro('\lx@author@and@affil@ Until:\\\\ Until:\\done',
484+
DefMacro('\lx@author@and@affil@ Until:\\\\ Until:\done',
480485
'\ifx.#2.\lx@add@creator[role=author]{#1}'
481486
. '\else\lx@add@creator[role=author]{#1\lx@add@contact[role=affiliation]{#2}}\fi');
482487

@@ -512,15 +517,13 @@ sub digestFrontMatter {
512517
AssignValue('frontmatter_raw', undef, 'global'); }
513518
my $frontmatter = LookupValue('frontmatter');
514519
my $digested = {};
515-
# Add punctuation to all ltx:creators, now that we know how many of each.
520+
# Add punctuation to all ltx:creators, now that we know how many of each role.
516521
if (my $list = $$frontmatter{'ltx:creator'}) {
517522
foreach my $item (@$list) {
518523
my ($tag, $attr, @stuff) = @$item;
519-
my $role = $$attr{role};
520-
my $i = $$attr{_num};
521-
# my $n = LookupMapping(num_creators => $role);
522-
my $n = LookupMapping('num_ltx:creator' => $role);
523-
if ((defined $i) && ($i > 1)) {
524+
if($$attr{role} && (defined $$attr{_num}) && ($$attr{_num} > 1)){
525+
my $i = $$attr{_num};
526+
my $n = LookupMapping('num_ltx:creator' => $$attr{role});
524527
$$attr{before} = DigestText(($i >= $n ? T_CS('\lx@author@conj') : T_CS('\lx@author@sep'))); } } }
525528
$stomach->egroup;
526529
return; }
@@ -607,23 +610,17 @@ Tag('ltx:document', 'afterOpen:late' => sub {
607610
insertFrontMatter($_[0]) unless LookupValue('frontmatter_deferred'); });
608611
# Request Frontmatter to appear HERE (if not already done),
609612
# deferring it from document begin.
610-
# NOTE: I *think* this is where ALL the digestion of frontmatter should be happening
611-
# NOTE: What exactly are in @boxes? Seems that it should mostly be empty (side effects only)?
613+
# This should be where ALL the digestion of frontmatter happens
612614
DefConstructor('\lx@frontmatterhere', sub { insertFrontMatter($_[0]); },
613615
afterDigest => sub {
614616
my ($stomach) = @_;
615-
my @boxes = ();
616617
my $frontmatter = LookupValue('frontmatter');
617618
digestFrontMatter();
618619
AssignValue(frontmatter_deferred => 1, 'global');
619-
return @boxes; });
620+
return; });
620621
DefPrimitiveI('\lx@frontmatter@fallback', undef, sub {
621622
my ($stomach) = @_;
622-
my @boxes = ();
623-
# if (my $frontmatter_tks = LookupValue('@at@begin@maketitle')) {
624-
# push(@boxes, $stomach->digest(Tokens(@$frontmatter_tks)));
625-
# AssignValue('@at@begin@maketitle', undef, 'global'); }
626-
return @boxes; });
623+
return; }); # ?
627624

628625
# Maintain a list of classes that apply to the document root.
629626
# This might involve global style options, like leqno.

0 commit comments

Comments
 (0)