Annotation of mandoc/html.c, Revision 1.229
1.229 ! schwarze 1: /* $Id: html.c,v 1.228 2018/05/21 01:11:31 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:
290: /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
291:
292: for (cp = buf; *cp != '\0'; cp++)
293: if (*cp == ' ')
294: *cp = '_';
295:
1.229 ! schwarze 296: if (unique == 0)
! 297: return buf;
! 298:
! 299: /* Avoid duplicate HTML id= attributes. */
! 300:
! 301: bufs = NULL;
! 302: suffix = 1;
! 303: slot = ohash_qlookup(&id_unique, buf);
! 304: cp = ohash_find(&id_unique, slot);
! 305: if (cp != NULL) {
! 306: while (cp != NULL) {
! 307: free(bufs);
! 308: if (++suffix > 127) {
! 309: free(buf);
! 310: return NULL;
! 311: }
! 312: mandoc_asprintf(&bufs, "%s_%d", buf, suffix);
! 313: slot = ohash_qlookup(&id_unique, bufs);
! 314: cp = ohash_find(&id_unique, slot);
! 315: }
! 316: free(buf);
! 317: buf = bufs;
! 318: }
! 319: ohash_insert(&id_unique, slot, buf);
1.210 schwarze 320: return buf;
1.88 kristaps 321: }
322:
1.138 kristaps 323: int
324: html_strlen(const char *cp)
325: {
1.151 schwarze 326: size_t rsz;
327: int skip, sz;
1.138 kristaps 328:
329: /*
330: * Account for escaped sequences within string length
331: * calculations. This follows the logic in term_strlen() as we
332: * must calculate the width of produced strings.
333: * Assume that characters are always width of "1". This is
334: * hacky, but it gets the job done for approximation of widths.
335: */
336:
337: sz = 0;
1.151 schwarze 338: skip = 0;
339: while (1) {
340: rsz = strcspn(cp, "\\");
341: if (rsz) {
342: cp += rsz;
343: if (skip) {
344: skip = 0;
345: rsz--;
346: }
347: sz += rsz;
348: }
349: if ('\0' == *cp)
350: break;
351: cp++;
352: switch (mandoc_escape(&cp, NULL, NULL)) {
1.156 schwarze 353: case ESCAPE_ERROR:
1.188 schwarze 354: return sz;
1.156 schwarze 355: case ESCAPE_UNICODE:
356: case ESCAPE_NUMBERED:
357: case ESCAPE_SPECIAL:
1.185 schwarze 358: case ESCAPE_OVERSTRIKE:
1.151 schwarze 359: if (skip)
360: skip = 0;
361: else
362: sz++;
363: break;
1.156 schwarze 364: case ESCAPE_SKIPCHAR:
1.151 schwarze 365: skip = 1;
1.138 kristaps 366: break;
367: default:
368: break;
369: }
370: }
1.188 schwarze 371: return sz;
1.138 kristaps 372: }
1.88 kristaps 373:
1.85 kristaps 374: static int
1.197 schwarze 375: print_escape(struct html *h, char c)
1.159 schwarze 376: {
377:
378: switch (c) {
379: case '<':
1.197 schwarze 380: print_word(h, "<");
1.159 schwarze 381: break;
382: case '>':
1.197 schwarze 383: print_word(h, ">");
1.159 schwarze 384: break;
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 ASCII_NBRSP:
1.197 schwarze 392: print_word(h, " ");
1.159 schwarze 393: break;
394: case ASCII_HYPH:
1.197 schwarze 395: print_byte(h, '-');
1.189 schwarze 396: break;
1.159 schwarze 397: case ASCII_BREAK:
398: break;
399: default:
1.188 schwarze 400: return 0;
1.159 schwarze 401: }
1.188 schwarze 402: return 1;
1.159 schwarze 403: }
404:
405: static int
1.195 schwarze 406: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29 kristaps 407: {
1.197 schwarze 408: char numbuf[16];
1.214 schwarze 409: struct tag *t;
410: const char *seq;
1.77 kristaps 411: size_t sz;
1.214 schwarze 412: int c, len, breakline, nospace;
1.132 kristaps 413: enum mandoc_esc esc;
1.214 schwarze 414: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 415: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 416:
1.195 schwarze 417: if (pend == NULL)
418: pend = strchr(p, '\0');
419:
1.214 schwarze 420: breakline = 0;
1.85 kristaps 421: nospace = 0;
422:
1.195 schwarze 423: while (p < pend) {
1.151 schwarze 424: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
425: h->flags &= ~HTML_SKIPCHAR;
426: p++;
427: continue;
428: }
429:
1.197 schwarze 430: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 schwarze 431: print_byte(h, *p);
432:
433: if (breakline &&
434: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
435: t = print_otag(h, TAG_DIV, "");
436: print_text(h, "\\~");
437: print_tagq(h, t);
438: breakline = 0;
439: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
440: p++;
441: continue;
442: }
1.77 kristaps 443:
1.195 schwarze 444: if (p >= pend)
1.132 kristaps 445: break;
446:
1.214 schwarze 447: if (*p == ' ') {
448: print_endword(h);
449: p++;
450: continue;
451: }
452:
1.197 schwarze 453: if (print_escape(h, *p++))
1.154 schwarze 454: continue;
1.77 kristaps 455:
1.132 kristaps 456: esc = mandoc_escape(&p, &seq, &len);
457: if (ESCAPE_ERROR == esc)
458: break;
1.82 kristaps 459:
1.132 kristaps 460: switch (esc) {
1.156 schwarze 461: case ESCAPE_FONT:
462: case ESCAPE_FONTPREV:
463: case ESCAPE_FONTBOLD:
464: case ESCAPE_FONTITALIC:
465: case ESCAPE_FONTBI:
466: case ESCAPE_FONTROMAN:
1.151 schwarze 467: if (0 == norecurse)
468: print_metaf(h, esc);
469: continue;
1.156 schwarze 470: case ESCAPE_SKIPCHAR:
1.151 schwarze 471: h->flags |= HTML_SKIPCHAR;
472: continue;
473: default:
474: break;
475: }
476:
477: if (h->flags & HTML_SKIPCHAR) {
478: h->flags &= ~HTML_SKIPCHAR;
479: continue;
480: }
481:
482: switch (esc) {
1.156 schwarze 483: case ESCAPE_UNICODE:
1.159 schwarze 484: /* Skip past "u" header. */
1.144 kristaps 485: c = mchars_num2uc(seq + 1, len - 1);
486: break;
1.156 schwarze 487: case ESCAPE_NUMBERED:
1.141 kristaps 488: c = mchars_num2char(seq, len);
1.181 schwarze 489: if (c < 0)
490: continue;
1.82 kristaps 491: break;
1.156 schwarze 492: case ESCAPE_SPECIAL:
1.191 schwarze 493: c = mchars_spec2cp(seq, len);
1.181 schwarze 494: if (c <= 0)
495: continue;
1.132 kristaps 496: break;
1.214 schwarze 497: case ESCAPE_BREAK:
498: breakline = 1;
499: continue;
1.156 schwarze 500: case ESCAPE_NOSPACE:
1.132 kristaps 501: if ('\0' == *p)
502: nospace = 1;
1.179 schwarze 503: continue;
1.185 schwarze 504: case ESCAPE_OVERSTRIKE:
505: if (len == 0)
506: continue;
507: c = seq[len - 1];
508: break;
1.82 kristaps 509: default:
1.179 schwarze 510: continue;
1.82 kristaps 511: }
1.181 schwarze 512: if ((c < 0x20 && c != 0x09) ||
513: (c > 0x7E && c < 0xA0))
1.179 schwarze 514: c = 0xFFFD;
1.197 schwarze 515: if (c > 0x7E) {
1.216 schwarze 516: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197 schwarze 517: print_word(h, numbuf);
518: } else if (print_escape(h, c) == 0)
519: print_byte(h, c);
1.32 kristaps 520: }
1.85 kristaps 521:
1.188 schwarze 522: return nospace;
1.14 kristaps 523: }
524:
1.94 kristaps 525: static void
1.195 schwarze 526: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 527: {
1.195 schwarze 528: const char *p, *pp;
529:
530: pp = man ? h->base_man : h->base_includes;
531: while ((p = strchr(pp, '%')) != NULL) {
532: print_encode(h, pp, p, 1);
533: if (man && p[1] == 'S') {
534: if (sec == NULL)
1.197 schwarze 535: print_byte(h, '1');
1.195 schwarze 536: else
537: print_encode(h, sec, NULL, 1);
538: } else if ((man && p[1] == 'N') ||
539: (man == 0 && p[1] == 'I'))
540: print_encode(h, name, NULL, 1);
541: else
542: print_encode(h, p, p + 2, 1);
543: pp = p + 2;
544: }
545: if (*pp != '\0')
546: print_encode(h, pp, NULL, 1);
1.94 kristaps 547: }
548:
1.51 kristaps 549: struct tag *
1.194 schwarze 550: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 551: {
1.194 schwarze 552: va_list ap;
553: struct roffsu mysu, *su;
1.197 schwarze 554: char numbuf[16];
1.30 kristaps 555: struct tag *t;
1.195 schwarze 556: const char *attr;
1.203 schwarze 557: char *arg1, *arg2;
1.195 schwarze 558: double v;
1.196 schwarze 559: int i, have_style, tflags;
560:
561: tflags = htmltags[tag].flags;
1.30 kristaps 562:
1.204 schwarze 563: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 564:
1.196 schwarze 565: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 566: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 567: t->tag = tag;
1.204 schwarze 568: t->next = h->tag;
569: h->tag = t;
1.30 kristaps 570: } else
571: t = NULL;
1.29 kristaps 572:
1.196 schwarze 573: if (tflags & HTML_NLBEFORE)
1.197 schwarze 574: print_endline(h);
575: if (h->col == 0)
576: print_indent(h);
1.196 schwarze 577: else if ((h->flags & HTML_NOSPACE) == 0) {
578: if (h->flags & HTML_KEEP)
1.216 schwarze 579: print_word(h, " ");
1.196 schwarze 580: else {
581: if (h->flags & HTML_PREKEEP)
582: h->flags |= HTML_KEEP;
1.197 schwarze 583: print_endword(h);
1.105 kristaps 584: }
1.196 schwarze 585: }
1.29 kristaps 586:
1.109 kristaps 587: if ( ! (h->flags & HTML_NONOSPACE))
588: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 589: else
590: h->flags |= HTML_NOSPACE;
1.109 kristaps 591:
1.94 kristaps 592: /* Print out the tag name and attributes. */
593:
1.197 schwarze 594: print_byte(h, '<');
595: print_word(h, htmltags[tag].name);
1.194 schwarze 596:
597: va_start(ap, fmt);
598:
599: have_style = 0;
600: while (*fmt != '\0') {
601: if (*fmt == 's') {
602: have_style = 1;
603: fmt++;
604: break;
605: }
1.203 schwarze 606:
607: /* Parse a non-style attribute and its arguments. */
608:
609: arg1 = va_arg(ap, char *);
1.194 schwarze 610: switch (*fmt++) {
611: case 'c':
1.195 schwarze 612: attr = "class";
1.194 schwarze 613: break;
614: case 'h':
1.195 schwarze 615: attr = "href";
1.194 schwarze 616: break;
617: case 'i':
1.195 schwarze 618: attr = "id";
1.194 schwarze 619: break;
620: case '?':
1.203 schwarze 621: attr = arg1;
622: arg1 = va_arg(ap, char *);
1.194 schwarze 623: break;
624: default:
625: abort();
626: }
1.203 schwarze 627: arg2 = NULL;
628: if (*fmt == 'M')
629: arg2 = va_arg(ap, char *);
630: if (arg1 == NULL)
631: continue;
632:
633: /* Print the non-style attributes. */
634:
1.197 schwarze 635: print_byte(h, ' ');
636: print_word(h, attr);
637: print_byte(h, '=');
638: print_byte(h, '"');
1.195 schwarze 639: switch (*fmt) {
1.208 schwarze 640: case 'I':
641: print_href(h, arg1, NULL, 0);
642: fmt++;
643: break;
1.195 schwarze 644: case 'M':
1.203 schwarze 645: print_href(h, arg1, arg2, 1);
1.195 schwarze 646: fmt++;
647: break;
1.208 schwarze 648: case 'R':
649: print_byte(h, '#');
650: print_encode(h, arg1, NULL, 1);
1.195 schwarze 651: fmt++;
652: break;
1.208 schwarze 653: case 'T':
654: print_encode(h, arg1, NULL, 1);
655: print_word(h, "\" title=\"");
656: print_encode(h, arg1, NULL, 1);
1.195 schwarze 657: fmt++;
1.208 schwarze 658: break;
1.195 schwarze 659: default:
1.203 schwarze 660: print_encode(h, arg1, NULL, 1);
1.195 schwarze 661: break;
662: }
1.197 schwarze 663: print_byte(h, '"');
1.194 schwarze 664: }
665:
666: /* Print out styles. */
667:
668: while (*fmt != '\0') {
1.203 schwarze 669: arg1 = NULL;
670: su = NULL;
1.194 schwarze 671:
672: /* First letter: input argument type. */
673:
674: switch (*fmt++) {
675: case 'h':
676: i = va_arg(ap, int);
1.203 schwarze 677: su = &mysu;
1.194 schwarze 678: SCALE_HS_INIT(su, i);
679: break;
680: case 's':
1.203 schwarze 681: arg1 = va_arg(ap, char *);
1.194 schwarze 682: break;
683: case 'u':
684: su = va_arg(ap, struct roffsu *);
685: break;
686: case 'w':
1.219 schwarze 687: if ((arg2 = va_arg(ap, char *)) != NULL) {
688: su = &mysu;
689: a2width(arg2, su);
690: }
691: if (*fmt == '*') {
692: if (su != NULL && su->unit == SCALE_EN &&
693: su->scale > 5.9 && su->scale < 6.1)
694: su = NULL;
695: fmt++;
1.218 schwarze 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: }
706: if (*fmt == '-') {
1.219 schwarze 707: if (su != NULL)
708: su->scale *= -1.0;
1.211 schwarze 709: fmt++;
710: }
1.194 schwarze 711: break;
712: default:
713: abort();
714: }
715:
716: /* Second letter: style name. */
717:
718: switch (*fmt++) {
719: case 'h':
1.195 schwarze 720: attr = "height";
1.194 schwarze 721: break;
722: case 'i':
1.195 schwarze 723: attr = "text-indent";
1.194 schwarze 724: break;
725: case 'l':
1.195 schwarze 726: attr = "margin-left";
1.194 schwarze 727: break;
728: case 'w':
1.195 schwarze 729: attr = "width";
1.194 schwarze 730: break;
731: case 'W':
1.195 schwarze 732: attr = "min-width";
1.194 schwarze 733: break;
734: case '?':
1.203 schwarze 735: attr = arg1;
736: arg1 = va_arg(ap, char *);
737: break;
1.194 schwarze 738: default:
739: abort();
740: }
1.203 schwarze 741: if (su == NULL && arg1 == NULL)
742: continue;
743:
744: if (have_style == 1)
745: print_word(h, " style=\"");
746: else
747: print_byte(h, ' ');
1.197 schwarze 748: print_word(h, attr);
749: print_byte(h, ':');
750: print_byte(h, ' ');
1.203 schwarze 751: if (su != NULL) {
752: v = su->scale;
753: if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
754: v = 1.0;
755: else if (su->unit == SCALE_BU)
756: v /= 24.0;
757: (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
758: print_word(h, numbuf);
759: print_word(h, roffscales[su->unit]);
760: } else
761: print_word(h, arg1);
1.197 schwarze 762: print_byte(h, ';');
1.203 schwarze 763: have_style = 2;
1.194 schwarze 764: }
1.203 schwarze 765: if (have_style == 2)
1.197 schwarze 766: print_byte(h, '"');
1.194 schwarze 767:
768: va_end(ap);
1.94 kristaps 769:
1.172 kristaps 770: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 771:
1.93 kristaps 772: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197 schwarze 773: print_byte(h, '/');
1.93 kristaps 774:
1.197 schwarze 775: print_byte(h, '>');
1.14 kristaps 776:
1.196 schwarze 777: if (tflags & HTML_NLBEGIN)
1.197 schwarze 778: print_endline(h);
1.196 schwarze 779: else
780: h->flags |= HTML_NOSPACE;
1.117 kristaps 781:
1.196 schwarze 782: if (tflags & HTML_INDENT)
783: h->indent++;
784: if (tflags & HTML_NOINDENT)
785: h->noindent++;
1.117 kristaps 786:
1.188 schwarze 787: return t;
1.14 kristaps 788: }
789:
1.29 kristaps 790: static void
1.184 schwarze 791: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 792: {
1.196 schwarze 793: int tflags;
1.156 schwarze 794:
1.184 schwarze 795: /*
796: * Remember to close out and nullify the current
797: * meta-font and table, if applicable.
798: */
799: if (tag == h->metaf)
800: h->metaf = NULL;
801: if (tag == h->tblt)
802: h->tblt = NULL;
803:
1.196 schwarze 804: tflags = htmltags[tag->tag].flags;
805:
806: if (tflags & HTML_INDENT)
807: h->indent--;
808: if (tflags & HTML_NOINDENT)
809: h->noindent--;
810: if (tflags & HTML_NLEND)
1.197 schwarze 811: print_endline(h);
812: print_indent(h);
813: print_byte(h, '<');
814: print_byte(h, '/');
815: print_word(h, htmltags[tag->tag].name);
816: print_byte(h, '>');
1.196 schwarze 817: if (tflags & HTML_NLAFTER)
1.197 schwarze 818: print_endline(h);
1.184 schwarze 819:
1.204 schwarze 820: h->tag = tag->next;
1.184 schwarze 821: free(tag);
1.14 kristaps 822: }
823:
1.51 kristaps 824: void
1.93 kristaps 825: print_gen_decls(struct html *h)
1.1 kristaps 826: {
1.197 schwarze 827: print_word(h, "<!DOCTYPE html>");
828: print_endline(h);
1.221 schwarze 829: }
830:
831: void
832: print_gen_comment(struct html *h, struct roff_node *n)
833: {
834: int wantblank;
835:
836: print_word(h, "<!-- This is an automatically generated file."
837: " Do not edit.");
838: h->indent = 1;
839: wantblank = 0;
840: while (n != NULL && n->type == ROFFT_COMMENT) {
841: if (strstr(n->string, "-->") == NULL &&
842: (wantblank || *n->string != '\0')) {
843: print_endline(h);
844: print_indent(h);
845: print_word(h, n->string);
846: wantblank = *n->string != '\0';
847: }
848: n = n->next;
849: }
850: if (wantblank)
851: print_endline(h);
852: print_word(h, " -->");
853: print_endline(h);
854: h->indent = 0;
1.1 kristaps 855: }
856:
1.51 kristaps 857: void
1.104 kristaps 858: print_text(struct html *h, const char *word)
1.1 kristaps 859: {
1.197 schwarze 860: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 861: if ( ! (HTML_KEEP & h->flags)) {
862: if (HTML_PREKEEP & h->flags)
863: h->flags |= HTML_KEEP;
1.197 schwarze 864: print_endword(h);
1.105 kristaps 865: } else
1.216 schwarze 866: print_word(h, " ");
1.105 kristaps 867: }
1.30 kristaps 868:
1.122 kristaps 869: assert(NULL == h->metaf);
1.152 schwarze 870: switch (h->metac) {
1.156 schwarze 871: case HTMLFONT_ITALIC:
1.194 schwarze 872: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 873: break;
1.156 schwarze 874: case HTMLFONT_BOLD:
1.194 schwarze 875: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 876: break;
1.156 schwarze 877: case HTMLFONT_BI:
1.194 schwarze 878: h->metaf = print_otag(h, TAG_B, "");
879: print_otag(h, TAG_I, "");
1.152 schwarze 880: break;
881: default:
1.197 schwarze 882: print_indent(h);
1.152 schwarze 883: break;
884: }
1.122 kristaps 885:
1.104 kristaps 886: assert(word);
1.195 schwarze 887: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 888: if ( ! (h->flags & HTML_NONOSPACE))
889: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 890: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 891: } else
1.183 schwarze 892: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 893:
894: if (h->metaf) {
895: print_tagq(h, h->metaf);
896: h->metaf = NULL;
897: }
1.113 schwarze 898:
899: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 900: }
1.30 kristaps 901:
1.51 kristaps 902: void
1.30 kristaps 903: print_tagq(struct html *h, const struct tag *until)
904: {
905: struct tag *tag;
906:
1.204 schwarze 907: while ((tag = h->tag) != NULL) {
1.184 schwarze 908: print_ctag(h, tag);
1.30 kristaps 909: if (until && tag == until)
910: return;
911: }
912: }
913:
1.51 kristaps 914: void
1.30 kristaps 915: print_stagq(struct html *h, const struct tag *suntil)
916: {
917: struct tag *tag;
918:
1.204 schwarze 919: while ((tag = h->tag) != NULL) {
1.30 kristaps 920: if (suntil && tag == suntil)
921: return;
1.184 schwarze 922: print_ctag(h, tag);
1.30 kristaps 923: }
924: }
1.171 kristaps 925:
926: void
927: print_paragraph(struct html *h)
928: {
929: struct tag *t;
930:
1.198 schwarze 931: t = print_otag(h, TAG_DIV, "c", "Pp");
1.171 kristaps 932: print_tagq(h, t);
933: }
934:
1.197 schwarze 935:
936: /***********************************************************************
937: * Low level output functions.
938: * They implement line breaking using a short static buffer.
939: ***********************************************************************/
940:
941: /*
942: * Buffer one HTML output byte.
943: * If the buffer is full, flush and deactivate it and start a new line.
944: * If the buffer is inactive, print directly.
945: */
946: static void
947: print_byte(struct html *h, char c)
948: {
949: if ((h->flags & HTML_BUFFER) == 0) {
950: putchar(c);
951: h->col++;
952: return;
953: }
954:
955: if (h->col + h->bufcol < sizeof(h->buf)) {
956: h->buf[h->bufcol++] = c;
957: return;
958: }
959:
960: putchar('\n');
961: h->col = 0;
962: print_indent(h);
963: putchar(' ');
964: putchar(' ');
965: fwrite(h->buf, h->bufcol, 1, stdout);
966: putchar(c);
967: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
968: h->bufcol = 0;
969: h->flags &= ~HTML_BUFFER;
970: }
971:
1.196 schwarze 972: /*
973: * If something was printed on the current output line, end it.
1.197 schwarze 974: * Not to be called right after print_indent().
1.196 schwarze 975: */
1.202 schwarze 976: void
1.197 schwarze 977: print_endline(struct html *h)
1.196 schwarze 978: {
1.197 schwarze 979: if (h->col == 0)
1.196 schwarze 980: return;
981:
1.197 schwarze 982: if (h->bufcol) {
983: putchar(' ');
984: fwrite(h->buf, h->bufcol, 1, stdout);
985: h->bufcol = 0;
986: }
1.196 schwarze 987: putchar('\n');
1.197 schwarze 988: h->col = 0;
989: h->flags |= HTML_NOSPACE;
990: h->flags &= ~HTML_BUFFER;
991: }
992:
993: /*
994: * Flush the HTML output buffer.
995: * If it is inactive, activate it.
996: */
997: static void
998: print_endword(struct html *h)
999: {
1000: if (h->noindent) {
1001: print_byte(h, ' ');
1002: return;
1003: }
1004:
1005: if ((h->flags & HTML_BUFFER) == 0) {
1006: h->col++;
1007: h->flags |= HTML_BUFFER;
1008: } else if (h->bufcol) {
1009: putchar(' ');
1010: fwrite(h->buf, h->bufcol, 1, stdout);
1011: h->col += h->bufcol + 1;
1012: }
1013: h->bufcol = 0;
1.196 schwarze 1014: }
1015:
1016: /*
1017: * If at the beginning of a new output line,
1018: * perform indentation and mark the line as containing output.
1019: * Make sure to really produce some output right afterwards,
1020: * but do not use print_otag() for producing it.
1021: */
1022: static void
1.197 schwarze 1023: print_indent(struct html *h)
1.196 schwarze 1024: {
1.197 schwarze 1025: size_t i;
1.196 schwarze 1026:
1.197 schwarze 1027: if (h->col)
1.196 schwarze 1028: return;
1029:
1.197 schwarze 1030: if (h->noindent == 0) {
1031: h->col = h->indent * 2;
1032: for (i = 0; i < h->col; i++)
1.196 schwarze 1033: putchar(' ');
1.197 schwarze 1034: }
1035: h->flags &= ~HTML_NOSPACE;
1036: }
1037:
1038: /*
1039: * Print or buffer some characters
1040: * depending on the current HTML output buffer state.
1041: */
1042: static void
1043: print_word(struct html *h, const char *cp)
1044: {
1045: while (*cp != '\0')
1046: print_byte(h, *cp++);
1.196 schwarze 1047: }
1.194 schwarze 1048:
1049: /*
1050: * Calculate the scaling unit passed in a `-width' argument. This uses
1051: * either a native scaling unit (e.g., 1i, 2m) or the string length of
1052: * the value.
1053: */
1054: static void
1055: a2width(const char *p, struct roffsu *su)
1056: {
1.213 schwarze 1057: const char *end;
1058:
1059: end = a2roffsu(p, su, SCALE_MAX);
1060: if (end == NULL || *end != '\0') {
1.194 schwarze 1061: su->unit = SCALE_EN;
1062: su->scale = html_strlen(p);
1063: } else if (su->scale < 0.0)
1064: su->scale = 0.0;
1.68 kristaps 1065: }
CVSweb