| 1 | package Alvis::Wikipedia::Templates; | ||
| 2 | |||
| 3 | $Alvis::Wikipedia::Templates::VERSION='0.1'; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Storable; | ||
| 9 | use Alvis::Wikipedia::WikitextParser; | ||
| 10 | use Alvis::Wikipedia::Variables; | ||
| 11 | |||
| 12 | ######################################################################## | ||
| 13 | # | ||
| 14 | # Global variables | ||
| 15 | # | ||
| 16 | ######################################################################## | ||
| 17 | |||
| 18 | my $DEBUG=0; | ||
| 19 | |||
| 20 | # | ||
| 21 | # Namespace codes and their meaning | ||
| 22 | # | ||
| 23 | my %NSCodes=(-2=>'Media', | ||
| 24 | -1=>'Special', | ||
| 25 | 0=>'', | ||
| 26 | 1=>'Talk', | ||
| 27 | 2=>'User', | ||
| 28 | 3=>'User talk', | ||
| 29 | 4=>'Wikipedia', | ||
| 30 | 5=>'Wikipedia talk', | ||
| 31 | 6=>'Image', | ||
| 32 | 7=>'Image talk', | ||
| 33 | 8=>'MediaWiki', | ||
| 34 | 9=>'MediaWiki talk', | ||
| 35 | 10=>'Template', | ||
| 36 | 11=>'Template talk', | ||
| 37 | 12=>'Help', | ||
| 38 | 13=>'Help talk', | ||
| 39 | 14=>'Category', | ||
| 40 | 15=>'Category talk', | ||
| 41 | 100=>'Portal', | ||
| 42 | 101=>'Portal talk'); | ||
| 43 | |||
| 44 | # | ||
| 45 | # Variables and their meaning | ||
| 46 | # | ||
| 47 | my %VarSubst=( | ||
| 48 | 'ns:-2'=>'Media', | ||
| 49 | 'ns:Media'=>'Media', | ||
| 50 | 'ns:-1'=>'Special', | ||
| 51 | 'ns:Special'=>'Special', | ||
| 52 | 'ns:1'=>'Talk', | ||
| 53 | 'ns:Talk'=>'Talk', | ||
| 54 | 'ns:2'=>'User', | ||
| 55 | 'ns:User'=>'User', | ||
| 56 | 'ns:3'=>'User_talk', | ||
| 57 | 'ns:User_talk'=>'User_talk', | ||
| 58 | 'ns:4'=>'Wikipedia', | ||
| 59 | 'ns:Project'=>'Wikipedia', | ||
| 60 | 'ns:5'=>'Wikipedia_talk', | ||
| 61 | 'ns:Project_talk'=>'Wikipedia_talk', | ||
| 62 | 'ns:6'=>'Image', | ||
| 63 | 'ns:Image'=>'Image', | ||
| 64 | 'ns:7'=>'Image_talk', | ||
| 65 | 'ns:Image_talk'=>'Image_talk', | ||
| 66 | 'ns:8'=>'MediaWiki', | ||
| 67 | 'ns:MediaWiki'=>'MediaWiki', | ||
| 68 | 'ns:9'=>'MediaWiki_talk', | ||
| 69 | 'ns:MediaWiki_talk'=>'MediaWiki_talk', | ||
| 70 | 'ns:10'=>'Template', | ||
| 71 | 'ns:Template'=>'Template', | ||
| 72 | 'ns:11'=>'Template_talk', | ||
| 73 | 'ns:Template_talk'=>'Template_talk', | ||
| 74 | 'ns:12'=>'Help', | ||
| 75 | 'ns:Help'=>'Help', | ||
| 76 | 'ns:13'=>'Help_talk', | ||
| 77 | 'ns:Help_talk'=>'Help_talk', | ||
| 78 | 'ns:14'=>'Category', | ||
| 79 | 'ns:Category'=>'Category', | ||
| 80 | 'ns:15'=>'Category_talk', | ||
| 81 | 'ns:Category_talk'=>'Category_talk', | ||
| 82 | 'ns:100'=>'Portal', | ||
| 83 | 'ns:101'=>'Portal_talk', | ||
| 84 | 'SITENAME'=>'wikipedia', | ||
| 85 | 'SERVER'=>'http://en.wikipedia.org', | ||
| 86 | 'SERVERNAME'=>'en.wikipedia.org', | ||
| 87 | 'localurl:'=>'/wiki/', | ||
| 88 | 'localurle:'=>'/wiki/', | ||
| 89 | 'fullurl:'=>'http://en.wikipedia.org/wiki/' | ||
| 90 | ); | ||
| 91 | |||
| 92 | ######################################################################### | ||
| 93 | # | ||
| 94 | # Error message stuff | ||
| 95 | # | ||
| 96 | ######################################################################### | ||
| 97 | |||
| 98 | my $ErrStr; | ||
| 99 | my ($ERR_OK, | ||
| 100 | $ERR_PARSER, | ||
| 101 | $ERR_NORM, | ||
| 102 | $ERR_UNK_TEMPL, | ||
| 103 | $ERR_PARAM, | ||
| 104 | $ERR_NO_TEXT, | ||
| 105 | $ERR_NO_TITLE, | ||
| 106 | $ERR_NO_NAMESPACE, | ||
| 107 | $ERR_STORE, | ||
| 108 | $ERR_UNDEF_DUMP, | ||
| 109 | $ERR_RETRIEVE | ||
| 110 | )=(0..10); | ||
| 111 | my %ErrMsgs=($ERR_OK=>"", | ||
| 112 | $ERR_PARSER=>"Unable to instantiate Alvis::Wikipedia::WikitextParser.", | ||
| 113 | $ERR_NORM=>"Title normalization failed.", | ||
| 114 | $ERR_UNK_TEMPL=>"Unrecognized template name.", | ||
| 115 | $ERR_PARAM=>"Application of a parameter pattern failed.", | ||
| 116 | $ERR_NO_TEXT=>"Undefined text to expand", | ||
| 117 | $ERR_NO_TITLE=>"Undefined title to expand", | ||
| 118 | $ERR_NO_NAMESPACE=>"Undefined namespace to expand", | ||
| 119 | $ERR_STORE=>"Storable::store() failed.", | ||
| 120 | $ERR_UNDEF_DUMP=>"Trying to dump when there are no definitions.", | ||
| 121 | $ERR_RETRIEVE=>"Storable::retrieve() failed." | ||
| 122 | ); | ||
| 123 | |||
| 124 | sub _set_err_state | ||
| 125 | { | ||
| 126 | my $self=shift; | ||
| 127 | my $errcode=shift; | ||
| 128 | my $errmsg=shift; | ||
| 129 | |||
| 130 | if (!defined($errcode)) | ||
| 131 | { | ||
| 132 | confess("set_err_state() called with an undefined argument."); | ||
| 133 | } | ||
| 134 | |||
| 135 | if (exists($ErrMsgs{$errcode})) | ||
| 136 | { | ||
| 137 | if ($errcode==$ERR_OK) | ||
| 138 | { | ||
| 139 | $self->{errstr}=""; | ||
| 140 | } | ||
| 141 | else | ||
| 142 | { | ||
| 143 | $self->{errstr}.=" " . $ErrMsgs{$errcode}; | ||
| 144 | if (defined($errmsg)) | ||
| 145 | { | ||
| 146 | $self->{errstr}.=" " . $errmsg; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | else | ||
| 151 | { | ||
| 152 | confess("Internal error: set_err_state() called with an " . | ||
| 153 | "unrecognized argument ($errcode).") | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub errmsg | ||
| 158 | { | ||
| 159 | my $self=shift; | ||
| 160 | |||
| 161 | return $self->{errstr}; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub clearerr | ||
| 165 | { | ||
| 166 | my $self=shift; | ||
| 167 | |||
| 168 | $self->{errstr}=""; | ||
| 169 | } | ||
| 170 | |||
| 171 | ####################################################################### | ||
| 172 | # | ||
| 173 | # Public methods | ||
| 174 | # | ||
| 175 | ####################################################################### | ||
| 176 | |||
| 177 | sub new | ||
| 178 | { | ||
| 179 | my $proto=shift; | ||
| 180 | |||
| 181 | my $class=ref($proto)||$proto; | ||
| 182 | my $parent=ref($proto)&&$proto; | ||
| 183 | my $self={}; | ||
| 184 | bless($self,$class); | ||
| 185 | |||
| 186 | $self->_init(@_); | ||
| 187 | |||
| 188 | $self->{parser}=Alvis::Wikipedia::WikitextParser->new(); | ||
| 189 | if (!defined($self->{parser})) | ||
| 190 | { | ||
| 191 | $self->_set_err_state($ERR_PARSER); | ||
| 192 | return undef; | ||
| 193 | } | ||
| 194 | |||
| 195 | return $self; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub _init | ||
| 199 | { | ||
| 200 | my $self=shift; | ||
| 201 | |||
| 202 | $self->{maxExpandedTextSize}=1000000; | ||
| 203 | $self->{maxNofExpansions}=10000; | ||
| 204 | |||
| 205 | if (defined(@_)) | ||
| 206 | { | ||
| 207 | my %args=@_; | ||
| 208 | @$self{ keys %args }=values(%args); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | sub dump | ||
| 213 | { | ||
| 214 | my $self=shift; | ||
| 215 | my $f=shift; | ||
| 216 | |||
| 217 | if (!defined($self->{defs})) | ||
| 218 | { | ||
| 219 | $self->_set_err_state($ERR_UNDEF_DUMP); | ||
| 220 | return 0; | ||
| 221 | } | ||
| 222 | |||
| 223 | my %defs=%{$self->{defs}}; | ||
| 224 | if (store(\%defs,$f)) | ||
| 225 | { | ||
| 226 | return 1; | ||
| 227 | } | ||
| 228 | else | ||
| 229 | { | ||
| 230 | $self->_set_err_state($ERR_STORE,"File:\"$f\"."); | ||
| 231 | return 0; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | sub load | ||
| 236 | { | ||
| 237 | my $self=shift; | ||
| 238 | my $f=shift; | ||
| 239 | |||
| 240 | my $defs=retrieve($f); | ||
| 241 | if (!defined($defs)) | ||
| 242 | { | ||
| 243 | $self->_set_err_state($ERR_RETRIEVE,"File:\"$f\"."); | ||
| 244 | return 0; | ||
| 245 | } | ||
| 246 | my %defs=%$defs; | ||
| 247 | $self->{defs}=\%defs; | ||
| 248 | |||
| 249 | return 1; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub add | ||
| 253 | { | ||
| 254 | my $self=shift; | ||
| 255 | my $name=shift; | ||
| 256 | my $def=shift; | ||
| 257 | |||
| 258 | my $norm_name=$self->{parser}->normalize_title($name); | ||
| 259 | if (!defined($norm_name)) | ||
| 260 | { | ||
| 261 | $self->_set_err_state($ERR_NORM,"Name:\"$name\"."); | ||
| 262 | return 0; | ||
| 263 | } | ||
| 264 | |||
| 265 | $def=~s/<noinclude>.*?<\/noinclude>//sgo; | ||
| 266 | $def=~s/<\/?includeonly>//sgo; | ||
| 267 | |||
| 268 | $self->{defs}{$norm_name}=$def; | ||
| 269 | |||
| 270 | return 1; | ||
| 271 | } | ||
| 272 | |||
| 273 | # | ||
| 274 | # expand_for_real: do we try to expand the templates for real | ||
| 275 | # (messy and error-prone) or do we simply replace | ||
| 276 | # with a list of the parameter values? | ||
| 277 | # | ||
| 278 | sub expand | ||
| 279 | { | ||
| 280 | my $self=shift; | ||
| 281 | my $namespace=shift; | ||
| 282 | my $title=shift; | ||
| 283 | my $text=shift; | ||
| 284 | my $expand_for_real=shift; | ||
| 285 | |||
| 286 | if (!defined($namespace)) | ||
| 287 | { | ||
| 288 | $self->_set_err_state($ERR_NO_NAMESPACE); | ||
| 289 | return undef; | ||
| 290 | } | ||
| 291 | if (!defined($title)) | ||
| 292 | { | ||
| 293 | $self->_set_err_state($ERR_NO_TITLE); | ||
| 294 | return undef; | ||
| 295 | } | ||
| 296 | if (!defined($text)) | ||
| 297 | { | ||
| 298 | $self->_set_err_state($ERR_NO_TEXT); | ||
| 299 | return undef; | ||
| 300 | } | ||
| 301 | |||
| 302 | $self->{currNamespace}=$namespace; | ||
| 303 | $self->{currTitle}=$title; | ||
| 304 | $self->{nofExpansions}=0; | ||
| 305 | |||
| 306 | warn "TRANSCLUDING...\n" if $DEBUG; | ||
| 307 | my $expanded_text=$self->_transclude($text,$expand_for_real); | ||
| 308 | warn "DONE TRANSCLUDING\n" if $DEBUG; | ||
| 309 | |||
| 310 | return $expanded_text; | ||
| 311 | } | ||
| 312 | |||
| 313 | # | ||
| 314 | # expand_for_real: do we try to expand the templates for real | ||
| 315 | # (messy and error-prone) or do we simply replace | ||
| 316 | # with a list of the parameter values? | ||
| 317 | # | ||
| 318 | sub _transclude | ||
| 319 | { | ||
| 320 | my $self=shift; | ||
| 321 | my $text=shift; | ||
| 322 | my $expand_for_real=shift; | ||
| 323 | |||
| 324 | $self->{higherLevelExpandedNames}={}; | ||
| 325 | while ($text=~/(([^\{])?\{\{([ %!\"\$\&\'\(\)\*,\-\.\/0-9:;=\?\@A-Z\\\^_\`a-z\~\x80-\xFF\n]*)(\|.*?)?\}\})/sgo) | ||
| 326 | { | ||
| 327 | $self->{thisLevelExpandedNames}={}; | ||
| 328 | # Safeguard against malevolent templates | ||
| 329 | if (length($text)>$self->{maxExpandedTextSize} || | ||
| 330 | $self->{nofExpansions}>$self->{maxNofExpansions}) | ||
| 331 | { | ||
| 332 | warn "Excessive expansion stopped for \"$self->{currNamespace}:$self->{currTitle}\"" . | ||
| 333 | ". Length of the text to expand: " . | ||
| 334 | length($text) . ", # of expansions: " . $self->{nofExpansions}; | ||
| 335 | return $text; | ||
| 336 | } | ||
| 337 | |||
| 338 | warn "BEFORE VARIABLE SUBSITUTION\n" if $DEBUG; | ||
| 339 | # Variable substitution | ||
| 340 | $text=~s/(\{\{([ %!\"\$\&\'\(\)\*,\-\.\/0-9:;=\?\@A-Z\\\^_\`a-z\~\x80-\xFF\n]*?)\}\})/$self->_substitute_variable($1,$2)/sgeo; | ||
| 341 | warn "TEXT AFTER VARIABLE SUBSTITUTION:$text\n" if $DEBUG; | ||
| 342 | |||
| 343 | # Template substitution | ||
| 344 | $text=~s/(([^\{])?\{\{([ %!\"\$\&\'\(\)\*,\-\.\/0-9:;=\?\@A-Z\\\^_\`a-z\~\x80-\xFF\n]*)(\|[^\{]*?)?\}\})/$self->_substitute_template($1,$2,$3,$4,$expand_for_real)/sgeo; | ||
| 345 | |||
| 346 | warn "TEXT AFTER TEMPLATE SUBSTITUTION:$text\n" if $DEBUG; | ||
| 347 | |||
| 348 | for my $name (keys %{$self->{thisLevelExpandedNames}}) | ||
| 349 | { | ||
| 350 | $self->{higherLevelExpandedNames}->{$name}=1; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | return $text; | ||
| 355 | |||
| 356 | } | ||
| 357 | |||
| 358 | sub _substitute_variable | ||
| 359 | { | ||
| 360 | my $self=shift; | ||
| 361 | my $text=shift; | ||
| 362 | my $var_name=shift; | ||
| 363 | |||
| 364 | if ($var_name=~/^(localurle?|fullurl):(.*)$/isgo) | ||
| 365 | { | ||
| 366 | my ($var,$rest)=($1,$2); | ||
| 367 | if ($rest) | ||
| 368 | { | ||
| 369 | return $VarSubst{$var} . $rest; | ||
| 370 | } | ||
| 371 | else | ||
| 372 | { | ||
| 373 | return $VarSubst{$var}; | ||
| 374 | } | ||
| 375 | } | ||
| 376 | elsif (exists($VarSubst{$var_name})) | ||
| 377 | { | ||
| 378 | return $VarSubst{$var_name}; | ||
| 379 | } | ||
| 380 | elsif ($var_name=~/^(subst|int):/isgo) | ||
| 381 | { | ||
| 382 | $var_name=~s/^(subst|int)://isgo; | ||
| 383 | } | ||
| 384 | elsif ($var_name=~/^(FULL)?PAGENAMEE?$/) | ||
| 385 | { | ||
| 386 | return $self->{currTitle}; | ||
| 387 | } | ||
| 388 | elsif ($var_name=~/^NAMESPACEE?$/) | ||
| 389 | { | ||
| 390 | return $self->{currNamespace}; | ||
| 391 | } | ||
| 392 | elsif ($var_name=~/^(__NOTOC__|__FORCETOC__|__TOC__|__NOEDITSECTION__|__START__|CURRENT(MONTH|MONTHNAME|MONTHNAMEGEN|MONTHABBREV|DAY|DAYNAME|YEAR|TIME)|NUMBEROFARTICLES|NUMBEROFFILES|PAGENAMEE|NAMESPACE|__END__|thumbnail|thumb|right|left|none|center|centre|framed|enframed|frame|SITENAME|SERVER|SERVERNAME|SCRIPTPATH|__NOTITLECONVERT__|__NOTC__|__NOCONTENTCONVERT__|__NOCC__|CURRENTWEEK|CURRENTDOWREVISIONID)$/go) | ||
| 393 | { | ||
| 394 | return "$var_name"; | ||
| 395 | } | ||
| 396 | else | ||
| 397 | { | ||
| 398 | return $text; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _substitute_template | ||
| 403 | { | ||
| 404 | my $self=shift; | ||
| 405 | my $orig_text=shift; | ||
| 406 | my $pre_context=shift; | ||
| 407 | my $name=shift; | ||
| 408 | my $params=shift; | ||
| 409 | my $expand_for_real=shift; | ||
| 410 | |||
| 411 | my $found=0; | ||
| 412 | |||
| 413 | my $expanded_text; | ||
| 414 | my %arg_assignments=(); | ||
| 415 | |||
| 416 | $name=$self->{parser}->normalize_title($name); | ||
| 417 | |||
| 418 | warn "substitute_template():" if $DEBUG; | ||
| 419 | warn "PRE:\"$pre_context\"\n" if $DEBUG; | ||
| 420 | warn "NAME:\"$name\"\n" if $DEBUG; | ||
| 421 | warn "PARAMS:\"$params\"\n" if $DEBUG; | ||
| 422 | |||
| 423 | # Don't parse {{{}}} because that's only for template arguments | ||
| 424 | if (defined($pre_context) && $pre_context eq '{') | ||
| 425 | { | ||
| 426 | warn "{ PRE-CONTEXT\n" if $DEBUG; | ||
| 427 | return $orig_text; | ||
| 428 | } | ||
| 429 | |||
| 430 | # Ok, now expand if it's a template | ||
| 431 | |||
| 432 | # Do we know this template or don't we care anyway? | ||
| 433 | if (($name && exists($self->{defs}{$name})) || !$expand_for_real) | ||
| 434 | { | ||
| 435 | warn "TEMPLATE $name FOUND\n" if $DEBUG; | ||
| 436 | |||
| 437 | $found=1; | ||
| 438 | |||
| 439 | if (defined($pre_context)) | ||
| 440 | { | ||
| 441 | $expanded_text=$pre_context; | ||
| 442 | } | ||
| 443 | |||
| 444 | # | ||
| 445 | # Not recommended atm .. the bloody syntax seems to keep | ||
| 446 | # on changing with each new server alpha version | ||
| 447 | # | ||
| 448 | if ($expand_for_real) | ||
| 449 | { | ||
| 450 | $expanded_text.=$self->{defs}{$name}; | ||
| 451 | warn "TEXT AFTER ADDING EXPANSION:$expanded_text\n" if $DEBUG; | ||
| 452 | |||
| 453 | if (defined($params)) | ||
| 454 | { | ||
| 455 | # Collect the parameter assignments | ||
| 456 | my @actual_args=$self->_get_template_call_args($params); | ||
| 457 | my $index=1; | ||
| 458 | for my $arg (@actual_args) | ||
| 459 | { | ||
| 460 | my $eq_pos=index($arg,'='); | ||
| 461 | if ($eq_pos<0) | ||
| 462 | { | ||
| 463 | warn "Adding actual arg \'$index\', value \'$arg\'\n" if $DEBUG; | ||
| 464 | $arg_assignments{$index++}=$arg; | ||
| 465 | } | ||
| 466 | else | ||
| 467 | { | ||
| 468 | $name=substr($arg,0,$eq_pos); | ||
| 469 | $name=~s/^\s+//; | ||
| 470 | $name=~s/\s+$//; | ||
| 471 | my $value=substr($arg,$eq_pos+1); | ||
| 472 | $value=~s/^\s+//; | ||
| 473 | $value=~s/\s+$//; | ||
| 474 | |||
| 475 | warn "Adding actual arg \'$name\', value \'$value\'\n" if $DEBUG; | ||
| 476 | $arg_assignments{$name}=$value; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | |||
| 481 | # Keep track of expanded names | ||
| 482 | $self->{thisLevelExpandedNames}{$name}=1; | ||
| 483 | |||
| 484 | # Substitute actual parameter values | ||
| 485 | while ($expanded_text=~/(\{\{\{([ %!\"\$\&\'\(\)\*,\-\.\/0-9:;=\?\@A-Z\\\^_\`a-z\~\x80-\xFF\n]*?)(\|[^\{]*?)?\}\}\})/sgo) | ||
| 486 | { | ||
| 487 | $expanded_text=~s/(\{\{\{([ %!\"\$\&\'\(\)\*,\-\.\/0-9:;=\?\@A-Z\\\^_\`a-z\~\x80-\xFF\n]*?)(\|[^\{]*?)?\}\}\})/$self->_substitute_param_value($1,$2,$3,\%arg_assignments)/sgeo; | ||
| 488 | warn "TEXT AFTER PARAMETER VALUE SUBSTITUTION:$expanded_text\n" if $DEBUG; | ||
| 489 | } | ||
| 490 | |||
| 491 | # If the template begins with a table or block-level | ||
| 492 | # element, it should be treated as beginning a new line. | ||
| 493 | if (defined($pre_context) && $pre_context!~/\n/ && $expanded_text=~/^(\{\||:|;|\#|\*)/) | ||
| 494 | { | ||
| 495 | warn "ADDING NEWLINE PRE-CONTEXT\n" if $DEBUG; | ||
| 496 | $expanded_text="\n" . $expanded_text; | ||
| 497 | } | ||
| 498 | # remove comments | ||
| 499 | $expanded_text=~s/<!--.*?-->//isgo; | ||
| 500 | |||
| 501 | } | ||
| 502 | else # play it safe -- shouldn't matter much for search engine | ||
| 503 | # purposes | ||
| 504 | { | ||
| 505 | if (defined($params)) | ||
| 506 | { | ||
| 507 | # Collect the parameter assignments | ||
| 508 | my @actual_args=$self->_get_template_call_args($params); | ||
| 509 | my $index=1; | ||
| 510 | for my $arg (@actual_args) | ||
| 511 | { | ||
| 512 | my $eq_pos=index($arg,'='); | ||
| 513 | if ($eq_pos<0) | ||
| 514 | { | ||
| 515 | warn "Adding actual arg \'$index\', value \'$arg\'\n" if $DEBUG; | ||
| 516 | $arg_assignments{$index++}=$arg; | ||
| 517 | } | ||
| 518 | else | ||
| 519 | { | ||
| 520 | $name=substr($arg,0,$eq_pos); | ||
| 521 | $name=~s/^\s+//; | ||
| 522 | $name=~s/\s+$//; | ||
| 523 | my $value=substr($arg,$eq_pos+1); | ||
| 524 | $value=~s/^\s+//; | ||
| 525 | $value=~s/\s+$//; | ||
| 526 | |||
| 527 | warn "Adding actual arg \'$name\', value \'$value\'\n" if $DEBUG; | ||
| 528 | $arg_assignments{$name}=$value; | ||
| 529 | } | ||
| 530 | } | ||
| 531 | } | ||
| 532 | # | ||
| 533 | # Simply insert the parameter values as a list | ||
| 534 | # | ||
| 535 | $expanded_text.="\n"; | ||
| 536 | for my $p (keys %arg_assignments) | ||
| 537 | { | ||
| 538 | $expanded_text.="*$arg_assignments{$p}\n"; | ||
| 539 | } | ||
| 540 | # If the template begins with a table or block-level | ||
| 541 | # element, it should be treated as beginning a new line. | ||
| 542 | if (defined($pre_context) && $pre_context!~/\n/ && $expanded_text=~/^(\{\||:|;|\#|\*)/) | ||
| 543 | { | ||
| 544 | warn "ADDING NEWLINE PRE-CONTEXT\n" if $DEBUG; | ||
| 545 | $expanded_text="\n" . $expanded_text; | ||
| 546 | } | ||
| 547 | # remove comments | ||
| 548 | $expanded_text=~s/<!--.*?-->//isgo if defined($expanded_text); | ||
| 549 | $expanded_text.="\n"; | ||
| 550 | $expanded_text.="----\n"; # to cause a logical section break | ||
| 551 | |||
| 552 | return $expanded_text; | ||
| 553 | } | ||
| 554 | } | ||
| 555 | |||
| 556 | if (!$found) | ||
| 557 | { | ||
| 558 | warn "AT END. NOT FOUND\n" if $DEBUG; | ||
| 559 | # | ||
| 560 | # Have to safeguard against retrying this | ||
| 561 | # | ||
| 562 | return $pre_context . "UNKNOWN_TEMPLATE_$name" if $DEBUG; | ||
| 563 | } | ||
| 564 | else | ||
| 565 | { | ||
| 566 | $self->{nofExpansions}++; | ||
| 567 | |||
| 568 | warn "AT END. FOUND.\n" if $DEBUG; | ||
| 569 | return $expanded_text; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | # | ||
| 574 | # Triple brace replacement -- used for template arguments | ||
| 575 | # | ||
| 576 | sub _substitute_param_value | ||
| 577 | { | ||
| 578 | my $self=shift; | ||
| 579 | my $orig_text=shift; | ||
| 580 | my $param_name=shift; | ||
| 581 | my $default=shift; | ||
| 582 | my $arg_assignments=shift; | ||
| 583 | |||
| 584 | $param_name=~s/^\s+//go; | ||
| 585 | $param_name=~s/\s+$//go; | ||
| 586 | if (!defined($default)) | ||
| 587 | { | ||
| 588 | $default=""; | ||
| 589 | } | ||
| 590 | else | ||
| 591 | { | ||
| 592 | $default=substr($default,1); # lose the | | ||
| 593 | } | ||
| 594 | |||
| 595 | warn "PARAM VALUE SUBST ENTRY.\n" if $DEBUG; | ||
| 596 | warn "ORIG TEXT:$orig_text\n" if $DEBUG; | ||
| 597 | warn "PARAM:$param_name\n" if $DEBUG; | ||
| 598 | warn "DEFAULT:$default\n" if $DEBUG; | ||
| 599 | warn "ARG ASSIGNMENTS:\n" if $DEBUG; | ||
| 600 | for my $name (keys(%$arg_assignments)) | ||
| 601 | { | ||
| 602 | warn "\t$name -> $arg_assignments->{$name}\n" if $DEBUG; | ||
| 603 | } | ||
| 604 | |||
| 605 | my $subst; | ||
| 606 | |||
| 607 | if (exists($arg_assignments->{$param_name})) | ||
| 608 | { | ||
| 609 | warn "PARAM HAS BEEN ASSIGNED TO\n" if $DEBUG; | ||
| 610 | $subst=$arg_assignments->{$param_name}; | ||
| 611 | } | ||
| 612 | else | ||
| 613 | { | ||
| 614 | warn "USING DEFAULT\n" if $DEBUG; | ||
| 615 | $subst=$default; | ||
| 616 | } | ||
| 617 | warn "SUBSTITUTE:\"$orig_text\"->\"$subst\"\n" if $DEBUG; | ||
| 618 | |||
| 619 | return $subst; | ||
| 620 | } | ||
| 621 | |||
| 622 | # | ||
| 623 | # Get the actual call argument values. Watch out for bl***y piped links. | ||
| 624 | # | ||
| 625 | sub _get_template_call_args | ||
| 626 | { | ||
| 627 | my $self=shift; | ||
| 628 | my $args_str=shift; | ||
| 629 | |||
| 630 | if ($args_str eq "") | ||
| 631 | { | ||
| 632 | return (); | ||
| 633 | } | ||
| 634 | |||
| 635 | # The first char is '|' | ||
| 636 | my @args=split(/\|/,substr($args_str,1)); | ||
| 637 | |||
| 638 | # If any of the arguments contains a '[[' but no ']]', it needs to be | ||
| 639 | # merged with the next arg because the '|' character between belongs | ||
| 640 | # to the link syntax and not the template parameter syntax. | ||
| 641 | my $argc=scalar(@args); | ||
| 642 | for (my $i=0;$i<$argc-1;$i++) | ||
| 643 | { | ||
| 644 | if ($self->_substr_count($args[$i],'[[') != | ||
| 645 | $self->_substr_count($args[$i],']]')) | ||
| 646 | { | ||
| 647 | $args[$i].='|' . $args[$i+1]; | ||
| 648 | splice(@args,$i+1,1); | ||
| 649 | $i--; | ||
| 650 | $argc--; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | return @args; | ||
| 655 | } | ||
| 656 | |||
| 657 | sub _substr_count | ||
| 658 | { | ||
| 659 | my $self=shift; | ||
| 660 | my $string=shift; | ||
| 661 | my $substr=shift; | ||
| 662 | |||
| 663 | my $pos=0; | ||
| 664 | my $N=0; | ||
| 665 | my $match_pos=index($string,$substr,$pos); | ||
| 666 | while ($match_pos>=$pos) | ||
| 667 | { | ||
| 668 | $pos=$match_pos+1; | ||
| 669 | $match_pos=index($string,$substr,$pos); | ||
| 670 | $N++; | ||
| 671 | } | ||
| 672 | |||
| 673 | return $N; | ||
| 674 | } | ||
| 675 | |||
| 676 | |||
| 677 | |||
| 678 | 1; |