Class: AbideDevUtils::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/markdown.rb

Overview

Formats text for output in markdown

Instance Method Summary collapse

Constructor Details

#initialize(file, with_toc: true) ⇒ Markdown

Returns a new instance of Markdown.



6
7
8
9
10
11
12
# File 'lib/abide_dev_utils/markdown.rb', line 6

def initialize(file, with_toc: true)
  @file = file
  @with_toc = with_toc
  @toc = ["## Table of Contents\n"]
  @body = []
  @title = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/abide_dev_utils/markdown.rb', line 24

def method_missing(name, *args, &block)
  if name.to_s.start_with?('add_')
    add(name.to_s.sub('add_', '').to_sym, *args, &block)
  else
    super
  end
end

Instance Method Details

#anchor(text) ⇒ Object



81
82
83
# File 'lib/abide_dev_utils/markdown.rb', line 81

def anchor(text)
  "##{text.downcase.gsub(%r{\s|_}, '-').tr('.,\'"()', '')}"
end

#bold(text) ⇒ Object



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

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

#code(text) ⇒ Object



73
74
75
# File 'lib/abide_dev_utils/markdown.rb', line 73

def code(text)
  "\`#{text}\`"
end

#code_block(text, language: nil) ⇒ Object



77
78
79
# File 'lib/abide_dev_utils/markdown.rb', line 77

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

#h1(text) ⇒ Object



40
41
42
# File 'lib/abide_dev_utils/markdown.rb', line 40

def h1(text)
  "## #{text}\n"
end

#h2(text) ⇒ Object



44
45
46
# File 'lib/abide_dev_utils/markdown.rb', line 44

def h2(text)
  "### #{text}\n"
end

#h3(text) ⇒ Object



48
49
50
# File 'lib/abide_dev_utils/markdown.rb', line 48

def h3(text)
  "#### #{text}\n"
end

#italic(text) ⇒ Object



64
65
66
# File 'lib/abide_dev_utils/markdown.rb', line 64

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


68
69
70
71
# File 'lib/abide_dev_utils/markdown.rb', line 68

def link(text, url, anchor: false)
  url = anchor(url) if anchor
  "[#{text}](#{url.downcase})"
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/abide_dev_utils/markdown.rb', line 32

def respond_to_missing?(name, include_private = false)
  name.to_s.start_with?('add_') || super
end

#title(text) ⇒ Object



36
37
38
# File 'lib/abide_dev_utils/markdown.rb', line 36

def title(text)
  "# #{text}\n"
end

#to_fileObject



20
21
22
# File 'lib/abide_dev_utils/markdown.rb', line 20

def to_file
  File.write(@file, to_markdown)
end

#to_markdownObject



14
15
16
17
18
# File 'lib/abide_dev_utils/markdown.rb', line 14

def to_markdown
  toc = @toc.join("\n")
  body = @body.join("\n")
  "#{@title}\n#{toc}\n\n#{body}"
end

#ul(text, indent: 0) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/abide_dev_utils/markdown.rb', line 52

def ul(text, indent: 0)
  indented_text = []
  indent.times { indented_text << '  ' } if indent.positive?

  indented_text << "* #{text}"
  indented_text.join
end