Annotation of src/usr.bin/mandoc/html.c, Revision 1.15
1.15 ! schwarze 1: /* $Id: html.c,v 1.14 2010/07/31 21:43:07 schwarze Exp $ */
1.1 schwarze 2: /*
1.12 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 schwarze 4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/types.h>
18:
19: #include <assert.h>
1.3 schwarze 20: #include <ctype.h>
1.4 schwarze 21: #include <stdarg.h>
1.1 schwarze 22: #include <stdio.h>
23: #include <stdint.h>
24: #include <stdlib.h>
25: #include <string.h>
26: #include <unistd.h>
27:
1.9 schwarze 28: #include "mandoc.h"
1.1 schwarze 29: #include "out.h"
30: #include "chars.h"
31: #include "html.h"
32: #include "main.h"
33:
34: struct htmldata {
35: const char *name;
36: int flags;
37: #define HTML_CLRLINE (1 << 0)
38: #define HTML_NOSTACK (1 << 1)
1.6 schwarze 39: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.1 schwarze 40: };
41:
42: static const struct htmldata htmltags[TAG_MAX] = {
43: {"html", HTML_CLRLINE}, /* TAG_HTML */
44: {"head", HTML_CLRLINE}, /* TAG_HEAD */
45: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.6 schwarze 46: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1 schwarze 47: {"title", HTML_CLRLINE}, /* TAG_TITLE */
48: {"div", HTML_CLRLINE}, /* TAG_DIV */
49: {"h1", 0}, /* TAG_H1 */
50: {"h2", 0}, /* TAG_H2 */
51: {"span", 0}, /* TAG_SPAN */
1.8 schwarze 52: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6 schwarze 53: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1 schwarze 54: {"a", 0}, /* TAG_A */
55: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.6 schwarze 56: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1 schwarze 57: {"tr", HTML_CLRLINE}, /* TAG_TR */
58: {"td", HTML_CLRLINE}, /* TAG_TD */
59: {"li", HTML_CLRLINE}, /* TAG_LI */
60: {"ul", HTML_CLRLINE}, /* TAG_UL */
61: {"ol", HTML_CLRLINE}, /* TAG_OL */
62: };
63:
1.5 schwarze 64: static const char *const htmlfonts[HTMLFONT_MAX] = {
65: "roman",
66: "bold",
67: "italic"
68: };
69:
70: static const char *const htmlattrs[ATTR_MAX] = {
1.1 schwarze 71: "http-equiv",
72: "content",
73: "name",
74: "rel",
75: "href",
76: "type",
77: "media",
78: "class",
79: "style",
80: "width",
81: "valign",
82: "target",
83: "id",
1.3 schwarze 84: "summary",
1.1 schwarze 85: };
86:
1.13 schwarze 87: static void print_spec(struct html *, enum roffdeco,
88: const char *, size_t);
1.5 schwarze 89: static void print_res(struct html *, const char *, size_t);
90: static void print_ctag(struct html *, enum htmltag);
1.6 schwarze 91: static void print_doctype(struct html *);
92: static void print_xmltype(struct html *);
1.5 schwarze 93: static int print_encode(struct html *, const char *, int);
94: static void print_metaf(struct html *, enum roffdeco);
1.6 schwarze 95: static void print_attr(struct html *,
96: const char *, const char *);
97: static void *ml_alloc(char *, enum htmltype);
1.5 schwarze 98:
99:
1.6 schwarze 100: static void *
101: ml_alloc(char *outopts, enum htmltype type)
1.1 schwarze 102: {
103: struct html *h;
104: const char *toks[4];
105: char *v;
106:
107: toks[0] = "style";
108: toks[1] = "man";
109: toks[2] = "includes";
110: toks[3] = NULL;
111:
1.3 schwarze 112: h = calloc(1, sizeof(struct html));
113: if (NULL == h) {
114: perror(NULL);
1.15 ! schwarze 115: exit(MANDOCLEVEL_SYSERR);
1.3 schwarze 116: }
1.1 schwarze 117:
1.6 schwarze 118: h->type = type;
1.2 schwarze 119: h->tags.head = NULL;
120: h->ords.head = NULL;
1.3 schwarze 121: h->symtab = chars_init(CHARS_HTML);
1.1 schwarze 122:
123: while (outopts && *outopts)
124: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
125: case (0):
126: h->style = v;
127: break;
128: case (1):
129: h->base_man = v;
130: break;
131: case (2):
132: h->base_includes = v;
133: break;
134: default:
135: break;
136: }
137:
138: return(h);
139: }
140:
1.6 schwarze 141: void *
142: html_alloc(char *outopts)
143: {
144:
145: return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
146: }
147:
148:
149: void *
150: xhtml_alloc(char *outopts)
151: {
152:
153: return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
154: }
155:
1.1 schwarze 156:
157: void
158: html_free(void *p)
159: {
160: struct tag *tag;
161: struct ord *ord;
162: struct html *h;
163:
164: h = (struct html *)p;
165:
1.2 schwarze 166: while ((ord = h->ords.head) != NULL) {
167: h->ords.head = ord->next;
1.1 schwarze 168: free(ord);
169: }
170:
1.2 schwarze 171: while ((tag = h->tags.head) != NULL) {
172: h->tags.head = tag->next;
1.1 schwarze 173: free(tag);
174: }
175:
176: if (h->symtab)
177: chars_free(h->symtab);
178:
179: free(h);
180: }
181:
182:
183: void
184: print_gen_head(struct html *h)
185: {
186: struct htmlpair tag[4];
187:
188: tag[0].key = ATTR_HTTPEQUIV;
189: tag[0].val = "Content-Type";
190: tag[1].key = ATTR_CONTENT;
191: tag[1].val = "text/html; charset=utf-8";
192: print_otag(h, TAG_META, 2, tag);
193:
194: tag[0].key = ATTR_NAME;
195: tag[0].val = "resource-type";
196: tag[1].key = ATTR_CONTENT;
197: tag[1].val = "document";
198: print_otag(h, TAG_META, 2, tag);
199:
200: if (h->style) {
201: tag[0].key = ATTR_REL;
202: tag[0].val = "stylesheet";
203: tag[1].key = ATTR_HREF;
204: tag[1].val = h->style;
205: tag[2].key = ATTR_TYPE;
206: tag[2].val = "text/css";
207: tag[3].key = ATTR_MEDIA;
208: tag[3].val = "all";
209: print_otag(h, TAG_LINK, 4, tag);
210: }
211: }
212:
213:
214: static void
1.13 schwarze 215: print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
1.1 schwarze 216: {
1.13 schwarze 217: int cp;
1.1 schwarze 218: const char *rhs;
219: size_t sz;
220:
1.13 schwarze 221: if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
222: printf("&#%d;", cp);
223: return;
224: } else if (-1 == cp && DECO_SSPECIAL == d) {
225: fwrite(p, 1, len, stdout);
226: return;
227: } else if (-1 == cp)
228: return;
1.1 schwarze 229:
1.13 schwarze 230: if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
231: fwrite(rhs, 1, sz, stdout);
1.1 schwarze 232: }
233:
234:
235: static void
1.5 schwarze 236: print_res(struct html *h, const char *p, size_t len)
1.1 schwarze 237: {
1.13 schwarze 238: int cp;
1.1 schwarze 239: const char *rhs;
240: size_t sz;
241:
1.13 schwarze 242: if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
243: printf("&#%d;", cp);
244: return;
245: } else if (-1 == cp)
246: return;
1.1 schwarze 247:
1.13 schwarze 248: if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
249: fwrite(rhs, 1, sz, stdout);
1.1 schwarze 250: }
251:
252:
1.5 schwarze 253: struct tag *
254: print_ofont(struct html *h, enum htmlfont font)
1.1 schwarze 255: {
1.5 schwarze 256: struct htmlpair tag;
1.1 schwarze 257:
1.5 schwarze 258: h->metal = h->metac;
259: h->metac = font;
1.1 schwarze 260:
1.5 schwarze 261: /* FIXME: DECO_ROMAN should just close out preexisting. */
1.1 schwarze 262:
1.5 schwarze 263: if (h->metaf && h->tags.head == h->metaf)
264: print_tagq(h, h->metaf);
1.1 schwarze 265:
1.5 schwarze 266: PAIR_CLASS_INIT(&tag, htmlfonts[font]);
267: h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
268: return(h->metaf);
269: }
1.1 schwarze 270:
271:
1.5 schwarze 272: static void
273: print_metaf(struct html *h, enum roffdeco deco)
274: {
275: enum htmlfont font;
1.1 schwarze 276:
1.5 schwarze 277: switch (deco) {
278: case (DECO_PREVIOUS):
279: font = h->metal;
280: break;
281: case (DECO_ITALIC):
282: font = HTMLFONT_ITALIC;
283: break;
284: case (DECO_BOLD):
285: font = HTMLFONT_BOLD;
286: break;
287: case (DECO_ROMAN):
288: font = HTMLFONT_NONE;
289: break;
290: default:
291: abort();
292: /* NOTREACHED */
1.1 schwarze 293: }
294:
1.5 schwarze 295: (void)print_ofont(h, font);
1.1 schwarze 296: }
297:
298:
1.5 schwarze 299: static int
300: print_encode(struct html *h, const char *p, int norecurse)
1.1 schwarze 301: {
1.4 schwarze 302: size_t sz;
1.5 schwarze 303: int len, nospace;
304: const char *seq;
305: enum roffdeco deco;
1.9 schwarze 306: static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.5 schwarze 307:
308: nospace = 0;
1.1 schwarze 309:
310: for (; *p; p++) {
1.9 schwarze 311: sz = strcspn(p, rejs);
1.4 schwarze 312:
313: fwrite(p, 1, sz, stdout);
314: p += /* LINTED */
315: sz;
316:
1.5 schwarze 317: if ('<' == *p) {
318: printf("<");
319: continue;
320: } else if ('>' == *p) {
321: printf(">");
322: continue;
323: } else if ('&' == *p) {
324: printf("&");
1.1 schwarze 325: continue;
1.9 schwarze 326: } else if (ASCII_HYPH == *p) {
327: /*
328: * Note: "soft hyphens" aren't graphically
329: * displayed when not breaking the text; we want
330: * them to be displayed.
331: */
332: /*printf("­");*/
333: putchar('-');
334: continue;
1.4 schwarze 335: } else if ('\0' == *p)
336: break;
337:
1.5 schwarze 338: seq = ++p;
339: len = a2roffdeco(&deco, &seq, &sz);
340:
341: switch (deco) {
342: case (DECO_RESERVED):
343: print_res(h, seq, sz);
344: break;
1.13 schwarze 345: case (DECO_SSPECIAL):
346: /* FALLTHROUGH */
1.5 schwarze 347: case (DECO_SPECIAL):
1.13 schwarze 348: print_spec(h, deco, seq, sz);
1.5 schwarze 349: break;
350: case (DECO_PREVIOUS):
351: /* FALLTHROUGH */
352: case (DECO_BOLD):
353: /* FALLTHROUGH */
354: case (DECO_ITALIC):
355: /* FALLTHROUGH */
356: case (DECO_ROMAN):
357: if (norecurse)
358: break;
359: print_metaf(h, deco);
360: break;
361: default:
362: break;
363: }
364:
365: p += len - 1;
366:
367: if (DECO_NOSPACE == deco && '\0' == *(p + 1))
368: nospace = 1;
1.1 schwarze 369: }
1.5 schwarze 370:
371: return(nospace);
1.1 schwarze 372: }
373:
374:
1.6 schwarze 375: static void
376: print_attr(struct html *h, const char *key, const char *val)
377: {
378: printf(" %s=\"", key);
379: (void)print_encode(h, val, 1);
380: putchar('\"');
381: }
382:
383:
1.1 schwarze 384: struct tag *
385: print_otag(struct html *h, enum htmltag tag,
386: int sz, const struct htmlpair *p)
387: {
388: int i;
389: struct tag *t;
390:
1.6 schwarze 391: /* Push this tags onto the stack of open scopes. */
392:
1.1 schwarze 393: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.3 schwarze 394: t = malloc(sizeof(struct tag));
395: if (NULL == t) {
396: perror(NULL);
1.15 ! schwarze 397: exit(MANDOCLEVEL_SYSERR);
1.3 schwarze 398: }
1.1 schwarze 399: t->tag = tag;
1.2 schwarze 400: t->next = h->tags.head;
401: h->tags.head = t;
1.1 schwarze 402: } else
403: t = NULL;
404:
405: if ( ! (HTML_NOSPACE & h->flags))
1.12 schwarze 406: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
407: /* Manage keeps! */
408: if ( ! (HTML_KEEP & h->flags)) {
409: if (HTML_PREKEEP & h->flags)
410: h->flags |= HTML_KEEP;
411: putchar(' ');
412: } else
413: printf(" ");
414: }
1.1 schwarze 415:
1.13 schwarze 416: if ( ! (h->flags & HTML_NONOSPACE))
417: h->flags &= ~HTML_NOSPACE;
1.14 schwarze 418: else
419: h->flags |= HTML_NOSPACE;
1.13 schwarze 420:
1.6 schwarze 421: /* Print out the tag name and attributes. */
422:
1.1 schwarze 423: printf("<%s", htmltags[tag].name);
1.6 schwarze 424: for (i = 0; i < sz; i++)
425: print_attr(h, htmlattrs[p[i].key], p[i].val);
426:
427: /* Add non-overridable attributes. */
428:
429: if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
430: print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
431: print_attr(h, "xml:lang", "en");
432: print_attr(h, "lang", "en");
1.1 schwarze 433: }
1.6 schwarze 434:
435: /* Accomodate for XML "well-formed" singleton escaping. */
436:
437: if (HTML_AUTOCLOSE & htmltags[tag].flags)
438: switch (h->type) {
439: case (HTML_XHTML_1_0_STRICT):
440: putchar('/');
441: break;
442: default:
443: break;
444: }
445:
1.4 schwarze 446: putchar('>');
1.1 schwarze 447:
448: h->flags |= HTML_NOSPACE;
449: return(t);
450: }
451:
452:
453: static void
454: print_ctag(struct html *h, enum htmltag tag)
455: {
456:
457: printf("</%s>", htmltags[tag].name);
1.3 schwarze 458: if (HTML_CLRLINE & htmltags[tag].flags) {
1.1 schwarze 459: h->flags |= HTML_NOSPACE;
1.4 schwarze 460: putchar('\n');
1.5 schwarze 461: }
1.1 schwarze 462: }
463:
464:
465: void
1.6 schwarze 466: print_gen_decls(struct html *h)
467: {
468:
469: print_xmltype(h);
470: print_doctype(h);
471: }
472:
473:
474: static void
475: print_xmltype(struct html *h)
476: {
477:
1.9 schwarze 478: if (HTML_XHTML_1_0_STRICT == h->type)
479: printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.6 schwarze 480: }
481:
482:
483: static void
484: print_doctype(struct html *h)
1.1 schwarze 485: {
1.6 schwarze 486: const char *doctype;
487: const char *dtd;
488: const char *name;
489:
490: switch (h->type) {
491: case (HTML_HTML_4_01_STRICT):
492: name = "HTML";
493: doctype = "-//W3C//DTD HTML 4.01//EN";
494: dtd = "http://www.w3.org/TR/html4/strict.dtd";
495: break;
496: default:
497: name = "html";
498: doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
499: dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
500: break;
501: }
502:
503: printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
504: name, doctype, dtd);
1.1 schwarze 505: }
506:
507:
508: void
1.12 schwarze 509: print_text(struct html *h, const char *word)
1.1 schwarze 510: {
511:
1.12 schwarze 512: if (word[0] && '\0' == word[1])
513: switch (word[0]) {
1.1 schwarze 514: case('.'):
515: /* FALLTHROUGH */
516: case(','):
517: /* FALLTHROUGH */
518: case(';'):
519: /* FALLTHROUGH */
520: case(':'):
521: /* FALLTHROUGH */
522: case('?'):
523: /* FALLTHROUGH */
524: case('!'):
525: /* FALLTHROUGH */
526: case(')'):
527: /* FALLTHROUGH */
528: case(']'):
529: if ( ! (HTML_IGNDELIM & h->flags))
530: h->flags |= HTML_NOSPACE;
531: break;
532: default:
533: break;
534: }
535:
1.12 schwarze 536: if ( ! (HTML_NOSPACE & h->flags)) {
537: /* Manage keeps! */
538: if ( ! (HTML_KEEP & h->flags)) {
539: if (HTML_PREKEEP & h->flags)
540: h->flags |= HTML_KEEP;
541: putchar(' ');
542: } else
543: printf(" ");
544: }
1.1 schwarze 545:
1.12 schwarze 546: assert(word);
547: if ( ! print_encode(h, word, 0))
1.13 schwarze 548: if ( ! (h->flags & HTML_NONOSPACE))
549: h->flags &= ~HTML_NOSPACE;
1.1 schwarze 550:
1.8 schwarze 551: /*
552: * Note that we don't process the pipe: the parser sees it as
553: * punctuation, but we don't in terms of typography.
554: */
1.12 schwarze 555: if (word[0] && '\0' == word[1])
556: switch (word[0]) {
1.1 schwarze 557: case('('):
558: /* FALLTHROUGH */
559: case('['):
560: h->flags |= HTML_NOSPACE;
561: break;
562: default:
563: break;
564: }
565: }
566:
567:
568: void
569: print_tagq(struct html *h, const struct tag *until)
570: {
571: struct tag *tag;
572:
1.2 schwarze 573: while ((tag = h->tags.head) != NULL) {
1.5 schwarze 574: if (tag == h->metaf)
575: h->metaf = NULL;
1.1 schwarze 576: print_ctag(h, tag->tag);
1.2 schwarze 577: h->tags.head = tag->next;
1.1 schwarze 578: free(tag);
579: if (until && tag == until)
580: return;
581: }
582: }
583:
584:
585: void
586: print_stagq(struct html *h, const struct tag *suntil)
587: {
588: struct tag *tag;
589:
1.2 schwarze 590: while ((tag = h->tags.head) != NULL) {
1.1 schwarze 591: if (suntil && tag == suntil)
592: return;
1.5 schwarze 593: if (tag == h->metaf)
594: h->metaf = NULL;
1.1 schwarze 595: print_ctag(h, tag->tag);
1.2 schwarze 596: h->tags.head = tag->next;
1.1 schwarze 597: free(tag);
598: }
599: }
600:
601:
602: void
603: bufinit(struct html *h)
604: {
605:
606: h->buf[0] = '\0';
607: h->buflen = 0;
608: }
609:
610:
611: void
612: bufcat_style(struct html *h, const char *key, const char *val)
613: {
614:
615: bufcat(h, key);
616: bufncat(h, ":", 1);
617: bufcat(h, val);
618: bufncat(h, ";", 1);
619: }
620:
621:
622: void
623: bufcat(struct html *h, const char *p)
624: {
625:
626: bufncat(h, p, strlen(p));
627: }
628:
629:
630: void
631: buffmt(struct html *h, const char *fmt, ...)
632: {
633: va_list ap;
634:
635: va_start(ap, fmt);
636: (void)vsnprintf(h->buf + (int)h->buflen,
637: BUFSIZ - h->buflen - 1, fmt, ap);
638: va_end(ap);
639: h->buflen = strlen(h->buf);
640: }
641:
642:
643: void
644: bufncat(struct html *h, const char *p, size_t sz)
645: {
646:
647: if (h->buflen + sz > BUFSIZ - 1)
648: sz = BUFSIZ - 1 - h->buflen;
649:
650: (void)strncat(h->buf, p, sz);
651: h->buflen += sz;
652: }
653:
654:
655: void
656: buffmt_includes(struct html *h, const char *name)
657: {
658: const char *p, *pp;
659:
660: pp = h->base_includes;
661:
662: while (NULL != (p = strchr(pp, '%'))) {
663: bufncat(h, pp, (size_t)(p - pp));
664: switch (*(p + 1)) {
665: case('I'):
666: bufcat(h, name);
667: break;
668: default:
669: bufncat(h, p, 2);
670: break;
671: }
672: pp = p + 2;
673: }
674: if (pp)
675: bufcat(h, pp);
676: }
677:
678:
679: void
680: buffmt_man(struct html *h,
681: const char *name, const char *sec)
682: {
683: const char *p, *pp;
684:
685: pp = h->base_man;
686:
687: /* LINTED */
688: while (NULL != (p = strchr(pp, '%'))) {
689: bufncat(h, pp, (size_t)(p - pp));
690: switch (*(p + 1)) {
691: case('S'):
692: bufcat(h, sec ? sec : "1");
693: break;
694: case('N'):
695: buffmt(h, name);
696: break;
697: default:
698: bufncat(h, p, 2);
699: break;
700: }
701: pp = p + 2;
702: }
703: if (pp)
704: bufcat(h, pp);
705: }
706:
707:
708: void
709: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
710: {
711: double v;
712: const char *u;
713:
714: v = su->scale;
715:
716: switch (su->unit) {
717: case (SCALE_CM):
718: u = "cm";
719: break;
720: case (SCALE_IN):
721: u = "in";
722: break;
723: case (SCALE_PC):
724: u = "pc";
725: break;
726: case (SCALE_PT):
727: u = "pt";
728: break;
729: case (SCALE_EM):
730: u = "em";
731: break;
732: case (SCALE_MM):
733: if (0 == (v /= 100))
734: v = 1;
735: u = "em";
736: break;
737: case (SCALE_EN):
738: u = "ex";
739: break;
740: case (SCALE_BU):
741: u = "ex";
742: break;
743: case (SCALE_VS):
744: u = "em";
745: break;
746: default:
747: u = "ex";
748: break;
749: }
750:
1.11 schwarze 751: /*
752: * XXX: the CSS spec isn't clear as to which types accept
753: * integer or real numbers, so we just make them all decimals.
754: */
755: buffmt(h, "%s: %.2f%s;", p, v, u);
1.1 schwarze 756: }
757:
1.3 schwarze 758:
759: void
760: html_idcat(char *dst, const char *src, int sz)
761: {
762: int ssz;
763:
764: assert(sz);
765:
766: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
767:
768: for ( ; *dst != '\0' && sz; dst++, sz--)
769: /* Jump to end. */ ;
770:
771: assert(sz > 2);
772:
773: /* We can't start with a number (bah). */
774:
775: *dst++ = 'x';
776: *dst = '\0';
777: sz--;
778:
779: for ( ; *src != '\0' && sz > 1; src++) {
780: ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
781: sz -= ssz;
782: dst += ssz;
783: }
784: }