diff options
author | Xavier Mendez <jmendeth@gmail.com> | 2014-05-11 10:17:32 +0200 |
---|---|---|
committer | Xavier Mendez <jmendeth@gmail.com> | 2014-05-11 10:17:32 +0200 |
commit | af11a53df48eba936f0f24593fa1fa6aac0ef4d7 (patch) | |
tree | 79a8be131e1c1b759098084d48b805a5e2e4c89f | |
parent | 238c4d57cce10d33b05cf52a91fc62a09f31ffbb (diff) | |
parent | 523118e3a566bc4dd451f6aff2a03ee0763e4c70 (diff) | |
download | rust-hoedown-af11a53df48eba936f0f24593fa1fa6aac0ef4d7.tar.gz |
Merge pull request #72 from jmendeth/small-fixes
Small fixes
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | bin/hoedown.c | 10 | ||||
-rw-r--r-- | src/document.c | 26 | ||||
-rw-r--r-- | src/escape.c | 4 | ||||
-rw-r--r-- | src/html.c | 39 | ||||
-rw-r--r-- | src/html.h | 16 |
7 files changed, 30 insertions, 69 deletions
@@ -7,4 +7,3 @@ hoedown.exp hoedown.lib smartypants libhoedown.so* -.DS_Store @@ -34,8 +34,7 @@ Features all possible DOS attacks (stack overflows, out of memory situations, malformed Markdown syntax...). - We've worked very hard to make `Hoedown` never crash or run out of memory - under *any* input. + We've worked very hard to make `Hoedown` never leak or crash under *any* input. **Warning**: `Hoedown` doesn't validate or post-process the HTML in Markdown documents. Unless you use `HTML_ESCAPE` or `HTML_SKIP`, you should strongly consider using a diff --git a/bin/hoedown.c b/bin/hoedown.c index 960bb3e..49a22d9 100644 --- a/bin/hoedown.c +++ b/bin/hoedown.c @@ -58,7 +58,7 @@ static struct extension_info extensions_info[] = { {HOEDOWN_EXT_FENCED_CODE, "fenced-code", "Parse fenced code blocks."}, {HOEDOWN_EXT_FOOTNOTES, "footnotes", "Parse footnotes."}, - {HOEDOWN_EXT_AUTOLINK, "autolink", "Automatically turn URLs into links."}, + {HOEDOWN_EXT_AUTOLINK, "autolink", "Automatically turn safe URLs into links."}, {HOEDOWN_EXT_STRIKETHROUGH, "strikethrough", "Parse ~~stikethrough~~ spans."}, {HOEDOWN_EXT_UNDERLINE, "underline", "Parse _underline_ instead of emphasis."}, {HOEDOWN_EXT_HIGHLIGHT, "highlight", "Parse ==highlight== spans."}, @@ -74,15 +74,11 @@ static struct extension_info extensions_info[] = { static struct html_flag_info html_flags_info[] = { {HOEDOWN_HTML_SKIP_HTML, "skip-html", "Strip all HTML tags."}, - {HOEDOWN_HTML_SKIP_STYLE, "skip-style", "Strip <style> tags."}, - {HOEDOWN_HTML_SKIP_IMAGES, "skip-images", "Don't render images."}, - {HOEDOWN_HTML_SKIP_LINKS, "skip-links", "Don't render links."}, + {HOEDOWN_HTML_ESCAPE, "escape", "Escape all HTML."}, {HOEDOWN_HTML_EXPAND_TABS, "expand-tabs", "Expand tabs to spaces."}, {HOEDOWN_HTML_SAFELINK, "safelink", "Only allow links to safe protocols."}, - {HOEDOWN_HTML_TOC, "toc", "Produce links to the Table of Contents."}, {HOEDOWN_HTML_HARD_WRAP, "hard-wrap", "Render each linebreak as <br>."}, {HOEDOWN_HTML_USE_XHTML, "xhtml", "Render XHTML."}, - {HOEDOWN_HTML_ESCAPE, "escape", "Escape all HTML."}, }; static const char *category_prefix = "all-"; @@ -107,7 +103,7 @@ print_help(const char *basename) { /* main options */ printf("Main options:\n"); print_option('n', "max-nesting=N", "Maximum level of block nesting parsed. Default is " str(DEF_MAX_NESTING) "."); - print_option('t', "toc-level=N", "Maximum level for headers included in the TOC. Implies '--toc'."); + print_option('t', "toc-level=N", "Maximum level for headers included in the TOC. Zero disables TOC (the default)."); print_option( 0, "html", "Render (X)HTML. The default."); print_option( 0, "html-toc", "Render the Table of Contents in (X)HTML."); print_option( 0, "null", "Use a special \"null\" renderer that has no callbacks."); diff --git a/src/document.c b/src/document.c index 78d5527..ec7e45d 100644 --- a/src/document.c +++ b/src/document.c @@ -1322,30 +1322,30 @@ is_hrule(uint8_t *data, size_t size) } /* check if a line is a code fence; return the - * width of the code fence. if passed, width of + * end of the code fence. if passed, width of * the fence rule and character will be returned */ static size_t is_codefence(uint8_t *data, size_t size, size_t *width, uint8_t *chr) { - size_t i = 0, n = 0; + size_t i = 0, n = 1; uint8_t c; /* skipping initial spaces */ - if (size < 3) return 0; + if (size < 3) + return 0; + if (data[0] == ' ') { i++; if (data[1] == ' ') { i++; if (data[2] == ' ') { i++; } } } /* looking at the hrule uint8_t */ - if (i + 2 >= size || !(data[i] == '~' || data[i] == '`')) - return 0; - c = data[i]; + if (i + 2 >= size || !(c=='~' || c=='`')) + return 0; - /* the whole line must be the uint8_t or whitespace */ - while (i < size && data[i] == c) { - n++; i++; - } + /* the fence must be that same character */ + while (++i < size && data[i] == c) + ++n; if (n < 3) return 0; @@ -1688,22 +1688,22 @@ parse_fencedcode(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_ // search for end i++; text_start = i; - for (; (line_start = i) < size; i++) { + while ((line_start = i) < size) { while (i < size && data[i] != '\n') i++; w2 = is_codefence(data + line_start, i - line_start, &width2, &chr2); if (w == w2 && width == width2 && chr == chr2 && is_empty(data + (line_start+w), i - (line_start+w))) break; + + i++; } text.data = data + text_start; text.size = line_start - text_start; - // call callback if (doc->md.blockcode) doc->md.blockcode(ob, text.size ? &text : NULL, lang.size ? &lang : NULL, doc->md.opaque); - if (data[i] == '\n') i++; return i; } diff --git a/src/escape.c b/src/escape.c index b1039ed..749eb75 100644 --- a/src/escape.c +++ b/src/escape.c @@ -4,8 +4,6 @@ #include <stdio.h> #include <string.h> -#define ESCAPE_GROW_FACTOR(x) (((x) * 12) / 10) /* this is very scientific, yes */ - /* * The following characters will not be escaped: * @@ -71,7 +69,6 @@ hoedown_escape_href(hoedown_buffer *ob, const uint8_t *src, size_t size) return; } - hoedown_buffer_grow(ob, ESCAPE_GROW_FACTOR(size)); } hoedown_buffer_put(ob, src + org, i - org); @@ -173,7 +170,6 @@ hoedown_escape_html(hoedown_buffer *ob, const uint8_t *src, size_t size, int sec return; } - hoedown_buffer_grow(ob, ESCAPE_GROW_FACTOR(size)); } hoedown_buffer_put(ob, src + org, i - org); @@ -224,7 +224,7 @@ rndr_header(hoedown_buffer *ob, const hoedown_buffer *text, int level, void *opa if (ob->size) hoedown_buffer_putc(ob, '\n'); - if ((state->flags & HOEDOWN_HTML_TOC) && (level <= state->toc_data.nesting_level)) + if (level <= state->toc_data.nesting_level) hoedown_buffer_printf(ob, "<h%d id=\"toc_%d\">", level, state->toc_data.header_count++); else hoedown_buffer_printf(ob, "<h%d>", level); @@ -335,6 +335,8 @@ rndr_raw_block(hoedown_buffer *ob, const hoedown_buffer *text, void *opaque) { size_t org, sz; if (!text) return; + //FIXME: do we *really* need to trim the HTML? + //how does that make a difference? sz = text->size; while (sz > 0 && text->data[sz - 1] == '\n') sz--; org = 0; @@ -389,8 +391,8 @@ rndr_raw_html(hoedown_buffer *ob, const hoedown_buffer *text, void *opaque) { hoedown_html_renderer_state *state = 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. */ + /* ESCAPE overrides SKIP_HTML. It doesn't look to see if + * there are any valid tags, just escapes all of them. */ if((state->flags & HOEDOWN_HTML_ESCAPE) != 0) { escape_html(ob, text->data, text->size); return 1; @@ -399,18 +401,6 @@ rndr_raw_html(hoedown_buffer *ob, const hoedown_buffer *text, void *opaque) if ((state->flags & HOEDOWN_HTML_SKIP_HTML) != 0) return 1; - if ((state->flags & HOEDOWN_HTML_SKIP_STYLE) != 0 && - hoedown_html_is_tag(text->data, text->size, "style")) - return 1; - - if ((state->flags & HOEDOWN_HTML_SKIP_LINKS) != 0 && - hoedown_html_is_tag(text->data, text->size, "a")) - return 1; - - if ((state->flags & HOEDOWN_HTML_SKIP_IMAGES) != 0 && - hoedown_html_is_tag(text->data, text->size, "img")) - return 1; - hoedown_buffer_put(ob, text->data, text->size); return 1; } @@ -650,10 +640,7 @@ hoedown_html_toc_renderer_new(int nesting_level) memset(state, 0x0, sizeof(hoedown_html_renderer_state)); - if (nesting_level > 0) { - state->flags |= HOEDOWN_HTML_TOC; - state->toc_data.nesting_level = nesting_level; - } + state->toc_data.nesting_level = nesting_level; /* Prepare the renderer */ renderer = malloc(sizeof(hoedown_renderer)); @@ -722,11 +709,7 @@ hoedown_html_renderer_new(unsigned int render_flags, int nesting_level) memset(state, 0x0, sizeof(hoedown_html_renderer_state)); state->flags = render_flags; - - if (nesting_level > 0) { - state->flags |= HOEDOWN_HTML_TOC; - state->toc_data.nesting_level = nesting_level; - } + state->toc_data.nesting_level = nesting_level; /* Prepare the renderer */ renderer = malloc(sizeof(hoedown_renderer)); @@ -737,14 +720,6 @@ hoedown_html_renderer_new(unsigned int render_flags, int nesting_level) memcpy(renderer, &cb_default, sizeof(hoedown_renderer)); - if (render_flags & HOEDOWN_HTML_SKIP_IMAGES) - renderer->image = NULL; - - if (render_flags & HOEDOWN_HTML_SKIP_LINKS) { - renderer->link = NULL; - renderer->autolink = NULL; - } - if (render_flags & HOEDOWN_HTML_SKIP_HTML || render_flags & HOEDOWN_HTML_ESCAPE) renderer->blockhtml = NULL; @@ -13,16 +13,12 @@ extern "C" { typedef enum { HOEDOWN_HTML_SKIP_HTML = (1 << 0), - HOEDOWN_HTML_SKIP_STYLE = (1 << 1), - HOEDOWN_HTML_SKIP_IMAGES = (1 << 2), - HOEDOWN_HTML_SKIP_LINKS = (1 << 3), - HOEDOWN_HTML_EXPAND_TABS = (1 << 4), - HOEDOWN_HTML_SAFELINK = (1 << 5), - HOEDOWN_HTML_TOC = (1 << 6), - HOEDOWN_HTML_HARD_WRAP = (1 << 7), - HOEDOWN_HTML_USE_XHTML = (1 << 8), - HOEDOWN_HTML_ESCAPE = (1 << 9) -} hoedown_html_render_mode; + HOEDOWN_HTML_ESCAPE = (1 << 1), + HOEDOWN_HTML_EXPAND_TABS = (1 << 2), + HOEDOWN_HTML_SAFELINK = (1 << 3), + HOEDOWN_HTML_HARD_WRAP = (1 << 4), + HOEDOWN_HTML_USE_XHTML = (1 << 5), +} hoedown_html_flags; typedef enum { HOEDOWN_HTML_TAG_NONE = 0, |