class Node
Nested
Definitions
def dup
Duplicate the current node and all children.
Implementation
def dup
# This is a bit crazy, but it's the best I can come up with right now:
node = Markly.parse(self.to_markdown)
# If we aren't duplicating a document, we return `first_child` as the root will be a document node:
if self.type == :document
return node
else
return node.first_child
end
end
def walk(&block)
Public: An iterator that "walks the tree," descending into children recursively.
block - A Proc representing the action to take for each child
Implementation
def walk(&block)
return enum_for(:walk) unless block_given?
yield self
each do |child|
child.walk(&block)
end
end
def to_html(flags: DEFAULT, extensions: [])
Public: Convert the node to an HTML string.
flags - A Symbol or Array of Symbols indicating the render options
extensions - An Array of Symbols indicating the extensions to use
Returns a String.
Implementation
def to_html(flags: DEFAULT, extensions: [])
_render_html(flags, extensions).force_encoding("utf-8")
end
def to_commonmark(flags: DEFAULT, width: 0)
Public: Convert the node to a CommonMark string.
flags - A Symbol or Array of Symbols indicating the render options
width - Column to wrap the output at
Returns a String.
Implementation
def to_commonmark(flags: DEFAULT, width: 0)
_render_commonmark(flags, width).force_encoding("utf-8")
end
def to_plaintext(flags: DEFAULT, width: 0)
Public: Convert the node to a plain text string.
flags - A Symbol or Array of Symbols indicating the render options
width - Column to wrap the output at
Returns a String.
Implementation
def to_plaintext(flags: DEFAULT, width: 0)
_render_plaintext(flags, width).force_encoding("utf-8")
end
def each
Public: Iterate over the children (if any) of the current pointer.
Implementation
def each
return enum_for(:each) unless block_given?
child = first_child
while child
next_child = child.next
yield child
child = next_child
end
end
def delete_until
Delete all nodes until the block returns true.
Signature
-
returns
Markly::Node the node that returned true.
Implementation
def delete_until
current = self
while current
return current if yield(current)
next_node = current.next
current.delete
current = next_node
end
end
def replace_section(new_node, replace_header: true, remove_subsections: true)
Replace a section (header + content) with a new node.
Signature
-
parameter
new_nodeMarkly::Node the node to replace the section with.
-
parameter
replace_headerBoolean whether to replace the header itself or not.
-
parameter
remove_subsectionsBoolean whether to remove subsections or not.
Implementation
def replace_section(new_node, replace_header: true, remove_subsections: true)
# Delete until the next heading:
self.next&.delete_until do |node|
node.type == :header && (!remove_subsections || node.header_level <= self.header_level)
end
self.append_after(new_node) if new_node
self.delete if replace_header
end
def append_after(node)
Append the given node after the current node.
It's okay to provide a document node, its children will be appended.
Signature
-
parameter
nodeMarkly::Node the node to append.
Implementation
def append_after(node)
if node.type == :document
node = node.first_child
end
current = self
while node
next_node = node.next
current.insert_after(node)
current = node
node = next_node
end
end
def append_before(node)
Append the given node before the current node.
It's okay to provide a document node, its children will be appended.
Signature
-
parameter
nodeMarkly::Node the node to append.
Implementation
def append_before(node)
if node.type == :document
node = node.first_child
end
current = self
while node
next_node = node.next
current.insert_before(node)
node = next_node
end
end
def extract_children
Extract the children as a fragment.
Signature
-
returns
Markly::Node the fragment.
Implementation
def extract_children
fragment = Markly::Node.new(:custom_inline)
while child = self.first_child
fragment.append_child(child)
end
fragment
end