class Markdown
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
Public Class Methods
Creates a new object.
You can ignore the argument so no header will be added on construction of this object.
# File lib/core/markdown.rb, line 36 def initialize(h1 = '') @md = '' unless h1.empty? then header(1, h1) end end
Public Instance Methods
Adds text which is bold.
# File lib/core/markdown.rb, line 74 def bold(txt) text('**' + txt.rstrip.lstrip + '**') end
Adds text which is both bold and cursive.
# File lib/core/markdown.rb, line 79 def bold_and_cursive(txt) text('***' + txt.rstrip.lstrip + '***') end
Adds a line of code.
# File lib/core/markdown.rb, line 89 def code(txt) text('`' + txt + '`') end
Adds text which is cursive.
# File lib/core/markdown.rb, line 69 def cursive(txt) text('*' + txt.rstrip.lstrip + '*') end
Adds a header to the markdown content.
The first argument specifies the header level (h1, h2…h6).
# 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
Adds a horizontal line.
# File lib/core/markdown.rb, line 63 def horizontal_line text('---') paragraph end
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
# File lib/core/markdown.rb, line 127 def hyperlink(desc, target, hover) text('[' + desc + ']' + '(' + target + ' ' + '"' + hover + '"' + ')') end
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
# File lib/core/markdown.rb, line 137 def image(alternative, path, hover) text('![' + alternative + ']' + '(' + path + ' ' + '"' + hover + '"' + ')') end
Adds a new (empty) line.
# File lib/core/markdown.rb, line 142 def new_line two_spaces = ' ' text(two_spaces + line_feed) end
Adds an organized list out of the given array.
# 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
Adds a new paragraph.
# File lib/core/markdown.rb, line 148 def paragraph text(line_feed + line_feed) end
Adds a quote.
# File lib/core/markdown.rb, line 84 def quote(txt) text('> ' + txt) end
Adds a string to the current content.
# File lib/core/markdown.rb, line 42 def text(txt) @md += txt self end
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.
# File lib/core/markdown.rb, line 157 def to_s @md end
Adds an unorganized list out of the given array.
# 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
Private Instance Methods
# File lib/core/markdown.rb, line 163 def line_feed "\n" end