]> git.cameronkatri.com Git - mandoc.git/blob - html.c
integrate preconv(1) into mandoc(1);
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.176 2014/10/10 15:26:29 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "mandoc.h"
32 #include "mandoc_aux.h"
33 #include "libmandoc.h"
34 #include "out.h"
35 #include "html.h"
36 #include "main.h"
37
38 struct htmldata {
39 const char *name;
40 int flags;
41 #define HTML_CLRLINE (1 << 0)
42 #define HTML_NOSTACK (1 << 1)
43 #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
44 };
45
46 static const struct htmldata htmltags[TAG_MAX] = {
47 {"html", HTML_CLRLINE}, /* TAG_HTML */
48 {"head", HTML_CLRLINE}, /* TAG_HEAD */
49 {"body", HTML_CLRLINE}, /* TAG_BODY */
50 {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
51 {"title", HTML_CLRLINE}, /* TAG_TITLE */
52 {"div", HTML_CLRLINE}, /* TAG_DIV */
53 {"h1", 0}, /* TAG_H1 */
54 {"h2", 0}, /* TAG_H2 */
55 {"span", 0}, /* TAG_SPAN */
56 {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
57 {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
58 {"a", 0}, /* TAG_A */
59 {"table", HTML_CLRLINE}, /* TAG_TABLE */
60 {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
61 {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
62 {"tr", HTML_CLRLINE}, /* TAG_TR */
63 {"td", HTML_CLRLINE}, /* TAG_TD */
64 {"li", HTML_CLRLINE}, /* TAG_LI */
65 {"ul", HTML_CLRLINE}, /* TAG_UL */
66 {"ol", HTML_CLRLINE}, /* TAG_OL */
67 {"dl", HTML_CLRLINE}, /* TAG_DL */
68 {"dt", HTML_CLRLINE}, /* TAG_DT */
69 {"dd", HTML_CLRLINE}, /* TAG_DD */
70 {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
71 {"pre", HTML_CLRLINE }, /* TAG_PRE */
72 {"b", 0 }, /* TAG_B */
73 {"i", 0 }, /* TAG_I */
74 {"code", 0 }, /* TAG_CODE */
75 {"small", 0 }, /* TAG_SMALL */
76 {"style", HTML_CLRLINE}, /* TAG_STYLE */
77 {"math", HTML_CLRLINE}, /* TAG_MATH */
78 {"mrow", 0}, /* TAG_MROW */
79 {"mi", 0}, /* TAG_MI */
80 {"mo", 0}, /* TAG_MO */
81 {"msup", 0}, /* TAG_MSUP */
82 {"msub", 0}, /* TAG_MSUB */
83 {"msubsup", 0}, /* TAG_MSUBSUP */
84 {"mfrac", 0}, /* TAG_MFRAC */
85 {"msqrt", 0}, /* TAG_MSQRT */
86 {"mfenced", 0}, /* TAG_MFENCED */
87 {"mtable", 0}, /* TAG_MTABLE */
88 {"mtr", 0}, /* TAG_MTR */
89 {"mtd", 0}, /* TAG_MTD */
90 {"munderover", 0}, /* TAG_MUNDEROVER */
91 {"munder", 0}, /* TAG_MUNDER*/
92 {"mover", 0}, /* TAG_MOVER*/
93 };
94
95 static const char *const htmlattrs[ATTR_MAX] = {
96 "name", /* ATTR_NAME */
97 "rel", /* ATTR_REL */
98 "href", /* ATTR_HREF */
99 "type", /* ATTR_TYPE */
100 "media", /* ATTR_MEDIA */
101 "class", /* ATTR_CLASS */
102 "style", /* ATTR_STYLE */
103 "id", /* ATTR_ID */
104 "colspan", /* ATTR_COLSPAN */
105 "charset", /* ATTR_CHARSET */
106 "open", /* ATTR_OPEN */
107 "close", /* ATTR_CLOSE */
108 "mathvariant", /* ATTR_MATHVARIANT */
109 };
110
111 static const char *const roffscales[SCALE_MAX] = {
112 "cm", /* SCALE_CM */
113 "in", /* SCALE_IN */
114 "pc", /* SCALE_PC */
115 "pt", /* SCALE_PT */
116 "em", /* SCALE_EM */
117 "em", /* SCALE_MM */
118 "ex", /* SCALE_EN */
119 "ex", /* SCALE_BU */
120 "em", /* SCALE_VS */
121 "ex", /* SCALE_FS */
122 };
123
124 static void bufncat(struct html *, const char *, size_t);
125 static void print_ctag(struct html *, enum htmltag);
126 static int print_escape(char);
127 static int print_encode(struct html *, const char *, int);
128 static void print_metaf(struct html *, enum mandoc_esc);
129 static void print_attr(struct html *, const char *, const char *);
130 static void *ml_alloc(char *);
131
132
133 static void *
134 ml_alloc(char *outopts)
135 {
136 struct html *h;
137 const char *toks[5];
138 char *v;
139
140 toks[0] = "style";
141 toks[1] = "man";
142 toks[2] = "includes";
143 toks[3] = "fragment";
144 toks[4] = NULL;
145
146 h = mandoc_calloc(1, sizeof(struct html));
147
148 h->tags.head = NULL;
149 h->symtab = mchars_alloc();
150
151 while (outopts && *outopts)
152 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
153 case 0:
154 h->style = v;
155 break;
156 case 1:
157 h->base_man = v;
158 break;
159 case 2:
160 h->base_includes = v;
161 break;
162 case 3:
163 h->oflags |= HTML_FRAGMENT;
164 break;
165 default:
166 break;
167 }
168
169 return(h);
170 }
171
172 void *
173 html_alloc(char *outopts)
174 {
175
176 return(ml_alloc(outopts));
177 }
178
179 void *
180 xhtml_alloc(char *outopts)
181 {
182
183 return(ml_alloc(outopts));
184 }
185
186 void
187 html_free(void *p)
188 {
189 struct tag *tag;
190 struct html *h;
191
192 h = (struct html *)p;
193
194 while ((tag = h->tags.head) != NULL) {
195 h->tags.head = tag->next;
196 free(tag);
197 }
198
199 if (h->symtab)
200 mchars_free(h->symtab);
201
202 free(h);
203 }
204
205 void
206 print_gen_head(struct html *h)
207 {
208 struct htmlpair tag[4];
209 struct tag *t;
210
211 tag[0].key = ATTR_CHARSET;
212 tag[0].val = "utf-8";
213 print_otag(h, TAG_META, 1, tag);
214
215 /*
216 * Print a default style-sheet.
217 */
218 t = print_otag(h, TAG_STYLE, 0, NULL);
219 print_text(h, "table.head, table.foot { width: 100%; }\n"
220 "td.head-rtitle, td.foot-os { text-align: right; }\n"
221 "td.head-vol { text-align: center; }\n"
222 "table.foot td { width: 50%; }\n"
223 "table.head td { width: 33%; }\n"
224 "div.spacer { margin: 1em 0; }\n");
225 print_tagq(h, t);
226
227 if (h->style) {
228 tag[0].key = ATTR_REL;
229 tag[0].val = "stylesheet";
230 tag[1].key = ATTR_HREF;
231 tag[1].val = h->style;
232 tag[2].key = ATTR_TYPE;
233 tag[2].val = "text/css";
234 tag[3].key = ATTR_MEDIA;
235 tag[3].val = "all";
236 print_otag(h, TAG_LINK, 4, tag);
237 }
238 }
239
240 static void
241 print_metaf(struct html *h, enum mandoc_esc deco)
242 {
243 enum htmlfont font;
244
245 switch (deco) {
246 case ESCAPE_FONTPREV:
247 font = h->metal;
248 break;
249 case ESCAPE_FONTITALIC:
250 font = HTMLFONT_ITALIC;
251 break;
252 case ESCAPE_FONTBOLD:
253 font = HTMLFONT_BOLD;
254 break;
255 case ESCAPE_FONTBI:
256 font = HTMLFONT_BI;
257 break;
258 case ESCAPE_FONT:
259 /* FALLTHROUGH */
260 case ESCAPE_FONTROMAN:
261 font = HTMLFONT_NONE;
262 break;
263 default:
264 abort();
265 /* NOTREACHED */
266 }
267
268 if (h->metaf) {
269 print_tagq(h, h->metaf);
270 h->metaf = NULL;
271 }
272
273 h->metal = h->metac;
274 h->metac = font;
275
276 switch (font) {
277 case HTMLFONT_ITALIC:
278 h->metaf = print_otag(h, TAG_I, 0, NULL);
279 break;
280 case HTMLFONT_BOLD:
281 h->metaf = print_otag(h, TAG_B, 0, NULL);
282 break;
283 case HTMLFONT_BI:
284 h->metaf = print_otag(h, TAG_B, 0, NULL);
285 print_otag(h, TAG_I, 0, NULL);
286 break;
287 default:
288 break;
289 }
290 }
291
292 int
293 html_strlen(const char *cp)
294 {
295 size_t rsz;
296 int skip, sz;
297
298 /*
299 * Account for escaped sequences within string length
300 * calculations. This follows the logic in term_strlen() as we
301 * must calculate the width of produced strings.
302 * Assume that characters are always width of "1". This is
303 * hacky, but it gets the job done for approximation of widths.
304 */
305
306 sz = 0;
307 skip = 0;
308 while (1) {
309 rsz = strcspn(cp, "\\");
310 if (rsz) {
311 cp += rsz;
312 if (skip) {
313 skip = 0;
314 rsz--;
315 }
316 sz += rsz;
317 }
318 if ('\0' == *cp)
319 break;
320 cp++;
321 switch (mandoc_escape(&cp, NULL, NULL)) {
322 case ESCAPE_ERROR:
323 return(sz);
324 case ESCAPE_UNICODE:
325 /* FALLTHROUGH */
326 case ESCAPE_NUMBERED:
327 /* FALLTHROUGH */
328 case ESCAPE_SPECIAL:
329 if (skip)
330 skip = 0;
331 else
332 sz++;
333 break;
334 case ESCAPE_SKIPCHAR:
335 skip = 1;
336 break;
337 default:
338 break;
339 }
340 }
341 return(sz);
342 }
343
344 static int
345 print_escape(char c)
346 {
347
348 switch (c) {
349 case '<':
350 printf("&lt;");
351 break;
352 case '>':
353 printf("&gt;");
354 break;
355 case '&':
356 printf("&amp;");
357 break;
358 case '"':
359 printf("&quot;");
360 break;
361 case ASCII_NBRSP:
362 putchar('-');
363 break;
364 case ASCII_HYPH:
365 putchar('-');
366 /* FALLTHROUGH */
367 case ASCII_BREAK:
368 break;
369 default:
370 return(0);
371 }
372 return(1);
373 }
374
375 static int
376 print_encode(struct html *h, const char *p, int norecurse)
377 {
378 size_t sz;
379 int c, len, nospace;
380 const char *seq;
381 enum mandoc_esc esc;
382 static const char rejs[9] = { '\\', '<', '>', '&', '"',
383 ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
384
385 nospace = 0;
386
387 while ('\0' != *p) {
388 if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
389 h->flags &= ~HTML_SKIPCHAR;
390 p++;
391 continue;
392 }
393
394 sz = strcspn(p, rejs);
395
396 fwrite(p, 1, sz, stdout);
397 p += (int)sz;
398
399 if ('\0' == *p)
400 break;
401
402 if (print_escape(*p++))
403 continue;
404
405 esc = mandoc_escape(&p, &seq, &len);
406 if (ESCAPE_ERROR == esc)
407 break;
408
409 switch (esc) {
410 case ESCAPE_FONT:
411 /* FALLTHROUGH */
412 case ESCAPE_FONTPREV:
413 /* FALLTHROUGH */
414 case ESCAPE_FONTBOLD:
415 /* FALLTHROUGH */
416 case ESCAPE_FONTITALIC:
417 /* FALLTHROUGH */
418 case ESCAPE_FONTBI:
419 /* FALLTHROUGH */
420 case ESCAPE_FONTROMAN:
421 if (0 == norecurse)
422 print_metaf(h, esc);
423 continue;
424 case ESCAPE_SKIPCHAR:
425 h->flags |= HTML_SKIPCHAR;
426 continue;
427 default:
428 break;
429 }
430
431 if (h->flags & HTML_SKIPCHAR) {
432 h->flags &= ~HTML_SKIPCHAR;
433 continue;
434 }
435
436 switch (esc) {
437 case ESCAPE_UNICODE:
438 /* Skip past "u" header. */
439 c = mchars_num2uc(seq + 1, len - 1);
440 if ('\0' != c)
441 printf("&#x%x;", c);
442 break;
443 case ESCAPE_NUMBERED:
444 c = mchars_num2char(seq, len);
445 if ( ! ('\0' == c || print_escape(c)))
446 putchar(c);
447 break;
448 case ESCAPE_SPECIAL:
449 c = mchars_spec2cp(h->symtab, seq, len);
450 if (c > 0)
451 printf("&#%d;", c);
452 else if (-1 == c && 1 == len &&
453 !print_escape(*seq))
454 putchar((int)*seq);
455 break;
456 case ESCAPE_NOSPACE:
457 if ('\0' == *p)
458 nospace = 1;
459 break;
460 default:
461 break;
462 }
463 }
464
465 return(nospace);
466 }
467
468 static void
469 print_attr(struct html *h, const char *key, const char *val)
470 {
471 printf(" %s=\"", key);
472 (void)print_encode(h, val, 1);
473 putchar('\"');
474 }
475
476 struct tag *
477 print_otag(struct html *h, enum htmltag tag,
478 int sz, const struct htmlpair *p)
479 {
480 int i;
481 struct tag *t;
482
483 /* Push this tags onto the stack of open scopes. */
484
485 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
486 t = mandoc_malloc(sizeof(struct tag));
487 t->tag = tag;
488 t->next = h->tags.head;
489 h->tags.head = t;
490 } else
491 t = NULL;
492
493 if ( ! (HTML_NOSPACE & h->flags))
494 if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
495 /* Manage keeps! */
496 if ( ! (HTML_KEEP & h->flags)) {
497 if (HTML_PREKEEP & h->flags)
498 h->flags |= HTML_KEEP;
499 putchar(' ');
500 } else
501 printf("&#160;");
502 }
503
504 if ( ! (h->flags & HTML_NONOSPACE))
505 h->flags &= ~HTML_NOSPACE;
506 else
507 h->flags |= HTML_NOSPACE;
508
509 /* Print out the tag name and attributes. */
510
511 printf("<%s", htmltags[tag].name);
512 for (i = 0; i < sz; i++)
513 print_attr(h, htmlattrs[p[i].key], p[i].val);
514
515 /* Accommodate for "well-formed" singleton escaping. */
516
517 if (HTML_AUTOCLOSE & htmltags[tag].flags)
518 putchar('/');
519
520 putchar('>');
521
522 h->flags |= HTML_NOSPACE;
523
524 if ((HTML_AUTOCLOSE | HTML_CLRLINE) & htmltags[tag].flags)
525 putchar('\n');
526
527 return(t);
528 }
529
530 static void
531 print_ctag(struct html *h, enum htmltag tag)
532 {
533
534 printf("</%s>", htmltags[tag].name);
535 if (HTML_CLRLINE & htmltags[tag].flags) {
536 h->flags |= HTML_NOSPACE;
537 putchar('\n');
538 }
539 }
540
541 void
542 print_gen_decls(struct html *h)
543 {
544
545 puts("<!DOCTYPE html>");
546 }
547
548 void
549 print_text(struct html *h, const char *word)
550 {
551
552 if ( ! (HTML_NOSPACE & h->flags)) {
553 /* Manage keeps! */
554 if ( ! (HTML_KEEP & h->flags)) {
555 if (HTML_PREKEEP & h->flags)
556 h->flags |= HTML_KEEP;
557 putchar(' ');
558 } else
559 printf("&#160;");
560 }
561
562 assert(NULL == h->metaf);
563 switch (h->metac) {
564 case HTMLFONT_ITALIC:
565 h->metaf = print_otag(h, TAG_I, 0, NULL);
566 break;
567 case HTMLFONT_BOLD:
568 h->metaf = print_otag(h, TAG_B, 0, NULL);
569 break;
570 case HTMLFONT_BI:
571 h->metaf = print_otag(h, TAG_B, 0, NULL);
572 print_otag(h, TAG_I, 0, NULL);
573 break;
574 default:
575 break;
576 }
577
578 assert(word);
579 if ( ! print_encode(h, word, 0)) {
580 if ( ! (h->flags & HTML_NONOSPACE))
581 h->flags &= ~HTML_NOSPACE;
582 } else
583 h->flags |= HTML_NOSPACE;
584
585 if (h->metaf) {
586 print_tagq(h, h->metaf);
587 h->metaf = NULL;
588 }
589
590 h->flags &= ~HTML_IGNDELIM;
591 }
592
593 void
594 print_tagq(struct html *h, const struct tag *until)
595 {
596 struct tag *tag;
597
598 while ((tag = h->tags.head) != NULL) {
599 /*
600 * Remember to close out and nullify the current
601 * meta-font and table, if applicable.
602 */
603 if (tag == h->metaf)
604 h->metaf = NULL;
605 if (tag == h->tblt)
606 h->tblt = NULL;
607 print_ctag(h, tag->tag);
608 h->tags.head = tag->next;
609 free(tag);
610 if (until && tag == until)
611 return;
612 }
613 }
614
615 void
616 print_stagq(struct html *h, const struct tag *suntil)
617 {
618 struct tag *tag;
619
620 while ((tag = h->tags.head) != NULL) {
621 if (suntil && tag == suntil)
622 return;
623 /*
624 * Remember to close out and nullify the current
625 * meta-font and table, if applicable.
626 */
627 if (tag == h->metaf)
628 h->metaf = NULL;
629 if (tag == h->tblt)
630 h->tblt = NULL;
631 print_ctag(h, tag->tag);
632 h->tags.head = tag->next;
633 free(tag);
634 }
635 }
636
637 void
638 print_paragraph(struct html *h)
639 {
640 struct tag *t;
641 struct htmlpair tag;
642
643 PAIR_CLASS_INIT(&tag, "spacer");
644 t = print_otag(h, TAG_DIV, 1, &tag);
645 print_tagq(h, t);
646 }
647
648
649 void
650 bufinit(struct html *h)
651 {
652
653 h->buf[0] = '\0';
654 h->buflen = 0;
655 }
656
657 void
658 bufcat_style(struct html *h, const char *key, const char *val)
659 {
660
661 bufcat(h, key);
662 bufcat(h, ":");
663 bufcat(h, val);
664 bufcat(h, ";");
665 }
666
667 void
668 bufcat(struct html *h, const char *p)
669 {
670
671 /*
672 * XXX This is broken and not easy to fix.
673 * When using the -Oincludes option, buffmt_includes()
674 * may pass in strings overrunning BUFSIZ, causing a crash.
675 */
676
677 h->buflen = strlcat(h->buf, p, BUFSIZ);
678 assert(h->buflen < BUFSIZ);
679 }
680
681 void
682 bufcat_fmt(struct html *h, const char *fmt, ...)
683 {
684 va_list ap;
685
686 va_start(ap, fmt);
687 (void)vsnprintf(h->buf + (int)h->buflen,
688 BUFSIZ - h->buflen - 1, fmt, ap);
689 va_end(ap);
690 h->buflen = strlen(h->buf);
691 }
692
693 static void
694 bufncat(struct html *h, const char *p, size_t sz)
695 {
696
697 assert(h->buflen + sz + 1 < BUFSIZ);
698 strncat(h->buf, p, sz);
699 h->buflen += sz;
700 }
701
702 void
703 buffmt_includes(struct html *h, const char *name)
704 {
705 const char *p, *pp;
706
707 pp = h->base_includes;
708
709 bufinit(h);
710 while (NULL != (p = strchr(pp, '%'))) {
711 bufncat(h, pp, (size_t)(p - pp));
712 switch (*(p + 1)) {
713 case'I':
714 bufcat(h, name);
715 break;
716 default:
717 bufncat(h, p, 2);
718 break;
719 }
720 pp = p + 2;
721 }
722 if (pp)
723 bufcat(h, pp);
724 }
725
726 void
727 buffmt_man(struct html *h, const char *name, const char *sec)
728 {
729 const char *p, *pp;
730
731 pp = h->base_man;
732
733 bufinit(h);
734 while (NULL != (p = strchr(pp, '%'))) {
735 bufncat(h, pp, (size_t)(p - pp));
736 switch (*(p + 1)) {
737 case 'S':
738 bufcat(h, sec ? sec : "1");
739 break;
740 case 'N':
741 bufcat_fmt(h, "%s", name);
742 break;
743 default:
744 bufncat(h, p, 2);
745 break;
746 }
747 pp = p + 2;
748 }
749 if (pp)
750 bufcat(h, pp);
751 }
752
753 void
754 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
755 {
756 double v;
757
758 v = su->scale;
759 if (SCALE_MM == su->unit && 0.0 == (v /= 100.0))
760 v = 1.0;
761 else if (SCALE_BU == su->unit)
762 v /= 24.0;
763
764 bufcat_fmt(h, "%s: %.2f%s;", p, v, roffscales[su->unit]);
765 }
766
767 void
768 bufcat_id(struct html *h, const char *src)
769 {
770
771 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
772
773 while ('\0' != *src)
774 bufcat_fmt(h, "%.2x", *src++);
775 }