Annotation of mandoc/html.c, Revision 1.279
1.279 ! schwarze 1: /* $Id: html.c,v 1.278 2022/07/06 14:34:59 schwarze Exp $ */
1.1 kristaps 2: /*
1.176 schwarze 3: * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.274 schwarze 4: * Copyright (c) 2011-2015, 2017-2021 Ingo Schwarze <schwarze@openbsd.org>
1.277 schwarze 5: * Copyright (c) 2022 Anna Vyalkova <cyber@sysrq.in>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.186 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.29 kristaps 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.186 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.29 kristaps 14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.264 schwarze 18: *
19: * Common functions for mandoc(1) HTML formatters.
20: * For use by individual formatters and by the main program.
1.1 kristaps 21: */
1.92 kristaps 22: #include "config.h"
23:
1.41 kristaps 24: #include <sys/types.h>
1.240 schwarze 25: #include <sys/stat.h>
1.30 kristaps 26:
1.1 kristaps 27: #include <assert.h>
1.68 kristaps 28: #include <ctype.h>
1.76 kristaps 29: #include <stdarg.h>
1.229 schwarze 30: #include <stddef.h>
1.29 kristaps 31: #include <stdio.h>
1.63 kristaps 32: #include <stdint.h>
1.1 kristaps 33: #include <stdlib.h>
1.33 kristaps 34: #include <string.h>
1.45 kristaps 35: #include <unistd.h>
1.1 kristaps 36:
1.210 schwarze 37: #include "mandoc_aux.h"
1.229 schwarze 38: #include "mandoc_ohash.h"
1.100 kristaps 39: #include "mandoc.h"
1.210 schwarze 40: #include "roff.h"
1.58 kristaps 41: #include "out.h"
1.51 kristaps 42: #include "html.h"
1.186 schwarze 43: #include "manconf.h"
1.64 kristaps 44: #include "main.h"
1.63 kristaps 45:
1.29 kristaps 46: struct htmldata {
1.63 kristaps 47: const char *name;
1.29 kristaps 48: int flags;
1.257 schwarze 49: #define HTML_INPHRASE (1 << 0) /* Can appear in phrasing context. */
50: #define HTML_TOPHRASE (1 << 1) /* Establishes phrasing context. */
51: #define HTML_NOSTACK (1 << 2) /* Does not have an end tag. */
52: #define HTML_NLBEFORE (1 << 3) /* Output line break before opening. */
53: #define HTML_NLBEGIN (1 << 4) /* Output line break after opening. */
54: #define HTML_NLEND (1 << 5) /* Output line break before closing. */
55: #define HTML_NLAFTER (1 << 6) /* Output line break after closing. */
1.196 schwarze 56: #define HTML_NLAROUND (HTML_NLBEFORE | HTML_NLAFTER)
57: #define HTML_NLINSIDE (HTML_NLBEGIN | HTML_NLEND)
58: #define HTML_NLALL (HTML_NLAROUND | HTML_NLINSIDE)
1.257 schwarze 59: #define HTML_INDENT (1 << 7) /* Indent content by two spaces. */
60: #define HTML_NOINDENT (1 << 8) /* Exception: never indent content. */
1.29 kristaps 61: };
1.7 kristaps 62:
1.29 kristaps 63: static const struct htmldata htmltags[TAG_MAX] = {
1.196 schwarze 64: {"html", HTML_NLALL},
65: {"head", HTML_NLALL | HTML_INDENT},
1.257 schwarze 66: {"meta", HTML_NOSTACK | HTML_NLALL},
67: {"link", HTML_NOSTACK | HTML_NLALL},
68: {"style", HTML_NLALL | HTML_INDENT},
69: {"title", HTML_NLAROUND},
1.196 schwarze 70: {"body", HTML_NLALL},
1.277 schwarze 71: {"main", HTML_NLALL},
1.196 schwarze 72: {"div", HTML_NLAROUND},
1.253 schwarze 73: {"section", HTML_NLALL},
1.276 schwarze 74: {"nav", HTML_NLALL},
1.196 schwarze 75: {"table", HTML_NLALL | HTML_INDENT},
76: {"tr", HTML_NLALL | HTML_INDENT},
77: {"td", HTML_NLAROUND},
78: {"li", HTML_NLAROUND | HTML_INDENT},
79: {"ul", HTML_NLALL | HTML_INDENT},
80: {"ol", HTML_NLALL | HTML_INDENT},
81: {"dl", HTML_NLALL | HTML_INDENT},
82: {"dt", HTML_NLAROUND},
83: {"dd", HTML_NLAROUND | HTML_INDENT},
1.257 schwarze 84: {"h2", HTML_TOPHRASE | HTML_NLAROUND},
1.278 schwarze 85: {"h3", HTML_TOPHRASE | HTML_NLAROUND},
1.257 schwarze 86: {"p", HTML_TOPHRASE | HTML_NLAROUND | HTML_INDENT},
1.271 schwarze 87: {"pre", HTML_TOPHRASE | HTML_NLAROUND | HTML_NOINDENT},
1.257 schwarze 88: {"a", HTML_INPHRASE | HTML_TOPHRASE},
89: {"b", HTML_INPHRASE | HTML_TOPHRASE},
90: {"cite", HTML_INPHRASE | HTML_TOPHRASE},
91: {"code", HTML_INPHRASE | HTML_TOPHRASE},
92: {"i", HTML_INPHRASE | HTML_TOPHRASE},
93: {"small", HTML_INPHRASE | HTML_TOPHRASE},
94: {"span", HTML_INPHRASE | HTML_TOPHRASE},
95: {"var", HTML_INPHRASE | HTML_TOPHRASE},
96: {"br", HTML_INPHRASE | HTML_NOSTACK | HTML_NLALL},
1.275 schwarze 97: {"hr", HTML_INPHRASE | HTML_NOSTACK},
1.263 schwarze 98: {"mark", HTML_INPHRASE },
1.257 schwarze 99: {"math", HTML_INPHRASE | HTML_NLALL | HTML_INDENT},
1.196 schwarze 100: {"mrow", 0},
101: {"mi", 0},
1.215 schwarze 102: {"mn", 0},
1.196 schwarze 103: {"mo", 0},
104: {"msup", 0},
105: {"msub", 0},
106: {"msubsup", 0},
107: {"mfrac", 0},
108: {"msqrt", 0},
109: {"mfenced", 0},
110: {"mtable", 0},
111: {"mtr", 0},
112: {"mtd", 0},
113: {"munderover", 0},
114: {"munder", 0},
115: {"mover", 0},
1.90 kristaps 116: };
117:
1.229 schwarze 118: /* Avoid duplicate HTML id= attributes. */
1.269 schwarze 119:
120: struct id_entry {
121: int ord; /* Ordinal number of the latest occurrence. */
122: char id[]; /* The id= attribute without any ordinal suffix. */
123: };
1.229 schwarze 124: static struct ohash id_unique;
125:
1.254 schwarze 126: static void html_reset_internal(struct html *);
1.197 schwarze 127: static void print_byte(struct html *, char);
128: static void print_endword(struct html *);
129: static void print_indent(struct html *);
130: static void print_word(struct html *, const char *);
131:
1.184 schwarze 132: static void print_ctag(struct html *, struct tag *);
1.197 schwarze 133: static int print_escape(struct html *, char);
1.195 schwarze 134: static int print_encode(struct html *, const char *, const char *, int);
135: static void print_href(struct html *, const char *, const char *, int);
1.255 schwarze 136: static void print_metaf(struct html *);
1.82 kristaps 137:
1.156 schwarze 138:
1.180 schwarze 139: void *
1.191 schwarze 140: html_alloc(const struct manoutput *outopts)
1.10 kristaps 141: {
1.30 kristaps 142: struct html *h;
143:
1.128 kristaps 144: h = mandoc_calloc(1, sizeof(struct html));
1.10 kristaps 145:
1.204 schwarze 146: h->tag = NULL;
1.272 schwarze 147: h->metac = h->metal = ESCAPE_FONTROMAN;
1.186 schwarze 148: h->style = outopts->style;
1.240 schwarze 149: if ((h->base_man1 = outopts->man) == NULL)
150: h->base_man2 = NULL;
151: else if ((h->base_man2 = strchr(h->base_man1, ';')) != NULL)
152: *h->base_man2++ = '\0';
1.186 schwarze 153: h->base_includes = outopts->includes;
154: if (outopts->fragment)
155: h->oflags |= HTML_FRAGMENT;
1.241 schwarze 156: if (outopts->toc)
157: h->oflags |= HTML_TOC;
1.43 kristaps 158:
1.269 schwarze 159: mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
1.229 schwarze 160:
1.188 schwarze 161: return h;
1.29 kristaps 162: }
1.10 kristaps 163:
1.254 schwarze 164: static void
165: html_reset_internal(struct html *h)
1.29 kristaps 166: {
1.30 kristaps 167: struct tag *tag;
1.269 schwarze 168: struct id_entry *entry;
1.229 schwarze 169: unsigned int slot;
1.30 kristaps 170:
1.204 schwarze 171: while ((tag = h->tag) != NULL) {
172: h->tag = tag->next;
1.30 kristaps 173: free(tag);
174: }
1.269 schwarze 175: entry = ohash_first(&id_unique, &slot);
176: while (entry != NULL) {
177: free(entry);
178: entry = ohash_next(&id_unique, &slot);
1.229 schwarze 179: }
180: ohash_delete(&id_unique);
1.254 schwarze 181: }
182:
183: void
184: html_reset(void *p)
185: {
186: html_reset_internal(p);
1.269 schwarze 187: mandoc_ohash_init(&id_unique, 4, offsetof(struct id_entry, id));
1.254 schwarze 188: }
189:
190: void
191: html_free(void *p)
192: {
193: html_reset_internal(p);
194: free(p);
1.10 kristaps 195: }
1.2 kristaps 196:
1.51 kristaps 197: void
1.29 kristaps 198: print_gen_head(struct html *h)
199: {
1.165 kristaps 200: struct tag *t;
1.41 kristaps 201:
1.194 schwarze 202: print_otag(h, TAG_META, "?", "charset", "utf-8");
1.273 schwarze 203: print_otag(h, TAG_META, "??", "name", "viewport",
204: "content", "width=device-width, initial-scale=1.0");
1.222 schwarze 205: if (h->style != NULL) {
206: print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
207: h->style, "type", "text/css", "media", "all");
208: return;
209: }
1.165 kristaps 210:
1.168 kristaps 211: /*
1.222 schwarze 212: * Print a minimal embedded style sheet.
1.168 kristaps 213: */
1.196 schwarze 214:
1.194 schwarze 215: t = print_otag(h, TAG_STYLE, "");
1.196 schwarze 216: print_text(h, "table.head, table.foot { width: 100%; }");
1.197 schwarze 217: print_endline(h);
1.196 schwarze 218: print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
1.197 schwarze 219: print_endline(h);
1.196 schwarze 220: print_text(h, "td.head-vol { text-align: center; }");
1.197 schwarze 221: print_endline(h);
1.256 schwarze 222: print_text(h, ".Nd, .Bf, .Op { display: inline; }");
1.225 schwarze 223: print_endline(h);
1.256 schwarze 224: print_text(h, ".Pa, .Ad { font-style: italic; }");
1.226 schwarze 225: print_endline(h);
1.256 schwarze 226: print_text(h, ".Ms { font-weight: bold; }");
1.228 schwarze 227: print_endline(h);
1.256 schwarze 228: print_text(h, ".Bl-diag ");
1.224 schwarze 229: print_byte(h, '>');
230: print_text(h, " dt { font-weight: bold; }");
1.223 schwarze 231: print_endline(h);
1.256 schwarze 232: print_text(h, "code.Nm, .Fl, .Cm, .Ic, code.In, .Fd, .Fn, .Cd "
233: "{ font-weight: bold; font-family: inherit; }");
1.165 kristaps 234: print_tagq(h, t);
1.4 kristaps 235: }
236:
1.255 schwarze 237: int
238: html_setfont(struct html *h, enum mandoc_esc font)
1.88 kristaps 239: {
1.255 schwarze 240: switch (font) {
1.156 schwarze 241: case ESCAPE_FONTPREV:
1.90 kristaps 242: font = h->metal;
1.88 kristaps 243: break;
1.156 schwarze 244: case ESCAPE_FONTITALIC:
245: case ESCAPE_FONTBOLD:
246: case ESCAPE_FONTBI:
1.255 schwarze 247: case ESCAPE_FONTROMAN:
1.274 schwarze 248: case ESCAPE_FONTCR:
249: case ESCAPE_FONTCB:
250: case ESCAPE_FONTCI:
1.242 schwarze 251: break;
1.156 schwarze 252: case ESCAPE_FONT:
1.255 schwarze 253: font = ESCAPE_FONTROMAN;
1.88 kristaps 254: break;
255: default:
1.255 schwarze 256: return 0;
1.88 kristaps 257: }
1.255 schwarze 258: h->metal = h->metac;
259: h->metac = font;
260: return 1;
261: }
1.88 kristaps 262:
1.255 schwarze 263: static void
264: print_metaf(struct html *h)
265: {
1.122 kristaps 266: if (h->metaf) {
267: print_tagq(h, h->metaf);
268: h->metaf = NULL;
269: }
1.255 schwarze 270: switch (h->metac) {
271: case ESCAPE_FONTITALIC:
1.194 schwarze 272: h->metaf = print_otag(h, TAG_I, "");
1.152 schwarze 273: break;
1.255 schwarze 274: case ESCAPE_FONTBOLD:
1.194 schwarze 275: h->metaf = print_otag(h, TAG_B, "");
1.152 schwarze 276: break;
1.255 schwarze 277: case ESCAPE_FONTBI:
1.194 schwarze 278: h->metaf = print_otag(h, TAG_B, "");
279: print_otag(h, TAG_I, "");
1.152 schwarze 280: break;
1.274 schwarze 281: case ESCAPE_FONTCR:
1.242 schwarze 282: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
283: break;
1.274 schwarze 284: case ESCAPE_FONTCB:
285: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
286: print_otag(h, TAG_B, "");
287: break;
288: case ESCAPE_FONTCI:
289: h->metaf = print_otag(h, TAG_SPAN, "c", "Li");
290: print_otag(h, TAG_I, "");
291: break;
1.152 schwarze 292: default:
293: break;
294: }
1.248 schwarze 295: }
296:
1.249 schwarze 297: void
298: html_close_paragraph(struct html *h)
299: {
1.259 schwarze 300: struct tag *this, *next;
301: int flags;
1.249 schwarze 302:
1.259 schwarze 303: this = h->tag;
304: for (;;) {
305: next = this->next;
306: flags = htmltags[this->tag].flags;
307: if (flags & (HTML_INPHRASE | HTML_TOPHRASE))
308: print_ctag(h, this);
309: if ((flags & HTML_INPHRASE) == 0)
1.249 schwarze 310: break;
1.259 schwarze 311: this = next;
1.249 schwarze 312: }
313: }
314:
1.248 schwarze 315: /*
316: * ROFF_nf switches to no-fill mode, ROFF_fi to fill mode.
317: * TOKEN_NONE does not switch. The old mode is returned.
318: */
319: enum roff_tok
320: html_fillmode(struct html *h, enum roff_tok want)
321: {
322: struct tag *t;
323: enum roff_tok had;
324:
325: for (t = h->tag; t != NULL; t = t->next)
326: if (t->tag == TAG_PRE)
327: break;
328:
329: had = t == NULL ? ROFF_fi : ROFF_nf;
330:
331: if (want != had) {
332: switch (want) {
333: case ROFF_fi:
334: print_tagq(h, t);
335: break;
336: case ROFF_nf:
1.249 schwarze 337: html_close_paragraph(h);
1.248 schwarze 338: print_otag(h, TAG_PRE, "");
339: break;
340: case TOKEN_NONE:
341: break;
342: default:
343: abort();
344: }
345: }
346: return had;
1.210 schwarze 347: }
348:
1.264 schwarze 349: /*
350: * Allocate a string to be used for the "id=" attribute of an HTML
351: * element and/or as a segment identifier for a URI in an <a> element.
352: * The function may fail and return NULL if the node lacks text data
353: * to create the attribute from.
1.269 schwarze 354: * The caller is responsible for free(3)ing the returned string.
355: *
1.264 schwarze 356: * If the "unique" argument is non-zero, the "id_unique" ohash table
1.269 schwarze 357: * is used for de-duplication. If the "unique" argument is 1,
358: * it is the first time the function is called for this tag and
359: * location, so if an ordinal suffix is needed, it is incremented.
360: * If the "unique" argument is 2, it is the second time the function
361: * is called for this tag and location, so the ordinal suffix
362: * remains unchanged.
1.264 schwarze 363: */
1.210 schwarze 364: char *
1.229 schwarze 365: html_make_id(const struct roff_node *n, int unique)
1.210 schwarze 366: {
367: const struct roff_node *nch;
1.269 schwarze 368: struct id_entry *entry;
369: char *buf, *cp;
370: size_t len;
1.229 schwarze 371: unsigned int slot;
1.210 schwarze 372:
1.267 schwarze 373: if (n->tag != NULL)
374: buf = mandoc_strdup(n->tag);
1.264 schwarze 375: else {
376: switch (n->tok) {
377: case MDOC_Sh:
378: case MDOC_Ss:
379: case MDOC_Sx:
380: case MAN_SH:
381: case MAN_SS:
382: for (nch = n->child; nch != NULL; nch = nch->next)
383: if (nch->type != ROFFT_TEXT)
384: return NULL;
385: buf = NULL;
386: deroff(&buf, n);
387: if (buf == NULL)
388: return NULL;
389: break;
390: default:
1.265 schwarze 391: if (n->child == NULL || n->child->type != ROFFT_TEXT)
1.264 schwarze 392: return NULL;
393: buf = mandoc_strdup(n->child->string);
394: break;
395: }
396: }
1.210 schwarze 397:
1.230 schwarze 398: /*
399: * In ID attributes, only use ASCII characters that are
400: * permitted in URL-fragment strings according to the
401: * explicit list at:
402: * https://url.spec.whatwg.org/#url-fragment-string
1.270 schwarze 403: * In addition, reserve '~' for ordinal suffixes.
1.230 schwarze 404: */
1.210 schwarze 405:
1.279 ! schwarze 406: for (cp = buf; *cp != '\0'; cp++) {
! 407: if (*cp == ASCII_HYPH)
! 408: *cp = '-';
! 409: else if (isalnum((unsigned char)*cp) == 0 &&
1.270 schwarze 410: strchr("!$&'()*+,-./:;=?@_", *cp) == NULL)
1.210 schwarze 411: *cp = '_';
1.279 ! schwarze 412: }
1.210 schwarze 413:
1.229 schwarze 414: if (unique == 0)
415: return buf;
416:
417: /* Avoid duplicate HTML id= attributes. */
418:
419: slot = ohash_qlookup(&id_unique, buf);
1.269 schwarze 420: if ((entry = ohash_find(&id_unique, slot)) == NULL) {
421: len = strlen(buf) + 1;
422: entry = mandoc_malloc(sizeof(*entry) + len);
423: entry->ord = 1;
424: memcpy(entry->id, buf, len);
425: ohash_insert(&id_unique, slot, entry);
426: } else if (unique == 1)
427: entry->ord++;
428:
429: if (entry->ord > 1) {
430: cp = buf;
1.270 schwarze 431: mandoc_asprintf(&buf, "%s~%d", cp, entry->ord);
1.269 schwarze 432: free(cp);
1.229 schwarze 433: }
1.210 schwarze 434: return buf;
1.88 kristaps 435: }
436:
1.85 kristaps 437: static int
1.197 schwarze 438: print_escape(struct html *h, char c)
1.159 schwarze 439: {
440:
441: switch (c) {
442: case '<':
1.197 schwarze 443: print_word(h, "<");
1.159 schwarze 444: break;
445: case '>':
1.197 schwarze 446: print_word(h, ">");
1.159 schwarze 447: break;
448: case '&':
1.197 schwarze 449: print_word(h, "&");
1.159 schwarze 450: break;
451: case '"':
1.197 schwarze 452: print_word(h, """);
1.159 schwarze 453: break;
454: case ASCII_NBRSP:
1.197 schwarze 455: print_word(h, " ");
1.159 schwarze 456: break;
457: case ASCII_HYPH:
1.197 schwarze 458: print_byte(h, '-');
1.189 schwarze 459: break;
1.159 schwarze 460: case ASCII_BREAK:
461: break;
462: default:
1.188 schwarze 463: return 0;
1.159 schwarze 464: }
1.188 schwarze 465: return 1;
1.159 schwarze 466: }
467:
468: static int
1.195 schwarze 469: print_encode(struct html *h, const char *p, const char *pend, int norecurse)
1.29 kristaps 470: {
1.197 schwarze 471: char numbuf[16];
1.214 schwarze 472: const char *seq;
1.77 kristaps 473: size_t sz;
1.214 schwarze 474: int c, len, breakline, nospace;
1.132 kristaps 475: enum mandoc_esc esc;
1.214 schwarze 476: static const char rejs[10] = { ' ', '\\', '<', '>', '&', '"',
1.154 schwarze 477: ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
1.14 kristaps 478:
1.195 schwarze 479: if (pend == NULL)
480: pend = strchr(p, '\0');
481:
1.214 schwarze 482: breakline = 0;
1.85 kristaps 483: nospace = 0;
484:
1.195 schwarze 485: while (p < pend) {
1.151 schwarze 486: if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
487: h->flags &= ~HTML_SKIPCHAR;
488: p++;
489: continue;
490: }
491:
1.197 schwarze 492: for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
1.214 schwarze 493: print_byte(h, *p);
494:
495: if (breakline &&
496: (p >= pend || *p == ' ' || *p == ASCII_NBRSP)) {
1.245 schwarze 497: print_otag(h, TAG_BR, "");
1.214 schwarze 498: breakline = 0;
499: while (p < pend && (*p == ' ' || *p == ASCII_NBRSP))
500: p++;
501: continue;
502: }
1.77 kristaps 503:
1.195 schwarze 504: if (p >= pend)
1.132 kristaps 505: break;
506:
1.214 schwarze 507: if (*p == ' ') {
508: print_endword(h);
509: p++;
510: continue;
511: }
512:
1.197 schwarze 513: if (print_escape(h, *p++))
1.154 schwarze 514: continue;
1.77 kristaps 515:
1.132 kristaps 516: esc = mandoc_escape(&p, &seq, &len);
517: switch (esc) {
1.156 schwarze 518: case ESCAPE_FONT:
519: case ESCAPE_FONTPREV:
520: case ESCAPE_FONTBOLD:
521: case ESCAPE_FONTITALIC:
522: case ESCAPE_FONTBI:
523: case ESCAPE_FONTROMAN:
1.274 schwarze 524: case ESCAPE_FONTCR:
525: case ESCAPE_FONTCB:
526: case ESCAPE_FONTCI:
1.243 schwarze 527: if (0 == norecurse) {
528: h->flags |= HTML_NOSPACE;
1.255 schwarze 529: if (html_setfont(h, esc))
530: print_metaf(h);
1.243 schwarze 531: h->flags &= ~HTML_NOSPACE;
532: }
1.151 schwarze 533: continue;
1.156 schwarze 534: case ESCAPE_SKIPCHAR:
1.151 schwarze 535: h->flags |= HTML_SKIPCHAR;
536: continue;
1.246 schwarze 537: case ESCAPE_ERROR:
538: continue;
1.151 schwarze 539: default:
540: break;
541: }
542:
543: if (h->flags & HTML_SKIPCHAR) {
544: h->flags &= ~HTML_SKIPCHAR;
545: continue;
546: }
547:
548: switch (esc) {
1.156 schwarze 549: case ESCAPE_UNICODE:
1.159 schwarze 550: /* Skip past "u" header. */
1.144 kristaps 551: c = mchars_num2uc(seq + 1, len - 1);
552: break;
1.156 schwarze 553: case ESCAPE_NUMBERED:
1.141 kristaps 554: c = mchars_num2char(seq, len);
1.181 schwarze 555: if (c < 0)
556: continue;
1.82 kristaps 557: break;
1.156 schwarze 558: case ESCAPE_SPECIAL:
1.191 schwarze 559: c = mchars_spec2cp(seq, len);
1.181 schwarze 560: if (c <= 0)
561: continue;
1.246 schwarze 562: break;
563: case ESCAPE_UNDEF:
564: c = *seq;
1.132 kristaps 565: break;
1.239 schwarze 566: case ESCAPE_DEVICE:
567: print_word(h, "html");
568: continue;
1.214 schwarze 569: case ESCAPE_BREAK:
570: breakline = 1;
571: continue;
1.156 schwarze 572: case ESCAPE_NOSPACE:
1.132 kristaps 573: if ('\0' == *p)
574: nospace = 1;
1.179 schwarze 575: continue;
1.185 schwarze 576: case ESCAPE_OVERSTRIKE:
577: if (len == 0)
578: continue;
579: c = seq[len - 1];
580: break;
1.82 kristaps 581: default:
1.179 schwarze 582: continue;
1.82 kristaps 583: }
1.181 schwarze 584: if ((c < 0x20 && c != 0x09) ||
585: (c > 0x7E && c < 0xA0))
1.179 schwarze 586: c = 0xFFFD;
1.197 schwarze 587: if (c > 0x7E) {
1.216 schwarze 588: (void)snprintf(numbuf, sizeof(numbuf), "&#x%.4X;", c);
1.197 schwarze 589: print_word(h, numbuf);
590: } else if (print_escape(h, c) == 0)
591: print_byte(h, c);
1.32 kristaps 592: }
1.85 kristaps 593:
1.188 schwarze 594: return nospace;
1.14 kristaps 595: }
596:
1.94 kristaps 597: static void
1.195 schwarze 598: print_href(struct html *h, const char *name, const char *sec, int man)
1.94 kristaps 599: {
1.240 schwarze 600: struct stat sb;
1.195 schwarze 601: const char *p, *pp;
1.240 schwarze 602: char *filename;
603:
604: if (man) {
605: pp = h->base_man1;
606: if (h->base_man2 != NULL) {
607: mandoc_asprintf(&filename, "%s.%s", name, sec);
608: if (stat(filename, &sb) == -1)
609: pp = h->base_man2;
610: free(filename);
611: }
612: } else
613: pp = h->base_includes;
1.195 schwarze 614:
615: while ((p = strchr(pp, '%')) != NULL) {
616: print_encode(h, pp, p, 1);
617: if (man && p[1] == 'S') {
618: if (sec == NULL)
1.197 schwarze 619: print_byte(h, '1');
1.195 schwarze 620: else
621: print_encode(h, sec, NULL, 1);
622: } else if ((man && p[1] == 'N') ||
623: (man == 0 && p[1] == 'I'))
624: print_encode(h, name, NULL, 1);
625: else
626: print_encode(h, p, p + 2, 1);
627: pp = p + 2;
628: }
629: if (*pp != '\0')
630: print_encode(h, pp, NULL, 1);
1.94 kristaps 631: }
632:
1.51 kristaps 633: struct tag *
1.194 schwarze 634: print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
1.14 kristaps 635: {
1.194 schwarze 636: va_list ap;
1.30 kristaps 637: struct tag *t;
1.195 schwarze 638: const char *attr;
1.203 schwarze 639: char *arg1, *arg2;
1.244 schwarze 640: int style_written, tflags;
1.196 schwarze 641:
642: tflags = htmltags[tag].flags;
1.30 kristaps 643:
1.257 schwarze 644: /* Flow content is not allowed in phrasing context. */
645:
646: if ((tflags & HTML_INPHRASE) == 0) {
647: for (t = h->tag; t != NULL; t = t->next) {
648: if (t->closed)
649: continue;
650: assert((htmltags[t->tag].flags & HTML_TOPHRASE) == 0);
651: break;
652: }
1.260 schwarze 653:
654: /*
655: * Always wrap phrasing elements in a paragraph
656: * unless already contained in some flow container;
657: * never put them directly into a section.
658: */
659:
660: } else if (tflags & HTML_TOPHRASE && h->tag->tag == TAG_SECTION)
661: print_otag(h, TAG_P, "c", "Pp");
1.257 schwarze 662:
1.204 schwarze 663: /* Push this tag onto the stack of open scopes. */
1.94 kristaps 664:
1.196 schwarze 665: if ((tflags & HTML_NOSTACK) == 0) {
1.128 kristaps 666: t = mandoc_malloc(sizeof(struct tag));
1.30 kristaps 667: t->tag = tag;
1.204 schwarze 668: t->next = h->tag;
1.252 schwarze 669: t->refcnt = 0;
670: t->closed = 0;
1.204 schwarze 671: h->tag = t;
1.30 kristaps 672: } else
673: t = NULL;
1.29 kristaps 674:
1.196 schwarze 675: if (tflags & HTML_NLBEFORE)
1.197 schwarze 676: print_endline(h);
677: if (h->col == 0)
678: print_indent(h);
1.196 schwarze 679: else if ((h->flags & HTML_NOSPACE) == 0) {
680: if (h->flags & HTML_KEEP)
1.216 schwarze 681: print_word(h, " ");
1.196 schwarze 682: else {
683: if (h->flags & HTML_PREKEEP)
684: h->flags |= HTML_KEEP;
1.197 schwarze 685: print_endword(h);
1.105 kristaps 686: }
1.196 schwarze 687: }
1.29 kristaps 688:
1.109 kristaps 689: if ( ! (h->flags & HTML_NONOSPACE))
690: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 691: else
692: h->flags |= HTML_NOSPACE;
1.109 kristaps 693:
1.94 kristaps 694: /* Print out the tag name and attributes. */
695:
1.197 schwarze 696: print_byte(h, '<');
697: print_word(h, htmltags[tag].name);
1.194 schwarze 698:
699: va_start(ap, fmt);
700:
1.244 schwarze 701: while (*fmt != '\0' && *fmt != 's') {
1.203 schwarze 702:
1.238 schwarze 703: /* Parse attributes and arguments. */
1.203 schwarze 704:
705: arg1 = va_arg(ap, char *);
1.238 schwarze 706: arg2 = NULL;
1.194 schwarze 707: switch (*fmt++) {
708: case 'c':
1.195 schwarze 709: attr = "class";
1.194 schwarze 710: break;
711: case 'h':
1.195 schwarze 712: attr = "href";
1.194 schwarze 713: break;
714: case 'i':
1.195 schwarze 715: attr = "id";
1.276 schwarze 716: break;
717: case 'r':
718: attr = "role";
1.194 schwarze 719: break;
720: case '?':
1.203 schwarze 721: attr = arg1;
722: arg1 = va_arg(ap, char *);
1.194 schwarze 723: break;
724: default:
725: abort();
726: }
1.203 schwarze 727: if (*fmt == 'M')
728: arg2 = va_arg(ap, char *);
729: if (arg1 == NULL)
730: continue;
731:
1.238 schwarze 732: /* Print the attributes. */
1.203 schwarze 733:
1.197 schwarze 734: print_byte(h, ' ');
735: print_word(h, attr);
736: print_byte(h, '=');
737: print_byte(h, '"');
1.195 schwarze 738: switch (*fmt) {
1.208 schwarze 739: case 'I':
740: print_href(h, arg1, NULL, 0);
741: fmt++;
742: break;
1.195 schwarze 743: case 'M':
1.203 schwarze 744: print_href(h, arg1, arg2, 1);
1.195 schwarze 745: fmt++;
746: break;
1.208 schwarze 747: case 'R':
748: print_byte(h, '#');
749: print_encode(h, arg1, NULL, 1);
1.195 schwarze 750: fmt++;
1.208 schwarze 751: break;
1.195 schwarze 752: default:
1.244 schwarze 753: print_encode(h, arg1, NULL, 1);
1.195 schwarze 754: break;
755: }
1.197 schwarze 756: print_byte(h, '"');
1.194 schwarze 757: }
1.244 schwarze 758:
759: style_written = 0;
760: while (*fmt++ == 's') {
761: arg1 = va_arg(ap, char *);
762: arg2 = va_arg(ap, char *);
763: if (arg2 == NULL)
764: continue;
765: print_byte(h, ' ');
766: if (style_written == 0) {
767: print_word(h, "style=\"");
768: style_written = 1;
769: }
770: print_word(h, arg1);
771: print_byte(h, ':');
772: print_byte(h, ' ');
773: print_word(h, arg2);
774: print_byte(h, ';');
775: }
776: if (style_written)
777: print_byte(h, '"');
778:
1.194 schwarze 779: va_end(ap);
1.94 kristaps 780:
1.172 kristaps 781: /* Accommodate for "well-formed" singleton escaping. */
1.94 kristaps 782:
1.257 schwarze 783: if (htmltags[tag].flags & HTML_NOSTACK)
1.197 schwarze 784: print_byte(h, '/');
1.93 kristaps 785:
1.197 schwarze 786: print_byte(h, '>');
1.14 kristaps 787:
1.196 schwarze 788: if (tflags & HTML_NLBEGIN)
1.197 schwarze 789: print_endline(h);
1.196 schwarze 790: else
791: h->flags |= HTML_NOSPACE;
1.117 kristaps 792:
1.196 schwarze 793: if (tflags & HTML_INDENT)
794: h->indent++;
795: if (tflags & HTML_NOINDENT)
796: h->noindent++;
1.117 kristaps 797:
1.188 schwarze 798: return t;
1.264 schwarze 799: }
800:
801: /*
802: * Print an element with an optional "id=" attribute.
1.265 schwarze 803: * If the element has phrasing content and an "id=" attribute,
804: * also add a permalink: outside if it can be in phrasing context,
805: * inside otherwise.
1.264 schwarze 806: */
807: struct tag *
808: print_otag_id(struct html *h, enum htmltag elemtype, const char *cattr,
809: struct roff_node *n)
810: {
1.265 schwarze 811: struct roff_node *nch;
1.264 schwarze 812: struct tag *ret, *t;
1.266 schwarze 813: char *id, *href;
1.264 schwarze 814:
815: ret = NULL;
1.266 schwarze 816: id = href = NULL;
1.264 schwarze 817: if (n->flags & NODE_ID)
818: id = html_make_id(n, 1);
1.266 schwarze 819: if (n->flags & NODE_HREF)
1.269 schwarze 820: href = id == NULL ? html_make_id(n, 2) : id;
1.266 schwarze 821: if (href != NULL && htmltags[elemtype].flags & HTML_INPHRASE)
822: ret = print_otag(h, TAG_A, "chR", "permalink", href);
1.264 schwarze 823: t = print_otag(h, elemtype, "ci", cattr, id);
824: if (ret == NULL) {
825: ret = t;
1.266 schwarze 826: if (href != NULL && (nch = n->child) != NULL) {
1.265 schwarze 827: /* man(7) is safe, it tags phrasing content only. */
828: if (n->tok > MDOC_MAX ||
829: htmltags[elemtype].flags & HTML_TOPHRASE)
830: nch = NULL;
831: else /* For mdoc(7), beware of nested blocks. */
832: while (nch != NULL && nch->type == ROFFT_TEXT)
833: nch = nch->next;
834: if (nch == NULL)
1.266 schwarze 835: print_otag(h, TAG_A, "chR", "permalink", href);
1.265 schwarze 836: }
1.264 schwarze 837: }
1.269 schwarze 838: free(id);
1.266 schwarze 839: if (id == NULL)
840: free(href);
1.264 schwarze 841: return ret;
1.14 kristaps 842: }
843:
1.29 kristaps 844: static void
1.184 schwarze 845: print_ctag(struct html *h, struct tag *tag)
1.14 kristaps 846: {
1.196 schwarze 847: int tflags;
1.156 schwarze 848:
1.252 schwarze 849: if (tag->closed == 0) {
850: tag->closed = 1;
851: if (tag == h->metaf)
852: h->metaf = NULL;
853: if (tag == h->tblt)
854: h->tblt = NULL;
855:
856: tflags = htmltags[tag->tag].flags;
857: if (tflags & HTML_INDENT)
858: h->indent--;
859: if (tflags & HTML_NOINDENT)
860: h->noindent--;
861: if (tflags & HTML_NLEND)
862: print_endline(h);
863: print_indent(h);
864: print_byte(h, '<');
865: print_byte(h, '/');
866: print_word(h, htmltags[tag->tag].name);
867: print_byte(h, '>');
868: if (tflags & HTML_NLAFTER)
869: print_endline(h);
870: }
871: if (tag->refcnt == 0) {
872: h->tag = tag->next;
873: free(tag);
874: }
1.14 kristaps 875: }
876:
1.51 kristaps 877: void
1.93 kristaps 878: print_gen_decls(struct html *h)
1.1 kristaps 879: {
1.197 schwarze 880: print_word(h, "<!DOCTYPE html>");
881: print_endline(h);
1.221 schwarze 882: }
883:
884: void
885: print_gen_comment(struct html *h, struct roff_node *n)
886: {
887: int wantblank;
888:
889: print_word(h, "<!-- This is an automatically generated file."
890: " Do not edit.");
891: h->indent = 1;
892: wantblank = 0;
893: while (n != NULL && n->type == ROFFT_COMMENT) {
894: if (strstr(n->string, "-->") == NULL &&
895: (wantblank || *n->string != '\0')) {
896: print_endline(h);
897: print_indent(h);
898: print_word(h, n->string);
899: wantblank = *n->string != '\0';
900: }
901: n = n->next;
902: }
903: if (wantblank)
904: print_endline(h);
905: print_word(h, " -->");
906: print_endline(h);
907: h->indent = 0;
1.1 kristaps 908: }
909:
1.51 kristaps 910: void
1.104 kristaps 911: print_text(struct html *h, const char *word)
1.1 kristaps 912: {
1.268 schwarze 913: print_tagged_text(h, word, NULL);
914: }
915:
916: void
917: print_tagged_text(struct html *h, const char *word, struct roff_node *n)
918: {
919: struct tag *t;
920: char *href;
921:
1.260 schwarze 922: /*
923: * Always wrap text in a paragraph unless already contained in
924: * some flow container; never put it directly into a section.
925: */
926:
927: if (h->tag->tag == TAG_SECTION)
928: print_otag(h, TAG_P, "c", "Pp");
929:
930: /* Output whitespace before this text? */
931:
1.197 schwarze 932: if (h->col && (h->flags & HTML_NOSPACE) == 0) {
1.105 kristaps 933: if ( ! (HTML_KEEP & h->flags)) {
934: if (HTML_PREKEEP & h->flags)
935: h->flags |= HTML_KEEP;
1.197 schwarze 936: print_endword(h);
1.105 kristaps 937: } else
1.216 schwarze 938: print_word(h, " ");
1.105 kristaps 939: }
1.260 schwarze 940:
941: /*
1.268 schwarze 942: * Optionally switch fonts, optionally write a permalink, then
943: * print the text, optionally surrounded by HTML whitespace.
1.260 schwarze 944: */
1.30 kristaps 945:
1.255 schwarze 946: assert(h->metaf == NULL);
947: print_metaf(h);
948: print_indent(h);
1.268 schwarze 949:
1.269 schwarze 950: if (n != NULL && (href = html_make_id(n, 2)) != NULL) {
1.268 schwarze 951: t = print_otag(h, TAG_A, "chR", "permalink", href);
952: free(href);
953: } else
954: t = NULL;
955:
1.195 schwarze 956: if ( ! print_encode(h, word, NULL, 0)) {
1.109 kristaps 957: if ( ! (h->flags & HTML_NONOSPACE))
958: h->flags &= ~HTML_NOSPACE;
1.183 schwarze 959: h->flags &= ~HTML_NONEWLINE;
1.149 kristaps 960: } else
1.183 schwarze 961: h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
1.122 kristaps 962:
1.255 schwarze 963: if (h->metaf != NULL) {
1.122 kristaps 964: print_tagq(h, h->metaf);
965: h->metaf = NULL;
1.268 schwarze 966: } else if (t != NULL)
967: print_tagq(h, t);
1.113 schwarze 968:
969: h->flags &= ~HTML_IGNDELIM;
1.1 kristaps 970: }
1.30 kristaps 971:
1.51 kristaps 972: void
1.30 kristaps 973: print_tagq(struct html *h, const struct tag *until)
974: {
1.252 schwarze 975: struct tag *this, *next;
1.30 kristaps 976:
1.252 schwarze 977: for (this = h->tag; this != NULL; this = next) {
978: next = this == until ? NULL : this->next;
979: print_ctag(h, this);
1.30 kristaps 980: }
981: }
982:
1.250 schwarze 983: /*
984: * Close out all open elements up to but excluding suntil.
985: * Note that a paragraph just inside stays open together with it
986: * because paragraphs include subsequent phrasing content.
987: */
1.51 kristaps 988: void
1.30 kristaps 989: print_stagq(struct html *h, const struct tag *suntil)
990: {
1.252 schwarze 991: struct tag *this, *next;
1.30 kristaps 992:
1.252 schwarze 993: for (this = h->tag; this != NULL; this = next) {
994: next = this->next;
995: if (this == suntil || (next == suntil &&
996: (this->tag == TAG_P || this->tag == TAG_PRE)))
997: break;
998: print_ctag(h, this);
1.30 kristaps 999: }
1.171 kristaps 1000: }
1001:
1.197 schwarze 1002:
1003: /***********************************************************************
1004: * Low level output functions.
1005: * They implement line breaking using a short static buffer.
1006: ***********************************************************************/
1007:
1008: /*
1009: * Buffer one HTML output byte.
1010: * If the buffer is full, flush and deactivate it and start a new line.
1011: * If the buffer is inactive, print directly.
1012: */
1013: static void
1014: print_byte(struct html *h, char c)
1015: {
1016: if ((h->flags & HTML_BUFFER) == 0) {
1017: putchar(c);
1018: h->col++;
1019: return;
1020: }
1021:
1022: if (h->col + h->bufcol < sizeof(h->buf)) {
1023: h->buf[h->bufcol++] = c;
1024: return;
1025: }
1026:
1027: putchar('\n');
1028: h->col = 0;
1029: print_indent(h);
1030: putchar(' ');
1031: putchar(' ');
1032: fwrite(h->buf, h->bufcol, 1, stdout);
1033: putchar(c);
1034: h->col = (h->indent + 1) * 2 + h->bufcol + 1;
1035: h->bufcol = 0;
1036: h->flags &= ~HTML_BUFFER;
1037: }
1038:
1.196 schwarze 1039: /*
1040: * If something was printed on the current output line, end it.
1.197 schwarze 1041: * Not to be called right after print_indent().
1.196 schwarze 1042: */
1.202 schwarze 1043: void
1.197 schwarze 1044: print_endline(struct html *h)
1.196 schwarze 1045: {
1.197 schwarze 1046: if (h->col == 0)
1.196 schwarze 1047: return;
1048:
1.197 schwarze 1049: if (h->bufcol) {
1050: putchar(' ');
1051: fwrite(h->buf, h->bufcol, 1, stdout);
1052: h->bufcol = 0;
1053: }
1.196 schwarze 1054: putchar('\n');
1.197 schwarze 1055: h->col = 0;
1056: h->flags |= HTML_NOSPACE;
1057: h->flags &= ~HTML_BUFFER;
1058: }
1059:
1060: /*
1061: * Flush the HTML output buffer.
1062: * If it is inactive, activate it.
1063: */
1064: static void
1065: print_endword(struct html *h)
1066: {
1067: if (h->noindent) {
1068: print_byte(h, ' ');
1069: return;
1070: }
1071:
1072: if ((h->flags & HTML_BUFFER) == 0) {
1073: h->col++;
1074: h->flags |= HTML_BUFFER;
1075: } else if (h->bufcol) {
1076: putchar(' ');
1077: fwrite(h->buf, h->bufcol, 1, stdout);
1078: h->col += h->bufcol + 1;
1079: }
1080: h->bufcol = 0;
1.196 schwarze 1081: }
1082:
1083: /*
1084: * If at the beginning of a new output line,
1085: * perform indentation and mark the line as containing output.
1086: * Make sure to really produce some output right afterwards,
1087: * but do not use print_otag() for producing it.
1088: */
1089: static void
1.197 schwarze 1090: print_indent(struct html *h)
1.196 schwarze 1091: {
1.197 schwarze 1092: size_t i;
1.196 schwarze 1093:
1.261 schwarze 1094: if (h->col || h->noindent)
1.196 schwarze 1095: return;
1096:
1.261 schwarze 1097: h->col = h->indent * 2;
1098: for (i = 0; i < h->col; i++)
1099: putchar(' ');
1.197 schwarze 1100: }
1101:
1102: /*
1103: * Print or buffer some characters
1104: * depending on the current HTML output buffer state.
1105: */
1106: static void
1107: print_word(struct html *h, const char *cp)
1108: {
1109: while (*cp != '\0')
1110: print_byte(h, *cp++);
1.68 kristaps 1111: }
CVSweb