Class: KramdownFilter

Inherits:
TextFilter
  • Object
show all
Defined in:
lib/kramdown_filter.rb

Instance Method Summary collapse

Instance Method Details

#filter(text) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kramdown_filter.rb', line 45

def filter(text)
  o = self.options
  Kramdown::Document.new(text,
  {
    :auto_ids                   => o[:auto_ids][:default],
    :auto_id_prefix             => o[:auto_id_prefix][:default],
    :parse_block_html           => o[:parse_block_html][:default],
    :parse_span_html            => o[:parse_span_html][:default],
    :footnote_nr                => o[:footnote_nr][:default],
    :coderay_wrap               => o[:coderay_wrap][:default],
    :coderay_line_numbers       => o[:coderay_line_numbers][:default],
    :coderay_line_number_start  => o[:coderay_line_number_start][:default],
    :coderay_tab_width          => o[:coderay_tab_width][:default],
    :coderay_bold_every         => o[:coderay_bold_every][:default],
    :coderay_css                => o[:coderay_css][:default],
    :entity_output              => o[:entity_output][:default],
    :toc_levels                 => o[:toc_levels][:default]
  }).to_html
end

#optionsObject



4
5
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
# File 'lib/kramdown_filter.rb', line 4

def options
  c = Radiant::Config
  # set default options for the kramdown library
  # the type conversion stuff is due to kramdown having strict rules
  # about the types given as option values but Radiant::Config
  # stores all values as simple strings.
  o = {
    :auto_ids                   => { :default => true,               :type => :boolean },
    :auto_id_prefix             => { :default => '',                 :type => :string  },
    :parse_block_html           => { :default => false,              :type => :boolean },
    :parse_span_html            => { :default => true,               :type => :boolean },
    :footnote_nr                => { :default => 1,                  :type => :integer },
    :coderay_wrap               => { :default => :div,               :type => :symbol  },
    :coderay_line_numbers       => { :default => :inline,            :type => :symbol  },
    :coderay_line_number_start  => { :default => 1,                  :type => :integer },
    :coderay_tab_width          => { :default => 0,                  :type => :integer },
    :coderay_bold_every         => { :default => 10,                 :type => :integer },
    :coderay_css                => { :default => :style,             :type => :symbol  },
    :entity_output              => { :default => :as_char,           :type => :symbol  },
    :toc_levels                 => { :default => [1, 2, 3, 4, 5, 6], :type => :array   }
  }
  # overwrite defaults with user set values
  o.keys.each { |key|
    if c["kramdown.#{key.to_s}"]
      case o[key][:type]
      when :boolean
        boolean = c["kramdown.#{key.to_s}"] == "true" ? true : false
        o[key][:default] = boolean
      when :integer
        o[key][:default] = c["kramdown.#{key.to_s}"].to_i
      when :string
        o[key][:default] = c["kramdown.#{key.to_s}"]
      when :symbol
        symbol = c["kramdown.#{key.to_s}"] != "nil" ? c["kramdown.#{key.to_s}"].to_sym : nil
        o[key][:default] = symbol
      end
    end
  }
  return o
end