Class: Turndown::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/turndown/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
11
# File 'lib/turndown/converter.rb', line 7

def initialize(options = {})
  normalized_options = normalize_options(options)
  @options = Utilities.extend(default_options, normalized_options)
  @rules = Rules.new(@options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/turndown/converter.rb', line 5

def options
  @options
end

#rulesObject (readonly)

Returns the value of attribute rules.



5
6
7
# File 'lib/turndown/converter.rb', line 5

def rules
  @rules
end

Instance Method Details

#add_rule(name, filter:, replacement:, append: nil) ⇒ Object



33
34
35
36
# File 'lib/turndown/converter.rb', line 33

def add_rule(name, filter:, replacement:, append: nil)
  rules.add(name, Rule.new(filter: filter, replacement: replacement, append: append))
  self
end

#convert(input) ⇒ Object

Raises:

  • (TypeError)


13
14
15
16
17
18
19
# File 'lib/turndown/converter.rb', line 13

def convert(input)
  raise TypeError, "#{input} is not a string, or an element/document/fragment node." unless can_convert?(input)
  return "" if input == ""

  output = process(RootNode.build(input, options))
  post_process(output)
end

#escape(string) ⇒ Object



48
49
50
# File 'lib/turndown/converter.rb', line 48

def escape(string)
  Utilities.escape_markdown(string)
end

#keep(filter) ⇒ Object



38
39
40
41
# File 'lib/turndown/converter.rb', line 38

def keep(filter)
  rules.keep(filter)
  self
end

#remove(filter) ⇒ Object



43
44
45
46
# File 'lib/turndown/converter.rb', line 43

def remove(filter)
  rules.remove(filter)
  self
end

#use(plugin) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/turndown/converter.rb', line 21

def use(plugin)
  if plugin.is_a?(Array)
    plugin.each { |entry| use(entry) }
  elsif plugin.respond_to?(:call)
    plugin.call(self)
  else
    raise TypeError, "plugin must be a callable or an Array of callables"
  end

  self
end