class

Cmark::Node

Inherits Reference < Object

A node is a construct of the abstract syntax tree (AST) for a given markdown content.

At the very minimum every node has a NodeType —available via #type— even when invalid. However, given that each type of node has a set of required properties —which if are not set may lead to unintended behavior— all initializers of Node are protected. Nodes can be created in a safer manner either with the module NodeMaker, or with the parsing methods of the moduleCmark (which return a node with NodeType::Document).

Instance methods can be grouped into:

  • Tree traversal

    • #next
    • #previous
    • #parent
    • #grandparent
    • #first_child
    • #last_child
    • #children
  • Accessors

    • #type
    • #literal
    • #literal=
    • #heading_level
    • #heading_level=
    • #list_type
    • #list_type=
    • #list_delim
    • #list_delim=
    • #list_start
    • #list_start=
    • #list_tight?
    • #list_tight=
    • #fence_info
    • #fence_info=
    • #fencing_details
    • #fencing_details=
    • #url
    • #url=
    • #title
    • #title=
    • #on_enter
    • #on_enter=
    • #on_exit
    • #on_exit=
    • #user_data
    • #user_data=
    • #start_line
    • #end_line
    • #start_column
    • #end_column
    • #autolink?
    • #table_string_content
    • #table_string_content=
    • #table_columns
    • #table_columns=
    • #table_alignments
    • #table_alignments=
    • #table_row_header?
    • #table_row_header=
    • #tasklist_item?
    • #tasklist_item=
    • #tasklist_item_checked?
    • #tasklist_item_checked=
    • #footnote_reference_index
    • #footnote_definition_count
    • #footnote_definition_literal
    • #footnote_parent_definition_literal
  • Tree manipulation

    • #unlink
    • #insert_before
    • #insert_after
    • #replace_with
    • #prepend_child
    • #append_child
    • #consolidate_text_nodes
  • Rendering

    • #render_xml
    • #render_html
    • #render_plaintext
    • #render_commonmark
    • #render_latex
    • #render_man
  • Equality

    • #==
  • Containment

    • #can_contain?

NOTE: The Node class is a thin wrapper for the node struct of the underlying cmark-gfm C library, as such, familiarity with the GFM Spec is required for advanced node creation, processing, and rendering.

Instance methods

==(other : Node)

Returns true if both instances point to the same underlying node structure.

Source
append_child(child : Node)

Adds child to the end of the children of node; raises TreeManipulationError on failure.

Source
can_contain?(node_type : NodeType) : Bool

Returns true node can contain node_type, false otherwise.

Source
children

Returns children nodes or an empty array if there are none.

Source
consolidate_text_nodes

Consolidates adjacent text nodes.

Source
end_column

Returns the column on which node ends.

Source
end_line

Returns the line on which node ends.

Source
fence_info

Returns the info string of a fenced code block.

Source
fence_info=(info : String) : String

Sets the info string of a fenced code block; on failure it raises NodeSetterError.

Source
fencing_details

Returns fencing details of a code block.

Returns nil if called on a node that is not a code block.

Source
fencing_details=(details : FencingDetails) : FencingDetails

Sets fencing details of a code block; on failure it raises NodeSetterError.

Source
first_child

Returns the first_child node of this node.

Source
footnote_definition_count

Returns the count the footnote definition.

Returns 0 if the node is not a footnote definition.

Source
footnote_definition_literal

Returns the definition literal of the footnote.

Returns an empty string if the node is not a footnote definition.

Source
footnote_parent_definition_literal

Returns the definition literal of the footnote for this reference.

Returns an empty string if the node is not a footnote reference.

Source
footnote_reference_index

Returns the index of the footnote reference.

Returns 0 if the node is not a footnote reference.

Source
grandparent

Returns the grandparent node of this node.

Source
heading_level

Returns the heading level of node, or 0 if node is not a heading.

Source
heading_level=(heading_level : Int32) : Int32

Sets the heading level of node; on failure it raises NodeSetterError.

Source
insert_after(sibling : Node)

Inserts sibling after node; raises TreeManipulationError on failure.

Source
insert_before(sibling : Node)

Inserts sibling before node; raises TreeManipulationError on failure.

Source
last_child

Returns the last_child node of this node.

Source
list_delim

Returns the list delimiter of node, using DelimType::None if node is not a list.

Source
list_delim=(list_delim : DelimType) : DelimType

Sets the list delimiter of node; on failure it raises NodeSetterError.

Source
list_start

Returns starting number of node, if it is an ordered list, otherwise 0.

Source
list_start=(list_start : Int32) : Int32

Sets the list delimiter of node; on failure it raises NodeSetterError.

Source
list_tight=(tight : Bool) : Bool

Sets the list tightness of node; on failure it raises NodeSetterError.

Source
list_tight?

