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