Class: Spider::TemplateBlocks::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/templates/template_blocks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(el, template = nil, allowed_blocks = nil) ⇒ Block

Returns a new instance of Block.



95
96
97
98
99
100
# File 'lib/spiderfw/templates/template_blocks.rb', line 95

def initialize(el, template=nil, allowed_blocks=nil)
    @el = el
    @template = template
    @allowed_blocks = allowed_blocks
    @content_blocks = []
end

Instance Attribute Details

#allowed_blocksObject (readonly)

Returns the value of attribute allowed_blocks.



92
93
94
# File 'lib/spiderfw/templates/template_blocks.rb', line 92

def allowed_blocks
  @allowed_blocks
end

#doctypeObject

Returns the value of attribute doctype.



93
94
95
# File 'lib/spiderfw/templates/template_blocks.rb', line 93

def doctype
  @doctype
end

#elObject (readonly)

Returns the value of attribute el.



92
93
94
# File 'lib/spiderfw/templates/template_blocks.rb', line 92

def el
  @el
end

#templateObject (readonly)

Returns the value of attribute template.



92
93
94
# File 'lib/spiderfw/templates/template_blocks.rb', line 92

def template
  @template
end

Class Method Details

.var_to_scene(var, container = 'self') ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/spiderfw/templates/template_blocks.rb', line 165

def self.var_to_scene(var, container='self')
    first, rest = var.split('.', 2)
    if (first =~ /([^\[]+)(\[.+)/)
        var_name = $1
        array_rest = $2
    else
        var_name = first
    end
    if (var[0].chr == '@')
        scene_var = "#{container}[:#{var_name[1..-1]}]"
    else
        scene_var = var_name
    end
    scene_var += array_rest if (array_rest)
    scene_var += '.'+rest if (rest)
    return scene_var
end

.vars_to_scene(str, container = 'self') ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/spiderfw/templates/template_blocks.rb', line 122

def self.vars_to_scene(str, container='self')
    res = ""
    Spider::Template.scan_scene_vars(str) do |type, val|
        case type
        when :plain
            res += val
        when :var
            res += "#{container}[:#{val}]"
        end
    end
    res
end

Instance Method Details

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



106
107
108
109
110
# File 'lib/spiderfw/templates/template_blocks.rb', line 106

def compile_content(c='', init='', options={})
    options[:allowed_blocks] ||= @allowed_blocks
    options[:template] ||= @template
    TemplateBlocks.compile_content(@el, c, init, options)
end

#compile_text(str) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/spiderfw/templates/template_blocks.rb', line 139

def compile_text(str)
    res = ""
    Spider::Template.scan_text(str) do |type, val, full|
        case type
        when :plain
            res += escape_text(val)
        when :escaped_expr
            res += "{ #{escape_text(val)} }"
        when :expr
            res += "'+("+vars_to_scene(val)+").to_s+'"
        when :gettext
            res += "'\n$out << _('#{escape_text(val[:val])}')"
            if val[:vars]
                res += " #{vars_to_scene(val[:vars])}" 
            end
            res += "\n$out << '"
        end
    end
    res
end

#escape_text(text) ⇒ Object



116
117
118
119
120
# File 'lib/spiderfw/templates/template_blocks.rb', line 116

def escape_text(text)
    res = text.gsub(/_(\\*)\\\((.+?)\)/, '_\1(\2)') \
        .gsub('\\', '\\\\\\').gsub("'", "\\\\'")
    return res
end

#get_following(el) ⇒ Object



112
113
114
# File 'lib/spiderfw/templates/template_blocks.rb', line 112

def get_following(el)
    return false
end

#inspectObject



161
162
163
# File 'lib/spiderfw/templates/template_blocks.rb', line 161

def inspect
    @el
end

#parse_content(el) ⇒ Object



102
103
104
# File 'lib/spiderfw/templates/template_blocks.rb', line 102

def parse_content(el)
    TemplateBlocks.parse_content(el, @allowed_blocks, @template)
end

#var_to_scene(var, container = 'self') ⇒ Object



183
184
185
# File 'lib/spiderfw/templates/template_blocks.rb', line 183

def var_to_scene(var, container='self')
    self.class.var_to_scene(var, container)
end

#vars_to_scene(str, container = 'self') ⇒ Object



135
136
137
# File 'lib/spiderfw/templates/template_blocks.rb', line 135

def vars_to_scene(str, container='self')
    self.class.vars_to_scene(str, container)
end