Skip to content

Commit

Permalink
Export cmark_is_inline, cmark_is_block, cmark_is_leaf.
Browse files Browse the repository at this point in the history
[Non-breaking API change]
  • Loading branch information
jgm committed Sep 3, 2024
1 parent 15d3928 commit e1b44d7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
24 changes: 23 additions & 1 deletion man/man3/cmark.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH cmark 3 "August 03, 2024" "cmark 0.31.1" "Library Functions Manual"
.TH cmark 3 "September 02, 2024" "cmark 0.31.1" "Library Functions Manual"
.SH
NAME
.PP
Expand Down Expand Up @@ -121,6 +121,28 @@ and allocating a document tree
.PP
Returns a pointer to the default memory allocator.

.SS
Classifying nodes

.PP
\fIbool\f[] \fBcmark_is_block\f[](\fIcmark_node *node\f[])

.PP
Returns true if the node is a block node. */

.PP
\fIbool\f[] \fBcmark_is_inline\f[](\fIcmark_node *node\f[])

.PP
Returns true if the node is an inline node. */

.PP
\fIbool\f[] \fBcmark_is_leaf\f[](\fIcmark_node *node\f[])

.PP
Returns true if the node is a leaf node (a node that cannot contain
children). */

.SS
Creating and Destroying Nodes

Expand Down
18 changes: 18 additions & 0 deletions src/cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CMARK_H

#include <stdio.h>
#include <stdbool.h>
#include <cmark_export.h>
#include <cmark_version.h>

Expand Down Expand Up @@ -104,6 +105,23 @@ typedef struct cmark_mem {
*/
CMARK_EXPORT cmark_mem *cmark_get_default_mem_allocator(void);

/**
* ## Classifying nodes
*/

/** Returns true if the node is a block node.
*/
CMARK_EXPORT bool cmark_is_block(cmark_node *node);

/** Returns true if the node is an inline node.
*/
CMARK_EXPORT bool cmark_is_inline(cmark_node *node);

/** Returns true if the node is a leaf node (a node that cannot
contain children).
*/
CMARK_EXPORT bool cmark_is_leaf(cmark_node *node);

/**
* ## Creating and Destroying Nodes
*/
Expand Down
24 changes: 20 additions & 4 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,38 @@

static void S_node_unlink(cmark_node *node);

static inline bool S_is_block(cmark_node *node) {
bool cmark_is_block(cmark_node *node) {
if (node == NULL) {
return false;
}
return node->type >= CMARK_NODE_FIRST_BLOCK &&
node->type <= CMARK_NODE_LAST_BLOCK;
}

static inline bool S_is_inline(cmark_node *node) {
bool cmark_is_inline(cmark_node *node) {
if (node == NULL) {
return false;
}
return node->type >= CMARK_NODE_FIRST_INLINE &&
node->type <= CMARK_NODE_LAST_INLINE;
}

bool cmark_is_leaf(cmark_node *node) {
if (node == NULL) {
return false;
}
switch (node->type) {
case CMARK_NODE_THEMATIC_BREAK: return true;
case CMARK_NODE_CODE_BLOCK : return true;
case CMARK_NODE_TEXT : return true;
case CMARK_NODE_SOFTBREAK : return true;
case CMARK_NODE_LINEBREAK : return true;
case CMARK_NODE_CODE : return true;
case CMARK_NODE_HTML_INLINE: return true;
}
return false;
}

static bool S_can_contain(cmark_node *node, cmark_node *child) {
if (node == NULL || child == NULL || node == child) {
return false;
Expand All @@ -47,7 +63,7 @@ static bool S_can_contain(cmark_node *node, cmark_node *child) {
case CMARK_NODE_DOCUMENT:
case CMARK_NODE_BLOCK_QUOTE:
case CMARK_NODE_ITEM:
return S_is_block(child) && child->type != CMARK_NODE_ITEM;
return cmark_is_block(child) && child->type != CMARK_NODE_ITEM;

case CMARK_NODE_LIST:
return child->type == CMARK_NODE_ITEM;
Expand All @@ -62,7 +78,7 @@ static bool S_can_contain(cmark_node *node, cmark_node *child) {
case CMARK_NODE_LINK:
case CMARK_NODE_IMAGE:
case CMARK_NODE_CUSTOM_INLINE:
return S_is_inline(child);
return cmark_is_inline(child);

default:
break;
Expand Down

0 comments on commit e1b44d7

Please sign in to comment.