cmark

My personal build of CMark ✏️

Commit
2b1b00fb19fe470f7745c208377345975b9e0b2c
Parent
ce5671ae0289b05322c09bb80af2d3317acd544f
Author
John MacFarlane <jgm@berkeley.edu>
Date

Reformatted.

Diffstat

11 files changed, 83 insertions, 70 deletions

Status File Name N° Changes Insertions Deletions
Modified src/blocks.c 28 15 13
Modified src/buffer.c 10 5 5
Modified src/buffer.h 6 4 2
Modified src/chunk.h 6 4 2
Modified src/cmark.c 8 5 3
Modified src/cmark.h 10 5 5
Modified src/commonmark.c 18 8 10
Modified src/inlines.c 49 27 22
Modified src/inlines.h 4 2 2
Modified src/references.c 3 2 1
Modified src/render.c 11 6 5
diff --git a/src/blocks.c b/src/blocks.c
@@ -59,8 +59,8 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
 static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
                            bufsize_t bytes);
 
-static cmark_node *make_block(cmark_mem *mem, cmark_node_type tag, int start_line,
-                              int start_column) {
+static cmark_node *make_block(cmark_mem *mem, cmark_node_type tag,
+                              int start_line, int start_column) {
   cmark_node *e;
 
   e = (cmark_node *)mem->calloc(1, sizeof(*e));
@@ -247,7 +247,8 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {
   cmark_node *parent;
 
   parent = b->parent;
-  assert(b->flags & CMARK_NODE__OPEN); // shouldn't call finalize on closed blocks
+  assert(b->flags &
+         CMARK_NODE__OPEN); // shouldn't call finalize on closed blocks
   b->flags &= ~CMARK_NODE__OPEN;
 
   if (parser->curline.size == 0) {
@@ -361,7 +362,8 @@ static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
     parent = finalize(parser, parent);
   }
 
-  cmark_node *child = make_block(parser->mem, block_type, parser->line_number, start_column);
+  cmark_node *child =
+      make_block(parser->mem, block_type, parser->line_number, start_column);
   child->parent = parent;
 
   if (parent->last_child) {
@@ -377,8 +379,8 @@ static cmark_node *add_child(cmark_parser *parser, cmark_node *parent,
 
 // Walk through node and all children, recursively, parsing
 // string content into inline content where appropriate.
-static void process_inlines(cmark_mem *mem, cmark_node *root, cmark_reference_map *refmap,
-                            int options) {
+static void process_inlines(cmark_mem *mem, cmark_node *root,
+                            cmark_reference_map *refmap, int options) {
   cmark_iter *iter = cmark_iter_new(root);
   cmark_node *cur;
   cmark_event_type ev_type;
@@ -398,8 +400,8 @@ static void process_inlines(cmark_mem *mem, cmark_node *root, cmark_reference_ma
 // Attempts to parse a list item marker (bullet or enumerated).
 // On success, returns length of the marker, and populates
 // data with the details.  On failure, returns 0.
-static bufsize_t parse_list_marker(cmark_mem *mem, cmark_chunk *input, bufsize_t pos,
-                                   cmark_list **dataptr) {
+static bufsize_t parse_list_marker(cmark_mem *mem, cmark_chunk *input,
+                                   bufsize_t pos, cmark_list **dataptr) {
   unsigned char c;
   bufsize_t startpos;
   cmark_list *data;
@@ -655,7 +657,8 @@ static void S_advance_offset(cmark_parser *parser, cmark_chunk *input,
 }
 
 static bool S_last_child_is_open(cmark_node *container) {
-  return container->last_child && (container->last_child->flags & CMARK_NODE__OPEN);
+  return container->last_child &&
+         (container->last_child->flags & CMARK_NODE__OPEN);
 }
 
 static bool parse_block_quote_prefix(cmark_parser *parser, cmark_chunk *input) {
@@ -917,8 +920,8 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container,
       *container = add_child(parser, *container, CMARK_NODE_THEMATIC_BREAK,
                              parser->first_nonspace + 1);
       S_advance_offset(parser, input, input->len - 1 - parser->offset, false);
-    } else if ((matched =
-                    parse_list_marker(parser->mem, input, parser->first_nonspace, &data)) &&
+    } else if ((matched = parse_list_marker(parser->mem, input,
+                                            parser->first_nonspace, &data)) &&
                (!indented || cont_type == CMARK_NODE_LIST)) {
       // Note that we can have new list items starting with >= 4
       // spaces indent, as long as the list container is still open.
@@ -1015,8 +1018,7 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
   const cmark_node_type ctype = S_type(container);
   const bool last_line_blank =
       (parser->blank && ctype != CMARK_NODE_BLOCK_QUOTE &&
-       ctype != CMARK_NODE_HEADING &&
-       ctype != CMARK_NODE_THEMATIC_BREAK &&
+       ctype != CMARK_NODE_HEADING && ctype != CMARK_NODE_THEMATIC_BREAK &&
        !(ctype == CMARK_NODE_CODE_BLOCK && container->as.code.fenced) &&
        !(ctype == CMARK_NODE_ITEM && container->first_child == NULL &&
          container->start_line == parser->line_number));
diff --git a/src/buffer.c b/src/buffer.c
@@ -21,7 +21,8 @@ unsigned char cmark_strbuf__initbuf[1];
 #define MIN(x, y) ((x < y) ? x : y)
 #endif
 
-void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf, bufsize_t initial_size) {
+void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
+                       bufsize_t initial_size) {
   buf->mem = mem;
   buf->asize = 0;
   buf->size = 0;
@@ -50,8 +51,8 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
   new_size += 1;
   new_size = (new_size + 7) & ~7;
 
-  buf->ptr = (unsigned char *)buf->mem->realloc(
-		buf->asize ? buf->ptr : NULL, new_size);
+  buf->ptr = (unsigned char *)buf->mem->realloc(buf->asize ? buf->ptr : NULL,
+                                                new_size);
   buf->asize = new_size;
 }
 
@@ -112,8 +113,7 @@ void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
 }
 
 void cmark_strbuf_puts(cmark_strbuf *buf, const char *string) {
-  cmark_strbuf_put(buf, (const unsigned char *)string,
-                   strlen(string));
+  cmark_strbuf_put(buf, (const unsigned char *)string, strlen(string));
 }
 
 void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
diff --git a/src/buffer.h b/src/buffer.h
@@ -23,7 +23,8 @@ typedef struct {
 
 extern unsigned char cmark_strbuf__initbuf[];
 
-#define CMARK_BUF_INIT(mem) {mem, cmark_strbuf__initbuf, 0, 0}
+#define CMARK_BUF_INIT(mem)                                                    \
+  { mem, cmark_strbuf__initbuf, 0, 0 }
 
 /**
  * Initialize a cmark_strbuf structure.
@@ -31,7 +32,8 @@ extern unsigned char cmark_strbuf__initbuf[];
  * For the cases where CMARK_BUF_INIT cannot be used to do static
  * initialization.
  */
-void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf, bufsize_t initial_size);
+void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf,
+                       bufsize_t initial_size);
 
 /**
  * Grow the buffer to hold at least `target_size` bytes.
diff --git a/src/chunk.h b/src/chunk.h
@@ -57,7 +57,8 @@ static CMARK_INLINE bufsize_t
   return p ? (bufsize_t)(p - ch->data) : ch->len;
 }
 
-static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem, cmark_chunk *c) {
+static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem,
+                                                    cmark_chunk *c) {
   unsigned char *str;
 
   if (c->alloc) {
@@ -74,7 +75,8 @@ static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem, cmark_chunk 
   return (char *)str;
 }
 
-static CMARK_INLINE void cmark_chunk_set_cstr(cmark_mem *mem, cmark_chunk *c, const char *str) {
+static CMARK_INLINE void cmark_chunk_set_cstr(cmark_mem *mem, cmark_chunk *c,
+                                              const char *str) {
   if (c->alloc) {
     mem->free(c->data);
   }
diff --git a/src/cmark.c b/src/cmark.c
@@ -12,17 +12,19 @@ const char *cmark_version_string() { return CMARK_VERSION_STRING; }
 
 static void *xcalloc(size_t nmem, size_t size) {
   void *ptr = calloc(nmem, size);
-  if (!ptr) abort();
+  if (!ptr)
+    abort();
   return ptr;
 }
 
 static void *xrealloc(void *ptr, size_t size) {
   void *new_ptr = realloc(ptr, size);
-  if (!new_ptr) abort();
+  if (!new_ptr)
+    abort();
   return new_ptr;
 }
 
-cmark_mem DEFAULT_MEM_ALLOCATOR = { xcalloc, xrealloc, free };
+cmark_mem DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, free};
 
 char *cmark_markdown_to_html(const char *text, size_t len, int options) {
   cmark_node *doc;
diff --git a/src/cmark.h b/src/cmark.h
@@ -95,12 +95,11 @@ typedef struct cmark_iter cmark_iter;
  * when parsing and allocating a document tree
  */
 typedef struct cmark_mem {
-	void *(*calloc)(size_t, size_t);
-	void *(*realloc)(void *, size_t);
-	void (*free)(void *);
+  void *(*calloc)(size_t, size_t);
+  void *(*realloc)(void *, size_t);
+  void (*free)(void *);
 } cmark_mem;
 
-
 /**
  * ## Creating and Destroying Nodes
  */
@@ -115,7 +114,8 @@ CMARK_EXPORT cmark_node *cmark_node_new(cmark_node_type type);
  * allocator used to allocate the node.  Note:  be sure to use the same
  * allocator for every node in a tree, or bad things can happen.
  */
-CMARK_EXPORT cmark_node *cmark_node_new_with_mem(cmark_node_type type, cmark_mem *mem);
+CMARK_EXPORT cmark_node *cmark_node_new_with_mem(cmark_node_type type,
+                                                 cmark_mem *mem);
 
 /** Frees the memory allocated for a node and any children.
  */
diff --git a/src/commonmark.c b/src/commonmark.c
@@ -29,8 +29,7 @@ static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_escaping escape,
   char encoded[ENCODED_SIZE];
 
   needs_escaping =
-      c < 0x80 &&
-      escape != LITERAL &&
+      c < 0x80 && escape != LITERAL &&
       ((escape == NORMAL &&
         (c == '*' || c == '_' || c == '[' || c == ']' || c == '#' || c == '<' ||
          c == '>' || c == '\\' || c == '`' || c == '!' ||
@@ -41,9 +40,9 @@ static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_escaping escape,
           !follows_digit) ||
          (renderer->begin_content && (c == '.' || c == ')') && follows_digit &&
           (nextc == 0 || cmark_isspace(nextc))))) ||
-       (escape == URL && (c == '`' || c == '<' || c == '>' ||
-                          cmark_isspace(c) || c == '\\' || c == ')' ||
-                          c == '(')) ||
+       (escape == URL &&
+        (c == '`' || c == '<' || c == '>' || cmark_isspace(c) || c == '\\' ||
+         c == ')' || c == '(')) ||
        (escape == TITLE &&
         (c == '`' || c == '<' || c == '>' || c == '"' || c == '\\')));
 
@@ -268,7 +267,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
 
   case CMARK_NODE_CODE_BLOCK:
     first_in_list_item = node->prev == NULL && node->parent &&
-          node->parent->type == CMARK_NODE_ITEM;
+                         node->parent->type == CMARK_NODE_ITEM;
 
     if (!first_in_list_item) {
       BLANKLINE();
@@ -348,10 +347,9 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
     if (CMARK_OPT_HARDBREAKS & options) {
       LIT("  ");
       CR();
-    } else if (!renderer->no_linebreaks &&
-		renderer->width == 0 &&
-		!(CMARK_OPT_HARDBREAKS & options) &&
-                !(CMARK_OPT_NOBREAKS & options)) {
+    } else if (!renderer->no_linebreaks && renderer->width == 0 &&
+               !(CMARK_OPT_HARDBREAKS & options) &&
+               !(CMARK_OPT_NOBREAKS & options)) {
       CR();
     } else {
       OUT(" ", allow_wrap, LITERAL);
diff --git a/src/inlines.c b/src/inlines.c
@@ -40,10 +40,10 @@ typedef struct delimiter {
 } delimiter;
 
 typedef struct bracket {
-	struct bracket *previous;
-	struct delimiter *previous_delimiter;
-	cmark_node *inl_text;
-	bufsize_t position;
+  struct bracket *previous;
+  struct delimiter *previous_delimiter;
+  cmark_node *inl_text;
+  bufsize_t position;
   bool image;
   bool active;
   bool bracket_after;
@@ -55,7 +55,7 @@ typedef struct {
   bufsize_t pos;
   cmark_reference_map *refmap;
   delimiter *last_delim;
-  bracket   *last_bracket;
+  bracket *last_bracket;
 } subject;
 
 static CMARK_INLINE bool S_is_line_end_char(char c) {
@@ -72,7 +72,8 @@ static void subject_from_buf(cmark_mem *mem, subject *e, cmark_strbuf *buffer,
 static bufsize_t subject_find_special_char(subject *subj, int options);
 
 // Create an inline with a literal string value.
-static CMARK_INLINE cmark_node *make_literal(cmark_mem *mem, cmark_node_type t, cmark_chunk s) {
+static CMARK_INLINE cmark_node *make_literal(cmark_mem *mem, cmark_node_type t,
+                                             cmark_chunk s) {
   cmark_node *e = (cmark_node *)mem->calloc(1, sizeof(*e));
   cmark_strbuf_init(mem, &e->content, 0);
   e->type = t;
@@ -89,7 +90,8 @@ static CMARK_INLINE cmark_node *make_simple(cmark_mem *mem, cmark_node_type t) {
 }
 
 // Like make_str, but parses entities.
-static cmark_node *make_str_with_entities(cmark_mem *mem, cmark_chunk *content) {
+static cmark_node *make_str_with_entities(cmark_mem *mem,
+                                          cmark_chunk *content) {
   cmark_strbuf unescaped = CMARK_BUF_INIT(mem);
 
   if (houdini_unescape_html(&unescaped, content->data, content->len)) {
@@ -114,7 +116,8 @@ static cmark_chunk chunk_clone(cmark_mem *mem, cmark_chunk *src) {
   return c;
 }
 
-static cmark_chunk cmark_clean_autolink(cmark_mem *mem, cmark_chunk *url, int is_email) {
+static cmark_chunk cmark_clean_autolink(cmark_mem *mem, cmark_chunk *url,
+                                        int is_email) {
   cmark_strbuf buf = CMARK_BUF_INIT(mem);
 
   cmark_chunk_trim(url);
@@ -131,7 +134,8 @@ static cmark_chunk cmark_clean_autolink(cmark_mem *mem, cmark_chunk *url, int is
   return cmark_chunk_buf_detach(&buf);
 }
 
-static CMARK_INLINE cmark_node *make_autolink(cmark_mem *mem, cmark_chunk url, int is_email) {
+static CMARK_INLINE cmark_node *make_autolink(cmark_mem *mem, cmark_chunk url,
+                                              int is_email) {
   cmark_node *link = make_simple(mem, CMARK_NODE_LINK);
   link->as.link.url = cmark_clean_autolink(mem, &url, is_email);
   link->as.link.title = cmark_chunk_literal("");
@@ -361,7 +365,6 @@ static void pop_bracket(subject *subj) {
   free(b);
 }
 
-
 static void push_delimiter(subject *subj, unsigned char c, bool can_open,
                            bool can_close, cmark_node *inl_text) {
   delimiter *delim = (delimiter *)subj->mem->calloc(1, sizeof(delimiter));
@@ -506,13 +509,15 @@ static void process_emphasis(subject *subj, delimiter *stack_bottom) {
       odd_match = false;
       while (opener != NULL && opener != stack_bottom &&
              opener != openers_bottom[closer->delim_char]) {
-	// interior closer of size 2 can't match opener of size 1
-	// or of size 1 can't match 2
-	odd_match = (closer->can_open || opener->can_close) &&
-		((opener->inl_text->as.literal.len +
-		    closer->inl_text->as.literal.len) % 3 == 0);
+        // interior closer of size 2 can't match opener of size 1
+        // or of size 1 can't match 2
+        odd_match = (closer->can_open || opener->can_close) &&
+                    ((opener->inl_text->as.literal.len +
+                      closer->inl_text->as.literal.len) %
+                         3 ==
+                     0);
         if (opener->delim_char == closer->delim_char && opener->can_open &&
-			!odd_match) {
+            !odd_match) {
           opener_found = true;
           break;
         }
@@ -544,10 +549,10 @@ static void process_emphasis(subject *subj, delimiter *stack_bottom) {
       }
       if (!opener_found && !odd_match) {
         // set lower bound for future searches for openers
-	// (we don't do this with 'odd_match' set because
-	// a ** that didn't match an earlier * might turn into
-	// an opener, and the * might be matched by something
-	// else.
+        // (we don't do this with 'odd_match' set because
+        // a ** that didn't match an earlier * might turn into
+        // an opener, and the * might be matched by something
+        // else.
         openers_bottom[old_closer->delim_char] = old_closer->previous;
         if (!old_closer->can_open) {
           // we can remove a closer that can't be an
@@ -1079,8 +1084,8 @@ static int parse_inline(subject *subj, cmark_node *parent, int options) {
 }
 
 // Parse inlines from parent's string_content, adding as children of parent.
-extern void cmark_parse_inlines(cmark_mem *mem, cmark_node *parent, cmark_reference_map *refmap,
-                                int options) {
+extern void cmark_parse_inlines(cmark_mem *mem, cmark_node *parent,
+                                cmark_reference_map *refmap, int options) {
   subject subj;
   subject_from_buf(mem, &subj, &parent->content, refmap);
   cmark_chunk_rtrim(&subj.input);
diff --git a/src/inlines.h b/src/inlines.h
@@ -8,8 +8,8 @@ extern "C" {
 cmark_chunk cmark_clean_url(cmark_mem *mem, cmark_chunk *url);
 cmark_chunk cmark_clean_title(cmark_mem *mem, cmark_chunk *title);
 
-void cmark_parse_inlines(cmark_mem *mem, cmark_node *parent, cmark_reference_map *refmap,
-                         int options);
+void cmark_parse_inlines(cmark_mem *mem, cmark_node *parent,
+                         cmark_reference_map *refmap, int options);
 
 bufsize_t cmark_parse_reference_inline(cmark_mem *mem, cmark_strbuf *input,
                                        cmark_reference_map *refmap);
diff --git a/src/references.c b/src/references.c
@@ -139,7 +139,8 @@ void cmark_reference_map_free(cmark_reference_map *map) {
 }
 
 cmark_reference_map *cmark_reference_map_new(cmark_mem *mem) {
-  cmark_reference_map *map = (cmark_reference_map *)mem->calloc(1, sizeof(cmark_reference_map));
+  cmark_reference_map *map =
+      (cmark_reference_map *)mem->calloc(1, sizeof(cmark_reference_map));
   map->mem = mem;
   return map;
 }
diff --git a/src/render.c b/src/render.c
@@ -109,8 +109,9 @@ static void S_out(cmark_renderer *renderer, const char *source, bool wrap,
         !renderer->begin_line && renderer->last_breakable > 0) {
 
       // copy from last_breakable to remainder
-      cmark_chunk_set_cstr(renderer->mem, &remainder, (char *)renderer->buffer->ptr +
-                                           renderer->last_breakable + 1);
+      cmark_chunk_set_cstr(renderer->mem, &remainder,
+                           (char *)renderer->buffer->ptr +
+                               renderer->last_breakable + 1);
       // truncate at last_breakable
       cmark_strbuf_truncate(renderer->buffer, renderer->last_breakable);
       // add newline, prefix, and remainder
@@ -155,9 +156,9 @@ char *cmark_render(cmark_node *root, int options, int width,
   char *result;
   cmark_iter *iter = cmark_iter_new(root);
 
-  cmark_renderer renderer = {mem, &buf, &pref, 0, width, 0,
-                             0,    true,  true,        false, false,
-                             outc, S_cr,  S_blankline, S_out};
+  cmark_renderer renderer = {mem,   &buf, &pref, 0,           width,
+                             0,     0,    true,  true,        false,
+                             false, outc, S_cr,  S_blankline, S_out};
 
   while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
     cur = cmark_iter_get_node(iter);