Class: TextUtils::PageTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/textutils/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tmpl) ⇒ PageTemplate

Returns a new instance of PageTemplate.



102
103
104
# File 'lib/textutils/page.rb', line 102

def initialize( tmpl )
  @tmpl = tmpl.dup   # make a copy; just to be sure no one will change text
end

Class Method Details

.read(path) ⇒ Object



97
98
99
# File 'lib/textutils/page.rb', line 97

def self.read( path )
  self.new( File.read_utf8( path ) )
end

Instance Method Details

#cleanup_newlines(text) ⇒ Object



173
174
175
176
# File 'lib/textutils/page.rb', line 173

def cleanup_newlines( text )
  # remove all blank lines that go over three
  text.gsub( /\n{4,}/, "\n\n\n" )
end

#concat_lines(text) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/textutils/page.rb', line 179

def concat_lines( text )
  #  lines ending with  ++  will get newlines get removed
  # e.g.
  # >|   hello1 ++
  # >1   hello2
  #  becomes
  # >|   hello1 hello2
  
  #
  # note: do NOT use \s - will include \n (newline) ??
  
  text.gsub( /[ \t]+\+{2}[ \t]*\n[ \t]*/, ' ' )  # note: replace with single space
end

#django_to_erb(text) ⇒ Object

filters

- use better names and make public for reuse!!!!


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
# File 'lib/textutils/page.rb', line 130

def django_to_erb( text )
  ## convert django style markers to erb style marker e.g
  #  {% %} becomes <% %>  -- supports multi-line
  #  {{ }} becomes <%= %>  - does NOT support multi-line

  ## comments (support multi-line)
  text = text.gsub( /\{#(.+?)#\}/m ) do |_|
   "<%# #{1} %>"
  end

  text = text.gsub( /\{%(.+?)%\}/m ) do |_|
    ## note: also replace newlines w/  %>\n<%  to split
    #   multi-line stmts into single-line stmts
    # lets us use
    # {%
    #  %} will become
    # <%  %>
    # <%  %>
    "<% #{$1} %>".gsub( "\n", " %>\n<% " )
  end

  # note: for now {{ }} will NOT support multi-line
  text = text.gsub( /\{\{(.+?)\}\}/ ) do |_|
    "<%= #{$1} %>"
  end

  text
end

#remove_blanks(text) ⇒ Object



168
169
170
171
# File 'lib/textutils/page.rb', line 168

def remove_blanks( text )
  # remove lines only with  ..
  text.gsub( /^[ \t]*\.{2}[ \t]*\n/, '' )
end

#remove_html_comments(text) ⇒ Object



159
160
161
# File 'lib/textutils/page.rb', line 159

def remove_html_comments( text )
  text.gsub( /<!--.+?-->/, '' )
end

#remove_leading_spaces(text) ⇒ Object



163
164
165
166
# File 'lib/textutils/page.rb', line 163

def remove_leading_spaces( text )
  # remove leading spaces if less than four !!!
  text.gsub( /^[ \t]+(?![ \t])/, '' )    # use negative regex lookahead e.g. (?!)
end

#render(ctx) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/textutils/page.rb', line 106

def render( ctx )
# note: erb offers the following trim modes:
#  1) <> omit newline for lines starting with <% and ending in %>
#  2)  >  omit newline for lines ending in %>
#  3)  omit blank lines ending in -%>
  ## run filters
  tmpl = remove_html_comments( @tmpl )
  tmpl = remove_blanks( tmpl )

  tmpl = django_to_erb( tmpl )  ## allow django/jinja style templates

  tmpl = remove_leading_spaces( tmpl )
  tmpl = concat_lines( tmpl )

  text = ERB.new( tmpl, nil, '<>' ).result( ctx )

  ### text = cleanup_newlines( text )
  text
end