Skip to content

Commit

Permalink
Added nolinks flag to skip links during rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggio committed Mar 7, 2017
1 parent 0d06958 commit 176fcc8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions fuzzing/snudown-validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
enum snudown_renderer_mode {
RENDERER_USERTEXT = 0,
RENDERER_WIKI,
RENDERER_USERTEXT_WITHOUTLINKS,
RENDERER_COUNT
};

Expand Down Expand Up @@ -60,6 +61,12 @@ static const unsigned int snudown_default_md_flags =
MKDEXT_STRIKETHROUGH |
MKDEXT_TABLES;

static const unsigned int snudown_default_md_flags_without_links =
MKDEXT_NO_INTRA_EMPHASIS |
MKDEXT_SUPERSCRIPT |
MKDEXT_STRIKETHROUGH |
MKDEXT_TABLES;

static const unsigned int snudown_default_render_flags =
HTML_SKIP_HTML |
HTML_SKIP_IMAGES |
Expand Down Expand Up @@ -129,6 +136,14 @@ void init_wiki_renderer() {
sundown[RENDERER_WIKI].toc_state = &wiki_toc_state;
}

void init_default_renderer_without_links(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_USERTEXT_WITHOUTLINKS", RENDERER_USERTEXT_WITHOUTLINKS);
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;
}

void
snudown_md(struct buf *ob, const uint8_t *document, size_t doc_size, int wiki_mode)
{
Expand Down Expand Up @@ -172,6 +187,7 @@ main(int argc, char **argv)
{
init_default_renderer();
init_wiki_renderer();
init_default_renderer_without_links(module);

struct buf *ib, *ob;
int size_read = 0, wiki_mode = 0, i = 0, have_errors = 0;
Expand Down
16 changes: 16 additions & 0 deletions snudown.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
enum snudown_renderer_mode {
RENDERER_USERTEXT = 0,
RENDERER_WIKI,
RENDERER_USERTEXT_WITHOUTLINKS,
RENDERER_COUNT
};

Expand Down Expand Up @@ -52,6 +53,12 @@ static const unsigned int snudown_default_md_flags =
MKDEXT_STRIKETHROUGH |
MKDEXT_TABLES;

static const unsigned int snudown_default_md_flags_without_links =
MKDEXT_NO_INTRA_EMPHASIS |
MKDEXT_SUPERSCRIPT |
MKDEXT_STRIKETHROUGH |
MKDEXT_TABLES;

static const unsigned int snudown_default_render_flags =
HTML_SKIP_HTML |
HTML_SKIP_IMAGES |
Expand Down Expand Up @@ -123,6 +130,14 @@ void init_wiki_renderer(PyObject *module) {
sundown[RENDERER_WIKI].toc_state = &wiki_toc_state;
}

void init_default_renderer_without_links(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_USERTEXT_WITHOUTLINKS", RENDERER_USERTEXT_WITHOUTLINKS);
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 PyObject *
snudown_md(PyObject *self, PyObject *args, PyObject *kwargs)
{
Expand Down Expand Up @@ -206,6 +221,7 @@ PyMODINIT_FUNC initsnudown(void)

init_default_renderer(module);
init_wiki_renderer(module);
init_default_renderer_without_links(module);

/* Version */
PyModule_AddStringConstant(module, "__version__", SNUDOWN_VERSION);
Expand Down
64 changes: 64 additions & 0 deletions test_snudown.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,64 @@
'<p><table scope="foo"></p>\n',
}


no_links_cases = {
'http://www.reddit.com':
'<p>http://www.reddit.com</p>\n',

'http://www.reddit.com/a\x00b':
'<p>http://www.reddit.com/ab</p>\n',

'ampersands http://www.google.com?test&blah':
'<p>ampersands http://www.google.com?test&amp;blah</p>\n',

'www.http://example.com/':
'<p>www.http://example.com/</p>\n',

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

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

'/u/test':
'<p>/u/test</p>\n',

'/r/not.cool':
'<p>/r/not.cool</p>\n',

'u/test':
'<p>u/test</p>\n',

'/r/whatever: fork':
'<p>/r/whatever: fork</p>\n',

'/r/t:timereddit':
'<p>/r/t:timereddit</p>\n',

'/r/reddit.com':
'<p>/r/reddit.com</p>\n',

'r/not.cool':
'<p>r/not.cool</p>\n',

'/r/very+clever+multireddit+reddit.com+t:fork+yay':
'<p>/r/very+clever+multireddit+reddit.com+t:fork+yay</p>\n',

'/r/t:heatdeathoftheuniverse':
'<p>/r/t:heatdeathoftheuniverse</p>\n',

'/r/all-minus-something':
'<p>/r/all-minus-something</p>\n',

r'escaped \/r/test':
'<p>escaped /r/test</p>\n',

'Words words /r/test words':
'<p>Words words /r/test words</p>\n',
}


class SnudownTestCase(unittest.TestCase):
def __init__(self, renderer=snudown.RENDERER_USERTEXT):
self.renderer = renderer
Expand Down Expand Up @@ -458,4 +516,10 @@ def test_snudown():
case.expected_output = expected_output
suite.addTest(case)

for input, expected_output in no_links_cases.iteritems():
case = SnudownTestCase(renderer=snudown.RENDERER_USERTEXT_WITHOUTLINKS)
case.input = input
case.expected_output = expected_output
suite.addTest(case)

return suite

0 comments on commit 176fcc8

Please sign in to comment.