Class: MdTransformer::Markdown

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/md_transformer/markdown.rb,
lib/md_transformer/markdown/section.rb

Overview

Class representing a parsed markdown file

Defined Under Namespace

Classes: Section

Constant Summary collapse

LOWEST_PRECEDENCE =

The lowest valid precedence of a header, allows for up to H6 (###### Header)

6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = '', options = {}) ⇒ MdTransformer::Markdown

Creates a new Markdown object

Parameters:

  • source (String) (defaults to: '')

    the markdown content or path to a markdown file

  • options (Hash) (defaults to: {})

    the options hash

Options Hash (options):

  • :file (Boolean)

    whether to treat the passed source as a file path

  • :parent (MdTransformer::Markdown)

    the parent of the new object

  • :title (String)

    the title of the new object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/md_transformer/markdown.rb', line 32

def initialize(source = '', options = {})
  @parent = options[:parent]
  @title = options[:title] || ''
  if options[:file]
    raise InvalidMarkdownPath, "Could not find markdown file at #{source}" unless File.exist?(source)

    source = File.read(source)
    @title ||= options[:file]
  end
  source = translate(source)
  parse!(source)
end

Instance Attribute Details

#childrenArray (readonly)

Returns the array of child objects.

Returns:

  • (Array)

    the array of child objects



17
18
19
# File 'lib/md_transformer/markdown.rb', line 17

def children
  @children
end

#contentString

Returns the content of the Markdown object.

Returns:

  • (String)

    the content of the Markdown object



14
15
16
# File 'lib/md_transformer/markdown.rb', line 14

def content
  @content
end

#parentMdTransformer::Markdown? (readonly)

Returns nil or the parent of the current Markdown object.

Returns:



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

def parent
  @parent
end

#titleString

Returns the title of the Markdown object.

Returns:

  • (String)

    the title of the Markdown object



11
12
13
# File 'lib/md_transformer/markdown.rb', line 11

def title
  @title
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares Markdown objects with other objects through string conversion

Parameters:

  • other (Object)

    object to compare

Returns:

  • (Integer)

    the result of the <=> operator on the string values of both objects



164
165
166
# File 'lib/md_transformer/markdown.rb', line 164

def <=>(other)
  to_s <=> other.to_s
end

#[](key) ⇒ MdTransformer::Markdown?

Retrieves the child object at a given key if it exists

Parameters:

  • key (String)

    the key of the child object

Returns:



102
103
104
# File 'lib/md_transformer/markdown.rb', line 102

def [](key)
  @children.find { |c| c.title == key }
end

#[]=(key, value) ⇒ String

Sets the value of the child object at a given key. If the key does not exist, a new child object is created.

Parameters:

  • key (String)

    the key of the child object

  • value (String)

    the new value of the child object

Returns:

  • (String)

    the newly assigned value



110
111
112
113
114
# File 'lib/md_transformer/markdown.rb', line 110

def []=(key, value)
  child = self[key] || Markdown.new('', title: key, parent: self)
  child.content = value.to_s
  @children.push(child) unless key?(key)
end

#dig(key, *rest) ⇒ MdTransformer::Markdown?

Digs through the hash for the child object at the given nested key(s)

Parameters:

  • key (String)

    the first key to check

  • rest (Array<String>)

    any number of nested string keys for which to find

Returns:



92
93
94
95
96
97
# File 'lib/md_transformer/markdown.rb', line 92

def dig(key, *rest)
  value = self[key]
  return value if value.nil? || rest.empty?

  value.dig(*rest)
end

#each {|k, v| ... } ⇒ Object

For a block { |k, v| … }

Yields:

  • (k, v)

    Gives the key and value of the object



153
154
155
156
157
158
159
# File 'lib/md_transformer/markdown.rb', line 153

def each
  return enum_for(__method__) unless block_given?

  @children.each do |child|
    yield child.title, child
  end
end

#key?(key) ⇒ Boolean

Checks for whether a child object has a given key

Parameters:

  • key (String)

    the key of the child object

Returns:

  • (Boolean)

    whether the passed key exists



71
72
73
# File 'lib/md_transformer/markdown.rb', line 71

def key?(key)
  !self[key].nil?
end

#keysArray

Gets all child object keys

Returns:

  • (Array)

    the array of child keys



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

def keys
  @children.map(&:title)
end

#levelInteger

Calculates the current nesting level of the Markdown object

Returns:

  • (Integer)

    the nesting level of the object (0 for the root, +1 for each additional level)



145
146
147
148
149
# File 'lib/md_transformer/markdown.rb', line 145

def level
  return 0 if root?

  @parent.level + 1
end

#root?Boolean

Checks whether the current object is the root of the Markdown object

Returns:

  • (Boolean)

    whether the current object is the root



139
140
141
# File 'lib/md_transformer/markdown.rb', line 139

def root?
  @parent.nil?
end

#to_s(options = { title: true }) ⇒ String

Creates a string representing the markdown document’s content from current content and all child content

Parameters:

  • options (Hash) (defaults to: { title: true })

    the options hash

Options Hash (options):

  • :title (Boolean) — default: true

    whether to include the title of the current object in the output

Returns:

  • (String)

    the constructed Markdown string



120
121
122
123
124
125
# File 'lib/md_transformer/markdown.rb', line 120

def to_s(options = { title: true })
  title_str = root? ? '' : "#{'#' * level} #{@title}\n"
  md_string = "#{options[:title] ? title_str : ''}#{@content}#{@children.map(&:to_s).join}"
  md_string << "\n" unless md_string.end_with?("\n")
  md_string
end

#value?(value) ⇒ Boolean

Checks for whether a child object has a given value

Parameters:

  • value (String)

    the value to check for

Returns:

  • (Boolean)

    whether the passed value exists



84
85
86
# File 'lib/md_transformer/markdown.rb', line 84

def value?(value)
  !@children.find { |c| c == value }.nil?
end

#valuesArray

Gets all child object values

Returns:

  • (Array)

    the array of child objects



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

def values
  @children
end

#write(path, options: { create_dir: true }) ⇒ Integer

Writes the current markdown object to a file

Parameters:

  • path (String)

    the path to the new file

  • options (Hash) (defaults to: { create_dir: true })

    the options hash

Options Hash (options:):

  • :create_dir (Boolean) — default: true

    whether to create the parent directories of the path

Returns:

  • (Integer)

    the length of the newly created file



132
133
134
135
# File 'lib/md_transformer/markdown.rb', line 132

def write(path, options: { create_dir: true })
  FileUtils.mkdir_p(File.dirname(path)) if options[:create_dir]
  File.write(path, to_s)
end