Module: Filters

Defined in:
lib/filters.rb

Instance Method Summary collapse

Instance Method Details

#filter_gist(args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/filters.rb', line 51

def filter_gist(args)
  if args.include?('url')
    args['src'] = args['url']
    args.delete('url')
  end
  if args['src'].nil?
    return nil
  end
  if not args['src'] =~ /\.js$/
    args['src'] += ".js"
  end
  return "<script "+args.each.map{ |k,v| "#{k}=\"#{v}\"" }.join(" ")+"></script>"
end

#filter_youtube(args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/filters.rb', line 37

def filter_youtube(args)
  if args.include?('url')
    args['src'] = args['url']
    args.delete('url')
  elsif args.include?('id')
    args['src'] = "https://www.youtube.com/embed/#{args['id']}"
  end
  if args['src'].nil?
    return nil
  end
  args['src'].gsub!(/\/watch\?v=/, "/embed/")
  return "<iframe "+args.each.map{ |k,v| "#{k}=\"#{v}\"" }.join(" ")+"></iframe>"
end

#run_filters(text) ⇒ 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
# File 'lib/filters.rb', line 6

def run_filters(text)
  newtext = text
  text.scan(/(\[(\w+)([^\]]*)\])/) do |m|
    match = m[0]
    debug "match = #{match}"
    filter = m[1]
    args = m[2]
    if Filters.method_defined?("filter_#{filter}")
      fn=Filters.method("filter_#{filter}")
      arg = {}
      args = HTMLEntities.new.decode(args)
      debug "args = #{args}"
      args.scan(/\b(\w+)=["“]([^"]*)["”]/) { |a|
        arg[a[0]] = a[1]
      }
      verbose "Calling filter_#{filter} with args #{arg}"
      result = fn.(arg)
      if not result.nil?
        newtext = newtext.gsub(match, result)
      end
    else
      # We only produce this message in verbose mode because it gets triggered
      # every time [some text in brackets] is used
      warn("Warning: nonexistent filter #{filter} used, leaving text as is") if $enwrite_verbose
    end
  end
  debug "After running filters:"
  debug newtext
  return newtext
end