| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::KeyVals | # | ||
| 3 | # | Support for key-value pairs for LaTeXML | # | ||
| 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::Core::KeyVals; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Global; | ||
| 16 | use LaTeXML::Common::Object; | ||
| 17 | use LaTeXML::Common::Error; | ||
| 18 | use LaTeXML::Core::Token; | ||
| 19 | use LaTeXML::Core::Tokens; | ||
| 20 | use base qw(LaTeXML::Common::Object); | ||
| 21 | |||
| 22 | our @EXPORT = ( | ||
| 23 | qw(&GetKeyVal &GetKeyVals) | ||
| 24 | ); | ||
| 25 | |||
| 26 | #====================================================================== | ||
| 27 | # Exposed Methods | ||
| 28 | #====================================================================== | ||
| 29 | |||
| 30 | sub GetKeyVal { | ||
| 31 | my ($keyval, $key) = @_; | ||
| 32 | return (defined $keyval) && $keyval->getValue($key); } | ||
| 33 | |||
| 34 | sub GetKeyVals { | ||
| 35 | my ($keyval) = @_; | ||
| 36 | return (defined $keyval ? $keyval->getKeyVals : {}); } | ||
| 37 | |||
| 38 | #====================================================================== | ||
| 39 | # The KeyVals constructor | ||
| 40 | #====================================================================== | ||
| 41 | # This defines the KeyVals data object that can appear in the datastream | ||
| 42 | # along with tokens, boxes, etc. | ||
| 43 | # Thus it has to be digestible, however we may not want to digest it more | ||
| 44 | # than once. | ||
| 45 | #********************************************************************** | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ($class, $prefix, $keysets, %options) = @_; | ||
| 49 | # parse all the arguments | ||
| 50 | $prefix = defined($prefix) ? ToString($prefix) : 'KV'; | ||
| 51 | $keysets = [split(',', ToString(defined($keysets) ? $keysets : '_anonymous_'))] unless (ref($keysets) eq 'ARRAY'); | ||
| 52 | my $skip = $options{skip}; | ||
| 53 | $skip = [split(',', ToString(defined($options{skip}) ? $options{skip} : ''))] unless (ref($options{skip}) eq 'ARRAY'); | ||
| 54 | my $setAll = $options{setAll} ? 1 : 0; | ||
| 55 | my $setInternals = $options{setInternals} ? 1 : 0; | ||
| 56 | my $skipMissing = $options{skipMissing}; | ||
| 57 | my $hookMissing = $options{hookMissing}; | ||
| 58 | # hook missing, if defined, must be a token | ||
| 59 | if (defined($hookMissing) && $hookMissing) { | ||
| 60 | $hookMissing = ref($hookMissing) ? $hookMissing : T_CS(ToString($hookMissing)); } | ||
| 61 | else { $hookMissing = undef; } | ||
| 62 | # skip missing may be a token (=store all the missing macros there) | ||
| 63 | unless (ref($skipMissing)) { | ||
| 64 | # may be undef or 0 (= throw errors) | ||
| 65 | unless (defined($skipMissing)) { $skipMissing = undef; } | ||
| 66 | elsif ($skipMissing eq '0') { $skipMissing = undef; } | ||
| 67 | # may be 1 (= ignore all missing keys) | ||
| 68 | elsif ($skipMissing eq '1') { $skipMissing = 1; } | ||
| 69 | # may be a string (= store all the missing keys there) | ||
| 70 | else { $skipMissing = T_CS($skipMissing); } } | ||
| 71 | my %hash = (); | ||
| 72 | my $self = bless { | ||
| 73 | # which KeyVals are we parsing and how do we behave? | ||
| 74 | prefix => $prefix, keysets => $keysets, | ||
| 75 | skip => $skip, setAll => $setAll, setInternals => $setInternals, | ||
| 76 | skipMissing => $skipMissing, hookMissing => $hookMissing, | ||
| 77 | |||
| 78 | # all the internal representations | ||
| 79 | tuples => [], cachedPairs => [()], cachedHash => \%hash, | ||
| 80 | |||
| 81 | # all the character tokens we used | ||
| 82 | punct => $options{punct}, assign => $options{assign} }, | ||
| 83 | |||
| 84 | $class; | ||
| 85 | return $self; } | ||
| 86 | |||
| 87 | #====================================================================== | ||
| 88 | # Accessors for internal usage | ||
| 89 | #====================================================================== | ||
| 90 | |||
| 91 | sub getPrefix { | ||
| 92 | my ($self) = @_; | ||
| 93 | return $$self{prefix}; } | ||
| 94 | |||
| 95 | sub getKeySets { | ||
| 96 | my ($self) = @_; | ||
| 97 | return @{ $$self{keysets} }; } | ||
| 98 | |||
| 99 | sub getSetAll { | ||
| 100 | my ($self) = @_; | ||
| 101 | return $$self{setAll}; } | ||
| 102 | |||
| 103 | sub getSetInternals { | ||
| 104 | my ($self) = @_; | ||
| 105 | return $$self{setInternals}; } | ||
| 106 | |||
| 107 | sub getTuples { | ||
| 108 | my ($self) = @_; | ||
| 109 | return @{ $$self{tuples}; } } | ||
| 110 | |||
| 111 | sub setTuples { | ||
| 112 | my ($self, @tuples) = @_; | ||
| 113 | $$self{tuples} = [@tuples]; | ||
| 114 | # we need to build all the caches | ||
| 115 | $self->rebuild; | ||
| 116 | return; } | ||
| 117 | |||
| 118 | sub getCachedPairs { | ||
| 119 | my ($self) = @_; | ||
| 120 | return @{ $$self{cachedPairs}; } } | ||
| 121 | |||
| 122 | sub getCachedHash { | ||
| 123 | my ($self) = @_; | ||
| 124 | return %{ $$self{cachedHash} }; } | ||
| 125 | |||
| 126 | sub getSkip { | ||
| 127 | my ($self) = @_; | ||
| 128 | return @{ $$self{skip} }; } | ||
| 129 | |||
| 130 | sub getSkipMissing { | ||
| 131 | my ($self) = @_; | ||
| 132 | return $$self{skipMissing}; } | ||
| 133 | |||
| 134 | sub getHookMissing { | ||
| 135 | my ($self) = @_; | ||
| 136 | return $$self{hookMissing}; } | ||
| 137 | |||
| 138 | #====================================================================== | ||
| 139 | # Resolution to KeySets | ||
| 140 | #====================================================================== | ||
| 141 | |||
| 142 | sub resolveKeyValFor { | ||
| 143 | my ($self, $key) = @_; | ||
| 144 | |||
| 145 | my $prefix = $self->getPrefix; | ||
| 146 | my @keysets = $self->getKeySets; | ||
| 147 | my @sets = (); | ||
| 148 | |||
| 149 | # iterate over the keysets | ||
| 150 | foreach my $keyset (@keysets) { | ||
| 151 | my $bkeyval = LaTeXML::Core::KeyVal->new($prefix, $keyset, $key); | ||
| 152 | push(@sets, $bkeyval) if $bkeyval->isDefined(1); } | ||
| 153 | |||
| 154 | # throw an error, unless we record the missing macros | ||
| 155 | if (scalar @sets == 0) { | ||
| 156 | Error( | ||
| 157 | 'undefined', 'Encountered unknown KeyVals key', | ||
| 158 | "'$key' with prefix '$prefix' not defined in '" . join(",", @keysets) . "', " . | ||
| 159 | 'were you perhaps using \setkeys instead of \setkeys*?') unless defined($self->getSkipMissing); | ||
| 160 | return; } | ||
| 161 | |||
| 162 | # return either the first or all of the elements | ||
| 163 | return ($sets[0]) unless $self->getSetAll; | ||
| 164 | return @sets; } | ||
| 165 | |||
| 166 | sub canResolveKeyValFor { | ||
| 167 | my ($self, $key) = @_; | ||
| 168 | my $prefix = $self->getPrefix; | ||
| 169 | my @keysets = $self->getKeySets; | ||
| 170 | |||
| 171 | # iterate over the keysets | ||
| 172 | foreach my $keyset (@keysets) { | ||
| 173 | my $bkeyval = LaTeXML::Core::KeyVal->new($prefix, $keyset, $key); | ||
| 174 | return 1 if $bkeyval->isDefined(1); } | ||
| 175 | |||
| 176 | return 0; } | ||
| 177 | |||
| 178 | sub getPrimaryKeyValOf { | ||
| 179 | my ($self, $key, @keysets) = @_; | ||
| 180 | |||
| 181 | if (scalar @keysets == 0) { | ||
| 182 | my $prefix = $self->getPrefix; | ||
| 183 | my @headsets = $self->getKeySets; | ||
| 184 | return LaTeXML::Core::KeyVal->new($prefix, $headsets[0], $key); } | ||
| 185 | else { return $keysets[0] } } | ||
| 186 | |||
| 187 | #====================================================================== | ||
| 188 | # Changing contained values | ||
| 189 | #====================================================================== | ||
| 190 | |||
| 191 | sub addValue { | ||
| 192 | my ($self, $key, $value, $useDefault, $noRebuild) = @_; | ||
| 193 | |||
| 194 | # figure out the keyset(s) for the key to be added | ||
| 195 | my @keysets = $self->resolveKeyValFor($key); | ||
| 196 | my $headset = $self->getPrimaryKeyValOf($key, @keysets); | ||
| 197 | |||
| 198 | # and add the new tuple to the set of tuples | ||
| 199 | push(@{ $$self{tuples} }, | ||
| 200 | [$key, ($useDefault ? $headset->getDefault : $value), $useDefault, [@keysets], $headset]); | ||
| 201 | |||
| 202 | # we now need to rebuild, unless we were asked not to | ||
| 203 | # TODO: Maybe only update the last element? | ||
| 204 | $self->rebuild unless $noRebuild; | ||
| 205 | |||
| 206 | return; } | ||
| 207 | |||
| 208 | sub setValue { | ||
| 209 | my ($self, $key, $value, $useDefault) = @_; | ||
| 210 | |||
| 211 | # delete the existing values by skipping key | ||
| 212 | $self->rebuild($key); | ||
| 213 | |||
| 214 | # if we have an array, we need to push all of them | ||
| 215 | if (ref $value eq 'ARRAY') { | ||
| 216 | foreach my $val (@{$value}) { | ||
| 217 | $self->addValue($key, $val, $useDefault, 1); } | ||
| 218 | |||
| 219 | $self->rebuild(); | ||
| 220 | return; } | ||
| 221 | |||
| 222 | # if we have a single value, set it normally | ||
| 223 | elsif (defined($value)) { | ||
| 224 | $self->addValue($key, $value, $useDefault); } | ||
| 225 | |||
| 226 | return; } | ||
| 227 | |||
| 228 | sub rebuild { | ||
| 229 | my ($self, $skip) = @_; | ||
| 230 | |||
| 231 | # the new data structures to create | ||
| 232 | my @newtuples = (); | ||
| 233 | my @pairs = (); | ||
| 234 | my %hash = (); | ||
| 235 | |||
| 236 | foreach my $tuple (@{ $$self{tuples} }) { | ||
| 237 | # take all the elements we need from the stack | ||
| 238 | my ($key, $value, $useDefault, $resolution, $keyval) = @$tuple; | ||
| 239 | |||
| 240 | # if we want to skip some values, we need to store new tuples | ||
| 241 | if (defined($skip)) { | ||
| 242 | next if $key eq $skip; | ||
| 243 | push(@newtuples, [$key, $value, $useDefault, $resolution, $keyval]) if defined($skip); } | ||
| 244 | |||
| 245 | # push key / value into the pair | ||
| 246 | push(@pairs, $key, $value); | ||
| 247 | |||
| 248 | # if we do not have a value yet, set it | ||
| 249 | if (!defined $hash{$key}) { $hash{$key} = $value; } | ||
| 250 | |||
| 251 | # If we get a third value, push into an array | ||
| 252 | # This is unlikely to be what the caller expects!! But what else? | ||
| 253 | elsif (ref $hash{$key} eq 'ARRAY') { push(@{ $hash{$key} }, $value); } | ||
| 254 | |||
| 255 | # If we get a second value, make an array | ||
| 256 | else { $hash{$key} = [$hash{$key}, $value]; } } | ||
| 257 | |||
| 258 | # store all of the values | ||
| 259 | $$self{cachedPairs} = [@pairs]; | ||
| 260 | $$self{cachedHash} = \%hash; | ||
| 261 | $$self{tuples} = [@newtuples] if defined($skip); | ||
| 262 | |||
| 263 | return; } | ||
| 264 | |||
| 265 | #====================================================================== | ||
| 266 | # parsing values from a gullet | ||
| 267 | #====================================================================== | ||
| 268 | |||
| 269 | # A KeyVal argument MUST be delimited by either braces or brackets (if optional) | ||
| 270 | # This method reads the keyval pairs INCLUDING the delimiters, (rather than | ||
| 271 | # parsing after the fact), since some values may have special catcode needs. | ||
| 272 | ##my $T_EQ = T_OTHER('='); # [CONSTANT] | ||
| 273 | ##my $T_COMMA = T_OTHER(','); # [CONSTANT] | ||
| 274 | |||
| 275 | sub readFrom { | ||
| 276 | my ($self, $gullet, $until, %options) = @_; | ||
| 277 | |||
| 278 | # if we want to force skipMissing keys, we set it up here | ||
| 279 | my $silenceMissing = $options{silenceMissing} ? 1 : 0; | ||
| 280 | |||
| 281 | my $skipMissing = $self->getSkipMissing; | ||
| 282 | my $hookMissing = $self->getHookMissing; | ||
| 283 | |||
| 284 | # if we want to silence all missing errors, store them in a hook | ||
| 285 | if ($silenceMissing) { | ||
| 286 | $$self{skipMissing} = 1; | ||
| 287 | $$self{hookMissing} = undef; } | ||
| 288 | |||
| 289 | # read the opening token and figure out where we are | ||
| 290 | my $startloc = $gullet->getLocator; | ||
| 291 | |||
| 292 | # set and read tokens | ||
| 293 | my $open = $gullet->readToken; | ||
| 294 | $$self{assign} = T_OTHER('='); | ||
| 295 | $$self{punct} = T_OTHER(','); | ||
| 296 | my ($punct, $assign) = ($$self{punct}, $$self{assign}); | ||
| 297 | |||
| 298 | # create arrays for key-value pairs and explicit values | ||
| 299 | my @kv = (); | ||
| 300 | my @explicits = (); | ||
| 301 | |||
| 302 | # iterate over all the key-value pairs to read | ||
| 303 | while (1) { | ||
| 304 | |||
| 305 | # gobble spaces | ||
| 306 | $gullet->skipSpaces; | ||
| 307 | |||
| 308 | # Read a single keyword, get a delimiter and a set of keyword tokens | ||
| 309 | my ($ktoks, $delim) = $self->readKeyWordFrom($gullet, $until); | ||
| 310 | |||
| 311 | # if there was no delimiter at the end, we throw an error | ||
| 312 | Error('expected', $until, $gullet, | ||
| 313 | "Fell off end expecting " . Stringify($until) . " while reading KeyVal key", | ||
| 314 | "key started at " . ToString($startloc)) | ||
| 315 | unless $delim; | ||
| 316 | |||
| 317 | # turn the key tokens into a string and normalize | ||
| 318 | my $key = ToString($ktoks); $key =~ s/\s//g; | ||
| 319 | |||
| 320 | # if we have a non-empty key | ||
| 321 | if ($key) { | ||
| 322 | |||
| 323 | my $value; | ||
| 324 | my $isDefault; | ||
| 325 | |||
| 326 | # if we have an '=', we explcity assign a value | ||
| 327 | if ($delim->equals($assign)) { | ||
| 328 | $isDefault = 0; | ||
| 329 | |||
| 330 | # setup the key-codes to properly read | ||
| 331 | my $keyval = $self->getPrimaryKeyValOf($key, $self->resolveKeyValFor($key)); | ||
| 332 | my $keydef = $keyval->getType(); | ||
| 333 | $keydef->setupCatcodes if $keydef; | ||
| 334 | |||
| 335 | # read until $punct | ||
| 336 | my ($tok, @toks) = (); | ||
| 337 | while ((!defined($delim = $gullet->readMatch($punct, $until))) | ||
| 338 | && (defined($tok = $gullet->readToken()))) { # Copy next token to args | ||
| 339 | push(@toks, $tok, | ||
| 340 | ($tok->getCatcode == CC_BEGIN ? ($gullet->readBalanced->unlist, T_END) : ())); } | ||
| 341 | |||
| 342 | # reparse (and expand) the tokens representing the value | ||
| 343 | $value = Tokens(@toks); | ||
| 344 | $value = $keydef->reparse($gullet, $value) if $keydef && $value; | ||
| 345 | |||
| 346 | # and cleanup | ||
| 347 | $keydef->revertCatcodes if $keydef; } | ||
| 348 | |||
| 349 | # we did not get an '=', and thus need to read the default value | ||
| 350 | else { $isDefault = 1; } | ||
| 351 | |||
| 352 | # and store our value please | ||
| 353 | $self->addValue($key, $value, $isDefault, 0) if (!$silenceMissing || $self->canResolveKeyValFor($key)); } | ||
| 354 | |||
| 355 | # we finish if we have the last element | ||
| 356 | last if $delim->equals($until); } | ||
| 357 | |||
| 358 | # rebuild and return nothing | ||
| 359 | $self->rebuild; | ||
| 360 | |||
| 361 | # restore all settings if we silenced the missing keys | ||
| 362 | if ($silenceMissing) { | ||
| 363 | $$self{skipMissing} = $skipMissing; | ||
| 364 | $$self{hookMissing} = $hookMissing; } | ||
| 365 | |||
| 366 | return; } | ||
| 367 | |||
| 368 | sub readKeyWordFrom { | ||
| 369 | my ($self, $gullet, $close) = @_; | ||
| 370 | |||
| 371 | # set of tokens we will expand | ||
| 372 | my @tokens = (); | ||
| 373 | |||
| 374 | # set of delimiters we want to ignore | ||
| 375 | my @delim = ($close, $$self{punct}, $$self{assign}); | ||
| 376 | |||
| 377 | # we do not want any spaces | ||
| 378 | $gullet->skipSpaces; | ||
| 379 | |||
| 380 | # read tokens one-by-one | ||
| 381 | my $token; | ||
| 382 | while ($token = $gullet->readXToken) { | ||
| 383 | # skip to the next iteration if we have a paragraph | ||
| 384 | next if $token->equals(T_CS('\par')); | ||
| 385 | |||
| 386 | # if we have one of out delimiters, we end | ||
| 387 | last if grep { $token->equals($_) } @delim; | ||
| 388 | |||
| 389 | # push a token unless we have a space | ||
| 390 | # TODO: remove or normalize | ||
| 391 | push(@tokens, $token) unless $$token[1] == CC_SPACE; } | ||
| 392 | |||
| 393 | # return the tokens and the last token | ||
| 394 | return (Tokens(@tokens), $token); } | ||
| 395 | |||
| 396 | #====================================================================== | ||
| 397 | # Public accessors of all the values | ||
| 398 | #====================================================================== | ||
| 399 | # Note: The API of this need to be stable, as people may be using it | ||
| 400 | |||
| 401 | # return the value of a given key. If multiple values are given, return the last one. | ||
| 402 | sub getValue { | ||
| 403 | my ($self, $key) = @_; | ||
| 404 | my %hash = $self->getCachedHash; | ||
| 405 | my $value = $hash{$key}; | ||
| 406 | # Since we (by default) accumulate lists of values when repeated, | ||
| 407 | # we need to provide the "common" thing: return the last value given. | ||
| 408 | return (!defined $value ? undef : (ref $value eq 'ARRAY' ? $$value[-1] : $value)); } | ||
| 409 | |||
| 410 | # return a list of values for a given key | ||
| 411 | sub getValues { | ||
| 412 | my ($self, $key) = @_; | ||
| 413 | my %hash = $self->getCachedHash; | ||
| 414 | my $value = $hash{$key}; | ||
| 415 | return (!defined $value ? () : (ref $value eq 'ARRAY' ? @$value : ($value))); } | ||
| 416 | |||
| 417 | # return the set of key-value pairs | ||
| 418 | sub getPairs { | ||
| 419 | my ($self) = @_; | ||
| 420 | return $self->getCachedPairs; } | ||
| 421 | |||
| 422 | # returns a key => ToString(value) | ||
| 423 | sub getHash { | ||
| 424 | my ($self) = @_; | ||
| 425 | my %hash = $self->getCachedHash; | ||
| 426 | return map { ($_ => ToString($hash{$_})) } keys %hash; } | ||
| 427 | |||
| 428 | # return a hash of key-value pairs | ||
| 429 | sub getKeyVals { | ||
| 430 | my ($self) = @_; | ||
| 431 | my %hash = $self->getCachedHash; | ||
| 432 | return \%hash; } | ||
| 433 | |||
| 434 | # checks if the value for a given key exists | ||
| 435 | sub hasKey { | ||
| 436 | my ($self, $key) = @_; | ||
| 437 | my %hash = $self->getCachedHash; | ||
| 438 | return exists $hash{$key}; } | ||
| 439 | |||
| 440 | #====================================================================== | ||
| 441 | # Value Related Reversion | ||
| 442 | #====================================================================== | ||
| 443 | |||
| 444 | sub setKeysExpansion { | ||
| 445 | my ($self) = @_; | ||
| 446 | my @skip = $self->getSkip; | ||
| 447 | my $setInternals = $self->getSetInternals; | ||
| 448 | |||
| 449 | my ($punct, $assign) = ($$self{punct}, $$self{assign}); | ||
| 450 | |||
| 451 | # we might have to store values in a seperate token | ||
| 452 | my $rmmacro = $self->getSkipMissing; | ||
| 453 | my $hookMissing = $self->getHookMissing; | ||
| 454 | my $definedrm = ref($rmmacro) ? 1 : 0; | ||
| 455 | my @rmtokens = (); | ||
| 456 | |||
| 457 | # read in existing tokens (if they are defined) | ||
| 458 | if ($definedrm && $STATE->lookupMeaning($rmmacro)) { | ||
| 459 | @rmtokens = LaTeXML::Package::Expand($rmmacro)->unlist; } | ||
| 460 | |||
| 461 | # define some xkeyval internals | ||
| 462 | my @tokens = $setInternals ? ( | ||
| 463 | T_CS('\def'), T_CS('\XKV@fams'), T_BEGIN, Explode(join(',', $self->getKeySets)), T_END, | ||
| 464 | T_CS('\def'), T_CS('\XKV@na'), T_BEGIN, Explode(join(',', @skip)), T_END | ||
| 465 | ) : (); | ||
| 466 | |||
| 467 | # iterate over the key-value pairs | ||
| 468 | foreach my $tuple (@{ $$self{tuples} }) { | ||
| 469 | my ($key, $value, $useDefault, $resolution, $keyval) = @$tuple; | ||
| 470 | my @keyvals = @{$resolution}; | ||
| 471 | |||
| 472 | # we might want to skip to the next iteration if key is to be omitted | ||
| 473 | next if (grep { $_ eq $key } @skip); | ||
| 474 | |||
| 475 | # we might need to save the macros that weren't saved | ||
| 476 | if (scalar @keyvals == 0) { | ||
| 477 | if ($definedrm) { | ||
| 478 | push(@rmtokens, $self->revertKeyVal($keyval, $value, $useDefault, (@rmtokens ? 0 : 1), | ||
| 479 | 1, $punct, $assign)); } | ||
| 480 | my @reversion = $self->revertKeyVal($keyval, $value, $useDefault, 1, 1, $punct, $assign); | ||
| 481 | push(@tokens, $hookMissing, T_BEGIN, $self->revertKeyVal($keyval, $value, $useDefault, 1, 1, $punct, $assign), T_END) if $hookMissing; | ||
| 482 | next; } | ||
| 483 | |||
| 484 | # and iterate over all valid keysets | ||
| 485 | foreach my $keyset (@keyvals) { | ||
| 486 | my $expansion = $keyset->setKeysExpansion($value, $useDefault, 1, 1, $setInternals); | ||
| 487 | next unless defined($expansion); | ||
| 488 | push(@tokens, $expansion->unlist); } } | ||
| 489 | |||
| 490 | # and assign the macro with the other keys | ||
| 491 | push(@tokens, T_CS('\def'), $rmmacro, T_BEGIN, @rmtokens, T_END) if $definedrm; | ||
| 492 | |||
| 493 | # reset all the internals (if applicable) | ||
| 494 | push(@tokens, | ||
| 495 | T_CS('\def'), T_CS('\XKV@fams'), T_BEGIN, T_END, | ||
| 496 | T_CS('\def'), T_CS('\XKV@na'), T_BEGIN, T_END) if $setInternals; | ||
| 497 | |||
| 498 | # and return the list of tokens | ||
| 499 | return Tokens(@tokens); } | ||
| 500 | |||
| 501 | sub beDigested { | ||
| 502 | my ($self, $stomach) = @_; | ||
| 503 | |||
| 504 | if ($$self{was_digested}) { | ||
| 505 | Info('ignore', 'keyvals', $self, | ||
| 506 | "Skipping digestion of \\setkeys as requested (did you digest a KeyVals twice?) "); } | ||
| 507 | else { | ||
| 508 | $stomach->digest($self->setKeysExpansion); } | ||
| 509 | |||
| 510 | # new tuples we want to create | ||
| 511 | my @newtuples = (); | ||
| 512 | |||
| 513 | # iterate over them | ||
| 514 | foreach my $tuple (@{ $$self{tuples} }) { | ||
| 515 | my ($key, $value, $useDefault, $resolution, $keyval) = @$tuple; | ||
| 516 | # digest a single token | ||
| 517 | my $keydef = $keyval->getType(); | ||
| 518 | my $v = (defined $value ? | ||
| 519 | ($keydef ? $keydef->digest($stomach, $value, undef) : $value->beDigested($stomach)) | ||
| 520 | : undef); | ||
| 521 | push(@newtuples, [$key, $v, $useDefault, $resolution, $keyval]); } | ||
| 522 | |||
| 523 | # read all our current state | ||
| 524 | my $prefix = $self->getPrefix; | ||
| 525 | my $keysets = $self->getKeySets; | ||
| 526 | my $setAll = $self->getSetAll; | ||
| 527 | my $skip = $self->getSkip; | ||
| 528 | my $setInternals = $self->getSetInternals; | ||
| 529 | my $skipMissing = $self->getSkipMissing; | ||
| 530 | my $hookMissing = $self->getHookMissing; | ||
| 531 | my ($punct, $assign) = ($$self{punct}, $$self{assign}); | ||
| 532 | |||
| 533 | # then re-create the current object | ||
| 534 | my $new = LaTeXML::Core::KeyVals->new( | ||
| 535 | $prefix, $keysets, | ||
| 536 | setAll => $setAll, setInternals => $setInternals, | ||
| 537 | skip => $skip, skipMissing => $skipMissing, hookMissing => $hookMissing, | ||
| 538 | was_digested => 1, | ||
| 539 | punct => $punct, assign => $assign); | ||
| 540 | $new->setTuples(@newtuples); | ||
| 541 | return $new; } | ||
| 542 | |||
| 543 | sub revert { | ||
| 544 | my ($self) = @_; | ||
| 545 | |||
| 546 | # read values from class | ||
| 547 | my ($punct, $assign) = ($$self{punct}, $$self{assign}); | ||
| 548 | |||
| 549 | my @tokens = (); | ||
| 550 | |||
| 551 | # iterate over the key-value pairs | ||
| 552 | foreach my $tuple (@{ $$self{tuples} }) { | ||
| 553 | my ($key, $value, $useDefault, $resolution, $keyval) = @$tuple; | ||
| 554 | # revert a single token | ||
| 555 | if ($keyval) { # when is this undef? | ||
| 556 | push(@tokens, $self->revertKeyVal($keyval, $value, $useDefault, (@tokens ? 0 : 1), 0, $punct, $assign)); } } | ||
| 557 | |||
| 558 | # and return the list of tokens | ||
| 559 | return Tokens(@tokens); } | ||
| 560 | |||
| 561 | # turns this object into a string | ||
| 562 | sub toString { | ||
| 563 | my ($self) = @_; | ||
| 564 | |||
| 565 | my @kv = $self->getPairs; | ||
| 566 | my ($punct, $assign) = ($$self{punct} || '', $$self{assign} || ' '); | ||
| 567 | |||
| 568 | my $string = ''; | ||
| 569 | |||
| 570 | while (@kv) { | ||
| 571 | my ($key, $value) = (shift(@kv), shift(@kv)); | ||
| 572 | $string .= ToString($punct) . ' ' if $string; | ||
| 573 | $string .= $key . ToString($assign) . ToString($value); } | ||
| 574 | return $string; } | ||
| 575 | |||
| 576 | sub revertKeyVal { | ||
| 577 | my ($self, $keyval, $value, $useDefault, $isFirst, $compact, $punct, $assign) = @_; | ||
| 578 | |||
| 579 | # get the key-value definition | ||
| 580 | my $keydef = $keyval->getType(); | ||
| 581 | |||
| 582 | # define the tokens | ||
| 583 | my @tokens = (); | ||
| 584 | |||
| 585 | # write comma and key, unless in the first iteration | ||
| 586 | push(@tokens, $punct) if $punct && !$isFirst; | ||
| 587 | push(@tokens, T_SPACE) if !$isFirst && !$compact; | ||
| 588 | push(@tokens, Explode($keyval->getKey)); | ||
| 589 | |||
| 590 | # write the default (if applicable) | ||
| 591 | if (!$useDefault && $value) { | ||
| 592 | push(@tokens, ($assign || T_SPACE)); | ||
| 593 | push(@tokens, ($keydef ? $keydef->revert($value) : Revert($value))); } | ||
| 594 | |||
| 595 | return @tokens; } | ||
| 596 | |||
| 597 | # TODO: ???? | ||
| 598 | sub unlist { | ||
| 599 | my ($self) = @_; | ||
| 600 | return $self; } | ||
| 601 | |||
| 602 | #====================================================================== | ||
| 603 | 1; | ||
| 604 | |||
| 605 | __END__ |