]> git.cameronkatri.com Git - mandoc.git/blob - html.c
Fix in mandoc.1 and mandoc_char.7 syntax (submitted Joerg Sonnenberger).
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.64 2009/10/13 10:57:25 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 #include <sys/queue.h>
19
20 #include <assert.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 };
85
86 #ifdef __linux__
87 extern int getsubopt(char **, char * const *, char **);
88 #endif
89
90 void *
91 html_alloc(char *outopts)
92 {
93 struct html *h;
94 const char *toks[4];
95 char *v;
96
97 toks[0] = "style";
98 toks[1] = "man";
99 toks[2] = "includes";
100 toks[3] = NULL;
101
102 if (NULL == (h = calloc(1, sizeof(struct html))))
103 return(NULL);
104
105 SLIST_INIT(&h->tags);
106 SLIST_INIT(&h->ords);
107
108 if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
109 free(h);
110 return(NULL);
111 }
112
113 while (outopts && *outopts)
114 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
115 case (0):
116 h->style = v;
117 break;
118 case (1):
119 h->base_man = v;
120 break;
121 case (2):
122 h->base_includes = v;
123 break;
124 default:
125 break;
126 }
127
128 return(h);
129 }
130
131
132 void
133 html_free(void *p)
134 {
135 struct tag *tag;
136 struct ord *ord;
137 struct html *h;
138
139 h = (struct html *)p;
140
141 while ( ! SLIST_EMPTY(&h->ords)) {
142 ord = SLIST_FIRST(&h->ords);
143 SLIST_REMOVE_HEAD(&h->ords, entry);
144 free(ord);
145 }
146
147 while ( ! SLIST_EMPTY(&h->tags)) {
148 tag = SLIST_FIRST(&h->tags);
149 SLIST_REMOVE_HEAD(&h->tags, entry);
150 free(tag);
151 }
152
153 if (h->symtab)
154 chars_free(h->symtab);
155
156 free(h);
157 }
158
159
160 void
161 print_gen_head(struct html *h)
162 {
163 struct htmlpair tag[4];
164
165 tag[0].key = ATTR_HTTPEQUIV;
166 tag[0].val = "Content-Type";
167 tag[1].key = ATTR_CONTENT;
168 tag[1].val = "text/html; charset=utf-8";
169 print_otag(h, TAG_META, 2, tag);
170
171 tag[0].key = ATTR_NAME;
172 tag[0].val = "resource-type";
173 tag[1].key = ATTR_CONTENT;
174 tag[1].val = "document";
175 print_otag(h, TAG_META, 2, tag);
176
177 if (h->style) {
178 tag[0].key = ATTR_REL;
179 tag[0].val = "stylesheet";
180 tag[1].key = ATTR_HREF;
181 tag[1].val = h->style;
182 tag[2].key = ATTR_TYPE;
183 tag[2].val = "text/css";
184 tag[3].key = ATTR_MEDIA;
185 tag[3].val = "all";
186 print_otag(h, TAG_LINK, 4, tag);
187 }
188 }
189
190
191 static void
192 print_spec(struct html *h, const char *p, int len)
193 {
194 const char *rhs;
195 int i;
196 size_t sz;
197
198 rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
199
200 if (NULL == rhs)
201 return;
202 for (i = 0; i < (int)sz; i++)
203 putchar(rhs[i]);
204 }
205
206
207 static void
208 print_res(struct html *h, const char *p, int len)
209 {
210 const char *rhs;
211 int i;
212 size_t sz;
213
214 rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
215
216 if (NULL == rhs)
217 return;
218 for (i = 0; i < (int)sz; i++)
219 putchar(rhs[i]);
220 }
221
222
223 static void
224 print_escape(struct html *h, const char **p)
225 {
226 int j, type;
227 const char *wp;
228
229 wp = *p;
230 type = 1;
231
232 if (0 == *(++wp)) {
233 *p = wp;
234 return;
235 }
236
237 if ('(' == *wp) {
238 wp++;
239 if (0 == *wp || 0 == *(wp + 1)) {
240 *p = 0 == *wp ? wp : wp + 1;
241 return;
242 }
243
244 print_spec(h, wp, 2);
245 *p = ++wp;
246 return;
247
248 } else if ('*' == *wp) {
249 if (0 == *(++wp)) {
250 *p = wp;
251 return;
252 }
253
254 switch (*wp) {
255 case ('('):
256 wp++;
257 if (0 == *wp || 0 == *(wp + 1)) {
258 *p = 0 == *wp ? wp : wp + 1;
259 return;
260 }
261
262 print_res(h, wp, 2);
263 *p = ++wp;
264 return;
265 case ('['):
266 type = 0;
267 break;
268 default:
269 print_res(h, wp, 1);
270 *p = wp;
271 return;
272 }
273
274 } else if ('f' == *wp) {
275 if (0 == *(++wp)) {
276 *p = wp;
277 return;
278 }
279
280 switch (*wp) {
281 case ('B'):
282 /* TODO */
283 break;
284 case ('I'):
285 /* TODO */
286 break;
287 case ('P'):
288 /* FALLTHROUGH */
289 case ('R'):
290 /* TODO */
291 break;
292 default:
293 break;
294 }
295
296 *p = wp;
297 return;
298
299 } else if ('[' != *wp) {
300 print_spec(h, wp, 1);
301 *p = wp;
302 return;
303 }
304
305 wp++;
306 for (j = 0; *wp && ']' != *wp; wp++, j++)
307 /* Loop... */ ;
308
309 if (0 == *wp) {
310 *p = wp;
311 return;
312 }
313
314 if (type)
315 print_spec(h, wp - j, j);
316 else
317 print_res(h, wp - j, j);
318
319 *p = wp;
320 }
321
322
323 static void
324 print_encode(struct html *h, const char *p)
325 {
326
327 for (; *p; p++) {
328 if ('\\' == *p) {
329 print_escape(h, &p);
330 continue;
331 }
332 switch (*p) {
333 case ('<'):
334 printf("&lt;");
335 break;
336 case ('>'):
337 printf("&gt;");
338 break;
339 case ('&'):
340 printf("&amp;");
341 break;
342 default:
343 putchar(*p);
344 break;
345 }
346 }
347 }
348
349
350 struct tag *
351 print_otag(struct html *h, enum htmltag tag,
352 int sz, const struct htmlpair *p)
353 {
354 int i;
355 struct tag *t;
356
357 if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
358 if (NULL == (t = malloc(sizeof(struct tag))))
359 err(EXIT_FAILURE, "malloc");
360 t->tag = tag;
361 SLIST_INSERT_HEAD(&h->tags, t, entry);
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 ( ! SLIST_EMPTY(&h->tags)) {
472 tag = SLIST_FIRST(&h->tags);
473 print_ctag(h, tag->tag);
474 SLIST_REMOVE_HEAD(&h->tags, entry);
475 free(tag);
476 if (until && tag == until)
477 return;
478 }
479 }
480
481
482 void
483 print_stagq(struct html *h, const struct tag *suntil)
484 {
485 struct tag *tag;
486
487 while ( ! SLIST_EMPTY(&h->tags)) {
488 tag = SLIST_FIRST(&h->tags);
489 if (suntil && tag == suntil)
490 return;
491 print_ctag(h, tag->tag);
492 SLIST_REMOVE_HEAD(&h->tags, entry);
493 free(tag);
494 }
495 }
496
497
498 void
499 bufinit(struct html *h)
500 {
501
502 h->buf[0] = '\0';
503 h->buflen = 0;
504 }
505
506
507 void
508 bufcat_style(struct html *h, const char *key, const char *val)
509 {
510
511 bufcat(h, key);
512 bufncat(h, ":", 1);
513 bufcat(h, val);
514 bufncat(h, ";", 1);
515 }
516
517
518 void
519 bufcat(struct html *h, const char *p)
520 {
521
522 bufncat(h, p, strlen(p));
523 }
524
525
526 void
527 buffmt(struct html *h, const char *fmt, ...)
528 {
529 va_list ap;
530
531 va_start(ap, fmt);
532 (void)vsnprintf(h->buf + (int)h->buflen,
533 BUFSIZ - h->buflen - 1, fmt, ap);
534 va_end(ap);
535 h->buflen = strlen(h->buf);
536 }
537
538
539 void
540 bufncat(struct html *h, const char *p, size_t sz)
541 {
542
543 if (h->buflen + sz > BUFSIZ - 1)
544 sz = BUFSIZ - 1 - h->buflen;
545
546 (void)strncat(h->buf, p, sz);
547 h->buflen += sz;
548 }
549
550
551 void
552 buffmt_includes(struct html *h, const char *name)
553 {
554 const char *p, *pp;
555
556 pp = h->base_includes;
557
558 while (NULL != (p = strchr(pp, '%'))) {
559 bufncat(h, pp, (size_t)(p - pp));
560 switch (*(p + 1)) {
561 case('I'):
562 bufcat(h, name);
563 break;
564 default:
565 bufncat(h, p, 2);
566 break;
567 }
568 pp = p + 2;
569 }
570 if (pp)
571 bufcat(h, pp);
572 }
573
574
575 void
576 buffmt_man(struct html *h,
577 const char *name, const char *sec)
578 {
579 const char *p, *pp;
580
581 pp = h->base_man;
582
583 /* LINTED */
584 while (NULL != (p = strchr(pp, '%'))) {
585 bufncat(h, pp, (size_t)(p - pp));
586 switch (*(p + 1)) {
587 case('S'):
588 bufcat(h, sec ? sec : "1");
589 break;
590 case('N'):
591 buffmt(h, name);
592 break;
593 default:
594 bufncat(h, p, 2);
595 break;
596 }
597 pp = p + 2;
598 }
599 if (pp)
600 bufcat(h, pp);
601 }
602
603
604 void
605 bufcat_su(struct html *h, const char *p, const struct roffsu *su)
606 {
607 double v;
608 const char *u;
609
610 v = su->scale;
611
612 switch (su->unit) {
613 case (SCALE_CM):
614 u = "cm";
615 break;
616 case (SCALE_IN):
617 u = "in";
618 break;
619 case (SCALE_PC):
620 u = "pc";
621 break;
622 case (SCALE_PT):
623 u = "pt";
624 break;
625 case (SCALE_EM):
626 u = "em";
627 break;
628 case (SCALE_MM):
629 if (0 == (v /= 100))
630 v = 1;
631 u = "em";
632 break;
633 case (SCALE_EN):
634 u = "ex";
635 break;
636 case (SCALE_BU):
637 u = "ex";
638 break;
639 case (SCALE_VS):
640 u = "em";
641 break;
642 default:
643 u = "ex";
644 break;
645 }
646
647 if (su->pt)
648 buffmt(h, "%s: %f%s;", p, v, u);
649 else
650 /* LINTED */
651 buffmt(h, "%s: %d%s;", p, (int)v, u);
652 }