Module: Slideshow::TextFilter

Included in:
Gen
Defined in:
lib/slideshow/filters/text_filter.rb

Instance Method Summary collapse

Instance Method Details

#code_block_curly_style(content) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/slideshow/filters/text_filter.rb', line 295

def code_block_curly_style( content )
  # 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) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/slideshow/filters/text_filter.rb', line 97

def comments_percent_style( content )    

# skip filter for pandoc
# - pandoc uses % for its own markdown extension

return content  if @markup_type == :markdown && @markdown_libs.first == 'pandoc-ruby'
  
  # 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

#directives_bang_style_to_percent_style(content) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slideshow/filters/text_filter.rb', line 9

def directives_bang_style_to_percent_style( content )

  # for compatibility allow !SLIDE/!STYLE as an alternative to %slide/%style-directive
  
  bang_count = 0

  # get unparsed helpers e.g. SLIDE|STYLE  
  unparsed = config.helper_unparsed.map { |item| item.upcase }.join( '|' )                                                                   
  
  content.gsub!(/^!(#{unparsed})(.*)$/) do |match|
    bang_count += 1
    "<%= #{$1.downcase} '#{$2 ? $2 : ''}' %>"
  end

  puts "  Patching !-directives (#{bang_count} #{config.helper_unparsed.join('/')}-directives)..."

  content
end

#directives_percent_style(content) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/slideshow/filters/text_filter.rb', line 28

def directives_percent_style( content )

  # skip filter for pandoc
  # - pandoc uses % for its own markdown extension
  # - don't process % pass it along/through to pandoc

  return content  if @markup_type == :markdown && @markdown_libs.first == 'pandoc-ruby'
    
    
  directive_unparsed  = 0
  directive_expr      = 0
  directive_block_beg = 0
  directive_block_end = 0

  # process directives (plus skip %begin/%end comment-blocks)

  inside_block  = 0
  inside_helper = false
  
  content2 = ""
  
  content.each_line do |line|
    if line =~ /^%([a-zA-Z][a-zA-Z0-9_]*)(.*)/
      directive = $1.downcase
      params    = $2

      logger.debug "processing %-directive: #{directive}"

      # slide, style
      if config.helper_unparsed.include?( directive )
        directive_unparsed += 1
        content2 << "<%= #{directive} '#{params ? params : ''}' %>"
      elsif config.helper_exprs.include?( directive )
        directive_expr += 1
        content2 << "<%= #{directive} #{params ? erb_simple_params(directive,params) : ''} %>"        
      elsif inside_helper && directive == 'end'
        inside_helper = false
        directive_block_end += 1
        content2 << "%>"        
      elsif inside_block > 0 && directive == 'end'
        inside_block -= 1
        directive_block_end += 1
        content2 << "<% end %>"
      elsif [ 'comment', 'comments', 'begin', 'end' ].include?( directive )  # skip begin/end comment blocks
        content2 << line
      elsif [ 'helper', 'helpers' ].include?( directive )
        inside_helper = true
        directive_block_beg += 1
        content2 << "<%"
      else
        inside_block += 1
        directive_block_beg += 1
        content2 << "<% #{directive} #{params ? erb_simple_params(directive,params) : ''} do %>"
      end
    else
      content2 << line
    end
  end  
    
  puts "  Preparing %-directives (" +
      "#{directive_unparsed} #{config.helper_unparsed.join('/')} directives, " +
      "#{directive_expr} #{config.helper_exprs.join('/')} expr-directives, " +
      "#{directive_block_beg}/#{directive_block_end} block-directives)..."

  content2
end

#erb(content) ⇒ Object

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



175
176
177
178
179
180
# File 'lib/slideshow/filters/text_filter.rb', line 175

def erb( content )
  puts "  Running embedded Ruby (erb) code/helpers..."
  
  content =  ERB.new( content ).result( binding() )
  content
end

#erb_django_simple_params(code) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/slideshow/filters/text_filter.rb', line 233

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) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/slideshow/filters/text_filter.rb', line 246

def erb_django_style( content )

  # 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_rename_helper_hack(content) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/slideshow/filters/text_filter.rb', line 155

def erb_rename_helper_hack( content )
  # note: include is a ruby keyword; rename to s9_include so we can use it 
  
  rename_counter = 0
  
  # turn renames into something like:
  #   include|class   etc.
  renames = config.helper_renames.join( '|' )
  
  content.gsub!( /<%=[ \t]*(#{renames})/ ) do |match|
    rename_counter += 1
    "<%= s9_#{$1}" 
  end

  puts "  Patching embedded Ruby (erb) code for aliases (#{rename_counter} #{config.helper_renames.join('/')}-aliases)..."

  content
end

#erb_simple_params(method, params) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/slideshow/filters/text_filter.rb', line 182

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) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/slideshow/filters/text_filter.rb', line 147

def skip_end_directive( content )
  # 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