Class: GithubToCanvasQuiz::MarkdownBuilder
- Inherits:
-
Object
- Object
- GithubToCanvasQuiz::MarkdownBuilder
- Defined in:
- lib/github_to_canvas_quiz/markdown_builder.rb
Overview
Custom DSL for building a Markdown string
Instance Attribute Summary collapse
-
#blocks ⇒ Object
Returns the value of attribute blocks.
Class Method Summary collapse
-
.build {|builder| ... } ⇒ Object
Provides a custom DSL for building a Markdown string Usage:.
Instance Method Summary collapse
-
#blockquote(text) ⇒ Object
Adds a blockquote.
-
#frontmatter(hash) ⇒ Object
Adds a frontmatter block.
-
#h1(text) ⇒ Object
Adds a H1 heading.
-
#h2(text) ⇒ Object
Adds a H2 heading.
-
#html_to_markdown(html) ⇒ Object
TODO: This doesn’t belong here…
-
#initialize ⇒ MarkdownBuilder
constructor
A new instance of MarkdownBuilder.
-
#md(markdown) ⇒ Object
Adds a block of arbitrary markdown.
-
#p(text) ⇒ Object
Adds a paragraph.
-
#to_s ⇒ String
Markdown produced by joining all blocks.
-
#ul(*texts) ⇒ Object
Adds multiple list items.
Constructor Details
#initialize ⇒ MarkdownBuilder
Returns a new instance of MarkdownBuilder.
26 27 28 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 26 def initialize @blocks = [] end |
Instance Attribute Details
#blocks ⇒ Object
Returns the value of attribute blocks.
24 25 26 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 24 def blocks @blocks end |
Class Method Details
.build {|builder| ... } ⇒ Object
Provides a custom DSL for building a Markdown string Usage:
MarkdownBuilder.build do |md|
md.h1('Hello')
md.ul('item 1', 'item 2')
md.blockquote('comment')
end
# => "# Hello\n\n- item 1\n- item 2\n\n> comment\n"
16 17 18 19 20 21 22 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 16 def self.build raise ArgumentError, 'no block given' unless block_given? builder = new yield(builder) builder.to_s end |
Instance Method Details
#blockquote(text) ⇒ Object
Adds a blockquote
61 62 63 64 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 61 def blockquote(text) text = text.gsub("\n", "\n> ") blocks << "> #{text}" end |
#frontmatter(hash) ⇒ Object
Adds a frontmatter block
37 38 39 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 37 def frontmatter(hash) blocks << "#{hash.to_yaml.strip}\n---" end |
#h1(text) ⇒ Object
Adds a H1 heading
43 44 45 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 43 def h1(text) blocks << "# #{text}" end |
#h2(text) ⇒ Object
Adds a H2 heading
49 50 51 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 49 def h2(text) blocks << "## #{text}" end |
#html_to_markdown(html) ⇒ Object
TODO: This doesn’t belong here…
79 80 81 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 79 def html_to_markdown(html) ReverseMarkdown.convert(html, github_flavored: true).strip end |
#md(markdown) ⇒ Object
Adds a block of arbitrary markdown
74 75 76 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 74 def md(markdown) blocks << markdown.strip end |
#p(text) ⇒ Object
Adds a paragraph
55 56 57 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 55 def p(text) blocks << escape(text) end |
#to_s ⇒ String
Returns markdown produced by joining all blocks.
31 32 33 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 31 def to_s "#{blocks.join("\n\n")}\n" end |
#ul(*texts) ⇒ Object
Adds multiple list items
68 69 70 |
# File 'lib/github_to_canvas_quiz/markdown_builder.rb', line 68 def ul(*texts) blocks << texts.map { |text| "- #{escape(text)}" }.join("\n") end |