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