/usr/share/perl5/LaTeXML/Util/KeyVal.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 | # -*- CPERL -*-
# /=====================================================================\ #
# | KeyVal | #
# | Support for key-value pairs for LaTeXML | #
# |=====================================================================| #
# | 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::Util::KeyVal;
use strict;
use LaTeXML::Package;
our @ISA = qw(Exporter);
our @EXPORT= (qw(&ReadRequiredKeyVals &ReadOptionalKeyVals
&DefKeyVal
&KeyVal &KeyVals));
#======================================================================
# New Readers for required and optional KeyVal sets.
# These can also be used as parameter types.
# They create a new data KeyVals object
sub ReadRequiredKeyVals {
my($gullet,$keyset)=@_;
if($gullet->ifNext(T_BEGIN)){
(readKeyVals($gullet,$keyset,T_END)); }
else {
Error(":expected:{ Missing keyval arguments");
(LaTeXML::KeyVals->new($keyset,T_BEGIN,T_END,)); }}
sub ReadOptionalKeyVals {
my($gullet,$keyset)=@_;
($gullet->ifNext(T_OTHER('[')) ? (readKeyVals($gullet,$keyset,T_OTHER(']'))) : undef); }
#======================================================================
# This new declaration allows you to define the type associated with
# the value for specific keys.
sub DefKeyVal {
my($keyset,$key,$type,$default)=@_;
my $paramlist=LaTeXML::Package::parseParameters($type,"KeyVal $key in set $keyset");
AssignValue('KEYVAL@'.$keyset.'@'.$key => $paramlist);
AssignValue('KEYVAL@'.$keyset.'@'.$key.'@default' => Tokenize($default))
if defined $default;
return; }
#======================================================================
# These functions allow convenient access to KeyVal objects within constructors.
# Access the value associated with a given key.
# Can use in constructor: eg. <foo attrib='&KeyVal(#1,'key')'>
sub KeyVal {
my($keyval,$key)=@_;
(defined $keyval) && $keyval->getValue($key); }
# Access the entire hash.
# Can use in constructor: <foo %&KeyVals(#1)/>
sub KeyVals {
my($keyval)=@_;
(defined $keyval ? $keyval->getKeyVals : {}); }
#======================================================================
# A KeyVal argument MUST be delimited by either braces or brackets (if optional)
# This method reads the keyval pairs INCLUDING the delimiters, (rather than parsing
# after the fact), since some values may have special catcode needs.
our $T_EQ = T_OTHER('=');
our $T_COMMA = T_OTHER(',');
sub readKeyVals {
my($gullet,$keyset,$close)=@_;
my $startloc = $gullet->getLocator();
my $open = $gullet->readToken;
$keyset = ($keyset ? ToString($keyset) : '_anonymous_');
my @kv=();
while(1) {
$gullet->skipSpaces;
# Read the keyword.
my($ktoks,$delim)=$gullet->readUntil($T_EQ,$T_COMMA,$close);
Error(":expected:".Stringify($close)." Fell off end expecting ".Stringify($close)." while reading KeyVal key starting at $startloc")
unless $delim;
my $key= ToString($ktoks); $key=~s/\s//g;
if($key){
my $keydef=LookupValue('KEYVAL@'.$keyset.'@'.$key);
my $value;
if($delim->equals($T_EQ)){ # Got =, so read the value
# WHOA!!! Secret knowledge!!!
my $type = ($keydef && (scalar(@$keydef)==1) && $keydef->[0]->{type}) || 'Plain';
my $typedef = $LaTeXML::Parameters::PARAMETER_TABLE{$type};
StartSemiverbatim if $typedef && $$typedef{semiverbatim};
($value,$delim)=$gullet->readUntil($T_COMMA,$close);
if(($type eq 'Plain') || ($typedef && $$typedef{undigested})){} # Fine as is.
elsif($type eq 'Semiverbatim'){ # Needs neutralization
$value = $value->neutralize; }
else {
($value) = $keydef->reparseArgument($gullet,$value) }
EndSemiverbatim if $typedef && $$typedef{semiverbatim};
}
else { # Else, get default value.
$value = LookupValue('KEYVAL@'.$keyset.'@'.$key.'@default'); }
push(@kv,$key);
push(@kv,$value); }
Error(":expected:".Stringify($close)." Fell off end expecting ".Stringify($close)." while reading KeyVal value starting at $startloc")
unless $delim;
last if $delim->equals($close); }
LaTeXML::KeyVals->new($keyset,$open,$close,@kv); }
#**********************************************************************
# This defines the KeyVal data object that can appear in the datastream
# along with tokens, boxes, etc.
# Thus it has to be digestible.
# KeyVals: representation of keyval arguments,
# Not necessarily a hash, since keys could be repeated and order may
# be significant.
#**********************************************************************
# Where does this really belong?
# The values can be Tokens, after parsing, or Boxes, after digestion.
# (or Numbers, etc. in either case)
# But also, it has a non-generic API used above...
# If Box-like, it could have a beAbsorbed method; which would do what?
# Should it convert to simple text? Or structure?
# If latter, there needs to be a key => tag mapping.
package LaTeXML::KeyVals;
use LaTeXML::Global;
use LaTeXML::Package;
use base qw(LaTeXML::Object);
# Spec??
sub new {
my($class,$keyset,$open,$close,@pairs)=@_;
my %hash = ();
my @pp=@pairs;
while(@pp){
my($k,$v) = (shift(@pp),shift(@pp));
if(!defined $hash{$k}){ $hash{$k}=$v; }
# Hmm, accumulate an ARRAY if multiple values for given key.
# This is unlikely to be what the caller expects!! But what?
elsif(ref $hash{$k} eq 'ARRAY'){ push(@{$hash{$k}},$v); }
else { $hash{$k}=[$hash{$k},$v]; }}
bless {keyset=>$keyset, open=>$open, close=>$close, keyvals=>[@pairs], hash=>{%hash}},$class; }
sub getValue {
my($self,$key)=@_;
$$self{hash}{$key}; }
sub setValue {
my($self,$key,$value)=@_;
if(defined $value){
$$self{hash}{$key}=$value; }
else {
delete $$self{hash}{$key}; }}
sub getPairs {
my($self)=@_;
@{$$self{keyvals}}; }
sub getKeyVals {
my($self)=@_;
$$self{hash}; }
sub getHash {
my($self)=@_;
map( ($_ => ToString($$self{hash}{$_})), keys %{$$self{hash}}); }
sub beDigested {
my($self,$stomach)=@_;
my $keyset = $$self{keyset};
my @kv=@{$$self{keyvals}};
my @dkv=();
while(@kv){
my($key,$value)=(shift(@kv),shift(@kv));
my $keydef=LookupValue('KEYVAL@'.$keyset.'@'.$key);
my $dodigest = (ref $value) && (!$keydef || !$$keydef[0]{undigested});
push(@dkv,$key, ($dodigest ? $value->beDigested($stomach) : $value)); }
(ref $self)->new($$self{keyset},$$self{open},$$self{close},@dkv); }
sub revert {
my($self)=@_;
my $keyset = $$self{keyset};
my @tokens=();
my @kv=@{$$self{keyvals}};
while(@kv){
my($key,$value)=(shift(@kv),shift(@kv));
my $keydef=LookupValue('KEYVAL@'.$keyset.'@'.$key);
push(@tokens,T_OTHER(','),T_SPACE) if @tokens;
push(@tokens,Explode($key));
push(@tokens,T_OTHER('='),
($keydef ? $keydef->revertArguments($value) : $value->revert)) if $value; }
unshift(@tokens,$$self{open} ) if $$self{open};
push( @tokens,$$self{close}) if $$self{close};
@tokens; }
sub unlist { $_[0]; } # ????
sub toString {
my($self)=@_;
my $string='';
my @kv=@{$$self{keyvals}};
while(@kv){
my($key,$value)=(shift(@kv),shift(@kv));
$string .= ', ' if $string;
$string .= $key.'='.ToString($value); }
$string; }
#======================================================================
1;
__END__
=pod
=head1 NAME
C<LaTeXML::Util::KeyVal> - support for keyvals
=head1 DESCRIPTION
Provides a parser and representation of keyval pairs
C<LaTeXML::KeyVal> represents parameters handled by LaTeX's keyval package.
=head2 Declarations
=over 4
=item C<< DefKeyVal($keyset,$key,$type); >>
Defines the type of value expected for the key $key when parsed in part
of a KeyVal using C<$keyset>. C<$type> would be something like 'any' or 'Number', but
I'm still working on this.
=back
=head2 Accessors
=over 4
=item C<< KeyVal($arg,$key) >>
This is useful within constructors to access the value associated with C<$key> in
the argument C<$arg>.
=item C<< KeyVals($arg) >>
This is useful within constructors to extract all keyvalue pairs to assign all attributes.
=back
=head2 KeyVal Methods
=over 4
=item C<< $value = $keyval->getValue($key); >>
Return the value associated with C<$key> in the C<$keyval>.
=item C<< @keyvals = $keyval->getKeyVals; >>
Return the hash reference containing the keys and values bound in the C<$keyval>.
Note that will only contain the last value for a given key, if they
were repeated.
=item C<< @keyvals = $keyval->getPairs; >>
Return the alternating keys and values bound in the C<$keyval>.
Note that this may contain multiple entries for a given key, if they
were repeated.
=item C<< $keyval->digestValues; >>
Return a new C<LaTeXML::KeyVals> object with all values digested as appropriate.
=back
=head1 AUTHOR
Bruce Miller <bruce.miller@nist.gov>
=head1 COPYRIGHT
Public domain software, produced as part of work done by the
United States Government & not subject to copyright in the US.
=cut
|