Skip to content

Commit

Permalink
Merge pull request #16 from gjtorikian/expose-more-info
Browse files Browse the repository at this point in the history
Add `sourcepos` information
  • Loading branch information
gjtorikian committed Sep 25, 2015
2 parents 73382bb + c615e53 commit 303e10f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ext/commonmarker/cmark
Submodule cmark updated from 1f4632 to cc9c76
29 changes: 29 additions & 0 deletions ext/commonmarker/commonmarker.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,34 @@ rb_node_get_type(VALUE self)
return symbol;
}

/*
* Public: Fetches the sourcepos of the node.
*
* Returns a {Hash} containing {Symbol} keys of the positions.
*/
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;
}

/*
* Public: Returns the type of the current pointer as a string.
*
Expand Down Expand Up @@ -981,6 +1009,7 @@ void Init_commonmarker()
rb_define_method(rb_mNode, "string_content=", rb_node_set_string_content, 1);
rb_define_method(rb_mNode, "type", rb_node_get_type, 0);
rb_define_method(rb_mNode, "type_string", rb_node_get_type_string, 0);
rb_define_method(rb_mNode, "sourcepos", rb_node_get_sourcepos, 0);
rb_define_method(rb_mNode, "delete", rb_node_unlink, 0);
rb_define_method(rb_mNode, "first_child", rb_node_first_child, 0);
rb_define_method(rb_mNode, "next", rb_node_next, 0);
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/dingus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Try CommonMark

You can try CommonMark here. This dingus is powered by
[commonmark.js](https://github.com/jgm/commonmark.js), the
JavaScript reference implementation.

1. item one
2. item two
- sublist
- sublist
101 changes: 101 additions & 0 deletions test/test_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'test_helper'

class TestAttributes < Minitest::Test
def setup
contents = File.read(File.join(FIXTURES_DIR, 'dingus.md'))
@doc = CommonMarker.render_doc(contents.strip)
end

def test_sourcepos
sourcepos = []

@doc.walk do |node|
sourcepos << node.sourcepos
end

sourcepos.delete_if { |h| h.values.all? { |v| v == 0 } }

result = [
{
:start_line => 1,
:start_column => 1,
:end_line => 10,
:end_column => 12
},
{
:start_line => 1,
:start_column => 4,
:end_line => 1,
:end_column => 17
},
{
:start_line => 3,
:start_column => 1,
:end_line => 5,
:end_column => 36
},
{
:start_line => 7,
:start_column => 1,
:end_line => 10,
:end_column => 12
},
{
:start_line => 7,
:start_column => 1,
:end_line => 7,
:end_column => 11
},
{
:start_line => 7,
:start_column => 4,
:end_line => 7,
:end_column => 11
},
{
:start_line => 8,
:start_column => 1,
:end_line => 10,
:end_column => 12
},
{
:start_line => 8,
:start_column => 4,
:end_line => 8,
:end_column => 11
},
{
:start_line => 9,
:start_column => 4,
:end_line => 10,
:end_column => 12
},
{
:start_line => 9,
:start_column => 4,
:end_line => 9,
:end_column => 12
},
{
:start_line => 9,
:start_column => 6,
:end_line => 9,
:end_column => 12
},
{
:start_line => 10,
:start_column => 4,
:end_line => 10,
:end_column => 12
},
{
:start_line => 10,
:start_column => 6,
:end_line => 10,
:end_column => 12
}
]

assert_equal result, sourcepos
end
end

0 comments on commit 303e10f

Please sign in to comment.