Class: Deplate::Template
- Inherits:
-
Object
- Object
- Deplate::Template
- Defined in:
- lib/deplate/template.rb
Constant Summary collapse
- @@templateKeys =
['pre', 'body', 'post', 'doc', 'ARG', 'arg', 'clip']
- @@deplate_for_template =
nil- @@deplate_options =
{ :formatter => Deplate::Formatter::Template, :onthefly_particles => false, :vanilla => true, :inherit_options => true, :paragraph_class => Deplate::Element::Paragraph, :elements => [ Deplate::Element::Region, Deplate::Element::Command, Deplate::Element::Whitespace, ], :commands => { 'INC' => Deplate::Command::INC, 'INCLUDE' => Deplate::Command::INC, 'GET' => Deplate::Command::GET, 'ARG' => Deplate::Command::ARG, 'XARG' => Deplate::Command::XARG, 'VAL' => Deplate::Command::ARG, 'XVAL' => Deplate::Command::XARG, 'DOC' => Deplate::Command::VAR, 'VAR' => Deplate::Command::VAR, 'OPT' => Deplate::Command::OPT, 'PROP' => Deplate::Command::OPT, 'PP' => Deplate::Command::OPT, 'PREMATTER' => Deplate::Command::PREMATTER, 'POSTMATTER' => Deplate::Command::POSTMATTER, 'BODY' => Deplate::Command::BODY, 'WITH' => Deplate::Command::WITH, }, :regions => { 'Foreach' => Deplate::Regions::Foreach, 'For' => Deplate::Regions::Foreach, 'Mingle' => Deplate::Regions::Mingle, 'Native' => Deplate::Regions::Native, 'Ruby' => Deplate::Regions::Ruby, 'Doc' => Deplate::Regions::Var, 'Var' => Deplate::Regions::Var, }, :particles => [ Deplate::Particle::Escaped, Deplate::Particle::Macro, Deplate::Particle::Whitespace, ], :macros => { 'get' => Deplate::Macro::Clip, 'clip' => Deplate::Macro::Clip, 'opt' => Deplate::Macro::Opt, 'arg' => Deplate::Macro::Arg, 'xarg' => Deplate::Macro::XArg, 'val' => Deplate::Macro::Arg, 'xval' => Deplate::Macro::XArg, 'var' => Deplate::Macro::Var, 'doc' => Deplate::Macro::Var, 'ruby' => Deplate::Macro::Ruby, 'msg' => Deplate::Macro::Msg, 'date' => Deplate::Macro::Date, }, }
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#mingled ⇒ Object
readonly
Returns the value of attribute mingled.
-
#post ⇒ Object
readonly
Returns the value of attribute post.
-
#pre ⇒ Object
readonly
Returns the value of attribute pre.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
Instance Method Summary collapse
- #fill_in(deplate, args = {}) ⇒ Object
- #fill_in_1 ⇒ Object
- #fill_in_2 ⇒ Object
- #gen_range(string) ⇒ Object
-
#initialize(args) ⇒ Template
constructor
A new instance of Template.
-
#process_arg(arg) ⇒ Object
alias :process_arg :process_doc.
- #process_ARG(arg) ⇒ Object
- #process_body(arg) ⇒ Object
- #process_clip(arg) ⇒ Object
- #process_doc(arg) ⇒ Object
- #process_post(arg) ⇒ Object
- #process_pre(arg) ⇒ Object
- #rx_ARG ⇒ Object
- #rx_arg ⇒ Object
- #rx_body ⇒ Object
- #rx_clip ⇒ Object
- #rx_doc ⇒ Object
- #rx_post ⇒ Object
- #rx_pre ⇒ Object
- #template_get_content(type, ranges) ⇒ Object
Constructor Details
#initialize(args) ⇒ Template
Returns a new instance of Template.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/deplate/template.rb', line 192 def initialize(args) @source = args[:source] @template = args[:template] @container = args[:container] @master = args[:master] # @deplate_options = @@deplate_options.dup @deplate_options = @@deplate_options if @master @deplate_options[:options] = @master..dup @deplate_options[:options].input_def = nil @deplate_options[:options].input_class = nil else @deplate_options[:options] = OpenStruct.new end if @template return end file = args[:file] if file if File.exists?(file) begin File.open(file) do |io| @template = io.read end rescue Exception => e Deplate::DeplateForTemplates.log([e.], :error, @source) @template = '' end else Deplate::DeplateForTemplates.log(['Template not found', file], :error, @source) end else Deplate::DeplateForTemplates.log('No template defined', :error, @source) end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
190 191 192 |
# File 'lib/deplate/template.rb', line 190 def body @body end |
#mingled ⇒ Object (readonly)
Returns the value of attribute mingled.
99 100 101 |
# File 'lib/deplate/template.rb', line 99 def mingled @mingled end |
#post ⇒ Object (readonly)
Returns the value of attribute post.
190 191 192 |
# File 'lib/deplate/template.rb', line 190 def post @post end |
#pre ⇒ Object (readonly)
Returns the value of attribute pre.
190 191 192 |
# File 'lib/deplate/template.rb', line 190 def pre @pre end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
190 191 192 |
# File 'lib/deplate/template.rb', line 190 def template @template end |
Class Method Details
.copy(deplate, src, dest, invoker = nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/deplate/template.rb', line 172 def copy(deplate, src, dest, invoker=nil) tpl = File.open(src) {|io| io.read} src = invoker ? invoker.source : nil tpl = Deplate::Template.new(:template => tpl, :source => src, :container => self) args = {} if block_given? yield(args) end Deplate::Define.let_variables(deplate, args) do tpl = tpl.fill_in(deplate, :source => src) end tpl = tpl.join("\n") File.open(dest, 'w') {|io| io.puts(tpl)} end |
.deplate_options ⇒ Object
168 169 170 |
# File 'lib/deplate/template.rb', line 168 def @@deplate_options end |
Instance Method Details
#fill_in(deplate, args = {}) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/deplate/template.rb', line 229 def fill_in(deplate, args={}) @deplate = deplate @output = args[:output] @pre = args[:pre] @body = args[:body] @post = args[:post] @source = args[:source] @consumed = { :pre => [], :body => [], :post => [], } @keep_whitespace = args.has_key?(:keep_whitespace) ? args[:keep_whitespace] : true case deplate.variables['template_version'] when '1' fill_in_1 else fill_in_2 end end |
#fill_in_1 ⇒ Object
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 |
# File 'lib/deplate/template.rb', line 290 def fill_in_1 accum = [] template = @template for key in @@templateKeys loop do m = send("rx_" + key).match(template) if m accum << m.pre_match template = m.post_match begin accum << send("process_" + key, m[3]) rescue Deplate::TemplateError Deplate::DeplateForTemplates.log(["Template definition error", m[0]], :error, @source) rescue Exception => err Deplate::DeplateForTemplates.log(["Template error", m[0], err], :error, @source) end else accum << template break end end template = accum.join accum = [] end template.gsub!(/\\([{}])/, "\\1") return [template] end |
#fill_in_2 ⇒ Object
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 |
# File 'lib/deplate/template.rb', line 250 def fill_in_2 if @@deplate_for_template d = @@deplate_for_template else d = @@deplate_for_template = Deplate::DeplateForTemplates.new('', @deplate_options) d.reset(true) d.push_input_format('template') end keep_whitespace = d..keep_whitespace master = d..master template_manager = d..template_manager begin d.reset d.variables = @deplate.variables.dup # d.doc_services = @deplate.doc_services d.set_all_clips(@deplate.get_unprocessed_clips) d..template_manager = self d..keep_whitespace = @keep_whitespace d..master = @master d..content_output = @output d..content_pre_matter = @pre d..content_body = @body d..content_post_matter = @post @mingled = {} if @source ln = @source.begin fn = @source.file else ln = nil fn = nil end rv = [d.printable_strings(@template, ln, fn)] return rv ensure d..keep_whitespace = keep_whitespace d..master = master d..template_manager = template_manager end end |
#gen_range(string) ⇒ Object
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 391 |
# File 'lib/deplate/template.rb', line 350 def gen_range(string) add = [] del = [] if string and !string.empty? for i in string.split(/\s+/) n = i.split(/\.\./).collect do |s| negative = s =~ /^-/ s = s[1..-1] if negative unless s =~ /^\d+$/ s = @deplate.slot_names[s.intern] if s == 0 Deplate::DeplateForTemplates.log("Slot is zero, which is most likely an error", :anyway, @source) end end s = s.to_i negative ? -s : s end a, b, rest = n if rest Deplate::DeplateForTemplates.log(["Bad range definition", i], :error, @source) else if b == 0 b = 100 end if a < 0 arr = del else arr = add end case n.size when 1 arr << a.abs when 2 arr << Range.new(a.abs, b.abs) else Deplate::DeplateForTemplates.log(["Bad range definition", i], :error, @source) end end end end return add.compact, del.compact end |
#process_arg(arg) ⇒ Object
alias :process_arg :process_doc
443 444 445 446 447 448 449 450 451 452 |
# File 'lib/deplate/template.rb', line 443 def process_arg(arg) if arg text = @deplate.variables[arg] if text container = @container || Deplate::PseudoContainer.new(@deplate, :source => @source) return @deplate.parse_and_format(container, text) end end raise Deplate::TemplateError.new end |
#process_ARG(arg) ⇒ Object
429 430 431 432 433 434 435 436 437 |
# File 'lib/deplate/template.rb', line 429 def process_ARG(arg) if arg text = @deplate.variables[arg] if text return @deplate.printable_strings(text, @source.begin, @source.file) end end raise Deplate::TemplateError.new end |
#process_body(arg) ⇒ Object
411 412 413 |
# File 'lib/deplate/template.rb', line 411 def process_body(arg) template_get_content(@body, arg) end |
#process_clip(arg) ⇒ Object
457 458 459 460 461 462 463 464 465 |
# File 'lib/deplate/template.rb', line 457 def process_clip(arg) if arg c = @deplate.get_clip(arg) if c return c.elt end end raise Deplate::TemplateError.new end |
#process_doc(arg) ⇒ Object
418 419 420 421 422 423 424 |
# File 'lib/deplate/template.rb', line 418 def process_doc(arg) if arg @deplate.variables[arg] else raise Deplate::TemplateError.new end end |
#process_post(arg) ⇒ Object
404 405 406 |
# File 'lib/deplate/template.rb', line 404 def process_post(arg) template_get_content(@post, arg) end |
#process_pre(arg) ⇒ Object
397 398 399 |
# File 'lib/deplate/template.rb', line 397 def process_pre(arg) template_get_content(@pre, arg) end |
#rx_ARG ⇒ Object
426 427 428 |
# File 'lib/deplate/template.rb', line 426 def rx_ARG /^[ \t]*#(ARG)(:\s*([^}\s]+)\s*)?$/ end |
#rx_arg ⇒ Object
439 440 441 |
# File 'lib/deplate/template.rb', line 439 def rx_arg /\{(arg)(:\s*([^}\s]+)\s*)\}/ end |
#rx_body ⇒ Object
408 409 410 |
# File 'lib/deplate/template.rb', line 408 def rx_body /^[ \t]*#(BODY)(:\s*([^}\s]+)\s*)?$/ end |
#rx_clip ⇒ Object
454 455 456 |
# File 'lib/deplate/template.rb', line 454 def rx_clip /\{(get)(:\s*([^}\s]+)\s*)\}/ end |
#rx_doc ⇒ Object
415 416 417 |
# File 'lib/deplate/template.rb', line 415 def rx_doc /\{(doc)(:\s*([^}\s]+)\s*)\}/ end |
#rx_post ⇒ Object
401 402 403 |
# File 'lib/deplate/template.rb', line 401 def rx_post /^[ \t]*#(POSTMATTER)(:\s*([^}\s]+)\s*)?$/ end |
#rx_pre ⇒ Object
394 395 396 |
# File 'lib/deplate/template.rb', line 394 def rx_pre /^[ \t]*#(PREMATTER)(:\s*([^}\s]+)\s*)?$/ end |
#template_get_content(type, ranges) ⇒ Object
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 |
# File 'lib/deplate/template.rb', line 319 def template_get_content(type, ranges) case type when :pre content = @pre when :post content = @post when :body content = @body end acc = [] cnt = content.dup for r in @consumed[type] cnt[r] = nil end add, del = gen_range(ranges) if add.empty? for i in del cnt[i] = nil end acc = cnt else @consumed[type] += add for i in add acc << cnt[i] end end return acc.flatten.compact.join("\n") end |