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