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