Class: Markdown
- Inherits:
-
Object
- Object
- Markdown
- 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
-
#bold(txt) ⇒ Object
Adds text which is bold.
-
#bold_and_cursive(txt) ⇒ Object
Adds text which is both bold and cursive.
-
#code(txt) ⇒ Object
Adds a line of code.
-
#cursive(txt) ⇒ Object
Adds text which is cursive.
-
#header(level, txt) ⇒ Object
Adds a header to the markdown content.
-
#horizontal_line ⇒ Object
Adds a horizontal line.
-
#hyperlink(desc, target, hover) ⇒ Object
Adds a hyperlink.
-
#image(alternative, path, hover) ⇒ Object
Adds an image.
-
#initialize(h1 = '') ⇒ Markdown
constructor
Creates a new object.
-
#new_line ⇒ Object
Adds a new (empty) line.
-
#ol(bullet_points) ⇒ Object
Adds an organized list out of the given array.
-
#paragraph ⇒ Object
Adds a new paragraph.
-
#quote(txt) ⇒ Object
Adds a quote.
-
#text(txt) ⇒ Object
Adds a string to the current content.
-
#to_s ⇒ Object
Converts this object to a string.
-
#ul(bullet_points) ⇒ Object
Adds an unorganized list out of the given array.
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).
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_line ⇒ Object
Adds a horizontal line.
63 64 65 66 |
# File 'lib/core/markdown.rb', line 63 def horizontal_line text('---') paragraph end |
#hyperlink(desc, target, hover) ⇒ Object
Adds a hyperlink.
This method takes three arguments:
-
The description of the hyperlink (the actual text shown)
-
The target of the hyperlink (e.g. google.com)
-
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:
-
The alternative text displayed if no image could be loaded
-
The path/resource of the image (e.g on your file system)
-
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_line ⇒ Object
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 |
#paragraph ⇒ Object
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_s ⇒ Object
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 |