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