Class: Streamd::Renderer

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/streamd/renderer.rb

Constant Summary collapse

BOLD =

ANSI color codes

"\033[1m"
DIM =
"\033[2m"
ITALIC =
"\033[3m"
UNDERLINE =
"\033[4m"
STRIKETHROUGH =
"\033[9m"
GRAY =

Colors

"\033[90m"
RESET =

Reset

"\033[0m"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.createObject



29
30
31
32
33
34
# File 'lib/streamd/renderer.rb', line 29

def create
  Redcarpet::Markdown.new(self,
    tables: true, fenced_code_blocks: true,
    space_after_headers: true, no_intra_emphasis: true
  )
end

Instance Method Details

#codespan(code) ⇒ Object



54
55
56
# File 'lib/streamd/renderer.rb', line 54

def codespan(code)
  "\e[38;2;80;250;123m#{code}#{RESET}"
end

#double_emphasis(text) ⇒ Object



58
59
60
# File 'lib/streamd/renderer.rb', line 58

def double_emphasis(text)
  "\e[38;2;255;184;108;1m#{text}#{RESET}"
end

#emphasis(text) ⇒ Object



62
63
64
# File 'lib/streamd/renderer.rb', line 62

def emphasis(text)
  "\e[38;2;255;184;108;1m#{text}#{RESET}"
end

#entity(text) ⇒ Object

Low level rendering



118
119
120
# File 'lib/streamd/renderer.rb', line 118

def entity(text)
  text
end

#footnote_ref(number) ⇒ Object



113
114
115
# File 'lib/streamd/renderer.rb', line 113

def footnote_ref(number)
  "#{DIM}[#{number}]#{RESET}"
end

#highlight(text) ⇒ Object



105
106
107
# File 'lib/streamd/renderer.rb', line 105

def highlight(text)
  "#{BOLD}#{text}#{RESET}"
end

#image(link, title, alt_text) ⇒ Object



66
67
68
69
# File 'lib/streamd/renderer.rb', line 66

def image(link, title, alt_text)
  alt_text ||= ""
  "#{GRAY}[image: #{alt_text}]#{RESET}"
end

#linebreakObject



71
72
73
# File 'lib/streamd/renderer.rb', line 71

def linebreak
  "\n"
end


75
76
77
# File 'lib/streamd/renderer.rb', line 75

def link(link, title, content)
  "\e[38;2;255;121;198m#{content}#{RESET}\e[38;2;139;233;253;4m#{UNDERLINE}#{link}#{RESET}"
end

#normal_text(text) ⇒ Object



122
123
124
# File 'lib/streamd/renderer.rb', line 122

def normal_text(text)
  text
end

#quote(text) ⇒ Object



109
110
111
# File 'lib/streamd/renderer.rb', line 109

def quote(text)
  "#{ITALIC}#{text}#{RESET}"
end

#raw_html(raw_html) ⇒ Object



79
80
81
# File 'lib/streamd/renderer.rb', line 79

def raw_html(raw_html)
  raw_html
end

#strikethrough(text) ⇒ Object



87
88
89
# File 'lib/streamd/renderer.rb', line 87

def strikethrough(text)
  "#{STRIKETHROUGH}#{text}#{RESET}"
end

#superscript(text) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/streamd/renderer.rb', line 91

def superscript(text)
  # Superscript approximation using superscript Unicode characters
  mapping = {
    '0' => '', '1' => '¹', '2' => '²', '3' => '³', '4' => '',
    '5' => '', '6' => '', '7' => '', '8' => '', '9' => '',
    '+' => '', '-' => '', '=' => '', '(' => '', ')' => ''
  }
  text.each_char.map { |c| mapping[c] || c }.join
end

#table(header, body) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/streamd/renderer.rb', line 37

def table(header, body)
  Terminal::Table.new do |t|
    t.headings = header[1..-1].split('|')
    t.rows = body.split("\n").map{|row| row[1..-1].split('|')}
    t.style = { :border_top => false, :border_bottom => false }
    t.instance_variable_set(:@style, FixedStyle.new(t.style))
  end.to_s + "\n\n"
end

#table_cell(content, alignment, header = false) ⇒ Object



50
51
52
# File 'lib/streamd/renderer.rb', line 50

def table_cell(content, alignment, header = false)
  "|#{content.strip}"
end

#table_row(content) ⇒ Object



46
47
48
# File 'lib/streamd/renderer.rb', line 46

def table_row(content)
  "#{content}\n"
end

#triple_emphasis(text) ⇒ Object



83
84
85
# File 'lib/streamd/renderer.rb', line 83

def triple_emphasis(text)
  "#{BOLD}#{ITALIC}#{UNDERLINE}#{text}#{RESET}"
end

#underline(text) ⇒ Object



101
102
103
# File 'lib/streamd/renderer.rb', line 101

def underline(text)
  "#{UNDERLINE}#{text}#{RESET}"
end