Class: PageTemplate::Parser
- Inherits:
-
Object
- Object
- PageTemplate::Parser
- Includes:
- NamespaceItem
- Defined in:
- lib/PageTemplate/parser.rb
Overview
The big ass parser that does all the dirty work of turning templates into compiled commands.
Parser.new() accepts a hash as an argument, and looks for these keys: (with defaults)
'namespace' => A namespace object. (A new namespace)
'glossary' => A SyntaxGlossary class singleton. (DefaultGlossary)
'preprocessor' => The preprocessor. (DefaultPreprocessor)
'default_processor' => The processor. (:process)
'source' => The Source for templates. (FileSource)
Once the parser is created, it can compile and parse any number of templates.
It can be treated as a one-template item by using Parser#load(template), and calling Parser.output
To create separate generated templates from the same engine, use Parser#parse, or Parser#load. (It will still keep the most recent one it’s #load’d, but that will not affect previously parsed or loaded)
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#default_processor ⇒ Object
readonly
Returns the value of attribute default_processor.
-
#glossary ⇒ Object
readonly
Returns the value of attribute glossary.
-
#method_separators ⇒ Object
readonly
Returns the value of attribute method_separators.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#preprocessor ⇒ Object
readonly
Returns the value of attribute preprocessor.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Attributes included from NamespaceItem
Class Method Summary collapse
-
.recent_parser ⇒ Object
This is corny, but recent_parser returns the most recently created Parser.
Instance Method Summary collapse
-
#clearCommands ⇒ Object
Not really of any point, but clears the saved commands.
-
#compile(name) ⇒ Object
Load
name
from a template, but do not save it. -
#initialize(args = {}) ⇒ Parser
constructor
Parser.new() accepts a hash as an argument, and looks for these keys: (with defaults).
-
#load(name) ⇒ Object
Load
name
from a template, and save it to allow this parser to use it for output. -
#output(*args) ⇒ Object
If any commands are loaded and saved, return a string of it.
-
#parse(body) ⇒ Object
Compile a Template (BlockCommand) from a string.
-
#parser ⇒ Object
But redefine parser.
Methods included from NamespaceItem
#clear, #delete, #get, #set, #true?
Constructor Details
#initialize(args = {}) ⇒ Parser
Parser.new() accepts a hash as an argument, and looks for these keys: (with defaults)
'namespace' => A namespace object. (A new namespace)
'glossary' => A SyntaxGlossary object. (a dup of DEFAULTGLOSSARY)
'preprocessor' => The preprocessor. (DefaultPreprocessor)
'default_processor' => The processor. (:process)
'source' => The Source for templates. (FileSource)
479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/PageTemplate/parser.rb', line 479 def initialize(args = {}) @namespace = self @@recent_parser = self @args = args # For sub-commands @parent = args['namespace'] || nil @glossary = args['glossary'] || DefaultGlossary @preprocessor = args['preprocessor'] || DefaultPreprocessor @default_processor = args['default_processor'] || :unescaped @method_separators = args['method_separators'] || './' @source = (args['source'] || FileSource).new(@args) @commands = nil end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
464 465 466 |
# File 'lib/PageTemplate/parser.rb', line 464 def args @args end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
464 465 466 |
# File 'lib/PageTemplate/parser.rb', line 464 def commands @commands end |
#default_processor ⇒ Object (readonly)
Returns the value of attribute default_processor.
462 463 464 |
# File 'lib/PageTemplate/parser.rb', line 462 def default_processor @default_processor end |
#glossary ⇒ Object (readonly)
Returns the value of attribute glossary.
463 464 465 |
# File 'lib/PageTemplate/parser.rb', line 463 def glossary @glossary end |
#method_separators ⇒ Object (readonly)
Returns the value of attribute method_separators.
464 465 466 |
# File 'lib/PageTemplate/parser.rb', line 464 def method_separators @method_separators end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
463 464 465 |
# File 'lib/PageTemplate/parser.rb', line 463 def namespace @namespace end |
#preprocessor ⇒ Object (readonly)
Returns the value of attribute preprocessor.
462 463 464 |
# File 'lib/PageTemplate/parser.rb', line 462 def preprocessor @preprocessor end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
463 464 465 |
# File 'lib/PageTemplate/parser.rb', line 463 def source @source end |
Class Method Details
.recent_parser ⇒ Object
This is corny, but recent_parser returns the most recently created Parser.
468 469 470 |
# File 'lib/PageTemplate/parser.rb', line 468 def Parser.recent_parser @@recent_parser end |
Instance Method Details
#clearCommands ⇒ Object
Not really of any point, but clears the saved commands.
574 575 576 |
# File 'lib/PageTemplate/parser.rb', line 574 def clearCommands @commands = nil end |
#compile(name) ⇒ Object
Load name
from a template, but do not save it.
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/PageTemplate/parser.rb', line 497 def compile(name) body = @source.get(name) case when body.is_a?(Command) body when body cmds = parse(body) @source.cache(name,cmds) if @source.respond_to?(:cache) cmds else cmds = Template.new(self) cmds.add TextCommand.new("[ Template '#{name}' not found ]") cmds end end |
#load(name) ⇒ Object
Load name
from a template, and save it to allow this parser to use it for output.
493 494 495 |
# File 'lib/PageTemplate/parser.rb', line 493 def load(name) @commands = compile(name) end |
#output(*args) ⇒ Object
If any commands are loaded and saved, return a string of it.
578 579 580 581 |
# File 'lib/PageTemplate/parser.rb', line 578 def output(*args) return '' unless @commands @commands.output(*args) end |
#parse(body) ⇒ Object
Compile a Template (BlockCommand) from a string. Does not save the commands.
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/PageTemplate/parser.rb', line 514 def parse(body) rx = @glossary.directive stack = [Template.new(self)] stack[0].parent = self last = stack.last modifier = nil closer = nil while (m = rx.match(body)) pre = m.pre_match command = m[1..-1].compact.first.strip.gsub(/\s+/,' ') body = m.post_match # Convert all pre-text to a TextCommand if (pre && pre.length > 0) stack.last.add TextCommand.new(pre) end # If the command at the top of the stack is a 'Stacker', # Does this command modify it? If so, just skip back. next if modifier && @glossary.modifies?(modifier,last,command) # If it closes, we're done changing this. Pop it off the # Stack and add it to the one above it. if closer and @glossary.modifies?(closer,last,command) cmd = stack.pop last = stack.last last.add(cmd) modifier = last.class.modifier closer = last.class.closer next end # Create the command cmd = @glossary.lookup(command) # If it's a stacking command, push it on the stack if cmd.is_a?(StackableCommand) modifier = cmd.class.modifier closer = cmd.class.closer stack.push cmd last = cmd else last.add cmd end end stack.last.add TextCommand.new(body) if body && body.length > 0 if (stack.length > 1) raise ArgumentError, 'Mismatched command closures in template' end stack[0] end |
#parser ⇒ Object
But redefine parser
569 570 571 |
# File 'lib/PageTemplate/parser.rb', line 569 def parser self end |