module Marble

The Marble Markdown formatter.

Constants

ESCAPED

build a hash of characters that must be escaped and their escaped forms, and a regex to detect the former. not using Array#to_h to maintain compatibility below Ruby 2.6

TO_ESCAPE
VERSION

Public Instance Methods

bold(text) click to toggle source

Create bolded Markdown text. @param text [String] @return [String]

# File lib/marble.rb, line 30
def bold(text)
  "**#{text}**"
end
code(text, language: nil) click to toggle source

Create a code block. If `language` is not `nil`, the code block will be “fenced”. If you want a fenced code block with no syntax highlighting, set `language` to an empty string. @param text [String] @param language [String, nil]

# File lib/marble.rb, line 38
def code(text, language: nil)
  if language.nil?
    "`#{text}`"
  else
    "```#{language}\n#{text}\n```"
  end
end
escape(text) click to toggle source

Return a string with all Markdown syntax escaped. @param text [String]

# File lib/marble.rb, line 23
def escape(text)
  text.gsub(TO_ESCAPE, ESCAPED)
end
h1(title) click to toggle source

Create headers of various levels.

# File lib/marble.rb, line 97
def h1(title)
  "# #{title}"
end
h2(title) click to toggle source
# File lib/marble.rb, line 101
def h2(title)
  "## #{title}"
end
h3(title) click to toggle source
# File lib/marble.rb, line 105
def h3(title)
  "### #{title}"
end
h4(title) click to toggle source
# File lib/marble.rb, line 109
def h4(title)
  "#### #{title}"
end
h5(title) click to toggle source
# File lib/marble.rb, line 113
def h5(title)
  "##### #{title}"
end
h6(title) click to toggle source
# File lib/marble.rb, line 117
def h6(title)
  "###### #{title}"
end
horizontal_rule() click to toggle source

Create a horizontal rule.

# File lib/marble.rb, line 92
def horizontal_rule
  '---'
end
image(alt_text, url) click to toggle source

Create a Markdown image. @param alt_text [String] @param url [String]

# File lib/marble.rb, line 74
def image(alt_text, url)
  "![#{alt_text}](#{url})"
end
italic(text) click to toggle source

Create italicised Markdown text. @param text [String] @return [String]

# File lib/marble.rb, line 49
def italic(text)
  "*#{text}*"
end
ordered_list(items, start: 1) click to toggle source

Create an ordered list. @param items [Array<String>] @param start [Integer]

# File lib/marble.rb, line 81
def ordered_list(items, start: 1)
  items.each.with_index(start).map { |item, n| "#{n}.   #{item}\n" }.join
end
quote(text) click to toggle source

Create a block quote.

# File lib/marble.rb, line 60
def quote(text)
  "> #{text}"
end
strikethrough(text) click to toggle source

Create struck-through Markdown text. @param text [String]

# File lib/marble.rb, line 55
def strikethrough(text)
  "~~#{text}~~"
end
unordered_list(items) click to toggle source

Create an unordered list. @param items [Enumerable<String>]

# File lib/marble.rb, line 87
def unordered_list(items)
  items.map { |item| "-   #{item}\n" }.join
end