1CommonMark::Node(3) User Contributed Perl Documentation CommonMark::Node(3)
2
3
4
6 CommonMark::Node - Node of the CommonMark parse tree
7
9 my $html = $node->render(format => 'html');
10
11 my $header = $doc->first_child;
12 my $level = $header->get_header_level;
13 my $paragraph = $header->next;
14
15 my $link = CommonMark::Node->new(CommonMark::NODE_LINK);
16 $link->set_url('http://example.com/');
17 my $text = CommonMark::Node->new(CommonMark::NODE_TEXT);
18 $text->set_literal('link text');
19 $link->append_child($link_text);
20 $paragraph->append_child($link);
21
22 $doc->render_html;
23
25 "CommonMark::Node" represents a node of the parse tree.
26
27 new
28 my $node = CommonMark::Node->new($type);
29
30 Creates a new node of type $type. See "Node types" for a list of types.
31 Note that the node creation functions provide a more powerful
32 interface.
33
34 Rendering
35 my $result = $node->render(
36 format => $string,
37 sourcepos => $bool, # Optional
38 hardbreaks => $bool, # Optional
39 nobreaks => $bool, # Optional
40 unsafe => $bool, # Optional
41 width => $int, # Optional
42 );
43
44 Convenience function to render documents. Supported formats are 'html',
45 'xml', 'man', 'commonmark', and 'latex'.
46
47 "sourcepos", "hardbreaks", "nobreaks", and "unsafe" enable the
48 respective render options.
49
50 "width" is passed to renderers that support it.
51
52 my $html = $node->render_html( [$options] )
53 my $xml = $node->render_xml( [$options] )
54 my $man = $node->render_man( [$options], [$width] )
55 my $md = $node->render_commonmark( [$options], [$width] )
56 my $latex = $node->render_latex( [$options], [$width] )
57
58 These methods render the contents of the node in the respective format.
59
60 $options is a bit field created by ORing the following constants:
61
62 CommonMark::OPT_DEFAULT => 0
63 CommonMark::OPT_SOURCEPOS
64 CommonMark::OPT_HARDBREAKS
65 CommonMark::OPT_UNSAFE
66 CommonMark::OPT_NOBREAKS
67
68 Render options can be imported from CommonMark with tag "opt".
69
70 use CommonMark qw(:opt);
71
72 $options may be omitted and defaults to "OPT_DEFAULT".
73
74 "SOURCEPOS" adds information about line numbers in the source file to
75 the XML and HTML formats.
76
77 "HARDBREAKS" translates "softbreak" nodes (typically corresponding to
78 newlines in the input) to hard line breaks. This is only supported by
79 some renderers. The HTML renderer, for example, generates a "<br />"
80 instead of a newline character.
81
82 "NOBREAKS" translates "softbreak" nodes to spaces. Requires libcmark
83 0.25 or higher.
84
85 "UNSAFE" only affects the HTML renderer. It allows raw HTML blocks and
86 some dangerous links.
87
88 See the documentation of libcmark for a more detailed explanation of
89 the render options.
90
91 $width specifies the number of characters at which lines are broken. A
92 value of 0 disables line wrapping. The default is 0.
93
94 Accessors
95 # Integer values
96
97 my $int = $node->get_type;
98 my $int = $node->get_header_level;
99 my $int = $node->get_list_type;
100 my $int = $node->get_list_delim;
101 my $int = $node->get_list_start;
102 my $int = $node->get_list_tight;
103 my $int = $node->get_start_line;
104 my $int = $node->get_start_column;
105 my $int = $node->get_end_line;
106 my $int = $node->get_end_column;
107
108 $node->set_header_level($int);
109 $node->set_list_type($int);
110 $node->set_list_delim($int);
111 $node->set_list_start($int);
112 $node->set_list_tight($int);
113
114 # String values
115
116 my $string = $node->get_type_string;
117 my $string = $node->get_literal;
118 my $string = $node->get_title;
119 my $string = $node->get_url;
120 my $string = $node->get_fence_info;
121 my $string = $node->get_on_enter;
122 my $string = $node->get_on_exit;
123
124 $node->set_literal($string);
125 $node->set_title($string);
126 $node->set_url($string);
127 $node->set_fence_info($string);
128 $node->set_on_enter($string);
129 $node->set_on_exit($string);
130
131 Various accessors to get and set integer and string values of a node.
132 Not all values are supported by every type of node. Getters return 0 or
133 "undef" for unsupported values. Setters die on failure.
134
135 See "Constants" for a list of constants used for node types, list
136 types, and list delimiters.
137
138 Tree traversal
139 my $iterator = $node->iterator;
140
141 Creates a new CommonMark::Iterator to walk through the descendants of
142 the node.
143
144 my $next = $node->next;
145 my $prev = $node->previous;
146 my $parent = $node->parent;
147 my $child = $node->first_child;
148 my $child = $node->last_child;
149
150 These methods return the respective node in the tree structure.
151
152 Tree manipulation
153 $node->unlink;
154 $node->replace($other);
155 $node->insert_before($other);
156 $node->insert_after($other);
157 $node->prepend_child($other);
158 $node->append_child($other);
159
160 "unlink" removes a node and all its descendants from the tree.
161
162 "replace" replaces $node with $other, unlinking $node.
163
164 "insert_before" and "insert_after" insert the $other node before or
165 after $node. "append_child" and "prepend_child" append or prepend
166 $other to the children of $node. $other is unlinked before it is moved
167 to its new position.
168
169 These methods may die on failure, for example if the document structure
170 is violated.
171
172 Constants
173 Node types
174
175 CommonMark::NODE_NONE => 0
176 CommonMark::NODE_DOCUMENT
177 CommonMark::NODE_BLOCK_QUOTE
178 CommonMark::NODE_LIST
179 CommonMark::NODE_ITEM
180 CommonMark::NODE_CODE_BLOCK
181 CommonMark::NODE_HTML_BLOCK
182 CommonMark::NODE_CUSTOM_BLOCK
183 CommonMark::NODE_PARAGRAPH
184 CommonMark::NODE_HEADING
185 CommonMark::NODE_THEMATIC_BREAK
186 CommonMark::NODE_TEXT
187 CommonMark::NODE_SOFTBREAK
188 CommonMark::NODE_LINEBREAK
189 CommonMark::NODE_CODE
190 CommonMark::NODE_HTML_INLINE
191 CommonMark::NODE_CUSTOM_INLINE
192 CommonMark::NODE_EMPH
193 CommonMark::NODE_STRONG
194 CommonMark::NODE_LINK
195 CommonMark::NODE_IMAGE
196
197 Node types can be imported from CommonMark with tag "node".
198
199 use CommonMark qw(:node);
200
201 List types
202
203 CommonMark::NO_LIST => 0
204 CommonMark::BULLET_LIST
205 CommonMark::ORDERED_LIST
206
207 List types can be imported from CommonMark with tag "list".
208
209 use CommonMark qw(:list);
210
211 Delimiter types for ordered lists
212
213 CommonMark::NO_DELIM => 0
214 CommonMark::PERIOD_DELIM
215 CommonMark::PAREN_DELIM
216
217 Delimiter types can be imported from CommonMark with tag "delim".
218
219 use CommonMark qw(:delim);
220
222 This software is copyright (C) by Nick Wellnhofer.
223
224 This is free software; you can redistribute it and/or modify it under
225 the same terms as the Perl 5 programming language system itself.
226
227
228
229perl v5.32.1 2021-01-27 CommonMark::Node(3)