From 759e03748a368be8b209dfd113441c05e1a5dcc0 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Thu, 20 Nov 2014 13:02:53 +0100
Subject: More tree hierarchy checks and tests
---
api_test/main.c | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/node.c | 11 ++++++
2 files changed, 131 insertions(+)
diff --git a/api_test/main.c b/api_test/main.c
index f74dee2..e17beda 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -7,6 +7,35 @@
#include "harness.h"
+static const cmark_node_type node_types[] = {
+ CMARK_NODE_DOCUMENT,
+ CMARK_NODE_BQUOTE,
+ CMARK_NODE_LIST,
+ CMARK_NODE_LIST_ITEM,
+ CMARK_NODE_FENCED_CODE,
+ CMARK_NODE_INDENTED_CODE,
+ CMARK_NODE_HTML,
+ CMARK_NODE_PARAGRAPH,
+ CMARK_NODE_ATX_HEADER,
+ CMARK_NODE_SETEXT_HEADER,
+ CMARK_NODE_HRULE,
+ CMARK_NODE_REFERENCE_DEF,
+ CMARK_NODE_STRING,
+ CMARK_NODE_SOFTBREAK,
+ CMARK_NODE_LINEBREAK,
+ CMARK_NODE_INLINE_CODE,
+ CMARK_NODE_INLINE_HTML,
+ CMARK_NODE_EMPH,
+ CMARK_NODE_STRONG,
+ CMARK_NODE_LINK,
+ CMARK_NODE_IMAGE
+};
+static const int num_node_types = sizeof(node_types) / sizeof(*node_types);
+
+static void
+test_content(test_batch_runner *runner, cmark_node_type type,
+ int allowed_content);
+
static void
accessors(test_batch_runner *runner)
{
@@ -275,12 +304,103 @@ create_tree(test_batch_runner *runner)
cmark_node_destroy(doc);
}
+void
+hierarchy(test_batch_runner *runner)
+{
+ cmark_node *bquote1 = cmark_node_new(CMARK_NODE_BQUOTE);
+ cmark_node *bquote2 = cmark_node_new(CMARK_NODE_BQUOTE);
+ cmark_node *bquote3 = cmark_node_new(CMARK_NODE_BQUOTE);
+
+ OK(runner, cmark_node_append_child(bquote1, bquote2),
+ "append bquote2");
+ OK(runner, cmark_node_append_child(bquote2, bquote3),
+ "append bquote3");
+ OK(runner, !cmark_node_append_child(bquote3, bquote3),
+ "adding a node as child of itself fails");
+ OK(runner, !cmark_node_append_child(bquote3, bquote1),
+ "adding a parent as child fails");
+
+ cmark_node_destroy(bquote1);
+
+ int max_node_type = CMARK_NODE_LAST_BLOCK > CMARK_NODE_LAST_INLINE
+ ? CMARK_NODE_LAST_BLOCK : CMARK_NODE_LAST_INLINE;
+ OK(runner, max_node_type < 32, "all node types < 32");
+
+ int list_item_flag = 1 << CMARK_NODE_LIST_ITEM;
+ int top_level_blocks =
+ (1 << CMARK_NODE_BQUOTE) |
+ (1 << CMARK_NODE_LIST) |
+ (1 << CMARK_NODE_FENCED_CODE) |
+ (1 << CMARK_NODE_INDENTED_CODE) |
+ (1 << CMARK_NODE_HTML) |
+ (1 << CMARK_NODE_PARAGRAPH) |
+ (1 << CMARK_NODE_ATX_HEADER) |
+ (1 << CMARK_NODE_SETEXT_HEADER) |
+ (1 << CMARK_NODE_HRULE) |
+ (1 << CMARK_NODE_REFERENCE_DEF);
+ int all_inlines =
+ (1 << CMARK_NODE_STRING) |
+ (1 << CMARK_NODE_SOFTBREAK) |
+ (1 << CMARK_NODE_LINEBREAK) |
+ (1 << CMARK_NODE_INLINE_CODE) |
+ (1 << CMARK_NODE_INLINE_HTML) |
+ (1 << CMARK_NODE_EMPH) |
+ (1 << CMARK_NODE_STRONG) |
+ (1 << CMARK_NODE_LINK) |
+ (1 << CMARK_NODE_IMAGE);
+
+ test_content(runner, CMARK_NODE_DOCUMENT, top_level_blocks);
+ test_content(runner, CMARK_NODE_BQUOTE, top_level_blocks);
+ test_content(runner, CMARK_NODE_LIST, list_item_flag);
+ test_content(runner, CMARK_NODE_LIST_ITEM, top_level_blocks);
+ test_content(runner, CMARK_NODE_FENCED_CODE, 0);
+ test_content(runner, CMARK_NODE_INDENTED_CODE, 0);
+ test_content(runner, CMARK_NODE_HTML, 0);
+ test_content(runner, CMARK_NODE_PARAGRAPH, all_inlines);
+ test_content(runner, CMARK_NODE_ATX_HEADER, all_inlines);
+ test_content(runner, CMARK_NODE_SETEXT_HEADER, all_inlines);
+ test_content(runner, CMARK_NODE_HRULE, 0);
+ test_content(runner, CMARK_NODE_REFERENCE_DEF, 0);
+ test_content(runner, CMARK_NODE_STRING, 0);
+ test_content(runner, CMARK_NODE_SOFTBREAK, 0);
+ test_content(runner, CMARK_NODE_LINEBREAK, 0);
+ test_content(runner, CMARK_NODE_INLINE_CODE, 0);
+ test_content(runner, CMARK_NODE_INLINE_HTML, 0);
+ test_content(runner, CMARK_NODE_EMPH, all_inlines);
+ test_content(runner, CMARK_NODE_STRONG, all_inlines);
+ test_content(runner, CMARK_NODE_LINK, all_inlines);
+ test_content(runner, CMARK_NODE_IMAGE, all_inlines);
+}
+
+static void
+test_content(test_batch_runner *runner, cmark_node_type type,
+ int allowed_content)
+{
+ cmark_node *node = cmark_node_new(type);
+
+ for (int i = 0; i < num_node_types; ++i) {
+ cmark_node_type child_type = node_types[i];
+ cmark_node *child = cmark_node_new(child_type);
+
+ int got = cmark_node_append_child(node, child);
+ int expected = (allowed_content >> i) & 1;
+
+ INT_EQ(runner, got, expected,
+ "add %d as child of %d", child_type, type);
+
+ cmark_node_destroy(child);
+ }
+
+ cmark_node_destroy(node);
+}
+
int main() {
int retval;
test_batch_runner *runner = test_batch_runner_new();
accessors(runner);
create_tree(runner);
+ hierarchy(runner);
test_print_summary(runner);
retval = test_ok(runner) ? 0 : 1;
diff --git a/src/node.c b/src/node.c
index 624baad..4317dd4 100644
--- a/src/node.c
+++ b/src/node.c
@@ -348,6 +348,17 @@ S_is_inline(cmark_node *node) {
static bool
S_can_contain(cmark_node *node, cmark_node *child)
{
+ cmark_node *cur;
+
+ // Verify that child is not an ancestor of node or equal to node.
+ cur = node;
+ do {
+ if (cur == child) {
+ return false;
+ }
+ cur = cur->parent;
+ } while (cur != NULL);
+
if (child->type == CMARK_NODE_DOCUMENT) {
return false;
}
--
cgit v1.2.3
From 32be14c39f67eedf356adc4f673b66479b717c7c Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Thu, 20 Nov 2014 15:17:33 +0100
Subject: Set defaults for new headers and lists
---
api_test/main.c | 35 +++++++++++++++++++++++++++++++++++
src/node.c | 19 +++++++++++++++++++
2 files changed, 54 insertions(+)
diff --git a/api_test/main.c b/api_test/main.c
index e17beda..933a4bd 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -36,6 +36,40 @@ static void
test_content(test_batch_runner *runner, cmark_node_type type,
int allowed_content);
+static void
+constructor(test_batch_runner *runner)
+{
+ for (int i = 0; i < num_node_types; ++i) {
+ cmark_node_type type = node_types[i];
+ cmark_node *node = cmark_node_new(type);
+ OK(runner, node != NULL, "new type %d", type);
+
+ switch (node->type) {
+ case CMARK_NODE_ATX_HEADER:
+ case CMARK_NODE_SETEXT_HEADER:
+ INT_EQ(runner, cmark_node_get_header_level(node), 1,
+ "default header level is 1");
+ node->as.header.level = 1;
+ break;
+
+ case CMARK_NODE_LIST:
+ INT_EQ(runner, cmark_node_get_list_type(node),
+ CMARK_BULLET_LIST,
+ "default is list type is bullet");
+ INT_EQ(runner, cmark_node_get_list_start(node), 1,
+ "default is list start is 1");
+ INT_EQ(runner, cmark_node_get_list_tight(node), 0,
+ "default is list is loose");
+ break;
+
+ default:
+ break;
+ }
+
+ cmark_node_destroy(node);
+ }
+}
+
static void
accessors(test_batch_runner *runner)
{
@@ -398,6 +432,7 @@ int main() {
int retval;
test_batch_runner *runner = test_batch_runner_new();
+ constructor(runner);
accessors(runner);
create_tree(runner);
hierarchy(runner);
diff --git a/src/node.c b/src/node.c
index 4317dd4..bb9cacc 100644
--- a/src/node.c
+++ b/src/node.c
@@ -11,6 +11,25 @@ cmark_node*
cmark_node_new(cmark_node_type type) {
cmark_node *node = (cmark_node *)calloc(1, sizeof(*node));
node->type = type;
+
+ switch (node->type) {
+ case CMARK_NODE_ATX_HEADER:
+ case CMARK_NODE_SETEXT_HEADER:
+ node->as.header.level = 1;
+ break;
+
+ case CMARK_NODE_LIST: {
+ cmark_list *list = &node->as.list;
+ list->list_type = CMARK_BULLET_LIST;
+ list->start = 1;
+ list->tight = false;
+ break;
+ }
+
+ default:
+ break;
+ }
+
return node;
}
--
cgit v1.2.3
From 0bbe389efb13410994e55fef8243157a2144d63d Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Thu, 20 Nov 2014 15:21:00 +0100
Subject: Rearrange get_type tests
---
api_test/main.c | 20 ++------------------
1 file changed, 2 insertions(+), 18 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index 933a4bd..6747d61 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -43,6 +43,8 @@ constructor(test_batch_runner *runner)
cmark_node_type type = node_types[i];
cmark_node *node = cmark_node_new(type);
OK(runner, node != NULL, "new type %d", type);
+ INT_EQ(runner, cmark_node_get_type(node), type,
+ "get_type %d", type);
switch (node->type) {
case CMARK_NODE_ATX_HEADER:
@@ -99,22 +101,16 @@ accessors(test_batch_runner *runner)
// Getters
cmark_node *header = cmark_node_first_child(doc);
- INT_EQ(runner, cmark_node_get_type(header), CMARK_NODE_ATX_HEADER,
- "get_type header");
INT_EQ(runner, cmark_node_get_header_level(header), 2,
"get_header_level");
cmark_node *bullet_list = cmark_node_next(header);
- INT_EQ(runner, cmark_node_get_type(bullet_list), CMARK_NODE_LIST,
- "get_type bullet list");
INT_EQ(runner, cmark_node_get_list_type(bullet_list),
CMARK_BULLET_LIST, "get_list_type bullet");
INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
"get_list_tight tight");
cmark_node *ordered_list = cmark_node_next(bullet_list);
- INT_EQ(runner, cmark_node_get_type(ordered_list), CMARK_NODE_LIST,
- "get_type ordered list");
INT_EQ(runner, cmark_node_get_list_type(ordered_list),
CMARK_ORDERED_LIST, "get_list_type ordered");
INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2,
@@ -123,28 +119,20 @@ accessors(test_batch_runner *runner)
"get_list_tight loose");
cmark_node *code = cmark_node_next(ordered_list);
- INT_EQ(runner, cmark_node_get_type(code), CMARK_NODE_INDENTED_CODE,
- "get_type indented code");
STR_EQ(runner, cmark_node_get_string_content(code), "code\n",
"get_string_content indented code");
cmark_node *fenced = cmark_node_next(code);
- INT_EQ(runner, cmark_node_get_type(fenced), CMARK_NODE_FENCED_CODE,
- "get_type fenced code");
STR_EQ(runner, cmark_node_get_string_content(fenced), "fenced\n",
"get_string_content fenced code");
STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang",
"get_fence_info");
cmark_node *html = cmark_node_next(fenced);
- INT_EQ(runner, cmark_node_get_type(html), CMARK_NODE_HTML,
- "get_type html");
STR_EQ(runner, cmark_node_get_string_content(html),
"html
\n", "get_string_content html");
cmark_node *paragraph = cmark_node_next(html);
- INT_EQ(runner, cmark_node_get_type(paragraph), CMARK_NODE_PARAGRAPH,
- "get_type paragraph");
INT_EQ(runner, cmark_node_get_start_line(paragraph), 19,
"get_start_line");
INT_EQ(runner, cmark_node_get_start_column(paragraph), 1,
@@ -153,16 +141,12 @@ accessors(test_batch_runner *runner)
"get_end_line");
cmark_node *link = cmark_node_first_child(paragraph);
- INT_EQ(runner, cmark_node_get_type(link), CMARK_NODE_LINK,
- "get_type link");
STR_EQ(runner, cmark_node_get_url(link), "url",
"get_url");
STR_EQ(runner, cmark_node_get_title(link), "title",
"get_title");
cmark_node *string = cmark_node_first_child(link);
- INT_EQ(runner, cmark_node_get_type(string), CMARK_NODE_STRING,
- "get_type string");
STR_EQ(runner, cmark_node_get_string_content(string), "link",
"get_string_content string");
--
cgit v1.2.3
From fd56f2878a6360fb019b743ceb7e11b11a6e2481 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Thu, 20 Nov 2014 15:22:10 +0100
Subject: Tests for node_previous, node_parent, node_unlink
---
api_test/main.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index 6747d61..1b5725d 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -269,6 +269,7 @@ create_tree(test_batch_runner *runner)
cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
OK(runner, cmark_node_append_child(doc, p), "append1");
INT_EQ(runner, cmark_node_check(doc), 0, "append1 consistent");
+ OK(runner, cmark_node_parent(p) == doc, "node_parent");
cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH);
OK(runner, cmark_node_prepend_child(p, emph), "prepend1");
@@ -307,19 +308,25 @@ create_tree(test_batch_runner *runner)
OK(runner, cmark_node_insert_after(str1, str3), "ins after1");
INT_EQ(runner, cmark_node_check(doc), 0, "ins after1 consistent");
// e13
- OK(runner, cmark_node_last_child(p) == str3, "ins after1 works");
+ OK(runner, cmark_node_next(str1) == str3, "ins after1 works");
OK(runner, cmark_node_insert_after(str1, emph), "ins after2");
INT_EQ(runner, cmark_node_check(doc), 0, "ins after2 consistent");
// 1e3
- OK(runner, cmark_node_first_child(p) == str1, "ins after2 works");
+ OK(runner, cmark_node_previous(emph) == str1, "ins after2 works");
+
+ cmark_node_unlink(emph);
html = (char *)cmark_render_html(doc);
- STR_EQ(runner, html, "Hello, world!
\n",
+ STR_EQ(runner, html, "Hello, !
\n",
"render_html after shuffling");
free(html);
cmark_node_destroy(doc);
+
+ // TODO: Test that the contents of an unlinked inline are valid
+ // after the parent block was destroyed. This doesn't work so far.
+ cmark_node_destroy(emph);
}
void
--
cgit v1.2.3
From 92a4d4cb4eb4a6103f68646a38f8caa29d6df3e3 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Sat, 22 Nov 2014 15:41:40 +0100
Subject: Fix and test node_check
---
api_test/main.c | 39 ++++++++++++++++++++++++++++++---------
src/blocks.c | 2 +-
src/node.c | 55 ++++++++++++++++++++++++++++++-------------------------
src/node.h | 4 +++-
4 files changed, 64 insertions(+), 36 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index 1b5725d..f484a53 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -260,6 +260,22 @@ accessors(test_batch_runner *runner)
cmark_node_destroy(doc);
}
+static void
+node_check(test_batch_runner *runner) {
+ // Construct an incomplete tree.
+ cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
+ cmark_node *p1 = cmark_node_new(CMARK_NODE_PARAGRAPH);
+ cmark_node *p2 = cmark_node_new(CMARK_NODE_PARAGRAPH);
+ doc->first_child = p1;
+ p1->next = p2;
+
+ INT_EQ(runner, cmark_node_check(doc, NULL), 4, "node_check works");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0,
+ "node_check fixes tree");
+
+ cmark_node_destroy(doc);
+}
+
static void
create_tree(test_batch_runner *runner)
{
@@ -268,27 +284,27 @@ create_tree(test_batch_runner *runner)
cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
OK(runner, cmark_node_append_child(doc, p), "append1");
- INT_EQ(runner, cmark_node_check(doc), 0, "append1 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append1 consistent");
OK(runner, cmark_node_parent(p) == doc, "node_parent");
cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH);
OK(runner, cmark_node_prepend_child(p, emph), "prepend1");
- INT_EQ(runner, cmark_node_check(doc), 0, "prepend1 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend1 consistent");
cmark_node *str1 = cmark_node_new(CMARK_NODE_STRING);
cmark_node_set_string_content(str1, "Hello, ");
OK(runner, cmark_node_prepend_child(p, str1), "prepend2");
- INT_EQ(runner, cmark_node_check(doc), 0, "prepend2 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend2 consistent");
cmark_node *str3 = cmark_node_new(CMARK_NODE_STRING);
cmark_node_set_string_content(str3, "!");
OK(runner, cmark_node_append_child(p, str3), "append2");
- INT_EQ(runner, cmark_node_check(doc), 0, "append2 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append2 consistent");
cmark_node *str2 = cmark_node_new(CMARK_NODE_STRING);
cmark_node_set_string_content(str2, "world");
OK(runner, cmark_node_append_child(emph, str2), "append3");
- INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append3 consistent");
html = (char *)cmark_render_html(doc);
STR_EQ(runner, html, "Hello, world!
\n",
@@ -296,22 +312,26 @@ create_tree(test_batch_runner *runner)
free(html);
OK(runner, cmark_node_insert_before(str1, str3), "ins before1");
- INT_EQ(runner, cmark_node_check(doc), 0, "ins before1 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0,
+ "ins before1 consistent");
// 31e
OK(runner, cmark_node_first_child(p) == str3, "ins before1 works");
OK(runner, cmark_node_insert_before(str1, emph), "ins before2");
- INT_EQ(runner, cmark_node_check(doc), 0, "ins before2 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0,
+ "ins before2 consistent");
// 3e1
OK(runner, cmark_node_last_child(p) == str1, "ins before2 works");
OK(runner, cmark_node_insert_after(str1, str3), "ins after1");
- INT_EQ(runner, cmark_node_check(doc), 0, "ins after1 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0,
+ "ins after1 consistent");
// e13
OK(runner, cmark_node_next(str1) == str3, "ins after1 works");
OK(runner, cmark_node_insert_after(str1, emph), "ins after2");
- INT_EQ(runner, cmark_node_check(doc), 0, "ins after2 consistent");
+ INT_EQ(runner, cmark_node_check(doc, NULL), 0,
+ "ins after2 consistent");
// 1e3
OK(runner, cmark_node_previous(emph) == str1, "ins after2 works");
@@ -425,6 +445,7 @@ int main() {
constructor(runner);
accessors(runner);
+ node_check(runner);
create_tree(runner);
hierarchy(runner);
diff --git a/src/blocks.c b/src/blocks.c
index 58162b5..ab9f667 100644
--- a/src/blocks.c
+++ b/src/blocks.c
@@ -818,7 +818,7 @@ cmark_node *cmark_finish(cmark_doc_parser *parser)
finalize_document(parser);
strbuf_free(parser->curline);
#if CMARK_DEBUG_NODES
- if (cmark_node_check(parser->root)) {
+ if (cmark_node_check(parser->root, stderr)) {
abort();
}
#endif
diff --git a/src/node.c b/src/node.c
index bb9cacc..a79dd82 100644
--- a/src/node.c
+++ b/src/node.c
@@ -558,58 +558,63 @@ cmark_node_append_child(cmark_node *node, cmark_node *child)
}
static void
-S_print_error(cmark_node *node, const char *elem)
+S_print_error(FILE *out, cmark_node *node, const char *elem)
{
- fprintf(stderr, "Invalid '%s' in node type %s at %d:%d\n", elem,
+ if (out == NULL) {
+ return;
+ }
+ fprintf(out, "Invalid '%s' in node type %s at %d:%d\n", elem,
S_type_string(node), node->start_line, node->start_column);
}
int
-cmark_node_check(cmark_node *node)
+cmark_node_check(cmark_node *node, FILE *out)
{
- cmark_node *cur = node;
+ cmark_node *cur;
int errors = 0;
- while (cur) {
+ if (!node) {
+ return 0;
+ }
+
+ cur = node;
+ while (true) {
if (cur->first_child) {
if (cur->first_child->parent != cur) {
- S_print_error(cur->first_child, "parent");
+ S_print_error(out, cur->first_child, "parent");
cur->first_child->parent = cur;
++errors;
}
cur = cur->first_child;
+ continue;
+ }
+
+ next_sibling:
+ if (cur == node) {
+ break;
}
- else if (cur->next) {
+ if (cur->next) {
if (cur->next->prev != cur) {
- S_print_error(cur->next, "prev");
+ S_print_error(out, cur->next, "prev");
cur->next->prev = cur;
++errors;
}
if (cur->next->parent != cur->parent) {
- S_print_error(cur->next, "parent");
+ S_print_error(out, cur->next, "parent");
cur->next->parent = cur->parent;
++errors;
}
cur = cur->next;
+ continue;
}
- else {
- if (cur->parent->last_child != cur) {
- S_print_error(cur->parent, "last_child");
- cur->parent->last_child = cur;
- ++errors;
- }
- cmark_node *ancestor = cur->parent;
- cur = NULL;
-
- while (ancestor != node->parent) {
- if (ancestor->next) {
- cur = ancestor->next;
- break;
- }
- ancestor = ancestor->parent;
- }
+ if (cur->parent->last_child != cur) {
+ S_print_error(out, cur->parent, "last_child");
+ cur->parent->last_child = cur;
+ ++errors;
}
+ cur = cur->parent;
+ goto next_sibling;
}
return errors;
diff --git a/src/node.h b/src/node.h
index 2d7f0a1..d1245a5 100644
--- a/src/node.h
+++ b/src/node.h
@@ -5,6 +5,8 @@
extern "C" {
#endif
+#include
+
#include "cmark.h"
#include "buffer.h"
#include "chunk.h"
@@ -62,7 +64,7 @@ struct cmark_node {
};
CMARK_EXPORT int
-cmark_node_check(cmark_node *node);
+cmark_node_check(cmark_node *node, FILE *out);
#ifdef __cplusplus
}
--
cgit v1.2.3
From a4b54c861e384e5574eda36d7b4bb050322b1c34 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Wed, 19 Nov 2014 20:23:59 +0100
Subject: Fix debug flags
---
src/CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dbe53a5..71f45a7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -64,7 +64,7 @@ set_target_properties(${PROGRAM} PROPERTIES
COMPILE_FLAGS -DCMARK_STATIC_DEFINE)
# Check integrity of node structure when compiled as debug:
-set(CMAKE_C_FLAGS_DEBUG "-DCMARK_DEBUG_NODES")
+set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DCMARK_DEBUG_NODES")
if (${CMAKE_MAJOR_VERSION} GREATER 1 AND ${CMAKE_MINOR_VERSION} GREATER 8)
set(CMAKE_C_VISIBILITY_PRESET hidden)
--
cgit v1.2.3
From 1cf1ec9a8f893a41910a300d9d8f1e6c20288287 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Wed, 19 Nov 2014 20:24:08 +0100
Subject: Stackless HTML rendering
Now that every node has a parent pointer, it's possible to implement
the HTML rendering functions without render stacks and any dynamic
memory allocations.
This commit also adds some minor optimizations that eliminate some
strbuf_put* calls for the common case and avoid printf for headers.
---
api_test/main.c | 32 +++-
src/html/html.c | 463 +++++++++++++++++++++++++++-----------------------------
2 files changed, 257 insertions(+), 238 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index f484a53..35570e9 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -197,7 +197,7 @@ accessors(test_batch_runner *runner)
"Item 2
\n"
"\n"
"\n"
- "type) {
+ case NODE_PARAGRAPH:
+ if (!tight) {
+ strbuf_puts(html, "
\n");
+ }
+ break;
+
+ case NODE_BQUOTE: {
+ cmark_list *list = &node->as.list;
+ strbuf_puts(html, "\n");
+ // Restore old 'tight' value.
+ tight = list->tight;
+ list->tight = false;
+ break;
+ }
- if (b->type == NODE_FENCED_CODE) {
- strbuf *info = &b->as.code.info;
+ case NODE_LIST_ITEM:
+ strbuf_puts(html, "\n");
+ break;
+
+ case NODE_LIST: {
+ cmark_list *list = &node->as.list;
+ bool tmp;
+ strbuf_puts(html,
+ list->list_type == CMARK_BULLET_LIST ?
+ "\n" : "\n");
+ // Restore old 'tight' value.
+ tmp = tight;
+ tight = list->tight;
+ list->tight = tmp;
+ break;
+ }
- if (strbuf_len(info) > 0) {
- int first_tag = strbuf_strchr(info, ' ', 0);
- if (first_tag < 0)
- first_tag = strbuf_len(info);
+ case NODE_ATX_HEADER:
+ case NODE_SETEXT_HEADER:
+ end_header[3] = '0' + node->as.header.level;
+ strbuf_puts(html, end_header);
+ break;
- strbuf_puts(html, " class=\"language-");
- escape_html(html, info->ptr, first_tag);
- strbuf_putc(html, '"');
- }
- }
+ case NODE_INDENTED_CODE:
+ case NODE_FENCED_CODE:
+ strbuf_puts(html, "\n");
+ break;
- strbuf_putc(html, '>');
- escape_html(html, b->string_content.ptr, b->string_content.size);
- strbuf_puts(html, "\n");
- break;
+ case NODE_INLINE_CODE:
+ strbuf_puts(html, "");
+ break;
- case NODE_HTML:
- strbuf_put(html, b->string_content.ptr, b->string_content.size);
- break;
+ case NODE_LINK:
+ strbuf_puts(html, "");
+ break;
- case NODE_HRULE:
- strbuf_puts(html, "
\n");
- break;
+ case NODE_STRONG:
+ strbuf_puts(html, "");
+ break;
- case NODE_REFERENCE_DEF:
- break;
+ case NODE_EMPH:
+ strbuf_puts(html, "");
+ break;
- default:
- assert(false);
- }
- if (visit_children) {
- b = b->first_child;
- } else {
- b = b->next;
- }
- while (b == NULL && rstack != NULL) {
- strbuf_puts(html, rstack->literal);
- if (rstack->trim) {
- strbuf_rtrim(html);
- }
- tight = rstack->tight;
- b = rstack->next_sibling;
- rstack = pop_render_stack(rstack);
- }
+ default:
+ break;
}
- free_render_stack(rstack);
+ return tight;
}
unsigned char *cmark_render_html(cmark_node *root)
{
unsigned char *result;
strbuf html = GH_BUF_INIT;
- blocks_to_html(&html, root);
+ node_to_html(&html, root);
result = strbuf_detach(&html);
strbuf_free(&html);
return result;
--
cgit v1.2.3
From a3e1e9918745d2a1cf921d7b13b94ffcc45d50fe Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Sat, 22 Nov 2014 16:48:01 +0100
Subject: Fix hierarchy test
---
api_test/main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/api_test/main.c b/api_test/main.c
index 35570e9..da9e241 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -428,7 +428,7 @@ test_content(test_batch_runner *runner, cmark_node_type type,
cmark_node *child = cmark_node_new(child_type);
int got = cmark_node_append_child(node, child);
- int expected = (allowed_content >> i) & 1;
+ int expected = (allowed_content >> child_type) & 1;
INT_EQ(runner, got, expected,
"add %d as child of %d", child_type, type);
--
cgit v1.2.3
From c100b483db2a35d004288f128c0c27d9976fc9c9 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Sat, 22 Nov 2014 17:16:10 +0100
Subject: Make render_html return a char*
---
api_test/main.c | 16 +++++++++-------
src/cmark.c | 4 ++--
src/cmark.h | 4 ++--
src/html/html.c | 6 +++---
src/main.c | 2 +-
5 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index da9e241..a946c1a 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -2,6 +2,7 @@
#include
#include
+#define CMARK_NO_SHORT_NAMES
#include "cmark.h"
#include "node.h"
@@ -186,7 +187,7 @@ accessors(test_batch_runner *runner)
OK(runner, cmark_node_set_string_content(string, "LINK"),
"set_string_content string");
- char *rendered_html = (char *)cmark_render_html(doc);
+ char *rendered_html = cmark_render_html(doc);
static const char expected_html[] =
"Header
\n"
"\n"
@@ -306,7 +307,7 @@ create_tree(test_batch_runner *runner)
OK(runner, cmark_node_append_child(emph, str2), "append3");
INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append3 consistent");
- html = (char *)cmark_render_html(doc);
+ html = cmark_render_html(doc);
STR_EQ(runner, html, "Hello, world!
\n",
"render_html");
free(html);
@@ -337,7 +338,7 @@ create_tree(test_batch_runner *runner)
cmark_node_unlink(emph);
- html = (char *)cmark_render_html(doc);
+ html = cmark_render_html(doc);
STR_EQ(runner, html, "Hello, !
\n",
"render_html after shuffling");
free(html);
@@ -439,7 +440,8 @@ test_content(test_batch_runner *runner, cmark_node_type type,
cmark_node_destroy(node);
}
-void render_html(test_batch_runner *runner)
+static void
+render_html(test_batch_runner *runner)
{
char *html;
@@ -450,18 +452,18 @@ void render_html(test_batch_runner *runner)
cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1);
cmark_node *paragraph = cmark_node_first_child(doc);
- html = (char *)cmark_render_html(paragraph);
+ html = cmark_render_html(paragraph);
STR_EQ(runner, html, "foo bar
\n",
"render single paragraph");
free(html);
cmark_node *string = cmark_node_first_child(paragraph);
- html = (char *)cmark_render_html(string);
+ html = cmark_render_html(string);
STR_EQ(runner, html, "foo ", "render single inline");
free(html);
cmark_node *emph = cmark_node_next(string);
- html = (char *)cmark_render_html(emph);
+ html = cmark_render_html(emph);
STR_EQ(runner, html, "bar", "render inline with children");
free(html);
diff --git a/src/cmark.c b/src/cmark.c
index b20b84b..07b7c4c 100644
--- a/src/cmark.c
+++ b/src/cmark.c
@@ -7,10 +7,10 @@
#include "cmark.h"
#include "buffer.h"
-unsigned char *cmark_markdown_to_html(unsigned char *text, int len)
+char *cmark_markdown_to_html(unsigned char *text, int len)
{
cmark_node *blocks;
- unsigned char *result;
+ char *result;
blocks = cmark_parse_document(text, len);
diff --git a/src/cmark.h b/src/cmark.h
index a74fe93..206b667 100644
--- a/src/cmark.h
+++ b/src/cmark.h
@@ -190,10 +190,10 @@ CMARK_EXPORT
void cmark_debug_print(cmark_node *root);
CMARK_EXPORT
-unsigned char *cmark_render_html(cmark_node *root);
+char *cmark_render_html(cmark_node *root);
CMARK_EXPORT
-unsigned char *cmark_markdown_to_html(unsigned char *text, int len);
+char *cmark_markdown_to_html(unsigned char *text, int len);
#ifndef CMARK_NO_SHORT_NAMES
#define NODE_DOCUMENT CMARK_NODE_DOCUMENT
diff --git a/src/html/html.c b/src/html/html.c
index 96fce66..4fa79b1 100644
--- a/src/html/html.c
+++ b/src/html/html.c
@@ -348,12 +348,12 @@ finish_node(strbuf *html, cmark_node *node, bool tight)
return tight;
}
-unsigned char *cmark_render_html(cmark_node *root)
+char *cmark_render_html(cmark_node *root)
{
- unsigned char *result;
+ char *result;
strbuf html = GH_BUF_INIT;
node_to_html(&html, root);
- result = strbuf_detach(&html);
+ result = (char *)strbuf_detach(&html);
strbuf_free(&html);
return result;
}
diff --git a/src/main.c b/src/main.c
index 1cfddda..61d98dc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,7 +17,7 @@ void print_usage()
static void print_document(cmark_node *document, bool ast)
{
- unsigned char *result;
+ char *result;
if (ast) {
cmark_debug_print(document);
} else {
--
cgit v1.2.3
From b2dac901288a3431fdadd9bbefb7875596a13e4d Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer
Date: Sat, 22 Nov 2014 17:23:37 +0100
Subject: Make parser accept a char*
---
api_test/main.c | 4 ++--
src/blocks.c | 16 ++++++++--------
src/cmark.c | 2 +-
src/cmark.h | 6 +++---
src/main.c | 2 +-
5 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/api_test/main.c b/api_test/main.c
index a946c1a..b3b5b5a 100644
--- a/api_test/main.c
+++ b/api_test/main.c
@@ -76,7 +76,7 @@ constructor(test_batch_runner *runner)
static void
accessors(test_batch_runner *runner)
{
- static const unsigned char markdown[] =
+ static const char markdown[] =
"## Header\n"
"\n"
"* Item 1\n"
@@ -445,7 +445,7 @@ render_html(test_batch_runner *runner)
{
char *html;
- static const unsigned char markdown[] =
+ static const char markdown[] =
"foo *bar*\n"
"\n"
"paragraph 2\n";
diff --git a/src/blocks.c b/src/blocks.c
index ab9f667..a30560f 100644
--- a/src/blocks.c
+++ b/src/blocks.c
@@ -416,13 +416,13 @@ static cmark_node *finalize_document(cmark_doc_parser *parser)
extern cmark_node *cmark_parse_file(FILE *f)
{
- unsigned char buffer[4096];
+ char buffer[4096];
cmark_doc_parser *parser = cmark_new_doc_parser();
size_t offset;
cmark_node *document;
- while (fgets((char *)buffer, sizeof(buffer), f)) {
- offset = strlen((char *)buffer);
+ while (fgets(buffer, sizeof(buffer), f)) {
+ offset = strlen(buffer);
cmark_process_line(parser, buffer, offset);
}
@@ -431,16 +431,16 @@ extern cmark_node *cmark_parse_file(FILE *f)
return document;
}
-extern cmark_node *cmark_parse_document(const unsigned char *buffer, size_t len)
+extern cmark_node *cmark_parse_document(const char *buffer, size_t len)
{
int linenum = 1;
- const unsigned char *end = buffer + len;
+ const char *end = buffer + len;
size_t offset;
cmark_doc_parser *parser = cmark_new_doc_parser();
cmark_node *document;
while (buffer < end) {
- const unsigned char *eol = (unsigned char *)memchr(buffer, '\n', end - buffer);
+ const char *eol = memchr(buffer, '\n', end - buffer);
offset = eol ? (eol - buffer) + 1 : eol - buffer;
cmark_process_line(parser, buffer, offset);
buffer += offset;
@@ -470,7 +470,7 @@ static void chop_trailing_hashtags(chunk *ch)
}
}
-void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer,
+void cmark_process_line(cmark_doc_parser *parser, const char *buffer,
size_t bytes)
{
cmark_node* last_matched_container;
@@ -487,7 +487,7 @@ void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer,
int indent;
chunk input;
- utf8proc_detab(parser->curline, buffer, bytes);
+ utf8proc_detab(parser->curline, (unsigned char *)buffer, bytes);
// Add a newline to the end if not present:
// TODO this breaks abstraction:
diff --git a/src/cmark.c b/src/cmark.c
index 07b7c4c..273a37b 100644
--- a/src/cmark.c
+++ b/src/cmark.c
@@ -7,7 +7,7 @@
#include "cmark.h"
#include "buffer.h"
-char *cmark_markdown_to_html(unsigned char *text, int len)
+char *cmark_markdown_to_html(const char *text, int len)
{
cmark_node *blocks;
char *result;
diff --git a/src/cmark.h b/src/cmark.h
index 206b667..6131024 100644
--- a/src/cmark.h
+++ b/src/cmark.h
@@ -176,10 +176,10 @@ CMARK_EXPORT
cmark_node *cmark_finish(cmark_doc_parser *parser);
CMARK_EXPORT
-void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer, size_t bytes);
+void cmark_process_line(cmark_doc_parser *parser, const char *buffer, size_t bytes);
CMARK_EXPORT
-cmark_node *cmark_parse_document(const unsigned char *buffer, size_t len);
+cmark_node *cmark_parse_document(const char *buffer, size_t len);
CMARK_EXPORT
cmark_node *cmark_parse_file(FILE *f);
@@ -193,7 +193,7 @@ CMARK_EXPORT
char *cmark_render_html(cmark_node *root);
CMARK_EXPORT
-char *cmark_markdown_to_html(unsigned char *text, int len);
+char *cmark_markdown_to_html(const char *text, int len);
#ifndef CMARK_NO_SHORT_NAMES
#define NODE_DOCUMENT CMARK_NODE_DOCUMENT
diff --git a/src/main.c b/src/main.c
index 61d98dc..96e53ce 100644
--- a/src/main.c
+++ b/src/main.c
@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
int i, numfps = 0;
bool ast = false;
int *files;
- unsigned char buffer[4096];
+ char buffer[4096];
cmark_doc_parser *parser;
size_t offset;
cmark_node *document;
--
cgit v1.2.3