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



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

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

.directiveObject

Returns the value of attribute directive.



255
256
257
# File 'lib/PageTemplate/parser.rb', line 255

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)


283
284
285
286
287
288
# File 'lib/PageTemplate/parser.rb', line 283

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.



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

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.



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

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)


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

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)


277
278
279
# File 'lib/PageTemplate/parser.rb', line 277

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