Annotation of mandoc/html.c, Revision 1.115
1.115 ! kristaps 1: /* $Id: html.c,v 1.114 2010/12/15 14:52:16 kristaps Exp $ */
1.1 kristaps 2: /*
1.106 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.92 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.41 kristaps 21: #include <sys/types.h>
1.30 kristaps 22:
1.1 kristaps 23: #include <assert.h>
1.68 kristaps 24: #include <ctype.h>
1.76 kristaps 25: #include <stdarg.h>
1.29 kristaps 26: #include <stdio.h>
1.63 kristaps 27: #include <stdint.h>
1.1 kristaps 28: #include <stdlib.h>
1.33 kristaps 29: #include <string.h>
1.45 kristaps 30: #include <unistd.h>
1.1 kristaps 31:
1.100 kristaps 32: #include "mandoc.h"
1.58 kristaps 33: #include "out.h"
1.32 kristaps 34: #include "chars.h"
1.51 kristaps 35: #include "html.h"
1.64 kristaps 36: #include "main.h"
1.63 kristaps 37:
1.29 kristaps 38: struct htmldata {
1.63 kristaps 39: const char *name;
1.29 kristaps 40: int flags;
1.30 kristaps 41: #define HTML_CLRLINE (1 << 0)
42: #define HTML_NOSTACK (1 << 1)
1.93 kristaps 43: #define HTML_AUTOCLOSE (1 << 2) /* Tag has auto-closure. */
1.29 kristaps 44: };
1.7 kristaps 45:
1.29 kristaps 46: static const struct htmldata htmltags[TAG_MAX] = {
1.30 kristaps 47: {"html", HTML_CLRLINE}, /* TAG_HTML */
48: {"head", HTML_CLRLINE}, /* TAG_HEAD */
49: {"body", HTML_CLRLINE}, /* TAG_BODY */
1.93 kristaps 50: {"meta", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_META */
1.33 kristaps 51: {"title", HTML_CLRLINE}, /* TAG_TITLE */
1.30 kristaps 52: {"div", HTML_CLRLINE}, /* TAG_DIV */
1.29 kristaps 53: {"h1", 0}, /* TAG_H1 */
54: {"h2", 0}, /* TAG_H2 */
55: {"span", 0}, /* TAG_SPAN */
1.99 kristaps 56: {"link", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_LINK */
1.93 kristaps 57: {"br", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_BR */
1.30 kristaps 58: {"a", 0}, /* TAG_A */
1.33 kristaps 59: {"table", HTML_CLRLINE}, /* TAG_TABLE */
1.114 kristaps 60: {"tbody", HTML_CLRLINE}, /* TAG_TBODY */
1.93 kristaps 61: {"col", HTML_CLRLINE | HTML_NOSTACK | HTML_AUTOCLOSE}, /* TAG_COL */
1.33 kristaps 62: {"tr", HTML_CLRLINE}, /* TAG_TR */
63: {"td", HTML_CLRLINE}, /* TAG_TD */
1.34 kristaps 64: {"li", HTML_CLRLINE}, /* TAG_LI */
65: {"ul", HTML_CLRLINE}, /* TAG_UL */
66: {"ol", HTML_CLRLINE}, /* TAG_OL */
1.114 kristaps 67: {"dl", HTML_CLRLINE}, /* TAG_DL */
68: {"dt", HTML_CLRLINE}, /* TAG_DT */
69: {"dd", HTML_CLRLINE}, /* TAG_DD */
1.115 ! kristaps 70: {"blockquote", HTML_CLRLINE}, /* TAG_BLOCKQUOTE */
1.29 kristaps 71: };
1.10 kristaps 72:
1.90 kristaps 73: static const char *const htmlfonts[HTMLFONT_MAX] = {
74: "roman",
75: "bold",
76: "italic"
77: };
78:
1.82 kristaps 79: static const char *const htmlattrs[ATTR_MAX] = {
1.29 kristaps 80: "http-equiv",
81: "content",
82: "name",
83: "rel",
84: "href",
85: "type",
86: "media",
1.33 kristaps 87: "class",
88: "style",
89: "width",
90: "valign",
1.54 kristaps 91: "target",
1.57 kristaps 92: "id",
1.67 kristaps 93: "summary",
1.29 kristaps 94: };
1.10 kristaps 95:
1.108 kristaps 96: static void print_spec(struct html *, enum roffdeco,
97: const char *, size_t);
1.83 kristaps 98: static void print_res(struct html *, const char *, size_t);
1.82 kristaps 99: static void print_ctag(struct html *, enum htmltag);
1.93 kristaps 100: static void print_doctype(struct html *);
101: static void print_xmltype(struct html *);
1.88 kristaps 102: static int print_encode(struct html *, const char *, int);
103: static void print_metaf(struct html *, enum roffdeco);
1.94 kristaps 104: static void print_attr(struct html *,
105: const char *, const char *);
1.93 kristaps 106: static void *ml_alloc(char *, enum htmltype);
1.82 kristaps 107:
108:
1.93 kristaps 109: static void *
110: ml_alloc(char *outopts, enum htmltype type)
1.10 kristaps 111: {
1.30 kristaps 112: struct html *h;
1.63 kristaps 113: const char *toks[4];
114: char *v;
1.43 kristaps 115:
116: toks[0] = "style";
1.53 kristaps 117: toks[1] = "man";
1.54 kristaps 118: toks[2] = "includes";
119: toks[3] = NULL;
1.30 kristaps 120:
1.72 kristaps 121: h = calloc(1, sizeof(struct html));
122: if (NULL == h) {
1.75 kristaps 123: perror(NULL);
1.112 kristaps 124: exit((int)MANDOCLEVEL_SYSERR);
1.72 kristaps 125: }
1.10 kristaps 126:
1.93 kristaps 127: h->type = type;
1.66 kristaps 128: h->tags.head = NULL;
1.72 kristaps 129: h->symtab = chars_init(CHARS_HTML);
1.41 kristaps 130:
1.47 kristaps 131: while (outopts && *outopts)
1.63 kristaps 132: switch (getsubopt(&outopts, UNCONST(toks), &v)) {
1.43 kristaps 133: case (0):
134: h->style = v;
135: break;
136: case (1):
1.53 kristaps 137: h->base_man = v;
1.43 kristaps 138: break;
1.54 kristaps 139: case (2):
140: h->base_includes = v;
141: break;
1.43 kristaps 142: default:
143: break;
144: }
145:
1.30 kristaps 146: return(h);
1.29 kristaps 147: }
1.10 kristaps 148:
1.93 kristaps 149: void *
150: html_alloc(char *outopts)
151: {
152:
153: return(ml_alloc(outopts, HTML_HTML_4_01_STRICT));
154: }
155:
156:
157: void *
158: xhtml_alloc(char *outopts)
159: {
160:
161: return(ml_alloc(outopts, HTML_XHTML_1_0_STRICT));
162: }
163:
1.33 kristaps 164:
1.29 kristaps 165: void
166: html_free(void *p)
167: {
1.30 kristaps 168: struct tag *tag;
169: struct html *h;
170:
171: h = (struct html *)p;
1.37 kristaps 172:
1.66 kristaps 173: while ((tag = h->tags.head) != NULL) {
174: h->tags.head = tag->next;
1.30 kristaps 175: free(tag);
176: }
1.36 kristaps 177:
178: if (h->symtab)
179: chars_free(h->symtab);
1.53 kristaps 180:
1.30 kristaps 181: free(h);
1.10 kristaps 182: }
1.2 kristaps 183:
1.33 kristaps 184:
1.51 kristaps 185: void
1.29 kristaps 186: print_gen_head(struct html *h)
187: {
1.41 kristaps 188: struct htmlpair tag[4];
189:
190: tag[0].key = ATTR_HTTPEQUIV;
191: tag[0].val = "Content-Type";
192: tag[1].key = ATTR_CONTENT;
193: tag[1].val = "text/html; charset=utf-8";
194: print_otag(h, TAG_META, 2, tag);
195:
196: tag[0].key = ATTR_NAME;
197: tag[0].val = "resource-type";
198: tag[1].key = ATTR_CONTENT;
199: tag[1].val = "document";
200: print_otag(h, TAG_META, 2, tag);
201:
202: if (h->style) {
203: tag[0].key = ATTR_REL;
204: tag[0].val = "stylesheet";
205: tag[1].key = ATTR_HREF;
206: tag[1].val = h->style;
207: tag[2].key = ATTR_TYPE;
208: tag[2].val = "text/css";
209: tag[3].key = ATTR_MEDIA;
210: tag[3].val = "all";
211: print_otag(h, TAG_LINK, 4, tag);
212: }
1.4 kristaps 213: }
214:
1.33 kristaps 215:
1.29 kristaps 216: static void
1.108 kristaps 217: print_spec(struct html *h, enum roffdeco d, const char *p, size_t len)
1.32 kristaps 218: {
1.107 kristaps 219: int cp;
1.32 kristaps 220: const char *rhs;
221: size_t sz;
222:
1.107 kristaps 223: if ((cp = chars_spec2cp(h->symtab, p, len)) > 0) {
224: printf("&#%d;", cp);
225: return;
1.108 kristaps 226: } else if (-1 == cp && DECO_SSPECIAL == d) {
227: fwrite(p, 1, len, stdout);
228: return;
1.107 kristaps 229: } else if (-1 == cp)
230: return;
1.32 kristaps 231:
1.107 kristaps 232: if (NULL != (rhs = chars_spec2str(h->symtab, p, len, &sz)))
233: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 234: }
235:
1.33 kristaps 236:
1.32 kristaps 237: static void
1.83 kristaps 238: print_res(struct html *h, const char *p, size_t len)
1.32 kristaps 239: {
1.107 kristaps 240: int cp;
1.32 kristaps 241: const char *rhs;
242: size_t sz;
243:
1.107 kristaps 244: if ((cp = chars_res2cp(h->symtab, p, len)) > 0) {
245: printf("&#%d;", cp);
246: return;
247: } else if (-1 == cp)
248: return;
1.32 kristaps 249:
1.107 kristaps 250: if (NULL != (rhs = chars_res2str(h->symtab, p, len, &sz)))
251: fwrite(rhs, 1, sz, stdout);
1.32 kristaps 252: }
253:
1.33 kristaps 254:
1.90 kristaps 255: struct tag *
256: print_ofont(struct html *h, enum htmlfont font)
257: {
258: struct htmlpair tag;
259:
260: h->metal = h->metac;
261: h->metac = font;
262:
263: /* FIXME: DECO_ROMAN should just close out preexisting. */
264:
1.91 kristaps 265: if (h->metaf && h->tags.head == h->metaf)
1.90 kristaps 266: print_tagq(h, h->metaf);
267:
268: PAIR_CLASS_INIT(&tag, htmlfonts[font]);
269: h->metaf = print_otag(h, TAG_SPAN, 1, &tag);
270: return(h->metaf);
271: }
272:
273:
1.88 kristaps 274: static void
275: print_metaf(struct html *h, enum roffdeco deco)
276: {
1.90 kristaps 277: enum htmlfont font;
1.88 kristaps 278:
279: switch (deco) {
1.90 kristaps 280: case (DECO_PREVIOUS):
281: font = h->metal;
1.88 kristaps 282: break;
283: case (DECO_ITALIC):
1.90 kristaps 284: font = HTMLFONT_ITALIC;
285: break;
286: case (DECO_BOLD):
287: font = HTMLFONT_BOLD;
1.88 kristaps 288: break;
289: case (DECO_ROMAN):
1.90 kristaps 290: font = HTMLFONT_NONE;
1.88 kristaps 291: break;
292: default:
293: abort();
294: /* NOTREACHED */
295: }
296:
1.90 kristaps 297: (void)print_ofont(h, font);
1.88 kristaps 298: }
299:
300:
1.85 kristaps 301: static int
1.88 kristaps 302: print_encode(struct html *h, const char *p, int norecurse)
1.29 kristaps 303: {
1.77 kristaps 304: size_t sz;
1.85 kristaps 305: int len, nospace;
1.82 kristaps 306: const char *seq;
307: enum roffdeco deco;
1.100 kristaps 308: static const char rejs[6] = { '\\', '<', '>', '&', ASCII_HYPH, '\0' };
1.14 kristaps 309:
1.85 kristaps 310: nospace = 0;
311:
1.32 kristaps 312: for (; *p; p++) {
1.100 kristaps 313: sz = strcspn(p, rejs);
1.77 kristaps 314:
315: fwrite(p, 1, sz, stdout);
1.80 kristaps 316: p += /* LINTED */
317: sz;
1.77 kristaps 318:
1.82 kristaps 319: if ('<' == *p) {
320: printf("<");
321: continue;
322: } else if ('>' == *p) {
323: printf(">");
324: continue;
325: } else if ('&' == *p) {
326: printf("&");
1.34 kristaps 327: continue;
1.100 kristaps 328: } else if (ASCII_HYPH == *p) {
329: /*
330: * Note: "soft hyphens" aren't graphically
331: * displayed when not breaking the text; we want
332: * them to be displayed.
333: */
334: /*printf("­");*/
335: putchar('-');
336: continue;
1.77 kristaps 337: } else if ('\0' == *p)
338: break;
339:
1.82 kristaps 340: seq = ++p;
341: len = a2roffdeco(&deco, &seq, &sz);
342:
343: switch (deco) {
344: case (DECO_RESERVED):
345: print_res(h, seq, sz);
346: break;
1.108 kristaps 347: case (DECO_SSPECIAL):
348: /* FALLTHROUGH */
1.82 kristaps 349: case (DECO_SPECIAL):
1.108 kristaps 350: print_spec(h, deco, seq, sz);
1.82 kristaps 351: break;
1.90 kristaps 352: case (DECO_PREVIOUS):
353: /* FALLTHROUGH */
1.88 kristaps 354: case (DECO_BOLD):
355: /* FALLTHROUGH */
356: case (DECO_ITALIC):
357: /* FALLTHROUGH */
358: case (DECO_ROMAN):
359: if (norecurse)
360: break;
361: print_metaf(h, deco);
362: break;
1.82 kristaps 363: default:
364: break;
365: }
366:
367: p += len - 1;
1.84 kristaps 368:
369: if (DECO_NOSPACE == deco && '\0' == *(p + 1))
1.85 kristaps 370: nospace = 1;
1.32 kristaps 371: }
1.85 kristaps 372:
373: return(nospace);
1.14 kristaps 374: }
375:
376:
1.94 kristaps 377: static void
378: print_attr(struct html *h, const char *key, const char *val)
379: {
380: printf(" %s=\"", key);
381: (void)print_encode(h, val, 1);
382: putchar('\"');
383: }
384:
385:
1.51 kristaps 386: struct tag *
1.29 kristaps 387: print_otag(struct html *h, enum htmltag tag,
388: int sz, const struct htmlpair *p)
1.14 kristaps 389: {
1.29 kristaps 390: int i;
1.30 kristaps 391: struct tag *t;
392:
1.94 kristaps 393: /* Push this tags onto the stack of open scopes. */
394:
1.30 kristaps 395: if ( ! (HTML_NOSTACK & htmltags[tag].flags)) {
1.72 kristaps 396: t = malloc(sizeof(struct tag));
397: if (NULL == t) {
1.75 kristaps 398: perror(NULL);
1.112 kristaps 399: exit((int)MANDOCLEVEL_SYSERR);
1.72 kristaps 400: }
1.30 kristaps 401: t->tag = tag;
1.66 kristaps 402: t->next = h->tags.head;
403: h->tags.head = t;
1.30 kristaps 404: } else
405: t = NULL;
1.29 kristaps 406:
407: if ( ! (HTML_NOSPACE & h->flags))
1.105 kristaps 408: if ( ! (HTML_CLRLINE & htmltags[tag].flags)) {
409: /* Manage keeps! */
410: if ( ! (HTML_KEEP & h->flags)) {
411: if (HTML_PREKEEP & h->flags)
412: h->flags |= HTML_KEEP;
413: putchar(' ');
414: } else
415: printf(" ");
416: }
1.29 kristaps 417:
1.109 kristaps 418: if ( ! (h->flags & HTML_NONOSPACE))
419: h->flags &= ~HTML_NOSPACE;
1.110 kristaps 420: else
421: h->flags |= HTML_NOSPACE;
1.109 kristaps 422:
1.94 kristaps 423: /* Print out the tag name and attributes. */
424:
1.29 kristaps 425: printf("<%s", htmltags[tag].name);
1.94 kristaps 426: for (i = 0; i < sz; i++)
427: print_attr(h, htmlattrs[p[i].key], p[i].val);
428:
429: /* Add non-overridable attributes. */
430:
431: if (TAG_HTML == tag && HTML_XHTML_1_0_STRICT == h->type) {
432: print_attr(h, "xmlns", "http://www.w3.org/1999/xhtml");
433: print_attr(h, "xml:lang", "en");
434: print_attr(h, "lang", "en");
1.29 kristaps 435: }
1.93 kristaps 436:
1.94 kristaps 437: /* Accomodate for XML "well-formed" singleton escaping. */
438:
1.93 kristaps 439: if (HTML_AUTOCLOSE & htmltags[tag].flags)
440: switch (h->type) {
441: case (HTML_XHTML_1_0_STRICT):
442: putchar('/');
443: break;
444: default:
445: break;
446: }
447:
1.78 kristaps 448: putchar('>');
1.14 kristaps 449:
1.29 kristaps 450: h->flags |= HTML_NOSPACE;
1.30 kristaps 451: return(t);
1.14 kristaps 452: }
453:
454:
1.29 kristaps 455: static void
456: print_ctag(struct html *h, enum htmltag tag)
1.14 kristaps 457: {
458:
1.29 kristaps 459: printf("</%s>", htmltags[tag].name);
1.71 kristaps 460: if (HTML_CLRLINE & htmltags[tag].flags) {
1.29 kristaps 461: h->flags |= HTML_NOSPACE;
1.78 kristaps 462: putchar('\n');
1.87 kristaps 463: }
1.14 kristaps 464: }
465:
466:
1.51 kristaps 467: void
1.93 kristaps 468: print_gen_decls(struct html *h)
1.1 kristaps 469: {
1.93 kristaps 470:
471: print_xmltype(h);
472: print_doctype(h);
473: }
474:
475:
476: static void
477: print_xmltype(struct html *h)
478: {
479:
1.100 kristaps 480: if (HTML_XHTML_1_0_STRICT == h->type)
481: printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1.93 kristaps 482: }
483:
484:
485: static void
486: print_doctype(struct html *h)
487: {
488: const char *doctype;
489: const char *dtd;
1.96 kristaps 490: const char *name;
1.93 kristaps 491:
492: switch (h->type) {
493: case (HTML_HTML_4_01_STRICT):
1.96 kristaps 494: name = "HTML";
1.93 kristaps 495: doctype = "-//W3C//DTD HTML 4.01//EN";
496: dtd = "http://www.w3.org/TR/html4/strict.dtd";
497: break;
498: default:
1.96 kristaps 499: name = "html";
1.93 kristaps 500: doctype = "-//W3C//DTD XHTML 1.0 Strict//EN";
501: dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
502: break;
503: }
504:
1.96 kristaps 505: printf("<!DOCTYPE %s PUBLIC \"%s\" \"%s\">\n",
506: name, doctype, dtd);
1.1 kristaps 507: }
508:
509:
1.51 kristaps 510: void
1.104 kristaps 511: print_text(struct html *h, const char *word)
1.1 kristaps 512: {
513:
1.104 kristaps 514: if (word[0] && '\0' == word[1])
515: switch (word[0]) {
1.29 kristaps 516: case('.'):
517: /* FALLTHROUGH */
518: case(','):
519: /* FALLTHROUGH */
520: case(';'):
521: /* FALLTHROUGH */
522: case(':'):
523: /* FALLTHROUGH */
524: case('?'):
525: /* FALLTHROUGH */
526: case('!'):
527: /* FALLTHROUGH */
528: case(')'):
529: /* FALLTHROUGH */
530: case(']'):
1.52 kristaps 531: if ( ! (HTML_IGNDELIM & h->flags))
532: h->flags |= HTML_NOSPACE;
1.30 kristaps 533: break;
1.29 kristaps 534: default:
535: break;
536: }
1.1 kristaps 537:
1.105 kristaps 538: if ( ! (HTML_NOSPACE & h->flags)) {
539: /* Manage keeps! */
540: if ( ! (HTML_KEEP & h->flags)) {
541: if (HTML_PREKEEP & h->flags)
542: h->flags |= HTML_KEEP;
543: putchar(' ');
544: } else
545: printf(" ");
546: }
1.30 kristaps 547:
1.104 kristaps 548: assert(word);
549: if ( ! print_encode(h, word, 0))
1.109 kristaps 550: if ( ! (h->flags & HTML_NONOSPACE))
551: h->flags &= ~HTML_NOSPACE;
1.113 schwarze 552:
553: h->flags &= ~HTML_IGNDELIM;
1.8 kristaps 554:
1.98 kristaps 555: /*
556: * Note that we don't process the pipe: the parser sees it as
557: * punctuation, but we don't in terms of typography.
558: */
1.104 kristaps 559: if (word[0] && '\0' == word[1])
560: switch (word[0]) {
1.29 kristaps 561: case('('):
562: /* FALLTHROUGH */
563: case('['):
564: h->flags |= HTML_NOSPACE;
1.30 kristaps 565: break;
1.29 kristaps 566: default:
567: break;
568: }
1.1 kristaps 569: }
1.30 kristaps 570:
571:
1.51 kristaps 572: void
1.30 kristaps 573: print_tagq(struct html *h, const struct tag *until)
574: {
575: struct tag *tag;
576:
1.66 kristaps 577: while ((tag = h->tags.head) != NULL) {
1.89 kristaps 578: if (tag == h->metaf)
579: h->metaf = NULL;
1.30 kristaps 580: print_ctag(h, tag->tag);
1.66 kristaps 581: h->tags.head = tag->next;
1.30 kristaps 582: free(tag);
583: if (until && tag == until)
584: return;
585: }
586: }
587:
588:
1.51 kristaps 589: void
1.30 kristaps 590: print_stagq(struct html *h, const struct tag *suntil)
591: {
592: struct tag *tag;
593:
1.66 kristaps 594: while ((tag = h->tags.head) != NULL) {
1.30 kristaps 595: if (suntil && tag == suntil)
596: return;
1.89 kristaps 597: if (tag == h->metaf)
598: h->metaf = NULL;
1.30 kristaps 599: print_ctag(h, tag->tag);
1.66 kristaps 600: h->tags.head = tag->next;
1.30 kristaps 601: free(tag);
602: }
603: }
1.55 kristaps 604:
605:
606: void
607: bufinit(struct html *h)
608: {
609:
610: h->buf[0] = '\0';
611: h->buflen = 0;
612: }
613:
614:
615: void
1.58 kristaps 616: bufcat_style(struct html *h, const char *key, const char *val)
617: {
618:
619: bufcat(h, key);
620: bufncat(h, ":", 1);
621: bufcat(h, val);
622: bufncat(h, ";", 1);
623: }
624:
625:
626: void
1.55 kristaps 627: bufcat(struct html *h, const char *p)
628: {
629:
630: bufncat(h, p, strlen(p));
631: }
632:
633:
634: void
635: buffmt(struct html *h, const char *fmt, ...)
636: {
637: va_list ap;
638:
639: va_start(ap, fmt);
1.56 kristaps 640: (void)vsnprintf(h->buf + (int)h->buflen,
1.55 kristaps 641: BUFSIZ - h->buflen - 1, fmt, ap);
642: va_end(ap);
643: h->buflen = strlen(h->buf);
644: }
645:
646:
647: void
648: bufncat(struct html *h, const char *p, size_t sz)
649: {
650:
651: if (h->buflen + sz > BUFSIZ - 1)
652: sz = BUFSIZ - 1 - h->buflen;
653:
654: (void)strncat(h->buf, p, sz);
655: h->buflen += sz;
656: }
657:
658:
659: void
660: buffmt_includes(struct html *h, const char *name)
661: {
662: const char *p, *pp;
663:
664: pp = h->base_includes;
1.61 kristaps 665:
666: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 667: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 668: switch (*(p + 1)) {
669: case('I'):
670: bufcat(h, name);
671: break;
672: default:
673: bufncat(h, p, 2);
674: break;
675: }
676: pp = p + 2;
677: }
678: if (pp)
679: bufcat(h, pp);
680: }
681:
682:
683: void
684: buffmt_man(struct html *h,
685: const char *name, const char *sec)
686: {
687: const char *p, *pp;
688:
689: pp = h->base_man;
1.61 kristaps 690:
691: /* LINTED */
692: while (NULL != (p = strchr(pp, '%'))) {
1.56 kristaps 693: bufncat(h, pp, (size_t)(p - pp));
1.55 kristaps 694: switch (*(p + 1)) {
695: case('S'):
1.58 kristaps 696: bufcat(h, sec ? sec : "1");
1.55 kristaps 697: break;
698: case('N'):
1.58 kristaps 699: buffmt(h, name);
1.55 kristaps 700: break;
701: default:
702: bufncat(h, p, 2);
703: break;
704: }
705: pp = p + 2;
706: }
707: if (pp)
708: bufcat(h, pp);
709: }
1.58 kristaps 710:
711:
712: void
713: bufcat_su(struct html *h, const char *p, const struct roffsu *su)
714: {
1.62 kristaps 715: double v;
1.63 kristaps 716: const char *u;
1.58 kristaps 717:
718: v = su->scale;
719:
720: switch (su->unit) {
721: case (SCALE_CM):
722: u = "cm";
723: break;
724: case (SCALE_IN):
725: u = "in";
726: break;
727: case (SCALE_PC):
728: u = "pc";
729: break;
730: case (SCALE_PT):
731: u = "pt";
732: break;
1.59 kristaps 733: case (SCALE_EM):
734: u = "em";
735: break;
1.58 kristaps 736: case (SCALE_MM):
737: if (0 == (v /= 100))
738: v = 1;
739: u = "em";
740: break;
1.59 kristaps 741: case (SCALE_EN):
742: u = "ex";
743: break;
744: case (SCALE_BU):
745: u = "ex";
746: break;
1.58 kristaps 747: case (SCALE_VS):
748: u = "em";
749: break;
750: default:
751: u = "ex";
752: break;
753: }
754:
1.103 kristaps 755: /*
756: * XXX: the CSS spec isn't clear as to which types accept
757: * integer or real numbers, so we just make them all decimals.
758: */
759: buffmt(h, "%s: %.2f%s;", p, v, u);
1.58 kristaps 760: }
1.65 kristaps 761:
1.68 kristaps 762:
763: void
1.70 kristaps 764: html_idcat(char *dst, const char *src, int sz)
1.68 kristaps 765: {
1.70 kristaps 766: int ssz;
1.68 kristaps 767:
768: assert(sz);
769:
770: /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */
771:
1.70 kristaps 772: for ( ; *dst != '\0' && sz; dst++, sz--)
1.68 kristaps 773: /* Jump to end. */ ;
774:
1.70 kristaps 775: assert(sz > 2);
1.68 kristaps 776:
1.70 kristaps 777: /* We can't start with a number (bah). */
1.68 kristaps 778:
1.70 kristaps 779: *dst++ = 'x';
1.68 kristaps 780: *dst = '\0';
1.70 kristaps 781: sz--;
782:
783: for ( ; *src != '\0' && sz > 1; src++) {
1.73 kristaps 784: ssz = snprintf(dst, (size_t)sz, "%.2x", *src);
1.70 kristaps 785: sz -= ssz;
786: dst += ssz;
787: }
1.68 kristaps 788: }
CVSweb