Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various fixes for footnotes #57

Open
wants to merge 5 commits into
base: gfm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions api_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,49 @@ static void table_spans(test_batch_runner *runner) {
}
}

static void footnote_metadata(test_batch_runner *runner) {
static const char markdown[] =
"this is a test[^test] and also a test[^other]\n"
"\n"
"[^test]: still a test\n"
"\n"
"[^other]: lorem ipsum\n";
static const char expected[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n"
"<document xmlns=\"http://commonmark.org/xml/1.0\">\n"
" <paragraph>\n"
" <text xml:space=\"preserve\">this is a test</text>\n"
" <footnote_reference id=\"test\" />\n"
" <text xml:space=\"preserve\"> and also a test</text>\n"
" <footnote_reference id=\"other\" />\n"
" </paragraph>\n"
" <footnote_definition id=\"test\">\n"
" <paragraph>\n"
" <text xml:space=\"preserve\">still a test</text>\n"
" </paragraph>\n"
" </footnote_definition>\n"
" <footnote_definition id=\"other\">\n"
" <paragraph>\n"
" <text xml:space=\"preserve\">lorem ipsum</text>\n"
" </paragraph>\n"
" </footnote_definition>\n"
"</document>\n";

cmark_node *doc =
cmark_parse_document(markdown, sizeof(markdown) - 1, CMARK_OPT_FOOTNOTES);

const char *xml = cmark_render_xml(doc, CMARK_OPT_DEFAULT);

STR_EQ(runner, xml, expected, "footnotes render correctly in XML");

cmark_node *test_def = cmark_node_nth_child(doc, 1);
STR_EQ(runner, cmark_node_get_footnote_id(test_def), "test", "footnote ID parsed properly");

cmark_node *other_def = cmark_node_nth_child(doc, 2);
STR_EQ(runner, cmark_node_get_footnote_id(other_def), "other", "footnote ID parsed properly");
}

int main() {
int retval;
test_batch_runner *runner = test_batch_runner_new();
Expand Down Expand Up @@ -1608,6 +1651,7 @@ int main() {
verify_custom_attributes_node_with_footnote(runner);
parser_interrupt(runner);
table_spans(runner);
footnote_metadata(runner);

test_print_summary(runner);
retval = test_ok(runner) ? 0 : 1;
Expand Down
5 changes: 4 additions & 1 deletion src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,12 +1249,15 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
parser->options & CMARK_OPT_FOOTNOTES &&
(matched = scan_footnote_definition(input, parser->first_nonspace))) {
cmark_chunk c = cmark_chunk_dup(input, parser->first_nonspace + 2, matched - 2);
cmark_chunk_to_cstr(parser->mem, &c);

while (c.data[c.len - 1] != ']')
--c.len;
--c.len;

// Allocate the cstr rendering after the length adjustment so that the
// length is accurate when `cmark_node_get_literal` is called.
cmark_chunk_to_cstr(parser->mem, &c);

S_advance_offset(parser, input, parser->first_nonspace + matched - parser->offset, false);
*container = add_child(parser, *container, CMARK_NODE_FOOTNOTE_DEFINITION, parser->first_nonspace + matched + 1);
(*container)->as.literal = c;
Expand Down
5 changes: 5 additions & 0 deletions src/include/cmark-gfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ CMARK_GFM_EXPORT const char *cmark_node_get_literal(cmark_node *node);
*/
CMARK_GFM_EXPORT int cmark_node_get_backtick_count(cmark_node *node);

/** If 'node' is a footnote reference or footnote definition, reutrns the
string ID of that footnote, otherwise returns NULL.
*/
CMARK_GFM_EXPORT const char *cmark_node_get_footnote_id(cmark_node *node);

/** Sets the string contents of 'node'. Returns 1 on success,
* 0 on failure.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ const char *cmark_node_get_type_string(cmark_node *node) {
return "link";
case CMARK_NODE_IMAGE:
return "image";
case CMARK_NODE_FOOTNOTE_REFERENCE:
return "footnote_reference";
case CMARK_NODE_FOOTNOTE_DEFINITION:
return "footnote_definition";
case CMARK_NODE_ATTRIBUTE:
return "attribute";
}
Expand Down Expand Up @@ -458,6 +462,24 @@ int cmark_node_set_literal(cmark_node *node, const char *content) {
return 0;
}

const char *cmark_node_get_footnote_id(cmark_node *node) {
if (node == NULL)
return NULL;

switch (node->type) {
case CMARK_NODE_FOOTNOTE_DEFINITION:
return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal);

case CMARK_NODE_FOOTNOTE_REFERENCE:
return cmark_chunk_to_cstr(NODE_MEM(node->parent_footnote_def), &node->parent_footnote_def->as.literal);

default:
break;
}

return NULL;
}

const char *cmark_node_get_string_content(cmark_node *node) {
return (char *) node->content.ptr;
}
Expand Down
10 changes: 10 additions & 0 deletions src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
case CMARK_NODE_ATTRIBUTE:
// TODO
break;
case CMARK_NODE_FOOTNOTE_DEFINITION:
cmark_strbuf_puts(xml, " id=\"");
escape_xml(xml, node->as.literal.data, node->as.literal.len);
cmark_strbuf_putc(xml, '"');
break;
case CMARK_NODE_FOOTNOTE_REFERENCE:
cmark_strbuf_puts(xml, " id=\"");
escape_xml(xml, node->parent_footnote_def->as.literal.data,
node->parent_footnote_def->as.literal.len);
cmark_strbuf_putc(xml, '"');
default:
break;
}
Expand Down