From 176fcc8b35a44b76edad95f795f6d19a68cc6ecc Mon Sep 17 00:00:00 2001 From: debuggio Date: Tue, 7 Mar 2017 11:55:50 -0800 Subject: [PATCH] Added nolinks flag to skip links during rendering --- fuzzing/snudown-validator.c | 16 ++++++++++ snudown.c | 16 ++++++++++ test_snudown.py | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/fuzzing/snudown-validator.c b/fuzzing/snudown-validator.c index 153e1c4..0a6f89a 100644 --- a/fuzzing/snudown-validator.c +++ b/fuzzing/snudown-validator.c @@ -22,6 +22,7 @@ enum snudown_renderer_mode { RENDERER_USERTEXT = 0, RENDERER_WIKI, + RENDERER_USERTEXT_WITHOUTLINKS, RENDERER_COUNT }; @@ -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 | @@ -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) { @@ -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; diff --git a/snudown.c b/snudown.c index e268f66..be4ae27 100644 --- a/snudown.c +++ b/snudown.c @@ -10,6 +10,7 @@ enum snudown_renderer_mode { RENDERER_USERTEXT = 0, RENDERER_WIKI, + RENDERER_USERTEXT_WITHOUTLINKS, RENDERER_COUNT }; @@ -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 | @@ -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) { @@ -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); diff --git a/test_snudown.py b/test_snudown.py index fa9568f..d0d0cb3 100644 --- a/test_snudown.py +++ b/test_snudown.py @@ -422,6 +422,64 @@ '

\n', } + +no_links_cases = { + 'http://www.reddit.com': + '

http://www.reddit.com

\n', + + 'http://www.reddit.com/a\x00b': + '

http://www.reddit.com/ab

\n', + + 'ampersands http://www.google.com?test&blah': + '

ampersands http://www.google.com?test&blah

\n', + + 'www.http://example.com/': + '

www.http://example.com/

\n', + + 'foo@example.com': + '

foo@example.com

\n', + + '/u/test/m/test test': + '

/u/test/m/test test

\n', + + '/u/test': + '

/u/test

\n', + + '/r/not.cool': + '

/r/not.cool

\n', + + 'u/test': + '

u/test

\n', + + '/r/whatever: fork': + '

/r/whatever: fork

\n', + + '/r/t:timereddit': + '

/r/t:timereddit

\n', + + '/r/reddit.com': + '

/r/reddit.com

\n', + + 'r/not.cool': + '

r/not.cool

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

/r/very+clever+multireddit+reddit.com+t:fork+yay

\n', + + '/r/t:heatdeathoftheuniverse': + '

/r/t:heatdeathoftheuniverse

\n', + + '/r/all-minus-something': + '

/r/all-minus-something

\n', + + r'escaped \/r/test': + '

escaped /r/test

\n', + + 'Words words /r/test words': + '

Words words /r/test words

\n', +} + + class SnudownTestCase(unittest.TestCase): def __init__(self, renderer=snudown.RENDERER_USERTEXT): self.renderer = renderer @@ -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