Module: Spider::TemplateBlocks

Defined in:
lib/spiderfw/templates/blocks/if.rb,
lib/spiderfw/templates/blocks/run.rb,
lib/spiderfw/templates/blocks/tag.rb,
lib/spiderfw/templates/blocks/each.rb,
lib/spiderfw/templates/blocks/html.rb,
lib/spiderfw/templates/blocks/pass.rb,
lib/spiderfw/templates/blocks/text.rb,
lib/spiderfw/templates/blocks/yield.rb,
lib/spiderfw/templates/blocks/lambda.rb,
lib/spiderfw/templates/blocks/output.rb,
lib/spiderfw/templates/blocks/render.rb,
lib/spiderfw/templates/blocks/tag_if.rb,
lib/spiderfw/templates/blocks/widget.rb,
lib/spiderfw/templates/blocks/attr_if.rb,
lib/spiderfw/templates/blocks/comment.rb,
lib/spiderfw/templates/blocks/recurse.rb,
lib/spiderfw/templates/blocks/debugger.rb,
lib/spiderfw/templates/template_blocks.rb,
lib/spiderfw/templates/blocks/layout_assets.rb,
lib/spiderfw/templates/blocks/parent_context.rb

Defined Under Namespace

Classes: AttrIf, Block, Comment, CompiledBlock, Debugger, Each, HTML, If, Lambda, LayoutAssets, Output, ParentContext, Pass, Recurse, Render, Run, Tag, TagIf, Text, Widget, Yield

Constant Summary collapse

ExpressionOutputRegexp =
/\{?\{\s([^\s].*?)\s\}\}?/
GettextRegexp =
/_\(([^\)]+)?\)(\s%\s([^\s,]+(?:,\s*\S+\s*)?))?/
ERBRegexp =
/(<%(.+)?%>)/

Class Method Summary collapse

Class Method Details

.compile_content(el, c = '', init = '', options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spiderfw/templates/template_blocks.rb', line 74

def self.compile_content(el, c='', init='', options={})
    c ||= ""
    init ||= ""
    blocks = self.parse_content(el, options[:allowed_blocks], options[:template])
    blocks.each do |block|
        compiled = block.compile(options)
        next unless compiled
        c += compiled.run_code if (compiled.run_code)
        init += compiled.init_code if (compiled.init_code)
    end
    return [c, init]
end

.get_block_type(el, skip_attributes = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/spiderfw/templates/template_blocks.rb', line 12

def self.get_block_type(el, skip_attributes=false)
    if el.class == ::Hpricot::Text
        block = :Text
    elsif el.class == ::Hpricot::Comment
        block = :Comment
    elsif !skip_attributes && el.has_attribute?('sp:lambda')
        block = :Lambda
    elsif !skip_attributes && (el.has_attribute?('sp:if') || el.has_attribute?('sp:run-if'))
        block = :If
    elsif !skip_attributes && el.has_attribute?('sp:tag-if')
        block = :TagIf
    elsif !skip_attributes && el.has_attribute?('sp:attr-if')
        block = :AttrIf
    elsif !skip_attributes && (el.has_attribute?('sp:each') || el.has_attribute?('sp:each_index') \
                            || el.has_attribute?('sp:each_pair') || el.has_attribute?('sp:each_with_index'))
        block = :Each
    elsif el.name == 'tpl:output'
        block = :Output
    elsif el.name == 'tpl:output-assets'
        block = :LayoutAssets    
    elsif el.name == 'sp:render'
        block = :Render
    elsif el.name == 'sp:run'
        block = :Run
    elsif el.name == 'sp:yield'
        block = :Yield
    elsif el.name == 'sp:pass' || el.name == 'tpl:pass' || el.name == 'sp:template'
        block = :Pass
    elsif el.name == 'sp:debugger'
        block = :Debugger
    elsif el.name == 'sp:parent-context'
        block = :ParentContext
    elsif el.name == 'sp:recurse'
        block = :Recurse
    elsif Spider::Template.registered?(el.name)
        klass = Spider::Template.get_registered_class(el.name)
        if klass < ::Spider::Widget
            block = :Widget
        elsif klass < Spider::Tag
            block = :Tag
        else
            Spider.logger.error("Could not parse #{el.name} tag")
        end
    else
        block = :HTML
    end
    return block
end

.parse_content(el, allowed_blocks = nil, template = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/spiderfw/templates/template_blocks.rb', line 61

def self.parse_content(el, allowed_blocks=nil, template=nil)
    content_blocks = []
    last_block = nil
    el.each_child do |ch|
        #Spider.logger.debug "TRAVERSING CHILD #{ch}"
        # Gives the preceding block the chance to "eat" the next elements
        next if (last_block && last_block.get_following(ch))
        last_block = TemplateBlocks.parse_element(ch, allowed_blocks, template)
        content_blocks << last_block if (last_block)
    end
    return content_blocks
end

.parse_element(el, allowed_blocks = nil, template = nil) ⇒ Object



5
6
7
8
9
10
# File 'lib/spiderfw/templates/template_blocks.rb', line 5

def self.parse_element(el, allowed_blocks=nil, template=nil)
    return nil if (el.class == ::Hpricot::BogusETag)
    block = get_block_type(el)
    return nil unless (!allowed_blocks || allowed_blocks.include?(block))
    return const_get(block).new(el, template, allowed_blocks)
end