Class: Redcarpet::Render::Terminal

Inherits:
StripDown
  • Object
show all
Defined in:
app/markdown.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#try_ansiObject

Returns the value of attribute try_ansi.



15
16
17
# File 'app/markdown.rb', line 15

def try_ansi
  @try_ansi
end

#widthObject

Returns the value of attribute width.



14
15
16
# File 'app/markdown.rb', line 14

def width
  @width
end

Instance Method Details

#ansi?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/markdown.rb', line 41

def ansi?
    defined?(Term::ANSIColor) and try_ansi
end

#block_code(code, lang) ⇒ Object



69
70
71
# File 'app/markdown.rb', line 69

def block_code(code, lang)
    code + "\n"
end

#codespan(text) ⇒ Object



54
55
56
57
58
59
60
# File 'app/markdown.rb', line 54

def codespan(text)
    if ansi?
        Term::ANSIColor.underline + text + Term::ANSIColor.reset
    else
        "'" + text + "'"
    end
end

#double_emphasis(text) ⇒ Object



73
74
75
76
77
78
79
# File 'app/markdown.rb', line 73

def double_emphasis(text)
    if ansi?
        Term::ANSIColor.bold + text + Term::ANSIColor.reset
    else
        '**' + text + '**'
    end
end

#emphasis(text) ⇒ Object



81
82
83
84
85
86
87
# File 'app/markdown.rb', line 81

def emphasis(text)
    if ansi?
        Term::ANSIColor.italic + text + Term::ANSIColor.reset
    else
        '*' + text + '*'
    end
end

#header(title, level) ⇒ Object



45
46
47
48
49
50
51
52
# File 'app/markdown.rb', line 45

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

#linebreakObject



105
106
107
# File 'app/markdown.rb', line 105

def linebreak
    "\n"
end

#list(content, list_type) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'app/markdown.rb', line 113

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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/markdown.rb', line 123

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



109
110
111
# File 'app/markdown.rb', line 109

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.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/markdown.rb', line 21

def reflow(text, prefix='')
    lines = []
    line_len = 0
    line = ''
    width = @width - prefix.size
    text.split.each do |word|
        word_size = Term::ANSIColor::uncolor(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



97
98
99
100
101
102
103
# File 'app/markdown.rb', line 97

def strikethrough(text)
    if ansi?
        Term::ANSIColor.striketrhough + text + Term::ANSIColor.reset
    else
        '_' + text + '_'
    end
end

#triple_emphasis(text) ⇒ Object



61
62
63
64
65
66
67
# File 'app/markdown.rb', line 61

def triple_emphasis(text)
    if ansi?
        Term::ANSIColor.bold + text + Term::ANSIColor.reset
    else
        '***' + text + '***'
    end
end

#underline(text) ⇒ Object



89
90
91
92
93
94
95
# File 'app/markdown.rb', line 89

def underline(text)
    if ansi?
        Term::ANSIColor.italic + text + Term::ANSIColor.reset
    else
        '_' + text + '_'
    end
end