Module: TextUtils::Filter

Includes:
StringFilter
Defined in:
lib/textutils/filter/string_filter.rb,
lib/textutils/filter/erb_filter.rb,
lib/textutils/filter/code_filter.rb,
lib/textutils/filter/comment_filter.rb,
lib/textutils/filter/erb_django_filter.rb

Overview

module StringFilter

Constant Summary

Constants included from StringFilter

StringFilter::ASCIIFY_MAPPINGS

Instance Method Summary collapse

Methods included from StringFilter

#asciify, #slugify

Instance Method Details

#code_block_curly_style(content, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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 = {}) ⇒ Object

allow plugins/helpers; process source (including header) using erb



7
8
9
10
11
12
# 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) ⇒ Object

“private” helpers - do NOT use as filters - todo: add :nodoc: how?



60
61
62
63
64
65
66
67
68
69
70
71
# 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 = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



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
# 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 = {}) ⇒ Object



51
52
53
54
55
56
57
# 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