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