This file is indexed.

/usr/share/perl5/LaTeXML/Post/CrossRef.pm is in latexml 0.7.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# /=====================================================================\ #
# |  LaTeXML::Post::CrossRef                                            | #
# | Scan for ID's etc                                                   | #
# |=====================================================================| #
# | Part of LaTeXML:                                                    | #
# |  Public domain software, produced as part of work done by the       | #
# |  United States Government & not subject to copyright in the US.     | #
# |---------------------------------------------------------------------| #
# | Bruce Miller <bruce.miller@nist.gov>                        #_#     | #
# | http://dlmf.nist.gov/LaTeXML/                              (o o)    | #
# \=========================================================ooo==U==ooo=/ #

package LaTeXML::Post::CrossRef;
use strict;
use LaTeXML::Util::Pathname;
use LaTeXML::Common::XML;
use charnames qw(:full);
use base qw(LaTeXML::Post);

our $PILCROW = "\N{PILCROW SIGN}";
our $SECTION = "\N{SECTION SIGN}";
our %TYPEPREFIX = 
  ('ltx:equation'     =>'Eq.',
   'ltx:equationmix'  =>'Eq.',
   'ltx:equationgroup'=>'Eq.',
   'ltx:figure'       =>'Fig.',
   'ltx:table'        =>'Tab.',
   'ltx:listing'      =>'Listing ',
   'ltx:chapter'      =>'Ch.',
   'ltx:part'         =>'Pt.',
   'ltx:section'      =>$SECTION,
   'ltx:subsection'   =>$SECTION,
   'ltx:subsubsection'=>$SECTION,
   'ltx:paragraph'    =>$PILCROW,
   'ltx:subparagraph' =>$PILCROW,
   'ltx:para'         =>'p',
#   'ltx:appendix'     =>'App.',
 );

sub new {
  my($class,%options)=@_;
  my $self = $class->SUPER::new(%options);
  $$self{db}       = $options{db};
  $$self{urlstyle} = $options{urlstyle};
  $$self{toc_show} = ($options{number_sections} ? "typerefnum title" : "title");
  $$self{ref_show} = ($options{number_sections} ? "typerefnum" : "title");
  $self; }

sub process {
  my($self,$doc)=@_;
  $self->ProgressDetailed($doc,"Beginning cross-references");
  my $root = $doc->getDocumentElement;
  local %LaTeXML::Post::CrossRef::MISSING=();
  $self->fill_in_navigation($doc);
  $self->fill_in_frags($doc);
  $self->fill_in_refs($doc);
  $self->fill_in_bibrefs($doc);
  if(($$self{verbosity} >= 0) && (keys %LaTeXML::Post::CrossRef::MISSING)){
    my @msgs=();
    foreach my $type (sort keys %LaTeXML::Post::CrossRef::MISSING){
      push(@msgs,$type.": ".join(', ',sort keys %{$LaTeXML::Post::CrossRef::MISSING{$type}}));}
    $self->Warn($doc,"Missing keys:\n  ".join(";\n  ",@msgs)); }
  $self->ProgressDetailed($doc,"done cross-references");
  $doc; }

sub note_missing {
  my($self,$type,$key)=@_;
  $LaTeXML::Post::CrossRef::MISSING{$type}{$key}++; }

sub fill_in_navigation {
  my($self,$doc)=@_;
  if(my $id = $doc->getDocumentElement->getAttribute('xml:id')){
    if(my $entry = $$self{db}->lookup("ID:$id")){
      # Add navigation in any case?
      $doc->addNodes($doc->getDocumentElement, ['ltx:navigation',{}]);
      if(my $nav = $doc->findnode('//ltx:navigation')){
	my $startref =$doc->findnode('ltx:ref[@class="start"]',$nav);
	my $start_id = $startref && $startref->getAttribute('idref');
	my $h_id = $id;
	# Generate Downward TOC
	my $navtoc= $self->navtoc_aux($id, $entry->getValue('location')||'');
	# And Upward Context
	my $p_id;
	while(($p_id = $entry->getValue('parent')) && ($entry = $$self{db}->lookup("ID:$p_id"))){
	  $navtoc = ['ltx:toclist',{},
		     map(['ltx:tocentry',
			  {($_ eq $id ? (class=>'self'):())},
			  ['ltx:ref',{class=>'toc',idref=>$_,show=>'fulltitle'}],
			  (($_ eq $h_id) && $navtoc ? ($navtoc) : ())],
			 @{ $entry->getValue('children')||[] })];
	  $h_id = $p_id; }
	$navtoc = ['ltx:toclist',{},
		   ['ltx:tocentry',{},
		    ['ltx:ref',{class=>'toc',show=>'fulltitle',idref=>$h_id}],
		    $navtoc]] if $h_id && (!$start_id || ($start_id ne $h_id));
	$doc->addNodes($nav,$navtoc); }
    }}}

