Class: Eggshell::Processor
- Inherits:
-
Object
- Object
- Eggshell::Processor
- Defined in:
- lib/eggshell/processor.rb
Constant Summary collapse
- BLOCK_MATCH =
/^([a-z0-9_-]+\.)/- BLOCK_MATCH_PARAMS =
/^([a-z0-9_-]+)\(/- TAB =
"\t"- TAB_SPACE =
' '- BH =
Eggshell::BlockHandler
- COMMENT =
'#!'- DIRECTIVE =
'#>'- PIPE_INLINE =
This string in a block indicates that a piped macro’s output should be inserted at this location rather than immediately after last line. For now, this is only checked for on the last line.
Multiple inline pipes can be specified on this line, with each pipe corresponding to each macro chained to the block. Any unfilled pipe will be replaced with a blank string.
To escape the pipe, use a backslash anywhere [AFTER] the initial dash (e.g. ‘-\>*<-`).
'->*<-'- BACKSLASH_REGEX =
/\\(u[0-9a-f]{4}|u\{[^}]+\}|.)/i- BACKSLASH_UNESCAPE_MAP =
{ 'f' => "\f", 'n' => "\n", 'r' => "\r", 't' => "\t", 'v' => "\v" }.freeze
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Class Method Summary collapse
- .parse_block_start(line) ⇒ Object
- .parse_macro_start(line) ⇒ Object
-
.unescape(str) ⇒ Object
Unescapes backslashes and Unicode characters.
Instance Method Summary collapse
- #_debug(msg) ⇒ Object
- #_error(msg) ⇒ Object
- #_info(msg) ⇒ Object
- #_trace(msg) ⇒ Object
- #_warn(msg) ⇒ Object
- #add_block_handler(handler, *names) ⇒ Object
-
#add_format_handler(handler, tags) ⇒ Object
Register inline format handlers with opening and closing tags.
- #add_macro_handler(handler, *names) ⇒ Object
-
#assemble(parse_tree, call_depth = 0, opts = {}) ⇒ Object
Goes through each item in parse tree, collecting output in the following manner:.
-
#expand_all(str) ⇒ Object
Calls inline formatting, expression extrapolator, and backslash unescape.
-
#expand_expr(expr) ⇒ Object
Expands expressions (‘${}`) and macro calls (`@@macro@@`).
-
#expand_formatting(str) ⇒ Object
Expands inline formatting with {Eggshell{Eggshell::FormatHandler}s.
- #expr_eval(struct) ⇒ Object
-
#get_block_params(block_type) ⇒ Object
Gets the block parameters for a block type, and merges default values if available.
- #get_out ⇒ Object
-
#initialize ⇒ Processor
constructor
A new instance of Processor.
- #preprocess(lines, line_count = 0) ⇒ Object
- #process(lines, line_count = 0, call_depth = 0) ⇒ Object
-
#register_functions(func_key, handler, func_names = nil) ⇒ Object
Registers a function for embedded expressions.
- #rem_block_handler(*names) ⇒ Object
- #rem_macro_handler(*names) ⇒ Object
-
#set_block_params(params, is_default = false, block_type = nil) ⇒ Object
block_typeused in ‘get_block_param()` or explicitly in third parameter. -
#set_out(out) ⇒ Object
Sets the default output object.
- #unescape(str) ⇒ Object
Constructor Details
#initialize ⇒ Processor
Returns a new instance of Processor.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/eggshell/processor.rb', line 6 def initialize @context = Eggshell::ProcessorContext.new @vars = @context.vars @funcs = @context.funcs @macros = @context.macros @blocks = @context.blocks @blocks_map = @context.blocks_map @block_params = @context.block_params @expr_cache = @context.expr_cache @fmt_handlers = @context.fmt_handlers @ee = Eggshell::ExpressionEvaluator.new(@vars, @funcs) @noop_macro = Eggshell::MacroHandler::Defaults::NoOpHandler.new @noop_block = Eggshell::BlockHandler::Defaults::NoOpHandler.new end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
22 23 24 |
# File 'lib/eggshell/processor.rb', line 22 def context @context end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
90 91 92 |
# File 'lib/eggshell/processor.rb', line 90 def vars @vars end |
Class Method Details
.parse_block_start(line) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/eggshell/processor.rb', line 611 def self.parse_block_start(line) block_type = nil args = [] bt = line.match(BLOCK_MATCH_PARAMS) if bt idx0 = bt[0].length idx1 = line.index(').', idx0) if idx1 block_type = line[0..idx0-2] params = line[0...idx1+1].strip line = line[idx1+2..line.length] || '' if params != '' struct = ExpressionEvaluator.struct(params) args = struct[0][2] end end else block_type = line.match(BLOCK_MATCH) if block_type && block_type[0].strip != '' block_type = block_type[1] len = block_type.length block_type = block_type[0..-2] if block_type[-1] == '.' line = line[len..line.length] || '' else block_type = nil end end [block_type, args, line] end |
.parse_macro_start(line) ⇒ Object
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/eggshell/processor.rb', line 643 def self.parse_macro_start(line) macro = nil args = [] delim = nil # either macro is a plain '@macro' or it has parameters/opening brace if line.index(' ') || line.index('(') || line.index('{') # since the macro statement is essentially a function call, parse the line as an expression to get components expr_struct = ExpressionEvaluator.struct(line) fn = expr_struct.shift if fn.is_a?(Array) && (fn[0] == :fn || fn[0] == :var) macro = fn[1][1..fn[1].length] args = fn[2] if expr_struct[-1].is_a?(Array) && expr_struct[-1][0] == :brace_op delim = expr_struct[-1][1] end end else macro = line[1..line.length] end [macro, args, delim] end |
.unescape(str) ⇒ Object
Unescapes backslashes and Unicode characters.
If a match is made against {BACKSLASH_UNESCAPE_MAP} that character will be used, otherwise, the literal is used.
Unicode sequences are standard Ruby-like syntax: {uabcd} or seq2 …}}.
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 |
# File 'lib/eggshell/processor.rb', line 682 def self.unescape(str) str = str.gsub(BACKSLASH_REGEX) do |match| if match.length == 2 c = match[1] BACKSLASH_UNESCAPE_MAP[c] || c else if match[2] == '{' parts = match[3..-1].split(' ') buff = '' parts.each do |part| buff += [part.to_i(16)].pack('U') end buff else [match[2..-1].to_i(16)].pack('U') end end end end |
Instance Method Details
#_debug(msg) ⇒ Object
80 81 82 83 |
# File 'lib/eggshell/processor.rb', line 80 def _debug(msg) return if @vars['log.level'] < 2 $stderr.write("[DEBUG] #{msg}\n") end |
#_error(msg) ⇒ Object
67 68 69 |
# File 'lib/eggshell/processor.rb', line 67 def _error(msg) $stderr.write("[ERROR] #{msg}\n") end |
#_info(msg) ⇒ Object
75 76 77 78 |
# File 'lib/eggshell/processor.rb', line 75 def _info(msg) return if @vars['log.level'] < 1 $stderr.write("[INFO] #{msg}\n") end |
#_trace(msg) ⇒ Object
85 86 87 88 |
# File 'lib/eggshell/processor.rb', line 85 def _trace(msg) return if @vars['log.level'] < 3 $stderr.write("[TRACE] #{msg}\n") end |
#_warn(msg) ⇒ Object
71 72 73 |
# File 'lib/eggshell/processor.rb', line 71 def _warn(msg) $stderr.write("[WARN] #{msg}\n") end |
#add_block_handler(handler, *names) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/eggshell/processor.rb', line 24 def add_block_handler(handler, *names) _trace "add_block_handler: #{names.inspect} -> #{handler.class}" @blocks << handler names.each do |name| @blocks_map[name] = handler end end |
#add_format_handler(handler, tags) ⇒ Object
if opening tag is regex, don’t escape (but make sure it doesn’t contain {^} or {$})
Register inline format handlers with opening and closing tags. Typically, tags can be arbitrarily nested. However, nesting can be shut off completely or selectively by specifying 0 or more tags separated by a space (empty string is completely disabled).
following form: close[, non_nest]]}
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'lib/eggshell/processor.rb', line 534 def add_format_handler(handler, ) return if !.is_a?(Array) .each do |entry| open, close, no_nest = entry no_nest = '' if no_nest.is_a?(TrueClass) @fmt_handlers[open] = [handler, close, no_nest] _trace "add_format_handler: #{open} #{close} (non-nested: #{no_nest.inspect})" end # regenerate splitting pattern going from longest to shortest openers = @fmt_handlers.keys.sort do |a, b| b.length <=> a.length end regex = '' openers.each do |op| regex = "#{regex}|#{Regexp.quote(op)}|#{Regexp.quote(@fmt_handlers[op][1])}" end @fmt_regex = /(\\|'|"#{regex})/ end |
#add_macro_handler(handler, *names) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/eggshell/processor.rb', line 40 def add_macro_handler(handler, *names) _trace "add_macro_handler: #{names.inspect} -> #{handler.class}" names.each do |name| @macros[name] = handler end end |
#assemble(parse_tree, call_depth = 0, opts = {}) ⇒ Object
Goes through each item in parse tree, collecting output in the following manner:
# {String}s and {Line}s are outputted as-is # macros and blocks with matching handlers get {process} called
All output is joined with ‘\n` by default.
The output object and join string can be overridden through the {opts} parameter keys {:out} and {:joiner}. 3
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/eggshell/processor.rb', line 414 def assemble(parse_tree, call_depth = 0, opts = {}) opts = {} if !opts.is_a?(Hash) out = opts[:out] || get_out joiner = opts[:join] || "\n" parse_tree = parse_tree.tree if parse_tree.is_a?(Eggshell::ParseTree) raise Exception.new("input not an array or ParseTree (depth=#{call_depth}") if !parse_tree.is_a?(Array) # @todo defer process to next unit so macro can inject lines back into previous block last_type = nil last_line = 0 deferred = nil parse_tree.each do |unit| if unit.is_a?(String) out << unit last_line += 1 elsif unit.is_a?(Eggshell::Line) out << unit.to_s last_line = unit.line_nameum elsif unit.is_a?(Array) handler = unit[0] == :block ? @blocks_map[unit[1]] : @macros[unit[1]] name = unit[1] if !handler _warn "handler not found: #{unit[0]} -> #{unit[1]}" next end args_o = unit[2] || [] args = [] args_o.each do |arg| args << expr_eval(arg) end lines = unit[ParseTree::IDX_LINES] lines_start = unit[ParseTree::IDX_LINES_START] lines_end = unit[ParseTree::IDX_LINES_END] _handler, _name, _args, _lines = deferred if unit[0] == :block if deferred # two cases: # 1. this block is immediately tied to block-macro chain and is continuation of same type of block # 2. part of block-macro chain but not same type, or immediately follows another block if last_type == :macro && (lines_end - last_line == 1) && _name == name lines.each do |line| _lines << line end else _handler.process(_name, _args, _lines, out, call_depth) deferred = [handler, name, args, lines.clone] end else deferred = [handler, name, args, lines.clone] end last_line = lines_end else # macro immediately after a block, so assume that output gets piped into last lines # of closest block if deferred && lines_start - last_line == 1 _last = _lines[-1] pinline = false pipe = _lines if _last.to_s.index(PIPE_INLINE) pipe = [] pinline = true end handler.process(name, args, lines, pipe, call_depth) # inline pipe; join output with literal \n to avoid processing lines in block process if pinline if _last.is_a?(Eggshell::Line) _lines[-1] = _last.replace(_last.line.sub(PIPE_INLINE, pipe.join('\n'))) else _lines[-1] = _last.sub(PIPE_INLINE, pipe.join('\n')) end end else if deferred _handler.process(_name, _args, _lines, out, call_depth) deferred = nil end handler.process(name, args, lines, out, call_depth) end last_line = lines_end end last_type = unit[0] elsif unit _warn "not sure how to handle #{unit.class}" _debug unit.inspect end end if deferred _handler, _name, _args, _lines = deferred _handler.process(_name, _args, _lines, out, call_depth) deferred = nil end out.join(joiner) end |
#expand_all(str) ⇒ Object
Calls inline formatting, expression extrapolator, and backslash unescape.
707 708 709 |
# File 'lib/eggshell/processor.rb', line 707 def (str) unescape(((str))) end |
#expand_expr(expr) ⇒ Object
deprecate @@macro@@?
Expands expressions (‘${}`) and macro calls (`@@macro@@`).
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/eggshell/processor.rb', line 98 def (expr) # replace dynamic placeholders # @todo expand to actual expressions buff = [] esc = false exp = false mac = false toks = expr.split(/(\\|\$\{|\}|@@|"|')/) i = 0 plain_str = '' expr_str = '' quote = nil expr_delim = nil while i < toks.length tok = toks[i] i += 1 next if tok == '' if esc plain_str += '\\' + tok esc = false next end if exp if quote expr_str += tok if tok == quote quote = nil end elsif tok == '"' || tok == "'" expr_str += tok quote = tok elsif tok == expr_delim struct = @expr_cache[expr_str] if !struct struct = Eggshell::ExpressionEvaluator.struct(expr_str) @expr_cache[expr_str] = struct end if !mac buff << expr_eval(struct) else args = struct[0] macro = args[1] args = args[2] || [] macro_handler = @macros[macro] if macro_handler macro_handler.process(buff, macro, args, nil, -1) else _warn("macro (inline) not found: #{macro}") end end exp = false mac = false expr_delim = nil expr_str = '' else expr_str += tok end # only unescape if not in expression, since expression needs to be given as-is elsif tok == '\\' esc = true next elsif tok == '${' || tok == '@@' if plain_str != '' buff << plain_str plain_str = '' end exp = true expr_delim = '}' if tok == '@@' mac = true expr_delim = tok end else plain_str += tok end end # if exp -- throw exception? buff << plain_str if plain_str != '' return buff.join('') end |
#expand_formatting(str) ⇒ Object
Expands inline formatting with Eggshell::Processor.{Eggshell{Eggshell::FormatHandler}s.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/eggshell/processor.rb', line 558 def (str) toks = str.gsub(PIPE_INLINE, '').split(@fmt_regex) toks.delete('') buff = [''] quote = nil opened = [] closing = [] non_nesting = [] i = 0 while i < toks.length tok = toks[i] i += 1 if tok == '\\' # preserve escape char otherwise we lose things like \n or \t buff[-1] += tok + toks[i] i += 1 elsif quote quote = nil if tok == quote buff[-1] += tok elsif tok == '"' || tok == "'" quote = tok if opened[-1] buff[-1] += tok elsif @fmt_handlers[tok] && (!non_nesting[-1] || non_nesting.index(tok)) handler, closer, non_nest = @fmt_handlers[tok] opened << tok closing << closer non_nesting << non_nest buff << '' elsif tok == closing[-1] opener = opened.pop handler = @fmt_handlers[opener][0] closing.pop non_nesting.pop # @todo insert placeholder and swap out at end? might be a prob if value has to be escaped bstr = buff.pop buff[-1] += handler.format(opener, bstr) else buff[-1] += tok end end opened.each do |op| bstr = buff.pop buff[-1] += op + bstr _warn "expand_formatting: unclosed #{op}, not doing anything: #{bstr}" end buff.join('') end |
#expr_eval(struct) ⇒ Object
92 93 94 |
# File 'lib/eggshell/processor.rb', line 92 def expr_eval(struct) return Eggshell::ExpressionEvaluator.expr_eval(struct, @vars, @funcs) end |
#get_block_params(block_type) ⇒ Object
Gets the block parameters for a block type, and merges default values if available.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/eggshell/processor.rb', line 204 def get_block_params(block_type) bp = @block_params.delete(:pending) if @block_params_default if block_type && bp @block_params[block_type] = bp if bp end @block_params_default = false bp = {} if !bp else bp = {} if !bp default = @block_params[block_type] if default default.each do |key,val| if !bp.has_key?(key) && val bp[key] = val.clone end end end end return bp end |
#get_out ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'lib/eggshell/processor.rb', line 233 def get_out if !@out [] elsif @out.is_a?(Class) @out.new else @out end end |
#preprocess(lines, line_count = 0) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/eggshell/processor.rb', line 248 def preprocess(lines, line_count = 0) line_start = line_count line_buff = nil indent = 0 mode = nil in_html = false end_html = nil parse_tree = Eggshell::ParseTree.new """ algorithm for normalizing lines: - skip comments (process directive if present) - if line is continuation, set current line = last + current - if line ends in \ and is not blank otherwise, set new continuation and move to next line - if line ends in \ and is effectively blank, append '\n' - calculate indent level """ i = 0 begin while i < lines.length oline = lines[i] i += 1 line_count += 1 hdr = oline.lstrip[0..1] if hdr == COMMENT next end line = oline.chomp line_end = oline[line.length..-1] if line_buff line_buff += line line = line_buff line_buff = nil else line_start += 1 end _hard_return = false # if line ends in a single \, either insert hard return into current block (with \n) # or init line_buff to collect next line if line[-1] == '\\' if line[-2] != '\\' nline = line[0...-1] # check if line is effectively blank, but add leading whitespace back # to maintain tab processing if nline.strip == '' line = "#{nline}\n" line_end = '' _hard_return = true else line_buff = nline next end end end # detect tabs (must be consistent per-line) _ind = 0 tab_str = line[0] == TAB ? TAB : nil tab_str = line.index(TAB_SPACE) == 0 ? TAB_SPACE : nil if !tab_str indent_str = '' if tab_str _ind += 1 _len = tab_str.length _pos = _len while line.index(tab_str, _pos) _pos += _len _ind += 1 end line = line[_pos..-1] # trim indent chars based on block_handler_indent if indent > 0 _ind -= indent _ind = 0 if _ind < 0 end end line_norm = Line.new(line, tab_str, _ind, line_start, oline.chomp) line_start = line_count if parse_tree.mode == :raw stat = parse_tree.collect(line_norm) next if stat != BH::RETRY parse_tree.push_block end # macro processing if line[0] == '@' parse_tree.new_macro(line_norm, line_count) next elsif parse_tree.macro_delim_match(line_norm, line_count) next end if parse_tree.mode == :block stat = parse_tree.collect(line_norm) if stat == BH::RETRY parse_tree.push_block else next end end # blank line and not in block if line == '' parse_tree.push_block next end found = false @blocks.each do |handler| stat = handler.can_handle(line) next if stat == BH::RETRY parse_tree.new_block(handler, handler.current_type, line_norm, stat, line_count) found = true _trace "(#{handler.current_type}->#{handler}) #{line} -> #{stat}" break end if !found @blocks_map['p'].can_handle('p.') parse_tree.new_block(@blocks_map['p'], 'p', line_norm, BH::COLLECT, line_count) end end parse_tree.push_block # @todo check if macros left open rescue => ex _error "Exception approximately on line: #{line}" _error ex. + "\t#{ex.backtrace.join("\n\t")}" #_error "vars = #{@vars.inspect}" end parse_tree end |
#process(lines, line_count = 0, call_depth = 0) ⇒ Object
521 522 523 524 |
# File 'lib/eggshell/processor.rb', line 521 def process(lines, line_count = 0, call_depth = 0) parse_tree = preprocess(lines, line_count) assemble(parse_tree.tree, call_depth) end |
#register_functions(func_key, handler, func_names = nil) ⇒ Object
Registers a function for embedded expressions. Functions are grouped into namespaces, and a handler can be assigned to handle all function calls within that namespace, or a specific set of functions within the namespace. The root namespace is a blank string.
root namespace, do :func_name. needs to only handle a subset of functions, supply the list of function names here.
63 64 65 |
# File 'lib/eggshell/processor.rb', line 63 def register_functions(func_key, handler, func_names = nil) @ee.register_functions(func_key, handler, func_names) end |
#rem_block_handler(*names) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/eggshell/processor.rb', line 32 def rem_block_handler(*names) _trace "rem_block_handler: #{names.inspect}" names.each do |name| handler = @blocks_map.delete(name) @blocks.delete(handler) end end |
#rem_macro_handler(*names) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/eggshell/processor.rb', line 47 def rem_macro_handler(*names) _trace "rem_macro_handler: #{names.inspect}" names.each do |name| @macros.delete(name) end end |
#set_block_params(params, is_default = false, block_type = nil) ⇒ Object
block_type used in ‘get_block_param()` or explicitly in third parameter.
194 195 196 197 198 199 200 201 |
# File 'lib/eggshell/processor.rb', line 194 def set_block_params(params, is_default = false, block_type = nil) if block_type && is_default @block_params[block_type] = params else @block_params[:pending] = params @block_param_default = is_default end end |
#set_out(out) ⇒ Object
Sets the default output object. Must support {<<} and {join(String)}.
If {out} is a Class, must support empty initialization.
229 230 231 |
# File 'lib/eggshell/processor.rb', line 229 def set_out(out) @out = out end |
#unescape(str) ⇒ Object
702 703 704 |
# File 'lib/eggshell/processor.rb', line 702 def unescape(str) return self.class.unescape(str) end |