Skip to content

Commit

Permalink
Experimental: Custom allocation of the HTML renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Aug 4, 2011
1 parent 67d7a1f commit e07028b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
6 changes: 4 additions & 2 deletions examples/sundown.c
Expand Up @@ -33,7 +33,10 @@ main(int argc, char **argv)
struct buf *ib, *ob;
int ret;
FILE *in = stdin;

struct mkd_renderer renderer;
struct html_renderopt options;

size_t i, iterations = 1;

/* opening the file if given from the command line */
Expand Down Expand Up @@ -62,9 +65,8 @@ main(int argc, char **argv)
for (i = 0; i < iterations; ++i) {
ob->size = 0;

sdhtml_renderer(&renderer, 0, NULL);
sdhtml_renderer(&renderer, &options, 0);
sd_markdown(ob, ib, &renderer, ~0);
sdhtml_free_renderer(&renderer);
}

/* writing the result to stdout */
Expand Down
61 changes: 31 additions & 30 deletions html/html.c
Expand Up @@ -25,17 +25,6 @@

#define USE_XHTML(opt) (opt->flags & HTML_USE_XHTML)

struct html_renderopt {
void *extra;

struct {
int header_count;
int current_level;
} toc_data;

unsigned int flags;
};

static inline void
put_scaped_char(struct buf *ob, char c)
{
Expand Down Expand Up @@ -122,7 +111,14 @@ rndr_autolink(struct buf *ob, struct buf *link, enum mkd_autolink type, void *op
if (type == MKDA_EMAIL)
BUFPUTSL(ob, "mailto:");
bufput(ob, link->data, link->size);
BUFPUTSL(ob, "\">");

if (options->link_attributes) {
bufputc(ob, '\"');
options->link_attributes(ob, link, opaque);
bufputc(ob, '>');
} else {
BUFPUTSL(ob, "\">");
}

/*
* Pretty printing: if we get an email address as
Expand Down Expand Up @@ -308,11 +304,23 @@ rndr_link(struct buf *ob, struct buf *link, struct buf *title, struct buf *conte
return 0;

BUFPUTSL(ob, "<a href=\"");
if (link && link->size) bufput(ob, link->data, link->size);

if (link && link->size)
bufput(ob, link->data, link->size);

if (title && title->size) {
BUFPUTSL(ob, "\" title=\"");
sdhtml_escape(ob, title->data, title->size); }
BUFPUTSL(ob, "\">");
sdhtml_escape(ob, title->data, title->size);
}

if (options->link_attributes) {
bufputc(ob, '\"');
options->link_attributes(ob, link, opaque);
bufputc(ob, '>');
} else {
BUFPUTSL(ob, "\">");
}

if (content && content->size) bufput(ob, content->data, content->size);
BUFPUTSL(ob, "</a>");
return 1;
Expand Down Expand Up @@ -558,7 +566,7 @@ toc_finalize(struct buf *ob, void *opaque)
}

void
sdhtml_toc_renderer(struct mkd_renderer *renderer, void *extra)
sdhtml_toc_renderer(struct mkd_renderer *renderer, struct html_renderopt *options)
{
static const struct mkd_renderer toc_render = {
NULL,
Expand Down Expand Up @@ -594,17 +602,17 @@ sdhtml_toc_renderer(struct mkd_renderer *renderer, void *extra)
NULL
};

struct html_renderopt *options;
options = calloc(1, sizeof(struct html_renderopt));
if (options == NULL)
options = calloc(1, sizeof(struct html_renderopt));

options->flags = HTML_TOC;
options->extra = extra;

memcpy(renderer, &toc_render, sizeof(struct mkd_renderer));
renderer->opaque = options;
}

void
sdhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags, void *extra)
sdhtml_renderer(struct mkd_renderer *renderer, struct html_renderopt *options, unsigned int render_flags)
{
static const struct mkd_renderer renderer_default = {
rndr_blockcode,
Expand Down Expand Up @@ -640,10 +648,10 @@ sdhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags, void *
NULL
};

struct html_renderopt *options;
options = calloc(1, sizeof(struct html_renderopt));
if (options == NULL)
options = calloc(1, sizeof(struct html_renderopt));

options->flags = render_flags;
options->extra = extra;

memcpy(renderer, &renderer_default, sizeof(struct mkd_renderer));
renderer->opaque = options;
Expand All @@ -662,10 +670,3 @@ sdhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags, void *
if (render_flags & HTML_GITHUB_BLOCKCODE)
renderer->blockcode = rndr_blockcode_github;
}

void
sdhtml_free_renderer(struct mkd_renderer *renderer)
{
free(renderer->opaque);
}

19 changes: 14 additions & 5 deletions html/html.h
Expand Up @@ -21,6 +21,18 @@
#include "buffer.h"
#include <stdlib.h>

struct html_renderopt {
struct {
int header_count;
int current_level;
} toc_data;

unsigned int flags;

/* extra callbacks */
void (*link_attributes)(struct buf *ob, struct buf *url, void *self);
};

typedef enum {
HTML_SKIP_HTML = (1 << 0),
HTML_SKIP_STYLE = (1 << 1),
Expand All @@ -47,13 +59,10 @@ int
sdhtml_tag(const char *tag_data, size_t tag_size, const char *tagname);

extern void
sdhtml_renderer(struct mkd_renderer *renderer, unsigned int render_flags, void *extra);

extern void
sdhtml_toc_renderer(struct mkd_renderer *renderer, void *extra);
sdhtml_renderer(struct mkd_renderer *renderer, struct html_renderopt *options_ptr, unsigned int render_flags);

extern void
sdhtml_free_renderer(struct mkd_renderer *renderer);
sdhtml_toc_renderer(struct mkd_renderer *renderer, struct html_renderopt *options_ptr);

extern void
sdhtml_smartypants(struct buf *ob, struct buf *text);
Expand Down

0 comments on commit e07028b

Please sign in to comment.