Class: PageTemplate::SyntaxGlossary

Inherits:
Object
  • Object
show all
Defined in:
lib/PageTemplate/parser.rb

Overview

This is the dictionary of commands and the directive that Parser uses to compile a template into a tree of commands.

directive is the general format of a PageTemplate command. Default: /[%(.+?)%]/m

regexps should not contain PageTemplate command text. i.e: /var w+/i should be used instead of /[% var %]/

Direct Known Subclasses

DefaultGlossary, HTGlossary

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default(&block) ⇒ Object


298
299
300
301
302
303
304
# File 'lib/PageTemplate/parser.rb', line 298

def default(&block)
  if block_given?
    @default = block
  else
    @default
  end
end

.directiveObject

Returns the value of attribute directive


296
297
298
# File 'lib/PageTemplate/parser.rb', line 296

def directive
  @directive
end

Class Method Details

.define(rx, &block) ⇒ Object

Define a regexp -> Command mapping. rx is inserted in the lookup table as a key for command

Raises:

  • (ArgumentError)

324
325
326
327
328
329
# File 'lib/PageTemplate/parser.rb', line 324

def define(rx,&block)
  raise ArgumentError, 'First argument to define must be a Regexp' unless rx.is_a?(Regexp)
  raise ArgumentError, 'Block expected' unless block
  @glossary ||= {}
  @glossary[rx] = block
end

.define_global_var(rx) ⇒ Object

This is shorthand for define(key,Valuecommand), and also allows key to be a string, converting it to a regexp before adding it to the dictionary.


341
342
343
344
345
346
347
# File 'lib/PageTemplate/parser.rb', line 341

def define_global_var(rx)
  @glossary ||= {}
  rx = /^(#{key.to_s}(?:\.\w+\??)*)(?:\s:(\w+))?$/ unless rx.is_a?(Regexp)
  @glossary[rx] = lambda { |match|
    ValueCommand.new(match[1],match[2])
  }
end

.lookup(command) ⇒ Object

Look up command to see if it matches a command within the lookup table, returning the instance of the command for it, or an UnknownCommand if none match.


309
310
311
312
313
314
315
316
317
# File 'lib/PageTemplate/parser.rb', line 309

def lookup(command)
  @glossary.each do |key,val|
    if m = key.match(command)
      return val.call(m)
    end
  end

  return @default.call(command)
end

.modifier(sym, &block) ⇒ Object

Raises:

  • (ArgumentError)

331
332
333
334
335
336
# File 'lib/PageTemplate/parser.rb', line 331

def modifier(sym,&block)
  raise ArgumentError, 'First argument to define must be a Symbol' unless sym.is_a?(Symbol)
  raise ArgumentError, 'Block expected' unless block
  @modifiers ||= Hash.new(lambda { false })
  @modifiers[sym] = block
end

.modifies?(modifier, cmd, command) ⇒ Boolean

Returns:

  • (Boolean)

318
319
320
# File 'lib/PageTemplate/parser.rb', line 318

def modifies?(modifier,cmd,command)
  @modifiers[modifier].call(cmd,command)
end