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