| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Post::MakeIndex | # | ||
| 3 | # | Make an index from scanned indexmark's | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Post::MakeIndex; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Util::Pathname; | ||
| 16 | use LaTeXML::Common::XML; | ||
| 17 | use charnames qw(:full); | ||
| 18 | use Unicode::Normalize; | ||
| 19 | use LaTeXML::Post; | ||
| 20 | use Text::Unidecode; | ||
| 21 | use base qw(LaTeXML::Post::Collector); | ||
| 22 | |||
| 23 | # Options: | ||
| 24 | # permuted : Generates a permuted index | ||
| 25 | # The phrases (separated by ! in LaTeX) within each \index entry | ||
| 26 | # are permuted before adding to the index tree. | ||
| 27 | # split : whether the split into separate pages by initial. | ||
| 28 | sub new { | ||
| 29 | my ($class, %options) = @_; | ||
| 30 | my $self = $class->SUPER::new(%options); | ||
| 31 | $$self{permuted} = $options{permuted}; | ||
| 32 | $$self{split} = $options{split}; | ||
| 33 | return $self; } | ||
| 34 | |||
| 35 | sub toProcess { | ||
| 36 | my ($self, $doc) = @_; | ||
| 37 | return $doc->findnodes('//ltx:index[not(ltx:indexlist)] | //ltx:glossary[not(ltx:glossarylist)]'); } | ||
| 38 | |||
| 39 | sub process { | ||
| 40 | my ($self, $doc, @lists) = @_; | ||
| 41 | my @newdocs = (); | ||
| 42 | $doc->addDate(); | ||
| 43 | foreach my $list (@lists) { | ||
| 44 | my $tag = $doc->getQName($list); | ||
| 45 | if ($tag eq 'ltx:index') { | ||
| 46 | my ($tree, $allphrases) = $self->build_tree($doc, $list); | ||
| 47 | if ($tree) { | ||
| 48 | if ($$self{split}) { | ||
| 49 | push(@newdocs, $self->makeSubCollectionDocuments($doc, $list, | ||
| 50 | map { ($_ => $self->makeIndexList($doc, $allphrases, $$tree{subtrees}{$_})) } | ||
| 51 | keys %{ $$tree{subtrees} })); } | ||
| 52 | else { | ||
| 53 | $doc->addNodes($list, $self->makeIndexList($doc, $allphrases, $tree)); } } } | ||
| 54 | elsif ($tag eq 'ltx:glossary') { | ||
| 55 | if (my @entries = $self->getGlossaryEntries($doc, $list)) { | ||
| 56 | if ($$self{split}) { # Separate by initial. | ||
| 57 | my $split = {}; | ||
| 58 | foreach my $entry (@entries) { | ||
| 59 | push(@{ $$split{ $$entry{initial} } }, $entry); } | ||
| 60 | push(@newdocs, $self->makeSubCollectionDocuments($doc, $list, | ||
| 61 | map { ($_ => $self->makeGlossaryList($doc, @{ $$split{$_} })) } | ||
| 62 | sort keys %$split)); } | ||
| 63 | else { | ||
| 64 | $doc->addNodes($list, $self->makeGlossaryList($doc, @entries)); } } } } | ||
| 65 | my @docs = map { $self->rescan($_); } (@newdocs ? @newdocs : ($doc)); | ||
| 66 | return @docs; } | ||
| 67 | |||
| 68 | # ================================================================================ | ||
| 69 | # Index | ||
| 70 | # Data generated: | ||
| 71 | # $tree : tree representation of the index. | ||
| 72 | # $allphrases : used for inferring the connection from see-also phrases to normal index entries. | ||
| 73 | # ================================================================================ | ||
| 74 | # Extracting a tree of index entries from the database | ||
| 75 | sub build_tree { | ||
| 76 | my ($self, $doc, $index) = @_; | ||
| 77 | my $defaultlistname = 'idx'; | ||
| 78 | my $listname = 'idx'; # Eventually customizable for different indices? | ||
| 79 | if (my @keys = grep { /^INDEX:/ } $$self{db}->getKeys) { | ||
| 80 | NoteLog("MakeIndex: " . scalar(@keys) . " entries"); | ||
| 81 | |||
| 82 | my $id = $index->getAttribute('xml:id'); | ||
| 83 | my $allphrases = {}; # Keep a hash of all phrase textContent=>id encountered (for seealso) | ||
| 84 | my $tree = { subtrees => {}, referrers => {}, id => $id, parent => undef }; | ||
| 85 | foreach my $key (@keys) { | ||
| 86 | my $entry = $$self{db}->lookup($key); | ||
| 87 | my $phrases = $entry->getValue('phrases'); | ||
| 88 | my @phrases = @$phrases; | ||
| 89 | if (($entry->getValue('inlist') || $defaultlistname) ne $listname) { | ||
| 90 | next; } # Skip any not in this list | ||
| 91 | if (!scalar(@phrases)) { | ||
| 92 | Warn('expected', $key, undef, "Missing phrases in indexmark: '$key'"); | ||
| 93 | next; } | ||
| 94 | |||
| 95 | if ($$self{permuted}) { | ||
| 96 | map { $self->add_entry($doc, $allphrases, $tree, $entry, @{$_}) } | ||
| 97 | cyclic_permute(@phrases); } | ||
| 98 | else { | ||
| 99 | $self->add_entry($doc, $allphrases, $tree, $entry, @phrases); } } | ||
| 100 | return ($tree, $allphrases); } | ||
| 101 | else { | ||
| 102 | return (undef, undef); } } | ||
| 103 | |||
| 104 | # NOTE: We're building ID's for each entry, of the form idx.key.key... | ||
| 105 | # I'd like to insert the initial in the case of split index: idx.A.key.key... | ||
| 106 | # But this makes it impossible to predict the id of a phrase key, w/o knowing | ||
| 107 | # whether the index has been split! | ||
| 108 | # OTOH, leaving it out risks that a single letter entry, say "A", will have the | ||
| 109 | # same id as the A page! (or maybe not if the key is downcased....) | ||
| 110 | sub add_entry { | ||
| 111 | my ($self, $doc, $allphrases, $tree, $entry, @phrases) = @_; | ||
| 112 | # NOTE: Still need option for splitting! | ||
| 113 | # We'll just prefix a level for the initial... | ||
| 114 | if ($$self{split}) { | ||
| 115 | my $init = $doc->initial($phrases[0]->getAttribute('key')); | ||
| 116 | my $subtree = $$tree{subtrees}{$init}; | ||
| 117 | if (!$subtree) { | ||
| 118 | $subtree = $$tree{subtrees}{$init} | ||
| 119 | = { phrase => $init, subtrees => {}, referrers => {}, id => $$tree{id}, parent => $tree }; } | ||
| 120 | add_rec($doc, $allphrases, $subtree, $entry, @phrases); } | ||
| 121 | else { | ||
| 122 | add_rec($doc, $allphrases, $tree, $entry, @phrases); } | ||
| 123 | return; } | ||
| 124 | |||
| 125 | sub add_rec { | ||
| 126 | my ($doc, $allphrases, $tree, $entry, @phrases) = @_; | ||
| 127 | if (@phrases) { | ||
| 128 | my $phrase = shift(@phrases); | ||
| 129 | my $key = $phrase->getAttribute('key'); | ||
| 130 | my $keyid = getIndexKeyID($key); | ||
| 131 | my $subtree = $$tree{subtrees}{$key}; | ||
| 132 | if (!$subtree) { | ||
| 133 | my $id = $$tree{id} . '.' . $keyid; | ||
| 134 | my $fullkey = ($$tree{key} ? "$$tree{key}." : '') . $key; | ||
| 135 | # phrasetext is for see & seealso lookup | ||
| 136 | my $phrasetext = getIndexContentKey($phrase); | ||
| 137 | # Store multi-level phrase as well, using space to separate levels | ||
| 138 | my $fullphrasetext = ($$tree{fullphrasetext} ? $$tree{fullphrasetext} . ' ' : '') | ||
| 139 | . $phrasetext; | ||
| 140 | $$allphrases{$fullkey} = $id; | ||
| 141 | $$allphrases{ lc($fullkey) } = $id; | ||
| 142 | $$allphrases{$fullphrasetext} = $id; | ||
| 143 | $$allphrases{ lc($fullphrasetext) } = $id; | ||
| 144 | |||
| 145 | my $phrasecopy = $doc->cloneNode($phrase); | ||
| 146 | $subtree = $$tree{subtrees}{$key} = { key => $fullkey, id => $id, | ||
| 147 | phrase => $phrasecopy, | ||
| 148 | phrasetext => $phrasetext, | ||
| 149 | fullphrasetext => $fullphrasetext, | ||
| 150 | subtrees => {}, referrers => {}, parent => $tree }; | ||
| 151 | } | ||
| 152 | add_rec($doc, $allphrases, $subtree, $entry, @phrases); } | ||
| 153 | else { | ||
| 154 | if (my $seealso = $entry->getValue('see_also')) { | ||
| 155 | $$tree{see_also} = $seealso; } | ||
| 156 | if (my $refs = $entry->getValue('referrers')) { | ||
| 157 | map { $$tree{referrers}{$_} = $$refs{$_} } keys %$refs; } } | ||
| 158 | return; } | ||
| 159 | |||
| 160 | # Extract the text from the (xml) phrase node, with some normalization. | ||
| 161 | sub getIndexContentKey { | ||
| 162 | my ($node) = @_; | ||
| 163 | my $string = (ref $node ? $node->textContent : $node); | ||
| 164 | $string =~ s/^\s+//s; | ||
| 165 | $string =~ s/\s+$//s; | ||
| 166 | $string =~ s/\s+/ /gs; | ||
| 167 | $string =~ s/\s*[\.\,\;]+$//s; # Remove trailing punctuation | ||
| 168 | return $string; } | ||
| 169 | |||
| 170 | # This generates the ID from the node | ||
| 171 | # By stripping non-letters, this strips out TOO MUCH; esp. greek etc, in math disappears | ||
| 172 | # Should we keep unicode? (any compatibility issues there?0 | ||
| 173 | # Should we just strip to rfc spec? | ||
| 174 | # Should we get the TeX for math? | ||
| 175 | my %GREEK_ASCII_MAP = ( | ||
| 176 | "\x{03B1}" => 'alpha', "\x{03B2}" => 'beta', "\x{03B3}" => 'gamma', "\x{03B4}" => 'delta', | ||
| 177 | "\x{03F5}" => 'epsilon', "\x{03B5}" => 'varepsilon', "\x{03B6}" => 'zeta', "\x{03B7}" => 'eta', | ||
| 178 | "\x{03B8}" => 'theta', "\x{03D1}" => 'vartheta', "\x{03B9}" => 'iota', "\x{03BA}" => 'kappa', | ||
| 179 | "\x{03BB}" => 'lambda', "\x{03BC}" => 'mu', "\x{03BD}" => 'nu', "\x{03BE}" => 'xi', | ||
| 180 | "\x{03C0}" => 'pi', "\x{03D6}" => 'varpi', "\x{03C1}" => 'rho', "\x{03F1}" => 'varrho', | ||
| 181 | "\x{03C3}" => 'sigma', "\x{03C2}" => 'varsigma', "\x{03C4}" => 'tau', "\x{03C5}" => 'upsilon', | ||
| 182 | "\x{03D5}" => 'phi', "\x{03C6}" => 'varphi', "\x{03C7}" => 'chi', "\x{03C8}" => 'psi', | ||
| 183 | "\x{03C9}" => 'omega', "\x{0393}" => 'Gamma', "\x{0394}" => 'Delta', "\x{0398}" => 'Theta', | ||
| 184 | "\x{039B}" => 'Lambda', "\x{039E}" => 'Xi', "\x{03A0}" => 'Pi', "\x{03A3}" => 'Sigma', | ||
| 185 | "\x{03A5}" => 'Upsilon', "\x{03A6}" => 'Phi', "\x{03A8}" => 'Psi', "\x{03A9}" => 'Omega'); | ||
| 186 | my $GREEK_RE = '(' . join('|', sort keys %GREEK_ASCII_MAP) . ')'; | ||
| 187 | |||
| 188 | sub getIndexKeyID { | ||
| 189 | my ($key) = @_; | ||
| 190 | $key =~ s/^\s+//s; $key =~ s/\s+$//s; # Trim leading/trailing, in any case | ||
| 191 | # We don't want accented chars (do we?) but we need to decompose the accents! | ||
| 192 | $key = NFD($key); | ||
| 193 | $key =~ s/$GREEK_RE/$GREEK_ASCII_MAP{$1}/eg; | ||
| 194 | $key = unidecode($key); | ||
| 195 | $key =~ s/[^a-zA-Z0-9]//g; | ||
| 196 | ## Shouldn't be case insensitive? | ||
| 197 | ## $key =~ tr|A-Z|a-z|; | ||
| 198 | return $key; } | ||
| 199 | |||
| 200 | # ================================================================================ | ||
| 201 | # Generate permutations of indexing phrases. | ||
| 202 | sub permute { | ||
| 203 | my (@l) = @_; | ||
| 204 | if (scalar(@l) > 1) { | ||
| 205 | return map { permute_aux($l[$_], @l[0 .. $_ - 1], @l[$_ + 1 .. $#l]) } 0 .. $#l; } | ||
| 206 | else { | ||
| 207 | return [@l]; } } | ||
| 208 | |||
| 209 | sub permute_aux { | ||
| 210 | my ($first, @rest) = @_; | ||
| 211 | return map { [$first, @$_] } permute(@rest); } | ||
| 212 | |||
| 213 | # Or would cyclic permutations be more appropriate? | ||
| 214 | # We could get odd orderings, if authors aren't consistent, | ||
| 215 | # but would avoid silly redundancies in small top-level listings. | ||
| 216 | sub cyclic_permute { | ||
| 217 | my (@l) = @_; | ||
| 218 | if (scalar(@l) > 1) { | ||
| 219 | return map { [@l[$_ .. $#l], @l[0 .. $_ - 1]] } 0 .. $#l; } | ||
| 220 | else { | ||
| 221 | return [@l]; } } | ||
| 222 | |||
| 223 | # ================================================================================ | ||
| 224 | # Formatting the resulting index tree. | ||
| 225 | |||
| 226 | sub makeIndexList { | ||
| 227 | my ($self, $doc, $allphrases, $tree) = @_; | ||
| 228 | my $subtrees = $$tree{subtrees}; | ||
| 229 | if (my @keys = $doc->unisort(keys %$subtrees)) { | ||
| 230 | return ['ltx:indexlist', {}, | ||
| 231 | map { $self->makeIndexEntry($doc, $allphrases, $$subtrees{$_}) } @keys]; } | ||
| 232 | else { | ||
| 233 | return (); } } | ||
| 234 | |||
| 235 | sub makeIndexEntry { | ||
| 236 | my ($self, $doc, $allphrases, $tree) = @_; | ||
| 237 | my $refs = $$tree{referrers}; | ||
| 238 | my $seealso = $$tree{see_also}; | ||
| 239 | my @links = (); | ||
| 240 | if (keys %$refs) { | ||
| 241 | push(@links, ['ltx:text', {}, ' '], $self->combineIndexEntries($doc, $refs)); } | ||
| 242 | if ($seealso) { | ||
| 243 | my %saw = (); | ||
| 244 | foreach my $see (@$seealso) { | ||
| 245 | push(@links, ', '); # if @links; | ||
| 246 | if (my $name = $see->getAttribute('name')) { | ||
| 247 | push(@links, ['ltx:text', { font => 'italic' }, $name, ' ']); } | ||
| 248 | my $phrase = getIndexContentKey($see); | ||
| 249 | if (my @seelinks = $self->seealsoSearch($doc, $allphrases, $tree, $see)) { | ||
| 250 | push(@links, @seelinks); } | ||
| 251 | else { | ||
| 252 | Warn('expected', $phrase, undef, | ||
| 253 | "Missing index see-also term '$phrase'", "(seen under $$tree{key})") | ||
| 254 | unless $doc->findnodes("descendant-or-self::ltx:ref", $see); | ||
| 255 | push(@links, ['ltx:text', {}, $see->childNodes]); } } } | ||
| 256 | |||
| 257 | return ['ltx:indexentry', { 'xml:id' => $$tree{id} }, | ||
| 258 | ['ltx:indexphrase', {}, $doc->trimChildNodes($$tree{phrase})], | ||
| 259 | (@links ? (['ltx:indexrefs', {}, @links]) : ()), | ||
| 260 | $self->makeIndexList($doc, $allphrases, $tree)]; } | ||
| 261 | |||
| 262 | # Sorting comparison that puts different cases together | ||
| 263 | # Really, it's only used for id's... (would like an id-sort!) | ||
| 264 | sub alphacmp { | ||
| 265 | return (lc($a) cmp lc($b)) || ($a cmp $b); } | ||
| 266 | |||
| 267 | # combine a set of links into the document; this corresponds to the list of page numbers. | ||
| 268 | # we want them sorted in document order, but also want to combine the end points of ranges. | ||
| 269 | sub combineIndexEntries { | ||
| 270 | my ($self, $doc, $refs) = @_; | ||
| 271 | my @ids = sort alphacmp keys %$refs; | ||
| 272 | # my @ids = $doc->unisort(keys %$refs); | ||
| 273 | my @links = (); | ||
| 274 | while (@ids) { | ||
| 275 | my $id = shift(@ids); | ||
| 276 | my $entry = $$refs{$id}; | ||
| 277 | if ($$entry{rangestart}) { | ||
| 278 | my $startid = $id; | ||
| 279 | my $endid = $id; | ||
| 280 | my $lvl = 1; | ||
| 281 | while (@ids) { | ||
| 282 | $endid = shift(@ids); | ||
| 283 | $lvl-- if $$refs{$endid}{rangestart}; | ||
| 284 | $lvl-- if $$refs{$endid}{rangeend}; | ||
| 285 | last unless $lvl; } | ||
| 286 | push(@links, | ||
| 287 | ['ltx:text', {}, | ||
| 288 | $self->makeIndexRefs($doc, $startid, | ||
| 289 | grep { $_ ne 'rangestart' } sort keys %$entry), | ||
| 290 | "\x{2014}", | ||
| 291 | $self->makeIndexRefs($doc, $endid, | ||
| 292 | grep { $_ ne 'rangeend' } sort keys %{ $$refs{$endid} })]); } | ||
| 293 | else { | ||
| 294 | push(@links, $self->makeIndexRefs($doc, $id, sort keys %$entry)); } } | ||
| 295 | return conjoin(@links); } | ||
| 296 | |||
| 297 | # Make a single ref to a "page", in a particular style. | ||
| 298 | # Given that sorted styles gives bold, italic, normal, | ||
| 299 | # let's just do the first. | ||
| 300 | sub makeIndexRefs { | ||
| 301 | my ($self, $doc, $id, @styles) = @_; | ||
| 302 | return ((($styles[0] || 'normal') ne 'normal') | ||
| 303 | ? ['ltx:text', { font => $styles[0] }, ['ltx:ref', { idref => $id, show => 'typerefnum' }]] | ||
| 304 | : ['ltx:ref', { idref => $id, show => 'typerefnum' }]); } | ||
| 305 | |||
| 306 | #====================================================================== | ||
| 307 | # Dealing with See & Seealso entries. | ||
| 308 | # A LOTTA work, for such a little thing! | ||
| 309 | |||
| 310 | # Regular index entries, possibly with several levels, are somewhat | ||
| 311 | # structured & formalized: they need to match so that entries can be combined; | ||
| 312 | # they also optionally allow for a sort (& comparison) key. | ||
| 313 | # Seealso entries, are not so structured; there's no provision for a sort key. | ||
| 314 | # They USUALLY will refer to another regular index entry, but aren't required to. | ||
| 315 | # It is nice to LINK such a seealso entry to the corresponding regular entry, if possible! | ||
| 316 | |||
| 317 | # So, we go on a fishing expedition to find possible phrases. | ||
| 318 | # I find several idioms in use (but perhaps biased by DLMF): | ||
| 319 | # (1) "topic" may refer to top-level "topic", or one within the current subtree | ||
| 320 | # (2) "topic1, topic2 and topic3" may refer to a single entry, or may refer to 3 separate entries. | ||
| 321 | # (3) "topic1 topic2" or "topic1, topic2" may refer to a single entry, or may refer | ||
| 322 | # to a 2 level entry like \index(topic1|topic2) | ||
| 323 | # And finally, case & plural differences may indicate distinct top-level concepts, | ||
| 324 | # or may simply be insignificant variations in phrasing! | ||
| 325 | |||
| 326 | sub seealsoSearch { | ||
| 327 | my ($self, $doc, $allphrases, $contexttree, $see) = @_; | ||
| 328 | return seealsoSearch_rec($doc, $allphrases, $contexttree, seealsoPartition($doc, $see)); } | ||
| 329 | |||
| 330 | # @parts are alternating (potential) term, (potential) delimiter, ... | ||
| 331 | sub seealsoSearch_rec { | ||
| 332 | my ($doc, $allphrases, $contexttree, @parts) = @_; | ||
| 333 | my ($link, @links); | ||
| 334 | if (scalar(@parts) < 1) { return (); } | ||
| 335 | elsif (scalar(@parts) < 3) { | ||
| 336 | # Single term? (w/ possible trailing punct) just look it up | ||
| 337 | if ($link = lookupSeealsoPhrase($doc, $allphrases, $contexttree, $parts[0])) { | ||
| 338 | return ($link, ($parts[1] ? cdr($parts[1]) : ())); } } | ||
| 339 | # try first delimiter "literally" (possibly reiterpretedy by lookupSeealsoPhrase) | ||
| 340 | # recurse, so that all alternatives of next delimiter will be considered. | ||
| 341 | elsif (@links = seealsoSearch_rec($doc, $allphrases, $contexttree, | ||
| 342 | seealsoJoin(@parts[0 .. 2]), @parts[3 .. $#parts])) { | ||
| 343 | return @links; } | ||
| 344 | # try any delimiter as possibly a separator between individual entries; | ||
| 345 | # and recurse to handle next delimiter | ||
| 346 | elsif (($link = lookupSeealsoPhrase($doc, $allphrases, $contexttree, $parts[0])) | ||
| 347 | && (@links = seealsoSearch_rec($doc, $allphrases, $contexttree, | ||
| 348 | @parts[2 .. $#parts]))) { | ||
| 349 | return ($link, cdr($parts[1]), @links); } | ||
| 350 | return; } | ||
| 351 | |||
| 352 | sub car { return $$_[0][0]; } | ||
| 353 | |||
| 354 | sub cdr { | ||
| 355 | my ($key, @xml) = @{ $_[0] }; | ||
| 356 | return @xml; } | ||
| 357 | |||
| 358 | # Reassemble a partition (list of [key,xml] pairs) into a single such pair. | ||
| 359 | sub seealsoJoin { | ||
| 360 | my (@parts) = @_; | ||
| 361 | return [getIndexContentKey(join('', map { $$_[0] } @parts)), map { cdr($_) } @parts]; } | ||
| 362 | |||
| 363 | # Look for single phrase either within one of the levels of the current $contexttree, | ||
| 364 | # or at top-level. Try it as-is, or ignoring commas, and/or case-differences. | ||
| 365 | sub lookupSeealsoPhrase { | ||
| 366 | my ($doc, $allphrases, $contexttree, $pair) = @_; | ||
| 367 | my ($phrase, @xml) = @$pair; | ||
| 368 | # concoct various phrases to search for | ||
| 369 | my $pnc = $phrase; $pnc =~ s/,\s*/ /sg; # Ignore punct? | ||
| 370 | my $ps = $phrase; $ps =~ s/(\w+)s\b/$1/sg; # Ignore plurals? | ||
| 371 | my $psnc = $ps; $psnc =~ s/,\s*/ /sg; # Ignore punct AND plurals? | ||
| 372 | my $pnlvl = $phrase; $pnlvl =~ s/,\s*/./sg; # Convert punct to levels? | ||
| 373 | foreach my $trial ($phrase, lc($phrase), | ||
| 374 | $pnc, lc($pnc), | ||
| 375 | $ps, lc($ps), | ||
| 376 | $psnc, lc($psnc), | ||
| 377 | $pnlvl, lc($pnlvl), | ||
| 378 | ) { | ||
| 379 | my $t = $contexttree; | ||
| 380 | while ($t) { | ||
| 381 | if (my $id = $$allphrases{ ($$t{fullphrasetext} ? $$t{fullphrasetext} . " " : '') . $trial }) { | ||
| 382 | return ['ltx:ref', { idref => $id }, @xml]; } | ||
| 383 | $t = $$t{parent}; } } | ||
| 384 | return; } | ||
| 385 | |||
| 386 | # Partition a seealso (xml) phrase into a sequence of alternating | ||
| 387 | # candidate index phrases (which will be looked up) | ||
| 388 | # candidate delimiters (which potentially split the phrase or not; | ||
| 389 | # see discussion above about delimiters). | ||
| 390 | # This is analogous to a simple split, but that | ||
| 391 | # (a) the argument is XML, with the pieces of delimiter ("," space, "and"...) | ||
| 392 | # potentially distributed amongst distinct xml elements, due to styling. | ||
| 393 | # (b) we want to preserve the XML associated with each phrase & delimiter | ||
| 394 | # in order to fill-in and separate the resulting ltx:ref's | ||
| 395 | # Messy! | ||
| 396 | sub seealsoPartition { | ||
| 397 | my ($doc, $see) = @_; | ||
| 398 | my @parts = seealsoPartition_aux($doc, $see); | ||
| 399 | # Combine adjacent conjunctions & punctuation chunks | ||
| 400 | my @result = (shift(@parts)); | ||
| 401 | while (@parts) { | ||
| 402 | my $next = shift(@parts); | ||
| 403 | my $prev_is = ($result[-1][0] =~ /^,?\s*(?:,|\.|\s+|\band\s+also|\band|\bor)\s*$/); | ||
| 404 | my $next_is = ($$next[0] =~ /^(?:,|\.|\s+|and\b|or\b)/); | ||
| 405 | # If either BOTH or NEITHER prev & next are delimiters, combine them. | ||
| 406 | if (!($prev_is xor $next_is)) { | ||
| 407 | my ($k, @x) = @$next; | ||
| 408 | $result[-1][0] .= $k; | ||
| 409 | push(@{ $result[-1] }, @x); } | ||
| 410 | else { | ||
| 411 | push(@result, $next); } } | ||
| 412 | # Now merge any adjacent phrase and/or space chunks into candidate phrase | ||
| 413 | @parts = @result; @result = (shift(@parts)); | ||
| 414 | while (@parts) { | ||
| 415 | my $next = shift(@parts); | ||
| 416 | # If next is pure space, combine with prev AND following! | ||
| 417 | if (($$next[0] =~ /^\s+$/s) && scalar(@parts)) { | ||
| 418 | my ($k1, @x1) = @$next; | ||
| 419 | my ($k2, @x2) = @{ shift(@parts) }; | ||
| 420 | $result[-1][0] .= $k1 . $k2; | ||
| 421 | push(@{ $result[-1] }, @x1, @x2); } | ||
| 422 | else { | ||
| 423 | push(@result, $next); } } | ||
| 424 | return @result; } | ||
| 425 | |||
| 426 | # Recursively split the XML $see into pure phrase or delimiter chunks. | ||
| 427 | # we'll still need to combine adjacent chunks appropriately (see above). | ||
| 428 | sub seealsoPartition_aux { | ||
| 429 | my ($doc, $see) = @_; | ||
| 430 | my @result = (); | ||
| 431 | foreach my $ch ($see->childNodes) { | ||
| 432 | my $t = $ch->nodeType; | ||
| 433 | if ($t == XML_TEXT_NODE) { | ||
| 434 | my $string = $ch->textContent; | ||
| 435 | while ($string) { | ||
| 436 | if ($string =~ s/^(,|\.|\s+|and\s+also\b|and\b|or\b)//) { | ||
| 437 | push(@result, [$1, $1]); } | ||
| 438 | elsif ($string =~ s/^([^,\.\s]+)//) { | ||
| 439 | push(@result, [getIndexContentKey($1), $1]); } } | ||
| 440 | push(@result, [getIndexContentKey($string), $string]) if $string; } | ||
| 441 | elsif ($t != XML_ELEMENT_NODE) { } | ||
| 442 | else { | ||
| 443 | my $tag = $doc->getQName($ch); | ||
| 444 | if ($tag =~ /^(ltx:text|ltx:emph)$/) { | ||
| 445 | my $attr = { map { ($_ => $ch->getAttribute($_)) } $ch->attributes }; | ||
| 446 | push(@result, map { [$$_[0], [$tag, $attr, cdr($_)]] } seealsoPartition_aux($doc, $ch)); } | ||
| 447 | else { | ||
| 448 | push(@result, [getIndexContentKey($ch), $ch]); } | ||
| 449 | } } | ||
| 450 | return @result; } | ||
| 451 | |||
| 452 | # ================================================================================ | ||
| 453 | # Glosssary | ||
| 454 | # ================================================================================ | ||
| 455 | |||
| 456 | sub getGlossaryEntries { | ||
| 457 | my ($self, $doc, $glossary) = @_; | ||
| 458 | my $lists = $glossary->getAttribute('lists') || ''; | ||
| 459 | return () unless $lists; | ||
| 460 | my $glossary_id = $glossary->getAttribute('xml:id') || ''; | ||
| 461 | my %lists = map { $_ => 1; } split(',', $lists); | ||
| 462 | my @entries = (); | ||
| 463 | foreach my $gkey (grep { /^GLOSSARY:/ } $$self{db}->getKeys) { | ||
| 464 | my ($ignore, $list, $key) = split(':', $gkey); | ||
| 465 | next unless $lists{$list}; | ||
| 466 | my $gitem = $$self{db}->lookup($gkey); | ||
| 467 | my $refs = $gitem->getValue('referrers'); | ||
| 468 | next unless $refs && %{$refs}; | ||
| 469 | my $term = $gitem->getValue('phrase:name'); | ||
| 470 | my $desc = $gitem->getValue('phrase:description'); | ||
| 471 | my $sortkey = $gitem->getValue('phrase:sort') || $key; | ||
| 472 | my $initial = ($sortkey =~ /^([a-zA-Z])/ ? uc($1) : '*'); | ||
| 473 | my $id = $glossary_id . '.' . $key; | ||
| 474 | push(@entries, | ||
| 475 | { initial => $initial, sortkey => $sortkey, | ||
| 476 | formatted => ['ltx:glossaryentry', { lists => $lists, 'xml:id' => $id, key => $key }, | ||
| 477 | ['ltx:glossaryphrase', { role => 'label', key => $key }, # what key?? | ||
| 478 | (map { $doc->cloneNode($_, 'glo') } $doc->trimChildNodes($term))], | ||
| 479 | ['ltx:glossaryphrase', { role => 'definition' }, | ||
| 480 | (map { $doc->cloneNode($_, 'glo') } $doc->trimChildNodes($desc)) | ||
| 481 | ]] }); | ||
| 482 | } | ||
| 483 | return @entries; } | ||
| 484 | |||
| 485 | sub makeGlossaryList { | ||
| 486 | my ($self, $doc, @entries) = @_; | ||
| 487 | my %hash = map { ($$_{sortkey} => $_) } @entries; | ||
| 488 | my @keys = $doc->unisort(keys %hash); | ||
| 489 | return ['ltx:glossarylist', {}, | ||
| 490 | map { $hash{$_}{formatted} } @keys]; } | ||
| 491 | |||
| 492 | # ================================================================================ | ||
| 493 | sub conjoin { | ||
| 494 | my (@items) = @_; | ||
| 495 | my @result = (); | ||
| 496 | if (@items) { | ||
| 497 | push(@result, shift(@items)); | ||
| 498 | while (@items) { | ||
| 499 | push(@result, ", ", shift(@items)); } } | ||
| 500 | return @result; } | ||
| 501 | |||
| 502 | # ================================================================================ | ||
| 503 | 1; | ||
| 504 |