module TextUtils::Filter
Public Instance Methods
code_block_curly_style( content, options={} )
click to toggle source
# File lib/textutils/filter/code_filter.rb, line 6 def code_block_curly_style( content, options={} ) # replace {{{ w/ <pre class='code'> # replace }}} w/ </pre> # use 4-6 { or } to escape back to literal value (e.g. {{{{ or {{{{{{ => {{{ ) # note: {{{ / }}} are anchored to beginning of line ( spaces and tabs before {{{/}}}allowed ) # track statistics code_begin = 0 code_begin_esc = 0 code_end = 0 code_end_esc = 0 content.gsub!( /^[ \t]*(\{{3,6})/ ) do |match| escaped = ($1.length > 3) if escaped code_begin_esc += 1 "{{{" else code_begin += 1 "<pre class='code'>" end end content.gsub!( /^[ \t]*(\}{3,6})/ ) do |match| escaped = ($1.length > 3) if escaped code_end_esc += 1 "}}}" else code_end += 1 "</pre>" end end puts " Patching {{{/}}}-code blocks (#{code_begin}/#{code_end} blocks, " + "#{code_begin_esc}/#{code_end_esc} escaped blocks)..." content end
comments_percent_style( content, options={} )
click to toggle source
# File lib/textutils/filter/comment_filter.rb, line 6 def comments_percent_style( content, options={} ) # remove comments # % comments # %begin multiline comment # %end multiline comment # track statistics comments_multi = 0 comments_single = 0 comments_end = 0 # remove multi-line comments content.gsub!(/^%(begin|comment|comments).*?%end/m) do |match| comments_multi += 1 "" end # remove everyting starting w/ %end (note, can only be once in file) content.sub!(/^%end.*/m) do |match| comments_end += 1 "" end # hack/note: # note multi-line erb expressions/stmts might cause trouble # # %> gets escaped as special case (not treated as comment) # <% # whatever # %> <!-- trouble here; would get removed as comment! # todo: issue warning? # remove single-line comments content.gsub!(/(^%$)|(^%[^>].*)/ ) do |match| comments_single += 1 "" end puts " Removing %-comments (#{comments_single} lines, " + "#{comments_multi} begin/end-blocks, #{comments_end} end-blocks)..." content end
erb( content, options={} )
click to toggle source
allow plugins/helpers; process source (including header) using erb
# File lib/textutils/filter/erb_filter.rb, line 7 def erb( content, options={} ) puts " Running embedded Ruby (erb) code/helpers..." content = ERB.new( content ).result( binding() ) content end
erb_django_simple_params( code )
click to toggle source
“private” helpers - do NOT use as filters - todo: add :nodoc: how?
# File lib/textutils/filter/erb_django_filter.rb, line 60 def erb_django_simple_params( code ) # split into method/directive and parms plus convert params code.sub!( /^[ \t]([\w.]+)(.*)/ ) do |match| directive = $1 params = $2 "#{directive} #{params ? erb_simple_params(directive,params) : ''}" end code end
erb_django_style( content, options={} )
click to toggle source
# File lib/textutils/filter/erb_django_filter.rb, line 6 def erb_django_style( content, options={} ) # replace expressions (support for single lines only) # {{ expr }} -> <%= expr %> # {% stmt %} -> <% stmt %> !! add in do if missing (for convenience) # # use use {{{ or {{{{ to escape expr back to literal value # and use {%% %} to escape stmts erb_expr = 0 erb_stmt_beg = 0 erb_stmt_end = 0 content.gsub!( /(\{{2,4})([^{}\n]+?)(\}{2,4})/ ) do |match| escaped = ($1.length > 2) if escaped "{{#{$2}}}" else erb_expr += 1 "<%= #{erb_django_simple_params($2)} %>" end end content.gsub!( /(\{%{1,2})([ \t]*end[ \t]*)%\}/ ) do |match| escaped = ($1.length > 2) if escaped "{%#{$2}%}" else erb_stmt_end += 1 "<% end %>" end end content.gsub!( /(\{%{1,2})([^%\n]+?)%\}/ ) do |match| escaped = ($1.length > 2) if escaped "{%#{$2}%}" else erb_stmt_beg += 1 "<% #{erb_django_simple_params($2)} do %>" end end puts " Patching embedded Ruby (erb) code Django-style (#{erb_expr} {{-expressions," + " #{erb_stmt_beg}/#{erb_stmt_end} {%-statements)..." content end
erb_simple_params( method, params )
click to toggle source
# File lib/textutils/filter/erb_django_filter.rb, line 73 def erb_simple_params( method, params ) # replace params to support html like attributes e.g. # plus add comma separator # # class=part -> :class => 'part' # 3rd/tutorial -> '3rd/tutorial' # :css -> :css return params if params.nil? || params.strip.empty? params.strip! ## todo: add check for " ?? if params.include?( '=>' ) puts "** warning: skipping patching of params for helper '#{method}'; already includes '=>':" puts " #{params}" return params end before = params.clone # 1) string-ify values and keys (that is, wrap in '') # plus separate w/ commas params.gsub!( /([:a-zA-Z0-9#][\w\/\-\.#()]*)|('[^'\n]*')/) do |match| symbol = ( Regexp.last_match( 0 )[0,1] == ':' ) quoted = ( Regexp.last_match( 0 )[0,1] == "'" ) if symbol || quoted # return symbols or quoted string as is "#{Regexp.last_match( 0 )}," else "'#{Regexp.last_match( 0 )}'," end end # 2) symbol-ize hash keys # change = to => # remove comma for key/value pairs params.gsub!( /'(\w+)',[ \t]*=/ ) do |match| ":#{$1}=>" end # 3) remove trailing comma params.sub!( /[ \t]*,[ \t]*$/, '' ) puts " Patching params for helper '#{method}' from '#{before}' to:" puts " #{params}" params end
skip_end_directive( content, options={} )
click to toggle source
# File lib/textutils/filter/comment_filter.rb, line 51 def skip_end_directive( content, options={} ) # codex-style __SKIP__, __END__ directive # ruby note: .*? is non-greedy (shortest-possible) regex match content.gsub!(/__SKIP__.*?__END__/m, '') content.sub!(/__END__.*/m, '') content end