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