sub navtoc_aux {
  my($self,$id, $relative_to)=@_;
  if(my $entry = $$self{db}->lookup("ID:$id")){
    if(($entry->getValue('location')||'') eq $relative_to){
      my $kids = $entry->getValue('children');
      if($kids && @$kids){
	return (['ltx:toclist',{},map(['ltx:tocentry',{},
				       ['ltx:ref',{class=>'toc',show=>'fulltitle',idref=>$_}],
				       $self->navtoc_aux($_,$relative_to)],
				      @$kids)]); }}}
  (); }

sub fill_in_frags {
  my($self,$doc)=@_;
  my $db = $$self{db};
  # Any nodes with an ID will get a fragid;
  # This is the id/name that will be used within xhtml/html.
  foreach my $node ($doc->findnodes('//@xml:id')){
    if(my $entry = $db->lookup("ID:".$node->value)){
      if(my $fragid = $entry->getValue('fragid')){
	$node->parentNode->setAttribute(fragid=>$fragid); }}}}

# Fill in content text for any <... @idref..>'s or @labelref
sub fill_in_refs {
  my($self,$doc)=@_;
  my $db = $$self{db};
  $self->ProgressDetailed($doc,"Filling in refs");
  foreach my $ref ($doc->findnodes('descendant::*[@idref or @labelref]')){
    my $tag = $doc->getQName($ref);
    next if $tag eq 'ltx:XMRef'; # Blech; list those TO fill-in, or list those to exclude?
    my $id = $ref->getAttribute('idref');
    my $show = $ref->getAttribute('show');
    $show = $$self{ref_show} unless $show;
    $show = $$self{toc_show} if $show eq 'fulltitle';
    if(!$id){
      if(my $label = $ref->getAttribute('labelref')){
	my $entry;
	if(($entry = $db->lookup($label)) && ($id=$entry->getValue('id'))){
	  $show =~ s/^type//; 	# Since author may have put explicit \S\ref... in! 
	}
	else {
	  $self->note_missing('Label',$label);
	  if(!$ref->textContent){
	    $doc->addNodes($ref,$label);  # Just to reassure (?) readers.
	    $ref->setAttribute(broken=>1); }
	}}}
    if($id){
      if(!$ref->getAttribute('href')){
	if(my $url = $self->generateURL($doc,$id)){
	  $ref->setAttribute(href=>$url); }}
      if(!$ref->getAttribute('title')){
	if(my $titlestring = $self->generateTitle($id)){
	  $ref->setAttribute(title=>$titlestring); }}
      if(!$ref->textContent && !(($tag eq 'ltx:graphics') || ($tag eq 'ltx:picture'))){
	$doc->addNodes($ref,$self->generateRef($doc,$id,$show)); }
      if(my $entry = $$self{db}->lookup("ID:$id")){
	$ref->setAttribute(stub=>1) if $entry->getValue('stub'); }
    }}}


# Needs to evolve into the combined stuff that we had in DLMF.
# (eg. concise author/year combinations for multiple bibrefs)
sub fill_in_bibrefs {
  my($self,$doc)=@_;
  $self->ProgressDetailed($doc,"Filling in bibrefs");
  my $db = $$self{db};
  foreach my $bibref ($doc->findnodes('descendant::ltx:bibref')){
    my $show   = $bibref->getAttribute('show');
    # Messy, since we're REPLACING the bibref with it's expansion.
    my @save = ();
    my ($p,$s) = ($bibref->parentNode, undef);
    while(($s = $p->lastChild) && ($$s != $$bibref)){ # Remove & Save following siblings.
      unshift(@save,$p->removeChild($s)); }
    $doc->removeNodes($bibref);
#    $doc->addNodes($p,$self->make_bibcite($doc,$show,split(/,/,$bibref->getAttribute('bibrefs'))));
    $doc->addNodes($p,$self->make_bibcite($doc,$bibref));
    map($p->appendChild($_),@save); # Put these back.
 }}

