]> git.cameronkatri.com Git - mandoc.git/blob - html.c
Removed superfluous memset (thanks Joerg Sonnenberger).
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.69 2009/10/28 06:54:12 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 #include <sys/types.h>
18
19 #include <assert.h>
20 #include <ctype.h>
21 #include <err.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "out.h"
30 #include "chars.h"
31 #include "html.h"
32 #include "main.h"
33
34 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
35
36 #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
37 #define DTD "http://www.w3.org/TR/html4/strict.dtd"
38
39 struct htmldata {
40 const char *name;
41 int flags;
42 #define HTML_CLRLINE (1 << 0)
43 #define HTML_NOSTACK (1 << 1)
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}, /* 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 {"p", HTML_CLRLINE}, /* TAG_P */
56 {"span", 0}, /* TAG_SPAN */
57 {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
58 {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
59 {"a", 0}, /* TAG_A */
60 {"table", HTML_CLRLINE}, /* TAG_TABLE */
61 {"col", HTML_CLRLINE | HTML_NOSTACK}, /* 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 {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
68 };
69
70 static const char *const htmlattrs[ATTR_MAX] = {
71 "http-equiv",
72 "content",
73 "name",
74 "rel",
75 "href",
76 "type",
77 "media",
78 "class",
79 "style",
80 "width",
81 "valign",
82 "target",
83 "id",
84 "summary",
85 };
86
87 #ifdef __linux__
88 extern int getsubopt(char **, char * const *, char **);
89 #endif
90
91 void *
92 html_alloc(char *outopts)
93 {
94 struct html *h;
95 const char *toks[4];
96 char *v;
97
98 toks[0] = "style";
99 toks[1] = "man";
100 toks[2] = "includes";
101 toks[3] = NULL;
102
103 if (NULL == (h = calloc(1, sizeof(struct html))))
104 return(NULL);
105
106 h->tags.head = NULL;
107 h->ords.head = NULL;
108
109 if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
110 free(h);
111 return(NULL);
112 }
113
114 while (outopts && *outopts)
115 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
116 case (0):
117 h->style = v;
118 break;
119 case (1):
120 h->base_man = v;
121 break;
122 case (2):
123 h->base_includes = v;
124 break;
125 default:
126 break;
127 }
128
129 return(h);
130 }
131
132
133 void
134 html_free(void *p)
135 {
136 struct tag *tag;
137 struct ord *ord;
138 struct html *h;
139
140 h = (struct html *)p;
141
142 while ((ord = h->ords.head) != NULL) {
143 h->ords.head = ord->next;
144 free(ord);
145 }
146
147 while ((tag = h->tags.head) != NULL) {
148 h->tags.head = tag->next;
149 free(tag);
150 }
151
152 if (h->symtab)
153 chars_free(h->symtab);
154
155 free(h);
156 }
157
158
159 void
160 print_gen_head(struct html *h)
161 {
162 struct htmlpair tag[4];
163
164 tag[0].key = ATTR_HTTPEQUIV;
165 tag[0].val = "Content-Type";
166 tag[1].key = ATTR_CONTENT;
167 tag[1].val = "text/html; charset=utf-8";
168 print_otag(h, TAG_META, 2, tag);
169
170 tag[0].key = ATTR_NAME;
171 tag[0].val = "resource-type";
172 tag[1].key = ATTR_CONTENT;
173 tag[1].val = "document";
174 print_otag(h, TAG_META, 2, tag);
175
176 if (h->style) {
177 tag[0].key = ATTR_REL;
178 tag[0].val = "stylesheet";
179 tag[1].key = ATTR_HREF;
180 tag[1].val = h->style;
181 tag[2].key = ATTR_TYPE;
182 tag[2].val = "text/css";
183 tag[3].key = ATTR_MEDIA;
184 tag[3].val = "all";
185 print_otag(h, TAG_LINK, 4, tag);
186 }
187 }
188
189
190 static void
191 print_spec(struct html *h, const char *p, int len)
192 {
193 const char *rhs;
194 int i;
195 size_t sz;
196
197 rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
198
199 if (NULL == rhs)
200 return;
201 for (i = 0; i < (int)sz; i++)
202 putchar(rhs[i]);
203 }
204
205
206 static void
207 print_res(struct html *h, const char *p, int len)
208 {
209 const char *rhs;
210 int i;
211 size_t sz;
212
213 rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
214
215 if (NULL == rhs)
216 return;
217 for (i = 0; i < (int)sz; i++)
218 putchar(rhs[i]);
219 }
220
221
222 static void
223 print_escape(struct html *h, const char **p)
224 {
225 int j, type;
226 const char *wp;
227
228 wp = *p;
229 type = 1;
230
231 if (0 == *(++wp)) {
232 *p = wp;
233 return;
234 }
235
236 if ('(' == *wp) {
237 wp++;
238 if (0 == *wp || 0 == *(wp + 1)) {
239 *p = 0 == *wp ? wp : wp + 1;
240 return;
241 }
242
243 print_spec(h, wp, 2);
244 *p = ++wp;
245 return;
246
247 } else if ('*' == *wp) {
248 if (0 == *(++wp)) {
249 *p = wp;
250 return;
251 }
252
253 switch (*wp) {
254 case ('('):
255 wp++;
256 if (0 == *wp || 0 == *(wp + 1)) {
257 *p = 0 == *wp ? wp : wp + 1;
258 return;
259 }
260
261 print_res(h, wp, 2);
262 *p = ++wp;
263 return;
264 case ('['):
265 type = 0;
266 break;
267 default:
268 print_res(h, wp, 1);
269 *p = wp;
270 return;
271 }
272
273 } else if ('f' == *wp) {
274 if (0 == *(++wp)) {
275 *p = wp;
276 return;
277 }
278
279 switch (*wp) {
280 case ('B'):
281 /* TODO */
282 break;
283 case ('I'):
284 /* TODO */
285 break;
286 case ('P'):
287 /* FALLTHROUGH */
288 case ('R'):
289 /* TODO */
290 break;
291 default:
292 break;
293 }
294
295 *p = wp;
296 return;
297
298 } else if ('[' != *wp) {
299 print_spec(h, wp, 1);
300 *p = wp;
301 return;
302 }
303
304 wp++;
305 for (j = 0; *wp && ']' != *wp; wp++, j++)
306 /* Loop... */ ;
307
308 if (0 == *wp) {
309 *p = wp;
310 return;
311 }
312
313 if (type)
314 print_spec(h, wp - j, j);
315 else
316 print_res(h, wp - j, j);
317
318 *p = wp;
319 }
320
321
322 static void
323 print_encode(struct html *h, const char *p)
324 {
325
326 for (; *p; p++) {
327 if ('\\' == *p) {
328 print_escape(h, &p);
329 continue;
330 }
331 switch (*p) {
332 case ('<'):
333 printf("&lt;");
334 break;
335 case ('>'):
336 printf("&gt;");
337 break;
338 case ('&'):
339 printf("&amp;");
340 break;
341 default:
342 putchar(*p);
343 break;
344 }
345 }
346 }
347
348
349 struct tag *
350 print_otag(struct html *h, enum htmltag tag,
351 int sz, const struct htmlpair *p)
352 {
353 int i;
354 struct tag *t;
355
356 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
357 if (NULL == (t = malloc(sizeof(struct tag))))
358 err(EXIT_FAILURE, "malloc");
359 t->tag = tag;
360 t->next = h->tags.head;
361 h->tags.head = t;
362 } else
363 t = NULL;
364
365 if ( ! (HTML_NOSPACE & h->flags))
366 if ( ! (HTML_CLRLINE & htmltags[tag].flags))
367 printf(" ");
368
369 printf("<%s", htmltags[tag].name);
370 for (i = 0; i < sz; i++) {
371 printf(" %s=\"", htmlattrs[p[i].key]);
372 assert(p->val);
373 print_encode(h, p[i].val);
374 printf("\"");
375 }
376 printf(">");
377
378 h->flags |= HTML_NOSPACE;
379 if (HTML_CLRLINE & htmltags[tag].flags)
380 h->flags |= HTML_NEWLINE;
381 else
382 h->flags &= ~HTML_NEWLINE;
383
384 return(t);
385 }
386
387
388 /* ARGSUSED */
389 static void
390 print_ctag(struct html *h, enum htmltag tag)
391 {
392
393 printf("</%s>", htmltags[tag].name);
394 if (HTML_CLRLINE & htmltags[tag].flags)
395 h->flags |= HTML_NOSPACE;
396 if (HTML_CLRLINE & htmltags[tag].flags)
397 h->flags |= HTML_NEWLINE;
398 else
399 h->flags &= ~HTML_NEWLINE;
400 }
401
402
403 /* ARGSUSED */
404 void
405 print_gen_doctype(struct html *h)
406 {
407
408 printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
409 }
410
411
412 void
413 print_text(struct html *h, const char *p)
414 {
415
416 if (*p && 0 == *(p + 1))
417 switch (*p) {
418 case('.'):
419 /* FALLTHROUGH */
420 case(','):
421 /* FALLTHROUGH */
422 case(';'):
423 /* FALLTHROUGH */
424 case(':'):
425 /* FALLTHROUGH */
426 case('?'):
427 /* FALLTHROUGH */
428 case('!'):
429 /* FALLTHROUGH */
430 case(')'):
431 /* FALLTHROUGH */
432 case(']'):
433 /* FALLTHROUGH */
434 case('}'):
435 if ( ! (HTML_IGNDELIM & h->flags))
436 h->flags |= HTML_NOSPACE;
437 break;
438 default:
439 break;
440 }
441
442 if ( ! (h->flags & HTML_NOSPACE))
443 printf(" ");
444
445 h->flags &= ~HTML_NOSPACE;
446 h->flags &= ~HTML_NEWLINE;
447
448 if (p)
449 print_encode(h, p);
450
451 if (*p && 0 == *(p + 1))
452 switch (*p) {
453 case('('):
454 /* FALLTHROUGH */
455 case('['):
456 /* FALLTHROUGH */
457 case('{'):
458 h->flags |= HTML_NOSPACE;
459 break;
460 default:
461 break;
462 }
463 }
464
465
466 void
467 print_tagq(struct html *h, const struct tag *until)
468 {
469 struct tag *tag;
470
471 while ((tag = h->tags.head) != NULL) {
472 print_ctag(h, tag->tag);
473 h->tags.head = tag->next;
474 free(tag);
475 if (until && tag == until)
476 return;
477 }
478 }
479
480
481 void
482 print_stagq(struct html *h, const struct tag *suntil)
483 {
484 struct tag *tag;
485
486 while ((tag = h->tags.head) != NULL) {
487 if (suntil && tag == suntil)
488 return;
489 print_ctag(h, tag->tag);
490 h->tags.head = tag->next;
491 free(tag);
492 }
493 }
494
495
496 void
497 bufinit(struct html *h)
498 {
499
500 h->buf[0] = '\0';
501 h->buflen = 0;
502 }
503
504
505 void
506 bufcat_style(struct html *h, const char *key, const char *val)
507 {
508
509 bufcat(h, key);
510 bufncat(h, ":", 1);
511 bufcat(h, val);
512 bufncat(h, ";", 1);
513 }
514
515
516 void
517 bufcat(struct html *h, const char *p)
518 {
519
520 bufncat(h, p, strlen(p));
521 }
522
523
524 void
525 buffmt(struct html *h, const char *fmt, ...)
526 {
527 va_list ap;
528
529 va_start(ap, fmt);
530 (void)vsnprintf(h->buf + (int)h->buflen,
531 BUFSIZ - h->buflen - 1, fmt, ap);
532 va_end(ap);
533 h->buflen = strlen(h->buf);
534 }
535
536
537 void
538 bufncat(struct html *h, const char *p, size_t sz)
539 {
540
541 if (h->buflen + sz > BUFSIZ - 1)
542 sz = BUFSIZ - 1 - h->buflen;
543
544 (void)strncat(h->buf, p, sz);
545 h->buflen += sz;
546 }
547
548
549 void
550 buffmt_includes(struct html *h, const char *name)
551 {
552 const char *p, *pp;
553
554 pp = h->base_includes;
555
556 while (NULL != (p = strchr(pp, '%'))) {
557 bufncat(h, pp, (size_t)(p - pp));
558 switch (*(p + 1)) {
559 case('I'):
560 bufcat(h, name);
561 break;
562 default:
563 bufncat(h, p, 2);
564 break;
565 }
566 pp = p + 2;
567 }
568 if (pp)
569 bufcat(h, pp);
570 }
571
572
573 void
574 buffmt_man(struct html *h,
575 const char *name, const char *sec)
576 {
577 const char *p, *pp;
578
579 pp = h->base_man;
580
581 /* LINTED */
582 while (NULL != (p = strchr(pp, '%'))) {
583 bufncat(h, pp, (size_t)(p - pp));
584 switch (*(p + 1)) {
585 case('S'):
586 bufcat(h, sec ? sec : "1");
587 break;
588 case('N'):
589 buffmt(h, name);
590 break;
591 default:
592 bufncat(h, p, 2);
593 break;
594 }
595 pp = p + 2;
596 }
597 if (pp)
598 bufcat(h, pp);
599 }
600
601
602 void
603 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
604 {
605 double v;
606 const char *u;
607
608 v = su->scale;
609
610 switch (su->unit) {
611 case (SCALE_CM):
612 u = "cm";
613 break;
614 case (SCALE_IN):
615 u = "in";
616 break;
617 case (SCALE_PC):
618 u = "pc";
619 break;
620 case (SCALE_PT):
621 u = "pt";
622 break;
623 case (SCALE_EM):
624 u = "em";
625 break;
626 case (SCALE_MM):
627 if (0 == (v /= 100))
628 v = 1;
629 u = "em";
630 break;
631 case (SCALE_EN):
632 u = "ex";
633 break;
634 case (SCALE_BU):
635 u = "ex";
636 break;
637 case (SCALE_VS):
638 u = "em";
639 break;
640 default:
641 u = "ex";
642 break;
643 }
644
645 if (su->pt)
646 buffmt(h, "%s: %f%s;", p, v, u);
647 else
648 /* LINTED */
649 buffmt(h, "%s: %d%s;", p, (int)v, u);
650 }
651
652
653 void
654 html_idcpy(char *dst, const char *src, int sz)
655 {
656
657 assert(sz);
658 dst[0] = '\0';
659 html_idcat(dst, src, sz);
660 }
661
662
663 void
664 html_idcat(char *dst, const char *src, int sz)
665 {
666 int i;
667
668 /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
669
670 for (i = 0; *dst != '\0' && i < sz - 1; dst++, i++)
671 /* Jump to end. */ ;
672
673 for ( ; *src != '\0' && i < sz - 1; src++, i++, dst++) {
674 if (isalnum((u_char)*src)) {
675 *dst = *src;
676 continue;
677 }
678
679 switch (*src) {
680 case (';'):
681 *dst = ';';
682 break;
683 case ('-'):
684 *dst = '-';
685 break;
686 case (':'):
687 *dst = ':';
688 break;
689 default:
690 *dst = '_';
691 break;
692 }
693 }
694
695 *dst = '\0';
696 }