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