Class: GithubToCanvasQuiz::MarkdownBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/github_to_canvas_quiz/markdown_builder.rb

Overview

Custom DSL for building a Markdown string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarkdownBuilder

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

#blocksObject

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"

Yields:

  • (builder)

Raises:

  • (ArgumentError)


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

Parameters:

  • text (String)

    blockquote text



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

Parameters:

  • hash (Hash)

    the data to covert to YAML for the frontmatter



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

Parameters:

  • text (String)

    heading text



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

Parameters:

  • text (String)

    heading text



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

Parameters:

  • markdown (String)

    markdown string



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

Parameters:

  • text (String)

    paragraph text



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

def p(text)
  blocks << escape(text)
end

#to_sString

Returns markdown produced by joining all blocks.

Returns:

  • (String)

    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

Parameters:

  • texts* (Strings)

    a list of text to convert to 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