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