Module: Marble

Defined in:
lib/marble.rb

Overview

The Marble Markdown formatter.

Constant Summary collapse

VERSION =
'0.2'
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

%w{! # ( ) * [ \\ ] _ ` | ~}.map { |c| [c, "\\#{c}"] }.to_h
TO_ESCAPE =
Regexp.union(ESCAPED.keys)

Class Method Summary collapse

Class Method Details

.bold(text) ⇒ String

Create bolded Markdown text.

Parameters:

  • text (String)

Returns:

  • (String)


30
31
32
# File 'lib/marble.rb', line 30

def bold(text)
  "**#{text}**"
end

.code(text, language: nil) ⇒ Object

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.

Parameters:

  • text (String)
  • language (String, nil) (defaults to: nil)


38
39
40
41
42
43
44
# File 'lib/marble.rb', line 38

def code(text, language: nil)
  if language.nil?
    "`#{text}`"
  else
    "```#{language}\n#{text}\n```"
  end
end

.escape(text) ⇒ Object

Return a string with all Markdown syntax escaped.

Parameters:

  • text (String)


23
24
25
# File 'lib/marble.rb', line 23

def escape(text)
  text.gsub(TO_ESCAPE, ESCAPED)
end

.h1(title) ⇒ Object

Create headers of various levels.



97
98
99
# File 'lib/marble.rb', line 97

def h1(title)
  "# #{title}"
end

.h2(title) ⇒ Object



101
102
103
# File 'lib/marble.rb', line 101

def h2(title)
  "## #{title}"
end

.h3(title) ⇒ Object



105
106
107
# File 'lib/marble.rb', line 105

def h3(title)
  "### #{title}"
end

.h4(title) ⇒ Object



109
110
111
# File 'lib/marble.rb', line 109

def h4(title)
  "#### #{title}"
end

.h5(title) ⇒ Object



113
114
115
# File 'lib/marble.rb', line 113

def h5(title)
  "##### #{title}"
end

.h6(title) ⇒ Object



117
118
119
# File 'lib/marble.rb', line 117

def h6(title)
  "###### #{title}"
end

.horizontal_ruleObject Also known as: hr

Create a horizontal rule.



92
93
94
# File 'lib/marble.rb', line 92

def horizontal_rule
  '---'
end

.image(alt_text, url) ⇒ Object

Create a Markdown image.

Parameters:

  • alt_text (String)
  • url (String)


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

def image(alt_text, url)
  "![#{alt_text}](#{url})"
end

.italic(text) ⇒ String Also known as: italics

Create italicised Markdown text.

Parameters:

  • text (String)

Returns:

  • (String)


49
50
51
# File 'lib/marble.rb', line 49

def italic(text)
  "*#{text}*"
end

Create a Markdown link.

Parameters:

  • text (String)
  • url (String)


67
68
69
# File 'lib/marble.rb', line 67

def link(text, url)
  "[#{text}](#{url})"
end

.ordered_list(items, start: 1) ⇒ Object Also known as: ol

Create an ordered list.

Parameters:

  • items (Array<String>)
  • start (Integer) (defaults to: 1)


81
82
83
# 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) ⇒ Object

Create a block quote.



60
61
62
# File 'lib/marble.rb', line 60

def quote(text)
  "> #{text}"
end

.strikethrough(text) ⇒ Object Also known as: strike

Create struck-through Markdown text.

Parameters:

  • text (String)


55
56
57
# File 'lib/marble.rb', line 55

def strikethrough(text)
  "~~#{text}~~"
end

.unordered_list(items) ⇒ Object Also known as: ul

Create an unordered list.

Parameters:

  • items (Enumerable<String>)


87
88
89
# File 'lib/marble.rb', line 87

def unordered_list(items)
  items.map { |item| "-   #{item}\n" }.join
end