static int discount_link(struct buf *ob, struct buf *link, struct buf *title, struct buf *content, void *opaque) { if (!link) return rndr_link(ob, link, title, content, opaque); else if (link->size > 5 && !strncasecmp(link->data, "abbr:", 5)) { BUFPUTSL(ob, "<abbr title=\""); lus_attr_escape(ob, link->data + 5, link->size - 5); BUFPUTSL(ob, "\">"); bufput(ob, content->data, content->size); BUFPUTSL(ob, "</abbr>"); return 1; } else if (link->size > 6 && !strncasecmp(link->data, "class:", 6)) { BUFPUTSL(ob, "<span class=\""); lus_attr_escape(ob, link->data + 6, link->size - 6); BUFPUTSL(ob, "\">"); bufput(ob, content->data, content->size); BUFPUTSL(ob, "</span>"); return 1; } else if (link->size > 3 && !strncasecmp(link->data, "id:", 3)) { BUFPUTSL(ob, "<span id=\""); lus_attr_escape(ob, link->data + 3, link->size - 3); BUFPUTSL(ob, "\">"); bufput(ob, content->data, content->size); BUFPUTSL(ob, "</span>"); return 1; } else if (link->size > 4 && !strncasecmp(link->data, "raw:", 4)) { bufput(ob, link->data + 4, link->size - 4); return 1; } return rndr_link(ob, link, title, content, opaque); }
static int rndr_link(struct buf *ob, struct buf *link, struct buf *title, struct buf *content, void *opaque) { BUFPUTSL(ob, "<a href=\""); if (link && link->size) lus_attr_escape(ob, link->data, link->size); if (title && title->size) { BUFPUTSL(ob, "\" title=\""); lus_attr_escape(ob, title->data, title->size); } BUFPUTSL(ob, "\">"); if (content && content->size) bufput(ob, content->data, content->size); BUFPUTSL(ob, "</a>"); return 1; }
static int xhtml_image(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt, void *opaque) { if (!link || !link->size) return 0; BUFPUTSL(ob, "<img src=\""); lus_attr_escape(ob, link->data, link->size); BUFPUTSL(ob, "\" alt=\""); if (alt && alt->size) lus_attr_escape(ob, alt->data, alt->size); if (title && title->size) { BUFPUTSL(ob, "\" title=\""); lus_attr_escape(ob, title->data, title->size); } BUFPUTSL(ob, "\" />"); return 1; }
static int rndr_autolink(struct buf *ob, struct buf *link, enum mkd_autolink type, void *opaque) { if (!link || !link->size) return 0; BUFPUTSL(ob, "<a href=\""); if (type == MKDA_IMPLICIT_EMAIL) BUFPUTSL(ob, "mailto:"); lus_attr_escape(ob, link->data, link->size); BUFPUTSL(ob, "\">"); if (type == MKDA_EXPLICIT_EMAIL && link->size > 7) lus_attr_escape(ob, link->data + 7, link->size - 7); else lus_attr_escape(ob, link->data, link->size); BUFPUTSL(ob, "</a>"); return 1; }
static int discount_image(struct buf *ob, struct buf *link, struct buf *title, struct buf *alt, int xhtml) { if (!link || !link->size) return 0; BUFPUTSL(ob, "<img src=\""); if (!print_link_wxh(ob, link)) { lus_attr_escape(ob, link->data, link->size); bufputc(ob, '"'); } BUFPUTSL(ob, " alt=\""); if (alt && alt->size) lus_attr_escape(ob, alt->data, alt->size); if (title && title->size) { BUFPUTSL(ob, "\" title=\""); lus_attr_escape(ob, title->data, title->size); } bufputs(ob, xhtml ? "\" />" : "\">"); return 1; }
static int print_link_wxh(struct buf *ob, struct buf *link) { size_t eq, ex, end; if (link->size < 1) return 0; eq = link->size - 1; while (eq > 0 && (link->data[eq - 1] != ' ' || link->data[eq] != '=')) eq -= 1; if (!eq) return 0; ex = eq + 1; while (ex < link->size && link->data[ex] >= '0' && link->data[ex] <= '9') ex += 1; if (ex >= link->size || ex == eq + 1 || link->data[ex] != 'x') return 0; end = ex + 1; while (end < link->size && link->data[end] >= '0' && link->data[end] <= '9') end += 1; if (end == ex + 1) return 0; /* everything is fine, proceeding to actual printing */ lus_attr_escape(ob, link->data, eq - 1); BUFPUTSL(ob, "\" width="); bufput(ob, link->data + eq + 1, ex - eq - 1); BUFPUTSL(ob, " height="); bufput(ob, link->data + ex + 1, end - ex - 1); return 1; }
static int rndr_codespan(struct buf *ob, struct buf *text, void *opaque) { BUFPUTSL(ob, "<code>"); if (text) lus_attr_escape(ob, text->data, text->size); BUFPUTSL(ob, "</code>"); return 1; }
static void rndr_blockcode(struct buf *ob, struct buf *text, void *opaque) { if (ob->size) bufputc(ob, '\n'); BUFPUTSL(ob, "<pre><code>"); if (text) lus_attr_escape(ob, text->data, text->size); BUFPUTSL(ob, "</code></pre>\n"); }
static int rndr_raw_html(struct buf *ob, struct buf *text, void *opaque) { unsigned int flags = *(unsigned int *)opaque; int escape_html = 0; if (flags & RENDER_SKIP_HTML) escape_html = 1; else if ((flags & RENDER_SKIP_STYLE) != 0 && is_html_tag(text, "<style>")) escape_html = 1; else if ((flags & RENDER_SKIP_LINKS) != 0 && is_html_tag(text, "<a>")) escape_html = 1; else if ((flags & RENDER_SKIP_IMAGES) != 0 && is_html_tag(text, "<img>")) escape_html = 1; if (escape_html) lus_attr_escape(ob, text->data, text->size); else bufput(ob, text->data, text->size); return 1; }
static void rndr_normal_text(struct buf *ob, struct buf *text, void *opaque) { unsigned int flags = *(unsigned int *)opaque; if (!text) return; if (flags & RENDER_SMARTYPANTS) smartypants(ob, text); else lus_attr_escape(ob, text->data, text->size); }