Class: PlainTextToMarkdown

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

Defined Under Namespace

Classes: CodeBlock, Line

Instance Method Summary collapse

Constructor Details

#initialize(plaintext, opts = {}) ⇒ PlainTextToMarkdown

Returns a new instance of PlainTextToMarkdown.



6
7
8
9
10
11
12
# File 'lib/plain_text_to_markdown.rb', line 6

def initialize(plaintext, opts = {})
  @plaintext = plaintext
  @lines = []

  @format_flowed = opts[:format_flowed] || false
  @delete_flowed_space = opts[:delete_flowed_space] || false
end

Instance Method Details

#to_markdownObject



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
44
45
46
47
# File 'lib/plain_text_to_markdown.rb', line 14

def to_markdown
  prepare_lines
  classify_lines

  markdown = +""
  last_quote_level = 0
  last_line_blank = false

  @lines.each do |line|
    current_line_blank = line.text.blank?

    unless last_line_blank && current_line_blank
      if line.quote_level > 0
        quote_identifiers = ">" * line.quote_level
        unless line.quote_level >= last_quote_level || current_line_blank
          markdown << quote_identifiers << "\n"
        end
        markdown << quote_identifiers
        markdown << " " unless current_line_blank
      else
        markdown << "\n" unless last_quote_level == 0 || current_line_blank
      end

      markdown << convert_text(line)
      markdown << "\n"
    end

    last_line_blank = current_line_blank
    last_quote_level = line.quote_level
  end

  markdown.rstrip!
  markdown
end