8. HTML buffer callbacks

Instead of writing to a file, HTML is written to a buffer. This buffer is allocated/freed inside the request callback, and then bound to the org parser data.

New callbacks will be required for the orgparse routine, which will be defined below.

This is for now just copy-pasted from the weewiki codebase, with the bits commented out.

<<funcdefs>>=
void wws_orgparse_setup(orgparse *op);
<<functions>>=
<<orgparse_callbacks>>
void wws_orgparse_setup(orgparse *op)
{
    orgparse_init(op);
<<orgparse_html_setup>>
}

8.1. Header

Since

is only reserved for titles, make all the header sizes one level smaller.
<<orgparse_callbacks>>=
static void html_header(void *ud,
                        const char *h,
                        size_t sz,
                        int lvl)
{
    wws_parser_d *p;
    char tmp[16];
    int tmp_sz;
    p = ud;
    tmp_sz = sprintf(tmp, "\n<h%d>", lvl + 1);
    wws_buffer_write(p->buf, tmp, tmp_sz);
    wws_buffer_write(p->buf, h, sz);
    tmp_sz = sprintf(tmp, "</h%d>", lvl + 1);
    wws_buffer_write(p->buf, tmp, tmp_sz);
}
<<orgparse_html_setup>>=
orgparse_set_header(op, html_header);

8.2. Text

<<orgparse_callbacks>>=
static void html_text(void *ud,
                      const char *str,
                      size_t sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, str, sz);
}
<<orgparse_html_setup>>=
orgparse_set_text(op, html_text);

8.3. Bold

<<orgparse_callbacks>>=
static void html_bold(void *ud,
                      const char *str,
                      size_t sz)
{
    /* weewiki_export_d *ex; */
    /* FILE *fp; */
    /* ex = ud; */
    /* fp = ex->fp; */
    /* fprintf(fp, "<b>"); */
    /* fwrite(str, 1, sz, fp); */
    /* fprintf(fp, "</b>"); */
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<b>", 3);
    wws_buffer_write(p->buf, str, sz);
    wws_buffer_write(p->buf, "</b>", 4);
}
<<orgparse_html_setup>>=
orgparse_set_bold(op, html_bold);

8.4. Aux

<<orgparse_callbacks>>=
static void html_aux(void *ud,
                     const char *str,
                     size_t sz)
{
    wws_parser_d *p;
    p = ud;
    janet_dobytes(p->wws->env,
                  (const uint8_t *)str, sz,
                  NULL, NULL);
}
<<orgparse_html_setup>>=
orgparse_set_aux(op, html_aux);

8.5. Newline

<<orgparse_callbacks>>=
static void html_newline(void *ud,
                         const char *str,
                         size_t sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<br>\n", 6);
}
<<orgparse_html_setup>>=
orgparse_set_newline(op, html_newline);

8.6. Code

<<orgparse_callbacks>>=
static void html_code(void *ud,
                      const char *str,
                      size_t sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<code>", 6);
    wws_buffer_write(p->buf, str, sz);
    wws_buffer_write(p->buf, "</code>", 7);
}
<<orgparse_html_setup>>=
orgparse_set_code(op, html_code);

8.7. Code Block

<<orgparse_callbacks>>=
static void html_codeblock(void *ud,
                           const char *str,
                           size_t sz)
{

    wws_parser_d *p;
    size_t n;
    p = ud;
    wws_buffer_write(p->buf, "<pre><code>", 11);
    for (n = 0; n < sz; n++) {
        switch (str[n]) {
            case '<':
                wws_buffer_write(p->buf, "<", 4);
                break;
            case '>':
                wws_buffer_write(p->buf, ">", 4);
                break;
            default:
                wws_buffer_write(p->buf, &str[n], 1);
                break;
        }
    }
    wws_buffer_write(p->buf, "</pre></code>", 13);
}
<<orgparse_html_setup>>=
orgparse_set_codeblock(op, html_codeblock);

8.8. Name

Ignored for now.

<<orgparse_callbacks>>=
static void html_name(void *ud,
                           const char *str,
                           size_t sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<div><b><i><<", 19);
    wws_buffer_write(p->buf, str, sz);
    wws_buffer_write(p->buf, ">>=</i></b></div>", 23);
}
<<orgparse_html_setup>>=
orgparse_set_name(op, html_name);

8.9. Title

<<orgparse_callbacks>>=
static void html_title(void *ud,
                           const char *str,
                           size_t sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<title>", 7);
    wws_buffer_write(p->buf, str, sz);
    wws_buffer_write(p->buf, "</title>", 8);
    wws_buffer_write(p->buf, "<h1>", 4);
    wws_buffer_write(p->buf, str, sz);
    wws_buffer_write(p->buf, "</h1>", 5);
}
<<orgparse_html_setup>>=
orgparse_set_title(op, html_title);

8.10. Link

<<orgparse_callbacks>>=
static void html_link(void *ud,
                      const char *link,
                      size_t link_sz,
                      const char *name,
                      size_t name_sz)
{
    wws_parser_d *p;
    p = ud;
    wws_buffer_write(p->buf, "<a href=\"", 9);
    wws_buffer_write(p->buf, link, link_sz);
    wws_buffer_write(p->buf, "\">", 2);
    wws_buffer_write(p->buf, name, name_sz);
    wws_buffer_write(p->buf, "</a>", 4);
}
<<orgparse_html_setup>>=
orgparse_set_link(op, html_link);

8.11. Paragraph

<<orgparse_callbacks>>=
static void html_pgrph(void *ud, int mode)
{
    wws_parser_d *p;
    p = ud;
    if (mode) {
        wws_buffer_write(p->buf, "</p>", 4);
    } else {
        wws_buffer_write(p->buf, "<p>", 3);
    }
}
<<orgparse_html_setup>>=
orgparse_set_pgrph(op, html_pgrph);



prev | home | next