Browse Source

fix foreground-color and quoting bugs in the "html" exporter; also,

the output is now labelled as XHTML 1.0 Strict, since it does conform
to that DTD. also removed spurious cellpadding and cellspacing from
<div /> markup. also handle non-characters by conversion to
replacement character, and handle more control characters by
conversion to no-break space. finally, switch from &nbsp; to the
equivalent &#160; so that a non-validating XML parser (which is most
of them) can correctly parse this document.
tags/v0.99.beta17
Ben Wiley Sittler bsittler 16 years ago
parent
commit
d32f8eb7f8
1 changed files with 43 additions and 9 deletions
  1. +43
    -9
      caca/codec/export.c

+ 43
- 9
caca/codec/export.c View File

@@ -382,18 +382,22 @@ static void *export_html(caca_canvas_t const *cv, size_t *bytes)
* A line: 7 chars for "<br />\n"
* A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
* 83 chars for ";font-weight..."
* up to 9 chars for "&#xxxxxx;", far less for pure ASCII
* up to 10 chars for "&#xxxxxxx;", far less for pure ASCII
* 7 chars for "</span>" */
*bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
*bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 10 + 7));
cur = data = malloc(*bytes);

/* HTML header */
cur += sprintf(cur, "<html><head>\n");

cur += sprintf(cur, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n");
cur += sprintf(cur, " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
cur += sprintf(cur, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
cur += sprintf(cur, "<head>\n");
cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
caca_get_version());
cur += sprintf(cur, "</head><body>\n");

cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
cur += sprintf(cur, "<div style=\"%s\">\n",
"font-family: monospace, fixed; font-weight: bold;");

for(y = 0; y < cv->height; y++)
@@ -404,7 +408,7 @@ static void *export_html(caca_canvas_t const *cv, size_t *bytes)
for(x = 0; x < cv->width; x += len)
{
cur += sprintf(cur, "<span style=\"");
if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
if(caca_attr_to_ansi_fg(lineattr[x]) != CACA_DEFAULT)
cur += sprintf(cur, ";color:#%.03x",
caca_attr_to_rgb12_fg(lineattr[x]));
if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
@@ -426,13 +430,43 @@ static void *export_html(caca_canvas_t const *cv, size_t *bytes)
{
if(linechar[x + len] == CACA_MAGIC_FULLWIDTH)
;
else if(linechar[x + len] <= 0x00000020)
cur += sprintf(cur, "&nbsp;");
else if((linechar[x + len] <= 0x00000020)
||
((linechar[x + len] >= 0x0000007f)
&&
(linechar[x + len] <= 0x000000a0)))
{
/* Control characters and space converted to
* U+00A0 NO-BREAK SPACE, a.k.a. "&nbsp;" in HTML,
* but we use the equivalent numeric character
* reference &#160; so this will work in plain
* XHTML with no DTD too. */
cur += sprintf(cur, "&#160;");
}
else if(linechar[x + len] == '&')
cur += sprintf(cur, "&amp;");
else if(linechar[x + len] == '<')
cur += sprintf(cur, "&lt;");
else if(linechar[x + len] == '>')
cur += sprintf(cur, "&gt;");
else if(linechar[x + len] == '\"')
cur += sprintf(cur, "&quot;");
else if(linechar[x + len] == '\'')
cur += sprintf(cur, "&#39;");
else if(linechar[x + len] < 0x00000080)
cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]);
else if((linechar[x + len] <= 0x0010fffd)
&&
((linechar[x + len] & 0x0000fffe) != 0x0000fffe)
&&
((linechar[x + len] < 0x0000d800)
||
(linechar[x + len] > 0x0000dfff)))
cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + len]);
else
cur += sprintf(cur, "&#%i;",
(unsigned int)linechar[x + len]);
/* non-character codepoints become U+FFFD
* REPLACEMENT CHARACTER */
cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd);
}
cur += sprintf(cur, "</span>");
}


Loading…
Cancel
Save