Skip to content

Commit

Permalink
Merge pull request #228 from github/upstream/inline-sourcepos
Browse files Browse the repository at this point in the history
Add sourcepos info for inlines
  • Loading branch information
jgm authored Nov 5, 2017
2 parents 5da792f + 1326fd0 commit b40ecdc
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 53 deletions.
97 changes: 94 additions & 3 deletions api_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,9 @@ static void render_xml(test_batch_runner *runner) {
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<paragraph sourcepos=\"1:1-1:9\">\n"
" <text>foo </text>\n"
" <emph>\n"
" <text>bar</text>\n"
" <text sourcepos=\"1:1-1:4\">foo </text>\n"
" <emph sourcepos=\"1:5-1:9\">\n"
" <text sourcepos=\"1:6-1:8\">bar</text>\n"
" </emph>\n"
"</paragraph>\n",
"render first paragraph with source pos");
Expand Down Expand Up @@ -883,6 +883,95 @@ static void test_feed_across_line_ending(test_batch_runner *runner) {
cmark_node_free(document);
}

static void source_pos(test_batch_runner *runner) {
static const char markdown[] =
"# Hi *there*.\n"
"\n"
"Hello &ldquo; <http://www.google.com>\n"
"there `hi` -- [okay](www.google.com (ok)).\n"
"\n"
"> 1. Okay.\n"
"> Sure.\n"
">\n"
"> 2. Yes, okay.\n"
"> ![ok](hi \"yes\")\n";

cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT | CMARK_OPT_SOURCEPOS);
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<document sourcepos=\"1:1-10:20\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
" <heading sourcepos=\"1:1-1:13\" level=\"1\">\n"
" <text sourcepos=\"1:3-1:5\">Hi </text>\n"
" <emph sourcepos=\"1:6-1:12\">\n"
" <text sourcepos=\"1:7-1:11\">there</text>\n"
" </emph>\n"
" <text sourcepos=\"1:13-1:13\">.</text>\n"
" </heading>\n"
" <paragraph sourcepos=\"3:1-4:42\">\n"
" <text sourcepos=\"3:1-3:14\">Hello “ </text>\n"
" <link sourcepos=\"3:15-3:37\" destination=\"http://www.google.com\" title=\"\">\n"
" <text sourcepos=\"3:16-3:36\">http://www.google.com</text>\n"
" </link>\n"
" <softbreak />\n"
" <text sourcepos=\"4:1-4:6\">there </text>\n"
" <code sourcepos=\"4:8-4:9\">hi</code>\n"
" <text sourcepos=\"4:11-4:14\"> -- </text>\n"
" <link sourcepos=\"4:15-4:41\" destination=\"www.google.com\" title=\"ok\">\n"
" <text sourcepos=\"4:16-4:19\">okay</text>\n"
" </link>\n"
" <text sourcepos=\"4:42-4:42\">.</text>\n"
" </paragraph>\n"
" <block_quote sourcepos=\"6:1-10:20\">\n"
" <list sourcepos=\"6:3-10:20\" type=\"ordered\" start=\"1\" delim=\"period\" tight=\"false\">\n"
" <item sourcepos=\"6:3-8:1\">\n"
" <paragraph sourcepos=\"6:6-7:10\">\n"
" <text sourcepos=\"6:6-6:10\">Okay.</text>\n"
" <softbreak />\n"
" <text sourcepos=\"7:6-7:10\">Sure.</text>\n"
" </paragraph>\n"
" </item>\n"
" <item sourcepos=\"9:3-10:20\">\n"
" <paragraph sourcepos=\"9:6-10:20\">\n"
" <text sourcepos=\"9:6-9:15\">Yes, okay.</text>\n"
" <softbreak />\n"
" <image sourcepos=\"10:6-10:20\" destination=\"hi\" title=\"yes\">\n"
" <text sourcepos=\"10:8-10:9\">ok</text>\n"
" </image>\n"
" </paragraph>\n"
" </item>\n"
" </list>\n"
" </block_quote>\n"
"</document>\n",
"sourcepos are as expected");
free(xml);
cmark_node_free(doc);
}

static void ref_source_pos(test_batch_runner *runner) {
static const char markdown[] =
"Let's try [reference] links.\n"
"\n"
"[reference]: https://github.com (GitHub)\n";

cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_DEFAULT);
char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT | CMARK_OPT_SOURCEPOS);
STR_EQ(runner, xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<document sourcepos=\"1:1-3:40\" xmlns=\"http://commonmark.org/xml/1.0\">\n"
" <paragraph sourcepos=\"1:1-1:28\">\n"
" <text sourcepos=\"1:1-1:10\">Let's try </text>\n"
" <link sourcepos=\"1:11-1:21\" destination=\"https://github.com\" title=\"GitHub\">\n"
" <text sourcepos=\"1:12-1:20\">reference</text>\n"
" </link>\n"
" <text sourcepos=\"1:22-1:28\"> links.</text>\n"
" </paragraph>\n"
"</document>\n",
"sourcepos are as expected");
free(xml);
cmark_node_free(doc);
}

int main() {
int retval;
test_batch_runner *runner = test_batch_runner_new();
Expand All @@ -908,6 +997,8 @@ int main() {
test_cplusplus(runner);
test_safe(runner);
test_feed_across_line_ending(runner);
source_pos(runner);
ref_source_pos(runner);

test_print_summary(runner);
retval = test_ok(runner) ? 0 : 1;
Expand Down
1 change: 1 addition & 0 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,

(*container)->as.heading.level = level;
(*container)->as.heading.setext = false;
(*container)->internal_offset = matched;

} else if (!indented && (matched = scan_open_code_fence(
input, parser->first_nonspace))) {
Expand Down
Loading

0 comments on commit b40ecdc

Please sign in to comment.