Class: Belajar::Markdown::Printer
- Inherits:
-
Object
- Object
- Belajar::Markdown::Printer
- Defined in:
- lib/belajar/markdown/printer.rb
Constant Summary collapse
- H1 =
‘# heading’
/\A\#{1}[^#]+/
- H2 =
‘## sub heading’
/\A\#{2}[^#]+/
- BOLD =
‘text’
/\*[^*]*\*/
- LINE =
‘—’ vertical line
/\A-{3,}/
- CODE =
‘`code line`’
/`[^`]*`/
Instance Attribute Summary collapse
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#initialize(window:) ⇒ Printer
constructor
A new instance of Printer.
- #print(text) ⇒ Object
- #print_bold(text) ⇒ Object
- #print_code_or_bold(text) ⇒ Object
- #print_escaped(text) ⇒ Object
- #print_h1(text) ⇒ Object
- #print_h2(text) ⇒ Object
- #print_line ⇒ Object
Constructor Details
#initialize(window:) ⇒ Printer
Returns a new instance of Printer.
15 16 17 |
# File 'lib/belajar/markdown/printer.rb', line 15 def initialize(window:) @window = window end |
Instance Attribute Details
#window ⇒ Object (readonly)
Returns the value of attribute window.
13 14 15 |
# File 'lib/belajar/markdown/printer.rb', line 13 def window @window end |
Instance Method Details
#print(text) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/belajar/markdown/printer.rb', line 19 def print(text) text_line = RubyDoc.parse(text) case text_line when H1 then print_h1(text_line) when H2 then print_h2(text_line) when /(#{CODE}|#{BOLD})/ then print_code_or_bold(text_line) when BOLD then print_bold(text_line) when LINE then print_line else print_escaped(text_line) end end |
#print_bold(text) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/belajar/markdown/printer.rb', line 68 def print_bold(text) text.chars.each_with_index do |char, index| if char == '*' && text[index - 1] != '\\' emphasized = !emphasized next end character = text[index..(index + 1)].to_s == '\\*' ? '' : char emphasized ? window.emphasize(character) : window.write(character) end end |
#print_code_or_bold(text) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/belajar/markdown/printer.rb', line 41 def print_code_or_bold(text) emphasized = false highlighted = false text.chars.each_with_index do |char, index| if char == '*' && text[index - 1] != '\\' emphasized = !emphasized next end if char == '`' highlighted = !highlighted next end character = text[index..(index + 1)] == '\\*' ? '' : char if highlighted window.red(character) elsif emphasized window.emphasize(character) else window.write(character) end end end |
#print_escaped(text) ⇒ Object
84 85 86 |
# File 'lib/belajar/markdown/printer.rb', line 84 def print_escaped(text) window.write(text.gsub(/(\\#)/, '#')) end |
#print_h1(text) ⇒ Object
32 33 34 |
# File 'lib/belajar/markdown/printer.rb', line 32 def print_h1(text) window.heading(text.sub(/\A#\s?/, '')) end |
#print_h2(text) ⇒ Object
36 37 38 39 |
# File 'lib/belajar/markdown/printer.rb', line 36 def print_h2(text) text_decoration = Curses::A_UNDERLINE | Curses::A_NORMAL window.emphasize(text.sub(/\A##\s?/, ''), text_decoration) end |
#print_line ⇒ Object
80 81 82 |
# File 'lib/belajar/markdown/printer.rb', line 80 def print_line window.write('-' * (Curses.cols - 2)) end |