Class: ConsoleRenderer
- Inherits:
-
Redcarpet::Render::Base
- Object
- Redcarpet::Render::Base
- ConsoleRenderer
- Defined in:
- lib/console_renderer.rb
Constant Summary collapse
- @@listitemid =
0
Class Method Summary collapse
Instance Method Summary collapse
- #autolink(link, link_type) ⇒ Object
- #block_code(code, language) ⇒ Object
- #block_html(raw_html) ⇒ Object
- #block_quote(quote) ⇒ Object
- #codespan(code) ⇒ Object
- #double_emphasis(text) ⇒ Object
- #emphasis(text) ⇒ Object
- #header(text, header_level) ⇒ Object
- #hrule ⇒ Object
- #image(link, title, alt_text) ⇒ Object
- #linebreak ⇒ Object
- #link(link, title, content) ⇒ Object
- #list(contents, list_type) ⇒ Object
- #list_item(text, list_type) ⇒ Object
- #paragraph(text) ⇒ Object
- #raw_html(raw_html) ⇒ Object
- #strikethrough(text) ⇒ Object
- #superscript(text) ⇒ Object
- #table(header, body) ⇒ Object
- #table_cell(content, alignment) ⇒ Object
- #table_row(content) ⇒ Object
- #triple_emphasis(text) ⇒ Object
Class Method Details
.syntax_highlight(code, inline = true) ⇒ Object
8 9 10 11 12 13 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 |
# File 'lib/console_renderer.rb', line 8 def self.syntax_highlight(code, inline=true) tokenizer = Syntax.load "ruby" c_line = "" code.split("\n").each do |line| c_line += " " unless inline begin bkp = c_line tokenizer.tokenize( line ) do |token| case token.group.to_s when "comment" then c_line = c_line + token.color(:green) when "constant" then c_line = c_line + token.color(:blue) when "expr" then c_line = c_line + token.color(:red) when "ident" then c_line = c_line + token.color(:white) when "keyword" then c_line = c_line + token.color(:yellow) when "normal" then c_line = c_line + token.color(:cyan) when "number" then c_line = c_line + token.color(:red) when "punct" then c_line = c_line + token.color(:white) when "string" then c_line = c_line + token.color(:red) when "symbol" then c_line = c_line + token.color(:green) else c_line += token end end rescue c_line = bkp + line end c_line += "\n" unless inline end c_line end |
Instance Method Details
#autolink(link, link_type) ⇒ Object
100 101 102 |
# File 'lib/console_renderer.rb', line 100 def autolink(link, link_type) link.color(:cyan) end |
#block_code(code, language) ⇒ Object
40 41 42 |
# File 'lib/console_renderer.rb', line 40 def block_code(code, language) "\n" + (ConsoleRenderer.syntax_highlight(code, false)) + "\n" end |
#block_html(raw_html) ⇒ Object
52 53 54 |
# File 'lib/console_renderer.rb', line 52 def block_html(raw_html) raw_html end |
#block_quote(quote) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/console_renderer.rb', line 44 def block_quote(quote) ret = "\n" quote.split("\n").each do |line| ret += "|".color(:cyan) + line + "\n" end ret + "\n" end |
#codespan(code) ⇒ Object
104 105 106 |
# File 'lib/console_renderer.rb', line 104 def codespan(code) ConsoleRenderer.syntax_highlight(code) end |
#double_emphasis(text) ⇒ Object
108 109 110 |
# File 'lib/console_renderer.rb', line 108 def double_emphasis(text) text.bright end |
#emphasis(text) ⇒ Object
112 113 114 |
# File 'lib/console_renderer.rb', line 112 def emphasis(text) text.underline end |
#header(text, header_level) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/console_renderer.rb', line 56 def header(text, header_level) text = "\n" + text + "\n" if header_level == 1 text.bright.underline else text.underline end end |
#hrule ⇒ Object
65 66 67 |
# File 'lib/console_renderer.rb', line 65 def hrule() "___________________________\n".color(:yellow) end |
#image(link, title, alt_text) ⇒ Object
116 117 118 |
# File 'lib/console_renderer.rb', line 116 def image(link, title, alt_text) ("<" + (alt_text || "") + ">").color(:green) + ("(image at: " + (link || "") + ")").color(:cyan) end |
#linebreak ⇒ Object
120 121 122 |
# File 'lib/console_renderer.rb', line 120 def linebreak() "\n\n" end |
#link(link, title, content) ⇒ Object
124 125 126 |
# File 'lib/console_renderer.rb', line 124 def link(link, title, content) (content || "") + " " + ("<#{link}#{title ? " :" + title : ''}>").color(:cyan) end |
#list(contents, list_type) ⇒ Object
69 70 71 72 |
# File 'lib/console_renderer.rb', line 69 def list(contents, list_type) @@listitemid = 0 contents end |
#list_item(text, list_type) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/console_renderer.rb', line 74 def list_item(text, list_type) case list_type when :unordered " " + "-".color(:cyan) + " " + text when :ordered @@listitemid += 1 " " + (@@listitemid.to_s + ".").color(:cyan) + " " + text end end |
#paragraph(text) ⇒ Object
84 85 86 |
# File 'lib/console_renderer.rb', line 84 def paragraph(text) text + "\n" end |
#raw_html(raw_html) ⇒ Object
128 129 130 |
# File 'lib/console_renderer.rb', line 128 def raw_html(raw_html) raw_html end |
#strikethrough(text) ⇒ Object
136 137 138 |
# File 'lib/console_renderer.rb', line 136 def strikethrough(text) text end |
#superscript(text) ⇒ Object
140 141 142 |
# File 'lib/console_renderer.rb', line 140 def superscript(text) ("^" + text).color(:red) end |
#table(header, body) ⇒ Object
88 89 90 |
# File 'lib/console_renderer.rb', line 88 def table(header, body) body end |
#table_cell(content, alignment) ⇒ Object
96 97 98 |
# File 'lib/console_renderer.rb', line 96 def table_cell(content, alignment) content end |
#table_row(content) ⇒ Object
92 93 94 |
# File 'lib/console_renderer.rb', line 92 def table_row(content) content end |
#triple_emphasis(text) ⇒ Object
132 133 134 |
# File 'lib/console_renderer.rb', line 132 def triple_emphasis(text) text.bright.underline end |