class TextUtils::PageTemplate
Public Class Methods
new( tmpl )
click to toggle source
# 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
read( path )
click to toggle source
todo: what is the best convention for loading file and handling string for now it its:
PageTemplate.read
( 'to/path ' ) or — use load ???? instead of read?? PageTemplate.new
( 'template content' )
# File lib/textutils/page.rb, line 97 def self.read( path ) self.new( File.read_utf8( path ) ) end
Public Instance Methods
cleanup_newlines( text )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
filters
- use better names and make public for reuse!!!!
# 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 )
click to toggle source
# 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 )
click to toggle source
# File lib/textutils/page.rb, line 159 def remove_html_comments( text ) text.gsub( /<!--.+?-->/, '' ) end
remove_leading_spaces( text )
click to toggle source
# 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 )
click to toggle source
# 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