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
Constant Summary
StringFilter::ASCIIFY_MAPPINGS
Instance Method Summary
collapse
#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={} )
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
|
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 ( content, options={} )
= 0
= 0
= 0
content.gsub!(/^%(begin|comment|comments).*?%end/m) do |match|
+= 1
""
end
content.sub!(/^%end.*/m) do |match|
+= 1
""
end
content.gsub!(/(^%$)|(^%[^>].*)/ ) do |match|
+= 1
""
end
puts " Removing %-comments (#{} lines, " +
"#{} begin/end-blocks, #{} 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 )
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={} )
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 )
return params if params.nil? || params.strip.empty?
params.strip!
if params.include?( '=>' )
puts "** warning: skipping patching of params for helper '#{method}'; already includes '=>':"
puts " #{params}"
return params
end
before = params.clone
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 "#{Regexp.last_match( 0 )},"
else
"'#{Regexp.last_match( 0 )}',"
end
end
params.gsub!( /'(\w+)',[ \t]*=/ ) do |match|
":#{$1}=>"
end
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={} )
content.gsub!(/__SKIP__.*?__END__/m, '')
content.sub!(/__END__.*/m, '')
content
end
|