Annotation of mandoc/html.c, Revision 1.54
1.54 ! kristaps 1: /* $Id: html.c,v 1.53 2009/10/03 15:08:09 kristaps Exp $ */
1.1 kristaps 2: /*
1.29 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.29 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.29 kristaps 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.
1.1 kristaps 16: */
1.41 kristaps 17: #include <sys/types.h>
1.30 kristaps 18: #include <sys/queue.h>
19:
1.1 kristaps 20: #include <assert.h>
1.4 kristaps 21: #include <err.h>
1.29 kristaps 22: #include <stdio.h>
1.1 kristaps 23: #include <stdlib.h>
1.33 kristaps 24: #include <string.h>
1.45 kristaps 25: #include <unistd.h>
1.1 kristaps 26:
1.32 kristaps 27: #include "chars.h"
1.51 kristaps 28: #include "html.h"
1.2 kristaps 29:
1.29 kristaps 30: #define DOCTYPE "-//W3C//DTD HTML 4.01//EN"
31: #define DTD "http://www.w3.org/TR/html4/strict.dtd"
1.8 kristaps 32:
1.29 kristaps 33: struct htmldata {
34: char *name;
35: int flags;
1.30 kristaps 36: #define HTML_CLRLINE (1 << 0)
37: #define HTML_NOSTACK (1 << 1)
1.29 kristaps 38: };
1.7 kristaps 39:
1.29 kristaps 40: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 41: {"html", HTML_CLRLINE}, /* TAG_HTML */
42: {"head", HTML_CLRLINE}, /* TAG_HEAD */
43: {"body", HTML_CLRLINE}, /* TAG_BODY */
44: {"meta", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_META */
1.33 kristaps 45: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 46: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 47: {"h1", 0}, /* TAG_H1 */
48: {"h2", 0}, /* TAG_H2 */
1.30 kristaps 49: {"p", HTML_CLRLINE}, /* TAG_P */
1.29 kristaps 50: {"span", 0}, /* TAG_SPAN */
1.30 kristaps 51: {"link", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
52: {"br", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_LINK */
53: {"a", 0}, /* TAG_A */
1.33 kristaps 54: {"table", HTML_CLRLINE}, /* TAG_TABLE */
55: {"col", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_COL */
56: {"tr", HTML_CLRLINE}, /* TAG_TR */
57: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 58: {"li", HTML_CLRLINE}, /* TAG_LI */
59: {"ul", HTML_CLRLINE}, /* TAG_UL */
60: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.41 kristaps 61: {"base", HTML_CLRLINE | HTML_NOSTACK}, /* TAG_BASE */
1.29 kristaps 62: };
1.10 kristaps 63:
1.29 kristaps 64: static const char *const htmlattrs[ATTR_MAX] = {
65: "http-equiv",
66: "content",
67: "name",
68: "rel",
69: "href",
70: "type",
71: "media",
1.33 kristaps 72: "class",
73: "style",
74: "width",
75: "valign",
1.54 ! kristaps 76: "target",
1.29 kristaps 77: };
1.10 kristaps 78:
1.33 kristaps 79: #ifdef __linux__
1.43 kristaps 80: extern int getsubopt(char **, char * const *, char **);
1.33 kristaps 81: #endif
1.29 kristaps 82:
83: void *
1.43 kristaps 84: html_alloc(char *outopts)
1.10 kristaps 85: {
1.30 kristaps 86: struct html *h;
1.53 kristaps 87: char *toks[4], *v;
1.43 kristaps 88:
89: toks[0] = "style";
1.53 kristaps 90: toks[1] = "man";
1.54 ! kristaps 91: toks[2] = "includes";
! 92: toks[3] = NULL;
1.30 kristaps 93:
94: if (NULL == (h = calloc(1, sizeof(struct html))))
95: return(NULL);
1.10 kristaps 96:
1.37 kristaps 97: SLIST_INIT(&h->tags);
98: SLIST_INIT(&h->ords);
99:
1.32 kristaps 100: if (NULL == (h->symtab = chars_init(CHARS_HTML))) {
101: free(h);
102: return(NULL);
103: }
1.41 kristaps 104:
1.47 kristaps 105: while (outopts && *outopts)
1.43 kristaps 106: switch (getsubopt(&outopts, toks, &v)) {
107: case (0):
108: h->style = v;
109: break;
110: case (1):
1.53 kristaps 111: h->base_man = v;
1.43 kristaps 112: break;
1.54 ! kristaps 113: case (2):
! 114: h->base_includes = v;
! 115: break;
1.43 kristaps 116: default:
117: break;
118: }
119:
1.30 kristaps 120: return(h);
1.29 kristaps 121: }
1.10 kristaps 122:
1.33 kristaps 123:
1.29 kristaps 124: void
125: html_free(void *p)
126: {
1.30 kristaps 127: struct tag *tag;
1.37 kristaps 128: struct ord *ord;
1.30 kristaps 129: struct html *h;
130:
131: h = (struct html *)p;
1.10 kristaps 132:
1.37 kristaps 133: while ( ! SLIST_EMPTY(&h->ords)) {
134: ord = SLIST_FIRST(&h->ords);
135: SLIST_REMOVE_HEAD(&h->ords, entry);
136: free(ord);
137: }
138:
139: while ( ! SLIST_EMPTY(&h->tags)) {
140: tag = SLIST_FIRST(&h->tags);
141: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 142: free(tag);
143: }
1.36 kristaps 144:
1.53 kristaps 145: if (h->buf)
146: free(h->buf);
1.36 kristaps 147: if (h->symtab)
148: chars_free(h->symtab);
1.53 kristaps 149:
1.30 kristaps 150: free(h);
1.10 kristaps 151: }
1.2 kristaps 152:
1.33 kristaps 153:
1.51 kristaps 154: void
1.29 kristaps 155: print_gen_head(struct html *h)
156: {
1.41 kristaps 157: struct htmlpair tag[4];
158:
159: tag[0].key = ATTR_HTTPEQUIV;
160: tag[0].val = "Content-Type";
161: tag[1].key = ATTR_CONTENT;
162: tag[1].val = "text/html; charset=utf-8";
163: print_otag(h, TAG_META, 2, tag);
164:
165: tag[0].key = ATTR_NAME;
166: tag[0].val = "resource-type";
167: tag[1].key = ATTR_CONTENT;
168: tag[1].val = "document";
169: print_otag(h, TAG_META, 2, tag);
170:
171: if (h->style) {
172: tag[0].key = ATTR_REL;
173: tag[0].val = "stylesheet";
174: tag[1].key = ATTR_HREF;
175: tag[1].val = h->style;
176: tag[2].key = ATTR_TYPE;
177: tag[2].val = "text/css";
178: tag[3].key = ATTR_MEDIA;
179: tag[3].val = "all";
180: print_otag(h, TAG_LINK, 4, tag);
181: }
1.4 kristaps 182: }
183:
1.33 kristaps 184:
1.29 kristaps 185: static void
1.32 kristaps 186: print_spec(struct html *h, const char *p, int len)
187: {
188: const char *rhs;
189: int i;
190: size_t sz;
191:
192: rhs = chars_a2ascii(h->symtab, p, (size_t)len, &sz);
193:
194: if (NULL == rhs)
195: return;
196: for (i = 0; i < (int)sz; i++)
197: putchar(rhs[i]);
198: }
199:
1.33 kristaps 200:
1.32 kristaps 201: static void
202: print_res(struct html *h, const char *p, int len)
203: {
204: const char *rhs;
205: int i;
206: size_t sz;
207:
208: rhs = chars_a2res(h->symtab, p, (size_t)len, &sz);
209:
210: if (NULL == rhs)
211: return;
212: for (i = 0; i < (int)sz; i++)
213: putchar(rhs[i]);
214: }
215:
1.33 kristaps 216:
1.32 kristaps 217: static void
218: print_escape(struct html *h, const char **p)
219: {
220: int j, type;
221: const char *wp;
222:
223: wp = *p;
224: type = 1;
225:
226: if (0 == *(++wp)) {
227: *p = wp;
228: return;
229: }
230:
231: if ('(' == *wp) {
232: wp++;
233: if (0 == *wp || 0 == *(wp + 1)) {
234: *p = 0 == *wp ? wp : wp + 1;
235: return;
236: }
237:
238: print_spec(h, wp, 2);
239: *p = ++wp;
240: return;
241:
242: } else if ('*' == *wp) {
243: if (0 == *(++wp)) {
244: *p = wp;
245: return;
246: }
247:
248: switch (*wp) {
249: case ('('):
250: wp++;
251: if (0 == *wp || 0 == *(wp + 1)) {
252: *p = 0 == *wp ? wp : wp + 1;
253: return;
254: }
255:
256: print_res(h, wp, 2);
257: *p = ++wp;
258: return;
259: case ('['):
260: type = 0;
261: break;
262: default:
263: print_res(h, wp, 1);
264: *p = wp;
265: return;
266: }
267:
268: } else if ('f' == *wp) {
269: if (0 == *(++wp)) {
270: *p = wp;
271: return;
272: }
273:
274: switch (*wp) {
275: case ('B'):
276: /* TODO */
277: break;
278: case ('I'):
279: /* TODO */
280: break;
281: case ('P'):
282: /* FALLTHROUGH */
283: case ('R'):
284: /* TODO */
285: break;
286: default:
287: break;
288: }
289:
290: *p = wp;
291: return;
292:
293: } else if ('[' != *wp) {
294: print_spec(h, wp, 1);
295: *p = wp;
296: return;
297: }
298:
299: wp++;
300: for (j = 0; *wp && ']' != *wp; wp++, j++)
301: /* Loop... */ ;
302:
303: if (0 == *wp) {
304: *p = wp;
305: return;
306: }
307:
308: if (type)
309: print_spec(h, wp - j, j);
310: else
311: print_res(h, wp - j, j);
312:
313: *p = wp;
314: }
315:
1.9 kristaps 316:
1.29 kristaps 317: static void
1.32 kristaps 318: print_encode(struct html *h, const char *p)
1.29 kristaps 319: {
1.14 kristaps 320:
1.32 kristaps 321: for (; *p; p++) {
1.34 kristaps 322: if ('\\' == *p) {
323: print_escape(h, &p);
324: continue;
325: }
326: switch (*p) {
327: case ('<'):
328: printf("<");
329: break;
330: case ('>'):
331: printf(">");
332: break;
333: case ('&'):
334: printf("&");
335: break;
336: default:
1.32 kristaps 337: putchar(*p);
1.34 kristaps 338: break;
1.32 kristaps 339: }
340: }
1.14 kristaps 341: }
342:
343:
1.51 kristaps 344: struct tag *
1.29 kristaps 345: print_otag(struct html *h, enum htmltag tag,
346: int sz, const struct htmlpair *p)
1.14 kristaps 347: {
1.29 kristaps 348: int i;
1.30 kristaps 349: struct tag *t;
350:
351: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
352: if (NULL == (t = malloc(sizeof(struct tag))))
353: err(EXIT_FAILURE, "malloc");
354: t->tag = tag;
1.37 kristaps 355: SLIST_INSERT_HEAD(&h->tags, t, entry);
1.30 kristaps 356: } else
357: t = NULL;
1.29 kristaps 358:
359: if ( ! (HTML_NOSPACE & h->flags))
1.30 kristaps 360: if ( ! (HTML_CLRLINE & htmltags[tag].flags))
1.29 kristaps 361: printf(" ");
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);
1.32 kristaps 367: print_encode(h, p[i].val);
1.29 kristaps 368: printf("\"");
369: }
370: printf(">");
1.14 kristaps 371:
1.29 kristaps 372: h->flags |= HTML_NOSPACE;
1.30 kristaps 373: if (HTML_CLRLINE & htmltags[tag].flags)
374: h->flags |= HTML_NEWLINE;
375: else
376: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 377:
1.30 kristaps 378: return(t);
1.14 kristaps 379: }
380:
381:
382: /* ARGSUSED */
1.29 kristaps 383: static void
384: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 385: {
386:
1.29 kristaps 387: printf("</%s>", htmltags[tag].name);
1.30 kristaps 388: if (HTML_CLRLINE & htmltags[tag].flags)
1.29 kristaps 389: h->flags |= HTML_NOSPACE;
1.30 kristaps 390: if (HTML_CLRLINE & htmltags[tag].flags)
391: h->flags |= HTML_NEWLINE;
392: else
393: h->flags &= ~HTML_NEWLINE;
1.14 kristaps 394: }
395:
396:
1.29 kristaps 397: /* ARGSUSED */
1.51 kristaps 398: void
1.29 kristaps 399: print_gen_doctype(struct html *h)
1.1 kristaps 400: {
1.29 kristaps 401:
1.46 kristaps 402: printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">", DOCTYPE, DTD);
1.1 kristaps 403: }
404:
405:
1.51 kristaps 406: void
1.29 kristaps 407: print_text(struct html *h, const char *p)
1.1 kristaps 408: {
409:
1.29 kristaps 410: if (*p && 0 == *(p + 1))
411: switch (*p) {
412: case('.'):
413: /* FALLTHROUGH */
414: case(','):
415: /* FALLTHROUGH */
416: case(';'):
417: /* FALLTHROUGH */
418: case(':'):
419: /* FALLTHROUGH */
420: case('?'):
421: /* FALLTHROUGH */
422: case('!'):
423: /* FALLTHROUGH */
424: case(')'):
425: /* FALLTHROUGH */
426: case(']'):
427: /* FALLTHROUGH */
428: case('}'):
1.52 kristaps 429: if ( ! (HTML_IGNDELIM & h->flags))
430: h->flags |= HTML_NOSPACE;
1.30 kristaps 431: break;
1.29 kristaps 432: default:
433: break;
434: }
1.1 kristaps 435:
1.29 kristaps 436: if ( ! (h->flags & HTML_NOSPACE))
437: printf(" ");
1.30 kristaps 438:
1.29 kristaps 439: h->flags &= ~HTML_NOSPACE;
1.30 kristaps 440: h->flags &= ~HTML_NEWLINE;
1.1 kristaps 441:
1.29 kristaps 442: if (p)
1.32 kristaps 443: print_encode(h, p);
1.8 kristaps 444:
1.29 kristaps 445: if (*p && 0 == *(p + 1))
446: switch (*p) {
447: case('('):
448: /* FALLTHROUGH */
449: case('['):
450: /* FALLTHROUGH */
451: case('{'):
452: h->flags |= HTML_NOSPACE;
1.30 kristaps 453: break;
1.29 kristaps 454: default:
455: break;
456: }
1.1 kristaps 457: }
1.30 kristaps 458:
459:
1.51 kristaps 460: void
1.30 kristaps 461: print_tagq(struct html *h, const struct tag *until)
462: {
463: struct tag *tag;
464:
1.37 kristaps 465: while ( ! SLIST_EMPTY(&h->tags)) {
466: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 467: print_ctag(h, tag->tag);
1.37 kristaps 468: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 469: free(tag);
470: if (until && tag == until)
471: return;
472: }
473: }
474:
475:
1.51 kristaps 476: void
1.30 kristaps 477: print_stagq(struct html *h, const struct tag *suntil)
478: {
479: struct tag *tag;
480:
1.37 kristaps 481: while ( ! SLIST_EMPTY(&h->tags)) {
482: tag = SLIST_FIRST(&h->tags);
1.30 kristaps 483: if (suntil && tag == suntil)
484: return;
485: print_ctag(h, tag->tag);
1.37 kristaps 486: SLIST_REMOVE_HEAD(&h->tags, entry);
1.30 kristaps 487: free(tag);
488: }
489: }
CVSweb