Class: HtmlTemplate
- Inherits:
-
Object
- Object
- HtmlTemplate
- Defined in:
- lib/html/template.rb,
lib/html/template/version.rb
Defined Under Namespace
Classes: Configuration, Context, Names
Constant Summary collapse
- IDENT =
'[a-zA-Z_][a-zA-Z0-9_]*'
- ESCAPES =
'HTML|NONE'
- VAR_RE =
%r{<TMPL_(?<tag>VAR) \s+ (?<ident>#{IDENT}) (\s+ (?<escape>ESCAPE) \s*=\s*( (?<q>['"])(?<format>#{ESCAPES})\k<q> # note: allow single or double enclosing quote (but MUST match) | (?<format>#{ESCAPES}) # note: support without quotes too ) )? >}x
- IF_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>IF|UNLESS) \s+ (?<ident>#{IDENT}) >}x
- IF_CLOSE_RE =
%r{(?<close></)TMPL_(?<tag>IF|UNLESS) (\s+ (?<ident>#{IDENT}) )? # note: allow optional identifier >}x
- ELSE_RE =
%r{<TMPL_(?<tag>ELSE) >}x
- LOOP_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>LOOP) \s+ (?<ident>#{IDENT}) >}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 =
1
- MINOR =
0
- PATCH =
0
- VERSION =
[MAJOR,MINOR,PATCH].join('.')
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#names ⇒ Object
readonly
for debugging - returns all referenced / used names in VAR/IF/UNLESS/LOOP/etc.
-
#template ⇒ Object
readonly
return “inner” (erb) template object.
-
#text ⇒ Object
readonly
returns converted template text (with “breaking” comments!!!).
Class Method Summary collapse
- .banner ⇒ Object
- .config ⇒ Object
-
.configure {|config| ... } ⇒ Object
lets you use HtmlTemplate.configure do |config| config.debug = true config.strict = true end.
- .root ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
-
#config ⇒ Object
config convenience (shortcut) helpers.
- #convert(text) ⇒ Object
- #debug? ⇒ Boolean
-
#ident_to_loop_it(ident) ⇒ Object
make loop iterator (e.g. Channels => channel and so on).
-
#initialize(text = nil, filename: nil, strict: config.strict?, loop_vars: config.loop_vars?) ⇒ HtmlTemplate
constructor
A new instance of HtmlTemplate.
- #loop_vars? ⇒ Boolean
-
#render(**kwargs) ⇒ Object
class Template::Context.
- #strict? ⇒ Boolean
- #to_recursive_ostruct(o) ⇒ Object
Constructor Details
#initialize(text = nil, filename: nil, strict: config.strict?, loop_vars: config.loop_vars?) ⇒ HtmlTemplate
Returns a new instance of HtmlTemplate.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/html/template.rb', line 139 def initialize( text=nil, filename: nil, strict: config.strict?, loop_vars: config.loop_vars? ) if text.nil? ## try to read file (by filename) text = File.open( filename, 'r:utf-8' ) { |f| f.read } end ## options @strict = strict @loop_vars = loop_vars ## todo/fix: add filename to ERB too (for better error reporting) @text, @errors, @names = 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 if strict? ## 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.
129 130 131 |
# File 'lib/html/template.rb', line 129 def errors @errors end |
#names ⇒ Object (readonly)
for debugging - returns all referenced / used names in VAR/IF/UNLESS/LOOP/etc.
130 131 132 |
# File 'lib/html/template.rb', line 130 def names @names end |
#template ⇒ Object (readonly)
return “inner” (erb) template object
128 129 130 |
# File 'lib/html/template.rb', line 128 def template @template end |
#text ⇒ Object (readonly)
returns converted template text (with “breaking” comments!!!)
127 128 129 |
# File 'lib/html/template.rb', line 127 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 |
.config ⇒ Object
122 123 124 |
# File 'lib/html/template.rb', line 122 def self.config @config ||= Configuration.new end |
.configure {|config| ... } ⇒ Object
lets you use
HtmlTemplate.configure do |config|
config.debug = true
config.strict = true
end
118 119 120 |
# File 'lib/html/template.rb', line 118 def self.configure yield( config ) 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
#config ⇒ Object
config convenience (shortcut) helpers
133 |
# File 'lib/html/template.rb', line 133 def config() self.class.config; end |
#convert(text) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 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 318 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 349 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/html/template.rb', line 274 def convert( text ) errors = [] # note: reset global errros list names = Names.new ## keep track of all referenced / used names in VAR/IF/UNLESS/LOOP/etc. 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 = if stack.empty? '' else ## check for special loop variables if loop_vars? && ['__INDEX__', '__COUNTER__', '__INDEX__', '__COUNTER__', '__ODD__', '__EVEN__', '__FIRST__', '__INNER__', '__OUTER__', '__LAST__' ].include?( ident ) "#{ident_to_loop_it( stack[-1] )}_loop." else ## 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 "#{ident_to_loop_it( stack[-1] )}." end end code = if tag == 'VAR' names.add( stack, ident, '$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 names.add( stack, ident, '$LOOP' ) ## 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_to_loop_it( ident ) stack.push( ident ) if loop_vars? "<% #{ctx}#{ident}.each_with_loop do |#{it}, #{it}_loop| %>" else "<% #{ctx}#{ident}.each do |#{it}| %>" end elsif tag == 'LOOP' && tag_close stack.pop "<% end %>" elsif tag == 'IF' && tag_open names.add( stack, ident, '$IF' ) "<% if #{ctx}#{ident} %>" elsif tag == 'UNLESS' && tag_open names.add( stack, ident, '$UNLESS' ) "<% 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}" if debug? code end end end # each_line [buf, errors, names.to_h] end |
#debug? ⇒ Boolean
134 |
# File 'lib/html/template.rb', line 134 def debug?() config.debug?; end |
#ident_to_loop_it(ident) ⇒ Object
make loop iterator (e.g. Channels => channel and so on)
265 266 267 268 269 270 271 |
# File 'lib/html/template.rb', line 265 def ident_to_loop_it( ident ) # make loop iterator (e.g. Channels => channel and so on) ## 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 ident[0..-2].downcase end |
#loop_vars? ⇒ Boolean
137 |
# File 'lib/html/template.rb', line 137 def loop_vars?() @loop_vars; end |
#render(**kwargs) ⇒ Object
class Template::Context
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/html/template.rb', line 403 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 |
#strict? ⇒ Boolean
136 |
# File 'lib/html/template.rb', line 136 def strict?() @strict; end |
#to_recursive_ostruct(o) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/html/template.rb', line 217 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 |