]> git.cameronkatri.com Git - mandoc.git/blob - html.c
Add support for `Sm' in -T[x]html -mdoc.
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.109 2010/07/23 00:08:57 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "mandoc.h"
33 #include "out.h"
34 #include "chars.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 {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
61 {"tr", HTML_CLRLINE}, /* TAG_TR */
62 {"td", HTML_CLRLINE}, /* TAG_TD */
63 {"li", HTML_CLRLINE}, /* TAG_LI */
64 {"ul", HTML_CLRLINE}, /* TAG_UL */
65 {"ol", HTML_CLRLINE}, /* TAG_OL */
66 };
67
68 static const char *const htmlfonts[HTMLFONT_MAX] = {
69 "roman",
70 "bold",
71 "italic"
72 };
73
74 static const char *const htmlattrs[ATTR_MAX] = {
75 "http-equiv",
76 "content",
77 "name",
78 "rel",
79 "href",
80 "type",
81 "media",
82 "class",
83 "style",
84 "width",
85 "valign",
86 "target",
87 "id",
88 "summary",
89 };
90
91 static void print_spec(struct html *, enum roffdeco,
92 const char *, size_t);
93 static void print_res(struct html *, const char *, size_t);
94 static void print_ctag(struct html *, enum htmltag);
95 static void print_doctype(struct html *);
96 static void print_xmltype(struct html *);
97 static int print_encode(struct html *, const char *, int);
98 static void print_metaf(struct html *, enum roffdeco);
99 static void print_attr(struct html *,
100 const char *, const char *);
101 static void *ml_alloc(char *, enum htmltype);
102
103
104 static void *
105 ml_alloc(char *outopts, enum htmltype type)
106 {
107 struct html *h;
108 const char *toks[4];
109 char *v;
110
111 toks[0] = "style";
112 toks[1] = "man";
113 toks[2] = "includes";
114 toks[3] = NULL;
115
116 h = calloc(1, sizeof(struct html));
117 if (NULL == h) {
118 perror(NULL);
119 exit(EXIT_FAILURE);
120 }
121
122 h->type = type;
123 h->tags.head = NULL;
124 h->ords.head = NULL;
125 h->symtab = chars_init(CHARS_HTML);
126
127 while (outopts && *outopts)
128 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
129 case (0):
130 h->style = v;
131 break;
132 case (1):
133 h->base_man = v;
134 break;
135 case (2):
136 h->base_includes = v;
137 break;
138 default:
139 break;
140 }
141
142 return(h);
143 }
144
145 void *
146 html_alloc(char *outopts)
147 {
148
149 return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
150 }
151
152
153 void *
154 xhtml_alloc(char *outopts)
155 {
156
157 return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
158 }
159
160
161 void
162 html_free(void *p)
163 {
164 struct tag *tag;
165 struct ord *ord;
166 struct html *h;
167
168 h = (struct html *)p;
169
170 while ((ord = h->ords.head) != NULL) {
171 h->ords.head = ord->next;
172 free(ord);
173 }
174
175 while ((tag = h->tags.head) != NULL) {
176 h->tags.head = tag->next;
177 free(tag);
178 }
179
180 if (h->symtab)
181 chars_free(h->symtab);
182
183 free(h);
184 }
185
186
187 void
188 print_gen_head(struct html *h)
189 {
190 struct htmlpair tag[4];
191
192 tag[0].key = ATTR_HTTPEQUIV;
193 tag[0].val = "Content-Type";
194 tag[1].key = ATTR_CONTENT;
195 tag[1].val = "text/html; charset=utf-8";
196 print_otag(h, TAG_META, 2, tag);
197
198 tag[0].key = ATTR_NAME;
199 tag[0].val = "resource-type";
200 tag[1].key = ATTR_CONTENT;
201 tag[1].val = "document";
202 print_otag(h, TAG_META, 2, tag);
203
204 if (h->style) {
205 tag[0].key = ATTR_REL;
206 tag[0].val = "stylesheet";
207 tag[1].key = ATTR_HREF;
208 tag[1].val = h->style;
209 tag[2].key = ATTR_TYPE;
210 tag[2].val = "text/css";
211 tag[3].key = ATTR_MEDIA;
212 tag[3].val = "all";
213 print_otag(h, TAG_LINK, 4, tag);
214 }
215 }
216
217
218 static void
219 print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
220 {
221 int cp;
222 const char *rhs;
223 size_t sz;
224
225 if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
226 printf("&#%d;", cp);
227 return;
228 } else if (-1 == cp && DECO_SSPECIAL == d) {
229 fwrite(p, 1, len, stdout);
230 return;
231 } else if (-1 == cp)
232 return;
233
234 if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
235 fwrite(rhs, 1, sz, stdout);
236 }
237
238
239 static void
240 print_res(struct html *h, const char *p, size_t len)
241 {
242 int cp;
243 const char *rhs;
244 size_t sz;
245
246 if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
247 printf("&#%d;", cp);
248 return;
249 } else if (-1 == cp)
250 return;
251
252 if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
253 fwrite(rhs, 1, sz, stdout);
254 }
255
256
257 struct tag *
258 print_ofont(struct html *h, enum htmlfont font)
259 {
260 struct htmlpair tag;
261
262 h->metal = h->metac;
263 h->metac = font;
264
265 /* FIXME: DECO_ROMAN should just close out preexisting. */
266
267 if (h->metaf && h->tags.head == h->metaf)
268 print_tagq(h, h->metaf);
269
270 PAIR_CLASS_INIT(&tag, htmlfonts[font]);
271 h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
272 return(h->metaf);
273 }
274
275
276 static void
277 print_metaf(struct html *h, enum roffdeco deco)
278 {
279 enum htmlfont font;
280
281 switch (deco) {
282 case (DECO_PREVIOUS):
283 font = h->metal;
284 break;
285 case (DECO_ITALIC):
286 font = HTMLFONT_ITALIC;
287 break;
288 case (DECO_BOLD):
289 font = HTMLFONT_BOLD;
290 break;
291 case (DECO_ROMAN):
292 font = HTMLFONT_NONE;
293 break;
294 default:
295 abort();
296 /* NOTREACHED */
297 }
298
299 (void)print_ofont(h, font);
300 }
301
302
303 static int
304 print_encode(struct html *h, const char *p, int norecurse)
305 {
306 size_t sz;
307 int len, nospace;
308 const char *seq;
309 enum roffdeco deco;
310 static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
311
312 nospace = 0;
313
314 for (; *p; p++) {
315 sz = strcspn(p, rejs);
316
317 fwrite(p, 1, sz, stdout);
318 p += /* LINTED */
319 sz;
320
321 if ('<' == *p) {
322 printf("&lt;");
323 continue;
324 } else if ('>' == *p) {
325 printf("&gt;");
326 continue;
327 } else if ('&' == *p) {
328 printf("&amp;");
329 continue;
330 } else if (ASCII_HYPH == *p) {
331 /*
332 * Note: "soft hyphens" aren't graphically
333 * displayed when not breaking the text; we want
334 * them to be displayed.
335 */
336 /*printf("&#173;");*/
337 putchar('-');
338 continue;
339 } else if ('\0' == *p)
340 break;
341
342 seq = ++p;
343 len = a2roffdeco(&deco, &seq, &sz);
344
345 switch (deco) {
346 case (DECO_RESERVED):
347 print_res(h, seq, sz);
348 break;
349 case (DECO_SSPECIAL):
350 /* FALLTHROUGH */
351 case (DECO_SPECIAL):
352 print_spec(h, deco, seq, sz);
353 break;
354 case (DECO_PREVIOUS):
355 /* FALLTHROUGH */
356 case (DECO_BOLD):
357 /* FALLTHROUGH */
358 case (DECO_ITALIC):
359 /* FALLTHROUGH */
360 case (DECO_ROMAN):
361 if (norecurse)
362 break;
363 print_metaf(h, deco);
364 break;
365 default:
366 break;
367 }
368
369 p += len - 1;
370
371 if (DECO_NOSPACE == deco && '\0' == *(p + 1))
372 nospace = 1;
373 }
374
375 return(nospace);
376 }
377
378
379 static void
380 print_attr(struct html *h, const char *key, const char *val)
381 {
382 printf(" %s=\"", key);
383 (void)print_encode(h, val, 1);
384 putchar('\"');
385 }
386
387
388 struct tag *
389 print_otag(struct html *h, enum htmltag tag,
390 int sz, const struct htmlpair *p)
391 {
392 int i;
393 struct tag *t;
394
395 /* Push this tags onto the stack of open scopes. */
396
397 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
398 t = malloc(sizeof(struct tag));
399 if (NULL == t) {
400 perror(NULL);
401 exit(EXIT_FAILURE);
402 }
403 t->tag = tag;
404 t->next = h->tags.head;
405 h->tags.head = t;
406 } else
407 t = NULL;
408
409 if ( ! (HTML_NOSPACE & h->flags))
410 if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
411 /* Manage keeps! */
412 if ( ! (HTML_KEEP & h->flags)) {
413 if (HTML_PREKEEP & h->flags)
414 h->flags |= HTML_KEEP;
415 putchar(' ');
416 } else
417 printf("&#160;");
418 }
419
420 if ( ! (h->flags & HTML_NONOSPACE))
421 h->flags &= ~HTML_NOSPACE;
422
423 /* Print out the tag name and attributes. */
424
425 printf("<%s", htmltags[tag].name);
426 for (i = 0; i < sz; i++)
427 print_attr(h, htmlattrs[p[i].key], p[i].val);
428
429 /* Add non-overridable attributes. */
430
431 if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
432 print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
433 print_attr(h, "xml:lang", "en");
434 print_attr(h, "lang", "en");
435 }
436
437 /* Accomodate for XML "well-formed" singleton escaping. */
438
439 if (HTML_AUTOCLOSE & htmltags[tag].flags)
440 switch (h->type) {
441 case (HTML_XHTML_1_0_STRICT):
442 putchar('/');
443 break;
444 default:
445 break;
446 }
447
448 putchar('>');
449
450 h->flags |= HTML_NOSPACE;
451 return(t);
452 }
453
454
455 static void
456 print_ctag(struct html *h, enum htmltag tag)
457 {
458
459 printf("</%s>", htmltags[tag].name);
460 if (HTML_CLRLINE & htmltags[tag].flags) {
461 h->flags |= HTML_NOSPACE;
462 putchar('\n');
463 }
464 }
465
466
467 void
468 print_gen_decls(struct html *h)
469 {
470
471 print_xmltype(h);
472 print_doctype(h);
473 }
474
475
476 static void
477 print_xmltype(struct html *h)
478 {
479
480 if (HTML_XHTML_1_0_STRICT == h->type)
481 printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
482 }
483
484
485 static void
486 print_doctype(struct html *h)
487 {
488 const char *doctype;
489 const char *dtd;
490 const char *name;
491
492 switch (h->type) {
493 case (HTML_HTML_4_01_STRICT):
494 name = "HTML";
495 doctype = "-//W3C//DTD HTML 4.01//EN";
496 dtd = "http://www.w3.org/TR/html4/strict.dtd";
497 break;
498 default:
499 name = "html";
500 doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
501 dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
502 break;
503 }
504
505 printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
506 name, doctype, dtd);
507 }
508
509
510 void
511 print_text(struct html *h, const char *word)
512 {
513
514 if (word[0] && '\0' == word[1])
515 switch (word[0]) {
516 case('.'):
517 /* FALLTHROUGH */
518 case(','):
519 /* FALLTHROUGH */
520 case(';'):
521 /* FALLTHROUGH */
522 case(':'):
523 /* FALLTHROUGH */
524 case('?'):
525 /* FALLTHROUGH */
526 case('!'):
527 /* FALLTHROUGH */
528 case(')'):
529 /* FALLTHROUGH */
530 case(']'):
531 if ( ! (HTML_IGNDELIM & h->flags))
532 h->flags |= HTML_NOSPACE;
533 break;
534 default:
535 break;
536 }
537
538 if ( ! (HTML_NOSPACE & h->flags)) {
539 /* Manage keeps! */
540 if ( ! (HTML_KEEP & h->flags)) {
541 if (HTML_PREKEEP & h->flags)
542 h->flags |= HTML_KEEP;
543 putchar(' ');
544 } else
545 printf("&#160;");
546 }
547
548 assert(word);
549 if ( ! print_encode(h, word, 0))
550 if ( ! (h->flags & HTML_NONOSPACE))
551 h->flags &= ~HTML_NOSPACE;
552
553 /*
554 * Note that we don't process the pipe: the parser sees it as
555 * punctuation, but we don't in terms of typography.
556 */
557 if (word[0] && '\0' == word[1])
558 switch (word[0]) {
559 case('('):
560 /* FALLTHROUGH */
561 case('['):
562 h->flags |= HTML_NOSPACE;
563 break;
564 default:
565 break;
566 }
567 }
568
569
570 void
571 print_tagq(struct html *h, const struct tag *until)
572 {
573 struct tag *tag;
574
575 while ((tag = h->tags.head) != NULL) {
576 if (tag == h->metaf)
577 h->metaf = NULL;
578 print_ctag(h, tag->tag);
579 h->tags.head = tag->next;
580 free(tag);
581 if (until && tag == until)
582 return;
583 }
584 }
585
586
587 void
588 print_stagq(struct html *h, const struct tag *suntil)
589 {
590 struct tag *tag;
591
592 while ((tag = h->tags.head) != NULL) {
593 if (suntil && tag == suntil)
594 return;
595 if (tag == h->metaf)
596 h->metaf = NULL;
597 print_ctag(h, tag->tag);
598 h->tags.head = tag->next;
599 free(tag);
600 }
601 }
602
603
604 void
605 bufinit(struct html *h)
606 {
607
608 h->buf[0] = '\0';
609 h->buflen = 0;
610 }
611
612
613 void
614 bufcat_style(struct html *h, const char *key, const char *val)
615 {
616
617 bufcat(h, key);
618 bufncat(h, ":", 1);
619 bufcat(h, val);
620 bufncat(h, ";", 1);
621 }
622
623
624 void
625 bufcat(struct html *h, const char *p)
626 {
627
628 bufncat(h, p, strlen(p));
629 }
630
631
632 void
633 buffmt(struct html *h, const char *fmt, ...)
634 {
635 va_list ap;
636
637 va_start(ap, fmt);
638 (void)vsnprintf(h->buf + (int)h->buflen,
639 BUFSIZ - h->buflen - 1, fmt, ap);
640 va_end(ap);
641 h->buflen = strlen(h->buf);
642 }
643
644
645 void
646 bufncat(struct html *h, const char *p, size_t sz)
647 {
648
649 if (h->buflen + sz > BUFSIZ - 1)
650 sz = BUFSIZ - 1 - h->buflen;
651
652 (void)strncat(h->buf, p, sz);
653 h->buflen += sz;
654 }
655
656
657 void
658 buffmt_includes(struct html *h, const char *name)
659 {
660 const char *p, *pp;
661
662 pp = h->base_includes;
663
664 while (NULL != (p = strchr(pp, '%'))) {
665 bufncat(h, pp, (size_t)(p - pp));
666 switch (*(p + 1)) {
667 case('I'):
668 bufcat(h, name);
669 break;
670 default:
671 bufncat(h, p, 2);
672 break;
673 }
674 pp = p + 2;
675 }
676 if (pp)
677 bufcat(h, pp);
678 }
679
680
681 void
682 buffmt_man(struct html *h,
683 const char *name, const char *sec)
684 {
685 const char *p, *pp;
686
687 pp = h->base_man;
688
689 /* LINTED */
690 while (NULL != (p = strchr(pp, '%'))) {
691 bufncat(h, pp, (size_t)(p - pp));
692 switch (*(p + 1)) {
693 case('S'):
694 bufcat(h, sec ? sec : "1");
695 break;
696 case('N'):
697 buffmt(h, name);
698 break;
699 default:
700 bufncat(h, p, 2);
701 break;
702 }
703 pp = p + 2;
704 }
705 if (pp)
706 bufcat(h, pp);
707 }
708
709
710 void
711 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
712 {
713 double v;
714 const char *u;
715
716 v = su->scale;
717
718 switch (su->unit) {
719 case (SCALE_CM):
720 u = "cm";
721 break;
722 case (SCALE_IN):
723 u = "in";
724 break;
725 case (SCALE_PC):
726 u = "pc";
727 break;
728 case (SCALE_PT):
729 u = "pt";
730 break;
731 case (SCALE_EM):
732 u = "em";
733 break;
734 case (SCALE_MM):
735 if (0 == (v /= 100))
736 v = 1;
737 u = "em";
738 break;
739 case (SCALE_EN):
740 u = "ex";
741 break;
742 case (SCALE_BU):
743 u = "ex";
744 break;
745 case (SCALE_VS):
746 u = "em";
747 break;
748 default:
749 u = "ex";
750 break;
751 }
752
753 /*
754 * XXX: the CSS spec isn't clear as to which types accept
755 * integer or real numbers, so we just make them all decimals.
756 */
757 buffmt(h, "%s: %.2f%s;", p, v, u);
758 }
759
760
761 void
762 html_idcat(char *dst, const char *src, int sz)
763 {
764 int ssz;
765
766 assert(sz);
767
768 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
769
770 for ( ; *dst != '\0' && sz; dst++, sz--)
771 /* Jump to end. */ ;
772
773 assert(sz > 2);
774
775 /* We can't start with a number (bah). */
776
777 *dst++ = 'x';
778 *dst = '\0';
779 sz--;
780
781 for ( ; *src != '\0' && sz > 1; src++) {
782 ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
783 sz -= ssz;
784 dst += ssz;
785 }
786 }