Annotation of mandoc/html.c, Revision 1.231
1.231 ! schwarze 1: /* $Id: html.c,v 1.230 2018/05/28 14:13:36 schwarze Exp $ */
1.1 kristaps 2: /*
1.176 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.221 schwarze 4: * Copyright (c) 2011-2015, 2017, 2018 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.186 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29 kristaps 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: #include "config.h"
19:
1.41 kristaps 20: #include <sys/types.h>
1.30 kristaps 21:
1.1 kristaps 22: #include <assert.h>
1.68 kristaps 23: #include <ctype.h>
1.76 kristaps 24: #include <stdarg.h>
1.229 schwarze 25: #include <stddef.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.210 schwarze 32: #include "mandoc_aux.h"
1.229 schwarze 33: #include "mandoc_ohash.h"
1.100 kristaps 34: #include "mandoc.h"
1.210 schwarze 35: #include "roff.h"
1.58 kristaps 36: #include "out.h"
1.51 kristaps 37: #include "html.h"
1.186 schwarze 38: #include "manconf.h"
1.64 kristaps 39: #include "main.h"
1.63 kristaps 40:
1.29 kristaps 41: struct htmldata {
1.63 kristaps 42: const char *name;
1.29 kristaps 43: int flags;
1.196 schwarze 44: #define HTML_NOSTACK (1 << 0)
45: #define HTML_AUTOCLOSE (1 << 1)
46: #define HTML_NLBEFORE (1 << 2)
47: #define HTML_NLBEGIN (1 << 3)
48: #define HTML_NLEND (1 << 4)
49: #define HTML_NLAFTER (1 << 5)
50: #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
51: #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
52: #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
53: #define HTML_INDENT (1 << 6)
54: #define HTML_NOINDENT (1 << 7)
1.29 kristaps 55: };
1.7 kristaps 56:
1.29 kristaps 57: static const struct htmldata htmltags[TAG_MAX] = {
1.196 schwarze 58: {"html", HTML_NLALL},
59: {"head", HTML_NLALL | HTML_INDENT},
60: {"body", HTML_NLALL},
61: {"meta", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
62: {"title", HTML_NLAROUND},
63: {"div", HTML_NLAROUND},
1.225 schwarze 64: {"div", 0},
1.196 schwarze 65: {"h1", HTML_NLAROUND},
66: {"h2", HTML_NLAROUND},
67: {"span", 0},
68: {"link", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
69: {"br", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
70: {"a", 0},
71: {"table", HTML_NLALL | HTML_INDENT},
1.205 schwarze 72: {"colgroup", HTML_NLALL | HTML_INDENT},
1.196 schwarze 73: {"col", HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
74: {"tr", HTML_NLALL | HTML_INDENT},
75: {"td", HTML_NLAROUND},
76: {"li", HTML_NLAROUND | HTML_INDENT},
77: {"ul", HTML_NLALL | HTML_INDENT},
78: {"ol", HTML_NLALL | HTML_INDENT},
79: {"dl", HTML_NLALL | HTML_INDENT},
80: {"dt", HTML_NLAROUND},
81: {"dd", HTML_NLAROUND | HTML_INDENT},
82: {"pre", HTML_NLALL | HTML_NOINDENT},
1.207 schwarze 83: {"var", 0},
1.206 schwarze 84: {"cite", 0},
1.196 schwarze 85: {"b", 0},
86: {"i", 0},
87: {"code", 0},
88: {"small", 0},
89: {"style", HTML_NLALL | HTML_INDENT},
90: {"math", HTML_NLALL | HTML_INDENT},
91: {"mrow", 0},
92: {"mi", 0},
1.215 schwarze 93: {"mn", 0},
1.196 schwarze 94: {"mo", 0},
95: {"msup", 0},
96: {"msub", 0},
97: {"msubsup", 0},
98: {"mfrac", 0},
99: {"msqrt", 0},
100: {"mfenced", 0},
101: {"mtable", 0},
102: {"mtr", 0},
103: {"mtd", 0},
104: {"munderover", 0},
105: {"munder", 0},
106: {"mover", 0},
1.90 kristaps 107: };
108:
1.140 kristaps 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: };
121:
1.229 schwarze 122: /* Avoid duplicate HTML id= attributes. */
123: static struct ohash id_unique;
124:
1.194 schwarze 125: static void a2width(const char *, struct roffsu *);
1.197 schwarze 126: static void print_byte(struct html *, char);
127: static void print_endword(struct html *);
128: static void print_indent(struct html *);
129: static void print_word(struct html *, const char *);
130:
1.184 schwarze 131: static void print_ctag(struct html *, struct tag *);
1.197 schwarze 132: static int print_escape(struct html *, char);
1.195 schwarze 133: static int print_encode(struct html *, const char *, const char *, int);
134: static void print_href(struct html *, const char *, const char *, int);
1.141 kristaps 135: static void print_metaf(struct html *, enum mandoc_esc);
1.82 kristaps 136:
1.156 schwarze 137:
1.180 schwarze 138: void *
1.191 schwarze 139: html_alloc(const struct manoutput *outopts)
1.10 kristaps 140: {
1.30 kristaps 141: struct html *h;
142:
1.128 kristaps 143: h = mandoc_calloc(1, sizeof(struct html));
1.10 kristaps 144:
1.204 schwarze 145: h->tag = NULL;
1.186 schwarze 146: h->style = outopts->style;
147: h->base_man = outopts->man;
148: h->base_includes = outopts->includes;
149: if (outopts->fragment)
150: h->oflags |= HTML_FRAGMENT;
1.43 kristaps 151:
1.229 schwarze 152: mandoc_ohash_init(&id_unique, 4, 0);
153:
1.188 schwarze 154: return h;
1.29 kristaps 155: }
1.10 kristaps 156:
1.29 kristaps 157: void
158: html_free(void *p)
159: {
1.30 kristaps 160: struct tag *tag;
161: struct html *h;
1.229 schwarze 162: char *cp;
163: unsigned int slot;
1.30 kristaps 164:
165: h = (struct html *)p;
1.204 schwarze 166: while ((tag = h->tag) != NULL) {
167: h->tag = tag->next;
1.30 kristaps 168: free(tag);
169: }
1.229 schwarze 170: free(h);
1.53 kristaps 171:
1.229 schwarze 172: cp = ohash_first(&id_unique, &slot);
173: while (cp != NULL) {
174: free(cp);
175: cp = ohash_next(&id_unique, &slot);
176: }
177: ohash_delete(&id_unique);
1.10 kristaps 178: }
1.2 kristaps 179:
1.51 kristaps 180: void
1.29 kristaps 181: print_gen_head(struct html *h)
182: {
1.165 kristaps 183: struct tag *t;
1.41 kristaps 184:
1.194 schwarze 185: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.222 schwarze 186: if (h->style != NULL) {
187: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
188: h->style, "type", "text/css", "media", "all");
189: return;
190: }
1.165 kristaps 191:
1.168 kristaps 192: /*
1.222 schwarze 193: * Print a minimal embedded style sheet.
1.168 kristaps 194: */
1.196 schwarze 195:
1.194 schwarze 196: t = print_otag(h, TAG_STYLE, "");
1.196 schwarze 197: print_text(h, "table.head, table.foot { width: 100%; }");
1.197 schwarze 198: print_endline(h);
1.196 schwarze 199: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197 schwarze 200: print_endline(h);
1.196 schwarze 201: print_text(h, "td.head-vol { text-align: center; }");
1.197 schwarze 202: print_endline(h);
1.198 schwarze 203: print_text(h, "div.Pp { margin: 1ex 0ex; }");
1.225 schwarze 204: print_endline(h);
205: print_text(h, "div.Nd, div.Bf, div.Op { display: inline; }");
1.226 schwarze 206: print_endline(h);
1.227 schwarze 207: print_text(h, "span.Pa, span.Ad { font-style: italic; }");
1.228 schwarze 208: print_endline(h);
209: print_text(h, "span.Ms { font-weight: bold; }");
1.224 schwarze 210: print_endline(h);
211: print_text(h, "dl.Bl-diag ");
212: print_byte(h, '>');
213: print_text(h, " dt { font-weight: bold; }");
1.223 schwarze 214: print_endline(h);
215: print_text(h, "code.Nm, code.Fl, code.Cm, code.Ic, "
216: "code.In, code.Fd, code.Fn,");
217: print_endline(h);
218: print_text(h, "code.Cd { font-weight: bold; "
219: "font-family: inherit; }");
1.165 kristaps 220: print_tagq(h, t);
1.4 kristaps 221: }
222:
1.126 schwarze 223: static void
1.132 kristaps 224: print_metaf(struct html *h, enum mandoc_esc deco)
1.88 kristaps 225: {
1.90 kristaps 226: enum htmlfont font;
1.88 kristaps 227:
228: switch (deco) {
1.156 schwarze 229: case ESCAPE_FONTPREV:
1.90 kristaps 230: font = h->metal;
1.88 kristaps 231: break;
1.156 schwarze 232: case ESCAPE_FONTITALIC:
1.90 kristaps 233: font = HTMLFONT_ITALIC;
234: break;
1.156 schwarze 235: case ESCAPE_FONTBOLD:
1.90 kristaps 236: font = HTMLFONT_BOLD;
1.88 kristaps 237: break;
1.156 schwarze 238: case ESCAPE_FONTBI:
1.152 schwarze 239: font = HTMLFONT_BI;
240: break;
1.156 schwarze 241: case ESCAPE_FONT:
242: case ESCAPE_FONTROMAN:
1.90 kristaps 243: font = HTMLFONT_NONE;
1.88 kristaps 244: break;
245: default:
246: abort();
247: }
248:
1.122 kristaps 249: if (h->metaf) {
250: print_tagq(h, h->metaf);
251: h->metaf = NULL;
252: }
253:
254: h->metal = h->metac;
255: h->metac = font;
256:
1.152 schwarze 257: switch (font) {
1.156 schwarze 258: case HTMLFONT_ITALIC:
1.194 schwarze 259: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 260: break;
1.156 schwarze 261: case HTMLFONT_BOLD:
1.194 schwarze 262: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 263: break;
1.156 schwarze 264: case HTMLFONT_BI:
1.194 schwarze 265: h->metaf = print_otag(h, TAG_B, "");
266: print_otag(h, TAG_I, "");
1.152 schwarze 267: break;
268: default:
269: break;
270: }
1.210 schwarze 271: }
272:
273: char *
1.229 schwarze 274: html_make_id(const struct roff_node *n, int unique)
1.210 schwarze 275: {
276: const struct roff_node *nch;
1.229 schwarze 277: char *buf, *bufs, *cp;
278: unsigned int slot;
279: int suffix;
1.210 schwarze 280:
281: for (nch = n->child; nch != NULL; nch = nch->next)
282: if (nch->type != ROFFT_TEXT)
283: return NULL;
284:
285: buf = NULL;
286: deroff(&buf, n);
1.220 schwarze 287: if (buf == NULL)
288: return NULL;
1.210 schwarze 289:
1.230 schwarze 290: /*
291: * In ID attributes, only use ASCII characters that are
292: * permitted in URL-fragment strings according to the
293: * explicit list at:
294: * https://url.spec.whatwg.org/#url-fragment-string
295: */
1.210 schwarze 296:
297: for (cp = buf; *cp != '\0'; cp++)
1.230 schwarze 298: if (isalnum((unsigned char)*cp) == 0 &&
299: strchr("!$&'()*+,-./:;=?@_~", *cp) == NULL)
1.210 schwarze 300: *cp = '_';
301:
1.229 schwarze 302: if (unique == 0)
303: return buf;
304:
305: /* Avoid duplicate HTML id= attributes. */
306:
307: bufs = NULL;
308: suffix = 1;
309: slot = ohash_qlookup(&id_unique, buf);
310: cp = ohash_find(&id_unique, slot);
311: if (cp != NULL) {
312: while (cp != NULL) {
313: free(bufs);
314: if (++suffix > 127) {
315: free(buf);
316: return NULL;
317: }
318: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
319: slot = ohash_qlookup(&id_unique, bufs);
320: cp = ohash_find(&id_unique, slot);
321: }
322: free(buf);
323: buf = bufs;
324: }
325: ohash_insert(&id_unique, slot, buf);
1.210 schwarze 326: return buf;
1.88 kristaps 327: }
328:
1.138 kristaps 329: int
330: html_strlen(const char *cp)
331: {
1.151 schwarze 332: size_t rsz;
333: int skip, sz;
1.138 kristaps 334:
335: /*
336: * Account for escaped sequences within string length
337: * calculations. This follows the logic in term_strlen() as we
338: * must calculate the width of produced strings.
339: * Assume that characters are always width of "1". This is
340: * hacky, but it gets the job done for approximation of widths.
341: */
342:
343: sz = 0;
1.151 schwarze 344: skip = 0;
345: while (1) {
346: rsz = strcspn(cp, "\\");
347: if (rsz) {
348: cp += rsz;
349: if (skip) {
350: skip = 0;
351: rsz--;
352: }
353: sz += rsz;
354: }
355: if ('\0' == *cp)
356: break;
357: cp++;
358: switch (mandoc_escape(&cp, NULL, NULL)) {
1.156 schwarze 359: case ESCAPE_ERROR:
1.188 schwarze 360: return sz;
1.156 schwarze 361: case ESCAPE_UNICODE:
362: case ESCAPE_NUMBERED:
363: case ESCAPE_SPECIAL:
1.185 schwarze 364: case ESCAPE_OVERSTRIKE:
1.151 schwarze 365: if (skip)
366: skip = 0;
367: else
368: sz++;
369: break;
1.156 schwarze 370: case ESCAPE_SKIPCHAR:
1.151 schwarze 371: skip = 1;
1.138 kristaps 372: break;
373: default:
374: break;
375: }
376: }
1.188 schwarze 377: return sz;
1.138 kristaps 378: }
1.88 kristaps 379:
1.85 kristaps 380: static int
1.197 schwarze 381: print_escape(struct html *h, char c)
1.159 schwarze 382: {
383:
384: switch (c) {
385: case '<':
1.197 schwarze 386: print_word(h, "<");
1.159 schwarze 387: break;
388: case '>':
1.197 schwarze 389: print_word(h, ">");
1.159 schwarze 390: break;
391: case '&':
1.197 schwarze 392: print_word(h, "&");
1.159 schwarze 393: break;
394: case '"':
1.197 schwarze 395: print_word(h, """);
1.159 schwarze 396: break;
397: case ASCII_NBRSP:
1.197 schwarze 398: print_word(h, " ");
1.159 schwarze 399: break;
400: case ASCII_HYPH:
1.197 schwarze 401: print_byte(h, '-');
1.189 schwarze 402: break;
1.159 schwarze 403: case ASCII_BREAK:
404: break;
405: default:
1.188 schwarze 406: return 0;
1.159 schwarze 407: }
1.188 schwarze 408: return 1;
1.159 schwarze 409: }
410:
411: static int
1.195 schwarze 412: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29 kristaps 413: {
1.197 schwarze 414: char numbuf[16];
1.214 schwarze 415: struct tag *t;
416: const char *seq;
1.77 kristaps 417: size_t sz;
1.214 schwarze 418: int c, len, breakline, nospace;
1.132 kristaps 419: enum mandoc_esc esc;
1.214 schwarze 420: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 421: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 422:
1.195 schwarze 423: if (pend == NULL)
424: pend = strchr(p, '\0');
425:
1.214 schwarze 426: breakline = 0;
1.85 kristaps 427: nospace = 0;
428:
1.195 schwarze 429: while (p < pend) {
1.151 schwarze 430: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
431: h->flags &= ~HTML_SKIPCHAR;
432: p++;
433: continue;
434: }
435:
1.197 schwarze 436: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 schwarze 437: print_byte(h, *p);
438:
439: if (breakline &&
440: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
441: t = print_otag(h, TAG_DIV, "");
442: print_text(h, "\\~");
443: print_tagq(h, t);
444: breakline = 0;
445: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
446: p++;
447: continue;
448: }
1.77 kristaps 449:
1.195 schwarze 450: if (p >= pend)
1.132 kristaps 451: break;
452:
1.214 schwarze 453: if (*p == ' ') {
454: print_endword(h);
455: p++;
456: continue;
457: }
458:
1.197 schwarze 459: if (print_escape(h, *p++))
1.154 schwarze 460: continue;
1.77 kristaps 461:
1.132 kristaps 462: esc = mandoc_escape(&p, &seq, &len);
463: if (ESCAPE_ERROR == esc)
464: break;
1.82 kristaps 465:
1.132 kristaps 466: switch (esc) {
1.156 schwarze 467: case ESCAPE_FONT:
468: case ESCAPE_FONTPREV:
469: case ESCAPE_FONTBOLD:
470: case ESCAPE_FONTITALIC:
471: case ESCAPE_FONTBI:
472: case ESCAPE_FONTROMAN:
1.151 schwarze 473: if (0 == norecurse)
474: print_metaf(h, esc);
475: continue;
1.156 schwarze 476: case ESCAPE_SKIPCHAR:
1.151 schwarze 477: h->flags |= HTML_SKIPCHAR;
478: continue;
479: default:
480: break;
481: }
482:
483: if (h->flags & HTML_SKIPCHAR) {
484: h->flags &= ~HTML_SKIPCHAR;
485: continue;
486: }
487:
488: switch (esc) {
1.156 schwarze 489: case ESCAPE_UNICODE:
1.159 schwarze 490: /* Skip past "u" header. */
1.144 kristaps 491: c = mchars_num2uc(seq + 1, len - 1);
492: break;
1.156 schwarze 493: case ESCAPE_NUMBERED:
1.141 kristaps 494: c = mchars_num2char(seq, len);
1.181 schwarze 495: if (c < 0)
496: continue;
1.82 kristaps 497: break;
1.156 schwarze 498: case ESCAPE_SPECIAL:
1.191 schwarze 499: c = mchars_spec2cp(seq, len);
1.181 schwarze 500: if (c <= 0)
501: continue;
1.132 kristaps 502: break;
1.214 schwarze 503: case ESCAPE_BREAK:
504: breakline = 1;
505: continue;
1.156 schwarze 506: case ESCAPE_NOSPACE:
1.132 kristaps 507: if ('\0' == *p)
508: nospace = 1;
1.179 schwarze 509: continue;
1.185 schwarze 510: case ESCAPE_OVERSTRIKE:
511: if (len == 0)
512: continue;
513: c = seq[len - 1];
514: break;
1.82 kristaps 515: default:
1.179 schwarze 516: continue;
1.82 kristaps 517: }
1.181 schwarze 518: if ((c < 0x20 && c != 0x09) ||
519: (c > 0x7E && c < 0xA0))
1.179 schwarze 520: c = 0xFFFD;
1.197 schwarze 521: if (c > 0x7E) {
1.216 schwarze 522: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197 schwarze 523: print_word(h, numbuf);
524: } else if (print_escape(h, c) == 0)
525: print_byte(h, c);
1.32 kristaps 526: }
1.85 kristaps 527:
1.188 schwarze 528: return nospace;
1.14 kristaps 529: }
530:
1.94 kristaps 531: static void
1.195 schwarze 532: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 533: {
1.195 schwarze 534: const char *p, *pp;
535:
536: pp = man ? h->base_man : h->base_includes;
537: while ((p = strchr(pp, '%')) != NULL) {
538: print_encode(h, pp, p, 1);
539: if (man && p[1] == 'S') {
540: if (sec == NULL)
1.197 schwarze 541: print_byte(h, '1');
1.195 schwarze 542: else
543: print_encode(h, sec, NULL, 1);
544: } else if ((man && p[1] == 'N') ||
545: (man == 0 && p[1] == 'I'))
546: print_encode(h, name, NULL, 1);
547: else
548: print_encode(h, p, p + 2, 1);
549: pp = p + 2;
550: }
551: if (*pp != '\0')
552: print_encode(h, pp, NULL, 1);
1.94 kristaps 553: }
554:
1.51 kristaps 555: struct tag *
1.194 schwarze 556: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 557: {
1.194 schwarze 558: va_list ap;
559: struct roffsu mysu, *su;
1.197 schwarze 560: char numbuf[16];
1.30 kristaps 561: struct tag *t;
1.195 schwarze 562: const char *attr;
1.203 schwarze 563: char *arg1, *arg2;
1.195 schwarze 564: double v;
1.196 schwarze 565: int i, have_style, tflags;
566:
567: tflags = htmltags[tag].flags;
1.30 kristaps 568:
1.204 schwarze 569: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 570:
1.196 schwarze 571: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 572: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 573: t->tag = tag;
1.204 schwarze 574: t->next = h->tag;
575: h->tag = t;
1.30 kristaps 576: } else
577: t = NULL;
1.29 kristaps 578:
1.196 schwarze 579: if (tflags & HTML_NLBEFORE)
1.197 schwarze 580: print_endline(h);
581: if (h->col == 0)
582: print_indent(h);
1.196 schwarze 583: else if ((h->flags & HTML_NOSPACE) == 0) {
584: if (h->flags & HTML_KEEP)
1.216 schwarze 585: print_word(h, " ");
1.196 schwarze 586: else {
587: if (h->flags & HTML_PREKEEP)
588: h->flags |= HTML_KEEP;
1.197 schwarze 589: print_endword(h);
1.105 kristaps 590: }
1.196 schwarze 591: }
1.29 kristaps 592:
1.109 kristaps 593: if ( ! (h->flags & HTML_NONOSPACE))
594: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 595: else
596: h->flags |= HTML_NOSPACE;
1.109 kristaps 597:
1.94 kristaps 598: /* Print out the tag name and attributes. */
599:
1.197 schwarze 600: print_byte(h, '<');
601: print_word(h, htmltags[tag].name);
1.194 schwarze 602:
603: va_start(ap, fmt);
604:
605: have_style = 0;
606: while (*fmt != '\0') {
607: if (*fmt == 's') {
608: have_style = 1;
609: fmt++;
610: break;
611: }
1.203 schwarze 612:
613: /* Parse a non-style attribute and its arguments. */
614:
615: arg1 = va_arg(ap, char *);
1.194 schwarze 616: switch (*fmt++) {
617: case 'c':
1.195 schwarze 618: attr = "class";
1.194 schwarze 619: break;
620: case 'h':
1.195 schwarze 621: attr = "href";
1.194 schwarze 622: break;
623: case 'i':
1.195 schwarze 624: attr = "id";
1.194 schwarze 625: break;
626: case '?':
1.203 schwarze 627: attr = arg1;
628: arg1 = va_arg(ap, char *);
1.194 schwarze 629: break;
630: default:
631: abort();
632: }
1.203 schwarze 633: arg2 = NULL;
634: if (*fmt == 'M')
635: arg2 = va_arg(ap, char *);
636: if (arg1 == NULL)
637: continue;
638:
639: /* Print the non-style attributes. */
640:
1.197 schwarze 641: print_byte(h, ' ');
642: print_word(h, attr);
643: print_byte(h, '=');
644: print_byte(h, '"');
1.195 schwarze 645: switch (*fmt) {
1.208 schwarze 646: case 'I':
647: print_href(h, arg1, NULL, 0);
648: fmt++;
649: break;
1.195 schwarze 650: case 'M':
1.203 schwarze 651: print_href(h, arg1, arg2, 1);
1.195 schwarze 652: fmt++;
653: break;
1.208 schwarze 654: case 'R':
655: print_byte(h, '#');
656: print_encode(h, arg1, NULL, 1);
1.195 schwarze 657: fmt++;
658: break;
1.208 schwarze 659: case 'T':
660: print_encode(h, arg1, NULL, 1);
661: print_word(h, "\" title=\"");
662: print_encode(h, arg1, NULL, 1);
1.195 schwarze 663: fmt++;
1.208 schwarze 664: break;
1.195 schwarze 665: default:
1.203 schwarze 666: print_encode(h, arg1, NULL, 1);
1.195 schwarze 667: break;
668: }
1.197 schwarze 669: print_byte(h, '"');
1.194 schwarze 670: }
671:
672: /* Print out styles. */
673:
674: while (*fmt != '\0') {
1.203 schwarze 675: arg1 = NULL;
676: su = NULL;
1.194 schwarze 677:
678: /* First letter: input argument type. */
679:
680: switch (*fmt++) {
681: case 'h':
682: i = va_arg(ap, int);
1.203 schwarze 683: su = &mysu;
1.194 schwarze 684: SCALE_HS_INIT(su, i);
685: break;
686: case 's':
1.203 schwarze 687: arg1 = va_arg(ap, char *);
1.194 schwarze 688: break;
689: case 'u':
690: su = va_arg(ap, struct roffsu *);
691: break;
692: case 'w':
1.219 schwarze 693: if ((arg2 = va_arg(ap, char *)) != NULL) {
694: su = &mysu;
695: a2width(arg2, su);
696: }
1.211 schwarze 697: if (*fmt == '+') {
1.219 schwarze 698: if (su != NULL) {
699: /* Make even bold text fit. */
700: su->scale *= 1.2;
701: /* Add padding. */
702: su->scale += 3.0;
703: }
1.211 schwarze 704: fmt++;
705: }
1.194 schwarze 706: break;
707: default:
708: abort();
709: }
710:
711: /* Second letter: style name. */
712:
713: switch (*fmt++) {
714: case 'h':
1.195 schwarze 715: attr = "height";
1.194 schwarze 716: break;
717: case 'i':
1.195 schwarze 718: attr = "text-indent";
1.194 schwarze 719: break;
720: case 'l':
1.195 schwarze 721: attr = "margin-left";
1.194 schwarze 722: break;
723: case 'w':
1.195 schwarze 724: attr = "width";
1.194 schwarze 725: break;
726: case 'W':
1.195 schwarze 727: attr = "min-width";
1.194 schwarze 728: break;
729: case '?':
1.203 schwarze 730: attr = arg1;
731: arg1 = va_arg(ap, char *);
732: break;
1.194 schwarze 733: default:
734: abort();
735: }
1.203 schwarze 736: if (su == NULL && arg1 == NULL)
737: continue;
738:
739: if (have_style == 1)
740: print_word(h, " style=\"");
741: else
742: print_byte(h, ' ');
1.197 schwarze 743: print_word(h, attr);
744: print_byte(h, ':');
745: print_byte(h, ' ');
1.203 schwarze 746: if (su != NULL) {
747: v = su->scale;
748: if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
749: v = 1.0;
750: else if (su->unit == SCALE_BU)
751: v /= 24.0;
752: (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
753: print_word(h, numbuf);
754: print_word(h, roffscales[su->unit]);
755: } else
756: print_word(h, arg1);
1.197 schwarze 757: print_byte(h, ';');
1.203 schwarze 758: have_style = 2;
1.194 schwarze 759: }
1.203 schwarze 760: if (have_style == 2)
1.197 schwarze 761: print_byte(h, '"');
1.194 schwarze 762:
763: va_end(ap);
1.94 kristaps 764:
1.172 kristaps 765: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 766:
1.93 kristaps 767: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197 schwarze 768: print_byte(h, '/');
1.93 kristaps 769:
1.197 schwarze 770: print_byte(h, '>');
1.14 kristaps 771:
1.196 schwarze 772: if (tflags & HTML_NLBEGIN)
1.197 schwarze 773: print_endline(h);
1.196 schwarze 774: else
775: h->flags |= HTML_NOSPACE;
1.117 kristaps 776:
1.196 schwarze 777: if (tflags & HTML_INDENT)
778: h->indent++;
779: if (tflags & HTML_NOINDENT)
780: h->noindent++;
1.117 kristaps 781:
1.188 schwarze 782: return t;
1.14 kristaps 783: }
784:
1.29 kristaps 785: static void
1.184 schwarze 786: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 787: {
1.196 schwarze 788: int tflags;
1.156 schwarze 789:
1.184 schwarze 790: /*
791: * Remember to close out and nullify the current
792: * meta-font and table, if applicable.
793: */
794: if (tag == h->metaf)
795: h->metaf = NULL;
796: if (tag == h->tblt)
797: h->tblt = NULL;
798:
1.196 schwarze 799: tflags = htmltags[tag->tag].flags;
800:
801: if (tflags & HTML_INDENT)
802: h->indent--;
803: if (tflags & HTML_NOINDENT)
804: h->noindent--;
805: if (tflags & HTML_NLEND)
1.197 schwarze 806: print_endline(h);
807: print_indent(h);
808: print_byte(h, '<');
809: print_byte(h, '/');
810: print_word(h, htmltags[tag->tag].name);
811: print_byte(h, '>');
1.196 schwarze 812: if (tflags & HTML_NLAFTER)
1.197 schwarze 813: print_endline(h);
1.184 schwarze 814:
1.204 schwarze 815: h->tag = tag->next;
1.184 schwarze 816: free(tag);
1.14 kristaps 817: }
818:
1.51 kristaps 819: void
1.93 kristaps 820: print_gen_decls(struct html *h)
1.1 kristaps 821: {
1.197 schwarze 822: print_word(h, "<!DOCTYPE html>");
823: print_endline(h);
1.221 schwarze 824: }
825:
826: void
827: print_gen_comment(struct html *h, struct roff_node *n)
828: {
829: int wantblank;
830:
831: print_word(h, "<!-- This is an automatically generated file."
832: " Do not edit.");
833: h->indent = 1;
834: wantblank = 0;
835: while (n != NULL && n->type == ROFFT_COMMENT) {
836: if (strstr(n->string, "-->") == NULL &&
837: (wantblank || *n->string != '\0')) {
838: print_endline(h);
839: print_indent(h);
840: print_word(h, n->string);
841: wantblank = *n->string != '\0';
842: }
843: n = n->next;
844: }
845: if (wantblank)
846: print_endline(h);
847: print_word(h, " -->");
848: print_endline(h);
849: h->indent = 0;
1.1 kristaps 850: }
851:
1.51 kristaps 852: void
1.104 kristaps 853: print_text(struct html *h, const char *word)
1.1 kristaps 854: {
1.197 schwarze 855: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 856: if ( ! (HTML_KEEP & h->flags)) {
857: if (HTML_PREKEEP & h->flags)
858: h->flags |= HTML_KEEP;
1.197 schwarze 859: print_endword(h);
1.105 kristaps 860: } else
1.216 schwarze 861: print_word(h, " ");
1.105 kristaps 862: }
1.30 kristaps 863:
1.122 kristaps 864: assert(NULL == h->metaf);
1.152 schwarze 865: switch (h->metac) {
1.156 schwarze 866: case HTMLFONT_ITALIC:
1.194 schwarze 867: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 868: break;
1.156 schwarze 869: case HTMLFONT_BOLD:
1.194 schwarze 870: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 871: break;
1.156 schwarze 872: case HTMLFONT_BI:
1.194 schwarze 873: h->metaf = print_otag(h, TAG_B, "");
874: print_otag(h, TAG_I, "");
1.152 schwarze 875: break;
876: default:
1.197 schwarze 877: print_indent(h);
1.152 schwarze 878: break;
879: }
1.122 kristaps 880:
1.104 kristaps 881: assert(word);
1.195 schwarze 882: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 883: if ( ! (h->flags & HTML_NONOSPACE))
884: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 885: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 886: } else
1.183 schwarze 887: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 888:
889: if (h->metaf) {
890: print_tagq(h, h->metaf);
891: h->metaf = NULL;
892: }
1.113 schwarze 893:
894: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 895: }
1.30 kristaps 896:
1.51 kristaps 897: void
1.30 kristaps 898: print_tagq(struct html *h, const struct tag *until)
899: {
900: struct tag *tag;
901:
1.204 schwarze 902: while ((tag = h->tag) != NULL) {
1.184 schwarze 903: print_ctag(h, tag);
1.30 kristaps 904: if (until && tag == until)
905: return;
906: }
907: }
908:
1.51 kristaps 909: void
1.30 kristaps 910: print_stagq(struct html *h, const struct tag *suntil)
911: {
912: struct tag *tag;
913:
1.204 schwarze 914: while ((tag = h->tag) != NULL) {
1.30 kristaps 915: if (suntil && tag == suntil)
916: return;
1.184 schwarze 917: print_ctag(h, tag);
1.30 kristaps 918: }
919: }
1.171 kristaps 920:
921: void
922: print_paragraph(struct html *h)
923: {
924: struct tag *t;
925:
1.198 schwarze 926: t = print_otag(h, TAG_DIV, "c", "Pp");
1.171 kristaps 927: print_tagq(h, t);
928: }
929:
1.197 schwarze 930:
931: /***********************************************************************
932: * Low level output functions.
933: * They implement line breaking using a short static buffer.
934: ***********************************************************************/
935:
936: /*
937: * Buffer one HTML output byte.
938: * If the buffer is full, flush and deactivate it and start a new line.
939: * If the buffer is inactive, print directly.
940: */
941: static void
942: print_byte(struct html *h, char c)
943: {
944: if ((h->flags & HTML_BUFFER) == 0) {
945: putchar(c);
946: h->col++;
947: return;
948: }
949:
950: if (h->col + h->bufcol < sizeof(h->buf)) {
951: h->buf[h->bufcol++] = c;
952: return;
953: }
954:
955: putchar('\n');
956: h->col = 0;
957: print_indent(h);
958: putchar(' ');
959: putchar(' ');
960: fwrite(h->buf, h->bufcol, 1, stdout);
961: putchar(c);
962: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
963: h->bufcol = 0;
964: h->flags &= ~HTML_BUFFER;
965: }
966:
1.196 schwarze 967: /*
968: * If something was printed on the current output line, end it.
1.197 schwarze 969: * Not to be called right after print_indent().
1.196 schwarze 970: */
1.202 schwarze 971: void
1.197 schwarze 972: print_endline(struct html *h)
1.196 schwarze 973: {
1.197 schwarze 974: if (h->col == 0)
1.196 schwarze 975: return;
976:
1.197 schwarze 977: if (h->bufcol) {
978: putchar(' ');
979: fwrite(h->buf, h->bufcol, 1, stdout);
980: h->bufcol = 0;
981: }
1.196 schwarze 982: putchar('\n');
1.197 schwarze 983: h->col = 0;
984: h->flags |= HTML_NOSPACE;
985: h->flags &= ~HTML_BUFFER;
986: }
987:
988: /*
989: * Flush the HTML output buffer.
990: * If it is inactive, activate it.
991: */
992: static void
993: print_endword(struct html *h)
994: {
995: if (h->noindent) {
996: print_byte(h, ' ');
997: return;
998: }
999:
1000: if ((h->flags & HTML_BUFFER) == 0) {
1001: h->col++;
1002: h->flags |= HTML_BUFFER;
1003: } else if (h->bufcol) {
1004: putchar(' ');
1005: fwrite(h->buf, h->bufcol, 1, stdout);
1006: h->col += h->bufcol + 1;
1007: }
1008: h->bufcol = 0;
1.196 schwarze 1009: }
1010:
1011: /*
1012: * If at the beginning of a new output line,
1013: * perform indentation and mark the line as containing output.
1014: * Make sure to really produce some output right afterwards,
1015: * but do not use print_otag() for producing it.
1016: */
1017: static void
1.197 schwarze 1018: print_indent(struct html *h)
1.196 schwarze 1019: {
1.197 schwarze 1020: size_t i;
1.196 schwarze 1021:
1.197 schwarze 1022: if (h->col)
1.196 schwarze 1023: return;
1024:
1.197 schwarze 1025: if (h->noindent == 0) {
1026: h->col = h->indent * 2;
1027: for (i = 0; i < h->col; i++)
1.196 schwarze 1028: putchar(' ');
1.197 schwarze 1029: }
1030: h->flags &= ~HTML_NOSPACE;
1031: }
1032:
1033: /*
1034: * Print or buffer some characters
1035: * depending on the current HTML output buffer state.
1036: */
1037: static void
1038: print_word(struct html *h, const char *cp)
1039: {
1040: while (*cp != '\0')
1041: print_byte(h, *cp++);
1.196 schwarze 1042: }
1.194 schwarze 1043:
1044: /*
1045: * Calculate the scaling unit passed in a `-width' argument. This uses
1046: * either a native scaling unit (e.g., 1i, 2m) or the string length of
1047: * the value.
1048: */
1049: static void
1050: a2width(const char *p, struct roffsu *su)
1051: {
1.213 schwarze 1052: const char *end;
1053:
1054: end = a2roffsu(p, su, SCALE_MAX);
1055: if (end == NULL || *end != '\0') {
1.194 schwarze 1056: su->unit = SCALE_EN;
1057: su->scale = html_strlen(p);
1058: } else if (su->scale < 0.0)
1059: su->scale = 0.0;
1.68 kristaps 1060: }
CVSweb