Class: CommonMarker::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/commonmarker/node.rb,
ext/commonmarker/commonmarker.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.markdown_to_html(rb_text, rb_options) ⇒ Object

Internal: Parses a Markdown string into an HTML string.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/commonmarker/commonmarker.c', line 98

static VALUE rb_markdown_to_html(VALUE self, VALUE rb_text, VALUE rb_options) {
  char *str;
  int len, options;

  Check_Type(rb_text, T_STRING);
  Check_Type(rb_options, T_FIXNUM);

  str = (char *)RSTRING_PTR(rb_text);
  len = RSTRING_LEN(rb_text);
  options = FIX2INT(rb_options);

  char *html = cmark_markdown_to_html(str, len, options);
  VALUE ruby_html = rb_str_new2(html);

  free(html);

  return ruby_html;
}

.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`

  • ‘:blockquote`

  • ‘:list`

  • ‘:list_item`

  • ‘:code_block`

  • ‘:html`

  • ‘:paragraph`

  • ‘:header`

  • ‘:hrule`

  • ‘:text`

  • ‘:softbreak`

  • ‘:linebreak`

  • ‘:code`

  • ‘:inline_html`

  • ‘:emph`

  • ‘:strong`

  • ‘:link`

  • ‘:image`



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/commonmarker/commonmarker.c', line 141

static VALUE rb_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_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
    rb_raise(rb_mNodeError, "invalid node of type %d", node_type);

  node = cmark_node_new(node_type);
  if (node == NULL) {
    rb_raise(rb_mNodeError, "could not create node of type %d", node_type);
  }

  return rb_node_to_value(node);
}

.parse_document(rb_text, rb_len, rb_options) ⇒ Object

Internal: Parses a Markdown string into a document.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/commonmarker/commonmarker.c', line 198

static VALUE rb_parse_document(VALUE self, VALUE rb_text, VALUE rb_len,
                               VALUE rb_options) {
  char *text;
  int len, options;
  cmark_node *doc;
  Check_Type(rb_text, T_STRING);
  Check_Type(rb_len, T_FIXNUM);
  Check_Type(rb_options, T_FIXNUM);

  text = (char *)RSTRING_PTR(rb_text);
  len = FIX2INT(rb_len);
  options = FIX2INT(rb_options);

  doc = cmark_parse_document(text, len, options);
  if (doc == NULL) {
    rb_raise(rb_mNodeError, "error parsing document");
  }

  return rb_node_to_value(doc);
}

Instance Method Details

#_render_html(rb_options) ⇒ Object

Internal: Convert the node to an HTML string.

Returns a String.



442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/commonmarker/commonmarker.c', line 442

static VALUE rb_render_html(VALUE n, VALUE rb_options) {
  int options;
  cmark_node *node;
  Check_Type(rb_options, T_FIXNUM);

  options = FIX2INT(rb_options);

  Data_Get_Struct(n, cmark_node, node);

  char *html = cmark_render_html(node, options);
  VALUE ruby_html = rb_str_new2(html);

  free(html);

  return ruby_html;
}

#append_child(child) ⇒ Object

Public: Inserts a node as the last child of the current node.

child - A child CommonMarker::Node to insert.

Returns ‘true` if successful. Raises NodeError if the node can’t be inserted.



513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/commonmarker/commonmarker.c', line 513

static VALUE rb_node_append_child(VALUE self, VALUE child) {
  cmark_node *node1, *node2;
  Data_Get_Struct(self, cmark_node, node1);

  Data_Get_Struct(child, cmark_node, node2);

  if (!cmark_node_append_child(node1, node2)) {
    rb_raise(rb_mNodeError, "could not append child");
  }

  rb_parent_added(child);

  return Qtrue;
}

#deleteObject

Internal: Unlinks the node from the tree (fixing pointers in parents and siblings appropriately).



378
379
380
381
382
383
384
385
386
387
# File 'ext/commonmarker/commonmarker.c', line 378

static VALUE rb_node_unlink(VALUE self) {
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  cmark_node_unlink(node);

  rb_parent_removed(self);

  return Qnil;
}

#each(&block) ⇒ Object

Public: Iterate over the children (if any) of the current pointer.



28
29
30
31
32
33
34
35
36
37
# File 'lib/commonmarker/node.rb', line 28

def each(&block)
  return enum_for(:each) unless block_given?

  child = first_child
  while child
    nextchild = child.next
    yield child
    child = nextchild
  end
end

#each_child(&block) ⇒ Object

Deprecated: Please use ‘each` instead



