Class: Tanuki::TemplateCompiler
- Defined in:
- lib/tanuki/template_compiler.rb
Overview
Tanuki::TemplateCompiler is used for, well, compiling templates.
Class Method Summary collapse
-
.compile(ios, src) ⇒ Object
Compiles code from a given
srcstring toios. -
.compile_template(ios, src, klass, sym) ⇒ Object
Compiles a template from a given
srcstring toiosfor methodsymin classklass.
Class Method Details
.compile(ios, src) ⇒ Object
Compiles code from a given src string to ios.
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 60 61 62 63 64 |
# File 'lib/tanuki/template_compiler.rb', line 18 def compile(ios, src) state = :outer last_state = nil index = 0 trim_newline = false code_buf = '' begin if new_index = src[index..-1].index(pattern = expect_pattern(state)) new_index += index match = src[index..-1].match(pattern)[0] new_state = next_state(state, match) else new_state = nil end if state == :outer s = new_index ? src[index, new_index - index] : src[index..-1] if trim_newline && !s.empty? s[0] = '' if s[0] == "\n" trim_newline = false end if new_state == :code_skip code_buf << s.dup << match[0..-2] index = new_index + match.length next elsif not s.empty? ios << "\n_.call(#{(code_buf << s).inspect},ctx)" code_buf = '' end end if new_index unless state != :outer && new_state == :code_skip if new_state == :outer process_code_state(ios, code_buf << src[index...new_index], state) code_buf = '' end index = new_index + match.length trim_newline = true if (match == '-%>') last_state = state unless state == :code_comment state = new_state else code_buf << src[index...new_index] << '%>' index = new_index + match.length end end end until new_index.nil? last_state end |
.compile_template(ios, src, klass, sym) ⇒ Object
Compiles a template from a given src string to ios for method sym in class klass.
9 10 11 12 13 14 15 |
# File 'lib/tanuki/template_compiler.rb', line 9 def compile_template(ios, src, klass, sym) ios << "# encoding: #{src.encoding}\nclass #{klass}\ndef #{sym}_view(*args,&block)\nproc do|_,ctx|\n" \ "if _has_tpl ctx,self.class,:#{sym}\nctx=_ctx(ctx)" last_state = compile(ios, src) ios << "\n_.call('',ctx)" unless PRINT_STATES.include? last_state ios << "\nelse\n(_run_tpl ctx,self,:#{sym},*args,&block).call(_,ctx)\nend\nend\nend\nend" end |