Skip to content

Commit

Permalink
Fix issues with tables during a round-trip parsing CM and then produc…
Browse files Browse the repository at this point in the history
…ing CM again. (#65)

* Wrapping CommonMark at 120 causes tables to be broken during a round-trip.

* Add optional parameter for wrapping width on render calls.

* Stray double-space introduced.

* Add width argument to `to_commonmark` and `to_plaintext`.
  • Loading branch information
jerryjvl authored and Ashe Connor committed Feb 13, 2018
1 parent 5a16c23 commit 15eb57a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
30 changes: 24 additions & 6 deletions ext/commonmarker/commonmarker.c
Expand Up @@ -571,15 +571,24 @@ static VALUE rb_render_html(VALUE n, VALUE rb_options, VALUE rb_extensions) {
*
* Returns a {String}.
*/
static VALUE rb_render_commonmark(VALUE n, VALUE rb_options) {
static VALUE rb_render_commonmark(int argc, VALUE *argv, VALUE n) {
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);
Data_Get_Struct(n, cmark_node, node);

char *cmark = cmark_render_commonmark(node, options, 120);
char *cmark = cmark_render_commonmark(node, options, width);
VALUE ruby_cmark = rb_str_new2(cmark);
free(cmark);

Expand All @@ -590,15 +599,24 @@ static VALUE rb_render_commonmark(VALUE n, VALUE rb_options) {
*
* Returns a {String}.
*/
static VALUE rb_render_plaintext(VALUE n, VALUE rb_options) {
static VALUE rb_render_plaintext(int argc, VALUE *argv, VALUE n) {
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);
Data_Get_Struct(n, cmark_node, node);

char *text = cmark_render_plaintext(node, options, 120);
char *text = cmark_render_plaintext(node, options, width);
VALUE ruby_text = rb_str_new2(text);
free(text);

Expand Down Expand Up @@ -1148,8 +1166,8 @@ __attribute__((visibility("default"))) void Init_commonmarker() {
rb_define_method(rb_mNode, "next", rb_node_next, 0);
rb_define_method(rb_mNode, "insert_before", rb_node_insert_before, 1);
rb_define_method(rb_mNode, "_render_html", rb_render_html, 2);
rb_define_method(rb_mNode, "_render_commonmark", rb_render_commonmark, 1);
rb_define_method(rb_mNode, "_render_plaintext", rb_render_plaintext, 1);
rb_define_method(rb_mNode, "_render_commonmark", rb_render_commonmark, -1);
rb_define_method(rb_mNode, "_render_plaintext", rb_render_plaintext, -1);
rb_define_method(rb_mNode, "insert_after", rb_node_insert_after, 1);
rb_define_method(rb_mNode, "prepend_child", rb_node_prepend_child, 1);
rb_define_method(rb_mNode, "append_child", rb_node_append_child, 1);
Expand Down
10 changes: 6 additions & 4 deletions lib/commonmarker/node.rb
Expand Up @@ -31,21 +31,23 @@ def to_html(options = :DEFAULT, extensions = [])
# Public: Convert the node to a CommonMark string.
#
# options - A {Symbol} or {Array of Symbol}s indicating the render options
# width - Column to wrap the output at
#
# Returns a {String}.
def to_commonmark(options = :DEFAULT)
def to_commonmark(options = :DEFAULT, width = 120)
opts = Config.process_options(options, :render)
_render_commonmark(opts).force_encoding('utf-8')
_render_commonmark(opts, width).force_encoding('utf-8')
end

# Public: Convert the node to a plain text string.
#
# options - A {Symbol} or {Array of Symbol}s indicating the render options
# width - Column to wrap the output at
#
# Returns a {String}.
def to_plaintext(options = :DEFAULT)
def to_plaintext(options = :DEFAULT, width = 120)
opts = Config.process_options(options, :render)
_render_plaintext(opts).force_encoding('utf-8')
_render_plaintext(opts, width).force_encoding('utf-8')
end

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

0 comments on commit 15eb57a

Please sign in to comment.