Changeset 3237 for libcaca


Ignore:
Timestamp:
Nov 3, 2008, 7:16:38 PM (11 years ago)
Author:
bsittler
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/codec/export.c

    r3236 r3237  
    383383     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
    384384     *          83 chars for ";font-weight..."
    385      *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
     385     *          up to 10 chars for "&#xxxxxxx;", far less for pure ASCII
    386386     *          7 chars for "</span>" */
    387     *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
     387    *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 10 + 7));
    388388    cur = data = malloc(*bytes);
    389389
    390390    /* HTML header */
    391     cur += sprintf(cur, "<html><head>\n");
     391
     392    cur += sprintf(cur, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n");
     393    cur += sprintf(cur, "   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
     394    cur += sprintf(cur, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
     395    cur += sprintf(cur, "<head>\n");
    392396    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
    393397                        caca_get_version());
    394398    cur += sprintf(cur, "</head><body>\n");
    395399
    396     cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
     400    cur += sprintf(cur, "<div style=\"%s\">\n",
    397401                        "font-family: monospace, fixed; font-weight: bold;");
    398402
     
    405409        {
    406410            cur += sprintf(cur, "<span style=\"");
    407             if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
     411            if(caca_attr_to_ansi_fg(lineattr[x]) != CACA_DEFAULT)
    408412                cur += sprintf(cur, ";color:#%.03x",
    409413                               caca_attr_to_rgb12_fg(lineattr[x]));
     
    427431                if(linechar[x + len] == CACA_MAGIC_FULLWIDTH)
    428432                    ;
    429                 else if(linechar[x + len] <= 0x00000020)
    430                     cur += sprintf(cur, "&nbsp;");
     433                else if((linechar[x + len] <= 0x00000020)
     434                        ||
     435                        ((linechar[x + len] >= 0x0000007f)
     436                         &&
     437                         (linechar[x + len] <= 0x000000a0)))
     438                {
     439                    /* Control characters and space converted to
     440                     * U+00A0 NO-BREAK SPACE, a.k.a. "&nbsp;" in HTML,
     441                     * but we use the equivalent numeric character
     442                     * reference &#160; so this will work in plain
     443                     * XHTML with no DTD too. */
     444                    cur += sprintf(cur, "&#160;");
     445                }
     446                else if(linechar[x + len] == '&')
     447                    cur += sprintf(cur, "&amp;");
     448                else if(linechar[x + len] == '<')
     449                    cur += sprintf(cur, "&lt;");
     450                else if(linechar[x + len] == '>')
     451                    cur += sprintf(cur, "&gt;");
     452                else if(linechar[x + len] == '\"')
     453                    cur += sprintf(cur, "&quot;");
     454                else if(linechar[x + len] == '\'')
     455                    cur += sprintf(cur, "&#39;");
    431456                else if(linechar[x + len] < 0x00000080)
    432457                    cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]);
     458                else if((linechar[x + len] <= 0x0010fffd)
     459                        &&
     460                        ((linechar[x + len] & 0x0000fffe) != 0x0000fffe)
     461                        &&
     462                        ((linechar[x + len] < 0x0000d800)
     463                         ||
     464                         (linechar[x + len] > 0x0000dfff)))
     465                    cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + len]);
    433466                else
    434                     cur += sprintf(cur, "&#%i;",
    435                                    (unsigned int)linechar[x + len]);
     467                    /* non-character codepoints become U+FFFD
     468                     * REPLACEMENT CHARACTER */
     469                    cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd);
    436470            }
    437471            cur += sprintf(cur, "</span>");
Note: See TracChangeset for help on using the changeset viewer.