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.



9
10
11
12
13
14
15
# File 'lib/abide_dev_utils/markdown.rb', line 9

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, **kwargs, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/abide_dev_utils/markdown.rb', line 33

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

Instance Method Details

#anchor(text) ⇒ Object



94
95
96
# File 'lib/abide_dev_utils/markdown.rb', line 94

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

#bold(text) ⇒ Object



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

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

#code(text) ⇒ Object



86
87
88
# File 'lib/abide_dev_utils/markdown.rb', line 86

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

#code_block(text, language: nil) ⇒ Object



90
91
92
# File 'lib/abide_dev_utils/markdown.rb', line 90

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

#h1(text) ⇒ Object



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

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

#h2(text) ⇒ Object



53
54
55
# File 'lib/abide_dev_utils/markdown.rb', line 53

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

#h3(text) ⇒ Object



57
58
59
# File 'lib/abide_dev_utils/markdown.rb', line 57

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

#italic(text) ⇒ Object



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

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


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

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

#paragraph(text) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#title(text) ⇒ Object



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

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

#to_fileObject



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

def to_file
  this_markdown = to_markdown
  Tempfile.create('markdown') do |f|
    f.write(this_markdown)
    check_file_content(f.path, this_markdown)
    FileUtils.mv(f.path, @file)
  end
end

#to_markdownObject Also known as: to_s



17
18
19
20
21
# File 'lib/abide_dev_utils/markdown.rb', line 17

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

#ul(text, indent: 0) ⇒ Object



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

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

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