diff options
-rw-r--r-- | src/blocks.c | 6 | ||||
-rw-r--r-- | src/buffer.h | 108 | ||||
-rw-r--r-- | src/chunk.h | 45 | ||||
-rw-r--r-- | src/cmark.h | 133 | ||||
-rw-r--r-- | src/inlines.c | 4 | ||||
-rw-r--r-- | src/inlines.h | 21 | ||||
-rw-r--r-- | src/references.h | 29 | ||||
-rw-r--r-- | src/utf8.h | 6 |
8 files changed, 225 insertions, 127 deletions
diff --git a/src/blocks.c b/src/blocks.c index ccb84a7..a70f5f8 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -33,7 +33,7 @@ static node_block* make_block(int tag, int start_line, int start_column) } // Create a root document node_block. -extern node_block* make_document() +static node_block* make_document() { node_block *e = make_block(BLOCK_DOCUMENT, 1, 1); e->as.document.refmap = reference_map_new(); @@ -43,7 +43,7 @@ extern node_block* make_document() } // Returns true if line has only space characters, else false. -bool is_blank(strbuf *s, int offset) +static bool is_blank(strbuf *s, int offset) { while (offset < s->size) { switch (s->ptr[offset]) { @@ -283,7 +283,7 @@ typedef struct BlockStack { // Walk through node_block and all children, recursively, parsing // string content into inline content where appropriate. -void process_inlines(node_block* cur, reference_map *refmap) +static void process_inlines(node_block* cur, reference_map *refmap) { block_stack* stack = NULL; block_stack* newstack = NULL; diff --git a/src/buffer.h b/src/buffer.h index 63d6202..9cfd5b9 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -9,12 +9,12 @@ typedef struct { unsigned char *ptr; int asize, size; -} strbuf; +} cmark_strbuf; -extern unsigned char strbuf__initbuf[]; -extern unsigned char strbuf__oom[]; +extern unsigned char cmark_strbuf__initbuf[]; +extern unsigned char cmark_strbuf__oom[]; -#define GH_BUF_INIT { strbuf__initbuf, 0, 0 } +#define CMARK_GH_BUF_INIT { cmark_strbuf__initbuf, 0, 0 } /** * Initialize a strbuf structure. @@ -22,7 +22,7 @@ extern unsigned char strbuf__oom[]; * For the cases where GH_BUF_INIT cannot be used to do static * initialization. */ -extern void strbuf_init(strbuf *buf, int initial_size); +extern void cmark_strbuf_init(cmark_strbuf *buf, int initial_size); /** * Attempt to grow the buffer to hold at least `target_size` bytes. @@ -32,7 +32,7 @@ extern void strbuf_init(strbuf *buf, int initial_size); * existing buffer content will be preserved, but calling code must handle * that buffer was not expanded. */ -extern int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom); +extern int cmark_strbuf_try_grow(cmark_strbuf *buf, int target_size, bool mark_oom); /** * Grow the buffer to hold at least `target_size` bytes. @@ -42,13 +42,13 @@ extern int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom); * * @return 0 on success or -1 on failure */ -static inline int strbuf_grow(strbuf *buf, int target_size) +static inline int cmark_strbuf_grow(cmark_strbuf *buf, int target_size) { - return strbuf_try_grow(buf, target_size, true); + return cmark_strbuf_try_grow(buf, target_size, true); } -extern void strbuf_free(strbuf *buf); -extern void strbuf_swap(strbuf *buf_a, strbuf *buf_b); +extern void cmark_strbuf_free(cmark_strbuf *buf); +extern void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b); /** * Test if there have been any reallocation failures with this strbuf. @@ -61,29 +61,29 @@ extern void strbuf_swap(strbuf *buf_a, strbuf *buf_b); * * @return false if no error, true if allocation error */ -static inline bool strbuf_oom(const strbuf *buf) +static inline bool cmark_strbuf_oom(const cmark_strbuf *buf) { - return (buf->ptr == strbuf__oom); + return (buf->ptr == cmark_strbuf__oom); } -static inline size_t strbuf_len(const strbuf *buf) +static inline size_t cmark_strbuf_len(const cmark_strbuf *buf) { return buf->size; } -extern int strbuf_cmp(const strbuf *a, const strbuf *b); +extern int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b); -extern void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize); -extern unsigned char *strbuf_detach(strbuf *buf); -extern void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf); +extern void cmark_strbuf_attach(cmark_strbuf *buf, unsigned char *ptr, int asize); +extern unsigned char *cmark_strbuf_detach(cmark_strbuf *buf); +extern void cmark_strbuf_copy_cstr(char *data, int datasize, const cmark_strbuf *buf); -static inline const char *strbuf_cstr(const strbuf *buf) +static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf) { return (char *)buf->ptr; } -#define strbuf_at(buf, n) ((buf)->ptr[n]) +#define cmark_strbuf_at(buf, n) ((buf)->ptr[n]) /* * Functions below that return int value error codes will return 0 on @@ -93,23 +93,59 @@ static inline const char *strbuf_cstr(const strbuf *buf) * return code of these functions and call them in a series then just call * strbuf_oom at the end. */ -extern int strbuf_set(strbuf *buf, const unsigned char *data, int len); -extern int strbuf_sets(strbuf *buf, const char *string); -extern int strbuf_putc(strbuf *buf, int c); -extern int strbuf_put(strbuf *buf, const unsigned char *data, int len); -extern int strbuf_puts(strbuf *buf, const char *string); -extern int strbuf_printf(strbuf *buf, const char *format, ...) +extern int cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, int len); +extern int cmark_strbuf_sets(cmark_strbuf *buf, const char *string); +extern int cmark_strbuf_putc(cmark_strbuf *buf, int c); +extern int cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, int len); +extern int cmark_strbuf_puts(cmark_strbuf *buf, const char *string); +extern int cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...) __attribute__((format (printf, 2, 3))); -extern int strbuf_vprintf(strbuf *buf, const char *format, va_list ap); -extern void strbuf_clear(strbuf *buf); - -int strbuf_strchr(const strbuf *buf, int c, int pos); -int strbuf_strrchr(const strbuf *buf, int c, int pos); -void strbuf_drop(strbuf *buf, int n); -void strbuf_truncate(strbuf *buf, int len); -void strbuf_rtrim(strbuf *buf); -void strbuf_trim(strbuf *buf); -void strbuf_normalize_whitespace(strbuf *s); -void strbuf_unescape(strbuf *s); +extern int cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap); +extern void cmark_strbuf_clear(cmark_strbuf *buf); + +int cmark_strbuf_strchr(const cmark_strbuf *buf, int c, int pos); +int cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, int pos); +void cmark_strbuf_drop(cmark_strbuf *buf, int n); +void cmark_strbuf_truncate(cmark_strbuf *buf, int len); +void cmark_strbuf_rtrim(cmark_strbuf *buf); +void cmark_strbuf_trim(cmark_strbuf *buf); +void cmark_strbuf_normalize_whitespace(cmark_strbuf *s); +void cmark_strbuf_unescape(cmark_strbuf *s); + +#ifndef CMARK_NO_SHORT_NAMES + #define strbuf cmark_strbuf + #define strbuf__initbuf cmark_strbuf__initbuf + #define strbuf__oom cmark_strbuf__oom + #define GH_BUF_INIT CMARK_GH_BUF_INIT + #define strbuf_init cmark_strbuf_init + #define strbuf_try_grow cmark_strbuf_try_grow + #define strbuf_grow cmark_strbuf_grow + #define strbuf_free cmark_strbuf_free + #define strbuf_swap cmark_strbuf_swap + #define strbuf_oom cmark_strbuf_oom + #define strbuf_len cmark_strbuf_len + #define strbuf_cmp cmark_strbuf_cmp + #define strbuf_attach cmark_strbuf_attach + #define strbuf_detach cmark_strbuf_detach + #define strbuf_copy_cstr cmark_strbuf_copy_cstr + #define strbuf_cstr cmark_strbuf_cstr + #define strbuf_at cmark_strbuf_at + #define strbuf_set cmark_strbuf_set + #define strbuf_sets cmark_strbuf_sets + #define strbuf_putc cmark_strbuf_putc + #define strbuf_put cmark_strbuf_put + #define strbuf_puts cmark_strbuf_puts + #define strbuf_printf cmark_strbuf_printf + #define strbuf_vprintf cmark_strbuf_vprintf + #define strbuf_clear cmark_strbuf_clear + #define strbuf_strchr cmark_strbuf_strchr + #define strbuf_strrchr cmark_strbuf_strrchr + #define strbuf_drop cmark_strbuf_drop + #define strbuf_truncate cmark_strbuf_truncate + #define strbuf_rtrim cmark_strbuf_rtrim + #define strbuf_trim cmark_strbuf_trim + #define strbuf_normalize_whitespace cmark_strbuf_normalize_whitespace + #define strbuf_unescape cmark_strbuf_unescape +#endif #endif diff --git a/src/chunk.h b/src/chunk.h index 015bbf9..e4a57b2 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -11,9 +11,9 @@ typedef struct { const unsigned char *data; int len; int alloc; -} chunk; +} cmark_chunk; -static inline void chunk_free(chunk *c) +static inline void cmark_chunk_free(cmark_chunk *c) { if (c->alloc) free((char *)c->data); @@ -23,7 +23,7 @@ static inline void chunk_free(chunk *c) c->len = 0; } -static inline void chunk_ltrim(chunk *c) +static inline void cmark_chunk_ltrim(cmark_chunk *c) { assert(!c->alloc); @@ -33,7 +33,7 @@ static inline void chunk_ltrim(chunk *c) } } -static inline void chunk_rtrim(chunk *c) +static inline void cmark_chunk_rtrim(cmark_chunk *c) { while (c->len > 0) { if (!isspace(c->data[c->len - 1])) @@ -43,19 +43,19 @@ static inline void chunk_rtrim(chunk *c) } } -static inline void chunk_trim(chunk *c) +static inline void cmark_chunk_trim(cmark_chunk *c) { - chunk_ltrim(c); - chunk_rtrim(c); + cmark_chunk_ltrim(c); + cmark_chunk_rtrim(c); } -static inline int chunk_strchr(chunk *ch, int c, int offset) +static inline int cmark_chunk_strchr(cmark_chunk *ch, int c, int offset) { const unsigned char *p = memchr(ch->data + offset, c, ch->len - offset); return p ? (int)(p - ch->data) : ch->len; } -static inline unsigned char *chunk_to_cstr(chunk *c) +static inline unsigned char *cmark_chunk_to_cstr(cmark_chunk *c) { unsigned char *str; @@ -67,27 +67,40 @@ static inline unsigned char *chunk_to_cstr(chunk *c) return str; } -static inline chunk chunk_literal(const char *data) +static inline cmark_chunk cmark_chunk_literal(const char *data) { - chunk c = {(const unsigned char *)data, data ? strlen(data) : 0, 0}; + cmark_chunk c = {(const unsigned char *)data, data ? strlen(data) : 0, 0}; return c; } -static inline chunk chunk_dup(const chunk *ch, int pos, int len) +static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, int pos, int len) { - chunk c = {ch->data + pos, len, 0}; + cmark_chunk c = {ch->data + pos, len, 0}; return c; } -static inline chunk chunk_buf_detach(strbuf *buf) +static inline cmark_chunk cmark_chunk_buf_detach(cmark_strbuf *buf) { - chunk c; + cmark_chunk c; c.len = buf->size; - c.data = strbuf_detach(buf); + c.data = cmark_strbuf_detach(buf); c.alloc = 1; return c; } +#ifndef CMARK_NO_SHORT_NAMES + #define chunk cmark_chunk + #define chunk_free cmark_chunk_free + #define chunk_ltrim cmark_chunk_ltrim + #define chunk_rtrim cmark_chunk_rtrim + #define chunk_trim cmark_chunk_trim + #define chunk_strchr cmark_chunk_strchr + #define chunk_to_cstr cmark_chunk_to_cstr + #define chunk_literal cmark_chunk_literal + #define chunk_dup cmark_chunk_dup + #define chunk_buf_detach cmark_chunk_buf_detach +#endif + #endif diff --git a/src/cmark.h b/src/cmark.h index 5f0d5f7..8b29934 100644 --- a/src/cmark.h +++ b/src/cmark.h @@ -11,39 +11,39 @@ extern "C" { #endif -#define VERSION "0.1" -#define CODE_INDENT 4 +#define CMARK_VERSION "0.1" +#define CMARK_CODE_INDENT 4 -#define MAX_LINK_LABEL_LENGTH 1000 +#define CMARK_MAX_LINK_LABEL_LENGTH 1000 -struct node_inl { +struct cmark_node_inl { enum { - INL_STRING, - INL_SOFTBREAK, - INL_LINEBREAK, - INL_CODE, - INL_RAW_HTML, - INL_EMPH, - INL_STRONG, - INL_LINK, - INL_IMAGE + CMARK_INL_STRING, + CMARK_INL_SOFTBREAK, + CMARK_INL_LINEBREAK, + CMARK_INL_CODE, + CMARK_INL_RAW_HTML, + CMARK_INL_EMPH, + CMARK_INL_STRONG, + CMARK_INL_LINK, + CMARK_INL_IMAGE } tag; union { - chunk literal; - struct node_inl *inlines; + cmark_chunk literal; + struct cmark_node_inl *inlines; struct { - struct node_inl *label; + struct cmark_node_inl *label; unsigned char *url; unsigned char *title; } linkable; } content; - struct node_inl *next; + struct cmark_node_inl *next; }; -typedef struct node_inl node_inl; +typedef struct cmark_node_inl cmark_node_inl; // Types for blocks -struct ListData { +struct cmark_ListData { enum { bullet, ordered @@ -59,67 +59,98 @@ struct ListData { bool tight; }; -struct FencedCodeData { +struct cmark_FencedCodeData { int fence_length; int fence_offset; unsigned char fence_char; - strbuf info; + cmark_strbuf info; }; -struct node_block { +struct cmark_node_block { enum { - BLOCK_DOCUMENT, - BLOCK_BQUOTE, - BLOCK_LIST, - BLOCK_LIST_ITEM, - BLOCK_FENCED_CODE, - BLOCK_INDENTED_CODE, - BLOCK_HTML, - BLOCK_PARAGRAPH, - BLOCK_ATX_HEADER, - BLOCK_SETEXT_HEADER, - BLOCK_HRULE, - BLOCK_REFERENCE_DEF + CMARK_BLOCK_DOCUMENT, + CMARK_BLOCK_BQUOTE, + CMARK_BLOCK_LIST, + CMARK_BLOCK_LIST_ITEM, + CMARK_BLOCK_FENCED_CODE, + CMARK_BLOCK_INDENTED_CODE, + CMARK_BLOCK_HTML, + CMARK_BLOCK_PARAGRAPH, + CMARK_BLOCK_ATX_HEADER, + CMARK_BLOCK_SETEXT_HEADER, + CMARK_BLOCK_HRULE, + CMARK_BLOCK_REFERENCE_DEF } tag; int start_line; int start_column; int end_line; bool open; bool last_line_blank; - struct node_block* children; - struct node_block* last_child; - struct node_block* parent; - struct node_block* top; - strbuf string_content; - node_inl* inline_content; + struct cmark_node_block* children; + struct cmark_node_block* last_child; + struct cmark_node_block* parent; + struct cmark_node_block* top; + cmark_strbuf string_content; + cmark_node_inl* inline_content; union { - struct ListData list; - struct FencedCodeData code; + struct cmark_ListData list; + struct cmark_FencedCodeData code; struct { int level; } header; struct { - reference_map *refmap; + cmark_reference_map *refmap; } document; } as; - struct node_block *next; - struct node_block *prev; + struct cmark_node_block *next; + struct cmark_node_block *prev; }; -typedef struct node_block node_block; +typedef struct cmark_node_block cmark_node_block; -node_block *cmark_parse_document(const unsigned char *buffer, size_t len); -node_block *cmark_parse_file(FILE *f); +cmark_node_block *cmark_parse_document(const unsigned char *buffer, size_t len); +cmark_node_block *cmark_parse_file(FILE *f); -void cmark_free_nodes(node_block *e); +void cmark_free_nodes(cmark_node_block *e); -void cmark_debug_print(node_block *root); -void cmark_render_html(strbuf *html, node_block *root); +void cmark_debug_print(cmark_node_block *root); +void cmark_render_html(cmark_strbuf *html, cmark_node_block *root); unsigned char *cmark_markdown_to_html(unsigned char *text, int len); +#ifndef CMARK_NO_SHORT_NAMES + #define VERSION CMARK_VERSION + #define CODE_INDENT CMARK_CODE_INDENT + #define MAX_LINK_LABEL_LENGTH CMARK_MAX_LINK_LABEL_LENGTH + #define node_inl cmark_node_inl + #define INL_STRING CMARK_INL_STRING + #define INL_SOFTBREAK CMARK_INL_SOFTBREAK + #define INL_LINEBREAK CMARK_INL_LINEBREAK + #define INL_CODE CMARK_INL_CODE + #define INL_RAW_HTML CMARK_INL_RAW_HTML + #define INL_EMPH CMARK_INL_EMPH + #define INL_STRONG CMARK_INL_STRONG + #define INL_LINK CMARK_INL_LINK + #define INL_IMAGE CMARK_INL_IMAGE + #define ListData cmark_ListData + #define FencedCodeData cmark_FencedCodeData + #define node_block cmark_node_block + #define BLOCK_DOCUMENT CMARK_BLOCK_DOCUMENT + #define BLOCK_BQUOTE CMARK_BLOCK_BQUOTE + #define BLOCK_LIST CMARK_BLOCK_LIST + #define BLOCK_LIST_ITEM CMARK_BLOCK_LIST_ITEM + #define BLOCK_FENCED_CODE CMARK_BLOCK_FENCED_CODE + #define BLOCK_INDENTED_CODE CMARK_BLOCK_INDENTED_CODE + #define BLOCK_HTML CMARK_BLOCK_HTML + #define BLOCK_PARAGRAPH CMARK_BLOCK_PARAGRAPH + #define BLOCK_ATX_HEADER CMARK_BLOCK_ATX_HEADER + #define BLOCK_SETEXT_HEADER CMARK_BLOCK_SETEXT_HEADER + #define BLOCK_HRULE CMARK_BLOCK_HRULE + #define BLOCK_REFERENCE_DEF CMARK_BLOCK_REFERENCE_DEF +#endif + #ifdef __cplusplus } #endif diff --git a/src/inlines.c b/src/inlines.c index 254ebb6..6a4e70c 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -112,7 +112,7 @@ inline static node_inl* make_simple(int t) #define make_strong(contents) make_inlines(INL_STRONG, contents) // Utility function used by free_inlines -void splice_into_list(node_inl* e, node_inl* children) { +static void splice_into_list(node_inl* e, node_inl* children) { node_inl * tmp; if (children) { tmp = children; @@ -948,7 +948,7 @@ extern node_inl* parse_inlines(strbuf *input, reference_map *refmap) } // Parse zero or more space characters, including at most one newline. -void spnl(subject* subj) +static void spnl(subject* subj) { bool seen_newline = false; while (peek_char(subj) == ' ' || diff --git a/src/inlines.h b/src/inlines.h index 8c6e2cb..6b3d4d4 100644 --- a/src/inlines.h +++ b/src/inlines.h @@ -1,13 +1,22 @@ #ifndef _INLINES_H_ #define _INLINES_H_ -unsigned char *clean_url(chunk *url); -unsigned char *clean_autolink(chunk *url, int is_email); -unsigned char *clean_title(chunk *title); +unsigned char *cmark_clean_url(cmark_chunk *url); +unsigned char *cmark_clean_autolink(cmark_chunk *url, int is_email); +unsigned char *cmark_clean_title(cmark_chunk *title); -node_inl* parse_inlines(strbuf *input, reference_map *refmap); -void free_inlines(node_inl* e); +cmark_node_inl* cmark_parse_inlines(cmark_strbuf *input, cmark_reference_map *refmap); +void cmark_free_inlines(cmark_node_inl* e); -int parse_reference_inline(strbuf *input, reference_map *refmap); +int cmark_parse_reference_inline(cmark_strbuf *input, cmark_reference_map *refmap); + +#ifndef CMARK_NO_SHORT_NAMES + #define clean_url cmark_clean_url + #define clean_autolink cmark_clean_autolink + #define clean_title cmark_clean_title + #define parse_inlines cmark_parse_inlines + #define free_inlines cmark_free_inlines + #define parse_reference_inline cmark_parse_reference_inline +#endif #endif diff --git a/src/references.h b/src/references.h index 28937f1..1bc187a 100644 --- a/src/references.h +++ b/src/references.h @@ -3,25 +3,34 @@ #define REFMAP_SIZE 16 -struct reference { - struct reference *next; +struct cmark_reference { + struct cmark_reference *next; unsigned char *label; unsigned char *url; unsigned char *title; unsigned int hash; }; -typedef struct reference reference; +typedef struct cmark_reference cmark_reference; -struct reference_map { - reference *table[REFMAP_SIZE]; +struct cmark_reference_map { + cmark_reference *table[REFMAP_SIZE]; }; -typedef struct reference_map reference_map; +typedef struct cmark_reference_map cmark_reference_map; -reference_map *reference_map_new(void); -void reference_map_free(reference_map *map); -reference* reference_lookup(reference_map *map, chunk *label); -extern void reference_create(reference_map *map, chunk *label, chunk *url, chunk *title); +cmark_reference_map *cmark_reference_map_new(void); +void cmark_reference_map_free(cmark_reference_map *map); +cmark_reference* cmark_reference_lookup(cmark_reference_map *map, cmark_chunk *label); +extern void cmark_reference_create(cmark_reference_map *map, cmark_chunk *label, cmark_chunk *url, cmark_chunk *title); + +#ifndef CMARK_NO_SHORT_NAMES + #define reference cmark_reference + #define reference_map cmark_reference_map + #define reference_map_new cmark_reference_map_new + #define reference_map_free cmark_reference_map_free + #define reference_lookup cmark_reference_lookup + #define reference_create cmark_reference_create +#endif #endif @@ -4,10 +4,10 @@ #include <stdint.h> #include "buffer.h" -void utf8proc_case_fold(strbuf *dest, const uint8_t *str, int len); -void utf8proc_encode_char(int32_t uc, strbuf *buf); +void utf8proc_case_fold(cmark_strbuf *dest, const uint8_t *str, int len); +void utf8proc_encode_char(int32_t uc, cmark_strbuf *buf); int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst); int utf8proc_charlen(const uint8_t *str, int str_len); -void utf8proc_detab(strbuf *dest, const uint8_t *line, size_t size); +void utf8proc_detab(cmark_strbuf *dest, const uint8_t *line, size_t size); #endif |