diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | LICENSE | 15 | ||||
-rw-r--r-- | Makefile | 29 | ||||
-rw-r--r-- | Makefile.win | 18 | ||||
-rw-r--r-- | README.md (renamed from README.markdown) | 0 | ||||
-rw-r--r-- | examples/hoedown.c (renamed from examples/sundown.c) | 28 | ||||
-rw-r--r-- | examples/smartypants.c | 16 | ||||
-rw-r--r-- | hoedown.def | 20 | ||||
-rw-r--r-- | html/houdini.h | 37 | ||||
-rw-r--r-- | html/houdini_html_e.c | 84 | ||||
-rw-r--r-- | html/html.h | 79 | ||||
-rw-r--r-- | src/autolink.c | 40 | ||||
-rw-r--r-- | src/autolink.h | 30 | ||||
-rw-r--r-- | src/buffer.c | 75 | ||||
-rw-r--r-- | src/buffer.h | 71 | ||||
-rw-r--r-- | src/escape.c (renamed from html/houdini_href_e.c) | 84 | ||||
-rw-r--r-- | src/escape.h | 17 | ||||
-rwxr-xr-x | src/html.c (renamed from html/html.c) | 253 | ||||
-rw-r--r-- | src/html.h | 63 | ||||
-rw-r--r-- | src/html_blocks.h | 4 | ||||
-rw-r--r-- | src/html_smartypants.c (renamed from html/html_smartypants.c) | 94 | ||||
-rw-r--r-- | src/markdown.c | 375 | ||||
-rw-r--r-- | src/markdown.h | 110 | ||||
-rw-r--r-- | src/stack.c | 16 | ||||
-rw-r--r-- | src/stack.h | 14 | ||||
-rw-r--r-- | sundown.def | 20 |
26 files changed, 714 insertions, 882 deletions
@@ -1,5 +1,5 @@ *.o -libsundown.so* -sundown +libhoedown.so* +hoedown smartypants *.exe @@ -0,0 +1,15 @@ +Copyright (c) 2008, Natacha Porté +Copyright (c) 2011, Vicent Martí +Copyright (c) 2013, Devin Torres + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. @@ -23,51 +23,50 @@ CFLAGS=-c -g -O3 -fPIC -Wall -Werror -Wsign-compare -Isrc -Ihtml LDFLAGS=-g -O3 -Wall -Werror -SUNDOWN_SRC=\ +HOEDOWN_SRC=\ src/markdown.o \ src/stack.o \ src/buffer.o \ src/autolink.o \ - html/html.o \ - html/html_smartypants.o \ - html/houdini_html_e.o \ - html/houdini_href_e.o + src/escape.o \ + src/html.o \ + src/html_smartypants.o \ -all: libsundown.so sundown smartypants html_blocks +all: libhoedown.so hoedown smartypants html_blocks .PHONY: all html_blocks test clean # libraries -libsundown.so: libsundown.so.1 +libhoedown.so: libhoedown.so.1 ln -f -s $^ $@ -libsundown.so.1: $(SUNDOWN_SRC) +libhoedown.so.1: $(HOEDOWN_SRC) $(CC) $(LDFLAGS) -shared $^ -o $@ # executables -sundown: examples/sundown.o $(SUNDOWN_SRC) +hoedown: examples/hoedown.o $(HOEDOWN_SRC) $(CC) $(LDFLAGS) $^ -o $@ -smartypants: examples/smartypants.o $(SUNDOWN_SRC) +smartypants: examples/smartypants.o $(HOEDOWN_SRC) $(CC) $(LDFLAGS) $^ -o $@ # perfect hashing html_blocks: src/html_blocks.h src/html_blocks.h: html_block_names.txt - gperf -N find_block_tag -H hash_block_tag -C -c -E -S 1 --ignore-case $^ > $@ + gperf -N hoedown_find_block_tag -H hash_block_tag -C -c -E -S 1 --ignore-case $^ > $@ -test: sundown +test: hoedown perl test/MarkdownTest_1.0.3/MarkdownTest.pl \ - --script=./sundown --testdir=test/MarkdownTest_1.0.3/Tests --tidy + --script=./hoedown --testdir=test/MarkdownTest_1.0.3/Tests --tidy # housekeeping clean: rm -f src/*.o html/*.o examples/*.o - rm -f libsundown.so libsundown.so.1 sundown smartypants - rm -f sundown.exe smartypants.exe + rm -f libhoedown.so libhoedown.so.1 hoedown smartypants + rm -f hoedown.exe smartypants.exe rm -rf $(DEPDIR) diff --git a/Makefile.win b/Makefile.win index ea668b2..a49d741 100644 --- a/Makefile.win +++ b/Makefile.win @@ -2,7 +2,7 @@ CFLAGS=/O2 /EHsc /I"src/" /I"examples"/ /I"html"/ CC=cl -SUNDOWN_SRC=\ +HOEDOWN_SRC=\ src\markdown.obj \ src\stack.obj \ src\buffer.obj \ @@ -12,19 +12,19 @@ SUNDOWN_SRC=\ html\houdini_html_e.obj \ html\houdini_href_e.obj -all: sundown.dll sundown.exe +all: hoedown.dll hoedown.exe -sundown.dll: $(SUNDOWN_SRC) sundown.def - $(CC) $(SUNDOWN_SRC) sundown.def /link /DLL $(LDFLAGS) /out:$@ +hoedown.dll: $(HOEDOWN_SRC) hoedown.def + $(CC) $(HOEDOWN_SRC) hoedown.def /link /DLL $(LDFLAGS) /out:$@ -sundown.exe: examples\sundown.obj $(SUNDOWN_SRC) - $(CC) examples\sundown.obj $(SUNDOWN_SRC) /link $(LDFLAGS) /out:$@ +hoedown.exe: examples\hoedown.obj $(HOEDOWN_SRC) + $(CC) examples\hoedown.obj $(HOEDOWN_SRC) /link $(LDFLAGS) /out:$@ # housekeeping clean: - del $(SUNDOWN_SRC) - del sundown.dll sundown.exe - del sundown.exp sundown.lib + del $(HOEDOWN_SRC) + del hoedown.dll hoedown.exe + del hoedown.exp hoedown.lib # generic object compilations diff --git a/README.markdown b/README.md index 39979eb..39979eb 100644 --- a/README.markdown +++ b/README.md diff --git a/examples/sundown.c b/examples/hoedown.c index 8a475dc..c413da4 100644 --- a/examples/sundown.c +++ b/examples/hoedown.c @@ -30,13 +30,13 @@ int main(int argc, char **argv) { - struct buf *ib, *ob; + struct hoedown_buffer *ib, *ob; int ret; FILE *in = stdin; - struct sd_callbacks callbacks; - struct html_renderopt options; - struct sd_markdown *markdown; + struct hoedown_callbacks callbacks; + struct hoedown_html_renderopt options; + struct hoedown_markdown *markdown; /* opening the file if given from the command line */ if (argc > 1) { @@ -48,31 +48,31 @@ main(int argc, char **argv) } /* reading everything */ - ib = bufnew(READ_UNIT); - bufgrow(ib, READ_UNIT); + ib = hoedown_buffer_new(READ_UNIT); + hoedown_buffer_grow(ib, READ_UNIT); while ((ret = fread(ib->data + ib->size, 1, ib->asize - ib->size, in)) > 0) { ib->size += ret; - bufgrow(ib, ib->size + READ_UNIT); + hoedown_buffer_grow(ib, ib->size + READ_UNIT); } if (in != stdin) fclose(in); /* performing markdown parsing */ - ob = bufnew(OUTPUT_UNIT); + ob = hoedown_buffer_new(OUTPUT_UNIT); - sdhtml_renderer(&callbacks, &options, 0); - markdown = sd_markdown_new(0, 16, &callbacks, &options); + hoedown_html_renderer(&callbacks, &options, 0); + markdown = hoedown_markdown_new(0, 16, &callbacks, &options); - sd_markdown_render(ob, ib->data, ib->size, markdown); - sd_markdown_free(markdown); + hoedown_markdown_render(ob, ib->data, ib->size, markdown); + hoedown_markdown_free(markdown); /* writing the result to stdout */ ret = fwrite(ob->data, 1, ob->size, stdout); /* cleanup */ - bufrelease(ib); - bufrelease(ob); + hoedown_buffer_release(ib); + hoedown_buffer_release(ob); return (ret < 0) ? -1 : 0; } diff --git a/examples/smartypants.c b/examples/smartypants.c index 4840703..8486795 100644 --- a/examples/smartypants.c +++ b/examples/smartypants.c @@ -30,7 +30,7 @@ int main(int argc, char **argv) { - struct buf *ib, *ob; + struct hoedown_buffer *ib, *ob; size_t ret; FILE *in = stdin; @@ -44,27 +44,27 @@ main(int argc, char **argv) } /* reading everything */ - ib = bufnew(READ_UNIT); - bufgrow(ib, READ_UNIT); + ib = hoedown_buffer_new(READ_UNIT); + hoedown_buffer_grow(ib, READ_UNIT); while ((ret = fread(ib->data + ib->size, 1, ib->asize - ib->size, in)) > 0) { ib->size += ret; - bufgrow(ib, ib->size + READ_UNIT); + hoedown_buffer_grow(ib, ib->size + READ_UNIT); } if (in != stdin) fclose(in); /* performing markdown parsing */ - ob = bufnew(OUTPUT_UNIT); + ob = hoedown_buffer_new(OUTPUT_UNIT); - sdhtml_smartypants(ob, ib->data, ib->size); + hoedown_html_smartypants(ob, ib->data, ib->size); /* writing the result to stdout */ (void)fwrite(ob->data, 1, ob->size, stdout); /* cleanup */ - bufrelease(ib); - bufrelease(ob); + hoedown_buffer_release(ib); + hoedown_buffer_release(ob); return 0; } diff --git a/hoedown.def b/hoedown.def new file mode 100644 index 0000000..cacc237 --- /dev/null +++ b/hoedown.def @@ -0,0 +1,20 @@ +LIBRARY HOEDOWN +EXPORTS + hoedown_html_renderer + hoedown_html_toc_renderer + hoedown_html_smartypants + hoedown_buffer_grow + hoedown_buffer_new + hoedown_buffer_cstr + hoedown_buffer_prefix + hoedown_buffer_put + hoedown_buffer_puts + hoedown_buffer_putc + hoedown_buffer_release + hoedown_buffer_reset + hoedown_buffer_slurp + hoedown_buffer_printf + hoedown_markdown_new + hoedown_markdown_render + hoedown_markdown_free + hoedown_version
\ No newline at end of file diff --git a/html/houdini.h b/html/houdini.h deleted file mode 100644 index b4954c0..0000000 --- a/html/houdini.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef HOUDINI_H__ -#define HOUDINI_H__ - -#include "buffer.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef HOUDINI_USE_LOCALE -# define _isxdigit(c) isxdigit(c) -# define _isdigit(c) isdigit(c) -#else -/* - * Helper _isdigit methods -- do not trust the current locale - * */ -# define _isxdigit(c) (strchr("0123456789ABCDEFabcdef", (c)) != NULL) -# define _isdigit(c) ((c) >= '0' && (c) <= '9') -#endif - -extern void houdini_escape_html(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure); -extern void houdini_unescape_html(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_xml(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_uri(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_url(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_unescape_uri(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_unescape_url(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_escape_js(struct buf *ob, const uint8_t *src, size_t size); -extern void houdini_unescape_js(struct buf *ob, const uint8_t *src, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/html/houdini_html_e.c b/html/houdini_html_e.c deleted file mode 100644 index d9bbf18..0000000 --- a/html/houdini_html_e.c +++ /dev/null @@ -1,84 +0,0 @@ -#include <assert.h> -#include <stdio.h> -#include <string.h> - -#include "houdini.h" - -#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10) /* this is very scientific, yes */ - -/** - * According to the OWASP rules: - * - * & --> & - * < --> < - * > --> > - * " --> " - * ' --> ' ' is not recommended - * / --> / forward slash is included as it helps end an HTML entity - * - */ -static const char HTML_ESCAPE_TABLE[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; - -static const char *HTML_ESCAPES[] = { - "", - """, - "&", - "'", - "/", - "<", - ">" -}; - -void -houdini_escape_html0(struct buf *ob, const uint8_t *src, size_t size, int secure) -{ - size_t i = 0, org, esc = 0; - - bufgrow(ob, ESCAPE_GROW_FACTOR(size)); - - while (i < size) { - org = i; - while (i < size && (esc = HTML_ESCAPE_TABLE[src[i]]) == 0) - i++; - - if (i > org) - bufput(ob, src + org, i - org); - - /* escaping */ - if (i >= size) - break; - - /* The forward slash is only escaped in secure mode */ - if (src[i] == '/' && !secure) { - bufputc(ob, '/'); - } else { - bufputs(ob, HTML_ESCAPES[esc]); - } - - i++; - } -} - -void -houdini_escape_html(struct buf *ob, const uint8_t *src, size_t size) -{ - houdini_escape_html0(ob, src, size, 1); -} - diff --git a/html/html.h b/html/html.h deleted file mode 100644 index 106e735..0000000 --- a/html/html.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef UPSKIRT_HTML_H -#define UPSKIRT_HTML_H - -#include "markdown.h" -#include "buffer.h" -#include <stdlib.h> - -#ifdef __cplusplus -extern "C" { -#endif - -struct html_renderopt { - struct { - int header_count; - int current_level; - int level_offset; - int nesting_level; - } toc_data; - - unsigned int flags; - - /* extra callbacks */ - void (*link_attributes)(struct buf *ob, const struct buf *url, void *self); -}; - -typedef enum { - HTML_SKIP_HTML = (1 << 0), - HTML_SKIP_STYLE = (1 << 1), - HTML_SKIP_IMAGES = (1 << 2), - HTML_SKIP_LINKS = (1 << 3), - HTML_EXPAND_TABS = (1 << 4), - HTML_SAFELINK = (1 << 5), - HTML_TOC = (1 << 6), - HTML_HARD_WRAP = (1 << 7), - HTML_USE_XHTML = (1 << 8), - HTML_ESCAPE = (1 << 9), - HTML_PRETTIFY = (1 << 10), -} html_render_mode; - -typedef enum { - HTML_TAG_NONE = 0, - HTML_TAG_OPEN, - HTML_TAG_CLOSE, -} html_tag; - -int -sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname); - -extern void -sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options_ptr, unsigned int render_flags); - -extern void -sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options_ptr, int nesting_level); - -extern void -sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/src/autolink.c b/src/autolink.c index 5e52519..e577359 100644 --- a/src/autolink.c +++ b/src/autolink.c @@ -1,19 +1,3 @@ -/* - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - #include "buffer.h" #include "autolink.h" @@ -27,7 +11,7 @@ #endif int -sd_autolink_issafe(const uint8_t *link, size_t link_len) +hoedown_autolink_issafe(const uint8_t *link, size_t link_len) { static const size_t valid_uris_count = 5; static const char *valid_uris[] = { @@ -159,9 +143,9 @@ check_domain(uint8_t *data, size_t size, int allow_short) } size_t -sd_autolink__www( +hoedown_autolink__www( size_t *rewind_p, - struct buf *link, + struct hoedown_buffer *link, uint8_t *data, size_t max_rewind, size_t size, @@ -188,16 +172,16 @@ sd_autolink__www( if (link_end == 0) return 0; - bufput(link, data, link_end); + hoedown_buffer_put(link, data, link_end); *rewind_p = 0; return (int)link_end; } size_t -sd_autolink__email( +hoedown_autolink__email( size_t *rewind_p, - struct buf *link, + struct hoedown_buffer *link, uint8_t *data, size_t max_rewind, size_t size, @@ -244,16 +228,16 @@ sd_autolink__email( if (link_end == 0) return 0; - bufput(link, data - rewind, link_end + rewind); + hoedown_buffer_put(link, data - rewind, link_end + rewind); *rewind_p = rewind; return link_end; } size_t -sd_autolink__url( +hoedown_autolink__url( size_t *rewind_p, - struct buf *link, + struct hoedown_buffer *link, uint8_t *data, size_t max_rewind, size_t size, @@ -267,7 +251,7 @@ sd_autolink__url( while (rewind < max_rewind && isalpha(data[-rewind - 1])) rewind++; - if (!sd_autolink_issafe(data - rewind, size + rewind)) + if (!hoedown_autolink_issafe(data - rewind, size + rewind)) return 0; link_end = strlen("://"); @@ -275,7 +259,7 @@ sd_autolink__url( domain_len = check_domain( data + link_end, size - link_end, - flags & SD_AUTOLINK_SHORT_DOMAINS); + flags & HOEDOWN_AUTOLINK_SHORT_DOMAINS); if (domain_len == 0) return 0; @@ -289,7 +273,7 @@ sd_autolink__url( if (link_end == 0) return 0; - bufput(link, data - rewind, link_end + rewind); + hoedown_buffer_put(link, data - rewind, link_end + rewind); *rewind_p = rewind; return link_end; diff --git a/src/autolink.h b/src/autolink.h index 65e0fe6..7cbeaef 100644 --- a/src/autolink.h +++ b/src/autolink.h @@ -1,21 +1,5 @@ -/* - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef UPSKIRT_AUTOLINK_H -#define UPSKIRT_AUTOLINK_H +#ifndef HOEDOWN_AUTOLINK_H +#define HOEDOWN_AUTOLINK_H #include "buffer.h" @@ -24,22 +8,22 @@ extern "C" { #endif enum { - SD_AUTOLINK_SHORT_DOMAINS = (1 << 0), + HOEDOWN_AUTOLINK_SHORT_DOMAINS = (1 << 0), }; int -sd_autolink_issafe(const uint8_t *link, size_t link_len); +hoedown_autolink_issafe(const uint8_t *link, size_t link_len); size_t -sd_autolink__www(size_t *rewind_p, struct buf *link, +hoedown_autolink__www(size_t *rewind_p, struct hoedown_buffer *link, uint8_t *data, size_t offset, size_t size, unsigned int flags); size_t -sd_autolink__email(size_t *rewind_p, struct buf *link, +hoedown_autolink__email(size_t *rewind_p, struct hoedown_buffer *link, uint8_t *data, size_t offset, size_t size, unsigned int flags); size_t -sd_autolink__url(size_t *rewind_p, struct buf *link, +hoedown_autolink__url(size_t *rewind_p, struct hoedown_buffer *link, uint8_t *data, size_t offset, size_t size, unsigned int flags); #ifdef __cplusplus diff --git a/src/buffer.c b/src/buffer.c index 47b40ce..e2324f3 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,20 +1,3 @@ -/* - * Copyright (c) 2008, Natacha Porté - * Copyright (c) 2011, Vicent Martí - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - #define BUFFER_MAX_ALLOC_SIZE (1024 * 1024 * 16) //16mb #include "buffer.h" @@ -32,7 +15,7 @@ #endif int -bufprefix(const struct buf *buf, const char *prefix) +hoedown_buffer_prefix(const struct hoedown_buffer *buf, const char *prefix) { size_t i; assert(buf && buf->unit); @@ -48,9 +31,9 @@ bufprefix(const struct buf *buf, const char *prefix) return 0; } -/* bufgrow: increasing the allocated size to the given value */ +/* hoedown_buffer_grow: increasing the allocated size to the given value */ int -bufgrow(struct buf *buf, size_t neosz) +hoedown_buffer_grow(struct hoedown_buffer *buf, size_t neosz) { size_t neoasz; void *neodata; @@ -77,12 +60,12 @@ bufgrow(struct buf *buf, size_t neosz) } -/* bufnew: allocation of a new buffer */ -struct buf * -bufnew(size_t unit) +/* hoedown_buffer_new: allocation of a new buffer */ +struct hoedown_buffer * +hoedown_buffer_new(size_t unit) { - struct buf *ret; - ret = malloc(sizeof (struct buf)); + struct hoedown_buffer *ret; + ret = malloc(sizeof (struct hoedown_buffer)); if (ret) { ret->data = 0; @@ -94,14 +77,14 @@ bufnew(size_t unit) /* bufnullterm: NULL-termination of the string array */ const char * -bufcstr(struct buf *buf) +hoedown_buffer_cstr(struct hoedown_buffer *buf) { assert(buf && buf->unit); if (buf->size < buf->asize && buf->data[buf->size] == 0) return (char *)buf->data; - if (buf->size + 1 <= buf->asize || bufgrow(buf, buf->size + 1) == 0) { + if (buf->size + 1 <= buf->asize || hoedown_buffer_grow(buf, buf->size + 1) == 0) { buf->data[buf->size] = 0; return (char *)buf->data; } @@ -109,16 +92,16 @@ bufcstr(struct buf *buf) return NULL; } -/* bufprintf: formatted printing to a buffer */ +/* hoedown_buffer_printf: formatted printing to a buffer */ void -bufprintf(struct buf *buf, const char *fmt, ...) +hoedown_buffer_printf(struct hoedown_buffer *buf, const char *fmt, ...) { va_list ap; int n; assert(buf && buf->unit); - if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0) + if (buf->size >= buf->asize && hoedown_buffer_grow(buf, buf->size + 1) < 0) return; va_start(ap, fmt); @@ -136,7 +119,7 @@ bufprintf(struct buf *buf, const char *fmt, ...) } if ((size_t)n >= buf->asize - buf->size) { - if (bufgrow(buf, buf->size + n + 1) < 0) + if (hoedown_buffer_grow(buf, buf->size + n + 1) < 0) return; va_start(ap, fmt); @@ -150,43 +133,43 @@ bufprintf(struct buf *buf, const char *fmt, ...) buf->size += n; } -/* bufput: appends raw data to a buffer */ +/* hoedown_buffer_put: appends raw data to a buffer */ void -bufput(struct buf *buf, const void *data, size_t len) +hoedown_buffer_put(struct hoedown_buffer *buf, const void *data, size_t len) { assert(buf && buf->unit); - if (buf->size + len > buf->asize && bufgrow(buf, buf->size + len) < 0) + if (buf->size + len > buf->asize && hoedown_buffer_grow(buf, buf->size + len) < 0) return; memcpy(buf->data + buf->size, data, len); buf->size += len; } -/* bufputs: appends a NUL-terminated string to a buffer */ +/* hoedown_buffer_puts: appends a NUL-terminated string to a buffer */ void -bufputs(struct buf *buf, const char *str) +hoedown_buffer_puts(struct hoedown_buffer *buf, const char *str) { - bufput(buf, str, strlen(str)); + hoedown_buffer_put(buf, str, strlen(str)); } -/* bufputc: appends a single uint8_t to a buffer */ +/* hoedown_buffer_putc: appends a single uint8_t to a buffer */ void -bufputc(struct buf *buf, int c) +hoedown_buffer_putc(struct hoedown_buffer *buf, int c) { assert(buf && buf->unit); - if (buf->size + 1 > buf->asize && bufgrow(buf, buf->size + 1) < 0) + if (buf->size + 1 > buf->asize && hoedown_buffer_grow(buf, buf->size + 1) < 0) return; buf->data[buf->size] = c; buf->size += 1; } -/* bufrelease: decrease the reference count and free the buffer if needed */ +/* hoedown_buffer_release: decrease the reference count and free the buffer if needed */ void -bufrelease(struct buf *buf) +hoedown_buffer_release(struct hoedown_buffer *buf) { if (!buf) return; @@ -196,9 +179,9 @@ bufrelease(struct buf *buf) } -/* bufreset: frees internal data of the buffer */ +/* hoedown_buffer_reset: frees internal data of the buffer */ void -bufreset(struct buf *buf) +hoedown_buffer_reset(struct hoedown_buffer *buf) { if (!buf) return; @@ -208,9 +191,9 @@ bufreset(struct buf *buf) buf->size = buf->asize = 0; } -/* bufslurp: removes a given number of bytes from the head of the array */ +/* hoedown_buffer_slurp: removes a given number of bytes from the head of the array */ void -bufslurp(struct buf *buf, size_t len) +hoedown_buffer_slurp(struct hoedown_buffer *buf, size_t len) { assert(buf && buf->unit); diff --git a/src/buffer.h b/src/buffer.h index df7ebf9..fda90a8 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -1,22 +1,5 @@ -/* - * Copyright (c) 2008, Natacha Porté - * Copyright (c) 2011, Vicent Martí - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef BUFFER_H__ -#define BUFFER_H__ +#ifndef HOEDOWN_BUFFER_H +#define HOEDOWN_BUFFER_H #include <stddef.h> #include <stdarg.h> @@ -36,50 +19,50 @@ typedef enum { BUF_ENOMEM = -1, } buferror_t; -/* struct buf: character array buffer */ -struct buf { +/* struct hoedown_buffer: character array buffer */ +struct hoedown_buffer { uint8_t *data; /* actual character data */ size_t size; /* size of the string */ size_t asize; /* allocated size (0 = volatile buffer) */ size_t unit; /* reallocation unit size (0 = read-only buffer) */ }; -/* BUFPUTSL: optimized bufputs of a string literal */ +/* BUFPUTSL: optimized hoedown_buffer_puts of a string literal */ #define BUFPUTSL(output, literal) \ - bufput(output, literal, sizeof(literal) - 1) + hoedown_buffer_put(output, literal, sizeof(literal) - 1) -/* bufgrow: increasing the allocated size to the given value */ -int bufgrow(struct buf *, size_t); +/* hoedown_buffer_grow: increasing the allocated size to the given value */ +int hoedown_buffer_grow(struct hoedown_buffer *, size_t); -/* bufnew: allocation of a new buffer */ -struct buf *bufnew(size_t) __attribute__ ((malloc)); +/* hoedown_buffer_new: allocation of a new buffer */ +struct hoedown_buffer *hoedown_buffer_new(size_t) __attribute__ ((malloc)); /* bufnullterm: NUL-termination of the string array (making a C-string) */ -const char *bufcstr(struct buf *); +const char *hoedown_buffer_cstr(struct hoedown_buffer *); -/* bufprefix: compare the beginning of a buffer with a string */ -int bufprefix(const struct buf *buf, const char *prefix); +/* hoedown_buffer_prefix: compare the beginning of a buffer with a string */ +int hoedown_buffer_prefix(const struct hoedown_buffer *buf, const char *prefix); -/* bufput: appends raw data to a buffer */ -void bufput(struct buf *, const void *, size_t); +/* hoedown_buffer_put: appends raw data to a buffer */ +void hoedown_buffer_put(struct hoedown_buffer *, const void *, size_t); -/* bufputs: appends a NUL-terminated string to a buffer */ -void bufputs(struct buf *, const char *); +/* hoedown_buffer_puts: appends a NUL-terminated string to a buffer */ +void hoedown_buffer_puts(struct hoedown_buffer *, const char *); -/* bufputc: appends a single char to a buffer */ -void bufputc(struct buf *, int); +/* hoedown_buffer_putc: appends a single char to a buffer */ +void hoedown_buffer_putc(struct hoedown_buffer *, int); -/* bufrelease: decrease the reference count and free the buffer if needed */ -void bufrelease(struct buf *); +/* hoedown_buffer_release: decrease the reference count and free the buffer if needed */ +void hoedown_buffer_release(struct hoedown_buffer *); -/* bufreset: frees internal data of the buffer */ -void bufreset(struct buf *); +/* hoedown_buffer_reset: frees internal data of the buffer */ +void hoedown_buffer_reset(struct hoedown_buffer *); -/* bufslurp: removes a given number of bytes from the head of the array */ -void bufslurp(struct buf *, size_t); +/* hoedown_buffer_slurp: removes a given number of bytes from the head of the array */ +void hoedown_buffer_slurp(struct hoedown_buffer *, size_t); -/* bufprintf: formatted printing to a buffer */ -void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3))); +/* hoedown_buffer_printf: formatted printing to a buffer */ +void hoedown_buffer_printf(struct hoedown_buffer *, const char *, ...) __attribute__ ((format (printf, 2, 3))); #ifdef __cplusplus } diff --git a/html/houdini_href_e.c b/src/escape.c index 981b3b1..77401c2 100644 --- a/html/houdini_href_e.c +++ b/src/escape.c @@ -2,9 +2,9 @@ #include <stdio.h> #include <string.h> -#include "houdini.h" +#include "escape.h" -#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10) +#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10) /* this is very scientific, yes */ /* * The following characters will not be escaped: @@ -51,13 +51,13 @@ static const char HREF_SAFE[] = { }; void -houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size) +hoedown_escape_href(struct hoedown_buffer *ob, const uint8_t *src, size_t size) { static const char hex_chars[] = "0123456789ABCDEF"; size_t i = 0, org; char hex_str[3]; - bufgrow(ob, ESCAPE_GROW_FACTOR(size)); + hoedown_buffer_grow(ob, ESCAPE_GROW_FACTOR(size)); hex_str[0] = '%'; while (i < size) { @@ -66,7 +66,7 @@ houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size) i++; if (i > org) - bufput(ob, src + org, i - org); + hoedown_buffer_put(ob, src + org, i - org); /* escaping */ if (i >= size) @@ -92,7 +92,7 @@ houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size) * when building GET strings */ #if 0 case ' ': - bufputc(ob, '+'); + hoedown_buffer_putc(ob, '+'); break; #endif @@ -100,7 +100,77 @@ houdini_escape_href(struct buf *ob, const uint8_t *src, size_t size) default: hex_str[1] = hex_chars[(src[i] >> 4) & 0xF]; hex_str[2] = hex_chars[src[i] & 0xF]; - bufput(ob, hex_str, 3); + hoedown_buffer_put(ob, hex_str, 3); + } + + i++; + } +} + +/** + * According to the OWASP rules: + * + * & --> & + * < --> < + * > --> > + * " --> " + * ' --> ' ' is not recommended + * / --> / forward slash is included as it helps end an HTML entity + * + */ +static const char HTML_ESCAPE_TABLE[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +static const char *HTML_ESCAPES[] = { + "", + """, + "&", + "'", + "/", + "<", + ">" +}; + +void +hoedown_escape_html(struct hoedown_buffer *ob, const uint8_t *src, size_t size, int secure) +{ + size_t i = 0, org, esc = 0; + + hoedown_buffer_grow(ob, ESCAPE_GROW_FACTOR(size)); + + while (i < size) { + org = i; + while (i < size && (esc = HTML_ESCAPE_TABLE[src[i]]) == 0) + i++; + + if (i > org) + hoedown_buffer_put(ob, src + org, i - org); + + /* escaping */ + if (i >= size) + break; + + /* The forward slash is only escaped in secure mode */ + if (src[i] == '/' && !secure) { + hoedown_buffer_putc(ob, '/'); + } else { + hoedown_buffer_puts(ob, HTML_ESCAPES[esc]); } i++; diff --git a/src/escape.h b/src/escape.h new file mode 100644 index 0000000..de08a1a --- /dev/null +++ b/src/escape.h @@ -0,0 +1,17 @@ +#ifndef HOEDOWN_ESCAPE_H +#define HOEDOWN_ESCAPE_H + +#include "buffer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern void hoedown_escape_html(struct hoedown_buffer *ob, const uint8_t *src, size_t size, int secure); +extern void hoedown_escape_href(struct hoedown_buffer *ob, const uint8_t *src, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif @@ -1,20 +1,3 @@ -/* - * Copyright (c) 2009, Natacha Porté - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - #include "markdown.h" #include "html.h" @@ -23,12 +6,12 @@ #include <stdio.h> #include <ctype.h> -#include "houdini.h" +#include "escape.h" #define USE_XHTML(opt) (opt->flags & HTML_USE_XHTML) int -sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname) +hoedown_html_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname) { size_t i; int closed = 0; @@ -60,29 +43,29 @@ sdhtml_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname) return HTML_TAG_NONE; } -static inline void escape_html(struct buf *ob, const uint8_t *source, size_t length) +static inline void escape_html(struct hoedown_buffer *ob, const uint8_t *source, size_t length) { - houdini_escape_html0(ob, source, length, 0); + hoedown_escape_html(ob, source, length, 0); } -static inline void escape_href(struct buf *ob, const uint8_t *source, size_t length) +static inline void escape_href(struct hoedown_buffer *ob, const uint8_t *source, size_t length) { - houdini_escape_href(ob, source, length); + hoedown_escape_href(ob, source, length); } /******************** * GENERIC RENDERER * ********************/ static int -rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque) +rndr_autolink(struct hoedown_buffer *ob, const struct hoedown_buffer *link, enum mkd_autolink type, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; if (!link || !link->size) return 0; if ((options->flags & HTML_SAFELINK) != 0 && - !sd_autolink_issafe(link->data, link->size) && + !hoedown_autolink_issafe(link->data, link->size) && type != MKDA_EMAIL) return 0; @@ -92,9 +75,9 @@ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, vo escape_href(ob, link->data, link->size); if (options->link_attributes) { - bufputc(ob, '\"'); + hoedown_buffer_putc(ob, '\"'); options->link_attributes(ob, link, opaque); - bufputc(ob, '>'); + hoedown_buffer_putc(ob, '>'); } else { BUFPUTSL(ob, "\">"); } @@ -104,7 +87,7 @@ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, vo * an actual URI, e.g. `mailto:foo@bar.com`, we don't * want to print the `mailto:` prefix */ - if (bufprefix(link, "mailto:") == 0) { + if (hoedown_buffer_prefix(link, "mailto:") == 0) { escape_html(ob, link->data + 7, link->size - 7); } else { escape_html(ob, link->data, link->size); @@ -116,11 +99,11 @@ rndr_autolink(struct buf *ob, const struct buf *link, enum mkd_autolink type, vo } static void -rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque) +rndr_blockcode(struct hoedown_buffer *ob, const struct hoedown_buffer *text, const struct hoedown_buffer *lang, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; - if (ob->size) bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); if (lang && lang->size) { size_t i, cls = 0; @@ -143,7 +126,7 @@ rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, v if (lang->data[org] == '.') org++; - if (cls) bufputc(ob, ' '); + if (cls) hoedown_buffer_putc(ob, ' '); escape_html(ob, lang->data + org, i - org); } } @@ -162,18 +145,18 @@ rndr_blockcode(struct buf *ob, const struct buf *text, const struct buf *lang, v } static void -rndr_blockquote(struct buf *ob, const struct buf *text, void *opaque) +rndr_blockquote(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { - if (ob->size) bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); BUFPUTSL(ob, "<blockquote>\n"); - if (text) bufput(ob, text->data, text->size); + if (text) hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</blockquote>\n"); } static int -rndr_codespan(struct buf *ob, const struct buf *text, void *opaque) +rndr_codespan(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; if (options->flags & HTML_PRETTIFY) BUFPUTSL(ob, "<code class=\"prettyprint\">"); else @@ -184,110 +167,110 @@ rndr_codespan(struct buf *ob, const struct buf *text, void *opaque) } static int -rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque) +rndr_strikethrough(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<del>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</del>"); return 1; } static int -rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque) +rndr_double_emphasis(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<strong>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</strong>"); return 1; } static int -rndr_emphasis(struct buf *ob, const struct buf *text, void *opaque) +rndr_emphasis(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<em>"); - if (text) bufput(ob, text->data, text->size); + if (text) hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</em>"); return 1; } static int -rndr_underline(struct buf *ob, const struct buf *text, void *opaque) +rndr_underline(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<u>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</u>"); return 1; } static int -rndr_highlight(struct buf *ob, const struct buf *text, void *opaque) +rndr_highlight(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<mark>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</mark>"); return 1; } static int -rndr_quote(struct buf *ob, const struct buf *text, void *opaque) +rndr_quote(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<q>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</q>"); return 1; } static int -rndr_linebreak(struct buf *ob, void *opaque) +rndr_linebreak(struct hoedown_buffer *ob, void *opaque) { - struct html_renderopt *options = opaque; - bufputs(ob, USE_XHTML(options) ? "<br/>\n" : "<br>\n"); + struct hoedown_html_renderopt *options = opaque; + hoedown_buffer_puts(ob, USE_XHTML(options) ? "<br/>\n" : "<br>\n"); return 1; } static void -rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque) +rndr_header(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int level, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; if (ob->size) - bufputc(ob, '\n'); + hoedown_buffer_putc(ob, '\n'); if ((options->flags & HTML_TOC) && (level <= options->toc_data.nesting_level)) - bufprintf(ob, "<h%d id=\"toc_%d\">", level, options->toc_data.header_count++); + hoedown_buffer_printf(ob, "<h%d id=\"toc_%d\">", level, options->toc_data.header_count++); else - bufprintf(ob, "<h%d>", level); + hoedown_buffer_printf(ob, "<h%d>", level); - if (text) bufput(ob, text->data, text->size); - bufprintf(ob, "</h%d>\n", level); + if (text) hoedown_buffer_put(ob, text->data, text->size); + hoedown_buffer_printf(ob, "</h%d>\n", level); } static int -rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque) +rndr_link(struct hoedown_buffer *ob, const struct hoedown_buffer *link, const struct hoedown_buffer *title, const struct hoedown_buffer *content, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; - if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !sd_autolink_issafe(link->data, link->size)) + if (link != NULL && (options->flags & HTML_SAFELINK) != 0 && !hoedown_autolink_issafe(link->data, link->size)) return 0; BUFPUTSL(ob, "<a href=\""); @@ -301,29 +284,29 @@ rndr_link(struct buf *ob, const struct buf *link, const struct buf *title, const } if (options->link_attributes) { - bufputc(ob, '\"'); + hoedown_buffer_putc(ob, '\"'); options->link_attributes(ob, link, opaque); - bufputc(ob, '>'); + hoedown_buffer_putc(ob, '>'); } else { BUFPUTSL(ob, "\">"); } - if (content && content->size) bufput(ob, content->data, content->size); + if (content && content->size) hoedown_buffer_put(ob, content->data, content->size); BUFPUTSL(ob, "</a>"); return 1; } static void -rndr_list(struct buf *ob, const struct buf *text, int flags, void *opaque) +rndr_list(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque) { - if (ob->size) bufputc(ob, '\n'); - bufput(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5); - if (text) bufput(ob, text->data, text->size); - bufput(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6); + if (ob->size) hoedown_buffer_putc(ob, '\n'); + hoedown_buffer_put(ob, flags & MKD_LIST_ORDERED ? "<ol>\n" : "<ul>\n", 5); + if (text) hoedown_buffer_put(ob, text->data, text->size); + hoedown_buffer_put(ob, flags & MKD_LIST_ORDERED ? "</ol>\n" : "</ul>\n", 6); } static void -rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque) +rndr_listitem(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque) { BUFPUTSL(ob, "<li>"); if (text) { @@ -331,18 +314,18 @@ rndr_listitem(struct buf *ob, const struct buf *text, int flags, void *opaque) while (size && text->data[size - 1] == '\n') size--; - bufput(ob, text->data, size); + hoedown_buffer_put(ob, text->data, size); } BUFPUTSL(ob, "</li>\n"); } static void -rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque) +rndr_paragraph(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; size_t i = 0; - if (ob->size) bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); if (!text || !text->size) return; @@ -361,7 +344,7 @@ rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque) i++; if (i > org) - bufput(ob, text->data + org, i - org); + hoedown_buffer_put(ob, text->data + org, i - org); /* * do not insert a line break if this newline @@ -374,13 +357,13 @@ rndr_paragraph(struct buf *ob, const struct buf *text, void *opaque) i++; } } else { - bufput(ob, &text->data[i], text->size - i); + hoedown_buffer_put(ob, &text->data[i], text->size - i); } BUFPUTSL(ob, "</p>\n"); } static void -rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque) +rndr_raw_block(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { size_t org, sz; if (!text) return; @@ -389,33 +372,33 @@ rndr_raw_block(struct buf *ob, const struct buf *text, void *opaque) org = 0; while (org < sz && text->data[org] == '\n') org++; if (org >= sz) return; - if (ob->size) bufputc(ob, '\n'); - bufput(ob, text->data + org, sz - org); - bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); + hoedown_buffer_put(ob, text->data + org, sz - org); + hoedown_buffer_putc(ob, '\n'); } static int -rndr_triple_emphasis(struct buf *ob, const struct buf *text, void *opaque) +rndr_triple_emphasis(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<strong><em>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</em></strong>"); return 1; } static void -rndr_hrule(struct buf *ob, void *opaque) +rndr_hrule(struct hoedown_buffer *ob, void *opaque) { - struct html_renderopt *options = opaque; - if (ob->size) bufputc(ob, '\n'); - bufputs(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n"); + struct hoedown_html_renderopt *options = opaque; + if (ob->size) hoedown_buffer_putc(ob, '\n'); + hoedown_buffer_puts(ob, USE_XHTML(options) ? "<hr/>\n" : "<hr>\n"); } static int -rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque) +rndr_image(struct hoedown_buffer *ob, const struct hoedown_buffer *link, const struct hoedown_buffer *title, const struct hoedown_buffer *alt, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; if (!link || !link->size) return 0; BUFPUTSL(ob, "<img src=\""); @@ -429,14 +412,14 @@ rndr_image(struct buf *ob, const struct buf *link, const struct buf *title, cons BUFPUTSL(ob, "\" title=\""); escape_html(ob, title->data, title->size); } - bufputs(ob, USE_XHTML(options) ? "\"/>" : "\">"); + hoedown_buffer_puts(ob, USE_XHTML(options) ? "\"/>" : "\">"); return 1; } static int -rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque) +rndr_raw_html(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; /* HTML_ESCAPE overrides SKIP_HTML, SKIP_STYLE, SKIP_LINKS and SKIP_IMAGES * It doens't see if there are any valid tags, just escape all of them. */ @@ -449,45 +432,45 @@ rndr_raw_html(struct buf *ob, const struct buf *text, void *opaque) return 1; if ((options->flags & HTML_SKIP_STYLE) != 0 && - sdhtml_is_tag(text->data, text->size, "style")) + hoedown_html_is_tag(text->data, text->size, "style")) return 1; if ((options->flags & HTML_SKIP_LINKS) != 0 && - sdhtml_is_tag(text->data, text->size, "a")) + hoedown_html_is_tag(text->data, text->size, "a")) return 1; if ((options->flags & HTML_SKIP_IMAGES) != 0 && - sdhtml_is_tag(text->data, text->size, "img")) + hoedown_html_is_tag(text->data, text->size, "img")) return 1; - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); return 1; } static void -rndr_table(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque) +rndr_table(struct hoedown_buffer *ob, const struct hoedown_buffer *header, const struct hoedown_buffer *body, void *opaque) { - if (ob->size) bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); BUFPUTSL(ob, "<table><thead>\n"); if (header) - bufput(ob, header->data, header->size); + hoedown_buffer_put(ob, header->data, header->size); BUFPUTSL(ob, "</thead><tbody>\n"); if (body) - bufput(ob, body->data, body->size); + hoedown_buffer_put(ob, body->data, body->size); BUFPUTSL(ob, "</tbody></table>\n"); } static void -rndr_tablerow(struct buf *ob, const struct buf *text, void *opaque) +rndr_tablerow(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { BUFPUTSL(ob, "<tr>\n"); if (text) - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</tr>\n"); } static void -rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque) +rndr_tablecell(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque) { if (flags & MKD_TABLE_HEADER) { BUFPUTSL(ob, "<th"); @@ -513,7 +496,7 @@ rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque) } if (text) - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); if (flags & MKD_TABLE_HEADER) { BUFPUTSL(ob, "</th>\n"); @@ -523,40 +506,40 @@ rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque) } static int -rndr_superscript(struct buf *ob, const struct buf *text, void *opaque) +rndr_superscript(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (!text || !text->size) return 0; BUFPUTSL(ob, "<sup>"); - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "</sup>"); return 1; } static void -rndr_normal_text(struct buf *ob, const struct buf *text, void *opaque) +rndr_normal_text(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { if (text) escape_html(ob, text->data, text->size); } static void -rndr_footnotes(struct buf *ob, const struct buf *text, void *opaque) +rndr_footnotes(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; - if (ob->size) bufputc(ob, '\n'); + if (ob->size) hoedown_buffer_putc(ob, '\n'); BUFPUTSL(ob, "<div class=\"footnotes\">\n"); BUFPUTSL(ob, (USE_XHTML(options) ? "<hr/>\n" : "<hr>\n")); BUFPUTSL(ob, "<ol>\n"); if (text) - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); BUFPUTSL(ob, "\n</ol>\n</div>\n"); } static void -rndr_footnote_def(struct buf *ob, const struct buf *text, unsigned int num, void *opaque) +rndr_footnote_def(struct hoedown_buffer *ob, const struct hoedown_buffer *text, unsigned int num, void *opaque) { size_t i = 0; int pfound = 0; @@ -574,28 +557,28 @@ rndr_footnote_def(struct buf *ob, const struct buf *text, unsigned int num, void } } - bufprintf(ob, "\n<li id=\"fn%d\">\n", num); + hoedown_buffer_printf(ob, "\n<li id=\"fn%d\">\n", num); if (pfound) { - bufput(ob, text->data, i); - bufprintf(ob, " <a href=\"#fnref%d\" rev=\"footnote\">↩</a>", num); - bufput(ob, text->data + i, text->size - i); + hoedown_buffer_put(ob, text->data, i); + hoedown_buffer_printf(ob, " <a href=\"#fnref%d\" rev=\"footnote\">↩</a>", num); + hoedown_buffer_put(ob, text->data + i, text->size - i); } else if (text) { - bufput(ob, text->data, text->size); + hoedown_buffer_put(ob, text->data, text->size); } BUFPUTSL(ob, "</li>\n"); } static int -rndr_footnote_ref(struct buf *ob, unsigned int num, void *opaque) +rndr_footnote_ref(struct hoedown_buffer *ob, unsigned int num, void *opaque) { - bufprintf(ob, "<sup id=\"fnref%d\"><a href=\"#fn%d\" rel=\"footnote\">%d</a></sup>", num, num, num); + hoedown_buffer_printf(ob, "<sup id=\"fnref%d\"><a href=\"#fn%d\" rel=\"footnote\">%d</a></sup>", num, num, num); return 1; } static void -toc_header(struct buf *ob, const struct buf *text, int level, void *opaque) +toc_header(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int level, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; if (level <= options->toc_data.nesting_level) { /* set the level offset if this is the first header @@ -621,24 +604,24 @@ toc_header(struct buf *ob, const struct buf *text, int level, void *opaque) BUFPUTSL(ob,"</li>\n<li>\n"); } - bufprintf(ob, "<a href=\"#toc_%d\">", options->toc_data.header_count++); + hoedown_buffer_printf(ob, "<a href=\"#toc_%d\">", options->toc_data.header_count++); if (text) escape_html(ob, text->data, text->size); BUFPUTSL(ob, "</a>\n"); } } static int -toc_link(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque) +toc_link(struct hoedown_buffer *ob, const struct hoedown_buffer *link, const struct hoedown_buffer *title, const struct hoedown_buffer *content, void *opaque) { if (content && content->size) - bufput(ob, content->data, content->size); + hoedown_buffer_put(ob, content->data, content->size); return 1; } static void -toc_finalize(struct buf *ob, void *opaque) +toc_finalize(struct hoedown_buffer *ob, void *opaque) { - struct html_renderopt *options = opaque; + struct hoedown_html_renderopt *options = opaque; while (options->toc_data.current_level > 0) { BUFPUTSL(ob, "</li>\n</ul>\n"); @@ -647,9 +630,9 @@ toc_finalize(struct buf *ob, void *opaque) } void -sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, int nesting_level) +hoedown_html_toc_renderer(struct hoedown_callbacks *callbacks, struct hoedown_html_renderopt *options, int nesting_level) { - static const struct sd_callbacks cb_default = { + static const struct hoedown_callbacks cb_default = { NULL, NULL, NULL, @@ -687,17 +670,17 @@ sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *optio toc_finalize, }; - memset(options, 0x0, sizeof(struct html_renderopt)); + memset(options, 0x0, sizeof(struct hoedown_html_renderopt)); options->flags = HTML_TOC; options->toc_data.nesting_level = nesting_level; - memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks)); + memcpy(callbacks, &cb_default, sizeof(struct hoedown_callbacks)); } void -sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, unsigned int render_flags) +hoedown_html_renderer(struct hoedown_callbacks *callbacks, struct hoedown_html_renderopt *options, unsigned int render_flags) { - static const struct sd_callbacks cb_default = { + static const struct hoedown_callbacks cb_default = { rndr_blockcode, rndr_blockquote, rndr_raw_block, @@ -736,11 +719,11 @@ sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options, }; /* Prepare the options pointer */ - memset(options, 0x0, sizeof(struct html_renderopt)); + memset(options, 0x0, sizeof(struct hoedown_html_renderopt)); options->flags = render_flags; /* Prepare the callbacks */ - memcpy(callbacks, &cb_default, sizeof(struct sd_callbacks)); + memcpy(callbacks, &cb_default, sizeof(struct hoedown_callbacks)); if (render_flags & HTML_SKIP_IMAGES) callbacks->image = NULL; diff --git a/src/html.h b/src/html.h new file mode 100644 index 0000000..63e8b4f --- /dev/null +++ b/src/html.h @@ -0,0 +1,63 @@ +#ifndef HOEDOWN_HTML_H +#define HOEDOWN_HTML_H + +#include "markdown.h" +#include "buffer.h" +#include <stdlib.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct hoedown_html_renderopt { + struct { + int header_count; + int current_level; + int level_offset; + int nesting_level; + } toc_data; + + unsigned int flags; + + /* extra callbacks */ + void (*link_attributes)(struct hoedown_buffer *ob, const struct hoedown_buffer *url, void *self); +}; + +typedef enum { + HTML_SKIP_HTML = (1 << 0), + HTML_SKIP_STYLE = (1 << 1), + HTML_SKIP_IMAGES = (1 << 2), + HTML_SKIP_LINKS = (1 << 3), + HTML_EXPAND_TABS = (1 << 4), + HTML_SAFELINK = (1 << 5), + HTML_TOC = (1 << 6), + HTML_HARD_WRAP = (1 << 7), + HTML_USE_XHTML = (1 << 8), + HTML_ESCAPE = (1 << 9), + HTML_PRETTIFY = (1 << 10), +} html_render_mode; + +typedef enum { + HTML_TAG_NONE = 0, + HTML_TAG_OPEN, + HTML_TAG_CLOSE, +} html_tag; + +int +hoedown_html_is_tag(const uint8_t *tag_data, size_t tag_size, const char *tagname); + +extern void +hoedown_html_renderer(struct hoedown_callbacks *callbacks, struct hoedown_html_renderopt *options_ptr, unsigned int render_flags); + +extern void +hoedown_html_toc_renderer(struct hoedown_callbacks *callbacks, struct hoedown_html_renderopt *options_ptr, int nesting_level); + +extern void +hoedown_html_smartypants(struct hoedown_buffer *ob, const uint8_t *text, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/html_blocks.h b/src/html_blocks.h index 0983f80..584f3ba 100644 --- a/src/html_blocks.h +++ b/src/html_blocks.h @@ -1,5 +1,5 @@ /* C code produced by gperf version 3.0.3 */ -/* Command-line: gperf -N find_block_tag -H hash_block_tag -C -c -E -P -S 1 --ignore-case html_block_names.txt */ +/* Command-line: gperf -N hoedown_find_block_tag -H hash_block_tag -C -c -E -P -S 1 --ignore-case html_block_names.txt */ /* Computed positions: -k'1-2' */ #if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \ @@ -141,7 +141,7 @@ __attribute__ ((__gnu_inline__)) #endif #endif const char * -find_block_tag (str, len) +hoedown_find_block_tag (str, len) register const char *str; register unsigned int len; { diff --git a/html/html_smartypants.c b/src/html_smartypants.c index ed4c1db..e88c1e6 100644 --- a/html/html_smartypants.c +++ b/src/html_smartypants.c @@ -1,19 +1,3 @@ -/* - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - #include "buffer.h" #include "html.h" @@ -31,19 +15,19 @@ struct smartypants_data { int in_dquote; }; -static size_t smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); -static size_t smartypants_cb__escape(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__ltag(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__dquote(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__amp(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__period(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__number(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__dash(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__parens(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__squote(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__backtick(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); +static size_t smartypants_cb__escape(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size); static size_t (*smartypants_cb_ptrs[]) - (struct buf *, struct smartypants_data *, uint8_t, const uint8_t *, size_t) = + (struct hoedown_buffer *, struct smartypants_data *, uint8_t, const uint8_t *, size_t) = { NULL, /* 0 */ smartypants_cb__dash, /* 1 */ @@ -104,7 +88,7 @@ squote_len(const uint8_t *text, size_t size) // Converts " or ' at very beginning or end of a word to left or right quote static int -smartypants_quotes(struct buf *ob, uint8_t previous_char, uint8_t next_char, uint8_t quote, int *is_open) +smartypants_quotes(struct hoedown_buffer *ob, uint8_t previous_char, uint8_t next_char, uint8_t quote, int *is_open) { char ent[8]; @@ -116,7 +100,7 @@ smartypants_quotes(struct buf *ob, uint8_t previous_char, uint8_t next_char, uin snprintf(ent, sizeof(ent), "&%c%cquo;", (*is_open) ? 'r' : 'l', quote); *is_open = !(*is_open); - bufputs(ob, ent); + hoedown_buffer_puts(ob, ent); return 1; } @@ -125,7 +109,7 @@ smartypants_quotes(struct buf *ob, uint8_t previous_char, uint8_t next_char, uin // 'squote_text' points to the original single quote, and 'squote_size' is its length. // 'text' points at the last character of the single-quote, e.g. ' or ; static size_t -smartypants_squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size, +smartypants_squote(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size, const uint8_t *squote_text, size_t squote_size) { if (size >= 2) { @@ -163,20 +147,20 @@ smartypants_squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previo if (smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 's', &smrt->in_squote)) return 0; - bufput(ob, squote_text, squote_size); + hoedown_buffer_put(ob, squote_text, squote_size); return 0; } // Converts ' to left or right single quote. static size_t -smartypants_cb__squote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__squote(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { return smartypants_squote(ob, smrt, previous_char, text, size, text, 1); } // Converts (c), (r), (tm) static size_t -smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__parens(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size >= 3) { uint8_t t1 = tolower(text[1]); @@ -198,13 +182,13 @@ smartypants_cb__parens(struct buf *ob, struct smartypants_data *smrt, uint8_t pr } } - bufputc(ob, text[0]); + hoedown_buffer_putc(ob, text[0]); return 0; } // Converts "--" to em-dash, etc. static size_t -smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__dash(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size >= 3 && text[1] == '-' && text[2] == '-') { BUFPUTSL(ob, "—"); @@ -216,13 +200,13 @@ smartypants_cb__dash(struct buf *ob, struct smartypants_data *smrt, uint8_t prev return 1; } - bufputc(ob, text[0]); + hoedown_buffer_putc(ob, text[0]); return 0; } // Converts " etc. static size_t -smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__amp(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size >= 6 && memcmp(text, """, 6) == 0) { if (smartypants_quotes(ob, previous_char, size >= 7 ? text[6] : 0, 'd', &smrt->in_dquote)) @@ -237,13 +221,13 @@ smartypants_cb__amp(struct buf *ob, struct smartypants_data *smrt, uint8_t previ if (size >= 4 && memcmp(text, "�", 4) == 0) return 3; - bufputc(ob, '&'); + hoedown_buffer_putc(ob, '&'); return 0; } // Converts "..." to ellipsis static size_t -smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__period(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size >= 3 && text[1] == '.' && text[2] == '.') { BUFPUTSL(ob, "…"); @@ -255,26 +239,26 @@ smartypants_cb__period(struct buf *ob, struct smartypants_data *smrt, uint8_t pr return 4; } - bufputc(ob, text[0]); + hoedown_buffer_putc(ob, text[0]); return 0; } // Converts `` to opening double quote static size_t -smartypants_cb__backtick(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__backtick(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size >= 2 && text[1] == '`') { if (smartypants_quotes(ob, previous_char, size >= 3 ? text[2] : 0, 'd', &smrt->in_dquote)) return 1; } - bufputc(ob, text[0]); + hoedown_buffer_putc(ob, text[0]); return 0; } // Converts 1/2, 1/4, 3/4 static size_t -smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__number(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (word_boundary(previous_char) && size >= 3) { if (text[0] == '1' && text[1] == '/' && text[2] == '2') { @@ -301,13 +285,13 @@ smartypants_cb__number(struct buf *ob, struct smartypants_data *smrt, uint8_t pr } } - bufputc(ob, text[0]); + hoedown_buffer_putc(ob, text[0]); return 0; } // Converts " to left or right double quote static size_t -smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__dquote(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (!smartypants_quotes(ob, previous_char, size > 0 ? text[1] : 0, 'd', &smrt->in_dquote)) BUFPUTSL(ob, """); @@ -316,7 +300,7 @@ smartypants_cb__dquote(struct buf *ob, struct smartypants_data *smrt, uint8_t pr } static size_t -smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__ltag(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { static const char *skip_tags[] = { "pre", "code", "var", "samp", "kbd", "math", "script", "style" @@ -329,7 +313,7 @@ smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t prev i++; for (tag = 0; tag < skip_tags_count; ++tag) { - if (sdhtml_is_tag(text, size, skip_tags[tag]) == HTML_TAG_OPEN) + if (hoedown_html_is_tag(text, size, skip_tags[tag]) == HTML_TAG_OPEN) break; } @@ -341,7 +325,7 @@ smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t prev if (i == size) break; - if (sdhtml_is_tag(text + i, size - i, skip_tags[tag]) == HTML_TAG_CLOSE) + if (hoedown_html_is_tag(text + i, size - i, skip_tags[tag]) == HTML_TAG_CLOSE) break; i++; @@ -351,12 +335,12 @@ smartypants_cb__ltag(struct buf *ob, struct smartypants_data *smrt, uint8_t prev i++; } - bufput(ob, text, i + 1); + hoedown_buffer_put(ob, text, i + 1); return i; } static size_t -smartypants_cb__escape(struct buf *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) +smartypants_cb__escape(struct hoedown_buffer *ob, struct smartypants_data *smrt, uint8_t previous_char, const uint8_t *text, size_t size) { if (size < 2) return 0; @@ -368,11 +352,11 @@ smartypants_cb__escape(struct buf *ob, struct smartypants_data *smrt, uint8_t pr case '.': case '-': case '`': - bufputc(ob, text[1]); + hoedown_buffer_putc(ob, text[1]); return 1; default: - bufputc(ob, '\\'); + hoedown_buffer_putc(ob, '\\'); return 0; } } @@ -408,7 +392,7 @@ static struct { #endif void -sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size) +hoedown_html_smartypants(struct hoedown_buffer *ob, const uint8_t *text, size_t size) { size_t i; struct smartypants_data smrt = {0, 0}; @@ -416,7 +400,7 @@ sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size) if (!text) return; - bufgrow(ob, size); + hoedown_buffer_grow(ob, size); for (i = 0; i < size; ++i) { size_t org; @@ -427,7 +411,7 @@ sdhtml_smartypants(struct buf *ob, const uint8_t *text, size_t size) i++; if (i > org) - bufput(ob, text + org, i - org); + hoedown_buffer_put(ob, text + org, i - org); if (i < size) { i += smartypants_cb_ptrs[(int)action] diff --git a/src/markdown.c b/src/markdown.c index 526e192..dc4370a 100644 --- a/src/markdown.c +++ b/src/markdown.c @@ -1,22 +1,5 @@ /* markdown.c - generic markdown parser */ -/* - * Copyright (c) 2009, Natacha Porté - * Copyright (c) 2011, Vicent Marti - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - #include "markdown.h" #include "stack.h" @@ -49,8 +32,8 @@ struct link_ref { unsigned int id; - struct buf *link; - struct buf *title; + struct hoedown_buffer *link; + struct hoedown_buffer *title; struct link_ref *next; }; @@ -62,7 +45,7 @@ struct footnote_ref { int is_used; unsigned int num; - struct buf *contents; + struct hoedown_buffer *contents; }; /* footnote_item: an item in a footnote_list */ @@ -82,22 +65,22 @@ struct footnote_list { /* returns the number of chars taken care of */ /* data is the pointer of the beginning of the span */ /* offset is the number of valid chars before data */ -struct sd_markdown; +struct hoedown_markdown; typedef size_t -(*char_trigger)(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); - -static size_t char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_quote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); -static size_t char_superscript(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size); +(*char_trigger)(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); + +static size_t char_emphasis(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_quote(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_linebreak(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_codespan(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_escape(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_entity(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_langle_tag(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_autolink_url(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_autolink_email(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_autolink_www(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_link(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); +static size_t char_superscript(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size); enum markdown_char_t { MD_CHAR_NONE = 0, @@ -132,15 +115,15 @@ static char_trigger markdown_char_ptrs[] = { }; /* render • structure containing one particular render */ -struct sd_markdown { - struct sd_callbacks cb; +struct hoedown_markdown { + struct hoedown_callbacks cb; void *opaque; struct link_ref *refs[REF_TABLE_SIZE]; struct footnote_list footnotes_found; struct footnote_list footnotes_used; uint8_t active_char[256]; - struct stack work_bufs[2]; + struct hoedown_stack work_bufs[2]; unsigned int ext_flags; size_t max_nesting; int in_link_body; @@ -150,33 +133,33 @@ struct sd_markdown { * HELPER FUNCTIONS * ***************************/ -static inline struct buf * -rndr_newbuf(struct sd_markdown *rndr, int type) +static inline struct hoedown_buffer * +rndr_newbuf(struct hoedown_markdown *rndr, int type) { static const size_t buf_size[2] = {256, 64}; - struct buf *work = NULL; - struct stack *pool = &rndr->work_bufs[type]; + struct hoedown_buffer *work = NULL; + struct hoedown_stack *pool = &rndr->work_bufs[type]; if (pool->size < pool->asize && pool->item[pool->size] != NULL) { work = pool->item[pool->size++]; work->size = 0; } else { - work = bufnew(buf_size[type]); - stack_push(pool, work); + work = hoedown_buffer_new(buf_size[type]); + hoedown_stack_push(pool, work); } return work; } static inline void -rndr_popbuf(struct sd_markdown *rndr, int type) +rndr_popbuf(struct hoedown_markdown *rndr, int type) { rndr->work_bufs[type].size--; } static void -unscape_text(struct buf *ob, struct buf *src) +unscape_text(struct hoedown_buffer *ob, struct hoedown_buffer *src) { size_t i = 0, org; while (i < src->size) { @@ -185,12 +168,12 @@ unscape_text(struct buf *ob, struct buf *src) i++; if (i > org) - bufput(ob, src->data + org, i - org); + hoedown_buffer_put(ob, src->data + org, i - org); if (i + 1 >= src->size) break; - bufputc(ob, src->data[i + 1]); + hoedown_buffer_putc(ob, src->data[i + 1]); i += 2; } } @@ -253,8 +236,8 @@ free_link_refs(struct link_ref **references) while (r) { next = r->next; - bufrelease(r->link); - bufrelease(r->title); + hoedown_buffer_release(r->link); + hoedown_buffer_release(r->title); free(r); r = next; } @@ -312,7 +295,7 @@ find_footnote_ref(struct footnote_list *list, uint8_t *name, size_t length) static void free_footnote_ref(struct footnote_ref *ref) { - bufrelease(ref->contents); + hoedown_buffer_release(ref->contents); free(ref); } @@ -449,11 +432,11 @@ tag_length(uint8_t *data, size_t size, enum mkd_autolink *autolink) /* parse_inline • parses inline markdown elements */ static void -parse_inline(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_inline(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t i = 0, end = 0; uint8_t action = 0; - struct buf work = { 0, 0, 0, 0 }; + struct hoedown_buffer work = { 0, 0, 0, 0 }; if (rndr->work_bufs[BUFFER_SPAN].size + rndr->work_bufs[BUFFER_BLOCK].size > rndr->max_nesting) @@ -471,7 +454,7 @@ parse_inline(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t siz rndr->cb.normal_text(ob, &work, rndr->opaque); } else - bufput(ob, data + i, end - i); + hoedown_buffer_put(ob, data + i, end - i); if (end >= size) break; i = end; @@ -580,10 +563,10 @@ find_emph_char(uint8_t *data, size_t size, uint8_t c) /* parse_emph1 • parsing single emphase */ /* closed by a symbol not preceded by whitespace and not followed by symbol */ static size_t -parse_emph1(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c) +parse_emph1(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, uint8_t c) { size_t i = 0, len; - struct buf *work = 0; + struct hoedown_buffer *work = 0; int r; /* skipping one symbol if coming from emph3 */ @@ -620,10 +603,10 @@ parse_emph1(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size /* parse_emph2 • parsing single emphase */ static size_t -parse_emph2(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c) +parse_emph2(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, uint8_t c) { size_t i = 0, len; - struct buf *work = 0; + struct hoedown_buffer *work = 0; int r; while (i < size) { @@ -653,7 +636,7 @@ parse_emph2(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size /* parse_emph3 • parsing single emphase */ /* finds the first closing tag, and delegates to the other emph */ static size_t -parse_emph3(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, uint8_t c) +parse_emph3(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, uint8_t c) { size_t i = 0, len; int r; @@ -669,7 +652,7 @@ parse_emph3(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size if (i + 2 < size && data[i + 1] == c && data[i + 2] == c && rndr->cb.triple_emphasis) { /* triple symbol found */ - struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN); + struct hoedown_buffer *work = rndr_newbuf(rndr, BUFFER_SPAN); parse_inline(work, rndr, data, i); r = rndr->cb.triple_emphasis(ob, work, rndr->opaque); @@ -694,7 +677,7 @@ parse_emph3(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size /* char_emphasis • single and double emphasis parsing */ static size_t -char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_emphasis(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { uint8_t c = data[0]; size_t ret; @@ -733,7 +716,7 @@ char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t of /* char_linebreak • '\n' preceded by two spaces (assuming linebreak != 0) */ static size_t -char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_linebreak(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { if (offset < 2 || data[-1] != ' ' || data[-2] != ' ') return 0; @@ -748,7 +731,7 @@ char_linebreak(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t o /* char_codespan • '`' parsing a code span (assuming codespan != 0) */ static size_t -char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_codespan(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { size_t end, nb = 0, i, f_begin, f_end; @@ -777,7 +760,7 @@ char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t of /* real code span */ if (f_begin < f_end) { - struct buf work = { data + f_begin, f_end - f_begin, 0, 0 }; + struct hoedown_buffer work = { data + f_begin, f_end - f_begin, 0, 0 }; if (!rndr->cb.codespan(ob, &work, rndr->opaque)) end = 0; } else { @@ -790,7 +773,7 @@ char_codespan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t of /* char_quote • '"' parsing a quote */ static size_t -char_quote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_quote(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { size_t end, nq = 0, i, f_begin, f_end; @@ -819,7 +802,7 @@ char_quote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offse /* real quote */ if (f_begin < f_end) { - struct buf work = { data + f_begin, f_end - f_begin, 0, 0 }; + struct hoedown_buffer work = { data + f_begin, f_end - f_begin, 0, 0 }; if (!rndr->cb.quote(ob, &work, rndr->opaque)) end = 0; } else { @@ -833,10 +816,10 @@ char_quote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offse /* char_escape • '\\' backslash escape */ static size_t -char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_escape(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { static const char *escape_chars = "\\`*_{}[]()#+-.!:|&<>^~"; - struct buf work = { 0, 0, 0, 0 }; + struct hoedown_buffer work = { 0, 0, 0, 0 }; if (size > 1) { if (strchr(escape_chars, data[1]) == NULL) @@ -847,9 +830,9 @@ char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offs work.size = 1; rndr->cb.normal_text(ob, &work, rndr->opaque); } - else bufputc(ob, data[1]); + else hoedown_buffer_putc(ob, data[1]); } else if (size == 1) { - bufputc(ob, data[0]); + hoedown_buffer_putc(ob, data[0]); } return 2; @@ -858,10 +841,10 @@ char_escape(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offs /* char_entity • '&' escaped when it doesn't belong to an entity */ /* valid entities are assumed to be anything matching &#?[A-Za-z0-9]+; */ static size_t -char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_entity(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { size_t end = 1; - struct buf work = { 0, 0, 0, 0 }; + struct hoedown_buffer work = { 0, 0, 0, 0 }; if (end < size && data[end] == '#') end++; @@ -879,23 +862,23 @@ char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offs work.size = end; rndr->cb.entity(ob, &work, rndr->opaque); } - else bufput(ob, data, end); + else hoedown_buffer_put(ob, data, end); return end; } /* char_langle_tag • '<' when tags or autolinks are allowed */ static size_t -char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_langle_tag(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { enum mkd_autolink altype = MKDA_NOT_AUTOLINK; size_t end = tag_length(data, size, &altype); - struct buf work = { data, end, 0, 0 }; + struct hoedown_buffer work = { data, end, 0, 0 }; int ret = 0; if (end > 2) { if (rndr->cb.autolink && altype != MKDA_NOT_AUTOLINK) { - struct buf *u_link = rndr_newbuf(rndr, BUFFER_SPAN); + struct hoedown_buffer *u_link = rndr_newbuf(rndr, BUFFER_SPAN); work.data = data + 1; work.size = end - 2; unscape_text(u_link, &work); @@ -911,9 +894,9 @@ char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t } static size_t -char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_autolink_www(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { - struct buf *link, *link_url, *link_text; + struct hoedown_buffer *link, *link_url, *link_text; size_t link_len, rewind; if (!rndr->cb.link || rndr->in_link_body) @@ -921,10 +904,10 @@ char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_ link = rndr_newbuf(rndr, BUFFER_SPAN); - if ((link_len = sd_autolink__www(&rewind, link, data, offset, size, SD_AUTOLINK_SHORT_DOMAINS)) > 0) { + if ((link_len = hoedown_autolink__www(&rewind, link, data, offset, size, HOEDOWN_AUTOLINK_SHORT_DOMAINS)) > 0) { link_url = rndr_newbuf(rndr, BUFFER_SPAN); BUFPUTSL(link_url, "http://"); - bufput(link_url, link->data, link->size); + hoedown_buffer_put(link_url, link->data, link->size); ob->size -= rewind; if (rndr->cb.normal_text) { @@ -943,9 +926,9 @@ char_autolink_www(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_ } static size_t -char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_autolink_email(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { - struct buf *link; + struct hoedown_buffer *link; size_t link_len, rewind; if (!rndr->cb.autolink || rndr->in_link_body) @@ -953,7 +936,7 @@ char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, siz link = rndr_newbuf(rndr, BUFFER_SPAN); - if ((link_len = sd_autolink__email(&rewind, link, data, offset, size, 0)) > 0) { + if ((link_len = hoedown_autolink__email(&rewind, link, data, offset, size, 0)) > 0) { ob->size -= rewind; rndr->cb.autolink(ob, link, MKDA_EMAIL, rndr->opaque); } @@ -963,9 +946,9 @@ char_autolink_email(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, siz } static size_t -char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_autolink_url(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { - struct buf *link; + struct hoedown_buffer *link; size_t link_len, rewind; if (!rndr->cb.autolink || rndr->in_link_body) @@ -973,7 +956,7 @@ char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_ link = rndr_newbuf(rndr, BUFFER_SPAN); - if ((link_len = sd_autolink__url(&rewind, link, data, offset, size, 0)) > 0) { + if ((link_len = hoedown_autolink__url(&rewind, link, data, offset, size, 0)) > 0) { ob->size -= rewind; rndr->cb.autolink(ob, link, MKDA_NORMAL, rndr->opaque); } @@ -984,14 +967,14 @@ char_autolink_url(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_ /* char_link • '[': parsing a link or an image */ static size_t -char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_link(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { int is_img = (offset && data[-1] == '!'), level; size_t i = 1, txt_e, link_b = 0, link_e = 0, title_b = 0, title_e = 0; - struct buf *content = 0; - struct buf *link = 0; - struct buf *title = 0; - struct buf *u_link = 0; + struct hoedown_buffer *content = 0; + struct hoedown_buffer *link = 0; + struct hoedown_buffer *title = 0; + struct hoedown_buffer *u_link = 0; size_t org_work_size = rndr->work_bufs[BUFFER_SPAN].size; int text_has_nl = 0, ret = 0; int in_title = 0, qtype = 0; @@ -1029,7 +1012,7 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset if (txt_e < 3) goto cleanup; - struct buf id = { 0, 0, 0, 0 }; + struct hoedown_buffer id = { 0, 0, 0, 0 }; struct footnote_ref *fr; id.data = data + 2; @@ -1125,12 +1108,12 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset /* building escaped link and title */ if (link_e > link_b) { link = rndr_newbuf(rndr, BUFFER_SPAN); - bufput(link, data + link_b, link_e - link_b); + hoedown_buffer_put(link, data + link_b, link_e - link_b); } if (title_e > title_b) { title = rndr_newbuf(rndr, BUFFER_SPAN); - bufput(title, data + title_b, title_e - title_b); + hoedown_buffer_put(title, data + title_b, title_e - title_b); } i++; @@ -1138,7 +1121,7 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset /* reference style link */ else if (i < size && data[i] == '[') { - struct buf id = { 0, 0, 0, 0 }; + struct hoedown_buffer id = { 0, 0, 0, 0 }; struct link_ref *lr; /* looking for the id */ @@ -1151,14 +1134,14 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset /* finding the link_ref */ if (link_b == link_e) { if (text_has_nl) { - struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN); + struct hoedown_buffer *b = rndr_newbuf(rndr, BUFFER_SPAN); size_t j; for (j = 1; j < txt_e; j++) { if (data[j] != '\n') - bufputc(b, data[j]); + hoedown_buffer_putc(b, data[j]); else if (data[j - 1] != ' ') - bufputc(b, ' '); + hoedown_buffer_putc(b, ' '); } id.data = b->data; @@ -1184,19 +1167,19 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset /* shortcut reference style link */ else { - struct buf id = { 0, 0, 0, 0 }; + struct hoedown_buffer id = { 0, 0, 0, 0 }; struct link_ref *lr; /* crafting the id */ if (text_has_nl) { - struct buf *b = rndr_newbuf(rndr, BUFFER_SPAN); + struct hoedown_buffer *b = rndr_newbuf(rndr, BUFFER_SPAN); size_t j; for (j = 1; j < txt_e; j++) { if (data[j] != '\n') - bufputc(b, data[j]); + hoedown_buffer_putc(b, data[j]); else if (data[j - 1] != ' ') - bufputc(b, ' '); + hoedown_buffer_putc(b, ' '); } id.data = b->data; @@ -1223,7 +1206,7 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset if (txt_e > 1) { content = rndr_newbuf(rndr, BUFFER_SPAN); if (is_img) { - bufput(content, data + 1, txt_e - 1); + hoedown_buffer_put(content, data + 1, txt_e - 1); } else { /* disable autolinking when parsing inline the * content of a link */ @@ -1255,10 +1238,10 @@ cleanup: } static size_t -char_superscript(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size) +char_superscript(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t offset, size_t size) { size_t sup_start, sup_len; - struct buf *sup; + struct hoedown_buffer *sup; if (!rndr->cb.superscript) return 0; @@ -1373,7 +1356,7 @@ prefix_codefence(uint8_t *data, size_t size) /* check if a line is a code fence; return its size if it is */ static size_t -is_codefence(uint8_t *data, size_t size, struct buf *syntax) +is_codefence(uint8_t *data, size_t size, struct hoedown_buffer *syntax) { size_t i = 0, syn_len = 0; uint8_t *syn_start; @@ -1430,7 +1413,7 @@ is_codefence(uint8_t *data, size_t size, struct buf *syntax) /* is_atxheader • returns whether the line is a hash-prefixed header */ static int -is_atxheader(struct sd_markdown *rndr, uint8_t *data, size_t size) +is_atxheader(struct hoedown_markdown *rndr, uint8_t *data, size_t size) { if (data[0] != '#') return 0; @@ -1560,17 +1543,17 @@ prefix_uli(uint8_t *data, size_t size) /* parse_block • parsing of one block, returning next uint8_t to parse */ -static void parse_block(struct buf *ob, struct sd_markdown *rndr, +static void parse_block(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size); /* parse_blockquote • handles parsing of a blockquote fragment */ static size_t -parse_blockquote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_blockquote(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t beg, end = 0, pre, work_size = 0; uint8_t *work_data = 0; - struct buf *out = 0; + struct hoedown_buffer *out = 0; out = rndr_newbuf(rndr, BUFFER_BLOCK); beg = 0; @@ -1589,7 +1572,7 @@ parse_blockquote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t break; if (beg < end) { /* copy into the in-place working buffer */ - /* bufput(work, data + beg, end - beg); */ + /* hoedown_buffer_put(work, data + beg, end - beg); */ if (!work_data) work_data = data + beg; else if (data + beg != work_data + work_size) @@ -1607,15 +1590,15 @@ parse_blockquote(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t } static size_t -parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int do_render); +parse_htmlblock(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, int do_render); /* parse_blockquote • handles parsing of a regular paragraph */ static size_t -parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_paragraph(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t i = 0, end = 0; int level = 0; - struct buf work = { data, 0, 0, 0 }; + struct hoedown_buffer work = { data, 0, 0, 0 }; while (i < size) { for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */; @@ -1672,13 +1655,13 @@ parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t work.size--; if (!level) { - struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK); + struct hoedown_buffer *tmp = rndr_newbuf(rndr, BUFFER_BLOCK); parse_inline(tmp, rndr, work.data, work.size); if (rndr->cb.paragraph) rndr->cb.paragraph(ob, tmp, rndr->opaque); rndr_popbuf(rndr, BUFFER_BLOCK); } else { - struct buf *header_work; + struct hoedown_buffer *header_work; if (work.size) { size_t beg; @@ -1693,7 +1676,7 @@ parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t work.size -= 1; if (work.size > 0) { - struct buf *tmp = rndr_newbuf(rndr, BUFFER_BLOCK); + struct hoedown_buffer *tmp = rndr_newbuf(rndr, BUFFER_BLOCK); parse_inline(tmp, rndr, work.data, work.size); if (rndr->cb.paragraph) @@ -1720,11 +1703,11 @@ parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t /* parse_fencedcode • handles parsing of a block-level code fragment */ static size_t -parse_fencedcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_fencedcode(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t beg, end; - struct buf *work = 0; - struct buf lang = { 0, 0, 0, 0 }; + struct hoedown_buffer *work = 0; + struct hoedown_buffer lang = { 0, 0, 0, 0 }; beg = is_codefence(data, size, &lang); if (beg == 0) return 0; @@ -1733,7 +1716,7 @@ parse_fencedcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t while (beg < size) { size_t fence_end; - struct buf fence_trail = { 0, 0, 0, 0 }; + struct hoedown_buffer fence_trail = { 0, 0, 0, 0 }; fence_end = is_codefence(data + beg, size - beg, &fence_trail); if (fence_end != 0 && fence_trail.size == 0) { @@ -1747,14 +1730,14 @@ parse_fencedcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t /* verbatim copy to the working buffer, escaping entities */ if (is_empty(data + beg, end - beg)) - bufputc(work, '\n'); - else bufput(work, data + beg, end - beg); + hoedown_buffer_putc(work, '\n'); + else hoedown_buffer_put(work, data + beg, end - beg); } beg = end; } if (work->size && work->data[work->size - 1] != '\n') - bufputc(work, '\n'); + hoedown_buffer_putc(work, '\n'); if (rndr->cb.blockcode) rndr->cb.blockcode(ob, work, lang.size ? &lang : NULL, rndr->opaque); @@ -1764,10 +1747,10 @@ parse_fencedcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t } static size_t -parse_blockcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_blockcode(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t beg, end, pre; - struct buf *work = 0; + struct hoedown_buffer *work = 0; work = rndr_newbuf(rndr, BUFFER_BLOCK); @@ -1786,8 +1769,8 @@ parse_blockcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t /* verbatim copy to the working buffer, escaping entities */ if (is_empty(data + beg, end - beg)) - bufputc(work, '\n'); - else bufput(work, data + beg, end - beg); + hoedown_buffer_putc(work, '\n'); + else hoedown_buffer_put(work, data + beg, end - beg); } beg = end; } @@ -1795,7 +1778,7 @@ parse_blockcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t while (work->size && work->data[work->size - 1] == '\n') work->size -= 1; - bufputc(work, '\n'); + hoedown_buffer_putc(work, '\n'); if (rndr->cb.blockcode) rndr->cb.blockcode(ob, work, NULL, rndr->opaque); @@ -1807,9 +1790,9 @@ parse_blockcode(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t /* parse_listitem • parsing of a single list item */ /* assuming initial prefix is already removed */ static size_t -parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int *flags) +parse_listitem(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, int *flags) { - struct buf *work = 0, *inter = 0; + struct hoedown_buffer *work = 0, *inter = 0; size_t beg = 0, end, pre, sublist = 0, orgpre = 0, i; int in_empty = 0, has_inside_empty = 0, in_fence = 0; @@ -1834,7 +1817,7 @@ parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t s inter = rndr_newbuf(rndr, BUFFER_SPAN); /* putting the first line into the working buffer */ - bufput(work, data + beg, end - beg); + hoedown_buffer_put(work, data + beg, end - beg); beg = end; /* process the following lines */ @@ -1899,14 +1882,14 @@ parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t s break; } else if (in_empty) { - bufputc(work, '\n'); + hoedown_buffer_putc(work, '\n'); has_inside_empty = 1; } in_empty = 0; /* adding the line without prefix into the working buffer */ - bufput(work, data + beg + i, end - beg - i); + hoedown_buffer_put(work, data + beg + i, end - beg - i); beg = end; } @@ -1944,9 +1927,9 @@ parse_listitem(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t s /* parse_list • parsing ordered or unordered list block */ static size_t -parse_list(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int flags) +parse_list(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, int flags) { - struct buf *work = 0; + struct hoedown_buffer *work = 0; size_t i = 0, j; work = rndr_newbuf(rndr, BUFFER_BLOCK); @@ -1967,7 +1950,7 @@ parse_list(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, /* parse_atxheader • parsing of atx-style headers */ static size_t -parse_atxheader(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_atxheader(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t level = 0; size_t i, end, skip; @@ -1987,7 +1970,7 @@ parse_atxheader(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t end--; if (end > i) { - struct buf *work = rndr_newbuf(rndr, BUFFER_SPAN); + struct hoedown_buffer *work = rndr_newbuf(rndr, BUFFER_SPAN); parse_inline(work, rndr, data + i, end - i); @@ -2002,9 +1985,9 @@ parse_atxheader(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t /* parse_footnote_def • parse a single footnote definition */ static void -parse_footnote_def(struct buf *ob, struct sd_markdown *rndr, unsigned int num, uint8_t *data, size_t size) +parse_footnote_def(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, unsigned int num, uint8_t *data, size_t size) { - struct buf *work = 0; + struct hoedown_buffer *work = 0; work = rndr_newbuf(rndr, BUFFER_SPAN); parse_block(work, rndr, data, size); @@ -2016,9 +1999,9 @@ parse_footnote_def(struct buf *ob, struct sd_markdown *rndr, unsigned int num, u /* parse_footnote_list • render the contents of the footnotes */ static void -parse_footnote_list(struct buf *ob, struct sd_markdown *rndr, struct footnote_list *footnotes) +parse_footnote_list(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, struct footnote_list *footnotes) { - struct buf *work = 0; + struct hoedown_buffer *work = 0; struct footnote_item *item; struct footnote_ref *ref; @@ -2045,7 +2028,7 @@ static size_t htmlblock_end_tag( const char *tag, size_t tag_len, - struct sd_markdown *rndr, + struct hoedown_markdown *rndr, uint8_t *data, size_t size) { @@ -2073,7 +2056,7 @@ htmlblock_end_tag( static size_t htmlblock_end(const char *curtag, - struct sd_markdown *rndr, + struct hoedown_markdown *rndr, uint8_t *data, size_t size, int start_of_line) @@ -2115,11 +2098,11 @@ htmlblock_end(const char *curtag, /* parse_htmlblock • parsing of inline HTML block */ static size_t -parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int do_render) +parse_htmlblock(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size, int do_render) { size_t i, j = 0, tag_end; const char *curtag = NULL; - struct buf work = { data, 0, 0, 0 }; + struct hoedown_buffer work = { data, 0, 0, 0 }; /* identification of the opening tag */ if (size < 2 || data[0] != '<') @@ -2130,7 +2113,7 @@ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t i++; if (i < size) - curtag = find_block_tag((char *)data + 1, (int)i - 1); + curtag = hoedown_find_block_tag((char *)data + 1, (int)i - 1); /* handling of special cases */ if (!curtag) { @@ -2200,8 +2183,8 @@ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t static void parse_table_row( - struct buf *ob, - struct sd_markdown *rndr, + struct hoedown_buffer *ob, + struct hoedown_markdown *rndr, uint8_t *data, size_t size, size_t columns, @@ -2209,7 +2192,7 @@ parse_table_row( int header_flag) { size_t i = 0, col; - struct buf *row_work = 0; + struct hoedown_buffer *row_work = 0; if (!rndr->cb.table_cell || !rndr->cb.table_row) return; @@ -2221,7 +2204,7 @@ parse_table_row( for (col = 0; col < columns && i < size; ++col) { size_t cell_start, cell_end; - struct buf *cell_work; + struct hoedown_buffer *cell_work; cell_work = rndr_newbuf(rndr, BUFFER_SPAN); @@ -2246,7 +2229,7 @@ parse_table_row( } for (; col < columns; ++col) { - struct buf empty_cell = { 0, 0, 0, 0 }; + struct hoedown_buffer empty_cell = { 0, 0, 0, 0 }; rndr->cb.table_cell(row_work, &empty_cell, col_data[col] | header_flag, rndr->opaque); } @@ -2257,8 +2240,8 @@ parse_table_row( static size_t parse_table_header( - struct buf *ob, - struct sd_markdown *rndr, + struct hoedown_buffer *ob, + struct hoedown_markdown *rndr, uint8_t *data, size_t size, size_t *columns, @@ -2346,15 +2329,15 @@ parse_table_header( static size_t parse_table( - struct buf *ob, - struct sd_markdown *rndr, + struct hoedown_buffer *ob, + struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t i; - struct buf *header_work = 0; - struct buf *body_work = 0; + struct hoedown_buffer *header_work = 0; + struct hoedown_buffer *body_work = 0; size_t columns; int *col_data = NULL; @@ -2404,7 +2387,7 @@ parse_table( /* parse_block • parsing of one block, returning next uint8_t to parse */ static void -parse_block(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) +parse_block(struct hoedown_buffer *ob, struct hoedown_markdown *rndr, uint8_t *data, size_t size) { size_t beg, end, i; uint8_t *txt_data; @@ -2474,7 +2457,7 @@ static int is_footnote(const uint8_t *data, size_t beg, size_t end, size_t *last, struct footnote_list *list) { size_t i = 0; - struct buf *contents = 0; + struct hoedown_buffer *contents = 0; size_t ind = 0; int in_empty = 0; size_t start = 0; @@ -2506,7 +2489,7 @@ is_footnote(const uint8_t *data, size_t beg, size_t end, size_t *last, struct fo i++; /* getting content buffer */ - contents = bufnew(64); + contents = hoedown_buffer_new(64); start = i; @@ -2538,16 +2521,16 @@ is_footnote(const uint8_t *data, size_t beg, size_t end, size_t *last, struct fo else break; } else if (in_empty) { - bufputc(contents, '\n'); + hoedown_buffer_putc(contents, '\n'); } in_empty = 0; /* adding the line into the content buffer */ - bufput(contents, data + start + ind, i - start - ind); + hoedown_buffer_put(contents, data + start + ind, i - start - ind); /* add carriage return */ if (i < end) { - bufput(contents, "\n", 1); + hoedown_buffer_put(contents, "\n", 1); if (i < end && (data[i] == '\n' || data[i] == '\r')) { i++; if (i < end && data[i] == '\n' && data[i - 1] == '\r') i++; @@ -2676,19 +2659,19 @@ is_ref(const uint8_t *data, size_t beg, size_t end, size_t *last, struct link_re if (!ref) return 0; - ref->link = bufnew(link_end - link_offset); - bufput(ref->link, data + link_offset, link_end - link_offset); + ref->link = hoedown_buffer_new(link_end - link_offset); + hoedown_buffer_put(ref->link, data + link_offset, link_end - link_offset); if (title_end > title_offset) { - ref->title = bufnew(title_end - title_offset); - bufput(ref->title, data + title_offset, title_end - title_offset); + ref->title = hoedown_buffer_new(title_end - title_offset); + hoedown_buffer_put(ref->title, data + title_offset, title_end - title_offset); } } return 1; } -static void expand_tabs(struct buf *ob, const uint8_t *line, size_t size) +static void expand_tabs(struct hoedown_buffer *ob, const uint8_t *line, size_t size) { size_t i = 0, tab = 0; @@ -2700,13 +2683,13 @@ static void expand_tabs(struct buf *ob, const uint8_t *line, size_t size) } if (i > org) - bufput(ob, line + org, i - org); + hoedown_buffer_put(ob, line + org, i - org); if (i >= size) break; do { - bufputc(ob, ' '); tab++; + hoedown_buffer_putc(ob, ' '); tab++; } while (tab % 4); i++; @@ -2717,25 +2700,25 @@ static void expand_tabs(struct buf *ob, const uint8_t *line, size_t size) * EXPORTED FUNCTIONS * **********************/ -struct sd_markdown * -sd_markdown_new( +struct hoedown_markdown * +hoedown_markdown_new( unsigned int extensions, size_t max_nesting, - const struct sd_callbacks *callbacks, + const struct hoedown_callbacks *callbacks, void *opaque) { - struct sd_markdown *md = NULL; + struct hoedown_markdown *md = NULL; assert(max_nesting > 0 && callbacks); - md = malloc(sizeof(struct sd_markdown)); + md = malloc(sizeof(struct hoedown_markdown)); if (!md) return NULL; - memcpy(&md->cb, callbacks, sizeof(struct sd_callbacks)); + memcpy(&md->cb, callbacks, sizeof(struct hoedown_callbacks)); - stack_init(&md->work_bufs[BUFFER_BLOCK], 4); - stack_init(&md->work_bufs[BUFFER_SPAN], 8); + hoedown_stack_init(&md->work_bufs[BUFFER_BLOCK], 4); + hoedown_stack_init(&md->work_bufs[BUFFER_SPAN], 8); memset(md->active_char, 0x0, 256); @@ -2783,20 +2766,20 @@ sd_markdown_new( } void -sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md) +hoedown_markdown_render(struct hoedown_buffer *ob, const uint8_t *document, size_t doc_size, struct hoedown_markdown *md) { #define MARKDOWN_GROW(x) ((x) + ((x) >> 1)) static const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF}; - struct buf *text; + struct hoedown_buffer *text; size_t beg, end; - text = bufnew(64); + text = hoedown_buffer_new(64); if (!text) return; /* Preallocate enough space for our buffer to avoid expanding while copying */ - bufgrow(text, doc_size); + hoedown_buffer_grow(text, doc_size); /* reset the references table */ memset(&md->refs, 0x0, REF_TABLE_SIZE * sizeof(void *)); @@ -2834,7 +2817,7 @@ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, str while (end < doc_size && (document[end] == '\n' || document[end] == '\r')) { /* add one \n per newline */ if (document[end] == '\n' || (end + 1 < doc_size && document[end + 1] != '\n')) - bufputc(text, '\n'); + hoedown_buffer_putc(text, '\n'); end++; } @@ -2842,7 +2825,7 @@ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, str } /* pre-grow the output buffer to minimize allocations */ - bufgrow(ob, MARKDOWN_GROW(text->size)); + hoedown_buffer_grow(ob, MARKDOWN_GROW(text->size)); /* second pass: actual rendering */ if (md->cb.doc_header) @@ -2851,7 +2834,7 @@ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, str if (text->size) { /* adding a final newline if not already present */ if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') - bufputc(text, '\n'); + hoedown_buffer_putc(text, '\n'); parse_block(ob, md, text->data, text->size); } @@ -2864,7 +2847,7 @@ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, str md->cb.doc_footer(ob, md->opaque); /* clean-up */ - bufrelease(text); + hoedown_buffer_release(text); free_link_refs(md->refs); if (footnotes_enabled) { free_footnote_list(&md->footnotes_found, 1); @@ -2876,28 +2859,28 @@ sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, str } void -sd_markdown_free(struct sd_markdown *md) +hoedown_markdown_free(struct hoedown_markdown *md) { size_t i; for (i = 0; i < (size_t)md->work_bufs[BUFFER_SPAN].asize; ++i) - bufrelease(md->work_bufs[BUFFER_SPAN].item[i]); + hoedown_buffer_release(md->work_bufs[BUFFER_SPAN].item[i]); for (i = 0; i < (size_t)md->work_bufs[BUFFER_BLOCK].asize; ++i) - bufrelease(md->work_bufs[BUFFER_BLOCK].item[i]); + hoedown_buffer_release(md->work_bufs[BUFFER_BLOCK].item[i]); - stack_free(&md->work_bufs[BUFFER_SPAN]); - stack_free(&md->work_bufs[BUFFER_BLOCK]); + hoedown_stack_free(&md->work_bufs[BUFFER_SPAN]); + hoedown_stack_free(&md->work_bufs[BUFFER_BLOCK]); free(md); } void -sd_version(int *ver_major, int *ver_minor, int *ver_revision) +hoedown_version(int *ver_major, int *ver_minor, int *ver_revision) { - *ver_major = SUNDOWN_VER_MAJOR; - *ver_minor = SUNDOWN_VER_MINOR; - *ver_revision = SUNDOWN_VER_REVISION; + *ver_major = HOEDOWN_VER_MAJOR; + *ver_minor = HOEDOWN_VER_MINOR; + *ver_revision = HOEDOWN_VER_REVISION; } /* vim: set filetype=c: */ diff --git a/src/markdown.h b/src/markdown.h index b9dc338..a4e0912 100644 --- a/src/markdown.h +++ b/src/markdown.h @@ -1,23 +1,7 @@ /* markdown.h - generic markdown parser */ -/* - * Copyright (c) 2009, Natacha Porté - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef UPSKIRT_MARKDOWN_H -#define UPSKIRT_MARKDOWN_H +#ifndef HOEDOWN_MARKDOWN_H +#define HOEDOWN_MARKDOWN_H #include "buffer.h" #include "autolink.h" @@ -26,10 +10,10 @@ extern "C" { #endif -#define SUNDOWN_VERSION "1.16.0" -#define SUNDOWN_VER_MAJOR 1 -#define SUNDOWN_VER_MINOR 16 -#define SUNDOWN_VER_REVISION 0 +#define HOEDOWN_VERSION "1.16.0" +#define HOEDOWN_VER_MAJOR 1 +#define HOEDOWN_VER_MINOR 16 +#define HOEDOWN_VER_REVISION 0 /******************** * TYPE DEFINITIONS * @@ -66,50 +50,50 @@ enum mkd_extensions { MKDEXT_QUOTE = (1 << 12) }; -/* sd_callbacks - functions for rendering parsed data */ -struct sd_callbacks { +/* hoedown_callbacks - functions for rendering parsed data */ +struct hoedown_callbacks { /* block level callbacks - NULL skips the block */ - void (*blockcode)(struct buf *ob, const struct buf *text, const struct buf *lang, void *opaque); - void (*blockquote)(struct buf *ob, const struct buf *text, void *opaque); - void (*blockhtml)(struct buf *ob,const struct buf *text, void *opaque); - void (*header)(struct buf *ob, const struct buf *text, int level, void *opaque); - void (*hrule)(struct buf *ob, void *opaque); - void (*list)(struct buf *ob, const struct buf *text, int flags, void *opaque); - void (*listitem)(struct buf *ob, const struct buf *text, int flags, void *opaque); - void (*paragraph)(struct buf *ob, const struct buf *text, void *opaque); - void (*table)(struct buf *ob, const struct buf *header, const struct buf *body, void *opaque); - void (*table_row)(struct buf *ob, const struct buf *text, void *opaque); - void (*table_cell)(struct buf *ob, const struct buf *text, int flags, void *opaque); - void (*footnotes)(struct buf *ob, const struct buf *text, void *opaque); - void (*footnote_def)(struct buf *ob, const struct buf *text, unsigned int num, void *opaque); + void (*blockcode)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, const struct hoedown_buffer *lang, void *opaque); + void (*blockquote)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + void (*blockhtml)(struct hoedown_buffer *ob,const struct hoedown_buffer *text, void *opaque); + void (*header)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int level, void *opaque); + void (*hrule)(struct hoedown_buffer *ob, void *opaque); + void (*list)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque); + void (*listitem)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque); + void (*paragraph)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + void (*table)(struct hoedown_buffer *ob, const struct hoedown_buffer *header, const struct hoedown_buffer *body, void *opaque); + void (*table_row)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + void (*table_cell)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, int flags, void *opaque); + void (*footnotes)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + void (*footnote_def)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, unsigned int num, void *opaque); /* span level callbacks - NULL or return 0 prints the span verbatim */ - int (*autolink)(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque); - int (*codespan)(struct buf *ob, const struct buf *text, void *opaque); - int (*double_emphasis)(struct buf *ob, const struct buf *text, void *opaque); - int (*emphasis)(struct buf *ob, const struct buf *text, void *opaque); - int (*underline)(struct buf *ob, const struct buf *text, void *opaque); - int (*highlight)(struct buf *ob, const struct buf *text, void *opaque); - int (*quote)(struct buf *ob, const struct buf *text, void *opaque); - int (*image)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *alt, void *opaque); - int (*linebreak)(struct buf *ob, void *opaque); - int (*link)(struct buf *ob, const struct buf *link, const struct buf *title, const struct buf *content, void *opaque); - int (*raw_html_tag)(struct buf *ob, const struct buf *tag, void *opaque); - int (*triple_emphasis)(struct buf *ob, const struct buf *text, void *opaque); - int (*strikethrough)(struct buf *ob, const struct buf *text, void *opaque); - int (*superscript)(struct buf *ob, const struct buf *text, void *opaque); - int (*footnote_ref)(struct buf *ob, unsigned int num, void *opaque); + int (*autolink)(struct hoedown_buffer *ob, const struct hoedown_buffer *link, enum mkd_autolink type, void *opaque); + int (*codespan)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*double_emphasis)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*emphasis)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*underline)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*highlight)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*quote)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*image)(struct hoedown_buffer *ob, const struct hoedown_buffer *link, const struct hoedown_buffer *title, const struct hoedown_buffer *alt, void *opaque); + int (*linebreak)(struct hoedown_buffer *ob, void *opaque); + int (*link)(struct hoedown_buffer *ob, const struct hoedown_buffer *link, const struct hoedown_buffer *title, const struct hoedown_buffer *content, void *opaque); + int (*raw_html_tag)(struct hoedown_buffer *ob, const struct hoedown_buffer *tag, void *opaque); + int (*triple_emphasis)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*strikethrough)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*superscript)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); + int (*footnote_ref)(struct hoedown_buffer *ob, unsigned int num, void *opaque); /* low level callbacks - NULL copies input directly into the output */ - void (*entity)(struct buf *ob, const struct buf *entity, void *opaque); - void (*normal_text)(struct buf *ob, const struct buf *text, void *opaque); + void (*entity)(struct hoedown_buffer *ob, const struct hoedown_buffer *entity, void *opaque); + void (*normal_text)(struct hoedown_buffer *ob, const struct hoedown_buffer *text, void *opaque); /* header and footer */ - void (*doc_header)(struct buf *ob, void *opaque); - void (*doc_footer)(struct buf *ob, void *opaque); + void (*doc_header)(struct hoedown_buffer *ob, void *opaque); + void (*doc_footer)(struct hoedown_buffer *ob, void *opaque); }; -struct sd_markdown; +struct hoedown_markdown; /********* * FLAGS * @@ -123,21 +107,21 @@ struct sd_markdown; * EXPORTED FUNCTIONS * **********************/ -extern struct sd_markdown * -sd_markdown_new( +extern struct hoedown_markdown * +hoedown_markdown_new( unsigned int extensions, size_t max_nesting, - const struct sd_callbacks *callbacks, + const struct hoedown_callbacks *callbacks, void *opaque); extern void -sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md); +hoedown_markdown_render(struct hoedown_buffer *ob, const uint8_t *document, size_t doc_size, struct hoedown_markdown *md); extern void -sd_markdown_free(struct sd_markdown *md); +hoedown_markdown_free(struct hoedown_markdown *md); extern void -sd_version(int *major, int *minor, int *revision); +hoedown_version(int *major, int *minor, int *revision); #ifdef __cplusplus } diff --git a/src/stack.c b/src/stack.c index ce069ff..13f28b2 100644 --- a/src/stack.c +++ b/src/stack.c @@ -2,7 +2,7 @@ #include <string.h> int -stack_grow(struct stack *st, size_t new_size) +hoedown_stack_grow(struct hoedown_stack *st, size_t new_size) { void **new_st; @@ -26,7 +26,7 @@ stack_grow(struct stack *st, size_t new_size) } void -stack_free(struct stack *st) +hoedown_stack_free(struct hoedown_stack *st) { if (!st) return; @@ -39,7 +39,7 @@ stack_free(struct stack *st) } int -stack_init(struct stack *st, size_t initial_size) +hoedown_stack_init(struct hoedown_stack *st, size_t initial_size) { st->item = NULL; st->size = 0; @@ -48,11 +48,11 @@ stack_init(struct stack *st, size_t initial_size) if (!initial_size) initial_size = 8; - return stack_grow(st, initial_size); + return hoedown_stack_grow(st, initial_size); } void * -stack_pop(struct stack *st) +hoedown_stack_pop(struct hoedown_stack *st) { if (!st->size) return NULL; @@ -61,9 +61,9 @@ stack_pop(struct stack *st) } int -stack_push(struct stack *st, void *item) +hoedown_stack_push(struct hoedown_stack *st, void *item) { - if (stack_grow(st, st->size * 2) < 0) + if (hoedown_stack_grow(st, st->size * 2) < 0) return -1; st->item[st->size++] = item; @@ -71,7 +71,7 @@ stack_push(struct stack *st, void *item) } void * -stack_top(struct stack *st) +hoedown_stack_top(struct hoedown_stack *st) { if (!st->size) return NULL; diff --git a/src/stack.h b/src/stack.h index 08ff030..4954489 100644 --- a/src/stack.h +++ b/src/stack.h @@ -7,20 +7,20 @@ extern "C" { #endif -struct stack { +struct hoedown_stack { void **item; size_t size; size_t asize; }; -void stack_free(struct stack *); -int stack_grow(struct stack *, size_t); -int stack_init(struct stack *, size_t); +void hoedown_stack_free(struct hoedown_stack *); +int hoedown_stack_grow(struct hoedown_stack *, size_t); +int hoedown_stack_init(struct hoedown_stack *, size_t); -int stack_push(struct stack *, void *); +int hoedown_stack_push(struct hoedown_stack *, void *); -void *stack_pop(struct stack *); -void *stack_top(struct stack *); +void *hoedown_stack_pop(struct hoedown_stack *); +void *hoedown_stack_top(struct hoedown_stack *); #ifdef __cplusplus } diff --git a/sundown.def b/sundown.def deleted file mode 100644 index 7cd41bb..0000000 --- a/sundown.def +++ /dev/null @@ -1,20 +0,0 @@ -LIBRARY SUNDOWN -EXPORTS - sdhtml_renderer - sdhtml_toc_renderer - sdhtml_smartypants - bufgrow - bufnew - bufcstr - bufprefix - bufput - bufputs - bufputc - bufrelease - bufreset - bufslurp - bufprintf - sd_markdown_new - sd_markdown_render - sd_markdown_free - sd_version
\ No newline at end of file |