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