Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nolinks flag to skip links during rendering #80

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 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,13 @@ void init_wiki_renderer() {
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;
}

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

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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this repeated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repeated where?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly same code is in fuzzing/snudown-validator.c

I don't understand how fuzzer/validator works in this code base, yet, but that seems like a DRY violation?

Copy link
Author

@debuggio debuggio Mar 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely agree. But, at least it doesn't register modules, also there are some minor differences that may affect something. I asked Peter about it. His response: "snudown-validator is the snudown.c with a main() function added, so it can be run as a standalone program for fuzz-testing."
I thought about rewriting this part to use validator.c but because I'm not sure how to test it, I decided to leave it as is.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we just have a .h file that we include from fuzz and main ?

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