Class: Markly::Node
- Inherits:
-
Object
- Object
- Markly::Node
- Includes:
- Enumerable, Inspect
- Defined in:
- lib/markly/node.rb,
lib/markly/node/inspect.rb,
ext/markly/markly.c
Overview
Represents a node in a parsed Markdown document tree.
Defined Under Namespace
Modules: Inspect
Constant Summary
Constants included from Inspect
Class Method Summary collapse
-
.new(type) ⇒ Object
Internal: Creates a node based on a node type.
Instance Method Summary collapse
-
#_dup ⇒ Object
Duplicate the current node and all its children.
-
#_render_commonmark(*args) ⇒ Object
Internal: Convert the node to a CommonMark string.
-
#_render_html(rb_options, rb_extensions) ⇒ Object
Internal: Convert the node to an HTML string.
-
#_render_plaintext(*args) ⇒ Object
Internal: Convert the node to a plain textstring.
-
#append_after(node) ⇒ Object
Append the given node after the current node.
-
#append_before(node) ⇒ Object
Append the given node before the current node.
-
#append_child(child) ⇒ Object
Public: Inserts a node as the last child of the current node.
-
#code_info ⇒ Object
Public: Gets the code info of the current node (must be a
:code_block,:front_matter, or inline:codenode). -
#code_info=(info) ⇒ Object
Public: Sets the code info of the current node (must be a
:code_block,:front_matter, or inline:codenode). -
#code_language ⇒ Object
Return the language identifier from the code info string.
-
#delete ⇒ Object
Internal: Unlinks the node from the tree (fixing pointers in parents and siblings appropriately).
-
#delete_until ⇒ Object
Delete all nodes until the block returns true.
-
#dup ⇒ Object
Duplicate the current node and all its children.
-
#each ⇒ Object
Iterate over the direct children of this node.
-
#extract_children ⇒ Object
Extract the children as a fragment.
-
#fence ⇒ Object
Public: Gets fencing details for the current node.
-
#fence_info ⇒ Object
Public: Gets the fence info of the current node (must be a
:code_block). -
#fence_info=(info) ⇒ Object
Public: Sets the fence info of the current node (must be a
:code_block). -
#find_header(title) ⇒ Object
Finds a direct child header with the given text.
-
#first_child ⇒ Object
Public: Fetches the first child of the node.
-
#header_level ⇒ Object
Public: Gets the header level of the current node (must be a
:header). -
#header_level=(level) ⇒ Object
Public: Sets the header level of the current node (must be a
:header). -
#html_escape_href(rb_text) ⇒ Object
Internal: Escapes href URLs safely.
-
#html_escape_html(rb_text) ⇒ Object
Internal: Escapes HTML content safely.
-
#insert_after(sibling) ⇒ Object
Public: Inserts a node as a sibling after the current node.
-
#insert_before(sibling) ⇒ Object
Public: Inserts a node as a sibling before the current node.
-
#last_child ⇒ Object
Public: Fetches the first child of the current node.
-
#list_start ⇒ Object
Public: Gets the starting number the current node (must be an
:ordered_list). -
#list_start=(start) ⇒ Object
Public: Sets the starting number of the current node (must be an
:ordered_list). -
#list_tight ⇒ Object
Public: Gets the tight status the current node (must be a
:list). -
#list_tight=(tight) ⇒ Object
Public: Sets the tight status of the current node (must be a
:list). -
#list_type ⇒ Object
Public: Gets the list type of the current node (must be a
:list). -
#list_type=(list_type) ⇒ Object
Public: Sets the list type of the current node (must be a
:list). -
#next ⇒ Object
Public: Fetches the next sibling of the node.
-
#next_header ⇒ Object
(also: #next_heading)
Finds the next sibling header.
-
#parent ⇒ Object
Public: Fetches the parent of the current node.
- #parent_footnote_def ⇒ Object
-
#prepend_child(child) ⇒ Object
Public: Inserts a node as the first child of the current node.
-
#previous ⇒ Object
Public: Fetches the previous sibling of the current node.
- #replace(other) ⇒ Object
-
#replace_section(new_node, replace_header: true, remove_subsections: true) ⇒ Object
Replace a section (header + content) with a new node.
-
#source_position ⇒ Object
Public: Fetches the source_position of the node.
-
#string_content ⇒ Object
Public: Fetch the string contents of the node.
-
#string_content=(s) ⇒ Object
Public: Sets the string content of the node.
- #table_alignments ⇒ Object
-
#tasklist_item_checked=(item_checked) ⇒ Object
Public: Sets the checkbox state of the current node (must be a
:tasklist). - #tasklist_item_checked? ⇒ Boolean
-
#title ⇒ Object
Public: Gets the title of the current node (must be a
:linkor:image). -
#title=(title) ⇒ Object
Public: Sets the title of the current node (must be a
:linkor:image). -
#to_commonmark(flags: DEFAULT, width: 0) ⇒ Object
(also: #to_markdown)
Convert the node to a CommonMark string.
-
#to_html(flags: DEFAULT, extensions: []) ⇒ Object
Convert the node to an HTML string.
-
#to_plaintext(flags: DEFAULT, width: 0) ⇒ Object
Convert the node to a plain-text string.
-
#type ⇒ Object
Public: Fetches the list type of the node.
-
#type_string ⇒ Object
Public: Returns the type of the current pointer as a string.
-
#url ⇒ Object
Public: Gets the URL of the current node (must be a
:linkor:image). -
#url=(url) ⇒ Object
Public: Sets the URL of the current node (must be a
:linkor:image). -
#walk {|_self| ... } ⇒ Object
Walk the node tree recursively.
Methods included from Inspect
Class Method Details
.new(type) ⇒ Object
Internal: Creates a node based on a node type.
type - A Symbol representing the node to be created. Must be one of the following:
:document:custom:blockquote:list:list_item:code_block:html:paragraph:header:hrule:text:softbreak:linebreak:code:inline_html:emph:strong:link:image
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/markly/markly.c', line 213
static VALUE Markly_Node_new(VALUE self, VALUE type) {
cmark_node_type node_type = 0;
cmark_node *node;
Check_Type(type, T_SYMBOL);
if (type == sym_document)
node_type = CMARK_NODE_DOCUMENT;
else if (type == sym_custom_block)
node_type = CMARK_NODE_CUSTOM_BLOCK;
else if (type == sym_custom_inline)
node_type = CMARK_NODE_CUSTOM_INLINE;
else if (type == sym_blockquote)
node_type = CMARK_NODE_BLOCK_QUOTE;
else if (type == sym_list)
node_type = CMARK_NODE_LIST;
else if (type == sym_list_item)
node_type = CMARK_NODE_ITEM;
else if (type == sym_code_block)
node_type = CMARK_NODE_CODE_BLOCK;
else if (type == sym_html)
node_type = CMARK_NODE_HTML;
else if (type == sym_paragraph)
node_type = CMARK_NODE_PARAGRAPH;
else if (type == sym_header)
node_type = CMARK_NODE_HEADER;
else if (type == sym_hrule)
node_type = CMARK_NODE_HRULE;
else if (type == sym_text)
node_type = CMARK_NODE_TEXT;
else if (type == sym_softbreak)
node_type = CMARK_NODE_SOFTBREAK;
else if (type == sym_linebreak)
node_type = CMARK_NODE_LINEBREAK;
else if (type == sym_code)
node_type = CMARK_NODE_CODE;
else if (type == sym_inline_html)
node_type = CMARK_NODE_INLINE_HTML;
else if (type == sym_emph)
node_type = CMARK_NODE_EMPH;
else if (type == sym_strong)
node_type = CMARK_NODE_STRONG;
else if (type == sym_link)
node_type = CMARK_NODE_LINK;
else if (type == sym_image)
node_type = CMARK_NODE_IMAGE;
else if (type == sym_footnote_reference)
node_type = CMARK_NODE_FOOTNOTE_REFERENCE;
else if (type == sym_footnote_definition)
node_type = CMARK_NODE_FOOTNOTE_DEFINITION;
else
rb_raise(Markly_Error, "invalid node of type %d", node_type);
node = cmark_node_new(node_type);
if (node == NULL) {
rb_raise(Markly_Error, "could not create node of type %d", node_type);
}
return Markly_Node_wrap(node);
}
|
Instance Method Details
#_dup ⇒ Object
Duplicate the current node and all its children.
277 278 279 280 281 282 283 284 285 286 287 |
# File 'ext/markly/markly.c', line 277
static VALUE Markly_Node_duplicate(VALUE self) {
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
cmark_node *copy = cmark_node_clone(node);
if (copy == NULL) {
rb_raise(Markly_Error, "could not duplicate node");
}
return Markly_Node_wrap(copy);
}
|
#_render_commonmark(*args) ⇒ Object
Internal: Convert the node to a CommonMark string.
Returns a String.
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'ext/markly/markly.c', line 596
static VALUE Markly_Node_render_commonmark(int argc, VALUE *argv, VALUE self) {
VALUE rb_options, rb_width;
rb_scan_args(argc, argv, "11", &rb_options, &rb_width);
int width = 120;
if (!NIL_P(rb_width)) {
Check_Type(rb_width, T_FIXNUM);
width = FIX2INT(rb_width);
}
int options;
cmark_node *node;
Check_Type(rb_options, T_FIXNUM);
options = FIX2INT(rb_options);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
char *cmark = cmark_render_commonmark(node, options, width);
VALUE ruby_cmark = rb_str_new2(cmark);
free(cmark);
return ruby_cmark;
}
|
#_render_html(rb_options, rb_extensions) ⇒ Object
Internal: Convert the node to an HTML string.
Returns a String.
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'ext/markly/markly.c', line 550
static VALUE Markly_Node_render_html(VALUE self, VALUE rb_options, VALUE rb_extensions) {
VALUE rb_ext_name;
int i;
cmark_node *node;
cmark_llist *extensions = NULL;
cmark_mem *mem = cmark_get_default_mem_allocator();
Check_Type(rb_options, T_FIXNUM);
Check_Type(rb_extensions, T_ARRAY);
int options = FIX2INT(rb_options);
long extensions_len = RARRAY_LEN(rb_extensions);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
for (i = 0; i < extensions_len; ++i) {
rb_ext_name = RARRAY_PTR(rb_extensions)[i];
if (!SYMBOL_P(rb_ext_name)) {
cmark_llist_free(mem, extensions);
rb_raise(rb_eTypeError, "extension names should be Symbols; got a %"PRIsVALUE"", rb_obj_class(rb_ext_name));
}
cmark_syntax_extension *syntax_extension =
cmark_find_syntax_extension(rb_id2name(SYM2ID(rb_ext_name)));
if (!syntax_extension) {
cmark_llist_free(mem, extensions);
rb_raise(rb_eArgError, "extension %s not found\n", rb_id2name(SYM2ID(rb_ext_name)));
}
extensions = cmark_llist_append(mem, extensions, syntax_extension);
}
char *html = cmark_render_html(node, options, extensions);
VALUE ruby_html = rb_str_new2(html);
cmark_llist_free(mem, extensions);
free(html);
return ruby_html;
}
|
#_render_plaintext(*args) ⇒ Object
Internal: Convert the node to a plain textstring.
Returns a String.
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'ext/markly/markly.c', line 624
static VALUE Markly_Node_render_plaintext(int argc, VALUE *argv, VALUE self) {
VALUE rb_options, rb_width;
rb_scan_args(argc, argv, "11", &rb_options, &rb_width);
int width = 120;
if (!NIL_P(rb_width)) {
Check_Type(rb_width, T_FIXNUM);
width = FIX2INT(rb_width);
}
int options;
cmark_node *node;
Check_Type(rb_options, T_FIXNUM);
options = FIX2INT(rb_options);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
char *text = cmark_render_plaintext(node, options, width);
VALUE ruby_text = rb_str_new2(text);
free(text);
return ruby_text;
}
|
#append_after(node) ⇒ Object
Append the given node after the current node.
It's okay to provide a document node, its children will be appended.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/markly/node.rb', line 155 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 |
#append_before(node) ⇒ Object
Append the given node before the current node.
It's okay to provide a document node, its children will be appended.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/markly/node.rb', line 174 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 |
#append_child(child) ⇒ Object
Public: Inserts a node as the last child of the current node.
child - A child Markly::Node to insert.
Returns true if successful.
Raises Error if the node can't be inserted.
696 697 698 699 700 701 702 703 704 705 706 |
# File 'ext/markly/markly.c', line 696
static VALUE Markly_Node_append_child(VALUE self, VALUE child) {
cmark_node *node1, *node2;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node1);
TypedData_Get_Struct(child, cmark_node, &Markly_Node_Type, node2);
if (!cmark_node_append_child(node1, node2)) {
rb_raise(Markly_Error, "could not append child");
}
return Qtrue;
}
|
#code_info ⇒ Object
Public: Gets the code info of the current node (must be a :code_block,
:front_matter, or inline :code node).
Returns a String representing the code info. Raises a Error if the code info can't be retrieved.
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'ext/markly/markly.c', line 1057
static VALUE Markly_Node_get_code_info(VALUE self) {
const char *code_info;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
code_info = cmark_node_get_code_info(node);
if (code_info == NULL) {
rb_raise(Markly_Error, "could not get code_info");
}
return rb_str_new2(code_info);
}
|
#code_info=(info) ⇒ Object
Public: Sets the code info of the current node (must be a :code_block,
:front_matter, or inline :code node).
info - A String representing the new code info, or nil to clear it.
Raises a Error if the code info can't be set.
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 |
# File 'ext/markly/markly.c', line 1079
static VALUE Markly_Node_set_code_info(VALUE self, VALUE info) {
const char *text = NULL;
cmark_node *node;
if (!NIL_P(info)) {
Check_Type(info, T_STRING);
text = StringValueCStr(info);
}
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
if (!cmark_node_set_code_info(node, text)) {
rb_raise(Markly_Error, "could not set code_info");
}
return Qnil;
}
|
#code_language ⇒ Object
Return the language identifier from the code info string.
63 64 65 |
# File 'lib/markly/node.rb', line 63 def code_language code_info.split(/\s+/, 2).first end |
#delete ⇒ Object
Internal: Unlinks the node from the tree (fixing pointers in parents and siblings appropriately).
491 492 493 494 495 496 497 498 |
# File 'ext/markly/markly.c', line 491
static VALUE Markly_Node_unlink(VALUE self) {
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
cmark_node_unlink(node);
return Qnil;
}
|
#delete_until ⇒ Object
Delete all nodes until the block returns true.
109 110 111 112 113 114 115 116 117 |
# File 'lib/markly/node.rb', line 109 def delete_until current = self while current return current if yield(current) next_node = current.next current.delete current = next_node end end |
#dup ⇒ Object
Duplicate the current node and all its children.
22 23 24 |
# File 'lib/markly/node.rb', line 22 def dup _dup end |
#each ⇒ Object
Iterate over the direct children of this node.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/markly/node.rb', line 81 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 |
#extract_children ⇒ Object
Extract the children as a fragment.
190 191 192 193 194 195 196 197 198 |
# File 'lib/markly/node.rb', line 190 def extract_children fragment = Markly::Node.new(:custom_inline) while child = self.first_child fragment.append_child(child) end fragment end |
#fence ⇒ Object
Public: Gets fencing details for the current node.
Returns a Fence for fenced code blocks, nil otherwise.
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# File 'ext/markly/markly.c', line 1102
static VALUE Markly_Node_get_fence(VALUE self) {
int fence_length = 0;
int fence_offset = 0;
char fence_character = '\0';
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
if (!cmark_node_get_fenced(node, &fence_length, &fence_offset, &fence_character)) {
return Qnil;
}
return rb_struct_new(
Markly_Node_Fence,
rb_str_new(&fence_character, 1),
INT2NUM(fence_length),
INT2NUM(fence_offset)
);
}
|
#fence_info ⇒ Object
Public: Gets the fence info of the current node (must be a :code_block).
Returns a String representing the fence info. Raises a Error if the fence info can't be retrieved.
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
# File 'ext/markly/markly.c', line 1014
static VALUE Markly_Node_get_fence_info(VALUE self) {
const char *fence_info;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
fence_info = cmark_node_get_fence_info(node);
if (fence_info == NULL) {
rb_raise(Markly_Error, "could not get fence_info");
}
return rb_str_new2(fence_info);
}
|
#fence_info=(info) ⇒ Object
Public: Sets the fence info of the current node (must be a :code_block).
info - A String representing the new fence info
Raises a Error if the fence info can't be set.
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 |
# File 'ext/markly/markly.c', line 1035
static VALUE Markly_Node_set_fence_info(VALUE self, VALUE info) {
char *text;
cmark_node *node;
Check_Type(info, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = StringValueCStr(info);
if (!cmark_node_set_fence_info(node, text)) {
rb_raise(Markly_Error, "could not set fence_info");
}
return Qnil;
}
|
#find_header(title) ⇒ Object
Finds a direct child header with the given text.
96 97 98 99 100 101 102 |
# File 'lib/markly/node.rb', line 96 def find_header(title) each do |child| if child.type == :header && child.first_child.string_content == title return child end end end |
#first_child ⇒ Object
Public: Fetches the first child of the node.
Returns a Markly::Node if a child exists, nil otherise.
504 505 506 507 508 509 510 511 |
# File 'ext/markly/markly.c', line 504
static VALUE Markly_Node_first_child(VALUE self) {
cmark_node *node, *child;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
child = cmark_node_first_child(node);
return Markly_Node_wrap(child);
}
|
#header_level ⇒ Object
Public: Gets the header level of the current node (must be a :header).
Returns a Number representing the header level. Raises a Error if the header level can't be retrieved.
835 836 837 838 839 840 841 842 843 844 845 846 847 |
# File 'ext/markly/markly.c', line 835
static VALUE Markly_Node_get_header_level(VALUE self) {
int header_level;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
header_level = cmark_node_get_header_level(node);
if (header_level == 0) {
rb_raise(Markly_Error, "could not get header_level");
}
return INT2NUM(header_level);
}
|
#header_level=(level) ⇒ Object
Public: Sets the header level of the current node (must be a :header).
level - A Number representing the new header level
Raises a Error if the header level can't be set.
856 857 858 859 860 861 862 863 864 865 866 867 868 869 |
# File 'ext/markly/markly.c', line 856
static VALUE Markly_Node_set_header_level(VALUE self, VALUE level) {
int l;
cmark_node *node;
Check_Type(level, T_FIXNUM);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
l = FIX2INT(level);
if (!cmark_node_set_header_level(node, l)) {
rb_raise(Markly_Error, "could not set header_level");
}
return Qnil;
}
|
#html_escape_href(rb_text) ⇒ Object
Internal: Escapes href URLs safely.
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
# File 'ext/markly/markly.c', line 1199
static VALUE Markly_Node_html_escape_href(VALUE self, VALUE rb_text) {
char *result;
cmark_node *node;
Check_Type(rb_text, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
cmark_mem *mem = cmark_node_mem(node);
cmark_strbuf buf = CMARK_BUF_INIT(mem);
if (houdini_escape_href(&buf, (const uint8_t *)RSTRING_PTR(rb_text),
(bufsize_t)RSTRING_LEN(rb_text))) {
result = (char *)cmark_strbuf_detach(&buf);
return rb_str_new2(result);
}
return rb_text;
}
|
#html_escape_html(rb_text) ⇒ Object
Internal: Escapes HTML content safely.
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 |
# File 'ext/markly/markly.c', line 1219
static VALUE Markly_Node_html_escape_html(VALUE self, VALUE rb_text) {
char *result;
cmark_node *node;
Check_Type(rb_text, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
cmark_mem *mem = cmark_node_mem(node);
cmark_strbuf buf = CMARK_BUF_INIT(mem);
if (houdini_escape_html0(&buf, (const uint8_t *)RSTRING_PTR(rb_text),
(bufsize_t)RSTRING_LEN(rb_text), 0)) {
result = (char *)cmark_strbuf_detach(&buf);
return rb_str_new2(result);
}
return rb_text;
}
|
#insert_after(sibling) ⇒ Object
Public: Inserts a node as a sibling after the current node.
sibling - A sibling Markly::Node to insert.
Returns true if successful.
Raises Error if the node can't be inserted.
656 657 658 659 660 661 662 663 664 665 666 |
# File 'ext/markly/markly.c', line 656
static VALUE Markly_Node_insert_after(VALUE self, VALUE sibling) {
cmark_node *node1, *node2;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node1);
TypedData_Get_Struct(sibling, cmark_node, &Markly_Node_Type, node2);
if (!cmark_node_insert_after(node1, node2)) {
rb_raise(Markly_Error, "could not insert after");
}
return Qtrue;
}
|
#insert_before(sibling) ⇒ Object
Public: Inserts a node as a sibling before the current node.
sibling - A sibling Markly::Node to insert.
Returns true if successful.
Raises Error if the node can't be inserted.
534 535 536 537 538 539 540 541 542 543 544 |
# File 'ext/markly/markly.c', line 534
static VALUE Markly_Node_insert_before(VALUE self, VALUE sibling) {
cmark_node *node1, *node2;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node1);
TypedData_Get_Struct(sibling, cmark_node, &Markly_Node_Type, node2);
if (!cmark_node_insert_before(node1, node2)) {
rb_raise(Markly_Error, "could not insert before");
}
return Qtrue;
}
|
#last_child ⇒ Object
Public: Fetches the first child of the current node.
Returns a Markly::Node if a child exists, nil otherise.
712 713 714 715 716 717 718 719 |
# File 'ext/markly/markly.c', line 712
static VALUE Markly_Node_last_child(VALUE self) {
cmark_node *node, *child;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
child = cmark_node_last_child(node);
return Markly_Node_wrap(child);
}
|
#list_start ⇒ Object
Public: Gets the starting number the current node (must be an
:ordered_list).
Returns a Number representing the starting number. Raises a Error if the starting number can't be retrieved.
932 933 934 935 936 937 938 939 940 941 942 943 |
# File 'ext/markly/markly.c', line 932
static VALUE Markly_Node_get_list_start(VALUE self) {
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
if (cmark_node_get_type(node) != CMARK_NODE_LIST ||
cmark_node_get_list_type(node) != CMARK_ORDERED_LIST) {
rb_raise(Markly_Error, "can't get list_start for non-ordered list %d",
cmark_node_get_list_type(node));
}
return INT2NUM(cmark_node_get_list_start(node));
}
|
#list_start=(start) ⇒ Object
Public: Sets the starting number of the current node (must be an
:ordered_list).
level - A Number representing the new starting number
Raises a Error if the starting number can't be set.
953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'ext/markly/markly.c', line 953
static VALUE Markly_Node_set_list_start(VALUE self, VALUE start) {
int s;
cmark_node *node;
Check_Type(start, T_FIXNUM);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
s = FIX2INT(start);
if (!cmark_node_set_list_start(node, s)) {
rb_raise(Markly_Error, "could not set list_start");
}
return Qnil;
}
|
#list_tight ⇒ Object
Public: Gets the tight status the current node (must be a :list).
Returns a true if the list is tight, false otherwise.
Raises a Error if the starting number can't be retrieved.
974 975 976 977 978 979 980 981 982 983 984 985 986 |
# File 'ext/markly/markly.c', line 974
static VALUE Markly_Node_get_list_tight(VALUE self) {
int flag;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
if (cmark_node_get_type(node) != CMARK_NODE_LIST) {
rb_raise(Markly_Error, "can't get list_tight for non-list");
}
flag = cmark_node_get_list_tight(node);
return flag ? Qtrue : Qfalse;
}
|
#list_tight=(tight) ⇒ Object
Public: Sets the tight status of the current node (must be a :list).
tight - A Boolean representing the new tightness
Raises a Error if the tightness can't be set.
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'ext/markly/markly.c', line 995
static VALUE Markly_Node_set_list_tight(VALUE self, VALUE tight) {
int t;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
t = RTEST(tight);
if (!cmark_node_set_list_tight(node, t)) {
rb_raise(Markly_Error, "could not set list_tight");
}
return Qnil;
}
|
#list_type ⇒ Object
Public: Gets the list type of the current node (must be a :list).
Returns a Symbol. Raises a Error if the title can't be retrieved.
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'ext/markly/markly.c', line 877
static VALUE Markly_Node_get_list_type(VALUE self) {
int list_type;
cmark_node *node;
VALUE symbol;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
list_type = cmark_node_get_list_type(node);
if (list_type == CMARK_BULLET_LIST) {
symbol = sym_bullet_list;
} else if (list_type == CMARK_ORDERED_LIST) {
symbol = sym_ordered_list;
} else {
rb_raise(Markly_Error, "could not get list_type");
}
return symbol;
}
|
#list_type=(list_type) ⇒ Object
Public: Sets the list type of the current node (must be a :list).
level - A Symbol representing the new list type
Raises a Error if the list type can't be set.
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 |
# File 'ext/markly/markly.c', line 903
static VALUE Markly_Node_set_list_type(VALUE self, VALUE list_type) {
int type = 0;
cmark_node *node;
Check_Type(list_type, T_SYMBOL);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
if (list_type == sym_bullet_list) {
type = CMARK_BULLET_LIST;
} else if (list_type == sym_ordered_list) {
type = CMARK_ORDERED_LIST;
} else {
rb_raise(Markly_Error, "invalid list_type");
}
if (!cmark_node_set_list_type(node, type)) {
rb_raise(Markly_Error, "could not set list_type");
}
return Qnil;
}
|
#next ⇒ Object
Public: Fetches the next sibling of the node.
Returns a Markly::Node if a sibling exists, nil otherwise.
517 518 519 520 521 522 523 524 |
# File 'ext/markly/markly.c', line 517
static VALUE Markly_Node_next(VALUE self) {
cmark_node *node, *next;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
next = cmark_node_next(node);
return Markly_Node_wrap(next);
}
|
#next_header ⇒ Object Also known as: next_heading
Finds the next sibling header.
137 138 139 140 141 142 143 144 145 |
# File 'lib/markly/node.rb', line 137 def next_header current = self.next while current if current.type == :header return current end current = current.next end end |
#parent ⇒ Object
Public: Fetches the parent of the current node.
Returns a Markly::Node if a parent exists, nil otherise.
725 726 727 728 729 730 731 732 |
# File 'ext/markly/markly.c', line 725
static VALUE Markly_Node_parent(VALUE self) {
cmark_node *node, *parent;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
parent = cmark_node_parent(node);
return Markly_Node_wrap(parent);
}
|
#parent_footnote_def ⇒ Object
1160 1161 1162 1163 1164 1165 1166 1167 1168 |
# File 'ext/markly/markly.c', line 1160
static VALUE Markly_Node_get_parent_footnote_def(VALUE self) {
cmark_node *node;
cmark_node *parent;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
parent = cmark_node_parent_footnote_def(node);
return Markly_Node_wrap(parent);
}
|
#prepend_child(child) ⇒ Object
Public: Inserts a node as the first child of the current node.
child - A child Markly::Node to insert.
Returns true if successful.
Raises Error if the node can't be inserted.
676 677 678 679 680 681 682 683 684 685 686 |
# File 'ext/markly/markly.c', line 676
static VALUE Markly_Node_prepend_child(VALUE self, VALUE child) {
cmark_node *node1, *node2;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node1);
TypedData_Get_Struct(child, cmark_node, &Markly_Node_Type, node2);
if (!cmark_node_prepend_child(node1, node2)) {
rb_raise(Markly_Error, "could not prepend child");
}
return Qtrue;
}
|
#previous ⇒ Object
Public: Fetches the previous sibling of the current node.
Returns a Markly::Node if a parent exists, nil otherise.
738 739 740 741 742 743 744 745 |
# File 'ext/markly/markly.c', line 738
static VALUE Markly_Node_previous(VALUE self) {
cmark_node *node, *previous;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
previous = cmark_node_previous(node);
return Markly_Node_wrap(previous);
}
|
#replace(other) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'ext/markly/markly.c', line 289
static VALUE Markly_Node_replace(VALUE self, VALUE other) {
cmark_node *current_node = NULL, *replacement_node = NULL;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, current_node);
TypedData_Get_Struct(other, cmark_node, &Markly_Node_Type, replacement_node);
int result = cmark_node_replace(current_node, replacement_node);
if (result == 0) {
rb_raise(Markly_Error, "could not replace node");
}
return other;
}
|
#replace_section(new_node, replace_header: true, remove_subsections: true) ⇒ Object
Replace a section (header + content) with a new node.
124 125 126 127 128 129 130 131 132 |
# File 'lib/markly/node.rb', line 124 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 |
#source_position ⇒ Object
Public: Fetches the source_position of the node.
Returns a Hash containing Symbol keys of the positions.
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'ext/markly/markly.c', line 454
static VALUE Markly_Node_get_source_position(VALUE self) {
int start_line, start_column, end_line, end_column;
VALUE result;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
start_line = cmark_node_get_start_line(node);
start_column = cmark_node_get_start_column(node);
end_line = cmark_node_get_end_line(node);
end_column = cmark_node_get_end_column(node);
result = rb_hash_new();
rb_hash_aset(result, CSTR2SYM("start_line"), INT2NUM(start_line));
rb_hash_aset(result, CSTR2SYM("start_column"), INT2NUM(start_column));
rb_hash_aset(result, CSTR2SYM("end_line"), INT2NUM(end_line));
rb_hash_aset(result, CSTR2SYM("end_column"), INT2NUM(end_column));
return result;
}
|
#string_content ⇒ Object
Public: Fetch the string contents of the node.
Returns a String or nil if there is no string content.
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'ext/markly/markly.c', line 316
static VALUE Markly_Node_get_string_content(VALUE self) {
const char *text;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = cmark_node_get_literal(node);
if (text == NULL) {
return RUBY_Qnil;
}
return encode_utf8_string(text);
}
|
#string_content=(s) ⇒ Object
Public: Sets the string content of the node.
string - A String containing new content.
Raises Error if the string content can't be set.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'ext/markly/markly.c', line 337
static VALUE Markly_Node_set_string_content(VALUE self, VALUE s) {
char *text;
cmark_node *node;
Check_Type(s, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = StringValueCStr(s);
if (!cmark_node_set_literal(node, text)) {
rb_raise(Markly_Error, "could not set string content");
}
return Qnil;
}
|
#table_alignments ⇒ Object
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'ext/markly/markly.c', line 1170
static VALUE Markly_Node_get_table_alignments(VALUE self) {
uint16_t column_count, i;
uint8_t *alignments;
cmark_node *node;
VALUE ary;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
column_count = cmark_gfm_extensions_get_table_columns(node);
alignments = cmark_gfm_extensions_get_table_alignments(node);
if (!column_count || !alignments) {
rb_raise(Markly_Error, "could not get column_count or alignments");
}
ary = rb_ary_new();
for (i = 0; i < column_count; ++i) {
if (alignments[i] == 'l')
rb_ary_push(ary, sym_left);
else if (alignments[i] == 'c')
rb_ary_push(ary, sym_center);
else if (alignments[i] == 'r')
rb_ary_push(ary, sym_right);
else
rb_ary_push(ary, Qnil);
}
return ary;
}
|
#tasklist_item_checked=(item_checked) ⇒ Object
Public: Sets the checkbox state of the current node (must be a :tasklist).
item_checked - A Boolean representing the new checkbox state
Returns a Boolean representing the new checkbox state. Raises a Error if the checkbox state can't be set.
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'ext/markly/markly.c', line 1143
static VALUE Markly_Node_set_tasklist_item_checked(VALUE self, VALUE item_checked) {
int tasklist_state;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
tasklist_state = RTEST(item_checked);
if (!cmark_gfm_extensions_set_tasklist_item_checked(node, tasklist_state)) {
rb_raise(Markly_Error, "could not set tasklist_item_checked");
};
if (tasklist_state) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#tasklist_item_checked? ⇒ Boolean
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 |
# File 'ext/markly/markly.c', line 1121
static VALUE Markly_Node_tasklist_item_checked_p(VALUE self) {
int tasklist_state;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
tasklist_state = cmark_gfm_extensions_get_tasklist_item_checked(node);
if (tasklist_state == 1) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#title ⇒ Object
Public: Gets the title of the current node (must be a :link or :image).
Returns a String. Raises a Error if the title can't be retrieved.
794 795 796 797 798 799 800 801 802 803 804 805 |
# File 'ext/markly/markly.c', line 794
static VALUE Markly_Node_get_title(VALUE self) {
const char *text;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = cmark_node_get_title(node);
if (text == NULL) {
rb_raise(Markly_Error, "could not get title");
}
return rb_str_new2(text);
}
|
#title=(title) ⇒ Object
Public: Sets the title of the current node (must be a :link or :image).
title - A String representing the new title
Raises a Error if the title can't be set.
814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
# File 'ext/markly/markly.c', line 814
static VALUE Markly_Node_set_title(VALUE self, VALUE title) {
char *text;
cmark_node *node;
Check_Type(title, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = StringValueCStr(title);
if (!cmark_node_set_title(node, text)) {
rb_raise(Markly_Error, "could not set title");
}
return Qnil;
}
|
#to_commonmark(flags: DEFAULT, width: 0) ⇒ Object Also known as: to_markdown
Convert the node to a CommonMark string.
54 55 56 |
# File 'lib/markly/node.rb', line 54 def to_commonmark(flags: DEFAULT, width: 0) _render_commonmark(flags, width).force_encoding("utf-8") end |
#to_html(flags: DEFAULT, extensions: []) ⇒ Object
Convert the node to an HTML string.
45 46 47 |
# File 'lib/markly/node.rb', line 45 def to_html(flags: DEFAULT, extensions: []) _render_html(flags, extensions).force_encoding("utf-8") end |
#to_plaintext(flags: DEFAULT, width: 0) ⇒ Object
Convert the node to a plain-text string.
72 73 74 |
# File 'lib/markly/node.rb', line 72 def to_plaintext(flags: DEFAULT, width: 0) _render_plaintext(flags, width).force_encoding("utf-8") end |
#type ⇒ Object
Public: Fetches the list type of the node.
Returns a Symbol representing the node's type.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'ext/markly/markly.c', line 357
static VALUE Markly_Node_get_type(VALUE self) {
int node_type = 0;
cmark_node *node = NULL;
VALUE symbol = Qnil;
const char *s = NULL;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
node_type = cmark_node_get_type(node);
symbol = Qnil;
switch (node_type) {
case CMARK_NODE_DOCUMENT:
symbol = sym_document;
break;
case CMARK_NODE_CUSTOM_BLOCK:
symbol = sym_custom_block;
break;
case CMARK_NODE_CUSTOM_INLINE:
symbol = sym_custom_inline;
break;
case CMARK_NODE_BLOCK_QUOTE:
symbol = sym_blockquote;
break;
case CMARK_NODE_LIST:
symbol = sym_list;
break;
case CMARK_NODE_ITEM:
symbol = sym_list_item;
break;
case CMARK_NODE_CODE_BLOCK:
symbol = sym_code_block;
break;
case CMARK_NODE_HTML:
symbol = sym_html;
break;
case CMARK_NODE_PARAGRAPH:
symbol = sym_paragraph;
break;
case CMARK_NODE_HEADER:
symbol = sym_header;
break;
case CMARK_NODE_HRULE:
symbol = sym_hrule;
break;
case CMARK_NODE_TEXT:
symbol = sym_text;
break;
case CMARK_NODE_SOFTBREAK:
symbol = sym_softbreak;
break;
case CMARK_NODE_LINEBREAK:
symbol = sym_linebreak;
break;
case CMARK_NODE_CODE:
symbol = sym_code;
break;
case CMARK_NODE_INLINE_HTML:
symbol = sym_inline_html;
break;
case CMARK_NODE_EMPH:
symbol = sym_emph;
break;
case CMARK_NODE_STRONG:
symbol = sym_strong;
break;
case CMARK_NODE_LINK:
symbol = sym_link;
break;
case CMARK_NODE_IMAGE:
symbol = sym_image;
break;
case CMARK_NODE_FOOTNOTE_REFERENCE:
symbol = sym_footnote_reference;
break;
case CMARK_NODE_FOOTNOTE_DEFINITION:
symbol = sym_footnote_definition;
break;
case CMARK_NODE_FRONT_MATTER:
symbol = sym_front_matter;
break;
default:
if (node->extension) {
s = node->extension->get_type_string_func(node->extension, node);
return ID2SYM(rb_intern(s));
}
rb_raise(Markly_Error, "invalid node type %d", node_type);
}
return symbol;
}
|
#type_string ⇒ Object
Public: Returns the type of the current pointer as a string.
Returns a String.
480 481 482 483 484 485 |
# File 'ext/markly/markly.c', line 480
static VALUE Markly_Node_get_type_string(VALUE self) {
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
return rb_str_new2(cmark_node_get_type_string(node));
}
|
#url ⇒ Object
Public: Gets the URL of the current node (must be a :link or :image).
Returns a String. Raises a Error if the URL can't be retrieved.
753 754 755 756 757 758 759 760 761 762 763 764 |
# File 'ext/markly/markly.c', line 753
static VALUE Markly_Node_get_url(VALUE self) {
const char *text;
cmark_node *node;
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = cmark_node_get_url(node);
if (text == NULL) {
rb_raise(Markly_Error, "could not get url");
}
return rb_str_new2(text);
}
|
#url=(url) ⇒ Object
Public: Sets the URL of the current node (must be a :link or :image).
url - A String representing the new URL
Raises a Error if the URL can't be set.
773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'ext/markly/markly.c', line 773
static VALUE Markly_Node_set_url(VALUE self, VALUE url) {
cmark_node *node;
char *text;
Check_Type(url, T_STRING);
TypedData_Get_Struct(self, cmark_node, &Markly_Node_Type, node);
text = StringValueCStr(url);
if (!cmark_node_set_url(node, text)) {
rb_raise(Markly_Error, "could not set url");
}
return Qnil;
}
|
#walk {|_self| ... } ⇒ Object
Walk the node tree recursively.
31 32 33 34 35 36 37 38 |
# File 'lib/markly/node.rb', line 31 def walk(&block) return enum_for(:walk) unless block_given? yield self each do |child| child.walk(&block) end end |