Module: RageRender::Language

Extended by:
Rsec::Helpers
Defined in:
lib/ragerender/language.rb

Defined Under Namespace

Classes: Conditional, EndTag, Function, Layout, Loop, Variable

Constant Summary collapse

TEXT =
/[^\[\]\|\<\>]+/.r
IDENT =

IDENT parses names: ‘names’ => ‘names’ IDENT parses underscores: ‘underscore_name’ => ‘underscore_name’ IDENT parses numbers: ‘name_with_1_number’ => ‘name_with_1_number’ IDENT parses capitals: ‘name_with_Capital’ => ‘name_with_Capital’

/[a-zA-Z0-9_]+/.r
PATH =

PATH parses names: ‘names’ => [‘names’] PATH parses dotted paths: ‘dotted.name’ => [‘dotted’, ‘name’]

IDENT.join('.'.r).even
OPERATOR =
%w{= != ~ !~ <= >= < > % !%}.map(&:r).reduce {|a, b| a | b }
VARIABLE =

VARIABLE parses names: ‘v:name’ => Variable.new() VARIABLE parses dotted paths: ‘v:dotted.name’ => Variable.new([‘dotted’, ‘name’])

('v:'.r >> PATH).map {|path| Variable.new path }
LOOP =
('l:'.r >> PATH).map {|path| Loop.new path }
CONDITIONAL =

CONDITIONAL tests for truthiness: ‘c:variable’ => Conditional.new(false, Variable.new(), nil, nil) CONDITIONAL tests for falsiness: ‘c:!variable’ => Conditional.new(true, Variable.new(), nil, nil) CONDITIONAL tests for equality: ‘c:variable=My comic about bees’ => Conditional.new(false, Variable.new(), ‘=’, ‘My comic about bees’) CONDITIONAL tests for inequality: ‘c:variable!=My comic about bees’ => Conditional.new(false, Variable.new(), ‘!=’, ‘My comic about bees’) CONDITIONAL tests for greater than: ‘c:variable>=3’ => Conditional.new(false, Variable.new(), ‘>=’, ‘3’) CONDITIONAL tests against two variables: ‘c:variable=v:other’ => Conditional.new(false, Variable.new(), ‘=’, Variable.new())

('c:'.r >> seq_(
  /!?/.r.map {|c| c == '!' },
  PATH.map {|p| Variable.new(p) },
  optional(OPERATOR),
  optional(VARIABLE | /[^\]]+/.r))
).map {|(reversed, lhs, operator, rhs)| Conditional.new reversed, lhs, operator, rhs }
FUNCTION =

FUNCTION with no arguments: ‘f:cowsay’ => Function.new(“cowsay”, []) FUNCTION with a variable argument: ‘f:js|v:foo’ => Function.new(‘js’, [Variable.new()]) FUNCTION with literal arguments: ‘f:add|2|3’ => Function.new(‘add’, [‘2’, ‘3’])

('f:'.r >> seq(IDENT, ('|'.r >> (VARIABLE | /[^\]\|]+/.r)).star)).map {|(name, params)| Function.new name, params }
TAG =

TAG matches variable tags: ‘[v:value]’ => Variable.new() TAG matches loop tags: ‘[l:loop]’ => Loop.new() TAG matches conditional tags: ‘[c:authorname=Simon W]’ => Conditional.new(false, Variable.new(), ‘=’, ‘Simon W’) TAG matches function tags: ‘[f:js|v:dench]’ => Function.new(‘js’, [Variable.new()])

'['.r >> (VARIABLE | CONDITIONAL | LOOP | FUNCTION) << ']'.r
LAYOUT_TAG =

LAYOUT_TAG with content: ‘<!–layout:–>’ => Layout.new(‘content’) LAYOUT_TAG with css: ‘<!–layout:–>’ => Layout.new(‘css’)

('<!--layout:['.r >> IDENT << ']-->'.r).map {|(name)| Layout.new name }
END_TAG =

END_TAG matches, er, end tags: ‘[/]’ => EndTag.new

'[/]'.r.map {|_| EndTag.new }
DOCUMENT =
(TAG | END_TAG | LAYOUT_TAG | TEXT | /[\[\]\|\<\>]/.r).star.eof

Class Method Summary collapse

Class Method Details

.optional(p, default = nil) ⇒ Object



15
16
17
# File 'lib/ragerender/language.rb', line 15

def self.optional p, default=nil
  p | ''.r.map {|_| default}
end

.parse(io) ⇒ Object



71
72
73
# File 'lib/ragerender/language.rb', line 71

def self.parse io
  DOCUMENT.parse! io.read
end