Class: Redcarpet::Render::Terminal
- Inherits:
-
StripDown
- Object
- StripDown
- Redcarpet::Render::Terminal
- Defined in:
- app/markdown.rb
Instance Attribute Summary collapse
-
#try_ansi ⇒ Object
Returns the value of attribute try_ansi.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #ansi? ⇒ Boolean
- #block_code(code, lang) ⇒ Object
- #codespan(text) ⇒ Object
- #double_emphasis(text) ⇒ Object
- #emphasis(text) ⇒ Object
- #header(title, level) ⇒ Object
- #linebreak ⇒ Object
- #list(content, list_type) ⇒ Object
- #list_item(content, list_type) ⇒ Object
- #paragraph(text) ⇒ Object
-
#reflow(text, prefix = '') ⇒ Object
reflow text so lines are not longer than @width.
- #strikethrough(text) ⇒ Object
- #strip_term_sequence(text) ⇒ Object
- #triple_emphasis(text) ⇒ Object
- #underline(text) ⇒ Object
Instance Attribute Details
#try_ansi ⇒ Object
Returns the value of attribute try_ansi.
15 16 17 |
# File 'app/markdown.rb', line 15 def try_ansi @try_ansi end |
#width ⇒ Object
Returns the value of attribute width.
14 15 16 |
# File 'app/markdown.rb', line 14 def width @width end |
Instance Method Details
#ansi? ⇒ Boolean
46 47 48 |
# File 'app/markdown.rb', line 46 def ansi? defined?(Term::ANSIColor) and try_ansi end |
#block_code(code, lang) ⇒ Object
74 75 76 |
# File 'app/markdown.rb', line 74 def block_code(code, lang) code + "\n" end |
#codespan(text) ⇒ Object
59 60 61 62 63 64 65 |
# File 'app/markdown.rb', line 59 def codespan(text) if ansi? Term::ANSIColor.underline + text + Term::ANSIColor.reset else "'" + text + "'" end end |
#double_emphasis(text) ⇒ Object
78 79 80 81 82 83 84 |
# File 'app/markdown.rb', line 78 def double_emphasis(text) if ansi? Term::ANSIColor.bold + text + Term::ANSIColor.reset else '**' + text + '**' end end |
#emphasis(text) ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/markdown.rb', line 86 def emphasis(text) if ansi? Term::ANSIColor.italic + text + Term::ANSIColor.reset else '*' + text + '*' end end |
#header(title, level) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'app/markdown.rb', line 50 def header(title, level) if ansi? Term::ANSIColor.bold + title + Term::ANSIColor.reset + "\n\n" else sep = (level == 1) ? '=' : '-' title + "\n" + (sep * title.size) + "\n" end end |
#linebreak ⇒ Object
110 111 112 |
# File 'app/markdown.rb', line 110 def linebreak "\n" end |
#list(content, list_type) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'app/markdown.rb', line 118 def list(content, list_type) case list_type when :ordered @list_count = 0 "#{content}\n" when :unordered "\n#{content}\n" end end |
#list_item(content, list_type) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/markdown.rb', line 128 def list_item(content, list_type) prefix = '' case list_type when :ordered @list_count ||= 0 @list_count += 1 prefix = "#{@list_count}. " when :unordered prefix = "* " end text = reflow(content, ' ' * prefix.size) text += "\n" if text =~ /\n./ prefix + text end |
#paragraph(text) ⇒ Object
114 115 116 |
# File 'app/markdown.rb', line 114 def paragraph(text) reflow(text) + "\n" end |
#reflow(text, prefix = '') ⇒ Object
reflow text so lines are not longer than @width. prefix is used only after the first line as this is what we want in lists.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/markdown.rb', line 26 def reflow(text, prefix='') lines = [] line_len = 0 line = '' width = @width - prefix.size text.split.each do |word| word_size = strip_term_sequence(word).size if (line_len + word_size) >= width lines << line line = prefix + word + ' ' line_len = prefix.size + word_size + 1 else line += word + ' ' line_len += word_size + 1 end end lines << line lines.join("\n") + "\n" end |
#strikethrough(text) ⇒ Object
102 103 104 105 106 107 108 |
# File 'app/markdown.rb', line 102 def strikethrough(text) if ansi? Term::ANSIColor.striketrhough + text + Term::ANSIColor.reset else '_' + text + '_' end end |
#strip_term_sequence(text) ⇒ Object
17 18 19 20 |
# File 'app/markdown.rb', line 17 def strip_term_sequence(text) # to be completed... text end |
#triple_emphasis(text) ⇒ Object
66 67 68 69 70 71 72 |
# File 'app/markdown.rb', line 66 def triple_emphasis(text) if ansi? Term::ANSIColor.bold + text + Term::ANSIColor.reset else '***' + text + '***' end end |
#underline(text) ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/markdown.rb', line 94 def underline(text) if ansi? Term::ANSIColor.italic + text + Term::ANSIColor.reset else '_' + text + '_' end end |