Annotation of src/usr.bin/mandoc/html.c, Revision 1.45
1.45 ! schwarze 1: /* $OpenBSD$
1.1 schwarze 2: /*
1.42 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.33 schwarze 4: * Copyright (c) 2011, 2012, 2013, 2014 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.34 schwarze 30: #include "mandoc_aux.h"
1.26 schwarze 31: #include "libmandoc.h"
1.1 schwarze 32: #include "out.h"
33: #include "html.h"
34: #include "main.h"
35:
36: struct htmldata {
37: const char *name;
38: int flags;
39: #define HTML_CLRLINE (1 << 0)
40: #define HTML_NOSTACK (1 << 1)
1.6 schwarze 41: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.1 schwarze 42: };
43:
44: static const struct htmldata htmltags[TAG_MAX] = {
45: {"html", HTML_CLRLINE}, /* TAG_HTML */
46: {"head", HTML_CLRLINE}, /* TAG_HEAD */
47: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.6 schwarze 48: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.1 schwarze 49: {"title", HTML_CLRLINE}, /* TAG_TITLE */
50: {"div", HTML_CLRLINE}, /* TAG_DIV */
51: {"h1", 0}, /* TAG_H1 */
52: {"h2", 0}, /* TAG_H2 */
53: {"span", 0}, /* TAG_SPAN */
1.8 schwarze 54: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.6 schwarze 55: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.1 schwarze 56: {"a", 0}, /* TAG_A */
57: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.18 schwarze 58: {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
1.6 schwarze 59: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.1 schwarze 60: {"tr", HTML_CLRLINE}, /* TAG_TR */
61: {"td", HTML_CLRLINE}, /* TAG_TD */
62: {"li", HTML_CLRLINE}, /* TAG_LI */
63: {"ul", HTML_CLRLINE}, /* TAG_UL */
64: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.18 schwarze 65: {"dl", HTML_CLRLINE}, /* TAG_DL */
66: {"dt", HTML_CLRLINE}, /* TAG_DT */
67: {"dd", HTML_CLRLINE}, /* TAG_DD */
68: {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
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.42 schwarze 74: {"style", HTML_CLRLINE}, /* TAG_STYLE */
1.43 schwarze 75: {"math", HTML_CLRLINE}, /* TAG_MATH */
76: {"mrow", 0}, /* TAG_MROW */
77: {"mi", 0}, /* TAG_MI */
78: {"mo", 0}, /* TAG_MO */
79: {"msup", 0}, /* TAG_MSUP */
80: {"msub", 0}, /* TAG_MSUB */
81: {"msubsup", 0}, /* TAG_MSUBSUP */
82: {"mfrac", 0}, /* TAG_MFRAC */
83: {"msqrt", 0}, /* TAG_MSQRT */
84: {"mfenced", 0}, /* TAG_MFENCED */
85: {"mtable", 0}, /* TAG_MTABLE */
86: {"mtr", 0}, /* TAG_MTR */
87: {"mtd", 0}, /* TAG_MTD */
1.44 schwarze 88: {"munderover", 0}, /* TAG_MUNDEROVER */
89: {"munder", 0}, /* TAG_MUNDER*/
90: {"mover", 0}, /* TAG_MOVER*/
1.5 schwarze 91: };
92:
93: static const char *const htmlattrs[ATTR_MAX] = {
1.19 schwarze 94: "name", /* ATTR_NAME */
95: "rel", /* ATTR_REL */
96: "href", /* ATTR_HREF */
97: "type", /* ATTR_TYPE */
98: "media", /* ATTR_MEDIA */
99: "class", /* ATTR_CLASS */
100: "style", /* ATTR_STYLE */
101: "id", /* ATTR_ID */
1.22 schwarze 102: "colspan", /* ATTR_COLSPAN */
1.42 schwarze 103: "charset", /* ATTR_CHARSET */
1.43 schwarze 104: "open", /* ATTR_OPEN */
105: "close", /* ATTR_CLOSE */
1.45 ! schwarze 106: "mathvariant", /* ATTR_MATHVARIANT */
1.1 schwarze 107: };
108:
1.26 schwarze 109: static const char *const roffscales[SCALE_MAX] = {
110: "cm", /* SCALE_CM */
111: "in", /* SCALE_IN */
112: "pc", /* SCALE_PC */
113: "pt", /* SCALE_PT */
114: "em", /* SCALE_EM */
115: "em", /* SCALE_MM */
116: "ex", /* SCALE_EN */
117: "ex", /* SCALE_BU */
118: "em", /* SCALE_VS */
119: "ex", /* SCALE_FS */
120: };
1.5 schwarze 121:
1.26 schwarze 122: static void bufncat(struct html *, const char *, size_t);
123: static void print_ctag(struct html *, enum htmltag);
1.38 schwarze 124: static int print_escape(char);
1.26 schwarze 125: static int print_encode(struct html *, const char *, int);
126: static void print_metaf(struct html *, enum mandoc_esc);
127: static void print_attr(struct html *, const char *, const char *);
1.42 schwarze 128: static void *ml_alloc(char *);
1.5 schwarze 129:
1.35 schwarze 130:
1.6 schwarze 131: static void *
1.42 schwarze 132: ml_alloc(char *outopts)
1.1 schwarze 133: {
134: struct html *h;
1.29 schwarze 135: const char *toks[5];
1.1 schwarze 136: char *v;
137:
138: toks[0] = "style";
139: toks[1] = "man";
140: toks[2] = "includes";
1.29 schwarze 141: toks[3] = "fragment";
142: toks[4] = NULL;
1.1 schwarze 143:
1.24 schwarze 144: h = mandoc_calloc(1, sizeof(struct html));
1.1 schwarze 145:
1.2 schwarze 146: h->tags.head = NULL;
1.26 schwarze 147: h->symtab = mchars_alloc();
1.1 schwarze 148:
149: while (outopts && *outopts)
150: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.35 schwarze 151: case 0:
1.1 schwarze 152: h->style = v;
153: break;
1.35 schwarze 154: case 1:
1.1 schwarze 155: h->base_man = v;
156: break;
1.35 schwarze 157: case 2:
1.1 schwarze 158: h->base_includes = v;
1.29 schwarze 159: break;
1.35 schwarze 160: case 3:
1.29 schwarze 161: h->oflags |= HTML_FRAGMENT;
1.1 schwarze 162: break;
163: default:
164: break;
165: }
166:
167: return(h);
168: }
169:
1.6 schwarze 170: void *
171: html_alloc(char *outopts)
172: {
173:
1.42 schwarze 174: return(ml_alloc(outopts));
1.6 schwarze 175: }
176:
177: void *
178: xhtml_alloc(char *outopts)
179: {
180:
1.42 schwarze 181: return(ml_alloc(outopts));
1.6 schwarze 182: }
183:
1.1 schwarze 184: void
185: html_free(void *p)
186: {
187: struct tag *tag;
188: struct html *h;
189:
190: h = (struct html *)p;
191:
1.2 schwarze 192: while ((tag = h->tags.head) != NULL) {
1.35 schwarze 193: h->tags.head = tag->next;
1.1 schwarze 194: free(tag);
195: }
1.35 schwarze 196:
1.1 schwarze 197: if (h->symtab)
1.26 schwarze 198: mchars_free(h->symtab);
1.1 schwarze 199:
200: free(h);
201: }
202:
203: void
204: print_gen_head(struct html *h)
205: {
206: struct htmlpair tag[4];
1.42 schwarze 207: struct tag *t;
208:
209: tag[0].key = ATTR_CHARSET;
210: tag[0].val = "utf-8";
211: print_otag(h, TAG_META, 1, tag);
1.1 schwarze 212:
1.42 schwarze 213: /*
214: * Print a default style-sheet.
215: */
216: t = print_otag(h, TAG_STYLE, 0, NULL);
217: print_text(h, "table.head, table.foot { width: 100%; }\n"
218: "td.head-rtitle, td.foot-os { text-align: right; }\n"
219: "td.head-vol { text-align: center; }\n"
220: "table.foot td { width: 50%; }\n"
221: "table.head td { width: 33%; }\n"
222: "div.spacer { margin: 1em 0; }\n");
223: print_tagq(h, t);
1.1 schwarze 224:
225: if (h->style) {
226: tag[0].key = ATTR_REL;
227: tag[0].val = "stylesheet";
228: tag[1].key = ATTR_HREF;
229: tag[1].val = h->style;
230: tag[2].key = ATTR_TYPE;
231: tag[2].val = "text/css";
232: tag[3].key = ATTR_MEDIA;
233: tag[3].val = "all";
234: print_otag(h, TAG_LINK, 4, tag);
235: }
236: }
237:
1.5 schwarze 238: static void
1.26 schwarze 239: print_metaf(struct html *h, enum mandoc_esc deco)
1.5 schwarze 240: {
241: enum htmlfont font;
1.1 schwarze 242:
1.5 schwarze 243: switch (deco) {
1.35 schwarze 244: case ESCAPE_FONTPREV:
1.5 schwarze 245: font = h->metal;
246: break;
1.35 schwarze 247: case ESCAPE_FONTITALIC:
1.5 schwarze 248: font = HTMLFONT_ITALIC;
249: break;
1.35 schwarze 250: case ESCAPE_FONTBOLD:
1.5 schwarze 251: font = HTMLFONT_BOLD;
252: break;
1.35 schwarze 253: case ESCAPE_FONTBI:
1.31 schwarze 254: font = HTMLFONT_BI;
255: break;
1.35 schwarze 256: case ESCAPE_FONT:
1.26 schwarze 257: /* FALLTHROUGH */
1.35 schwarze 258: case ESCAPE_FONTROMAN:
1.5 schwarze 259: font = HTMLFONT_NONE;
260: break;
261: default:
262: abort();
263: /* NOTREACHED */
1.1 schwarze 264: }
265:
1.20 schwarze 266: if (h->metaf) {
267: print_tagq(h, h->metaf);
268: h->metaf = NULL;
269: }
270:
271: h->metal = h->metac;
272: h->metac = font;
273:
1.31 schwarze 274: switch (font) {
1.35 schwarze 275: case HTMLFONT_ITALIC:
1.31 schwarze 276: h->metaf = print_otag(h, TAG_I, 0, NULL);
277: break;
1.35 schwarze 278: case HTMLFONT_BOLD:
1.31 schwarze 279: h->metaf = print_otag(h, TAG_B, 0, NULL);
280: break;
1.35 schwarze 281: case HTMLFONT_BI:
1.31 schwarze 282: h->metaf = print_otag(h, TAG_B, 0, NULL);
283: print_otag(h, TAG_I, 0, NULL);
284: break;
285: default:
286: break;
287: }
1.1 schwarze 288: }
289:
1.26 schwarze 290: int
291: html_strlen(const char *cp)
292: {
1.30 schwarze 293: size_t rsz;
294: int skip, sz;
1.26 schwarze 295:
296: /*
297: * Account for escaped sequences within string length
298: * calculations. This follows the logic in term_strlen() as we
299: * must calculate the width of produced strings.
300: * Assume that characters are always width of "1". This is
301: * hacky, but it gets the job done for approximation of widths.
302: */
303:
304: sz = 0;
1.30 schwarze 305: skip = 0;
306: while (1) {
307: rsz = strcspn(cp, "\\");
308: if (rsz) {
309: cp += rsz;
310: if (skip) {
311: skip = 0;
312: rsz--;
313: }
314: sz += rsz;
315: }
316: if ('\0' == *cp)
317: break;
318: cp++;
319: switch (mandoc_escape(&cp, NULL, NULL)) {
1.35 schwarze 320: case ESCAPE_ERROR:
1.26 schwarze 321: return(sz);
1.35 schwarze 322: case ESCAPE_UNICODE:
1.26 schwarze 323: /* FALLTHROUGH */
1.35 schwarze 324: case ESCAPE_NUMBERED:
1.26 schwarze 325: /* FALLTHROUGH */
1.35 schwarze 326: case ESCAPE_SPECIAL:
1.30 schwarze 327: if (skip)
328: skip = 0;
329: else
330: sz++;
331: break;
1.35 schwarze 332: case ESCAPE_SKIPCHAR:
1.30 schwarze 333: skip = 1;
1.26 schwarze 334: break;
335: default:
336: break;
337: }
338: }
1.30 schwarze 339: return(sz);
1.26 schwarze 340: }
1.1 schwarze 341:
1.5 schwarze 342: static int
1.38 schwarze 343: print_escape(char c)
344: {
345:
346: switch (c) {
347: case '<':
348: printf("<");
349: break;
350: case '>':
351: printf(">");
352: break;
353: case '&':
354: printf("&");
355: break;
356: case '"':
357: printf(""");
358: break;
359: case ASCII_NBRSP:
360: putchar('-');
361: break;
362: case ASCII_HYPH:
363: putchar('-');
364: /* FALLTHROUGH */
365: case ASCII_BREAK:
366: break;
367: default:
368: return(0);
369: }
370: return(1);
371: }
372:
373: static int
1.5 schwarze 374: print_encode(struct html *h, const char *p, int norecurse)
1.1 schwarze 375: {
1.4 schwarze 376: size_t sz;
1.26 schwarze 377: int c, len, nospace;
1.5 schwarze 378: const char *seq;
1.26 schwarze 379: enum mandoc_esc esc;
1.37 schwarze 380: static const char rejs[9] = { '\\', '<', '>', '&', '"',
1.33 schwarze 381: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.5 schwarze 382:
383: nospace = 0;
1.1 schwarze 384:
1.26 schwarze 385: while ('\0' != *p) {
1.30 schwarze 386: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
387: h->flags &= ~HTML_SKIPCHAR;
388: p++;
389: continue;
390: }
391:
1.9 schwarze 392: sz = strcspn(p, rejs);
1.4 schwarze 393:
394: fwrite(p, 1, sz, stdout);
1.26 schwarze 395: p += (int)sz;
1.4 schwarze 396:
1.26 schwarze 397: if ('\0' == *p)
398: break;
399:
1.38 schwarze 400: if (print_escape(*p++))
1.33 schwarze 401: continue;
1.4 schwarze 402:
1.26 schwarze 403: esc = mandoc_escape(&p, &seq, &len);
404: if (ESCAPE_ERROR == esc)
405: break;
1.5 schwarze 406:
1.26 schwarze 407: switch (esc) {
1.35 schwarze 408: case ESCAPE_FONT:
1.30 schwarze 409: /* FALLTHROUGH */
1.35 schwarze 410: case ESCAPE_FONTPREV:
1.30 schwarze 411: /* FALLTHROUGH */
1.35 schwarze 412: case ESCAPE_FONTBOLD:
1.30 schwarze 413: /* FALLTHROUGH */
1.35 schwarze 414: case ESCAPE_FONTITALIC:
1.30 schwarze 415: /* FALLTHROUGH */
1.35 schwarze 416: case ESCAPE_FONTBI:
1.31 schwarze 417: /* FALLTHROUGH */
1.35 schwarze 418: case ESCAPE_FONTROMAN:
1.30 schwarze 419: if (0 == norecurse)
420: print_metaf(h, esc);
421: continue;
1.35 schwarze 422: case ESCAPE_SKIPCHAR:
1.30 schwarze 423: h->flags |= HTML_SKIPCHAR;
424: continue;
425: default:
426: break;
427: }
428:
429: if (h->flags & HTML_SKIPCHAR) {
430: h->flags &= ~HTML_SKIPCHAR;
431: continue;
432: }
433:
434: switch (esc) {
1.35 schwarze 435: case ESCAPE_UNICODE:
1.38 schwarze 436: /* Skip past "u" header. */
1.26 schwarze 437: c = mchars_num2uc(seq + 1, len - 1);
438: if ('\0' != c)
439: printf("&#x%x;", c);
440: break;
1.35 schwarze 441: case ESCAPE_NUMBERED:
1.26 schwarze 442: c = mchars_num2char(seq, len);
1.38 schwarze 443: if ( ! ('\0' == c || print_escape(c)))
1.26 schwarze 444: putchar(c);
445: break;
1.35 schwarze 446: case ESCAPE_SPECIAL:
1.26 schwarze 447: c = mchars_spec2cp(h->symtab, seq, len);
448: if (c > 0)
449: printf("&#%d;", c);
1.38 schwarze 450: else if (-1 == c && 1 == len &&
451: !print_escape(*seq))
1.26 schwarze 452: putchar((int)*seq);
453: break;
1.35 schwarze 454: case ESCAPE_NOSPACE:
1.26 schwarze 455: if ('\0' == *p)
456: nospace = 1;
1.5 schwarze 457: break;
458: default:
459: break;
460: }
1.1 schwarze 461: }
1.5 schwarze 462:
463: return(nospace);
1.1 schwarze 464: }
465:
1.6 schwarze 466: static void
467: print_attr(struct html *h, const char *key, const char *val)
468: {
469: printf(" %s=\"", key);
470: (void)print_encode(h, val, 1);
471: putchar('\"');
472: }
473:
1.1 schwarze 474: struct tag *
1.35 schwarze 475: print_otag(struct html *h, enum htmltag tag,
1.1 schwarze 476: int sz, const struct htmlpair *p)
477: {
478: int i;
479: struct tag *t;
480:
1.6 schwarze 481: /* Push this tags onto the stack of open scopes. */
482:
1.1 schwarze 483: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.24 schwarze 484: t = mandoc_malloc(sizeof(struct tag));
1.1 schwarze 485: t->tag = tag;
1.2 schwarze 486: t->next = h->tags.head;
487: h->tags.head = t;
1.1 schwarze 488: } else
489: t = NULL;
490:
491: if ( ! (HTML_NOSPACE & h->flags))
1.12 schwarze 492: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
493: /* Manage keeps! */
494: if ( ! (HTML_KEEP & h->flags)) {
495: if (HTML_PREKEEP & h->flags)
496: h->flags |= HTML_KEEP;
497: putchar(' ');
498: } else
499: printf(" ");
500: }
1.1 schwarze 501:
1.13 schwarze 502: if ( ! (h->flags & HTML_NONOSPACE))
503: h->flags &= ~HTML_NOSPACE;
1.14 schwarze 504: else
505: h->flags |= HTML_NOSPACE;
1.13 schwarze 506:
1.6 schwarze 507: /* Print out the tag name and attributes. */
508:
1.1 schwarze 509: printf("<%s", htmltags[tag].name);
1.6 schwarze 510: for (i = 0; i < sz; i++)
511: print_attr(h, htmlattrs[p[i].key], p[i].val);
512:
1.42 schwarze 513: /* Accommodate for "well-formed" singleton escaping. */
1.6 schwarze 514:
515: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.42 schwarze 516: putchar('/');
1.6 schwarze 517:
1.4 schwarze 518: putchar('>');
1.1 schwarze 519:
520: h->flags |= HTML_NOSPACE;
1.18 schwarze 521:
522: if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
523: putchar('\n');
524:
1.1 schwarze 525: return(t);
526: }
527:
528: static void
529: print_ctag(struct html *h, enum htmltag tag)
530: {
1.35 schwarze 531:
1.1 schwarze 532: printf("</%s>", htmltags[tag].name);
1.3 schwarze 533: if (HTML_CLRLINE & htmltags[tag].flags) {
1.1 schwarze 534: h->flags |= HTML_NOSPACE;
1.4 schwarze 535: putchar('\n');
1.35 schwarze 536: }
1.1 schwarze 537: }
538:
539: void
1.6 schwarze 540: print_gen_decls(struct html *h)
541: {
542:
1.42 schwarze 543: puts("<!DOCTYPE html>");
1.1 schwarze 544: }
545:
546: void
1.12 schwarze 547: print_text(struct html *h, const char *word)
1.1 schwarze 548: {
549:
1.12 schwarze 550: if ( ! (HTML_NOSPACE & h->flags)) {
551: /* Manage keeps! */
552: if ( ! (HTML_KEEP & h->flags)) {
553: if (HTML_PREKEEP & h->flags)
554: h->flags |= HTML_KEEP;
555: putchar(' ');
556: } else
557: printf(" ");
558: }
1.1 schwarze 559:
1.20 schwarze 560: assert(NULL == h->metaf);
1.31 schwarze 561: switch (h->metac) {
1.35 schwarze 562: case HTMLFONT_ITALIC:
1.31 schwarze 563: h->metaf = print_otag(h, TAG_I, 0, NULL);
564: break;
1.35 schwarze 565: case HTMLFONT_BOLD:
1.31 schwarze 566: h->metaf = print_otag(h, TAG_B, 0, NULL);
567: break;
1.35 schwarze 568: case HTMLFONT_BI:
1.31 schwarze 569: h->metaf = print_otag(h, TAG_B, 0, NULL);
570: print_otag(h, TAG_I, 0, NULL);
571: break;
572: default:
573: break;
574: }
1.20 schwarze 575:
1.12 schwarze 576: assert(word);
1.28 schwarze 577: if ( ! print_encode(h, word, 0)) {
1.13 schwarze 578: if ( ! (h->flags & HTML_NONOSPACE))
579: h->flags &= ~HTML_NOSPACE;
1.28 schwarze 580: } else
581: h->flags |= HTML_NOSPACE;
1.20 schwarze 582:
583: if (h->metaf) {
584: print_tagq(h, h->metaf);
585: h->metaf = NULL;
586: }
1.17 schwarze 587:
588: h->flags &= ~HTML_IGNDELIM;
1.1 schwarze 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.35 schwarze 597: /*
1.22 schwarze 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: void
614: print_stagq(struct html *h, const struct tag *suntil)
615: {
616: struct tag *tag;
617:
1.2 schwarze 618: while ((tag = h->tags.head) != NULL) {
1.1 schwarze 619: if (suntil && tag == suntil)
620: return;
1.35 schwarze 621: /*
1.22 schwarze 622: * Remember to close out and nullify the current
623: * meta-font and table, if applicable.
624: */
1.5 schwarze 625: if (tag == h->metaf)
626: h->metaf = NULL;
1.22 schwarze 627: if (tag == h->tblt)
628: h->tblt = NULL;
1.1 schwarze 629: print_ctag(h, tag->tag);
1.2 schwarze 630: h->tags.head = tag->next;
1.1 schwarze 631: free(tag);
632: }
633: }
1.42 schwarze 634:
635: void
636: print_paragraph(struct html *h)
637: {
638: struct tag *t;
639: struct htmlpair tag;
640:
641: PAIR_CLASS_INIT(&tag, "spacer");
642: t = print_otag(h, TAG_DIV, 1, &tag);
643: print_tagq(h, t);
644: }
645:
1.1 schwarze 646:
647: void
648: bufinit(struct html *h)
649: {
650:
651: h->buf[0] = '\0';
652: h->buflen = 0;
653: }
654:
655: void
656: bufcat_style(struct html *h, const char *key, const char *val)
657: {
658:
659: bufcat(h, key);
1.26 schwarze 660: bufcat(h, ":");
1.1 schwarze 661: bufcat(h, val);
1.26 schwarze 662: bufcat(h, ";");
1.1 schwarze 663: }
664:
665: void
666: bufcat(struct html *h, const char *p)
667: {
1.36 schwarze 668:
669: /*
670: * XXX This is broken and not easy to fix.
671: * When using the -Oincludes option, buffmt_includes()
672: * may pass in strings overrunning BUFSIZ, causing a crash.
673: */
1.1 schwarze 674:
1.26 schwarze 675: h->buflen = strlcat(h->buf, p, BUFSIZ);
676: assert(h->buflen < BUFSIZ);
1.1 schwarze 677: }
678:
679: void
1.26 schwarze 680: bufcat_fmt(struct html *h, const char *fmt, ...)
1.1 schwarze 681: {
682: va_list ap;
683:
684: va_start(ap, fmt);
1.35 schwarze 685: (void)vsnprintf(h->buf + (int)h->buflen,
686: BUFSIZ - h->buflen - 1, fmt, ap);
1.1 schwarze 687: va_end(ap);
688: h->buflen = strlen(h->buf);
689: }
690:
1.26 schwarze 691: static void
1.1 schwarze 692: bufncat(struct html *h, const char *p, size_t sz)
693: {
694:
1.26 schwarze 695: assert(h->buflen + sz + 1 < BUFSIZ);
696: strncat(h->buf, p, sz);
1.1 schwarze 697: h->buflen += sz;
698: }
699:
700: void
701: buffmt_includes(struct html *h, const char *name)
702: {
703: const char *p, *pp;
704:
705: pp = h->base_includes;
1.35 schwarze 706:
1.26 schwarze 707: bufinit(h);
1.1 schwarze 708: while (NULL != (p = strchr(pp, '%'))) {
709: bufncat(h, pp, (size_t)(p - pp));
710: switch (*(p + 1)) {
1.35 schwarze 711: case'I':
1.1 schwarze 712: bufcat(h, name);
713: break;
714: default:
715: bufncat(h, p, 2);
716: break;
717: }
718: pp = p + 2;
719: }
720: if (pp)
721: bufcat(h, pp);
722: }
723:
724: void
1.35 schwarze 725: buffmt_man(struct html *h, const char *name, const char *sec)
1.1 schwarze 726: {
727: const char *p, *pp;
728:
729: pp = h->base_man;
1.35 schwarze 730:
1.26 schwarze 731: bufinit(h);
1.1 schwarze 732: while (NULL != (p = strchr(pp, '%'))) {
733: bufncat(h, pp, (size_t)(p - pp));
734: switch (*(p + 1)) {
1.35 schwarze 735: case 'S':
1.1 schwarze 736: bufcat(h, sec ? sec : "1");
737: break;
1.35 schwarze 738: case 'N':
1.32 schwarze 739: bufcat_fmt(h, "%s", name);
1.1 schwarze 740: break;
741: default:
742: bufncat(h, p, 2);
743: break;
744: }
745: pp = p + 2;
746: }
747: if (pp)
748: bufcat(h, pp);
749: }
750:
751: void
752: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
753: {
754: double v;
755:
756: v = su->scale;
1.26 schwarze 757: if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
758: v = 1.0;
1.40 schwarze 759: else if (SCALE_BU == su->unit)
760: v /= 24.0;
1.1 schwarze 761:
1.26 schwarze 762: bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.1 schwarze 763: }
764:
1.3 schwarze 765: void
1.26 schwarze 766: bufcat_id(struct html *h, const char *src)
1.3 schwarze 767: {
768:
769: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
770:
1.26 schwarze 771: while ('\0' != *src)
772: bufcat_fmt(h, "%.2x", *src++);
1.3 schwarze 773: }