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::CHAIN_CONTINUE, MacroHandler::CHAIN_END, MacroHandler::CHAIN_NONE, MacroHandler::CHAIN_START, MacroHandler::COLLECT_NORMAL, MacroHandler::COLLECT_RAW, MacroHandler::COLLECT_RAW_MACRO

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Eggshell::BlockHandler::BlockParams

#get_block_params, #set_block_params

Methods included from MacroHandler

#chain_type, #collection_type

Class Method Details

.make_include_paths(inc, opts) ⇒ Object



747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/eggshell/bundles/basics.rb', line 747

def self.make_include_paths(inc, opts)
  checks = []
  if inc[0] != '/'
    if opts[:include_paths]
      opts[: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
    end
  else
    # sandboxed root include
    if opts[:include_root]
      checks << "#{opts[:include_root]}#{inc}"
    else
      checks << inc
    end
  end
  checks
end

Instance Method Details

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



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/eggshell/bundles/basics.rb', line 720

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)

    CoreMacros.make_include_paths(inc, @vars).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



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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/eggshell/bundles/basics.rb', line 666

def process(name, args, lines, out, call_depth = 0)
  if name == '=' || name == 'var'
    # @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



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/eggshell/bundles/basics.rb', line 646

def set_processor(proc, opts = nil)
  opts = {} if !opts
  @opts = opts
  @eggshell = proc
  @eggshell.add_macro_handler(self, '=', 'var', '!', '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