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 Pikchr support #90

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ OBJS = autolink.o \
libdiff.o \
nroff.o \
odt.o \
pikchr.o \
smartypants.o \
term.o \
tree.o \
Expand Down Expand Up @@ -82,6 +83,7 @@ SOURCES = autolink.c \
main.c \
nroff.c \
odt.c \
pikchr.c \
smartypants.c \
term.c \
tests.c \
Expand Down
38 changes: 19 additions & 19 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,13 +1003,13 @@ node_lcs(const struct lowdown_node *nold,
*/
static struct lowdown_node *
node_merge(const struct lowdown_node *nold,
const struct lowdown_node *nnew, struct merger *parms)
const struct lowdown_node *nnew, struct merger *params)
{
const struct xnode *xnew, *xold;
struct lowdown_node *n, *nn;
const struct lowdown_node *nnold;
const struct xmap *xoldmap = parms->xoldmap,
*xnewmap = parms->xnewmap;
const struct xmap *xoldmap = params->xoldmap,
*xnewmap = params->xnewmap;

/*
* Invariant: the current nodes are matched.
Expand All @@ -1023,7 +1023,7 @@ node_merge(const struct lowdown_node *nold,
assert(xold->match != NULL);
assert(xnew->match == xold->node);

if ((n = node_clone(nnew, parms->id++)) == NULL)
if ((n = node_clone(nnew, params->id++)) == NULL)
goto err;

/* Now walk through the children on both sides. */
Expand All @@ -1046,7 +1046,7 @@ node_merge(const struct lowdown_node *nold,
LOWDOWN_NORMAL_TEXT == nold->type)
break;
if ((nn = node_clonetree
(nold, &parms->id)) == NULL)
(nold, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand All @@ -1067,7 +1067,7 @@ node_merge(const struct lowdown_node *nold,
LOWDOWN_NORMAL_TEXT == nnew->type)
break;
if ((nn = node_clonetree
(nnew, &parms->id)) == NULL)
(nnew, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand All @@ -1086,7 +1086,7 @@ node_merge(const struct lowdown_node *nold,
xold->match == NULL &&
nnew->type == LOWDOWN_NORMAL_TEXT &&
xnew->match == NULL) {
if (!node_lcs(nold, nnew, n, &parms->id))
if (!node_lcs(nold, nnew, n, &params->id))
goto err;
nold = TAILQ_NEXT(nold, entries);
nnew = TAILQ_NEXT(nnew, entries);
Expand All @@ -1097,7 +1097,7 @@ node_merge(const struct lowdown_node *nold,
if (xold->match != NULL)
break;
if ((nn = node_clonetree
(nold, &parms->id)) == NULL)
(nold, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand All @@ -1110,7 +1110,7 @@ node_merge(const struct lowdown_node *nold,
if (xnew->match != NULL)
break;
if ((nn = node_clonetree
(nnew, &parms->id)) == NULL)
(nnew, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand Down Expand Up @@ -1152,7 +1152,7 @@ node_merge(const struct lowdown_node *nold,

if (nnold == NULL) {
if ((nn = node_clonetree
(nnew, &parms->id)) == NULL)
(nnew, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand All @@ -1168,7 +1168,7 @@ node_merge(const struct lowdown_node *nold,
if (xnew->node == xold->match)
break;
if ((nn = node_clonetree
(nold, &parms->id)) == NULL)
(nold, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand All @@ -1187,13 +1187,13 @@ node_merge(const struct lowdown_node *nold,
if (is_opaque(nnew)) {
assert(is_opaque(nold));
if ((nn = node_clonetree
(nnew, &parms->id)) == NULL)
(nnew, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
} else {
assert(!is_opaque(nold));
nn = node_merge(nold, nnew, parms);
nn = node_merge(nold, nnew, params);
if (nn == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
Expand All @@ -1207,7 +1207,7 @@ node_merge(const struct lowdown_node *nold,
/* Flush remaining old nodes. */

while (nold != NULL) {
if ((nn = node_clonetree (nold, &parms->id)) == NULL)
if ((nn = node_clonetree (nold, &params->id)) == NULL)
goto err;
TAILQ_INSERT_TAIL(&n->children, nn, entries);
nn->parent = n;
Expand Down Expand Up @@ -1395,7 +1395,7 @@ lowdown_diff(const struct lowdown_node *nold,
const struct lowdown_node *n, *nn;
struct lowdown_node *comp = NULL;
size_t i;
struct merger parms;
struct merger params;

memset(&xoldmap, 0, sizeof(struct xmap));
memset(&xnewmap, 0, sizeof(struct xmap));
Expand Down Expand Up @@ -1533,10 +1533,10 @@ lowdown_diff(const struct lowdown_node *nold,
* See "Phase 5", sec. 5.2.
*/

memset(&parms, 0, sizeof(struct merger));
parms.xoldmap = &xoldmap;
parms.xnewmap = &xnewmap;
comp = node_merge(nold, nnew, &parms);
memset(&params, 0, sizeof(struct merger));
params.xoldmap = &xoldmap;
params.xnewmap = &xnewmap;
comp = node_merge(nold, nnew, &params);

*maxn = xnewmap.maxid > xoldmap.maxid ?
xnewmap.maxid + 1 :
Expand Down
57 changes: 39 additions & 18 deletions html.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "lowdown.h"
#include "extern.h"
#include "pikchr.h"

/*
* Our internal state object.
Expand Down Expand Up @@ -117,18 +118,18 @@ escape_attr(struct lowdown_buf *ob, const struct lowdown_buf *in)

static int
rndr_autolink(struct lowdown_buf *ob,
const struct rndr_autolink *parm,
const struct rndr_autolink *param,
const struct html *st)
{

if (parm->link.size == 0)
if (param->link.size == 0)
return 1;

if (!HBUF_PUTSL(ob, "<a href=\""))
return 0;
if (parm->type == HALINK_EMAIL && !HBUF_PUTSL(ob, "mailto:"))
if (param->type == HALINK_EMAIL && !HBUF_PUTSL(ob, "mailto:"))
return 0;
if (!escape_href(ob, &parm->link, st))
if (!escape_href(ob, &param->link, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
Expand All @@ -139,13 +140,13 @@ rndr_autolink(struct lowdown_buf *ob,
* want to print the `mailto:` prefix
*/

if (hbuf_strprefix(&parm->link, "mailto:")) {
if (hbuf_strprefix(&param->link, "mailto:")) {
if (!escape_html(ob,
parm->link.data + 7,
parm->link.size - 7, st))
param->link.data + 7,
param->link.size - 7, st))
return 0;
} else {
if (!escape_htmlb(ob, &parm->link, st))
if (!escape_htmlb(ob, &param->link, st))
return 0;
}

Expand All @@ -154,25 +155,45 @@ rndr_autolink(struct lowdown_buf *ob,

static int
rndr_blockcode(struct lowdown_buf *ob,
const struct rndr_blockcode *parm,
const struct rndr_blockcode *param,
const struct html *st)
{
if (ob->size && !hbuf_putc(ob, '\n'))
return 0;

if (parm->lang.size) {
if (!HBUF_PUTSL(ob, "<pre><code class=\"language-"))
return 0;
if (!escape_href(ob, &parm->lang, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
if (param->lang.size) {
if (param->lang.size == 6 && strncmp(param->lang.data, "pikchr", 6) == 0) {
int res = 0;
int w, h;
char *rndr_pikchr = pikchr(param->text.data, "pikchr", PIKCHR_PLAINTEXT_ERRORS, &w, &h);
if(w < 0) {
if (rndr_pikchr == 0) {
fprintf(stderr, "pikchr() returns NULL. Out of memory?\n");
} else {
fprintf(stderr, "%s\n", rndr_pikchr);
}
}
else {
res = hbuf_printf(ob, "<div style='max-width:%d'>\n%s", w, rndr_pikchr);
if (res)
res = HBUF_PUTSL(ob, "</div>\n");
}
free(rndr_pikchr);
return res;
} else {
if (!HBUF_PUTSL(ob, "<pre><code class=\"language-"))
return 0;
if (!escape_href(ob, &param->lang, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
}
} else {
if (! HBUF_PUTSL(ob, "<pre><code>"))
return 0;
}

if (!escape_literal(ob, &parm->text, st))
if (!escape_literal(ob, &param->text, st))
return 0;
return HBUF_PUTSL(ob, "</code></pre>\n");
}
Expand Down Expand Up @@ -236,7 +257,7 @@ rndr_blockquote(struct lowdown_buf *ob,

static int
rndr_codespan(struct lowdown_buf *ob,
const struct rndr_codespan *param,
const struct rndr_codespan *param,
const struct html *st)
{

Expand Down
42 changes: 21 additions & 21 deletions odt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,20 +1053,20 @@ escape_attr(struct lowdown_buf *ob, const struct lowdown_buf *in)
*/
static int
rndr_autolink(struct lowdown_buf *ob,
const struct rndr_autolink *parm,
const struct rndr_autolink *param,
struct odt *st)
{

if (parm->link.size == 0)
if (param->link.size == 0)
return 1;

if (!HBUF_PUTSL(ob,
"<text:a xlink:type=\"simple\""
" text:style-name=\"Internet_20_Link\" xlink:href=\""))
return 0;
if (parm->type == HALINK_EMAIL && !HBUF_PUTSL(ob, "mailto:"))
if (param->type == HALINK_EMAIL && !HBUF_PUTSL(ob, "mailto:"))
return 0;
if (!escape_href(ob, &parm->link, st))
if (!escape_href(ob, &param->link, st))
return 0;
if (!HBUF_PUTSL(ob, "\">"))
return 0;
Expand All @@ -1077,13 +1077,13 @@ rndr_autolink(struct lowdown_buf *ob,
* want to print the `mailto:` prefix
*/

if (hbuf_strprefix(&parm->link, "mailto:")) {
if (hbuf_strprefix(&param->link, "mailto:")) {
if (!escape_html(ob,
parm->link.data + 7,
parm->link.size - 7, st))
param->link.data + 7,
param->link.size - 7, st))
return 0;
} else {
if (!escape_htmlb(ob, &parm->link, st))
if (!escape_htmlb(ob, &param->link, st))
return 0;
}

Expand All @@ -1095,7 +1095,7 @@ rndr_autolink(struct lowdown_buf *ob,
*/
static int
rndr_blockcode(struct lowdown_buf *ob,
const struct rndr_blockcode *parm,
const struct rndr_blockcode *param,
struct odt *st)
{
size_t i, j, sz, ssz;
Expand Down Expand Up @@ -1123,7 +1123,7 @@ rndr_blockcode(struct lowdown_buf *ob,
} else
s = &st->stys[i];

for (i = 0; i < parm->text.size; ) {
for (i = 0; i < param->text.size; ) {
if (!hbuf_printf(ob,
"<text:p text:style-name=\"%s\">", s->name))
return 0;
Expand All @@ -1135,32 +1135,32 @@ rndr_blockcode(struct lowdown_buf *ob,
* literal spaces.
*/

for (sz = 0, j = i; i < parm->text.size; i++, sz++) {
if (parm->text.data[i] == ' ' &&
i < parm->text.size - 1 &&
parm->text.data[i + 1] == ' ') {
for (sz = 0, j = i; i < param->text.size; i++, sz++) {
if (param->text.data[i] == ' ' &&
i < param->text.size - 1 &&
param->text.data[i + 1] == ' ') {
if (!hesc_html(ob,
&parm->text.data[j], sz, 1, 1, 1))
&param->text.data[j], sz, 1, 1, 1))
return 0;
sz = 0;
for (ssz = 0; i < parm->text.size;
for (ssz = 0; i < param->text.size;
i++, ssz++)
if (parm->text.data[i] != ' ')
if (param->text.data[i] != ' ')
break;
j = i;
if (!hbuf_printf(ob,
"<text:s text:c=\"%zu\"/>", ssz))
return 0;
}
if (i < parm->text.size &&
parm->text.data[i] == '\n')
if (i < param->text.size &&
param->text.data[i] == '\n')
break;
}
if (!hesc_html(ob, &parm->text.data[j], sz, 1, 1, 1))
if (!hesc_html(ob, &param->text.data[j], sz, 1, 1, 1))
return 0;
if (!HBUF_PUTSL(ob, "</text:p>\n"))
return 0;
if (i < parm->text.size)
if (i < param->text.size)
i++;
}

Expand Down
Loading