# Given a list of bibkeys, construct links to them.
# Mostly tuned to author-year style.
# Combines when multiple bibitems share the same authors.
sub make_bibcite {
  my($self,$doc,$bibref)=@_;

  my @keys = split(/,/,$bibref->getAttribute('bibrefs'));
  my $show = $bibref->getAttribute('show');
  if(!$show){
    $self->Warn($doc,"No show in bibref ".join(', ',@keys)); 
    $show = 'refnum'; }

  my $sep  = $bibref->getAttribute('separator') || ',';
  my $yysep= $bibref->getAttribute('yyseparator') || ',';
  my @phrases = $bibref->getChildNodes();	  # get the ltx;note's in the bibref!
  # Collect all the data from the bibliography
  my @data = ();
  foreach my $key (@keys){
    if(my $bentry = $$self{db}->lookup("BIBLABEL:$key")){
      if(my $id = $bentry->getValue('id')){
	if(my $entry = $$self{db}->lookup("ID:$id")){
	  my $authors  = $entry->getValue('authors');
	  my $fauthors = $entry->getValue('fullauthors');
	  my $keytag   = $entry->getValue('keytag');
	  my $year     = $entry->getValue('year');
	  my $typetag  = $entry->getValue('typetag');
	  my $number   = $entry->getValue('number');
	  my $title    = $entry->getValue('title');
	  my $refnum   = $entry->getValue('refnum'); # This come's from the \bibitem, w/o BibTeX
	  my($rawyear,$suffix);
	  if($year && ($year->textContent) =~ /^(\d\d\d\d)(\w)$/){
	    ($rawyear,$suffix)=($1,$2); }
	  $show = 'refnum' unless $authors || $fauthors || $keytag; # Disable author-year format!
	  # fullnames ?
	  push(@data,{authors     =>[$doc->trimChildNodes($authors || $fauthors || $keytag)],
		      fullauthors =>[$doc->trimChildNodes($fauthors || $authors || $keytag)],
		      authortext  =>($authors||$fauthors ? ($authors||$fauthors)->textContent :''),
		      year        =>[$doc->trimChildNodes($year || $typetag)],
		      rawyear     =>$rawyear,
		      suffix      =>$suffix,
		      number      =>[$doc->trimChildNodes($number)],
		      refnum      =>[$doc->trimChildNodes($refnum)],
		      title       =>[$doc->trimChildNodes($title || $keytag)],
		      attr=>{idref=>$id,
			     href=>$self->generateURL($doc,$id),
			     ($title ? (title=>$title->textContent):())}}); }}}
    else {
      $self->note_missing('Citation',$key); }}
  my $checkdups = ($show =~ /author/i) && ($show =~ /(year|number)/i);
  my @refs=();
  my $saveshow = $show;
  while(@data){
    my $datum = shift(@data);
    my $didref = 0;
    my @stuff=();
    $show=$saveshow;
    while($show){
      if($show =~ s/^authors?//i){
	push(@stuff,@{$$datum{authors}}); }
      elsif($show =~ s/^fullauthors?//i){
	push(@stuff,@{$$datum{fullauthors}}); }
      elsif($show =~ s/^title//i){
	push(@stuff,@{$$datum{title}}); }
      elsif($show =~ s/^refnum//i){
	push(@stuff,@{$$datum{refnum}}); }
      elsif($show =~ s/^phrase(\d)//i){
	push(@stuff,$phrases[$1-1]->childNodes) if $phrases[$1-1]; }
      elsif($show =~ s/^year//i){
	push(@stuff,['ltx:ref',$$datum{attr},@{$$datum{year}}]);
	$didref=1; 
	while($checkdups && @data && ($$datum{authortext} eq $data[0]{authortext})){
	  my $next = shift(@data);
	  push(@stuff, $yysep,' ');
	  if((($$datum{rawyear}||'no_year_1') eq ($$next{rawyear}||'no_year_2')) && $$next{suffix}){
	    push(@stuff,['ltx:ref',$$next{attr},$$next{suffix}]);  }
	  else {
	    push(@stuff,['ltx:ref',$$next{attr},@{$$next{year}}]);  }}}
      elsif($show =~ s/^number//i){
	push(@stuff,['ltx:ref',$$datum{attr},@{$$datum{number}}]);
	$didref=1;
	while($checkdups && @data && ($$datum{authortext} eq $data[0]{authortext})){
	  my $next = shift(@data);
	  push(@stuff,$yysep,' ',['ltx:ref',$$next{attr},@{$$next{number}}]);  }}
      elsif($show =~ s/^(.)//){
	push(@stuff, $1); }}
    push(@refs,
	 (@refs ? ($sep,' ') : ()),
	 ($didref ? @stuff : (['ltx:ref',$$datum{attr},@stuff]))); }
  @refs; }

