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

Add a flag to not put paragraphs around html tags. #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 10 additions & 2 deletions snudown.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ static const unsigned int snudown_default_md_flags =
MKDEXT_STRIKETHROUGH |
MKDEXT_TABLES;

static const unsigned int snudown_wiki_md_flags =
MKDEXT_NO_INTRA_EMPHASIS |
MKDEXT_SUPERSCRIPT |
MKDEXT_AUTOLINK |
MKDEXT_STRIKETHROUGH |
MKDEXT_HTML_INLINE |
MKDEXT_TABLES;

static const unsigned int snudown_default_render_flags =
HTML_SKIP_HTML |
HTML_SKIP_IMAGES |
Expand Down Expand Up @@ -116,8 +124,8 @@ void init_default_renderer(PyObject *module) {

void init_wiki_renderer(PyObject *module) {
PyModule_AddIntConstant(module, "RENDERER_WIKI", RENDERER_WIKI);
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].main_renderer = make_custom_renderer(&wiki_state, snudown_wiki_render_flags, snudown_wiki_md_flags, 0);
sundown[RENDERER_WIKI].toc_renderer = make_custom_renderer(&wiki_toc_state, snudown_wiki_render_flags, snudown_wiki_md_flags, 1);
sundown[RENDERER_WIKI].state = &wiki_state;
sundown[RENDERER_WIKI].toc_state = &wiki_toc_state;
}
Expand Down
32 changes: 28 additions & 4 deletions src/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,13 @@ char_entity(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offs
return end;
}

static size_t is_html_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) {
enum mkd_autolink altype = MKDA_NOT_AUTOLINK;
size_t end = tag_length(data, size, &altype);

return ((end > 2) && (rndr->cb.raw_html_tag) && (!rndr->cb.autolink || altype == MKDA_NOT_AUTOLINK));
}

/* char_langle_tag • '<' when tags or autolinks are allowed */
static size_t
char_langle_tag(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset, size_t size)
Expand Down Expand Up @@ -1453,13 +1460,27 @@ parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t
static size_t
parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
{
size_t i = 0, end = 0;
size_t i = 0, end = 0, hit_tag;
int level = 0;
struct buf work = { data, 0, 0, 0 };

while (i < size) {
for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */;

hit_tag = 0;
for (end = i + 1; end < size && data[end - 1] != '\n'; end++) {
if(data[end - 1] == '<' && (rndr->ext_flags & MKDEXT_HTML_INLINE)) {
if(is_html_tag(ob, rndr, data + (end - 1), size - (end - 1))) {
end = end - 1;
i = end;
hit_tag = 1;
break;
}
}
}

if(hit_tag) {
break;
}

if (prefix_quote(data + i, end - i) != 0) {
end = i;
break;
Expand Down Expand Up @@ -2231,7 +2252,10 @@ parse_block(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size
else if (data[beg] == '<' && rndr->cb.blockhtml &&
(i = parse_htmlblock(ob, rndr, txt_data, end, 1)) != 0)
beg += i;


else if (data[beg] == '<' && (rndr->ext_flags & MKDEXT_HTML_INLINE) &&
(i = char_langle_tag(ob, rndr, txt_data, beg, end)))
beg += i;
else if ((i = is_empty(txt_data, end)) != 0)
beg += i;

Expand Down
1 change: 1 addition & 0 deletions src/markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum mkd_extensions {
MKDEXT_SPACE_HEADERS = (1 << 6),
MKDEXT_SUPERSCRIPT = (1 << 7),
MKDEXT_LAX_SPACING = (1 << 8),
MKDEXT_HTML_INLINE = (1 << 9)
};

/* sd_callbacks - functions for rendering parsed data */
Expand Down