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