Skip to content

Commit

Permalink
updated to cmark 0.24.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinoman committed Jan 14, 2016
1 parent 7d62b56 commit bbc056c
Show file tree
Hide file tree
Showing 10 changed files with 10,529 additions and 16,438 deletions.
6 changes: 1 addition & 5 deletions blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,7 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,

} else if (!indented && container->type == CMARK_NODE_PARAGRAPH &&
(lev =
scan_setext_heading_line(&input, parser->first_nonspace)) &&
// check that there is only one line in the paragraph:
(cmark_strbuf_strrchr(
&container->string_content, '\n',
cmark_strbuf_len(&container->string_content) - 2) < 0)) {
scan_setext_heading_line(&input, parser->first_nonspace))) {

container->type = CMARK_NODE_HEADING;
container->as.heading.level = lev;
Expand Down
20 changes: 13 additions & 7 deletions cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@ typedef struct cmark_node cmark_node;
typedef struct cmark_parser cmark_parser;
typedef struct cmark_iter cmark_iter;

typedef enum {
CMARK_EVENT_NONE,
CMARK_EVENT_DONE,
CMARK_EVENT_ENTER,
CMARK_EVENT_EXIT
} cmark_event_type;

/**
* ## Creating and Destroying Nodes
*/
Expand Down Expand Up @@ -178,6 +171,13 @@ CMARK_EXPORT cmark_node *cmark_node_last_child(cmark_node *node);
* leaf nodes.
*/

typedef enum {
CMARK_EVENT_NONE,
CMARK_EVENT_DONE,
CMARK_EVENT_ENTER,
CMARK_EVENT_EXIT
} cmark_event_type;

/** Creates a new iterator starting at 'root'. The current node and event
* type are undefined until `cmark_iter_next` is called for the first time.
*/
Expand Down Expand Up @@ -387,6 +387,12 @@ CMARK_EXPORT int cmark_node_insert_before(cmark_node *node,
*/
CMARK_EXPORT int cmark_node_insert_after(cmark_node *node, cmark_node *sibling);

/** Replaces 'oldnode' with 'newnode' and unlinks 'oldnode' (but does
* not free its memory).
* Returns 1 on success, 0 on failure.
*/
CMARK_EXPORT int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode);

/** Adds 'child' to the beginning of the children of 'node'.
* Returns 1 on success, 0 on failure.
*/
Expand Down
4 changes: 2 additions & 2 deletions cmark_version.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CMARK_VERSION_H
#define CMARK_VERSION_H

#define CMARK_VERSION ((0 << 16) | (23 << 8) | 0)
#define CMARK_VERSION_STRING "0.23.0"
#define CMARK_VERSION ((0 << 16) | (24 << 8) | 0)
#define CMARK_VERSION_STRING "0.24.0"

#endif
22 changes: 14 additions & 8 deletions commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@ static bool is_autolink(cmark_node *node) {

// if node is a block node, returns node.
// otherwise returns first block-level node that is an ancestor of node.
// if there is no block-level ancestor, returns NULL.
static cmark_node *get_containing_block(cmark_node *node) {
while (node && (node->type < CMARK_NODE_FIRST_BLOCK ||
node->type > CMARK_NODE_LAST_BLOCK)) {
node = node->parent;
while (node) {
if (node->type >= CMARK_NODE_FIRST_BLOCK &&
node->type <= CMARK_NODE_LAST_BLOCK) {
return node;
} else {
node = node->parent;
}
}
return node;
return NULL;
}

static int S_render_node(cmark_renderer *renderer, cmark_node *node,
Expand All @@ -163,10 +168,11 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) {
tmp = get_containing_block(node);
renderer->in_tight_list_item =
(tmp->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent)) ||
(tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent->parent));
tmp && // tmp might be NULL if there is no containing block
((tmp->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent)) ||
(tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent->parent)));
}

