Class: Eggshell::Bundles::Basic::CoreMacros

Inherits:
Object
  • Object
show all
Includes:
Eggshell::BlockHandler::BlockParams, MacroHandler
Defined in:
lib/eggshell/bundles/basics.rb

Overview

These macros are highly recommended to always be part of an instance of the processor.

dl.

{!}

sets default parameter values (block parameters) so that they don’t have to be

specified for every block instance.

{raw}

passes all block lines up into output chain. Macros are assembled before being

outputted.

{pipe}

allows blocks to pipe into the adjacent block

{process} always dynamic processing of content. This allows previous macros to build up a document dynamically.

Constant Summary collapse

CAP_OUT =
'capture_output'

Constants included from Eggshell::BlockHandler::BlockParams

Eggshell::BlockHandler::BlockParams::ATTRIBUTES, Eggshell::BlockHandler::BlockParams::CLASS, Eggshell::BlockHandler::BlockParams::ID, Eggshell::BlockHandler::BlockParams::KEYS, Eggshell::BlockHandler::BlockParams::STYLE

Constants included from MacroHandler

MacroHandler::COLLECT_NORMAL, MacroHandler::COLLECT_RAW, MacroHandler::COLLECT_RAW_MACRO

Instance Method Summary collapse

Methods included from Eggshell::BlockHandler::BlockParams

#get_block_params, #set_block_params

Methods included from MacroHandler

#collection_type

Instance Method Details

#do_include(paths, buff, call_depth, opts = {}) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/eggshell/bundles/basics.rb', line 699

def do_include(paths, buff, call_depth, opts = {})
  paths = [paths] if !paths.is_a?(Array)
  # @todo check all include paths?
  paths.each do |inc|
    inc = inc.line if inc.is_a?(Eggshell::Line)
    inc = @eggshell.expand_expr(inc.strip)
    checks = []
    if inc[0] != '/'
      @vars[:include_paths].each do |root|
        checks << "#{root}/#{inc}"
      end
      # @todo if :include_root, expand path and check that it's under the root, otherwise, sandbox
    else
      # sandboxed root include
      if @eggshell.vars[:include_root]
        checks << "#{@vars[:include_root]}#{inc}"
      else
        checks << inc
      end
    end
    checks.each do |inc|
      if File.exists?(inc)
        lines = IO.readlines(inc, $/, opts)
        @vars[:include_stack] << inc
        begin
          buff << @eggshell.process(lines, 0, call_depth + 1)
          @eggshell._debug("include: 200 #{inc}")
        rescue => ex
          @eggshell._error("include: 500 #{inc}: #{ex.message}#{ex.backtrace.join("\n\t")}")
        end
        
        @vars[:include_stack].pop
        break
      else
        @eggshell._warn("include: 404 #{inc}")
      end
    end
  end
end

#process(name, args, lines, out, call_depth = 0) ⇒ Object



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/eggshell/bundles/basics.rb', line 645

def process(name, args, lines, out, call_depth = 0)
  if name == '='
    # @todo expand args[0]?
    if args[0]
      val = nil
      if args[1].is_a?(Array) && args[1][0].is_a?(Symbol)
        val = @eggshell.expr_eval(args[1])
      else
        val = args[1]
      end
      @eggshell.vars[args[0]] = val
    end
  elsif name == '!'
    block_name = args[0]
    block_params = args[1]
    set_block_params(block_name, block_params) if block_name
  elsif name == 'process'
    
  elsif name == 'capture'
    var = args[0] || CAP_OUT
    captured = @eggshell.assemble(lines, call_depth + 1)
    captured = @eggshell.expand_expr(captured)
    @eggshell.vars[var] = captured
  elsif name == 'include'
    paths = args[0]
    opts = args[1] || {}
    if opts['encoding']
      opts[:encoding] = opts['encoding']
    else
      opts[:encoding] = 'utf-8'
    end
    if lines && lines.length > 0
      paths = lines
    end
    do_include(paths, out, call_depth, opts)
  elsif name == 'raw'
    lines.each do |unit|
      if unit.is_a?(Array)
        if unit[0] == :block
          unit[Eggshell::ParseTree::IDX_LINES].each do |line|
            out << line.to_s
          end
        else
          out << @eggshell.assemble(unit, call_depth + 1)
        end
      else
        out << line
      end
    end
  elsif name == 'pipe'
    out << @eggshell.assemble(lines, call_depth + 1)
  end
end

#set_processor(proc, opts = nil) ⇒ Object



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/eggshell/bundles/basics.rb', line 625

def set_processor(proc, opts = nil)
  opts = {} if !opts
  @opts = opts
  @eggshell = proc
  @eggshell.add_macro_handler(self, '=', '!', 'process', 'capture', 'raw', 'pipe')
  @eggshell.add_macro_handler(self, 'include') if !@opts['macro.include.off']
  @vars = @eggshell.vars

  @vars[:include_stack] = []
  if opts['include.options']
    opts['include.options'].each do |k,v|
      @vars[k.to_sym] = v
    end
  end
  
  if @vars[:include_paths].length == 0
    @vars[:include_paths] = [Dir.getwd]
  end
end