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



245
246
247
248
249
250
251
# File 'lib/PageTemplate/parser.rb', line 245

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

.directiveObject

Returns the value of attribute directive.



243
244
245
# File 'lib/PageTemplate/parser.rb', line 243

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)


271
272
273
274
275
276
# File 'lib/PageTemplate/parser.rb', line 271

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.



288
289
290
291
292
293
294
# File 'lib/PageTemplate/parser.rb', line 288

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.



256
257
258
259
260
261
262
263
264
# File 'lib/PageTemplate/parser.rb', line 256

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)


278
279
280
281
282
283
# File 'lib/PageTemplate/parser.rb', line 278

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)


265
266
267
# File 'lib/PageTemplate/parser.rb', line 265

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