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

new(h1 = '') click to toggle source

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

bold(txt) click to toggle source

Adds text which is bold.

# File lib/core/markdown.rb, line 74
def bold(txt)
  text('**' + txt.rstrip.lstrip + '**')
end
bold_and_cursive(txt) click to toggle source

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
code(txt) click to toggle source

Adds a line of code.

# File lib/core/markdown.rb, line 89
def code(txt)
  text('`' + txt + '`')
end
cursive(txt) click to toggle source

Adds text which is cursive.

# File lib/core/markdown.rb, line 69
def cursive(txt)
  text('*' + txt.rstrip.lstrip + '*')
end
header(level, txt) click to toggle source

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
horizontal_line() click to toggle source

Adds a horizontal line.

# File lib/core/markdown.rb, line 63
def horizontal_line
  text('---')
  paragraph
end
image(alternative, path, hover) click to toggle source

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

# File lib/core/markdown.rb, line 137
def image(alternative, path, hover)
  text('![' + alternative + ']' + '(' + path + ' ' + '"' + hover + '"' + ')')
end
new_line() click to toggle source

Adds a new (empty) line.

# File lib/core/markdown.rb, line 142
def new_line
  two_spaces = '  '
  text(two_spaces + line_feed)
end
ol(bullet_points) click to toggle source

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
paragraph() click to toggle source

Adds a new paragraph.

# File lib/core/markdown.rb, line 148
def paragraph
  text(line_feed + line_feed)
end
quote(txt) click to toggle source

Adds a quote.

# File lib/core/markdown.rb, line 84
def quote(txt)
  text('> ' + txt)
end
text(txt) click to toggle source

Adds a string to the current content.

# File lib/core/markdown.rb, line 42
def text(txt)
  @md += txt
  self
end
to_s() click to toggle source

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
ul(bullet_points) click to toggle source

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

line_feed() click to toggle source
# File lib/core/markdown.rb, line 163
def line_feed
  "\n"
end