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 support for colors in Markdown #86

Open
wants to merge 7 commits 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
17 changes: 17 additions & 0 deletions html/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ rndr_strikethrough(struct buf *ob, const struct buf *text, void *opaque)
return 1;
}

static int
rndr_coloredtext(struct buf *ob, const struct buf *text, const struct buf *color, void *opaque)
{
if (!text || !text->size || !color || !color->size || !color+text->size)
return 0;

BUFPUTSL(ob, "<span style=\"color:");
bufput(ob, color->data, color->size);
BUFPUTSL(ob, "\">");
bufput(ob, text->data, text->size);
BUFPUTSL(ob, "</span>");

return 1;
}

static int
rndr_double_emphasis(struct buf *ob, const struct buf *text, void *opaque)
{
Expand Down Expand Up @@ -734,6 +749,7 @@ sdhtml_toc_renderer(struct sd_callbacks *callbacks, struct html_renderopt *optio

NULL,
rndr_codespan,
rndr_coloredtext,
rndr_spoilerspan,
rndr_double_emphasis,
rndr_emphasis,
Expand Down Expand Up @@ -777,6 +793,7 @@ sdhtml_renderer(struct sd_callbacks *callbacks, struct html_renderopt *options,

rndr_autolink,
rndr_codespan,
rndr_coloredtext,
rndr_spoilerspan,
rndr_double_emphasis,
rndr_emphasis,
Expand Down
68 changes: 68 additions & 0 deletions src/markdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,48 @@ parse_spoilerspan(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_
return 0;
}

static size_t
parse_coloredtext(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size)
{
int (*render_method)(struct buf *ob, const struct buf *text, const struct buf *color, void *opaque);
size_t len;
size_t i = 0;
struct buf *work = 0;
struct buf *color = 0;
int r;

render_method = rndr->cb.coloredtext;

if (!render_method) return 0;

while (i < size) {
colorlen = find_emph_char(data + i, size - i, ':');
if (!colorlen) return 0;
i += colorlen
if (i < size && data[i] == ':' && data[i - 1] == ':') {
color = rndr_newbuf(rndr, BUFFER_SPAN);
parse_inline(color, rndr, data, i - 1);
}

len = find_emph_char(data + i, size - i, '<');
if (!len) return 0;
i += len;

if (i < size && data[i] == '<' && data[i - 1] == ':') {
work = rndr_newbuf(rndr, BUFFER_SPAN);
parse_inline(work, rndr, data, i - 1);
r = render_method(ob, work, color, rndr->opaque);
rndr_popbuf(rndr, BUFFER_SPAN);

if (!r) return 0;

return i + 1;
}
i++;
}
return 0;
}

/* char_emphasis • single and double emphasis parsing */
static size_t
char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t max_rewind, size_t max_lookbehind, size_t size)
Expand All @@ -643,6 +685,13 @@ char_emphasis(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t ma
return ret + 2;
}

if (size > 4 && c == '>' && data[1] == 'c' && data[2] == ':') {
if(_isspace(data[2]) || (ret = parse_coloredtext(ob, rndr, data + 2, size - 2)) == 0)
return 0;

return ret + 3;
}


if (size > 2 && data[1] != c) {
/* whitespace cannot follow an opening emphasis;
Expand Down Expand Up @@ -1480,6 +1529,25 @@ prefix_blockspoiler(uint8_t *data, size_t size)
return 0;
}

static size_t
prefix_coloredtext(uint8_t *data, size_t size)
{
size_t i = 0;
if (i < size && data[i] == ' ') i++;
if (i < size && data[i] == ' ') i++;
if (i < size && data[i] == ' ') i++;

if (i + 2 < size && data[i] == '>' && data[i + 1] == 'c' && data[i + 2] == ':') {
size_t coloredspan = find_emph_char(data + i + 2, size - i - 1, '<');
if (i + coloredspan < size && coloredspan > 0 && data[i + coloredspan] == ':')
return 0;

return i + 3;
}

return 0;
}

/* prefix_code • returns prefix length for block code*/
static size_t
prefix_code(uint8_t *data, size_t size)
Expand Down
1 change: 1 addition & 0 deletions src/markdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ struct sd_callbacks {
/* span level callbacks - NULL or return 0 prints the span verbatim */
int (*autolink)(struct buf *ob, const struct buf *link, enum mkd_autolink type, void *opaque);
int (*codespan)(struct buf *ob, const struct buf *text, void *opaque);
int (*coloredtext)(struct buf *ob, const struct buf *text, const struct buf *color, void *opaque);
int (*spoilerspan)(struct buf *ob, const struct buf *text, void *opaque);
int (*double_emphasis)(struct buf *ob, const struct buf *text, void *opaque);
int (*emphasis)(struct buf *ob, const struct buf *text, void *opaque);
Expand Down