Class: HtmlTemplate
- Inherits:
-
Object
- Object
- HtmlTemplate
- Defined in:
- lib/html/template.rb,
lib/html/template/version.rb
Defined Under Namespace
Classes: Context
Constant Summary collapse
- VAR_RE =
%r{<TMPL_(?<tag>VAR) \s (?<ident>[a-zA-Z_][a-zA-Z0-9_]*) (\s (?<escape>ESCAPE) = "(?<format>HTML|NONE)" )? >}x
- IF_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>IF|UNLESS) \s (?<ident>[a-zA-Z_][a-zA-Z0-9_]*) >}x
- IF_CLOSE_RE =
%r{(?<close></)TMPL_(?<tag>IF|UNLESS) (\s (?<ident>[a-zA-Z_][a-zA-Z0-9_]*) )? # note: allow optional identifier >}x
- ELSE_RE =
%r{<TMPL_(?<tag>ELSE) >}x
- LOOP_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>LOOP) \s (?<ident>[a-zA-Z_][a-zA-Z0-9_]*) >}x
- LOOP_CLOSE_RE =
%r{(?<close></)TMPL_(?<tag>LOOP) >}x
- CATCH_OPEN_RE =
%r{(?<open><)TMPL_(?<unknown>[^>]+?) >}x
- CATCH_CLOSE_RE =
%r{(?<close></)TMPL_(?<unknown>[^>]+?) >}x
- ALL_RE =
Regexp.union( VAR_RE, IF_OPEN_RE, IF_CLOSE_RE, ELSE_RE, LOOP_OPEN_RE, LOOP_CLOSE_RE, CATCH_OPEN_RE, CATCH_CLOSE_RE )
- MAJOR =
0
- MINOR =
1
- PATCH =
0
- VERSION =
[MAJOR,MINOR,PATCH].join('.')
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#template ⇒ Object
readonly
return “inner” (erb) template object.
-
#text ⇒ Object
readonly
returns converted template text (with “breaking” comments!!!).
Class Method Summary collapse
Instance Method Summary collapse
- #convert(text) ⇒ Object
-
#initialize(text = nil, filename: nil) ⇒ HtmlTemplate
constructor
A new instance of HtmlTemplate.
-
#render(**kwargs) ⇒ Object
class Template::Context.
- #to_recursive_ostruct(o) ⇒ Object
Constructor Details
#initialize(text = nil, filename: nil) ⇒ HtmlTemplate
Returns a new instance of HtmlTemplate.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/html/template.rb', line 42 def initialize( text=nil, filename: nil ) if text.nil? ## try to read file (by filename) text = File.open( filename, 'r:utf-8' ) { |f| f.read } end ## todo/fix: add filename to ERB too (for better error reporting) @text, @errors = convert( text ) ## note: keep a copy of the converted template text if @errors.size > 0 puts "!! ERROR - #{@errors.size} conversion / syntax error(s):" pp @errors raise ## todo - find a good Error - StandardError - why? why not? end @template = ERB.new( @text, nil, '%<>' ) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
40 41 42 |
# File 'lib/html/template.rb', line 40 def errors @errors end |
#template ⇒ Object (readonly)
return “inner” (erb) template object
39 40 41 |
# File 'lib/html/template.rb', line 39 def template @template end |
#text ⇒ Object (readonly)
returns converted template text (with “breaking” comments!!!)
38 39 40 |
# File 'lib/html/template.rb', line 38 def text @text end |
Class Method Details
.banner ⇒ Object
14 15 16 17 |
# File 'lib/html/template/version.rb', line 14 def self. ### todo: add RUBY_PATCHLEVEL or RUBY_PATCH_LEVEL e.g. -p124 "html-template/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" end |
.root ⇒ Object
19 20 21 |
# File 'lib/html/template/version.rb', line 19 def self.root File.( File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) ) end |
.version ⇒ Object
10 11 12 |
# File 'lib/html/template/version.rb', line 10 def self.version VERSION end |
Instance Method Details
#convert(text) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/html/template.rb', line 128 def convert( text ) errors = [] # note: reset global errros list stack = [] ## note: convert line-by-line ## allows comments and line no reporting etc. buf = String.new('') ## note: '' required for getting source encoding AND not ASCII-8BIT!!! lineno = 0 text.each_line do |line| lineno += 1 if line.lstrip.start_with?( '#' ) ## or make it tripple ### - why? why not? buf << "%#{line.lstrip}" elsif line.strip.empty? buf << line else buf << line.gsub( ALL_RE ) do |_| m = $~ ## (global) last match object tag = m[:tag] tag_open = m[:open] tag_close = m[:close] ident = m[:ident] unknown = m[:unknown] # catch all for unknown / unmatched tags escape = m[:escape] format = m[:format] ## todo/fix: rename ctx to scope or __ - why? why not? ## note: peek; get top stack item ## if top level (stack empty) => nothing ## otherwise => channel. or item. etc. (with trailing dot included!) ctx = stack.empty? ? '' : "#{stack[-1]}." code = if tag == 'VAR' if escape && format == 'HTML' ## check or use long form e.g. CGI.escapeHTML - why? why not? "<%=h #{ctx}#{ident} %>" else "<%= #{ctx}#{ident} %>" end elsif tag == 'LOOP' && tag_open ## assume plural ident e.g. channels ## cut-off last char, that is, the plural s channels => channel ## note: ALWAYS downcase (auto-generated) loop iterator/pass name it = ident[0..-2].downcase stack.push( it ) "<% #{ctx}#{ident}.each_with_loop do |#{it}, #{it}_loop| %>" elsif tag == 'LOOP' && tag_close stack.pop "<% end %>" elsif tag == 'IF' && tag_open "<% if #{ctx}#{ident} %>" elsif tag == 'UNLESS' && tag_open "<% unless #{ctx}#{ident} %>" elsif (tag == 'IF' || tag == 'UNLESS') && tag_close "<% end %>" elsif tag == 'ELSE' "<% else %>" elsif unknown errors << if tag_open "line #{lineno} - unknown open tag: #{unknown}" else ## assume tag_close "line #{lineno} - unknown close tag: #{unknown}" end puts "!! ERROR in line #{lineno} - #{errors[-1]}:" puts line "<%# !!error - #{errors[-1]} %>" else raise ArgumentError ## unknown tag #{tag} end puts " line #{lineno} - match #{m[0]} replacing with: #{code}" code end end end # each_line [buf, errors] end |
#render(**kwargs) ⇒ Object
class Template::Context
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/html/template.rb', line 222 def render( **kwargs ) ## todo: use locals / assigns or something instead of **kwargs - why? why not? ## allow/support (extra) locals / assigns - why? why not? ## note: Ruby >= 2.5 has ERB#result_with_hash - use later - why? why not? kwargs = kwargs.reduce( {} ) do |hash, (key, val)| ## puts "#{key} => #{val}:#{val.class.name}" hash[key] = to_recursive_ostruct( val ) hash end ## (auto-)convert array and hash values to ostruct ## for easy dot (.) access ## e.g. student.name instead of student[:name] @template.result( Context.new( **kwargs ).get_binding ) end |
#to_recursive_ostruct(o) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/html/template.rb', line 109 def to_recursive_ostruct( o ) if o.is_a?( Array ) o.reduce( [] ) do |ary, item| ary << to_recursive_ostruct( item ) ary end elsif o.is_a?( Hash ) ## puts 'to_recursive_ostruct (hash):' OpenStruct.new( o.reduce( {} ) do |hash, (key, val)| ## puts "#{key} => #{val}:#{val.class.name}" hash[key] = to_recursive_ostruct( val ) hash end ) else ## assume regular "primitive" value - pass along as is o end end |