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
-
.bold(text) ⇒ String
Create bolded Markdown text.
-
.code(text, language: nil) ⇒ Object
Create a code block.
-
.escape(text) ⇒ Object
Return a string with all Markdown syntax escaped.
-
.h1(title) ⇒ Object
Create headers of various levels.
- .h2(title) ⇒ Object
- .h3(title) ⇒ Object
- .h4(title) ⇒ Object
- .h5(title) ⇒ Object
- .h6(title) ⇒ Object
-
.horizontal_rule ⇒ Object
(also: hr)
Create a horizontal rule.
-
.image(alt_text, url) ⇒ Object
Create a Markdown image.
-
.italic(text) ⇒ String
(also: italics)
Create italicised Markdown text.
-
.link(text, url) ⇒ Object
Create a Markdown link.
-
.ordered_list(items, start: 1) ⇒ Object
(also: ol)
Create an ordered list.
-
.quote(text) ⇒ Object
Create a block quote.
-
.strikethrough(text) ⇒ Object
(also: strike)
Create struck-through Markdown text.
-
.unordered_list(items) ⇒ Object
(also: ul)
Create an unordered list.
Class Method Details
.bold(text) ⇒ String
Create bolded Markdown text.
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.
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.
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_rule ⇒ Object 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.
74 75 76 |
# File 'lib/marble.rb', line 74 def image(alt_text, url) "" end |
.italic(text) ⇒ String Also known as: italics
Create italicised Markdown text.
49 50 51 |
# File 'lib/marble.rb', line 49 def italic(text) "*#{text}*" end |
.link(text, url) ⇒ Object
Create a Markdown link.
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.
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.
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.
87 88 89 |
# File 'lib/marble.rb', line 87 def unordered_list(items) items.map { |item| "- #{item}\n" }.join end |