Class: HtmlTemplate

Inherits:
Object
  • Object
show all
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_0-9]+)
>}x
IF_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>IF)
  \s
  (?<ident>[a-zA-Z_0-9]+)
>}x
IF_CLOSE_RE =
%r{(?<close></)TMPL_(?<tag>IF)
>}x
LOOP_OPEN_RE =
%r{(?<open><)TMPL_(?<tag>LOOP)
  \s
  (?<ident>[a-zA-Z_0-9]+)
>}x
LOOP_CLOSE_RE =
%r{(?<close></)TMPL_(?<tag>LOOP)
>}x
CATCH_OPEN_RE =
%r{(?<open><)TMPL_(?<unknown>[^>]+?)
>}x
ALL_RE =
Regexp.union( VAR_RE,
IF_OPEN_RE,
IF_CLOSE_RE,
LOOP_OPEN_RE,
LOOP_CLOSE_RE,
CATCH_OPEN_RE )
MAJOR =
0
MINOR =
0
PATCH =
1
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ HtmlTemplate

Returns a new instance of HtmlTemplate.



19
20
21
22
# File 'lib/html/template.rb', line 19

def initialize( text )
  @text     = convert( text )    ## note: keep a copy of the converted template text
  @template = ERB.new( strip_comments( @text ) )
end

Instance Attribute Details

#textObject (readonly)

returns converted template text (with “breaking” comments!!!)



17
18
19
# File 'lib/html/template.rb', line 17

def text
  @text
end

Class Method Details



14
15
16
17
# File 'lib/html/template/version.rb', line 14

def self.banner
  ### todo: add RUBY_PATCHLEVEL or RUBY_PATCH_LEVEL  e.g. -p124
  "html-template/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.rootObject



19
20
21
# File 'lib/html/template/version.rb', line 19

def self.root
  File.expand_path( File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) )
end

.versionObject



10
11
12
# File 'lib/html/template/version.rb', line 10

def self.version
  VERSION
end

Instance Method Details

#convert(text) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/html/template.rb', line 68

def convert( text )
  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    ## pass along as is for now!!
    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

                ## 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'
                         "<%= #{ctx}#{ident} %>"
                       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 do |#{it}| %>"
                       elsif tag == 'LOOP' && tag_close
                         stack.pop
                         "<% end %>"
                       elsif tag == 'IF' && tag_open
                         "<% if #{ctx}#{ident} %>"
                       elsif tag == 'IF' && tag_close
                         "<% end %>"
                       elsif unknown && tag_open
                        puts "!! ERROR"
                         "<%# !!error - unknown open tag: #{unknown} %>"
                       else
                         raise ArgumentError  ## unknown tag #{tag}
                       end

                puts " line #{lineno} - match #{m[0]} replacing with: #{code}"
                code

              end
      end
    end # each_line
  buf
end

#render(**kwargs) ⇒ Object

class Template::Context



141
142
143
144
145
146
# File 'lib/html/template.rb', line 141

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?
  @template.result( Context.new( **kwargs ).get_binding )
end

#strip_comments(text) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/html/template.rb', line 58

def strip_comments( text )
   ## strip/remove comments lines starting with #
   buf = String.new('') ## note: '' required for getting source encoding AND not ASCII-8BIT!!!
   text.each_line do |line|
      next if line.lstrip.start_with?( '#' )
      buf << line
   end
   buf
end