sub generateURL {
  my($self,$doc,$id)=@_;
  my($object,$location);
  if(($object = $$self{db}->lookup("ID:".$id))
     && ($location = $object->getValue('location'))){
    my $doclocation = $self->siteRelativePathname($doc->getDestination);
    my $url = pathname_relative('/'.$location,  '/'.pathname_directory($doclocation));
    my $format = $$self{format} || 'xml';
    my $urlstyle = $$self{urlstyle}||'file';
    if($urlstyle eq 'server'){
      $url =~ s/(^|\/)index.\Q$format\E$/$1/; } # Remove trailing index.$format
    elsif($urlstyle eq 'negotiated'){
      $url =~ s/\.\Q$format\E$//; # Remove trailing $format
      $url =~ s/(^|\/)index$/$1/; # AND trailing index
    }
    $url = '.' unless $url;
#    $url .= '/' if ($url ne '.') && ($url =~ /\/$/);
    if(my $fragid = $object->getValue('fragid')){
      $url = '' if ($url eq '.') or ($location eq $doclocation);
      $url .= '#'.$fragid; }
    elsif($location eq $doclocation){
      $url = ''; }
    $url; }
  else {
    $self->note_missing('ID',$id); }}

# Generate the contents of a <ltx:ref> of the given id.
# show is a string containing substrings 'type', 'refnum' and 'title'
# (standing for the type prefix, refnum and title of the id'd object)
# and any other random characters; the
our $NBSP = pack('U',0xA0);
sub generateRef {
  my($self,$doc,$id,$show)=@_;
  my $fallback_show = ($show !~ /title/ ? "title" : "refnum");
  my @fallback=();
  # Find entry associated with $id, or first ancestor, that can fill in the show pattern.
  while(my $entry = $id && $$self{db}->lookup("ID:$id")){
    my @stuff = $self->generateRef_aux($doc,$entry,$show);
    return @stuff if @stuff;
    @fallback = $self->generateRef_aux($doc,$entry,$fallback_show) unless @fallback;
    $id = $entry->getValue('parent'); }
  (@fallback ? @fallback : ("?")); }

# Interpret a "Show" pattern for a given DB entry.
# The pattern can contain substrings to be substituted
#   type   => the type prefix (eg Ch. or similar)
#   refnum => the reference number
#   title  => the title.
# and any other random characters which are preserved.
sub generateRef_aux {
  my($self,$doc,$entry,$show)=@_;
  my $OK=0;
  my @stuff=();
  while($show){
    if($show =~ s/^typerefnum(\.?\s*)//){
      my $r = $1;
      $r =~ s/\s+/$NBSP/ if $r;
      my @rest = ($1 ? ($1):());
      if(my $refnum = $entry->getValue('refnum')){
	my $type   = $entry->getValue('type');
	$OK = 1;
	push(@stuff, ['ltx:span',{class=>'refnum'},
		      ($type && $TYPEPREFIX{$type} ? ($TYPEPREFIX{$type}) :()),
		      $doc->trimChildNodes($refnum),
		      ($r ? ($r):())]); }}
    elsif($show =~ s/^type(\.?\s*)//){
      my $r = $1;
      $r =~ s/\s+/$NBSP/ if $r;
      my $type   = $entry->getValue('type');
      if($type && $TYPEPREFIX{$type}){
	$OK = 1;
	push(@stuff, $TYPEPREFIX{$type},($r ? ($r):())); }}
    elsif($show =~ s/^refnum(\.?\s*)//){
      my $r = $1;
      $r =~ s/\s+/$NBSP/ if $r;
      if(my $refnum = $entry->getValue('refnum')){
	$OK = 1;
	push(@stuff, ['ltx:span',{class=>'refnum'},
		      $doc->trimChildNodes($refnum),
		      ($r ? ($r):())]); }}
    elsif($show =~ s/^title//){
      if(my $title = $entry->getValue('title')){
	$OK = 1;
	push(@stuff, ['ltx:span',{class=>'title'},$doc->trimChildNodes($title)]); }}
    elsif($show =~ s/^(.)//){
      push(@stuff, $1); }}
  ($OK ? @stuff : ()); }

sub generateTitle {
  my($self,$id)=@_;
  # Add author, if any ???
  my $string = "";
  while(my $entry = $id && $$self{db}->lookup("ID:$id")){
    my $title  = $entry->getValue('title');
    $title = $title->textContent if $title && ref $title;
    $title =~ s/^\s+// if $title;
    $title =~ s/\s+$// if $title;
    my $refnum = ($$self{number_sections} ? $entry->getValue('refnum') : '');
    $refnum = $refnum->textContent if $refnum && ref $refnum;
    $refnum =~ s/^\s+// if $refnum;
    $refnum =~ s/\s+$// if $refnum;
    if($title || $refnum){
      $string .= ' in ' if $string;
      my $type   = $entry->getValue('type');
      $string .= $TYPEPREFIX{$type} if $TYPEPREFIX{$type};
      $string .= $refnum if $refnum;
      $string .= ($refnum ? '. ':'').$title if $title; }
    $id = $entry->getValue('parent'); }
  $string; }

# ================================================================================
1;