Returns true if node is a tight list, or false if loose.

Source
list_type

Returns the list type of node, using ListType::None if node is not a list.

Source
list_type=(list_type : ListType) : ListType

Sets the list type of node; on failure it raises NodeSetterError.

Source
literal

Returns the literal string contents of node, or an empty string if none is set.

Source
literal=(literal : String) : String

Sets the literal string contents of node; on failure it raises NodeSetterError.

Source
next

Returns the next node of this node.

Source
on_enter

Returns the literal on_enter of a custom node or an empty string if unset.

Source
on_enter=(on_enter : String) : String

Sets the literal on_enter of a custom node; on failure it raises NodeSetterError.

Any children of the node will be rendered after this text.

Source
on_exit

Returns the literal on_exit of a custom node or an empty string if unset.

Returns nil if called on a node that is not custom.

Source
on_exit=(on_exit : String) : String

Sets the literal on_exit of a custom node; on failure it raises NodeSetterError.

Any children of the node will be rendered before this text.

Source
parent

Returns the parent node of this node.

Source
prepend_child(child : Node)

Adds child to the beginning of the children of node; raises TreeManipulationError on failure.

Source
previous

Returns the previous node of this node.

Source
render_commonmark(options = Option::None, width = 120) : String

Renders node tree as a commonmark string, including GFM extension nodes, if present.

Source
render_html(options = Option::None, extensions = Extension::None) : String

Renders node tree as an HTML string.

Source
render_latex(options = Option::None, width = 120) : String

Renders node tree as a LaTeX string.

Source
render_man(options = Option::None, width = 80) : String

Renders node tree as a groff man page string.

Source
render_plaintext(options = Option::None, width = 120) : String

Renders node tree as a plaintext string.

Source
render_xml(options = Option::None) : String

Renders node tree an XML string.

Source
replace_with(new_node : Node)

Replaces node with new_node; raises TreeManipulationError on failure.

Source
start_column

Returns the column on which node begins.

Source
start_line

Returns the line on which node begins.

Source
table_alignments

Returns the alignments of columns for the table.

Returns an empty array if called on a node that is not a table.

Source
table_alignments=(alignments : Array(Alignment)) : Array(Alignment)

Sets the alignments of columns for the table; on failure it raises NodeSetterError.

It should be called only on table nodes.

Example for a table node with two columns:

node.table_alignments = [Alignment::Center, Alignment::Right]
Source
table_cell_alignment

Returns node table alignment or Alignment::None if called on a node that is not a table cell.

Source
table_columns

Returns the number of columns for the table.

Returns 0 if called on a node that is not a table.

Source
table_columns=(n_columns : UInt16) : UInt16

Sets the number of columns for the table; on failure it raises NodeSetterError.

It must be called only on table and table row nodes that already have defined columns.

Source
table_row_header=(header : Bool) : Bool

Sets a table row as table header.

Source
table_row_header?

Returns true if the the node is a header table row, false otherwise.

Source
table_string_content

Returns the string content of tables and table cells, otherwise an empty string.

With the following table

| foo | bar |
| --- | --- |
| baz | bim |

the string content of table is the header row | foo | bar |. and the string content of the cells is foo, bar, baz, and bim.

Source
table_string_content=(content : String) : String

Sets the table string content for tables and table cells.

Beware of setting incorrect string content. See #table_string_content for more information.

Source
tasklist_item=(tasklist : Bool) : Bool

Sets a list item as tasklist item; on failure it raises NodeSetterError.

It must be called only on nodes with NodeType::Item.

Source
tasklist_item?

Returns true is a node is a tasklist item, false otherwise.

Source
tasklist_item_checked=(checked : Bool) : Bool

Sets the checked status of tasklist item: on failure it raises NodeSetterError.

Source
tasklist_item_checked?

Returns true is a node is a tasklist_item and is checked, false otherwise.

Source
title

Returns the title of a link or image node, or an empty string if no title is set.

Source
title=(title : String) : String

Sets the title of a link or image node; on failure it raises NodeSetterError.

Source
type

Returns the type of node.

Source
url

Returns the URL of a link or image node, or an empty string if no URL is set.

Source
url=(url : String) : String

Sets the URL of node; on failure it raises NodeSetterError.

It must be called only on link or image nodes.

Source
user_data

Returns the user data of node, which can then be unboxed.

Example:

node = NodeMaker.text("Hi")
node.user_data.null? # => true
node.user_data = Box.box(:greetings)
user_data = Box(Symbol).unbox(node.user_data)
user_data # => :grettings
Source
user_data=(user_data : Pointer(Void)) : Pointer(Void)

Sets the user data of node, which should be already boxed; on failure it raises NodeSetterError.

NOTE: user_data must be boxed into the node struct of the C library; see #user_data.

Source