Annotation of mandoc/html.c, Revision 1.214
1.214 ! schwarze 1: /* $Id: html.c,v 1.213 2017/06/08 12:54:58 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.214 ! schwarze 348: struct tag *t;
! 349: const char *seq;
1.77 kristaps 350: size_t sz;
1.214 ! schwarze 351: int c, len, breakline, nospace;
1.132 kristaps 352: enum mandoc_esc esc;
1.214 ! schwarze 353: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 354: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 355:
1.195 schwarze 356: if (pend == NULL)
357: pend = strchr(p, '\0');
358:
1.214 ! schwarze 359: breakline = 0;
1.85 kristaps 360: nospace = 0;
361:
1.195 schwarze 362: while (p < pend) {
1.151 schwarze 363: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
364: h->flags &= ~HTML_SKIPCHAR;
365: p++;
366: continue;
367: }
368:
1.197 schwarze 369: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 ! schwarze 370: print_byte(h, *p);
! 371:
! 372: if (breakline &&
! 373: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
! 374: t = print_otag(h, TAG_DIV, "");
! 375: print_text(h, "\\~");
! 376: print_tagq(h, t);
! 377: breakline = 0;
! 378: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
! 379: p++;
! 380: continue;
! 381: }
1.77 kristaps 382:
1.195 schwarze 383: if (p >= pend)
1.132 kristaps 384: break;
385:
1.214 ! schwarze 386: if (*p == ' ') {
! 387: print_endword(h);
! 388: p++;
! 389: continue;
! 390: }
! 391:
1.197 schwarze 392: if (print_escape(h, *p++))
1.154 schwarze 393: continue;
1.77 kristaps 394:
1.132 kristaps 395: esc = mandoc_escape(&p, &seq, &len);
396: if (ESCAPE_ERROR == esc)
397: break;
1.82 kristaps 398:
1.132 kristaps 399: switch (esc) {
1.156 schwarze 400: case ESCAPE_FONT:
401: case ESCAPE_FONTPREV:
402: case ESCAPE_FONTBOLD:
403: case ESCAPE_FONTITALIC:
404: case ESCAPE_FONTBI:
405: case ESCAPE_FONTROMAN:
1.151 schwarze 406: if (0 == norecurse)
407: print_metaf(h, esc);
408: continue;
1.156 schwarze 409: case ESCAPE_SKIPCHAR:
1.151 schwarze 410: h->flags |= HTML_SKIPCHAR;
411: continue;
412: default:
413: break;
414: }
415:
416: if (h->flags & HTML_SKIPCHAR) {
417: h->flags &= ~HTML_SKIPCHAR;
418: continue;
419: }
420:
421: switch (esc) {
1.156 schwarze 422: case ESCAPE_UNICODE:
1.159 schwarze 423: /* Skip past "u" header. */
1.144 kristaps 424: c = mchars_num2uc(seq + 1, len - 1);
425: break;
1.156 schwarze 426: case ESCAPE_NUMBERED:
1.141 kristaps 427: c = mchars_num2char(seq, len);
1.181 schwarze 428: if (c < 0)
429: continue;
1.82 kristaps 430: break;
1.156 schwarze 431: case ESCAPE_SPECIAL:
1.191 schwarze 432: c = mchars_spec2cp(seq, len);
1.181 schwarze 433: if (c <= 0)
434: continue;
1.132 kristaps 435: break;
1.214 ! schwarze 436: case ESCAPE_BREAK:
! 437: breakline = 1;
! 438: continue;
1.156 schwarze 439: case ESCAPE_NOSPACE:
1.132 kristaps 440: if ('\0' == *p)
441: nospace = 1;
1.179 schwarze 442: continue;
1.185 schwarze 443: case ESCAPE_OVERSTRIKE:
444: if (len == 0)
445: continue;
446: c = seq[len - 1];
447: break;
1.82 kristaps 448: default:
1.179 schwarze 449: continue;
1.82 kristaps 450: }
1.181 schwarze 451: if ((c < 0x20 && c != 0x09) ||
452: (c > 0x7E && c < 0xA0))
1.179 schwarze 453: c = 0xFFFD;
1.197 schwarze 454: if (c > 0x7E) {
455: (void)snprintf(numbuf, sizeof(numbuf), "&#%d;", c);
456: print_word(h, numbuf);
457: } else if (print_escape(h, c) == 0)
458: print_byte(h, c);
1.32 kristaps 459: }
1.85 kristaps 460:
1.188 schwarze 461: return nospace;
1.14 kristaps 462: }
463:
1.94 kristaps 464: static void
1.195 schwarze 465: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 466: {
1.195 schwarze 467: const char *p, *pp;
468:
469: pp = man ? h->base_man : h->base_includes;
470: while ((p = strchr(pp, '%')) != NULL) {
471: print_encode(h, pp, p, 1);
472: if (man && p[1] == 'S') {
473: if (sec == NULL)
1.197 schwarze 474: print_byte(h, '1');
1.195 schwarze 475: else
476: print_encode(h, sec, NULL, 1);
477: } else if ((man && p[1] == 'N') ||
478: (man == 0 && p[1] == 'I'))
479: print_encode(h, name, NULL, 1);
480: else
481: print_encode(h, p, p + 2, 1);
482: pp = p + 2;
483: }
484: if (*pp != '\0')
485: print_encode(h, pp, NULL, 1);
1.94 kristaps 486: }
487:
1.51 kristaps 488: struct tag *
1.194 schwarze 489: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 490: {
1.194 schwarze 491: va_list ap;
492: struct roffsu mysu, *su;
1.197 schwarze 493: char numbuf[16];
1.30 kristaps 494: struct tag *t;
1.195 schwarze 495: const char *attr;
1.203 schwarze 496: char *arg1, *arg2;
1.195 schwarze 497: double v;
1.196 schwarze 498: int i, have_style, tflags;
499:
500: tflags = htmltags[tag].flags;
1.30 kristaps 501:
1.204 schwarze 502: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 503:
1.196 schwarze 504: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 505: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 506: t->tag = tag;
1.204 schwarze 507: t->next = h->tag;
508: h->tag = t;
1.30 kristaps 509: } else
510: t = NULL;
1.29 kristaps 511:
1.196 schwarze 512: if (tflags & HTML_NLBEFORE)
1.197 schwarze 513: print_endline(h);
514: if (h->col == 0)
515: print_indent(h);
1.196 schwarze 516: else if ((h->flags & HTML_NOSPACE) == 0) {
517: if (h->flags & HTML_KEEP)
1.197 schwarze 518: print_word(h, " ");
1.196 schwarze 519: else {
520: if (h->flags & HTML_PREKEEP)
521: h->flags |= HTML_KEEP;
1.197 schwarze 522: print_endword(h);
1.105 kristaps 523: }
1.196 schwarze 524: }
1.29 kristaps 525:
1.109 kristaps 526: if ( ! (h->flags & HTML_NONOSPACE))
527: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 528: else
529: h->flags |= HTML_NOSPACE;
1.109 kristaps 530:
1.94 kristaps 531: /* Print out the tag name and attributes. */
532:
1.197 schwarze 533: print_byte(h, '<');
534: print_word(h, htmltags[tag].name);
1.194 schwarze 535:
536: va_start(ap, fmt);
537:
538: have_style = 0;
539: while (*fmt != '\0') {
540: if (*fmt == 's') {
541: have_style = 1;
542: fmt++;
543: break;
544: }
1.203 schwarze 545:
546: /* Parse a non-style attribute and its arguments. */
547:
548: arg1 = va_arg(ap, char *);
1.194 schwarze 549: switch (*fmt++) {
550: case 'c':
1.195 schwarze 551: attr = "class";
1.194 schwarze 552: break;
553: case 'h':
1.195 schwarze 554: attr = "href";
1.194 schwarze 555: break;
556: case 'i':
1.195 schwarze 557: attr = "id";
1.194 schwarze 558: break;
559: case '?':
1.203 schwarze 560: attr = arg1;
561: arg1 = va_arg(ap, char *);
1.194 schwarze 562: break;
563: default:
564: abort();
565: }
1.203 schwarze 566: arg2 = NULL;
567: if (*fmt == 'M')
568: arg2 = va_arg(ap, char *);
569: if (arg1 == NULL)
570: continue;
571:
572: /* Print the non-style attributes. */
573:
1.197 schwarze 574: print_byte(h, ' ');
575: print_word(h, attr);
576: print_byte(h, '=');
577: print_byte(h, '"');
1.195 schwarze 578: switch (*fmt) {
1.208 schwarze 579: case 'I':
580: print_href(h, arg1, NULL, 0);
581: fmt++;
582: break;
1.195 schwarze 583: case 'M':
1.203 schwarze 584: print_href(h, arg1, arg2, 1);
1.195 schwarze 585: fmt++;
586: break;
1.208 schwarze 587: case 'R':
588: print_byte(h, '#');
589: print_encode(h, arg1, NULL, 1);
1.195 schwarze 590: fmt++;
591: break;
1.208 schwarze 592: case 'T':
593: print_encode(h, arg1, NULL, 1);
594: print_word(h, "\" title=\"");
595: print_encode(h, arg1, NULL, 1);
1.195 schwarze 596: fmt++;
1.208 schwarze 597: break;
1.195 schwarze 598: default:
1.203 schwarze 599: print_encode(h, arg1, NULL, 1);
1.195 schwarze 600: break;
601: }
1.197 schwarze 602: print_byte(h, '"');
1.194 schwarze 603: }
604:
605: /* Print out styles. */
606:
607: while (*fmt != '\0') {
1.203 schwarze 608: arg1 = NULL;
609: su = NULL;
1.194 schwarze 610:
611: /* First letter: input argument type. */
612:
613: switch (*fmt++) {
614: case 'h':
615: i = va_arg(ap, int);
1.203 schwarze 616: su = &mysu;
1.194 schwarze 617: SCALE_HS_INIT(su, i);
618: break;
619: case 's':
1.203 schwarze 620: arg1 = va_arg(ap, char *);
1.194 schwarze 621: break;
622: case 'u':
623: su = va_arg(ap, struct roffsu *);
624: break;
625: case 'v':
626: i = va_arg(ap, int);
1.203 schwarze 627: su = &mysu;
1.194 schwarze 628: SCALE_VS_INIT(su, i);
629: break;
630: case 'w':
1.203 schwarze 631: if ((arg2 = va_arg(ap, char *)) == NULL)
632: break;
633: su = &mysu;
634: a2width(arg2, su);
1.211 schwarze 635: if (*fmt == '+') {
636: /* Increase to make even bold text fit. */
1.212 schwarze 637: su->scale *= 1.2;
1.211 schwarze 638: /* Add padding. */
639: su->scale += 3.0;
640: fmt++;
641: }
642: if (*fmt == '-') {
1.201 schwarze 643: su->scale *= -1.0;
1.211 schwarze 644: fmt++;
645: }
1.194 schwarze 646: break;
647: default:
648: abort();
649: }
650:
651: /* Second letter: style name. */
652:
653: switch (*fmt++) {
654: case 'b':
1.195 schwarze 655: attr = "margin-bottom";
1.194 schwarze 656: break;
657: case 'h':
1.195 schwarze 658: attr = "height";
1.194 schwarze 659: break;
660: case 'i':
1.195 schwarze 661: attr = "text-indent";
1.194 schwarze 662: break;
663: case 'l':
1.195 schwarze 664: attr = "margin-left";
1.194 schwarze 665: break;
666: case 't':
1.195 schwarze 667: attr = "margin-top";
1.194 schwarze 668: break;
669: case 'w':
1.195 schwarze 670: attr = "width";
1.194 schwarze 671: break;
672: case 'W':
1.195 schwarze 673: attr = "min-width";
1.194 schwarze 674: break;
675: case '?':
1.203 schwarze 676: attr = arg1;
677: arg1 = va_arg(ap, char *);
678: break;
1.194 schwarze 679: default:
680: abort();
681: }
1.203 schwarze 682: if (su == NULL && arg1 == NULL)
683: continue;
684:
685: if (have_style == 1)
686: print_word(h, " style=\"");
687: else
688: print_byte(h, ' ');
1.197 schwarze 689: print_word(h, attr);
690: print_byte(h, ':');
691: print_byte(h, ' ');
1.203 schwarze 692: if (su != NULL) {
693: v = su->scale;
694: if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
695: v = 1.0;
696: else if (su->unit == SCALE_BU)
697: v /= 24.0;
698: (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
699: print_word(h, numbuf);
700: print_word(h, roffscales[su->unit]);
701: } else
702: print_word(h, arg1);
1.197 schwarze 703: print_byte(h, ';');
1.203 schwarze 704: have_style = 2;
1.194 schwarze 705: }
1.203 schwarze 706: if (have_style == 2)
1.197 schwarze 707: print_byte(h, '"');
1.194 schwarze 708:
709: va_end(ap);
1.94 kristaps 710:
1.172 kristaps 711: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 712:
1.93 kristaps 713: if (HTML_AUTOCLOSE & htmltags[tag].flags)
1.197 schwarze 714: print_byte(h, '/');
1.93 kristaps 715:
1.197 schwarze 716: print_byte(h, '>');
1.14 kristaps 717:
1.196 schwarze 718: if (tflags & HTML_NLBEGIN)
1.197 schwarze 719: print_endline(h);
1.196 schwarze 720: else
721: h->flags |= HTML_NOSPACE;
1.117 kristaps 722:
1.196 schwarze 723: if (tflags & HTML_INDENT)
724: h->indent++;
725: if (tflags & HTML_NOINDENT)
726: h->noindent++;
1.117 kristaps 727:
1.188 schwarze 728: return t;
1.14 kristaps 729: }
730:
1.29 kristaps 731: static void
1.184 schwarze 732: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 733: {
1.196 schwarze 734: int tflags;
1.156 schwarze 735:
1.184 schwarze 736: /*
737: * Remember to close out and nullify the current
738: * meta-font and table, if applicable.
739: */
740: if (tag == h->metaf)
741: h->metaf = NULL;
742: if (tag == h->tblt)
743: h->tblt = NULL;
744:
1.196 schwarze 745: tflags = htmltags[tag->tag].flags;
746:
747: if (tflags & HTML_INDENT)
748: h->indent--;
749: if (tflags & HTML_NOINDENT)
750: h->noindent--;
751: if (tflags & HTML_NLEND)
1.197 schwarze 752: print_endline(h);
753: print_indent(h);
754: print_byte(h, '<');
755: print_byte(h, '/');
756: print_word(h, htmltags[tag->tag].name);
757: print_byte(h, '>');
1.196 schwarze 758: if (tflags & HTML_NLAFTER)
1.197 schwarze 759: print_endline(h);
1.184 schwarze 760:
1.204 schwarze 761: h->tag = tag->next;
1.184 schwarze 762: free(tag);
1.14 kristaps 763: }
764:
1.51 kristaps 765: void
1.93 kristaps 766: print_gen_decls(struct html *h)
1.1 kristaps 767: {
1.197 schwarze 768: print_word(h, "<!DOCTYPE html>");
769: print_endline(h);
1.1 kristaps 770: }
771:
1.51 kristaps 772: void
1.104 kristaps 773: print_text(struct html *h, const char *word)
1.1 kristaps 774: {
1.197 schwarze 775: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 776: if ( ! (HTML_KEEP & h->flags)) {
777: if (HTML_PREKEEP & h->flags)
778: h->flags |= HTML_KEEP;
1.197 schwarze 779: print_endword(h);
1.105 kristaps 780: } else
1.197 schwarze 781: print_word(h, " ");
1.105 kristaps 782: }
1.30 kristaps 783:
1.122 kristaps 784: assert(NULL == h->metaf);
1.152 schwarze 785: switch (h->metac) {
1.156 schwarze 786: case HTMLFONT_ITALIC:
1.194 schwarze 787: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 788: break;
1.156 schwarze 789: case HTMLFONT_BOLD:
1.194 schwarze 790: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 791: break;
1.156 schwarze 792: case HTMLFONT_BI:
1.194 schwarze 793: h->metaf = print_otag(h, TAG_B, "");
794: print_otag(h, TAG_I, "");
1.152 schwarze 795: break;
796: default:
1.197 schwarze 797: print_indent(h);
1.152 schwarze 798: break;
799: }
1.122 kristaps 800:
1.104 kristaps 801: assert(word);
1.195 schwarze 802: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 803: if ( ! (h->flags & HTML_NONOSPACE))
804: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 805: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 806: } else
1.183 schwarze 807: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 808:
809: if (h->metaf) {
810: print_tagq(h, h->metaf);
811: h->metaf = NULL;
812: }
1.113 schwarze 813:
814: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 815: }
1.30 kristaps 816:
1.51 kristaps 817: void
1.30 kristaps 818: print_tagq(struct html *h, const struct tag *until)
819: {
820: struct tag *tag;
821:
1.204 schwarze 822: while ((tag = h->tag) != NULL) {
1.184 schwarze 823: print_ctag(h, tag);
1.30 kristaps 824: if (until && tag == until)
825: return;
826: }
827: }
828:
1.51 kristaps 829: void
1.30 kristaps 830: print_stagq(struct html *h, const struct tag *suntil)
831: {
832: struct tag *tag;
833:
1.204 schwarze 834: while ((tag = h->tag) != NULL) {
1.30 kristaps 835: if (suntil && tag == suntil)
836: return;
1.184 schwarze 837: print_ctag(h, tag);
1.30 kristaps 838: }
839: }
1.171 kristaps 840:
841: void
842: print_paragraph(struct html *h)
843: {
844: struct tag *t;
845:
1.198 schwarze 846: t = print_otag(h, TAG_DIV, "c", "Pp");
1.171 kristaps 847: print_tagq(h, t);
848: }
849:
1.197 schwarze 850:
851: /***********************************************************************
852: * Low level output functions.
853: * They implement line breaking using a short static buffer.
854: ***********************************************************************/
855:
856: /*
857: * Buffer one HTML output byte.
858: * If the buffer is full, flush and deactivate it and start a new line.
859: * If the buffer is inactive, print directly.
860: */
861: static void
862: print_byte(struct html *h, char c)
863: {
864: if ((h->flags & HTML_BUFFER) == 0) {
865: putchar(c);
866: h->col++;
867: return;
868: }
869:
870: if (h->col + h->bufcol < sizeof(h->buf)) {
871: h->buf[h->bufcol++] = c;
872: return;
873: }
874:
875: putchar('\n');
876: h->col = 0;
877: print_indent(h);
878: putchar(' ');
879: putchar(' ');
880: fwrite(h->buf, h->bufcol, 1, stdout);
881: putchar(c);
882: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
883: h->bufcol = 0;
884: h->flags &= ~HTML_BUFFER;
885: }
886:
1.196 schwarze 887: /*
888: * If something was printed on the current output line, end it.
1.197 schwarze 889: * Not to be called right after print_indent().
1.196 schwarze 890: */
1.202 schwarze 891: void
1.197 schwarze 892: print_endline(struct html *h)
1.196 schwarze 893: {
1.197 schwarze 894: if (h->col == 0)
1.196 schwarze 895: return;
896:
1.197 schwarze 897: if (h->bufcol) {
898: putchar(' ');
899: fwrite(h->buf, h->bufcol, 1, stdout);
900: h->bufcol = 0;
901: }
1.196 schwarze 902: putchar('\n');
1.197 schwarze 903: h->col = 0;
904: h->flags |= HTML_NOSPACE;
905: h->flags &= ~HTML_BUFFER;
906: }
907:
908: /*
909: * Flush the HTML output buffer.
910: * If it is inactive, activate it.
911: */
912: static void
913: print_endword(struct html *h)
914: {
915: if (h->noindent) {
916: print_byte(h, ' ');
917: return;
918: }
919:
920: if ((h->flags & HTML_BUFFER) == 0) {
921: h->col++;
922: h->flags |= HTML_BUFFER;
923: } else if (h->bufcol) {
924: putchar(' ');
925: fwrite(h->buf, h->bufcol, 1, stdout);
926: h->col += h->bufcol + 1;
927: }
928: h->bufcol = 0;
1.196 schwarze 929: }
930:
931: /*
932: * If at the beginning of a new output line,
933: * perform indentation and mark the line as containing output.
934: * Make sure to really produce some output right afterwards,
935: * but do not use print_otag() for producing it.
936: */
937: static void
1.197 schwarze 938: print_indent(struct html *h)
1.196 schwarze 939: {
1.197 schwarze 940: size_t i;
1.196 schwarze 941:
1.197 schwarze 942: if (h->col)
1.196 schwarze 943: return;
944:
1.197 schwarze 945: if (h->noindent == 0) {
946: h->col = h->indent * 2;
947: for (i = 0; i < h->col; i++)
1.196 schwarze 948: putchar(' ');
1.197 schwarze 949: }
950: h->flags &= ~HTML_NOSPACE;
951: }
952:
953: /*
954: * Print or buffer some characters
955: * depending on the current HTML output buffer state.
956: */
957: static void
958: print_word(struct html *h, const char *cp)
959: {
960: while (*cp != '\0')
961: print_byte(h, *cp++);
1.196 schwarze 962: }
1.194 schwarze 963:
964: /*
965: * Calculate the scaling unit passed in a `-width' argument. This uses
966: * either a native scaling unit (e.g., 1i, 2m) or the string length of
967: * the value.
968: */
969: static void
970: a2width(const char *p, struct roffsu *su)
971: {
1.213 schwarze 972: const char *end;
973:
974: end = a2roffsu(p, su, SCALE_MAX);
975: if (end == NULL || *end != '\0') {
1.194 schwarze 976: su->unit = SCALE_EN;
977: su->scale = html_strlen(p);
978: } else if (su->scale < 0.0)
979: su->scale = 0.0;
1.68 kristaps 980: }
CVSweb