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