Skip to content

Commit

Permalink
Code review fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggio committed Mar 28, 2017
1 parent d6e2afb commit 90415a9
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 89 deletions.
4 changes: 2 additions & 2 deletions fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(HEADERS
../src/html_blocks.h
../src/html_entities.h
../src/markdown.h
../src/stack.h
../src/stack.h ../src/renderers.h
)
set(LIBRARY_SOURCES
../html/houdini_href_e.c
Expand All @@ -18,7 +18,7 @@ set(LIBRARY_SOURCES
../src/autolink.c
../src/buffer.c
../src/markdown.c
../src/stack.c
../src/stack.c ../src/renderers.c
${HEADERS}
)

Expand Down
48 changes: 9 additions & 39 deletions snudown.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,33 @@

#include "markdown.h"
#include "autolink.h"
#include "renderers.h"
#include "snudown.h"

#define SNUDOWN_VERSION "1.4.0"
#define SNUDOWN_VERSION "1.5.0"

/* The module doc strings */
PyDoc_STRVAR(snudown_module__doc__, "When does the narwhal bacon? At Sundown.");
PyDoc_STRVAR(snudown_md__doc__, "Render a Markdown document");

static void
snudown_link_attr(struct buf *ob, const struct buf *link, void *opaque)
{
struct snudown_renderopt *options = opaque;

if (options->nofollow)
BUFPUTSL(ob, " rel=\"nofollow\"");

if (options->target != NULL) {
BUFPUTSL(ob, " target=\"");
bufputs(ob, options->target);
bufputc(ob, '\"');
}
}

void init_default_renderer() {
sundown[RENDERER_USERTEXT].main_renderer = make_custom_renderer(&usertext_state, snudown_default_render_flags, snudown_default_md_flags, 0);
sundown[RENDERER_USERTEXT].toc_renderer = make_custom_renderer(&usertext_toc_state, snudown_default_render_flags, snudown_default_md_flags, 1);
sundown[RENDERER_USERTEXT].state = &usertext_state;
sundown[RENDERER_USERTEXT].toc_state = &usertext_toc_state;
}

void init_wiki_renderer() {
sundown[RENDERER_WIKI].main_renderer = make_custom_renderer(&wiki_state, snudown_wiki_render_flags, snudown_default_md_flags, 0);
sundown[RENDERER_WIKI].toc_renderer = make_custom_renderer(&wiki_toc_state, snudown_wiki_render_flags, snudown_default_md_flags, 1);
sundown[RENDERER_WIKI].state = &wiki_state;
sundown[RENDERER_WIKI].toc_state = &wiki_toc_state;
}

void init_default_renderer_without_links() {
sundown[RENDERER_USERTEXT_WITHOUTLINKS].main_renderer = make_custom_renderer(&usertext_state, snudown_default_render_flags, snudown_default_md_flags_without_links, 0);
sundown[RENDERER_USERTEXT_WITHOUTLINKS].toc_renderer = make_custom_renderer(&usertext_toc_state, snudown_default_render_flags, snudown_default_md_flags_without_links, 1);
sundown[RENDERER_USERTEXT_WITHOUTLINKS].state = &usertext_state;
sundown[RENDERER_USERTEXT_WITHOUTLINKS].toc_state = &usertext_toc_state;
}
static struct snudown_renderer sundown[RENDERER_COUNT];

void register_default_renderer(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_USERTEXT", RENDERER_USERTEXT);
init_default_renderer();
struct snudown_renderer *renderer = get_default_renderer();
sundown[RENDERER_USERTEXT] = *renderer;
}

void register_wiki_renderer(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_WIKI", RENDERER_WIKI);
init_wiki_renderer();
struct snudown_renderer *renderer = get_wiki_renderer();
sundown[RENDERER_WIKI] = *renderer;
}

void register_default_renderer_without_links(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_USERTEXT_WITHOUTLINKS", RENDERER_USERTEXT_WITHOUTLINKS);
init_default_renderer_without_links();
struct snudown_renderer *renderer = get_default_renderer_without_links();
sundown[RENDERER_USERTEXT_WITHOUTLINKS] = *renderer;
}

static PyObject *
Expand Down
5 changes: 2 additions & 3 deletions src/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -2530,16 +2530,15 @@ sd_markdown_new(
if (md->cb.linebreak)
md->active_char['\n'] = MD_CHAR_LINEBREAK;

if (md->cb.image || md->cb.link)
md->active_char['['] = MD_CHAR_LINK;

md->active_char['<'] = MD_CHAR_LANGLE;
md->active_char['\\'] = MD_CHAR_ESCAPE;
md->active_char['&'] = MD_CHAR_ENTITITY;

if (extensions & MKDEXT_AUTOLINK) {
if (!(extensions & MKDEXT_NO_EMAIL_AUTOLINK))
md->active_char['@'] = MD_CHAR_AUTOLINK_EMAIL;
if (md->cb.image || md->cb.link)
md->active_char['['] = MD_CHAR_LINK;
md->active_char[':'] = MD_CHAR_AUTOLINK_URL;
md->active_char['w'] = MD_CHAR_AUTOLINK_WWW;
md->active_char['/'] = MD_CHAR_AUTOLINK_SUBREDDIT_OR_USERNAME;
Expand Down
98 changes: 98 additions & 0 deletions src/renderers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "markdown.h"
#include "../html/html.h"
#include "renderers.h"

static struct module_state usertext_toc_state;
static struct module_state wiki_toc_state;
static struct module_state usertext_state;
static struct module_state wiki_state;

static struct sd_markdown *make_custom_renderer(struct module_state *state,
const unsigned int renderflags,
const unsigned int markdownflags,
int toc_renderer) {
if (toc_renderer) {
sdhtml_toc_renderer(&state->callbacks,
(struct html_renderopt *)&state->options);
}
else {
sdhtml_renderer(&state->callbacks,
(struct html_renderopt *)&state->options,
renderflags);
}

state->options.html.link_attributes = &snudown_link_attr;
state->options.html.html_element_whitelist = html_element_whitelist;
state->options.html.html_attr_whitelist = html_attr_whitelist;

return sd_markdown_new(
markdownflags,
16,
64,
&state->callbacks,
&state->options
);
}

struct snudown_renderer *get_default_renderer() {
struct snudown_renderer *sr = NULL;

sr = calloc(1, sizeof(struct snudown_renderer));

if (!sr)
return NULL;

sr->main_renderer = make_custom_renderer(&usertext_state, snudown_default_render_flags, snudown_default_md_flags, 0);
sr->toc_renderer = make_custom_renderer(&usertext_toc_state, snudown_default_render_flags, snudown_default_md_flags, 1);
sr->state = &usertext_state;
sr->toc_state = &usertext_toc_state;

return sr;
}

struct snudown_renderer *get_wiki_renderer() {
struct snudown_renderer *sr = NULL;

sr = calloc(1, sizeof(struct snudown_renderer));

if (!sr)
return NULL;

sr->main_renderer = make_custom_renderer(&wiki_state, snudown_wiki_render_flags, snudown_default_md_flags, 0);
sr->toc_renderer = make_custom_renderer(&wiki_toc_state, snudown_wiki_render_flags, snudown_default_md_flags, 1);
sr->state = &wiki_state;
sr->toc_state = &wiki_toc_state;

return sr;
}

struct snudown_renderer *get_default_renderer_without_links() {
struct snudown_renderer *sr = NULL;

sr = calloc(1, sizeof(struct snudown_renderer));

if (!sr)
return NULL;

sr->main_renderer = make_custom_renderer(&usertext_state, snudown_default_render_flags, snudown_default_md_flags_without_links, 0);
sr->toc_renderer = make_custom_renderer(&usertext_toc_state, snudown_default_render_flags, snudown_default_md_flags_without_links, 1);
sr->state = &usertext_state;
sr->toc_state = &usertext_toc_state;

return sr;
}

static void
snudown_link_attr(struct buf *ob, const struct buf *link, void *opaque)
{
struct snudown_renderopt *options = opaque;

if (options->nofollow)
BUFPUTSL(ob, " rel=\"nofollow\"");

if (options->target != NULL) {
BUFPUTSL(ob, " target=\"");
bufputs(ob, options->target);
bufputc(ob, '\"');
}
}
55 changes: 11 additions & 44 deletions snudown.h → src/renderers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* snudown.h - header for snudown */
/* renderers.h - header for renderers */

/*
* Copyright (c) 2017, Evgenii Timofeev
* Copyright (c) 2017, Reddit
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -15,7 +15,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "html/html.h"
#include "../html/html.h"

enum snudown_renderer_mode {
RENDERER_USERTEXT = 0,
Expand Down Expand Up @@ -72,47 +72,14 @@ static const unsigned int snudown_wiki_render_flags =
HTML_ESCAPE |
HTML_USE_XHTML;

static struct snudown_renderer sundown[RENDERER_COUNT];
static void
snudown_link_attr(struct buf *ob, const struct buf *link, void *opaque);

static struct module_state usertext_toc_state;
static struct module_state wiki_toc_state;
static struct module_state usertext_state;
static struct module_state wiki_state;
extern struct snudown_renderer *
get_default_renderer(void);

static void snudown_link_attr(struct buf *ob, const struct buf *link, void *opaque);
extern struct snudown_renderer *
get_wiki_renderer(void);

static struct sd_markdown* make_custom_renderer(struct module_state* state,
const unsigned int renderflags,
const unsigned int markdownflags,
int toc_renderer) {
if (toc_renderer) {
sdhtml_toc_renderer(&state->callbacks,
(struct html_renderopt *)&state->options);
}
else {
sdhtml_renderer(&state->callbacks,
(struct html_renderopt *)&state->options,
renderflags);
}

state->options.html.link_attributes = &snudown_link_attr;
state->options.html.html_element_whitelist = html_element_whitelist;
state->options.html.html_attr_whitelist = html_attr_whitelist;

return sd_markdown_new(
markdownflags,
16,
64,
&state->callbacks,
&state->options
);
}

extern void
init_default_renderer(void);

extern void
init_wiki_renderer(void);

extern void
init_default_renderer_without_links(void);
extern struct snudown_renderer *
get_default_renderer_without_links(void);
8 changes: 7 additions & 1 deletion test_snudown.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,13 @@
'<p>www.http://example.com/</p>\n',

'[email protected]':
'<p>[email protected]</p>\n',
'<p>[email protected]</p>\n',

'[foo](/u/bar)':
'<p>[foo](/u/bar)</p>\n',

'</u/foobar>':
'<p>&lt;/u/foobar&gt;</p>\n',

'/u/test/m/test test':
'<p>/u/test/m/test test</p>\n',
Expand Down

0 comments on commit 90415a9

Please sign in to comment.