| 1 | # | ||||||||||
| 2 | # Package Definition | ||||||||||
| 3 | # | ||||||||||
| 4 | |||||||||||
| 5 | package Text::UberText; | ||||||||||
| 6 | |||||||||||
| 7 | # | ||||||||||
| 8 | # Compiler Directives | ||||||||||
| 9 | # | ||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | # | ||||||||||
| 15 | # Includes | ||||||||||
| 16 | # | ||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use Text::UberText::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Text::UberText::Dispatch; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use Text::UberText::Log; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | # | ||||||||||
| 23 | # Global Variables | ||||||||||
| 24 | # | ||||||||||
| 25 | |||||||||||
| 26 | 1 | ▶ | use vars qw/$VERSION $ImportMap @ImportList /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 1 | $VERSION=0.95; | |||||||||
| 29 | |||||||||||
| 30 | 1 | $ImportMap={ | |||||||||
| 31 | "none" => [ ], | ||||||||||
| 32 | "minimal" => [ "Text::UberText::Modules::Info" | ||||||||||
| 33 | ], | ||||||||||
| 34 | "standard" => [ "Text::UberText::Modules::Info", | ||||||||||
| 35 | "Text::UberText::Modules::Variables", | ||||||||||
| 36 | "Text::UberText::Modules::Loop", | ||||||||||
| 37 | ], | ||||||||||
| 38 | }; | ||||||||||
| 39 | |||||||||||
| 40 | # | ||||||||||
| 41 | # Methods | ||||||||||
| 42 | # | ||||||||||
| 43 | |||||||||||
| 44 | sub new | ||||||||||
| 45 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 46 | 0 | my ($class)=shift; | |||||||||
| 47 | 0 | my ($object); | |||||||||
| 48 | 0 | $object={}; | |||||||||
| 49 | 0 | bless ($object,$class); | |||||||||
| 50 | 0 | $object->_init(@_); | |||||||||
| 51 | 0 | return $object; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub read | ||||||||||
| 55 | 0 | ▶ | { | ||||||||
|
Subroutine
read: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 56 | 0 | my ($self)=shift; | |||||||||
| 57 | 0 | my ($file)=shift; | |||||||||
| 58 | 0 | my ($l,$par,@input); | |||||||||
| 59 | 0 | ▶ | open (F, $file) || return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | (@input)=<F>; | |||||||||
| 61 | 0 | close(F); | |||||||||
| 62 | 0 | $self->{parser}->log($self->{log}); | |||||||||
| 63 | 0 | $self->{parser}->dispatch($self->{dispatch}); | |||||||||
| 64 | 0 | $self->{parser}->input(@input); | |||||||||
| 65 | 0 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | sub parse | ||||||||||
| 69 | 0 | ▶ | { | ||||||||
|
Subroutine
parse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 70 | 0 | my ($self)=shift; | |||||||||
| 71 | 0 | return $self->{parser}->parse(); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub extend | ||||||||||
| 75 | 0 | ▶ | { | ||||||||
|
Subroutine
extend: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 76 | 0 | my ($self)=shift; | |||||||||
| 77 | 0 | $self->{dispatch}->extend(@_); | |||||||||
| 78 | 0 | return; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | sub log | ||||||||||
| 82 | 0 | ▶ | { | ||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 83 | 0 | my ($self)=shift; | |||||||||
| 84 | 0 | return $self->{log}; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub parser | ||||||||||
| 88 | 0 | ▶ | { | ||||||||
|
Subroutine
parser: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 89 | 0 | my ($self)=shift; | |||||||||
| 90 | 0 | return $self->{parser}; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub dispatch | ||||||||||
| 94 | 0 | ▶ | { | ||||||||
|
Subroutine
dispatch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 95 | 0 | my ($self)=shift; | |||||||||
| 96 | 0 | return $self->{dispatch}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | # | ||||||||||
| 100 | # Import method | ||||||||||
| 101 | sub import | ||||||||||
| 102 | 1 | ▶ | { | ||||||||
|
Subroutine
import: called CC=2 SCAR=7.1
| |||||||||||
| 103 | 1 | shift; | |||||||||
| 104 | 1 | my ($modules)=shift; | |||||||||
| 105 | 1 | ▶ | if ($modules) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | { | ||||||||||
| 107 | 0 | $modules=lc($modules); | |||||||||
| 108 | } else { | ||||||||||
| 109 | 1 | $modules="standard"; | |||||||||
| 110 | } | ||||||||||
| 111 | 1 | (@ImportList)=@{$ImportMap->{$modules}}; | |||||||||
| 112 | 1 | return; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # | ||||||||||
| 116 | # Hidden Methods | ||||||||||
| 117 | # | ||||||||||
| 118 | |||||||||||
| 119 | sub _init | ||||||||||
| 120 | 0 | ▶ | { | ||||||||
|
Subroutine
_init: not called CC=1 SCAR=6.9
| |||||||||||
| 121 | 0 | my ($self)=shift; | |||||||||
| 122 | 0 | $self->{log}=Text::UberText::Log->new(-main => $self); | |||||||||
| 123 | 0 | $self->{parser}=Text::UberText::Parser->new(-main => $self); | |||||||||
| 124 | 0 | $self->{dispatch}=Text::UberText::Dispatch->new( -main => $self); | |||||||||
| 125 | # Import requested UberText tag namespaces | ||||||||||
| 126 | 0 | $self->_loadModules(); | |||||||||
| 127 | #$self->{dispatch}->extend("Text::UberText::Modules::Variables"); | ||||||||||
| 128 | 0 | return; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | sub _loadModules | ||||||||||
| 132 | 0 | ▶ | { | ||||||||
|
Subroutine
_loadModules: not called CC=2 SCAR=17.9
| |||||||||||
| 133 | 0 | my ($self)=shift; | |||||||||
| 134 | 0 | my ($m); | |||||||||
| 135 | 0 | foreach $m (@ImportList) | |||||||||
| 136 | { | ||||||||||
| 137 | 0 | $self->{dispatch}->extend($m); | |||||||||
| 138 | } | ||||||||||
| 139 | 0 | return; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | # | ||||||||||
| 143 | # Exit Block | ||||||||||
| 144 | # | ||||||||||
| 145 | 1 | 1; | |||||||||
| 146 | |||||||||||
| 147 | # | ||||||||||
| 148 | # POD Documentation | ||||||||||
| 149 | # | ||||||||||
| 150 | |||||||||||
| 151 | =head1 NAME | ||||||||||
| 152 | |||||||||||
| 153 | Text::UberText - Customizable Template Engine | ||||||||||
| 154 | |||||||||||
| 155 | =head1 SYNOPSIS | ||||||||||
| 156 | |||||||||||
| 157 | use Text::UberText qw(standard); | ||||||||||
| 158 | |||||||||||
| 159 | $ub=Text::UberText->new(); | ||||||||||
| 160 | $ub->read(@array); | ||||||||||
| 161 | $ub->extend($acme_invoice); | ||||||||||
| 162 | $doctree=$ub->parse(); | ||||||||||
| 163 | if ($doctree) | ||||||||||
| 164 | { | ||||||||||
| 165 | $doctree->run(); | ||||||||||
| 166 | print($doctree->output()); | ||||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | =head1 DESCRIPTION | ||||||||||
| 170 | |||||||||||
| 171 | Text::UberText is an expandable template system designed to let programmers | ||||||||||
| 172 | write their own commands and tie those commands to program code. UberText | ||||||||||
| 173 | reads command tags in documents, and then calls perl modules based on | ||||||||||
| 174 | the tag. | ||||||||||
| 175 | |||||||||||
| 176 | =head1 SAMPLE DOCUMENT | ||||||||||
| 177 | |||||||||||
| 178 | The following is an example of a document that uses the UberText template | ||||||||||
| 179 | system. | ||||||||||
| 180 | |||||||||||
| 181 | Invoice#: [uber.print string:([acme.invoice number]) fmt:(%010d)] | ||||||||||
| 182 | Date: [uber.date date fmt:(MM/DD/YYYY)] | ||||||||||
| 183 | |||||||||||
| 184 | Ship To: [acme.invoice address:(shipping) ] | ||||||||||
| 185 | |||||||||||
| 186 | Tracking number: [acme.invoice tracking-number vendor:(ups)] | ||||||||||
| 187 | |||||||||||
| 188 | [acme.invoice itemlist -> ] | ||||||||||
| 189 | Item: [acme.invoice itemnumber] [acme.invoice description] | ||||||||||
| 190 | [<- acme.invoice itemlist ] | ||||||||||
| 191 | |||||||||||
| 192 | [acme.invoice terms] | ||||||||||
| 193 | |||||||||||
| 194 | The template is designed by a programmer that has designated the "acme.invoice" | ||||||||||
| 195 | namespace is to be used for his code. He then writes Perl modules that | ||||||||||
| 196 | interface with Text::UberText when the document is parsed. | ||||||||||
| 197 | |||||||||||
| 198 | =head1 UBERTEXT FORMAT | ||||||||||
| 199 | |||||||||||
| 200 | UberText documents are plain text mixed with command tags. A very simple | ||||||||||
| 201 | command tag contains a namespace and a command. | ||||||||||
| 202 | |||||||||||
| 203 | [uber.info version] | ||||||||||
| 204 | |||||||||||
| 205 | In this case, the namespace is C<uber.info> and the command is C<version>. | ||||||||||
| 206 | |||||||||||
| 207 | Commands can have values passed to them. | ||||||||||
| 208 | |||||||||||
| 209 | [uber.info version:"full"] | ||||||||||
| 210 | |||||||||||
| 211 | Commands can also have additional options, and those options can have | ||||||||||
| 212 | values. | ||||||||||
| 213 | |||||||||||
| 214 | [uber.var name:(variable) value:"This is the value of the variable"] | ||||||||||
| 215 | |||||||||||
| 216 | Values for commands or options are specified by following the command | ||||||||||
| 217 | or options with a colon, and then enclosing the value in | ||||||||||
| 218 | paranthesis or double quotes. | ||||||||||
| 219 | |||||||||||
| 220 | An UberText command can also be wrapped around portions of text or other | ||||||||||
| 221 | UberText commands. These are considered block commands. | ||||||||||
| 222 | |||||||||||
| 223 | [uber.transform indent class:(quote) ->] | ||||||||||
| 224 | "You can reach be anytime at my work number [per.info phonenum:(work)]" | ||||||||||
| 225 | [<- uber.transform ] | ||||||||||
| 226 | |||||||||||
| 227 | Command options can be placed in either the tag that starts the block, | ||||||||||
| 228 | or the tag that ends the block. This example.... | ||||||||||
| 229 | |||||||||||
| 230 | [uber.loop count:(20) -> ] | ||||||||||
| 231 | This text will repeat 10 times | ||||||||||
| 232 | [<- uber.loop start:(11) ] | ||||||||||
| 233 | |||||||||||
| 234 | Is identical to..... | ||||||||||
| 235 | [uber.loop count:(20) start:(11) -> ] | ||||||||||
| 236 | This text will repeat 10 times | ||||||||||
| 237 | [<- uber.loop ] | ||||||||||
| 238 | |||||||||||
| 239 | You can also insert UberText commands in the values of options or | ||||||||||
| 240 | commands. | ||||||||||
| 241 | |||||||||||
| 242 | [uber.var name:(version) value:([uber.info version])] | ||||||||||
| 243 | |||||||||||
| 244 | [uber.print string:(The fruit is [uber.rand words:(orange,apple,banana)]) ] | ||||||||||
| 245 | |||||||||||
| 246 | But you cannot use block commands inside the values to options or | ||||||||||
| 247 | commands. | ||||||||||
| 248 | |||||||||||
| 249 | UberText command tags can span multiple lines. | ||||||||||
| 250 | |||||||||||
| 251 | [ | ||||||||||
| 252 | uber.var name:(customer) | ||||||||||
| 253 | value : "John Q. Public" | ||||||||||
| 254 | ] | ||||||||||
| 255 | |||||||||||
| 256 | Leading and trailing whitespace within an UberText command is eliminated. | ||||||||||
| 257 | Some whitespace is needed to seperate the namespace, the command and | ||||||||||
| 258 | options, but that's it. Whitespace within values to commands and | ||||||||||
| 259 | options is kept intact. Also, whitespace after the command tag (including | ||||||||||
| 260 | carriage returns) is kept intact as well. | ||||||||||
| 261 | |||||||||||
| 262 | =head1 LOADING | ||||||||||
| 263 | |||||||||||
| 264 | UberText comes with a set of modules handling a small set of basic | ||||||||||
| 265 | functions. All of the modules are in the Text::UberText::Modules | ||||||||||
| 266 | Perl namespace. You can specify which of those modules are loaded | ||||||||||
| 267 | on the use line. | ||||||||||
| 268 | |||||||||||
| 269 | use Text::UberText qw(minimal); | ||||||||||
| 270 | |||||||||||
| 271 | The modules are not actually loaded until an UberText object is initiated. | ||||||||||
| 272 | |||||||||||
| 273 | The following is a list of module sets. | ||||||||||
| 274 | |||||||||||
| 275 | =over 4 | ||||||||||
| 276 | |||||||||||
| 277 | =item none | ||||||||||
| 278 | |||||||||||
| 279 | None of the included UberText modules are loaded. | ||||||||||
| 280 | |||||||||||
| 281 | =item minimal | ||||||||||
| 282 | |||||||||||
| 283 | Only the Text::UberText::Modules::Info module is loaded | ||||||||||
| 284 | |||||||||||
| 285 | =item standard | ||||||||||
| 286 | |||||||||||
| 287 | The modules Text::UberText::Modules::(Info,Version) are loaded. | ||||||||||
| 288 | |||||||||||
| 289 | =back | ||||||||||
| 290 | |||||||||||
| 291 | =head1 METHODS | ||||||||||
| 292 | |||||||||||
| 293 | =over 4 | ||||||||||
| 294 | |||||||||||
| 295 | =item $ubt=Text::UberText->new(); | ||||||||||
| 296 | |||||||||||
| 297 | Creates a new UberText object. The parser, the dispatch table, and the log | ||||||||||
| 298 | are all initialized. | ||||||||||
| 299 | |||||||||||
| 300 | =item $ubt->read(@input); | ||||||||||
| 301 | |||||||||||
| 302 | Reads in the passed array and passes it to the parser. | ||||||||||
| 303 | |||||||||||
| 304 | =item $tree=$ubt->parse(); | ||||||||||
| 305 | |||||||||||
| 306 | Runs the parser against the input. At this time, the commands and the document | ||||||||||
| 307 | text are sorted out and placed in a Text::UberText::Tree object, which keeps | ||||||||||
| 308 | track of the document. The tree object is then returned from the method call. | ||||||||||
| 309 | |||||||||||
| 310 | =item $ubt->extend($object); | ||||||||||
| 311 | |||||||||||
| 312 | Sends an object to the Text::UberText::Dispatch object, which controls all | ||||||||||
| 313 | methods that interface with the command nodes. $object is either a | ||||||||||
| 314 | Perl object, or just the name of a class. In either case, the Dispatch | ||||||||||
| 315 | object searches for a method called C<uberText> and runs it. It then takes | ||||||||||
| 316 | data returned from the call and uses it to expand its internal dispatch table. | ||||||||||
| 317 | |||||||||||
| 318 | =back | ||||||||||
| 319 | |||||||||||
| 320 | =head1 BUGS/CAVEATS | ||||||||||
| 321 | |||||||||||
| 322 | UberText is pretty complex. There's probably bugs floating around because I | ||||||||||
| 323 | haven't even conceived of all of the possible input errors or third party | ||||||||||
| 324 | module errors that it needs to deal with. | ||||||||||
| 325 | |||||||||||
| 326 | =head1 AUTHOR | ||||||||||
| 327 | |||||||||||
| 328 | Chris Josephes E<lt>cpj1@cpj1.comE<gt> | ||||||||||
| 329 | |||||||||||
| 330 | =head1 SEE ALSO | ||||||||||
| 331 | |||||||||||
| 332 | For documentation on the code, read | ||||||||||
| 333 | L<Text::UberText::Tree>, | ||||||||||
| 334 | L<Text::UberText::Dispatch>, | ||||||||||
| 335 | L<Text::UberText::Parser> | ||||||||||
| 336 | |||||||||||
| 337 | For general documentation, read | ||||||||||
| 338 | L<Text::UberText::Overview>, and | ||||||||||
| 339 | L<Text::UberText::NewModule>. | ||||||||||
| 340 | |||||||||||
| 341 | =head1 COPYRIGHT | ||||||||||
| 342 | |||||||||||
| 343 | Copyright 2002, Chris Josephes. All rights reserved. | ||||||||||
| 344 | This module is free software. It may be used, redistributed, | ||||||||||
| 345 | and/or modified under the same terms as Perl itself. | ||||||||||