class Belajar::Markdown::Printer
Constants
- BOLD
- CODE
- H1
- H2
- LINE
Attributes
window[R]
Public Class Methods
new(window:)
click to toggle source
# File lib/belajar/markdown/printer.rb, line 15 def initialize(window:) @window = window end
Public Instance Methods
print(text)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/belajar/markdown/printer.rb, line 84 def print_escaped(text) window.write(text.gsub(/(\\#)/, '#')) end
print_h1(text)
click to toggle source
# File lib/belajar/markdown/printer.rb, line 32 def print_h1(text) window.heading(text.sub(/\A#\s?/, '')) end
print_h2(text)
click to toggle source
# 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()
click to toggle source
# File lib/belajar/markdown/printer.rb, line 80 def print_line window.write('-' * (Curses.cols - 2)) end