Class: Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/core/markdown.rb

Overview

The Markdown object. All its methods (with the exception of to_s) return ‘self’. That way you can use this object as fluent builder to create your Markdown content.

author

Niklas Schultz

version

0.1.0

license

MII

Instance Method Summary collapse

Constructor Details

#initialize(h1 = '') ⇒ Markdown

Creates a new object.

You can ignore the argument so no header will be added on construction of this object.



36
37
38
39
# File 'lib/core/markdown.rb', line 36

def initialize(h1 = '')
 @md = ''
 unless h1.empty? then header(1, h1) end
end

Instance Method Details

#bold(txt) ⇒ Object

Adds text which is bold.



74
75
76
# File 'lib/core/markdown.rb', line 74

def bold(txt)
  text('**' + txt.rstrip.lstrip + '**')
end

#bold_and_cursive(txt) ⇒ Object

Adds text which is both bold and cursive.



79
80
81
# File 'lib/core/markdown.rb', line 79

def bold_and_cursive(txt)
  text('***' + txt.rstrip.lstrip + '***')
end

#code(txt) ⇒ Object

Adds a line of code.



89
90
91
# File 'lib/core/markdown.rb', line 89

def code(txt)
  text('`' + txt + '`')
end

#cursive(txt) ⇒ Object

Adds text which is cursive.



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

def cursive(txt)
  text('*' + txt.rstrip.lstrip + '*')
end

#header(level, txt) ⇒ Object

Adds a header to the markdown content.

The first argument specifies the header level (h1, h2…h6).

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/core/markdown.rb', line 50

def header(level, txt)
  raise ArgumentError, 'header level must not be 0 or smaller' if level <= 0
  raise ArgumentError, 'header level must not be greater than 6' if level > 6

  hashes = ''
  level.times do
    hashes += '#'
  end
  text(hashes + ' ' + txt)
  paragraph
end

#horizontal_lineObject

Adds a horizontal line.



63
64
65
66
# File 'lib/core/markdown.rb', line 63

def horizontal_line
  text('---')
  paragraph
end

Adds a hyperlink.

This method takes three arguments:

  1. The description of the hyperlink (the actual text shown)

  2. The target of the hyperlink (e.g. google.com)

  3. The text which will be displayed if you hover over the link



127
128
129
# File 'lib/core/markdown.rb', line 127

def hyperlink(desc, target, hover)
  text('[' + desc + ']' + '(' + target + ' ' + '"' + hover + '"' + ')')
end

#image(alternative, path, hover) ⇒ Object

Adds an image.

This method takes three arguments:

  1. The alternative text displayed if no image could be loaded

  2. The path/resource of the image (e.g on your file system)

  3. The text which will be displayed if you hover over the image



137
138
139
# File 'lib/core/markdown.rb', line 137

def image(alternative, path, hover)
  text('![' + alternative + ']' + '(' + path + ' ' + '"' + hover + '"' + ')')
end

#new_lineObject

Adds a new (empty) line.



142
143
144
145
# File 'lib/core/markdown.rb', line 142

def new_line
  two_spaces = '  '
  text(two_spaces + line_feed)
end

#ol(bullet_points) ⇒ Object

Adds an organized list out of the given array.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/core/markdown.rb', line 108

def ol(bullet_points)
  unless bullet_points.respond_to?(:each_with_index)
    raise ArgumentError, 'arg must respond to "each_width_index" call'
  end

  md_list = ''
  bullet_points.each_with_index do |v, i|
    md_list += (i + 1).to_s + '. ' + v + line_feed
  end
  text(md_list)
  paragraph
end

#paragraphObject

Adds a new paragraph.



148
149
150
# File 'lib/core/markdown.rb', line 148

def paragraph
  text(line_feed + line_feed)
end

#quote(txt) ⇒ Object

Adds a quote.



84
85
86
# File 'lib/core/markdown.rb', line 84

def quote(txt)
  text('> ' + txt)
end

#text(txt) ⇒ Object

Adds a string to the current content.



42
43
44
45
# File 'lib/core/markdown.rb', line 42

def text(txt)
  @md += txt
  self
end

#to_sObject

Converts this object to a string.

In other words this method should be the final call you should invoke when you done creating the markdown content. After this call you can use the produced string and show it in a markdown viewer for example.



157
158
159
# File 'lib/core/markdown.rb', line 157

def to_s
  @md
end

#ul(bullet_points) ⇒ Object

Adds an unorganized list out of the given array.



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/core/markdown.rb', line 94

def ul(bullet_points)
  unless bullet_points.respond_to?(:each)
    raise ArgumentError, 'arg must respond to "each" call'
  end

  md_list = ''
  bullet_points.each do |v|
    md_list += '* ' + v + line_feed
  end
  text(md_list)
  paragraph
end