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