| 1 |
|
| 2 |
################################################################################### |
| 3 |
# |
| 4 |
# Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de |
| 5 |
# Embperl - Copyright (c) 2008-2014 Gerald Richter |
| 6 |
# |
| 7 |
# You may distribute under the terms of either the GNU General Public |
| 8 |
# License or the Artistic License, as specified in the Perl README file. |
| 9 |
# |
| 10 |
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 11 |
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 12 |
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
# |
| 14 |
# $Id: RTF.pm 1578075 2014-03-16 14:01:14Z richter $ |
| 15 |
# |
| 16 |
################################################################################### |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package Embperl::Syntax::RTF ; |
| 21 |
|
| 22 |
use Embperl::Syntax (':types') ; |
| 23 |
use Embperl::Syntax::EmbperlBlocks ; |
| 24 |
|
| 25 |
use strict ; |
| 26 |
use vars qw{@ISA %RTF %Para %ParaBlockInside %Block %BlockInside %FieldStart %CmdStart %Var %Spaces %Inside} ; |
| 27 |
|
| 28 |
require Text::ParseWords ; |
| 29 |
|
| 30 |
@ISA = qw(Embperl::Syntax::EmbperlBlocks) ; |
| 31 |
|
| 32 |
|
| 33 |
################################################################################### |
| 34 |
# |
| 35 |
# Methods |
| 36 |
# |
| 37 |
################################################################################### |
| 38 |
|
| 39 |
# --------------------------------------------------------------------------------- |
| 40 |
# |
| 41 |
# Create new Syntax Object |
| 42 |
# |
| 43 |
# --------------------------------------------------------------------------------- |
| 44 |
|
| 45 |
sub new |
| 46 |
|
| 47 |
{ |
| 48 |
my $self = shift ; |
| 49 |
|
| 50 |
$self = Embperl::Syntax::EmbperlBlocks::new ($self, { 'unescape' => 17 }) ; |
| 51 |
|
| 52 |
if (!$self -> {-rtfBlocks}) |
| 53 |
{ |
| 54 |
my $eb = $self -> {-epbBlocks} ; |
| 55 |
my $k ; |
| 56 |
my $v ; |
| 57 |
my $ebesc = $self -> CloneHash ($eb, { 'unescape' => 17 }) ; |
| 58 |
|
| 59 |
$self -> {-root} = $self -> CloneHash ($self -> {-root}, { 'unescape' => 17 }) ; |
| 60 |
|
| 61 |
while (($k, $v) = each %$ebesc) |
| 62 |
{ |
| 63 |
$Block{$k} = $v ; |
| 64 |
} |
| 65 |
|
| 66 |
$self -> {-rtfBlocks} = $self -> CloneHash (\%RTF) ; |
| 67 |
|
| 68 |
$self -> AddToRoot ($self -> {-rtfBlocks}) ; |
| 69 |
|
| 70 |
# $self -> {-rtfCmds} = $self -> {-rtfBlocks}{'RTF mainblock'}{'inside'}{'RTF first paragraph'}{'inside'}{'RTF field'}{'inside'}{'RTF fieldstart'}{'inside'}{'RTF block cmd'}{'inside'} ; |
| 71 |
# $self -> {-rtfCmds2} = $self -> {-rtfBlocks}{'RTF mainblock'}{'inside'}{'RTF first paragraph'}{'inside'}{'RTF field'}{'inside'}{'RTF fieldstart'}{'inside'} ; |
| 72 |
$self -> {-rtfCmds} = $self -> {-rtfBlocks}{'RTF mainblock'}{'inside'}{'RTF field'}{'inside'}{'RTF fieldstart'}{'inside'}{'RTF block cmd'}{'inside'} ; |
| 73 |
$self -> {-rtfCmds2} = $self -> {-rtfBlocks}{'RTF mainblock'}{'inside'}{'RTF field'}{'inside'}{'RTF fieldstart'}{'inside'} ; |
| 74 |
Init ($self) ; |
| 75 |
} |
| 76 |
|
| 77 |
return $self ; |
| 78 |
} |
| 79 |
|
| 80 |
# --------------------------------------------------------------------------------- |
| 81 |
# |
| 82 |
# Add new rtf command |
| 83 |
# |
| 84 |
# --------------------------------------------------------------------------------- |
| 85 |
|
| 86 |
|
| 87 |
sub AddRTFCmd |
| 88 |
|
| 89 |
{ |
| 90 |
my ($self, $cmdname, $procinfo, $taginfo, $procinfoinside) = @_ ; |
| 91 |
|
| 92 |
my $ttfollow = $self -> {-rtfCmds} ; |
| 93 |
|
| 94 |
my $tag = $ttfollow -> {$cmdname} = { |
| 95 |
'text' => $cmdname, |
| 96 |
'nodetype' => ntypStartEndTag, |
| 97 |
#'nodetype' => ntypTag, |
| 98 |
#'cdatatype' => ntypAttrValue, |
| 99 |
'forcetype' => 1, |
| 100 |
'unescape' => 1, |
| 101 |
'removespaces' => 16, |
| 102 |
(ref($taginfo) eq 'HASH'?%$taginfo:()), |
| 103 |
} ; |
| 104 |
if ($procinfo) |
| 105 |
{ |
| 106 |
#$procinfo -> {compiletimeperlcode} = q[my $tmp = %#'0% ; $tmp =~ s/_ep_rp\(.*?\,/push \@_ep_rtf_tmp,(/, $Embperl::req -> component -> code ($tmp) ; ] ; |
| 107 |
$tag -> {'procinfo'} = { $self -> {-procinfotype} => $procinfo } ; |
| 108 |
} |
| 109 |
$self -> {-rtfCmds2} -> {$cmdname} = $tag ; |
| 110 |
|
| 111 |
|
| 112 |
return $tag ; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
# --------------------------------------------------------------------------------- |
| 117 |
# |
| 118 |
# Add new rtf command that has an corresponding end rtf command |
| 119 |
# |
| 120 |
# --------------------------------------------------------------------------------- |
| 121 |
|
| 122 |
|
| 123 |
sub AddRTFCmdWithEnd |
| 124 |
|
| 125 |
{ |
| 126 |
my ($self, $cmdname, $endname, $procinfo) = @_ ; |
| 127 |
|
| 128 |
my $tag = $self -> AddRTFCmd ($cmdname, $procinfo) ; |
| 129 |
|
| 130 |
$tag -> {'endtag'} = $endname ; |
| 131 |
|
| 132 |
return $tag ; |
| 133 |
} |
| 134 |
|
| 135 |
# --------------------------------------------------------------------------------- |
| 136 |
# |
| 137 |
# Add new rtf command with start and end |
| 138 |
# |
| 139 |
# --------------------------------------------------------------------------------- |
| 140 |
|
| 141 |
|
| 142 |
sub AddRTFCmdBlock |
| 143 |
|
| 144 |
{ |
| 145 |
my ($self, $cmdname, $endname, $procinfostart, $procinfoend) = @_ ; |
| 146 |
|
| 147 |
my $tag ; |
| 148 |
my $pinfo ; |
| 149 |
|
| 150 |
$tag = $self -> AddRTFCmd ($cmdname, $procinfostart) ; |
| 151 |
$tag -> {'endtag'} = $endname ; |
| 152 |
$pinfo = $tag -> {'procinfo'} -> {$self -> {-procinfotype}} ; |
| 153 |
$pinfo -> {'stackname'} = 'metacmd' ; |
| 154 |
$pinfo -> {'push'} = $cmdname ; |
| 155 |
|
| 156 |
$tag = $self -> AddRTFCmd ($endname, $procinfoend) ; |
| 157 |
$pinfo = $tag -> {'procinfo'} -> {$self -> {-procinfotype}} ; |
| 158 |
$pinfo -> {'stackname'} = 'metacmd' ; |
| 159 |
$pinfo -> {'stackmatch'} = $cmdname ; |
| 160 |
|
| 161 |
|
| 162 |
return $tag ; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
################################################################################### |
| 169 |
# |
| 170 |
# Definitions for RTF |
| 171 |
# |
| 172 |
################################################################################### |
| 173 |
|
| 174 |
sub Init |
| 175 |
|
| 176 |
{ |
| 177 |
my ($self) = @_ ; |
| 178 |
|
| 179 |
$self -> AddInitCode (undef, '$_ep_rtf_ndx=0;$escmode=0;sub esc { my $x = shift ; $x =~ s/([{}])/\\\\$1/g ; $x =~ s/\n/\\\\line /g ; $x} ; ', undef) ; |
| 180 |
|
| 181 |
$self -> AddRTFCmd ('DOCVARIABLE', |
| 182 |
{ |
| 183 |
perlcode => '_ep_rp(%$x%,scalar(esc(join(\'\',', |
| 184 |
perlcodeend => '))));', |
| 185 |
compiletimeperlcode => q[if ($_ep_rtf_inside) { my $tmp = $Embperl::req -> component -> code () ; $tmp =~ s/_ep_rp\(.*?\,/push \@_ep_rtf_tmp,(/ ; $Embperl::req -> component -> code ($tmp) } ; $_ep_rtf_cmd = 1 ;], |
| 186 |
}, |
| 187 |
{ |
| 188 |
'inside' => \%Var, |
| 189 |
'cdatatype' => 0, |
| 190 |
}, |
| 191 |
) ; |
| 192 |
$self -> AddRTFCmd ('MERGEFIELD', |
| 193 |
{ |
| 194 |
perlcode => '_ep_rp(%$x%,scalar(esc(join(\'\', ', |
| 195 |
perlcodeend => '))));', |
| 196 |
compiletimeperlcode => q[if ($_ep_rtf_inside) { my $tmp = $Embperl::req -> component -> code () ; $tmp =~ s/_ep_rp\(.*?\,/push \@_ep_rtf_tmp,(/ ; $tmp .= '\'' . (%>'-1%) . '\'' . ',' ; $Embperl::req -> component -> code ($tmp) } ; $_ep_rtf_cmd = 1 ;], |
| 197 |
}, |
| 198 |
{ |
| 199 |
'inside' => \%Var, |
| 200 |
'cdatatype' => 0, |
| 201 |
}, |
| 202 |
) ; |
| 203 |
|
| 204 |
|
| 205 |
$self -> AddRTFCmd ('NEXT', |
| 206 |
{ |
| 207 |
perlcode => '$_ep_rtf_ndx++;', |
| 208 |
'removenode' => 1, |
| 209 |
}, |
| 210 |
{ |
| 211 |
'nodename' => '::::NEXT', |
| 212 |
'cdatatype' => 0, |
| 213 |
}) ; |
| 214 |
|
| 215 |
$self -> AddRTFCmd ('MERGEREC', |
| 216 |
{ |
| 217 |
perlcode => '_ep_rp(%$x%,$_ep_rtf_ndx+1);', |
| 218 |
}, |
| 219 |
{ |
| 220 |
'nodename' => '::::MERGEREC', |
| 221 |
'cdatatype' => 0, |
| 222 |
}, |
| 223 |
{ |
| 224 |
perlcode => 'push @_ep_rtf_tmp,$_ep_rtf_ndx+1', |
| 225 |
}, |
| 226 |
) ; |
| 227 |
|
| 228 |
$self -> AddRTFCmd ('MERGESEQ', |
| 229 |
{ |
| 230 |
perlcode => '_ep_rp(%$x%,$_ep_rtf_ndx+1);', |
| 231 |
}, |
| 232 |
{ |
| 233 |
'nodename' => '::::MERGESEQ', |
| 234 |
'cdatatype' => 0, |
| 235 |
}, |
| 236 |
{ |
| 237 |
perlcode => 'push @_ep_rtf_tmp,$_ep_rtf_ndx+1', |
| 238 |
}, |
| 239 |
) ; |
| 240 |
|
| 241 |
$self -> AddRTFCmd ('IF', |
| 242 |
{ |
| 243 |
perlcode => '@_ep_rtf_tmp=();%>\'-1% =~ /^\s*(.*?)\s*$/ ; $_ep_rtf_preif=$1;', |
| 244 |
compiletimeperlcode => q[$_ep_rtf_inside = 1 ; $_ep_rtf_code = '{ my $itmp = $true?$_ep_rtf_tmp[3]:$_ep_rtf_tmp[4]; _ep_rp($x, \'{\'.$_ep_rtf_preif.(($itmp =~ /^\\\\\\\\/) || !$_ep_rtf_preif?$itmp:" $itmp").\'}\');}' ; $_ep_rtf_cmd = 1 ; ], |
| 245 |
'removenode' => 1, |
| 246 |
}, |
| 247 |
{ |
| 248 |
'nodename' => '::::IF', |
| 249 |
'cdatatype' => 0, |
| 250 |
}, |
| 251 |
) ; |
| 252 |
|
| 253 |
$self -> AddRTFCmd ('NEXTIF', |
| 254 |
{ |
| 255 |
perlcode => '@_ep_rtf_tmp=();', |
| 256 |
compiletimeperlcode => q[$_ep_rtf_inside = 1 ; $_ep_rtf_code = '$_ep_rtf_ndx++ if ($true); ' ; $_ep_rtf_cmd = 1 ;], |
| 257 |
'removenode' => 1, |
| 258 |
}, |
| 259 |
{ |
| 260 |
'nodename' => '::::NEXTIF', |
| 261 |
'cdatatype' => 0, |
| 262 |
}, |
| 263 |
) ; |
| 264 |
|
| 265 |
$self -> AddRTFCmd ('SKIPIF', |
| 266 |
{ |
| 267 |
perlcode => '@_ep_rtf_tmp=();', |
| 268 |
compiletimeperlcode => q[$_ep_rtf_inside = 1 ; $_ep_rtf_code = '$_ep_rtf_ndx+=2 if ($true); ' ; $_ep_rtf_cmd = 1 ; ], |
| 269 |
'removenode' => 1, |
| 270 |
}, |
| 271 |
{ |
| 272 |
'nodename' => '::::NEXTIF', |
| 273 |
'cdatatype' => 0, |
| 274 |
}, |
| 275 |
) ; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
sub Var2Code |
| 281 |
|
| 282 |
{ |
| 283 |
my $var = shift ; |
| 284 |
$var =~ s/(\r|\n)//g ; # variablename can contain \r and/or \n !!! |
| 285 |
my @parts = split (/\./, $var) ; |
| 286 |
return '' if (!@parts) ; |
| 287 |
my $code = '$param[$_ep_rtf_ndx]' ; |
| 288 |
|
| 289 |
foreach (@parts) |
| 290 |
{ |
| 291 |
if (/^\d+/) |
| 292 |
{ $code .= "[$_]" } |
| 293 |
else |
| 294 |
{ $code .= "{'$_'}" } |
| 295 |
} |
| 296 |
return $code ; |
| 297 |
} |
| 298 |
|
| 299 |
# Variablename inside of a command |
| 300 |
|
| 301 |
%Var = ( |
| 302 |
'-lsearch' => 1, |
| 303 |
'Varname' => |
| 304 |
{ |
| 305 |
'contains' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789.\r\n", |
| 306 |
#'inside' => \%Varseparator, |
| 307 |
#'inside' => \%Varinside, |
| 308 |
'cdatatype' => ntypTag, |
| 309 |
'nodename' => ':{:}:full_var', |
| 310 |
'procinfo' => { |
| 311 |
'embperl' => { |
| 312 |
compiletimeperlcode => q[$Embperl::req -> component -> code (Embperl::Syntax::RTF::Var2Code (%#'0%)) ;], |
| 313 |
}, |
| 314 |
}, |
| 315 |
|
| 316 |
}, |
| 317 |
'VarnameComment' => |
| 318 |
{ |
| 319 |
text => '\\\\*', |
| 320 |
'cdatatype' => 0, |
| 321 |
'contains' => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789.\r\n", |
| 322 |
}, |
| 323 |
) ; |
| 324 |
|
| 325 |
|
| 326 |
%Inside = () ; |
| 327 |
|
| 328 |
|
| 329 |
%ParaBlockInside = ( |
| 330 |
'-lsearch' => 1, |
| 331 |
'RTF block' => { |
| 332 |
'text' => '{', |
| 333 |
'end' => '}', |
| 334 |
'nodename' => '!:{:::}', |
| 335 |
'nodetype' => ntypStartEndTag, |
| 336 |
'cdatatype' => ntypCDATA, |
| 337 |
'removespaces' => 0, |
| 338 |
'inside' => \%ParaBlockInside, |
| 339 |
'procinfo' => { |
| 340 |
'embperl' => { |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
'RTF field' => { |
| 345 |
'text' => '{\field', |
| 346 |
'end' => '}', |
| 347 |
'nodename' => '!:{:::}', |
| 348 |
'nodetype' => ntypStartEndTag, |
| 349 |
#'cdatatype' => ntypAttrValue, |
| 350 |
'insidemustexist' => 1, |
| 351 |
'inside' => \%FieldStart, |
| 352 |
'procinfo' => { |
| 353 |
'embperl' => { |
| 354 |
}, |
| 355 |
}, |
| 356 |
}, |
| 357 |
) ; |
| 358 |
|
| 359 |
|
| 360 |
# Start of commands |
| 361 |
|
| 362 |
%CmdStart = ( |
| 363 |
'-lsearch' => 1, |
| 364 |
'RTF block cmd' => { |
| 365 |
'text' => '{', |
| 366 |
'end' => '}', |
| 367 |
'unescape' => 1, |
| 368 |
'nodetype' => ntypStartEndTag, |
| 369 |
'removespaces' => 2, |
| 370 |
#'cdatatype' => ntypCDATA, |
| 371 |
#'cdatatype' => ntypAttrValue, |
| 372 |
'nodename' => '!:', |
| 373 |
'inside' => {%ParaBlockInside,}, |
| 374 |
'procinfo' => { |
| 375 |
'embperl' => { |
| 376 |
compiletimeperlcodeend => q[ $Embperl::req -> component -> code ('') if (!$_ep_rtf_inside || $_ep_rtf_cmd) ; $_ep_rtf_cmd = 0 ;], |
| 377 |
perlcodeend => q[ { my $tmp = %#'0% ; if ($tmp =~ /\"\s*$/) { $tmp =~ s/\\\\/\\\\\\\\/g ; push @_ep_rtf_tmp, Text::ParseWords::quotewords('\s+', 0, $tmp) } else { push @_ep_rtf_tmp,$tmp } }], |
| 378 |
}, |
| 379 |
}, |
| 380 |
# 'procinfo' => {'embperl' => {}}, |
| 381 |
}, |
| 382 |
'RTF field' => { |
| 383 |
'text' => '{\field', |
| 384 |
'end' => '}', |
| 385 |
'nodename' => '!:{:::}', |
| 386 |
'nodetype' => ntypStartEndTag, |
| 387 |
'insidemustexist' => 1, |
| 388 |
'inside' => \%FieldStart, |
| 389 |
'procinfo' => { |
| 390 |
'embperl' => { |
| 391 |
compiletimeperlcode => q[$_ep_rtf_inside++ if ($_ep_rtf_inside) ; ], |
| 392 |
compiletimeperlcodeend => q[ if ($_ep_rtf_inside) { $_ep_rtf_inside-- ; if ($_ep_rtf_inside == 0) { $Embperl::req -> component -> code ($_ep_rtf_code) ; } } ], |
| 393 |
}, |
| 394 |
}, |
| 395 |
}, |
| 396 |
|
| 397 |
) ; |
| 398 |
|
| 399 |
# Field start and end |
| 400 |
|
| 401 |
%FieldStart = ( |
| 402 |
'-lsearch' => 1, |
| 403 |
'RTF block inside' => { |
| 404 |
'text' => '{', |
| 405 |
'end' => '}', |
| 406 |
'nodename' => '!:{:::}', |
| 407 |
'nodetype' => ntypStartEndTag, |
| 408 |
'cdatatype' => ntypCDATA, |
| 409 |
'removespaces' => 0, |
| 410 |
'inside' => \%Block, |
| 411 |
}, |
| 412 |
'RTF fieldstart' => { |
| 413 |
'text' => '{\*\fldinst', |
| 414 |
'end' => '}', |
| 415 |
'nodename' => '!:', |
| 416 |
'nodetype' => ntypStartEndTag, |
| 417 |
#'cdatatype' => ntypCDATA, |
| 418 |
#'cdatatype' => ntypAttrValue, |
| 419 |
'inside' => \%CmdStart, |
| 420 |
'procinfo' => {'embperl' => {}}, |
| 421 |
}, |
| 422 |
'RTF fieldend' => { |
| 423 |
'text' => '{\fldrslt', |
| 424 |
'end' => '}', |
| 425 |
'nodename' => '!', |
| 426 |
'cdatatype' => ntypAttrValue, |
| 427 |
'inside' => \%BlockInside, |
| 428 |
}, |
| 429 |
) ; |
| 430 |
|
| 431 |
|
| 432 |
=pod |
| 433 |
|
| 434 |
=begin comment |
| 435 |
|
| 436 |
'RTF first paragraph' => { |
| 437 |
'text' => '\pard', |
| 438 |
'end' => '}', |
| 439 |
'nodename' => '!:::\pard:', |
| 440 |
'nodetype' => ntypStartTag, |
| 441 |
'inside' => \%Block, |
| 442 |
'procinfo' => { |
| 443 |
'embperl' => { |
| 444 |
perlcode => q[ do { ], |
| 445 |
perlcodeend => q[ $_ep_rtf_ndx++;} while ($param[$_ep_rtf_ndx]) ; ], |
| 446 |
mayjump => 1, |
| 447 |
}, |
| 448 |
}, |
| 449 |
}, |
| 450 |
|
| 451 |
=end comment |
| 452 |
|
| 453 |
=cut |
| 454 |
|
| 455 |
# Finds the first paragraph |
| 456 |
|
| 457 |
%Para = ( |
| 458 |
'-lsearch' => 1, |
| 459 |
'RTF field' => { |
| 460 |
'text' => '{\field', |
| 461 |
'end' => '}', |
| 462 |
'nodename' => '!:{:::}', |
| 463 |
'nodetype' => ntypStartEndTag, |
| 464 |
#'cdatatype' => ntypAttrValue, |
| 465 |
'insidemustexist' => 1, |
| 466 |
'inside' => \%FieldStart, |
| 467 |
'procinfo' => { |
| 468 |
'embperl' => { |
| 469 |
}, |
| 470 |
}, |
| 471 |
}, |
| 472 |
%ParaBlockInside, |
| 473 |
) ; |
| 474 |
|
| 475 |
%RTF = ( |
| 476 |
'-lsearch' => 1, |
| 477 |
'RTF mainblock' => { |
| 478 |
'text' => '{\rtf1', |
| 479 |
'end' => '}', |
| 480 |
'nodename' => '!:{\rtf1:::}', |
| 481 |
'nodetype' => ntypStartEndTag, |
| 482 |
'cdatatype' => ntypCDATA, |
| 483 |
'removespaces' => 0, |
| 484 |
'inside' => \%Block, |
| 485 |
'procinfo' => { |
| 486 |
'embperl' => { |
| 487 |
perlcode => q[ my @_ep_rtf_stack ; do { ], |
| 488 |
perlcodeend => q[ $_ep_rtf_ndx++;} while ($param[$_ep_rtf_ndx]) ; ], |
| 489 |
mayjump => 1, |
| 490 |
}, |
| 491 |
}, |
| 492 |
}, |
| 493 |
'RTF field' => { |
| 494 |
'text' => '{\field', |
| 495 |
'end' => '}', |
| 496 |
'nodename' => '!:{:::}', |
| 497 |
'nodetype' => ntypStartEndTag, |
| 498 |
'insidemustexist' => 1, |
| 499 |
'inside' => \%FieldStart, |
| 500 |
'procinfo' => { |
| 501 |
'embperl' => { |
| 502 |
}, |
| 503 |
}, |
| 504 |
}, |
| 505 |
) ; |
| 506 |
|
| 507 |
# Basic definition of Block in a RTF file |
| 508 |
|
| 509 |
#print "---------\n" ; |
| 510 |
#foreach (@_ep_rtf_tmp) { print "<$_>\n" ; } ; |
| 511 |
|
| 512 |
|
| 513 |
%Block = ( |
| 514 |
'-lsearch' => 1, |
| 515 |
# 'RTF block' => { |
| 516 |
# 'text' => '{', |
| 517 |
# 'end' => '}', |
| 518 |
# 'nodename' => '!:{:}:', |
| 519 |
# 'cdatatype' => ntypAttrValue, |
| 520 |
# #'forcetype' => ntypAttrValue, |
| 521 |
# 'removespaces' => 0, |
| 522 |
# 'inside' => \%Block, |
| 523 |
# }, |
| 524 |
'RTF block' => { |
| 525 |
'text' => '{', |
| 526 |
'end' => '}', |
| 527 |
'nodename' => '!:{:::}', |
| 528 |
'nodetype' => ntypStartEndTag, |
| 529 |
'cdatatype' => ntypCDATA, |
| 530 |
'removespaces' => 0, |
| 531 |
'inside' => \%Block, |
| 532 |
'procinfo' => { |
| 533 |
'embperl' => {}, |
| 534 |
}, |
| 535 |
}, |
| 536 |
'RTF field' => { |
| 537 |
'text' => '{\field', |
| 538 |
'end' => '}', |
| 539 |
'nodename' => '!:{:::}', |
| 540 |
'nodetype' => ntypStartEndTag, |
| 541 |
'insidemustexist' => 1, |
| 542 |
'inside' => \%FieldStart, |
| 543 |
'procinfo' => { |
| 544 |
'embperl' => { |
| 545 |
compiletimeperlcode => q[$_ep_rtf_inside++ if ($_ep_rtf_inside) ; ], |
| 546 |
perlcodeend => '%$x%', |
| 547 |
compiletimeperlcodeend => q[#!- |
| 548 |
if ($_ep_rtf_inside) |
| 549 |
{ |
| 550 |
$_ep_rtf_inside-- ; |
| 551 |
if ($_ep_rtf_inside == 0) |
| 552 |
{ |
| 553 |
my $x = $Embperl::req -> component -> code ; |
| 554 |
$_ep_rtf_code =~ s/\$x/$x/g ; |
| 555 |
|
| 556 |
$Embperl::req -> component -> code (q[ |
| 557 |
{ |
| 558 |
|
| 559 |
$_ep_rtf_tmp[0] =~ s/\\\\\\\\[0-9a-zA-Z]+\s*//g ; |
| 560 |
$_ep_rtf_tmp[1] =~ s/\\\\\\\\[0-9a-zA-Z]+\s*//g ; |
| 561 |
if ($_ep_rtf_tmp[0] =~ /^\s*(.+?)\s*(=|<|>)$/) |
| 562 |
{ |
| 563 |
unshift @_ep_rtf_tmp, $1 ; |
| 564 |
$_ep_rtf_tmp[1] = $2 ; |
| 565 |
} |
| 566 |
if ($_ep_rtf_tmp[1] =~ /^(=|<|>)\s*\"?\s*(.+?)\s*$/) |
| 567 |
{ |
| 568 |
unshift @_ep_rtf_tmp, $_ep_rtf_tmp[0] ; |
| 569 |
$_ep_rtf_tmp[1] = $1 ; |
| 570 |
$_ep_rtf_tmp[2] = $2 ; |
| 571 |
} |
| 572 |
if (!$_ep_rtf_tmp[1]) |
| 573 |
{ |
| 574 |
my $tmp = shift @_ep_rtf_tmp ; |
| 575 |
$_ep_rtf_tmp[0] = $tmp ; |
| 576 |
} |
| 577 |
if ($_ep_rtf_tmp[4] =~ /^(\\\\\\\\[0-9a-zA-Z]+\s*)+$/) |
| 578 |
{ |
| 579 |
$_ep_rtf_tmp[4] = $_ep_rtf_tmp[5] ; |
| 580 |
} |
| 581 |
|
| 582 |
my $op = $_ep_rtf_tmp[1] ; |
| 583 |
if ($op eq '=') |
| 584 |
{ $true = $_ep_rtf_tmp[0] eq $_ep_rtf_tmp[2] } |
| 585 |
elsif ($op eq '<') |
| 586 |
{ $true = $_ep_rtf_tmp[0] lt $_ep_rtf_tmp[2] } |
| 587 |
elsif ($op eq '>') |
| 588 |
{ $true = $_ep_rtf_tmp[0] gt $_ep_rtf_tmp[2] } |
| 589 |
elsif ($op eq '<=') |
| 590 |
{ $true = $_ep_rtf_tmp[0] le $_ep_rtf_tmp[2] } |
| 591 |
elsif ($op eq '>=') |
| 592 |
{ $true = $_ep_rtf_tmp[0] gt $_ep_rtf_tmp[2] } |
| 593 |
elsif ($op eq '!=') |
| 594 |
{ $true = $_ep_rtf_tmp[0] ne $_ep_rtf_tmp[2] } |
| 595 |
elsif ($op eq '<>') |
| 596 |
{ $true = $_ep_rtf_tmp[0] ne $_ep_rtf_tmp[2] } |
| 597 |
|
| 598 |
] . $_ep_rtf_code . '}') ; |
| 599 |
} |
| 600 |
} |
| 601 |
else |
| 602 |
{ |
| 603 |
$Embperl::req -> component -> code ('') ; |
| 604 |
} |
| 605 |
], |
| 606 |
}, |
| 607 |
}, |
| 608 |
}, |
| 609 |
|
| 610 |
'RTF escape open' => { |
| 611 |
'text' => '\\{', |
| 612 |
'nodename' => '\\{', |
| 613 |
'nodetype' => ntypCDATA, |
| 614 |
}, |
| 615 |
'RTF escape close' => { |
| 616 |
'text' => '\\}', |
| 617 |
'nodename' => '\\}', |
| 618 |
'nodetype' => ntypCDATA, |
| 619 |
}, |
| 620 |
|
| 621 |
) ; |
| 622 |
|
| 623 |
# Block inside of a command that should be deleted in the output |
| 624 |
|
| 625 |
%BlockInside = ( |
| 626 |
'-lsearch' => 1, |
| 627 |
'RTF block' => { |
| 628 |
'text' => '{', |
| 629 |
'end' => '}', |
| 630 |
'nodename' => '!', |
| 631 |
'cdatatype' => ntypAttrValue, |
| 632 |
'inside' => \%BlockInside, |
| 633 |
}, |
| 634 |
) ; |
| 635 |
|
| 636 |
|
| 637 |
1; |
| 638 |
=pod |
| 639 |
|
| 640 |
=begin comment |
| 641 |
|
| 642 |
#$Embperl::req -> component -> code ($_ep_rtf_code) ; |
| 643 |
|
| 644 |
my $x = $Embperl::req -> component -> code ; |
| 645 |
my ($op, $cmp, $a, $b) = XML::Embperl::DOM::Node::iChildsText (%$q%,%$x%,1) =~ /\:([=<>])+\s*\"(.*?)\"(?:\s*\"(.*?)\"\s*\"(.*?)\")?/ ; |
| 646 |
|
| 647 |
if ($op eq '=') { $op = 'eq' } |
| 648 |
elsif ($op eq '<') { $op = 'lt' } |
| 649 |
elsif ($op eq '>') { $op = 'gt' } |
| 650 |
elsif ($op eq '>=') { $op = 'ge' } |
| 651 |
elsif ($op eq '<=') { $op = 'le' } |
| 652 |
|
| 653 |
print "\n#" . __LINE__ . " op = $op cmp = $cmp a = $a b = $b code=$_ep_rtf_code tmp=$_ep_rtf_tmp 0=$param[0]{'adressen_anrede'} ndx=$_ep_rtf_ndx eval=qq[$_ep_rtf_code]\n" ; |
| 654 |
$_ep_rtf_code =~ s/\$a/q\[$a\]/g ; |
| 655 |
$_ep_rtf_code =~ s/\$b/q\[$b\]/g ; |
| 656 |
$_ep_rtf_code =~ s/\$cmp/q\[$cmp\]/g ; |
| 657 |
$_ep_rtf_code =~ s/\$op/$op/g ; |
| 658 |
$_ep_rtf_code =~ s/\$x/$x/g ; |
| 659 |
print "result=$_ep_rtf_code\n" ; |
| 660 |
|
| 661 |
warn "RTF IF syntax error. Missing operator" if (!$op) ; |
| 662 |
|
| 663 |
=end comment |
| 664 |
|
| 665 |
=cut |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
__END__ |
| 670 |
|
| 671 |
=pod |
| 672 |
|
| 673 |
=head1 NAME |
| 674 |
|
| 675 |
Embperl::Syntax::RTF - define syntax for RTF files |
| 676 |
|
| 677 |
=head1 SYNOPSIS |
| 678 |
|
| 679 |
|
| 680 |
=head1 DESCRIPTION |
| 681 |
|
| 682 |
Class derived from Embperl::Syntax to define the syntax for |
| 683 |
RTF files. RTF files can be read and written by various word processing |
| 684 |
programs. This allows you to create dynamic wordprocessing documents or |
| 685 |
let process serial letters thru Embperl. |
| 686 |
|
| 687 |
Currently Embperl regocnices the fields C<DOCVARIABLE>, C<MERGEFIELD> and |
| 688 |
C<NEXT>. Variablenames are resolved as hash keys to $param[0] e.g. C<foo.bar> |
| 689 |
referes to C<$param[0]{foo}{bar}>, the C<@param> Array can by set via the |
| 690 |
C<param> parameter of the C<Execute> function. C<NEXT> moves to the next element |
| 691 |
of the @param array. If the end of the document is reached, Embperl repeats |
| 692 |
the document until all element of @param are processed. This can for example |
| 693 |
be use to tie a database table to @param and generate a serial letter. |
| 694 |
|
| 695 |
NOTE: Extenting this syntax to support full Embperl support (like embedding Perl |
| 696 |
into RTF file is planned, but not implemented yet) |
| 697 |
|
| 698 |
=head1 Example for generating a serial letter from a database |
| 699 |
|
| 700 |
|
| 701 |
use DBIx::Recordset ; |
| 702 |
use Embperl ; |
| 703 |
|
| 704 |
*set = DBIx::Recordset -> Search({'!DataSource' => $db, '!Table' => 'address', '!WriteMode' => 0}) ; |
| 705 |
|
| 706 |
die DBIx::Recordset -> LastError if (DBIx::Recordset -> LastError) ; |
| 707 |
|
| 708 |
Embperl::Execute ({'inputfile' => 'address.rtf', param => \@set, syntax => 'RTF'}) ; |
| 709 |
|
| 710 |
|
| 711 |
# if your database table contains fields 'name' and 'street' you can now simply insert a |
| 712 |
# fields call 'name' and 'street' in your RTF file and Embperl will repeat the document |
| 713 |
# until all records are outputed |
| 714 |
|
| 715 |
|
| 716 |
=head1 Methods |
| 717 |
|
| 718 |
I<Embperl::Syntax::RTF> defines the following methods: |
| 719 |
|
| 720 |
=head2 Embperl::Syntax::RTF -> new / $self -> new |
| 721 |
|
| 722 |
Create a new syntax class. This method should only be called inside a constructor |
| 723 |
of a derived class. |
| 724 |
|
| 725 |
|
| 726 |
=head2 AddRTFCmd ($cmdname, $procinfo) |
| 727 |
|
| 728 |
Add a new RTF command with name C<$cmdname> and use processor info from |
| 729 |
C<$procinfo>. See I<Embperl::Syntax> for a definition of procinfo. |
| 730 |
|
| 731 |
=head2 AddRTFCmdWithEnd ($cmdname, $endname, $procinfo) |
| 732 |
|
| 733 |
Add a new RTF command with name C<$cmdname> and use processor info from |
| 734 |
C<$procinfo>. Addtionaly specify that a matching C<$endname> RTF command |
| 735 |
must be found to end the block, that is started by this RTF command. |
| 736 |
See I<Embperl::Syntax> for a definition of procinfo. |
| 737 |
|
| 738 |
=head2 AddRTFCmdBlock ($cmdname, $endname, $procinfostart, $procinfoend) |
| 739 |
|
| 740 |
Add a new RTF command with name C<$cmdname> and and a second RTF command |
| 741 |
C<$endname> which ends the block that is started by C<$cmdname>. |
| 742 |
Use processor info from C<$procinfo>. |
| 743 |
See I<Embperl::Syntax> for a definition of procinfo. |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
=head1 Author |
| 748 |
|
| 749 |
G. Richter (richter at embperl dot org) |
| 750 |
|
| 751 |
=head1 See Also |
| 752 |
|
| 753 |
Embperl::Syntax |