40
41
42
43
# File 'lib/commonmarker/node.rb', line 40

def each_child(&block)
  warn '[DEPRECATION] `each_child` is deprecated.  Please use `each` instead.'
  each(&block)
end

#fence_infoObject

Public: Gets the fence info of the current node (must be a ‘:code_block`).

Returns a String representing the fence info. Raises a NodeError if the fence info can’t be retrieved.



834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'ext/commonmarker/commonmarker.c', line 834

static VALUE rb_node_get_fence_info(VALUE self) {
  const char *fence_info;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  fence_info = cmark_node_get_fence_info(node);

  if (fence_info == NULL) {
    rb_raise(rb_mNodeError, "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 NodeError if the fence info can’t be set.



855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'ext/commonmarker/commonmarker.c', line 855

static VALUE rb_node_set_fence_info(VALUE self, VALUE info) {
  char *text;
  cmark_node *node;
  Check_Type(info, T_STRING);

  Data_Get_Struct(self, cmark_node, node);
  text = StringValueCStr(info);

  if (!cmark_node_set_fence_info(node, text)) {
    rb_raise(rb_mNodeError, "could not set fence_info");
  }

  return Qnil;
}

#first_childObject

Public: Fetches the first child of the node.

Returns a CommonMarker::Node if a child exists, ‘nil` otherise.



393
394
395
396
397
398
399
400
# File 'ext/commonmarker/commonmarker.c', line 393

static VALUE rb_node_first_child(VALUE self) {
  cmark_node *node, *child;
  Data_Get_Struct(self, cmark_node, node);

  child = cmark_node_first_child(node);

  return rb_node_to_value(child);
}

#header_levelObject

Public: Gets the header level of the current node (must be a ‘:header`).

Returns a Number representing the header level. Raises a NodeError if the header level can’t be retrieved.



655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'ext/commonmarker/commonmarker.c', line 655

static VALUE rb_node_get_header_level(VALUE self) {
  int header_level;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  header_level = cmark_node_get_header_level(node);

  if (header_level == 0) {
    rb_raise(rb_mNodeError, "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 NodeError if the header level can’t be set.



676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'ext/commonmarker/commonmarker.c', line 676

static VALUE rb_node_set_header_level(VALUE self, VALUE level) {
  int l;
  cmark_node *node;
  Check_Type(level, T_FIXNUM);

  Data_Get_Struct(self, cmark_node, node);
  l = FIX2INT(level);

  if (!cmark_node_set_header_level(node, l)) {
    rb_raise(rb_mNodeError, "could not set header_level");
  }

  return Qnil;
}

#html_escape_href(rb_text) ⇒ Object

Internal: Escapes href URLs safely.



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
# File 'ext/commonmarker/commonmarker.c', line 871

static VALUE rb_html_escape_href(VALUE self, VALUE rb_text) {
  char *result;
  cmark_node *node;
  Check_Type(rb_text, T_STRING);

  Data_Get_Struct(self, cmark_node, 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),
                          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.



891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'ext/commonmarker/commonmarker.c', line 891

static VALUE rb_html_escape_html(VALUE self, VALUE rb_text) {
  char *result;
  cmark_node *node;
  Check_Type(rb_text, T_STRING);

  Data_Get_Struct(self, cmark_node, 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),
                           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 CommonMarker::Node to insert.

Returns ‘true` if successful. Raises NodeError if the node can’t be inserted.



467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'ext/commonmarker/commonmarker.c', line 467

static VALUE rb_node_insert_after(VALUE self, VALUE sibling) {
  cmark_node *node1, *node2;
  Data_Get_Struct(self, cmark_node, node1);

  Data_Get_Struct(sibling, cmark_node, node2);

  if (!cmark_node_insert_after(node1, node2)) {
    rb_raise(rb_mNodeError, "could not insert after");
  }

  rb_parent_added(sibling);

  return Qtrue;
}

#insert_before(sibling) ⇒ Object

Public: Inserts a node as a sibling before the current node.

sibling - A sibling CommonMarker::Node to insert.

Returns ‘true` if successful. Raises NodeError if the node can’t be inserted.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'ext/commonmarker/commonmarker.c', line 423

static VALUE rb_node_insert_before(VALUE self, VALUE sibling) {
  cmark_node *node1, *node2;
  Data_Get_Struct(self, cmark_node, node1);

  Data_Get_Struct(sibling, cmark_node, node2);

  if (!cmark_node_insert_before(node1, node2)) {
    rb_raise(rb_mNodeError, "could not insert before");
  }

  rb_parent_added(sibling);

  return Qtrue;
}

#last_childObject

Public: Fetches the first child of the current node.

Returns a CommonMarker::Node if a child exists, ‘nil` otherise.



532
533
534
535
536
537
538
539
# File 'ext/commonmarker/commonmarker.c', line 532

static VALUE rb_node_last_child(VALUE self) {
  cmark_node *node, *child;
  Data_Get_Struct(self, cmark_node, node);

  child = cmark_node_last_child(node);

  return rb_node_to_value(child);
}

#list_startObject

Public: Gets the starting number the current node (must be an ‘:ordered_list`).

Returns a Number representing the starting number. Raises a NodeError if the starting number can’t be retrieved.



752
753
754
755
756
757
758
759
760
761
762
763
# File 'ext/commonmarker/commonmarker.c', line 752

static VALUE rb_node_get_list_start(VALUE self) {
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  if (cmark_node_get_type(node) != CMARK_NODE_LIST ||
      cmark_node_get_list_type(node) != CMARK_ORDERED_LIST) {
    rb_raise(rb_mNodeError, "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 NodeError if the starting number can’t be set.



773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'ext/commonmarker/commonmarker.c', line 773

static VALUE rb_node_set_list_start(VALUE self, VALUE start) {
  int s;
  cmark_node *node;
  Check_Type(start, T_FIXNUM);

  Data_Get_Struct(self, cmark_node, node);
  s = FIX2INT(start);

  if (!cmark_node_set_list_start(node, s)) {
    rb_raise(rb_mNodeError, "could not set list_start");
  }

  return Qnil;
}

#list_tightObject

Public: Gets the tight status the current node (must be a ‘:list`).

Returns a ‘true` if the list is tight, `false` otherwise. Raises a NodeError if the starting number can’t be retrieved.



794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'ext/commonmarker/commonmarker.c', line 794

static VALUE rb_node_get_list_tight(VALUE self) {
  int flag;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  if (cmark_node_get_type(node) != CMARK_NODE_LIST) {
    rb_raise(rb_mNodeError, "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 NodeError if the tightness can’t be set.



815
816
817
818
819
820
821
822
823
824
825
826
# File 'ext/commonmarker/commonmarker.c', line 815

static VALUE rb_node_set_list_tight(VALUE self, VALUE tight) {
  int t;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);
  t = RTEST(tight);

  if (!cmark_node_set_list_tight(node, t)) {
    rb_raise(rb_mNodeError, "could not set list_tight");
  }

  return Qnil;
}

#list_typeObject

Public: Gets the list type of the current node (must be a ‘:list`).

Returns a Symbol. Raises a NodeError if the title can’t be retrieved.



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'ext/commonmarker/commonmarker.c', line 697

static VALUE rb_node_get_list_type(VALUE self) {
  int list_type;
  cmark_node *node;
  VALUE symbol;
  Data_Get_Struct(self, cmark_node, 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(rb_mNodeError, "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 NodeError if the list type can’t be set.



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'ext/commonmarker/commonmarker.c', line 723

static VALUE rb_node_set_list_type(VALUE self, VALUE list_type) {
  int type = 0;
  cmark_node *node;
  Check_Type(list_type, T_SYMBOL);

  Data_Get_Struct(self, cmark_node, 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(rb_mNodeError, "invalid list_type");
  }

  if (!cmark_node_set_list_type(node, type)) {
    rb_raise(rb_mNodeError, "could not set list_type");
  }

  return Qnil;
}

#nextObject

Public: Fetches the next sibling of the node.

Returns a CommonMarker::Node if a sibling exists, ‘nil` otherwise.



406
407
408
409
410
411
412
413
# File 'ext/commonmarker/commonmarker.c', line 406

static VALUE rb_node_next(VALUE self) {
  cmark_node *node, *next;
  Data_Get_Struct(self, cmark_node, node);

  next = cmark_node_next(node);

  return rb_node_to_value(next);
}

#parentObject

Public: Fetches the parent of the current node.

Returns a CommonMarker::Node if a parent exists, ‘nil` otherise.



545
546
547
548
549
550
551
552
# File 'ext/commonmarker/commonmarker.c', line 545

static VALUE rb_node_parent(VALUE self) {
  cmark_node *node, *parent;
  Data_Get_Struct(self, cmark_node, node);

  parent = cmark_node_parent(node);

  return rb_node_to_value(parent);
}

#prepend_child(child) ⇒ Object

Public: Inserts a node as the first child of the current node.

child - A child CommonMarker::Node to insert.

Returns ‘true` if successful. Raises NodeError if the node can’t be inserted.



490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'ext/commonmarker/commonmarker.c', line 490

static VALUE rb_node_prepend_child(VALUE self, VALUE child) {
  cmark_node *node1, *node2;
  Data_Get_Struct(self, cmark_node, node1);

  Data_Get_Struct(child, cmark_node, node2);

  if (!cmark_node_prepend_child(node1, node2)) {
    rb_raise(rb_mNodeError, "could not prepend child");
  }

  rb_parent_added(child);

  return Qtrue;
}

#previousObject

Public: Fetches the previous sibling of the current node.

Returns a CommonMarker::Node if a parent exists, ‘nil` otherise.



558
559
560
561
562
563
564
565
# File 'ext/commonmarker/commonmarker.c', line 558

static VALUE rb_node_previous(VALUE self) {
  cmark_node *node, *previous;
  Data_Get_Struct(self, cmark_node, node);

  previous = cmark_node_previous(node);

  return rb_node_to_value(previous);
}

#sourceposObject

Public: Fetches the sourcepos of the node.

Returns a Hash containing Symbol keys of the positions.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/commonmarker/commonmarker.c', line 341

static VALUE rb_node_get_sourcepos(VALUE self) {
  int start_line, start_column, end_line, end_column;
  VALUE result;

  cmark_node *node;
  Data_Get_Struct(self, cmark_node, 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_contentObject

Public: Fetch the string contents of the node.

Returns a String.



224
225
226
227
228
229
230
231
232
233
234
235
# File 'ext/commonmarker/commonmarker.c', line 224

static VALUE rb_node_get_string_content(VALUE self) {
  const char *text;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  text = cmark_node_get_literal(node);
  if (text == NULL) {
    rb_raise(rb_mNodeError, "could not get string content");
  }

  return rb_str_new2(text);
}

#string_content=(s) ⇒ Object

Public: Sets the string content of the node.

string - A String containing new content.

Raises NodeError if the string content can’t be set.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'ext/commonmarker/commonmarker.c', line 244

static VALUE rb_node_set_string_content(VALUE self, VALUE s) {
  char *text;
  cmark_node *node;
  Check_Type(s, T_STRING);

  Data_Get_Struct(self, cmark_node, node);
  text = StringValueCStr(s);

  if (!cmark_node_set_literal(node, text)) {
    rb_raise(rb_mNodeError, "could not set string content");
  }

  return Qnil;
}

#titleObject

Public: Gets the title of the current node (must be a ‘:link` or `:image`).

Returns a String. Raises a NodeError if the title can’t be retrieved.



614
615
616
617
618
619
620
621
622
623
624
625
# File 'ext/commonmarker/commonmarker.c', line 614

static VALUE rb_node_get_title(VALUE self) {
  const char *text;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  text = cmark_node_get_title(node);
  if (text == NULL) {
    rb_raise(rb_mNodeError, "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 NodeError if the title can’t be set.



634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'ext/commonmarker/commonmarker.c', line 634

static VALUE rb_node_set_title(VALUE self, VALUE title) {
  char *text;
  cmark_node *node;
  Check_Type(title, T_STRING);

  Data_Get_Struct(self, cmark_node, node);
  text = StringValueCStr(title);

  if (!cmark_node_set_title(node, text)) {
    rb_raise(rb_mNodeError, "could not set title");
  }

  return Qnil;
}

#to_html(options = :default) ⇒ Object

Public: Convert the node to an HTML string.

options - A Symbol or of Symbols indicating the render options

Returns a String.



22
23
24
25
# File 'lib/commonmarker/node.rb', line 22

def to_html(options = :default)
  opts = Config.process_options(options, :render)
  _render_html(opts).force_encoding('utf-8')
end

#typeObject

Public: Fetches the list type of the node.

Returns a Symbol representing the node’s type.



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'ext/commonmarker/commonmarker.c', line 264

static VALUE rb_node_get_type(VALUE self) {
  int node_type;
  cmark_node *node;
  VALUE symbol;

  Data_Get_Struct(self, cmark_node, node);

  node_type = cmark_node_get_type(node);
  symbol = Qnil;

  switch (node_type) {
  case CMARK_NODE_DOCUMENT:
    symbol = sym_document;
    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;
  default:
    rb_raise(rb_mNodeError, "invalid node type %d", node_type);
  }

  return symbol;
}

#type_stringObject

Public: Returns the type of the current pointer as a string.

Returns a String.



367
368
369
370
371
372
# File 'ext/commonmarker/commonmarker.c', line 367

static VALUE rb_node_get_type_string(VALUE self) {
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  return rb_str_new2(cmark_node_get_type_string(node));
}

#urlObject

Public: Gets the URL of the current node (must be a ‘:link` or `:image`).

Returns a String. Raises a NodeError if the URL can’t be retrieved.



573
574
575
576
577
578
579
580
581
582
583
584
# File 'ext/commonmarker/commonmarker.c', line 573

static VALUE rb_node_get_url(VALUE self) {
  const char *text;
  cmark_node *node;
  Data_Get_Struct(self, cmark_node, node);

  text = cmark_node_get_url(node);
  if (text == NULL) {
    rb_raise(rb_mNodeError, "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 NodeError if the URL can’t be set.



593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'ext/commonmarker/commonmarker.c', line 593

static VALUE rb_node_set_url(VALUE self, VALUE url) {
  cmark_node *node;
  char *text;
  Check_Type(url, T_STRING);

  Data_Get_Struct(self, cmark_node, node);
  text = StringValueCStr(url);

  if (!cmark_node_set_url(node, text)) {
    rb_raise(rb_mNodeError, "could not set url");
  }

  return Qnil;
}

#walk {|_self| ... } ⇒ Object

Public: An iterator that “walks the tree,” descending into children recursively.

blk - A Proc representing the action to take for each child

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
15
# File 'lib/commonmarker/node.rb', line 8

def walk(&block)
  return enum_for(:walk) unless block_given?

  yield self
  each do |child|
    child.walk(&block)
  end
end