Class: Kramdown::Converter::PlainText

Inherits:
Base
  • Object
show all
Defined in:
lib/liquid_markdown/converter/plain_text.rb

Overview

Converts tree to plain text. Removes all formatting and attributes. This converter can be used to generate clean text for use in meta tags, plain text emails, etc.

Instance Method Summary collapse

Constructor Details

#initialize(root, options) ⇒ PlainText

Returns a new instance of PlainText.



30
31
32
33
# File 'lib/liquid_markdown/converter/plain_text.rb', line 30

def initialize(root, options)
  super
  @plain_text = '' # bin for plain text
end

Instance Method Details

#convert(el) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/liquid_markdown/converter/plain_text.rb', line 35

def convert(el)
  type = el.type
  category = ::Kramdown::Element.category(el)

  @plain_text << convert_type(type, el) if TEXT_TYPES.include?(type)
  @plain_text << "\n" if category == :block

  el.children.each { |e| convert(e) }

  @plain_text.strip if type == :root
end

#convert_entity(el) ⇒ Object



55
56
57
# File 'lib/liquid_markdown/converter/plain_text.rb', line 55

def convert_entity(el)
  el.value.char
end

#convert_smart_quote(el) ⇒ Object



59
60
61
# File 'lib/liquid_markdown/converter/plain_text.rb', line 59

def convert_smart_quote(el)
  smart_quote_entity(el).char
end

#convert_text(el) ⇒ Object



51
52
53
# File 'lib/liquid_markdown/converter/plain_text.rb', line 51

def convert_text(el)
  el.value
end

#convert_type(type, el) ⇒ Object



47
48
49
# File 'lib/liquid_markdown/converter/plain_text.rb', line 47

def convert_type(type, el)
  send("convert_#{type}", el)
end

#convert_typographic_sym(el) ⇒ Object



63
64
65
66
67
# File 'lib/liquid_markdown/converter/plain_text.rb', line 63

def convert_typographic_sym(el)
  ::Kramdown::Converter::Html::TYPOGRAPHIC_SYMS[el.value]
      .map(&:char)
      .join('')
end