Annotation of mandoc/html.c, Revision 1.141
1.141 ! kristaps 1: /* $Id: html.c,v 1.140 2011/05/17 10:48:06 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.138 kristaps 34: #include "libmandoc.h"
1.58 kristaps 35: #include "out.h"
1.51 kristaps 36: #include "html.h"
1.64 kristaps 37: #include "main.h"
1.63 kristaps 38:
1.29 kristaps 39: struct htmldata {
1.63 kristaps 40: const char *name;
1.29 kristaps 41: int flags;
1.30 kristaps 42: #define HTML_CLRLINE (1 << 0)
43: #define HTML_NOSTACK (1 << 1)
1.93 kristaps 44: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.29 kristaps 45: };
1.7 kristaps 46:
1.29 kristaps 47: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 48: {"html", HTML_CLRLINE}, /* TAG_HTML */
49: {"head", HTML_CLRLINE}, /* TAG_HEAD */
50: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.93 kristaps 51: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.33 kristaps 52: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 53: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 54: {"h1", 0}, /* TAG_H1 */
55: {"h2", 0}, /* TAG_H2 */
56: {"span", 0}, /* TAG_SPAN */
1.99 kristaps 57: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.93 kristaps 58: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.30 kristaps 59: {"a", 0}, /* TAG_A */
1.33 kristaps 60: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.114 kristaps 61: {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
1.93 kristaps 62: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.33 kristaps 63: {"tr", HTML_CLRLINE}, /* TAG_TR */
64: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 65: {"li", HTML_CLRLINE}, /* TAG_LI */
66: {"ul", HTML_CLRLINE}, /* TAG_UL */
67: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.114 kristaps 68: {"dl", HTML_CLRLINE}, /* TAG_DL */
69: {"dt", HTML_CLRLINE}, /* TAG_DT */
70: {"dd", HTML_CLRLINE}, /* TAG_DD */
1.115 kristaps 71: {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
1.116 kristaps 72: {"p", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_P */
1.118 kristaps 73: {"pre", HTML_CLRLINE }, /* TAG_PRE */
1.119 kristaps 74: {"b", 0 }, /* TAG_B */
1.120 kristaps 75: {"i", 0 }, /* TAG_I */
1.121 kristaps 76: {"code", 0 }, /* TAG_CODE */
1.122 kristaps 77: {"small", 0 }, /* TAG_SMALL */
1.90 kristaps 78: };
79:
1.82 kristaps 80: static const char *const htmlattrs[ATTR_MAX] = {
1.119 kristaps 81: "http-equiv", /* ATTR_HTTPEQUIV */
82: "content", /* ATTR_CONTENT */
83: "name", /* ATTR_NAME */
84: "rel", /* ATTR_REL */
85: "href", /* ATTR_HREF */
86: "type", /* ATTR_TYPE */
87: "media", /* ATTR_MEDIA */
88: "class", /* ATTR_CLASS */
89: "style", /* ATTR_STYLE */
90: "width", /* ATTR_WIDTH */
91: "id", /* ATTR_ID */
92: "summary", /* ATTR_SUMMARY */
93: "align", /* ATTR_ALIGN */
1.125 kristaps 94: "colspan", /* ATTR_COLSPAN */
1.29 kristaps 95: };
1.10 kristaps 96:
1.140 kristaps 97: static const char *const roffscales[SCALE_MAX] = {
98: "cm", /* SCALE_CM */
99: "in", /* SCALE_IN */
100: "pc", /* SCALE_PC */
101: "pt", /* SCALE_PT */
102: "em", /* SCALE_EM */
103: "em", /* SCALE_MM */
104: "ex", /* SCALE_EN */
105: "ex", /* SCALE_BU */
106: "em", /* SCALE_VS */
107: "ex", /* SCALE_FS */
108: };
109:
1.141 ! kristaps 110: static void bufncat(struct html *, const char *, size_t);
! 111: static void print_spec(struct html *, const char *, size_t);
! 112: static void print_res(struct html *, const char *, size_t);
! 113: static void print_ctag(struct html *, enum htmltag);
! 114: static int print_encode(struct html *, const char *, int);
! 115: static void print_metaf(struct html *, enum mandoc_esc);
! 116: static void print_attr(struct html *, const char *, const char *);
! 117: static void *ml_alloc(char *, enum htmltype);
1.82 kristaps 118:
1.93 kristaps 119: static void *
120: ml_alloc(char *outopts, enum htmltype type)
1.10 kristaps 121: {
1.30 kristaps 122: struct html *h;
1.63 kristaps 123: const char *toks[4];
124: char *v;
1.43 kristaps 125:
126: toks[0] = "style";
1.53 kristaps 127: toks[1] = "man";
1.54 kristaps 128: toks[2] = "includes";
129: toks[3] = NULL;
1.30 kristaps 130:
1.128 kristaps 131: h = mandoc_calloc(1, sizeof(struct html));
1.10 kristaps 132:
1.93 kristaps 133: h->type = type;
1.66 kristaps 134: h->tags.head = NULL;
1.136 kristaps 135: h->symtab = mchars_alloc();
1.41 kristaps 136:
1.47 kristaps 137: while (outopts && *outopts)
1.63 kristaps 138: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43 kristaps 139: case (0):
140: h->style = v;
141: break;
142: case (1):
1.53 kristaps 143: h->base_man = v;
1.43 kristaps 144: break;
1.54 kristaps 145: case (2):
146: h->base_includes = v;
147: break;
1.43 kristaps 148: default:
149: break;
150: }
151:
1.30 kristaps 152: return(h);
1.29 kristaps 153: }
1.10 kristaps 154:
1.93 kristaps 155: void *
156: html_alloc(char *outopts)
157: {
158:
159: return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
160: }
161:
162:
163: void *
164: xhtml_alloc(char *outopts)
165: {
166:
167: return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
168: }
169:
1.33 kristaps 170:
1.29 kristaps 171: void
172: html_free(void *p)
173: {
1.30 kristaps 174: struct tag *tag;
175: struct html *h;
176:
177: h = (struct html *)p;
1.37 kristaps 178:
1.66 kristaps 179: while ((tag = h->tags.head) != NULL) {
180: h->tags.head = tag->next;
1.30 kristaps 181: free(tag);
182: }
1.36 kristaps 183:
184: if (h->symtab)
1.133 kristaps 185: mchars_free(h->symtab);
1.53 kristaps 186:
1.30 kristaps 187: free(h);
1.10 kristaps 188: }
1.2 kristaps 189:
1.33 kristaps 190:
1.51 kristaps 191: void
1.29 kristaps 192: print_gen_head(struct html *h)
193: {
1.41 kristaps 194: struct htmlpair tag[4];
195:
196: tag[0].key = ATTR_HTTPEQUIV;
197: tag[0].val = "Content-Type";
198: tag[1].key = ATTR_CONTENT;
199: tag[1].val = "text/html; charset=utf-8";
200: print_otag(h, TAG_META, 2, tag);
201:
202: tag[0].key = ATTR_NAME;
203: tag[0].val = "resource-type";
204: tag[1].key = ATTR_CONTENT;
205: tag[1].val = "document";
206: print_otag(h, TAG_META, 2, tag);
207:
208: if (h->style) {
209: tag[0].key = ATTR_REL;
210: tag[0].val = "stylesheet";
211: tag[1].key = ATTR_HREF;
212: tag[1].val = h->style;
213: tag[2].key = ATTR_TYPE;
214: tag[2].val = "text/css";
215: tag[3].key = ATTR_MEDIA;
216: tag[3].val = "all";
217: print_otag(h, TAG_LINK, 4, tag);
218: }
1.4 kristaps 219: }
220:
1.126 schwarze 221: static void
1.132 kristaps 222: print_spec(struct html *h, const char *p, size_t len)
1.32 kristaps 223: {
1.107 kristaps 224: int cp;
1.32 kristaps 225: const char *rhs;
226: size_t sz;
227:
1.133 kristaps 228: if ((cp = mchars_spec2cp(h->symtab, p, len)) > 0) {
1.107 kristaps 229: printf("&#%d;", cp);
230: return;
1.132 kristaps 231: } else if (-1 == cp && 1 == len) {
1.108 kristaps 232: fwrite(p, 1, len, stdout);
233: return;
1.107 kristaps 234: } else if (-1 == cp)
235: return;
1.32 kristaps 236:
1.133 kristaps 237: if (NULL != (rhs = mchars_spec2str(h->symtab, p, len, &sz)))
1.107 kristaps 238: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 239: }
240:
1.33 kristaps 241:
1.32 kristaps 242: static void
1.83 kristaps 243: print_res(struct html *h, const char *p, size_t len)
1.32 kristaps 244: {
1.107 kristaps 245: int cp;
1.32 kristaps 246: const char *rhs;
247: size_t sz;
248:
1.133 kristaps 249: if ((cp = mchars_res2cp(h->symtab, p, len)) > 0) {
1.107 kristaps 250: printf("&#%d;", cp);
251: return;
252: } else if (-1 == cp)
253: return;
1.32 kristaps 254:
1.133 kristaps 255: if (NULL != (rhs = mchars_res2str(h->symtab, p, len, &sz)))
1.107 kristaps 256: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 257: }
258:
1.33 kristaps 259:
1.88 kristaps 260: static void
1.132 kristaps 261: print_metaf(struct html *h, enum mandoc_esc deco)
1.88 kristaps 262: {
1.90 kristaps 263: enum htmlfont font;
1.88 kristaps 264:
265: switch (deco) {
1.132 kristaps 266: case (ESCAPE_FONTPREV):
1.90 kristaps 267: font = h->metal;
1.88 kristaps 268: break;
1.132 kristaps 269: case (ESCAPE_FONTITALIC):
1.90 kristaps 270: font = HTMLFONT_ITALIC;
271: break;
1.132 kristaps 272: case (ESCAPE_FONTBOLD):
1.90 kristaps 273: font = HTMLFONT_BOLD;
1.88 kristaps 274: break;
1.132 kristaps 275: case (ESCAPE_FONTROMAN):
1.90 kristaps 276: font = HTMLFONT_NONE;
1.88 kristaps 277: break;
278: default:
279: abort();
280: /* NOTREACHED */
281: }
282:
1.122 kristaps 283: if (h->metaf) {
284: print_tagq(h, h->metaf);
285: h->metaf = NULL;
286: }
287:
288: h->metal = h->metac;
289: h->metac = font;
290:
291: if (HTMLFONT_NONE != font)
292: h->metaf = HTMLFONT_BOLD == font ?
293: print_otag(h, TAG_B, 0, NULL) :
294: print_otag(h, TAG_I, 0, NULL);
1.88 kristaps 295: }
296:
1.138 kristaps 297: int
298: html_strlen(const char *cp)
299: {
300: int ssz, sz;
301: const char *seq, *p;
302:
303: /*
304: * Account for escaped sequences within string length
305: * calculations. This follows the logic in term_strlen() as we
306: * must calculate the width of produced strings.
307: * Assume that characters are always width of "1". This is
308: * hacky, but it gets the job done for approximation of widths.
309: */
310:
311: sz = 0;
312: while (NULL != (p = strchr(cp, '\\'))) {
313: sz += (int)(p - cp);
314: ++cp;
315: switch (mandoc_escape(&cp, &seq, &ssz)) {
316: case (ESCAPE_ERROR):
317: return(sz);
1.139 kristaps 318: case (ESCAPE_NUMBERED):
319: /* FALLTHROUGH */
1.138 kristaps 320: case (ESCAPE_PREDEF):
1.139 kristaps 321: /* FALLTHROUGH */
1.138 kristaps 322: case (ESCAPE_SPECIAL):
323: sz++;
324: break;
325: default:
326: break;
327: }
328: }
329:
330: assert(sz >= 0);
331: return(sz + strlen(cp));
332: }
1.88 kristaps 333:
1.85 kristaps 334: static int
1.88 kristaps 335: print_encode(struct html *h, const char *p, int norecurse)
1.29 kristaps 336: {
1.77 kristaps 337: size_t sz;
1.141 ! kristaps 338: int c, len, nospace;
1.82 kristaps 339: const char *seq;
1.132 kristaps 340: enum mandoc_esc esc;
1.100 kristaps 341: static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.14 kristaps 342:
1.85 kristaps 343: nospace = 0;
344:
1.132 kristaps 345: while ('\0' != *p) {
1.100 kristaps 346: sz = strcspn(p, rejs);
1.77 kristaps 347:
348: fwrite(p, 1, sz, stdout);
1.132 kristaps 349: p += (int)sz;
1.77 kristaps 350:
1.132 kristaps 351: if ('\0' == *p)
352: break;
353:
354: switch (*p++) {
355: case ('<'):
1.82 kristaps 356: printf("<");
357: continue;
1.132 kristaps 358: case ('>'):
1.82 kristaps 359: printf(">");
360: continue;
1.132 kristaps 361: case ('&'):
1.82 kristaps 362: printf("&");
1.34 kristaps 363: continue;
1.132 kristaps 364: case (ASCII_HYPH):
1.100 kristaps 365: putchar('-');
366: continue;
1.132 kristaps 367: default:
1.77 kristaps 368: break;
1.132 kristaps 369: }
1.77 kristaps 370:
1.132 kristaps 371: esc = mandoc_escape(&p, &seq, &len);
372: if (ESCAPE_ERROR == esc)
373: break;
1.82 kristaps 374:
1.132 kristaps 375: switch (esc) {
376: case (ESCAPE_NUMBERED):
1.141 ! kristaps 377: c = mchars_num2char(seq, len);
! 378: if ('\0' != c)
! 379: putchar(c);
1.126 schwarze 380: break;
1.132 kristaps 381: case (ESCAPE_PREDEF):
382: print_res(h, seq, len);
1.82 kristaps 383: break;
1.132 kristaps 384: case (ESCAPE_SPECIAL):
385: print_spec(h, seq, len);
1.82 kristaps 386: break;
1.132 kristaps 387: case (ESCAPE_FONTPREV):
1.90 kristaps 388: /* FALLTHROUGH */
1.132 kristaps 389: case (ESCAPE_FONTBOLD):
1.88 kristaps 390: /* FALLTHROUGH */
1.132 kristaps 391: case (ESCAPE_FONTITALIC):
1.88 kristaps 392: /* FALLTHROUGH */
1.132 kristaps 393: case (ESCAPE_FONTROMAN):
1.88 kristaps 394: if (norecurse)
395: break;
1.132 kristaps 396: print_metaf(h, esc);
397: break;
398: case (ESCAPE_NOSPACE):
399: if ('\0' == *p)
400: nospace = 1;
1.88 kristaps 401: break;
1.82 kristaps 402: default:
403: break;
404: }
1.32 kristaps 405: }
1.85 kristaps 406:
407: return(nospace);
1.14 kristaps 408: }
409:
410:
1.94 kristaps 411: static void
412: print_attr(struct html *h, const char *key, const char *val)
413: {
414: printf(" %s=\"", key);
415: (void)print_encode(h, val, 1);
416: putchar('\"');
417: }
418:
419:
1.51 kristaps 420: struct tag *
1.29 kristaps 421: print_otag(struct html *h, enum htmltag tag,
422: int sz, const struct htmlpair *p)
1.14 kristaps 423: {
1.29 kristaps 424: int i;
1.30 kristaps 425: struct tag *t;
426:
1.94 kristaps 427: /* Push this tags onto the stack of open scopes. */
428:
1.30 kristaps 429: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.128 kristaps 430: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 431: t->tag = tag;
1.66 kristaps 432: t->next = h->tags.head;
433: h->tags.head = t;
1.30 kristaps 434: } else
435: t = NULL;
1.29 kristaps 436:
437: if ( ! (HTML_NOSPACE & h->flags))
1.105 kristaps 438: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
439: /* Manage keeps! */
440: if ( ! (HTML_KEEP & h->flags)) {
441: if (HTML_PREKEEP & h->flags)
442: h->flags |= HTML_KEEP;
443: putchar(' ');
444: } else
445: printf(" ");
446: }
1.29 kristaps 447:
1.109 kristaps 448: if ( ! (h->flags & HTML_NONOSPACE))
449: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 450: else
451: h->flags |= HTML_NOSPACE;
1.109 kristaps 452:
1.94 kristaps 453: /* Print out the tag name and attributes. */
454:
1.29 kristaps 455: printf("<%s", htmltags[tag].name);
1.94 kristaps 456: for (i = 0; i < sz; i++)
457: print_attr(h, htmlattrs[p[i].key], p[i].val);
458:
459: /* Add non-overridable attributes. */
460:
461: if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
462: print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
463: print_attr(h, "xml:lang", "en");
464: print_attr(h, "lang", "en");
1.29 kristaps 465: }
1.93 kristaps 466:
1.134 kristaps 467: /* Accommodate for XML "well-formed" singleton escaping. */
1.94 kristaps 468:
1.93 kristaps 469: if (HTML_AUTOCLOSE & htmltags[tag].flags)
470: switch (h->type) {
471: case (HTML_XHTML_1_0_STRICT):
472: putchar('/');
473: break;
474: default:
475: break;
476: }
477:
1.78 kristaps 478: putchar('>');
1.14 kristaps 479:
1.29 kristaps 480: h->flags |= HTML_NOSPACE;
1.117 kristaps 481:
482: if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
483: putchar('\n');
484:
1.30 kristaps 485: return(t);
1.14 kristaps 486: }
487:
488:
1.29 kristaps 489: static void
490: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 491: {
492:
1.29 kristaps 493: printf("</%s>", htmltags[tag].name);
1.71 kristaps 494: if (HTML_CLRLINE & htmltags[tag].flags) {
1.29 kristaps 495: h->flags |= HTML_NOSPACE;
1.78 kristaps 496: putchar('\n');
1.87 kristaps 497: }
1.14 kristaps 498: }
499:
1.51 kristaps 500: void
1.93 kristaps 501: print_gen_decls(struct html *h)
1.1 kristaps 502: {
1.93 kristaps 503: const char *doctype;
504: const char *dtd;
1.96 kristaps 505: const char *name;
1.93 kristaps 506:
507: switch (h->type) {
508: case (HTML_HTML_4_01_STRICT):
1.96 kristaps 509: name = "HTML";
1.93 kristaps 510: doctype = "-//W3C//DTD HTML 4.01//EN";
511: dtd = "http://www.w3.org/TR/html4/strict.dtd";
512: break;
513: default:
1.141 ! kristaps 514: puts("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.96 kristaps 515: name = "html";
1.93 kristaps 516: doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
517: dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
518: break;
519: }
520:
1.96 kristaps 521: printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
522: name, doctype, dtd);
1.1 kristaps 523: }
524:
1.51 kristaps 525: void
1.104 kristaps 526: print_text(struct html *h, const char *word)
1.1 kristaps 527: {
528:
1.105 kristaps 529: if ( ! (HTML_NOSPACE & h->flags)) {
530: /* Manage keeps! */
531: if ( ! (HTML_KEEP & h->flags)) {
532: if (HTML_PREKEEP & h->flags)
533: h->flags |= HTML_KEEP;
534: putchar(' ');
535: } else
536: printf(" ");
537: }
1.30 kristaps 538:
1.122 kristaps 539: assert(NULL == h->metaf);
540: if (HTMLFONT_NONE != h->metac)
541: h->metaf = HTMLFONT_BOLD == h->metac ?
542: print_otag(h, TAG_B, 0, NULL) :
543: print_otag(h, TAG_I, 0, NULL);
544:
1.104 kristaps 545: assert(word);
546: if ( ! print_encode(h, word, 0))
1.109 kristaps 547: if ( ! (h->flags & HTML_NONOSPACE))
548: h->flags &= ~HTML_NOSPACE;
1.122 kristaps 549:
550: if (h->metaf) {
551: print_tagq(h, h->metaf);
552: h->metaf = NULL;
553: }
1.113 schwarze 554:
555: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 556: }
1.30 kristaps 557:
558:
1.51 kristaps 559: void
1.30 kristaps 560: print_tagq(struct html *h, const struct tag *until)
561: {
562: struct tag *tag;
563:
1.66 kristaps 564: while ((tag = h->tags.head) != NULL) {
1.125 kristaps 565: /*
566: * Remember to close out and nullify the current
567: * meta-font and table, if applicable.
568: */
1.89 kristaps 569: if (tag == h->metaf)
570: h->metaf = NULL;
1.125 kristaps 571: if (tag == h->tblt)
572: h->tblt = NULL;
1.30 kristaps 573: print_ctag(h, tag->tag);
1.66 kristaps 574: h->tags.head = tag->next;
1.30 kristaps 575: free(tag);
576: if (until && tag == until)
577: return;
578: }
579: }
580:
581:
1.51 kristaps 582: void
1.30 kristaps 583: print_stagq(struct html *h, const struct tag *suntil)
584: {
585: struct tag *tag;
586:
1.66 kristaps 587: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 588: if (suntil && tag == suntil)
589: return;
1.125 kristaps 590: /*
591: * Remember to close out and nullify the current
592: * meta-font and table, if applicable.
593: */
1.89 kristaps 594: if (tag == h->metaf)
595: h->metaf = NULL;
1.125 kristaps 596: if (tag == h->tblt)
597: h->tblt = NULL;
1.30 kristaps 598: print_ctag(h, tag->tag);
1.66 kristaps 599: h->tags.head = tag->next;
1.30 kristaps 600: free(tag);
601: }
602: }
1.55 kristaps 603:
604: void
605: bufinit(struct html *h)
606: {
607:
608: h->buf[0] = '\0';
609: h->buflen = 0;
610: }
611:
612: void
1.58 kristaps 613: bufcat_style(struct html *h, const char *key, const char *val)
614: {
615:
616: bufcat(h, key);
1.141 ! kristaps 617: bufcat(h, ":");
1.58 kristaps 618: bufcat(h, val);
1.141 ! kristaps 619: bufcat(h, ";");
1.58 kristaps 620: }
621:
622: void
1.55 kristaps 623: bufcat(struct html *h, const char *p)
624: {
625:
1.141 ! kristaps 626: h->buflen = strlcat(h->buf, p, BUFSIZ);
! 627: assert(h->buflen < BUFSIZ);
! 628: h->buflen--;
1.55 kristaps 629: }
630:
631: void
1.141 ! kristaps 632: bufcat_fmt(struct html *h, const char *fmt, ...)
1.55 kristaps 633: {
634: va_list ap;
635:
636: va_start(ap, fmt);
1.56 kristaps 637: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 638: BUFSIZ - h->buflen - 1, fmt, ap);
639: va_end(ap);
640: h->buflen = strlen(h->buf);
641: }
642:
1.141 ! kristaps 643: static void
1.55 kristaps 644: bufncat(struct html *h, const char *p, size_t sz)
645: {
646:
1.141 ! kristaps 647: assert(h->buflen + sz + 1 < BUFSIZ);
! 648: strncat(h->buf, p, sz);
1.55 kristaps 649: h->buflen += sz;
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: void
676: buffmt_man(struct html *h,
677: const char *name, const char *sec)
678: {
679: const char *p, *pp;
680:
681: pp = h->base_man;
1.61 kristaps 682:
683: /* LINTED */
684: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 685: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 686: switch (*(p + 1)) {
687: case('S'):
1.58 kristaps 688: bufcat(h, sec ? sec : "1");
1.55 kristaps 689: break;
690: case('N'):
1.141 ! kristaps 691: bufcat_fmt(h, name);
1.55 kristaps 692: break;
693: default:
694: bufncat(h, p, 2);
695: break;
696: }
697: pp = p + 2;
698: }
699: if (pp)
700: bufcat(h, pp);
701: }
1.58 kristaps 702:
703: void
704: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
705: {
1.62 kristaps 706: double v;
1.58 kristaps 707:
708: v = su->scale;
1.140 kristaps 709: if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
710: v = 1.0;
1.58 kristaps 711:
1.141 ! kristaps 712: bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
1.58 kristaps 713: }
1.68 kristaps 714:
715: void
1.70 kristaps 716: html_idcat(char *dst, const char *src, int sz)
1.68 kristaps 717: {
1.70 kristaps 718: int ssz;
1.68 kristaps 719:
1.124 schwarze 720: assert(sz > 2);
1.68 kristaps 721:
722: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
723:
1.124 schwarze 724: /* We can't start with a number (bah). */
725:
726: if ('#' == *dst) {
727: dst++;
728: sz--;
729: }
730: if ('\0' == *dst) {
731: *dst++ = 'x';
732: *dst = '\0';
733: sz--;
734: }
735:
1.70 kristaps 736: for ( ; *dst != '\0' && sz; dst++, sz--)
1.68 kristaps 737: /* Jump to end. */ ;
1.70 kristaps 738:
739: for ( ; *src != '\0' && sz > 1; src++) {
1.73 kristaps 740: ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70 kristaps 741: sz -= ssz;
742: dst += ssz;
743: }
1.68 kristaps 744: }
CVSweb