Annotation of mandoc/html.c, Revision 1.96
1.96 ! kristaps 1: /* $Id: html.c,v 1.95 2010/01/30 08:42:20 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;
315: } else if ('&' == *p) {
316: printf("&");
1.34 kristaps 317: continue;
1.77 kristaps 318: } else if ('\0' == *p)
319: break;
320:
1.82 kristaps 321: seq = ++p;
322: len = a2roffdeco(&deco, &seq, &sz);
323:
324: switch (deco) {
325: case (DECO_RESERVED):
326: print_res(h, seq, sz);
327: break;
328: case (DECO_SPECIAL):
329: print_spec(h, seq, sz);
330: break;
1.90 kristaps 331: case (DECO_PREVIOUS):
332: /* FALLTHROUGH */
1.88 kristaps 333: case (DECO_BOLD):
334: /* FALLTHROUGH */
335: case (DECO_ITALIC):
336: /* FALLTHROUGH */
337: case (DECO_ROMAN):
338: if (norecurse)
339: break;
340: print_metaf(h, deco);
341: break;
1.82 kristaps 342: default:
343: break;
344: }
345:
346: p += len - 1;
1.84 kristaps 347:
348: if (DECO_NOSPACE == deco && '\0' == *(p + 1))
1.85 kristaps 349: nospace = 1;
1.32 kristaps 350: }
1.85 kristaps 351:
352: return(nospace);
1.14 kristaps 353: }
354:
355:
1.94 kristaps 356: static void
357: print_attr(struct html *h, const char *key, const char *val)
358: {
359: printf(" %s=\"", key);
360: (void)print_encode(h, val, 1);
361: putchar('\"');
362: }
363:
364:
1.51 kristaps 365: struct tag *
1.29 kristaps 366: print_otag(struct html *h, enum htmltag tag,
367: int sz, const struct htmlpair *p)
1.14 kristaps 368: {
1.29 kristaps 369: int i;
1.30 kristaps 370: struct tag *t;
371:
1.94 kristaps 372: /* Push this tags onto the stack of open scopes. */
373:
1.30 kristaps 374: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72 kristaps 375: t = malloc(sizeof(struct tag));
376: if (NULL == t) {
1.75 kristaps 377: perror(NULL);
1.72 kristaps 378: exit(EXIT_FAILURE);
379: }
1.30 kristaps 380: t->tag = tag;
1.66 kristaps 381: t->next = h->tags.head;
382: h->tags.head = t;
1.30 kristaps 383: } else
384: t = NULL;
1.29 kristaps 385:
386: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 387: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.78 kristaps 388: putchar(' ');
1.29 kristaps 389:
1.94 kristaps 390: /* Print out the tag name and attributes. */
391:
1.29 kristaps 392: printf("<%s", htmltags[tag].name);
1.94 kristaps 393: for (i = 0; i < sz; i++)
394: print_attr(h, htmlattrs[p[i].key], p[i].val);
395:
396: /* Add non-overridable attributes. */
397:
398: if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
399: print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
400: print_attr(h, "xml:lang", "en");
401: print_attr(h, "lang", "en");
1.29 kristaps 402: }
1.93 kristaps 403:
1.94 kristaps 404: /* Accomodate for XML "well-formed" singleton escaping. */
405:
1.93 kristaps 406: if (HTML_AUTOCLOSE & htmltags[tag].flags)
407: switch (h->type) {
408: case (HTML_XHTML_1_0_STRICT):
409: putchar('/');
410: break;
411: default:
412: break;
413: }
414:
1.78 kristaps 415: putchar('>');
1.14 kristaps 416:
1.29 kristaps 417: h->flags |= HTML_NOSPACE;
1.30 kristaps 418: return(t);
1.14 kristaps 419: }
420:
421:
1.29 kristaps 422: static void
423: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 424: {
425:
1.29 kristaps 426: printf("</%s>", htmltags[tag].name);
1.71 kristaps 427: if (HTML_CLRLINE & htmltags[tag].flags) {
1.29 kristaps 428: h->flags |= HTML_NOSPACE;
1.78 kristaps 429: putchar('\n');
1.87 kristaps 430: }
1.14 kristaps 431: }
432:
433:
1.51 kristaps 434: void
1.93 kristaps 435: print_gen_decls(struct html *h)
1.1 kristaps 436: {
1.93 kristaps 437:
438: print_xmltype(h);
439: print_doctype(h);
440: }
441:
442:
443: static void
444: print_xmltype(struct html *h)
445: {
446: const char *decl;
447:
448: switch (h->type) {
449: case (HTML_XHTML_1_0_STRICT):
450: decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
451: break;
452: default:
453: decl = NULL;
454: break;
455: }
456:
457: if (NULL == decl)
458: return;
459:
460: printf("%s\n", decl);
461: }
462:
463:
464: static void
465: print_doctype(struct html *h)
466: {
467: const char *doctype;
468: const char *dtd;
1.96 ! kristaps 469: const char *name;
1.93 kristaps 470:
471: switch (h->type) {
472: case (HTML_HTML_4_01_STRICT):
1.96 ! kristaps 473: name = "HTML";
1.93 kristaps 474: doctype = "-//W3C//DTD HTML 4.01//EN";
475: dtd = "http://www.w3.org/TR/html4/strict.dtd";
476: break;
477: default:
1.96 ! kristaps 478: name = "html";
1.93 kristaps 479: doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
480: dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
481: break;
482: }
483:
1.96 ! kristaps 484: printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
! 485: name, doctype, dtd);
1.1 kristaps 486: }
487:
488:
1.51 kristaps 489: void
1.29 kristaps 490: print_text(struct html *h, const char *p)
1.1 kristaps 491: {
492:
1.29 kristaps 493: if (*p && 0 == *(p + 1))
494: switch (*p) {
495: case('.'):
496: /* FALLTHROUGH */
497: case(','):
498: /* FALLTHROUGH */
499: case(';'):
500: /* FALLTHROUGH */
501: case(':'):
502: /* FALLTHROUGH */
503: case('?'):
504: /* FALLTHROUGH */
505: case('!'):
506: /* FALLTHROUGH */
507: case(')'):
508: /* FALLTHROUGH */
509: case(']'):
510: /* FALLTHROUGH */
511: case('}'):
1.52 kristaps 512: if ( ! (HTML_IGNDELIM & h->flags))
513: h->flags |= HTML_NOSPACE;
1.30 kristaps 514: break;
1.29 kristaps 515: default:
516: break;
517: }
1.1 kristaps 518:
1.29 kristaps 519: if ( ! (h->flags & HTML_NOSPACE))
1.78 kristaps 520: putchar(' ');
1.30 kristaps 521:
1.86 kristaps 522: assert(p);
1.88 kristaps 523: if ( ! print_encode(h, p, 0))
1.86 kristaps 524: h->flags &= ~HTML_NOSPACE;
1.8 kristaps 525:
1.29 kristaps 526: if (*p && 0 == *(p + 1))
527: switch (*p) {
528: case('('):
529: /* FALLTHROUGH */
530: case('['):
531: /* FALLTHROUGH */
532: case('{'):
533: h->flags |= HTML_NOSPACE;
1.30 kristaps 534: break;
1.29 kristaps 535: default:
536: break;
537: }
1.1 kristaps 538: }
1.30 kristaps 539:
540:
1.51 kristaps 541: void
1.30 kristaps 542: print_tagq(struct html *h, const struct tag *until)
543: {
544: struct tag *tag;
545:
1.66 kristaps 546: while ((tag = h->tags.head) != NULL) {
1.89 kristaps 547: if (tag == h->metaf)
548: h->metaf = NULL;
1.30 kristaps 549: print_ctag(h, tag->tag);
1.66 kristaps 550: h->tags.head = tag->next;
1.30 kristaps 551: free(tag);
552: if (until && tag == until)
553: return;
554: }
555: }
556:
557:
1.51 kristaps 558: void
1.30 kristaps 559: print_stagq(struct html *h, const struct tag *suntil)
560: {
561: struct tag *tag;
562:
1.66 kristaps 563: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 564: if (suntil && tag == suntil)
565: return;
1.89 kristaps 566: if (tag == h->metaf)
567: h->metaf = NULL;
1.30 kristaps 568: print_ctag(h, tag->tag);
1.66 kristaps 569: h->tags.head = tag->next;
1.30 kristaps 570: free(tag);
571: }
572: }
1.55 kristaps 573:
574:
575: void
576: bufinit(struct html *h)
577: {
578:
579: h->buf[0] = '\0';
580: h->buflen = 0;
581: }
582:
583:
584: void
1.58 kristaps 585: bufcat_style(struct html *h, const char *key, const char *val)
586: {
587:
588: bufcat(h, key);
589: bufncat(h, ":", 1);
590: bufcat(h, val);
591: bufncat(h, ";", 1);
592: }
593:
594:
595: void
1.55 kristaps 596: bufcat(struct html *h, const char *p)
597: {
598:
599: bufncat(h, p, strlen(p));
600: }
601:
602:
603: void
604: buffmt(struct html *h, const char *fmt, ...)
605: {
606: va_list ap;
607:
608: va_start(ap, fmt);
1.56 kristaps 609: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 610: BUFSIZ - h->buflen - 1, fmt, ap);
611: va_end(ap);
612: h->buflen = strlen(h->buf);
613: }
614:
615:
616: void
617: bufncat(struct html *h, const char *p, size_t sz)
618: {
619:
620: if (h->buflen + sz > BUFSIZ - 1)
621: sz = BUFSIZ - 1 - h->buflen;
622:
623: (void)strncat(h->buf, p, sz);
624: h->buflen += sz;
625: }
626:
627:
628: void
629: buffmt_includes(struct html *h, const char *name)
630: {
631: const char *p, *pp;
632:
633: pp = h->base_includes;
1.61 kristaps 634:
635: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 636: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 637: switch (*(p + 1)) {
638: case('I'):
639: bufcat(h, name);
640: break;
641: default:
642: bufncat(h, p, 2);
643: break;
644: }
645: pp = p + 2;
646: }
647: if (pp)
648: bufcat(h, pp);
649: }
650:
651:
652: void
653: buffmt_man(struct html *h,
654: const char *name, const char *sec)
655: {
656: const char *p, *pp;
657:
658: pp = h->base_man;
1.61 kristaps 659:
660: /* LINTED */
661: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 662: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 663: switch (*(p + 1)) {
664: case('S'):
1.58 kristaps 665: bufcat(h, sec ? sec : "1");
1.55 kristaps 666: break;
667: case('N'):
1.58 kristaps 668: buffmt(h, name);
1.55 kristaps 669: break;
670: default:
671: bufncat(h, p, 2);
672: break;
673: }
674: pp = p + 2;
675: }
676: if (pp)
677: bufcat(h, pp);
678: }
1.58 kristaps 679:
680:
681: void
682: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
683: {
1.62 kristaps 684: double v;
1.63 kristaps 685: const char *u;
1.58 kristaps 686:
687: v = su->scale;
688:
689: switch (su->unit) {
690: case (SCALE_CM):
691: u = "cm";
692: break;
693: case (SCALE_IN):
694: u = "in";
695: break;
696: case (SCALE_PC):
697: u = "pc";
698: break;
699: case (SCALE_PT):
700: u = "pt";
701: break;
1.59 kristaps 702: case (SCALE_EM):
703: u = "em";
704: break;
1.58 kristaps 705: case (SCALE_MM):
706: if (0 == (v /= 100))
707: v = 1;
708: u = "em";
709: break;
1.59 kristaps 710: case (SCALE_EN):
711: u = "ex";
712: break;
713: case (SCALE_BU):
714: u = "ex";
715: break;
1.58 kristaps 716: case (SCALE_VS):
717: u = "em";
718: break;
719: default:
720: u = "ex";
721: break;
722: }
723:
1.62 kristaps 724: if (su->pt)
725: buffmt(h, "%s: %f%s;", p, v, u);
726: else
727: /* LINTED */
728: buffmt(h, "%s: %d%s;", p, (int)v, u);
1.58 kristaps 729: }
1.65 kristaps 730:
1.68 kristaps 731:
732: void
1.70 kristaps 733: html_idcat(char *dst, const char *src, int sz)
1.68 kristaps 734: {
1.70 kristaps 735: int ssz;
1.68 kristaps 736:
737: assert(sz);
738:
739: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
740:
1.70 kristaps 741: for ( ; *dst != '\0' && sz; dst++, sz--)
1.68 kristaps 742: /* Jump to end. */ ;
743:
1.70 kristaps 744: assert(sz > 2);
1.68 kristaps 745:
1.70 kristaps 746: /* We can't start with a number (bah). */
1.68 kristaps 747:
1.70 kristaps 748: *dst++ = 'x';
1.68 kristaps 749: *dst = '\0';
1.70 kristaps 750: sz--;
751:
752: for ( ; *src != '\0' && sz > 1; src++) {
1.73 kristaps 753: ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70 kristaps 754: sz -= ssz;
755: dst += ssz;
756: }
1.68 kristaps 757: }
CVSweb