Class: MarkdownStreamFormatter

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

Constant Summary collapse

RESET =
"\e[0m"
TITLE =
"\e[33m"
CODE =
"\e[32m"
BOLD =
"\e[1m"
ITALIC =
"\e[3m"
QUOTE =
"\e[32m\e[1m"

Instance Method Summary collapse

Constructor Details

#initializeMarkdownStreamFormatter

Returns a new instance of MarkdownStreamFormatter.



9
10
11
12
13
14
15
16
# File 'lib/markdown_stream_formatter.rb', line 9

def initialize
  @line_buffer = ""
  @title = false
  @bold = false
  @italic = false
  @quote = false
  @code = false
end

Instance Method Details

#next(chunk) ⇒ Object



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/markdown_stream_formatter.rb', line 18

def next(chunk)
  return unless chunk

  chunk.lines.map do |line|
    line = titleize(line)
    line = boldify(line)
    line = italicize(line)
    line = quotify(line)
    line = codify(line)

    if line.end_with? "\n"
      @title = false
      @bold = false
      @italic = false
      @quote = false if @line_buffer.size == 0 && line == "\n"
      @code = false

      @line_buffer = ""
      line += RESET
    else
      @line_buffer += line
    end

    line
  end.join
end