Class: MdTransformer::Markdown
- Inherits:
-
Object
- Object
- MdTransformer::Markdown
- 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
-
#children ⇒ Array
readonly
The array of child objects.
-
#content ⇒ String
The content of the Markdown object.
-
#parent ⇒ MdTransformer::Markdown?
readonly
Nil or the parent of the current Markdown object.
-
#title ⇒ String
The title of the Markdown object.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compares Markdown objects with other objects through string conversion.
-
#[](key) ⇒ MdTransformer::Markdown?
Retrieves the child object at a given key if it exists.
-
#[]=(key, value) ⇒ String
Sets the value of the child object at a given key.
-
#dig(key, *rest) ⇒ MdTransformer::Markdown?
Digs through the hash for the child object at the given nested key(s).
-
#each {|k, v| ... } ⇒ Object
For a block { |k, v| … }.
-
#initialize(source = '', options = {}) ⇒ MdTransformer::Markdown
constructor
Creates a new Markdown object.
-
#key?(key) ⇒ Boolean
Checks for whether a child object has a given key.
-
#keys ⇒ Array
Gets all child object keys.
-
#level ⇒ Integer
Calculates the current nesting level of the Markdown object.
-
#root? ⇒ Boolean
Checks whether the current object is the root of the Markdown object.
-
#to_s(options = { title: true }) ⇒ String
Creates a string representing the markdown document’s content from current content and all child content.
-
#value?(value) ⇒ Boolean
Checks for whether a child object has a given value.
-
#values ⇒ Array
Gets all child object values.
-
#write(path, options: { create_dir: true }) ⇒ Integer
Writes the current markdown object to a file.
Constructor Details
#initialize(source = '', options = {}) ⇒ MdTransformer::Markdown
Creates a new Markdown object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/md_transformer/markdown.rb', line 32 def initialize(source = '', = {}) @parent = [:parent] @title = [:title] || '' if [:file] raise InvalidMarkdownPath, "Could not find markdown file at #{source}" unless File.exist?(source) source = File.read(source) @title ||= [:file] end source = translate(source) parse!(source) end |
Instance Attribute Details
#children ⇒ Array (readonly)
Returns the array of child objects.
17 18 19 |
# File 'lib/md_transformer/markdown.rb', line 17 def children @children end |
#content ⇒ String
Returns the content of the Markdown object.
14 15 16 |
# File 'lib/md_transformer/markdown.rb', line 14 def content @content end |
#parent ⇒ MdTransformer::Markdown? (readonly)
Returns nil or the parent of the current Markdown object.
20 21 22 |
# File 'lib/md_transformer/markdown.rb', line 20 def parent @parent end |
#title ⇒ String
Returns 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
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
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.
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)
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| … }
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
71 72 73 |
# File 'lib/md_transformer/markdown.rb', line 71 def key?(key) !self[key].nil? end |
#keys ⇒ Array
Gets all child object keys
64 65 66 |
# File 'lib/md_transformer/markdown.rb', line 64 def keys @children.map(&:title) end |
#level ⇒ Integer
Calculates the current nesting level of the Markdown object
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
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
120 121 122 123 124 125 |
# File 'lib/md_transformer/markdown.rb', line 120 def to_s( = { title: true }) title_str = root? ? '' : "#{'#' * level} #{@title}\n" md_string = "#{[: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
84 85 86 |
# File 'lib/md_transformer/markdown.rb', line 84 def value?(value) !@children.find { |c| c == value }.nil? end |
#values ⇒ Array
Gets all child object values
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
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 [:create_dir] File.write(path, to_s) end |