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