switch (node->type) {
Expand Down
20 changes: 18 additions & 2 deletions commonmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func TestCMarkNodeOps(t *testing.T) {
t.Error("Couldn't prepend header to root")
}
root.AppendChild(header2)
//Replace a Node
header3 := commonmark.NewCMarkNode(commonmark.CMARK_NODE_HEADING)
header3str := commonmark.NewCMarkNode(commonmark.CMARK_NODE_TEXT)
header3.AppendChild(header3str)
header3.SetHeaderLevel(2)
if header3str.SetLiteral("Replacement header!") == false {
t.Error("SetLiteral returned false for valid input")
}
if header3.Replace(header2) == false {
t.Error("Couldn't Replace Node!")
}
//Custom nodes
custom := commonmark.NewCMarkNode(commonmark.CMARK_NODE_CUSTOM_BLOCK)
custom.SetOnEnter("ENTER")
custom.SetOnExit("EXIT")
Expand All @@ -125,10 +137,14 @@ func TestCMarkNodeOps(t *testing.T) {
t.Logf("\nXML: %v", root.RenderXML(commonmark.CMARK_OPT_DEFAULT))

htmlStr := root.RenderHtml(commonmark.CMARK_OPT_DEFAULT)
if htmlStr != "<h1>I'm the main header!</h1>\n<h2>Another header!</h2>\n" {
if htmlStr != "<h1>I'm the main header!</h1>\n<h2>Replacement header!</h2>\n" {
t.Error("htmlStr is wrong!")
}
t.Logf("Html Text: %v", htmlStr)
//Replace again
if header2.Replace(header3) == false {
t.Error("replaced node was freed prematurely.")
}
//Rearrange...
header1.InsertBefore(header2)
t.Logf("\nXML: %v", root.RenderXML(commonmark.CMARK_OPT_DEFAULT))
Expand Down Expand Up @@ -204,7 +220,7 @@ func TestCMarkCodeBlocks(t *testing.T) {
t.Logf("\nXML: %v", root.RenderXML(commonmark.CMARK_OPT_DEFAULT))
htmlString := root.RenderHtml(commonmark.CMARK_OPT_DEFAULT)
t.Logf("\nHtml String: %v\n", htmlString)
if htmlString != "<pre><code>int main(){\n return 0;\n }</code></pre>\n" {
if htmlString != "<pre><code class=\"language-c\">int main(){\n return 0;\n }</code></pre>\n" {
t.Error("htmlString isn't right!")
}
root.Free()
Expand Down
2 changes: 1 addition & 1 deletion html.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
case CMARK_NODE_CODE_BLOCK:
cr(html);

if (!node->as.code.fenced || node->as.code.info.len == 0) {
if (node->as.code.info.len == 0) {
cmark_strbuf_puts(html, "<pre");
S_render_sourcepos(node, html, options);
cmark_strbuf_puts(html, "><code>");
Expand Down
16 changes: 13 additions & 3 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ static bool S_can_contain(cmark_node *node, cmark_node *child) {
case CMARK_NODE_DOCUMENT:
case CMARK_NODE_BLOCK_QUOTE:
case CMARK_NODE_ITEM:
case CMARK_NODE_CUSTOM_BLOCK:
return S_is_block(child) && child->type != CMARK_NODE_ITEM;

case CMARK_NODE_LIST:
return child->type == CMARK_NODE_ITEM;

case CMARK_NODE_CUSTOM_BLOCK:
return true;

case CMARK_NODE_PARAGRAPH:
case CMARK_NODE_HEADING:
case CMARK_NODE_EMPH:
Expand Down Expand Up @@ -169,7 +171,7 @@ const char *cmark_node_get_type_string(cmark_node *node) {
case CMARK_NODE_HTML_BLOCK:
return "html_block";
case CMARK_NODE_CUSTOM_BLOCK:
return "raw_block";
return "custom_block";
case CMARK_NODE_PARAGRAPH:
return "paragraph";
case CMARK_NODE_HEADING:
Expand All @@ -187,7 +189,7 @@ const char *cmark_node_get_type_string(cmark_node *node) {
case CMARK_NODE_HTML_INLINE:
return "html_inline";
case CMARK_NODE_CUSTOM_INLINE:
return "raw_inline";
return "custom_inline";
case CMARK_NODE_EMPH:
return "emph";
case CMARK_NODE_STRONG:
Expand Down Expand Up @@ -728,6 +730,14 @@ int cmark_node_insert_after(cmark_node *node, cmark_node *sibling) {
return 1;
}

int cmark_node_replace(cmark_node *oldnode, cmark_node *newnode) {
if (!cmark_node_insert_before(oldnode, newnode)) {
return 0;
}
cmark_node_unlink(oldnode);
return 1;
}

int cmark_node_prepend_child(cmark_node *node, cmark_node *child) {
if (!S_can_contain(node, child)) {
return 0;
Expand Down
7 changes: 7 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ func (node *CMarkNode) InsertAfter(sibling *CMarkNode) bool {
return success(C.cmark_node_insert_after(node.node, sibling.node))
}

// Replaces 'oldNode' with 'newNode' and unlinks 'oldnode' (but does
// not free its memory).
// Returns true on success, false on failure.
func (newNode *CMarkNode) Replace(oldNode *CMarkNode) bool {
return success(C.cmark_node_replace(oldNode.node, newNode.node))
}

//Prepend a child node
func (node *CMarkNode) PrependChild(child *CMarkNode) bool {
return success(C.cmark_node_prepend_child(node.node, child.node))
Expand Down
Loading

0 comments on commit bbc056c

Please sign in to comment.