Annotation of mandoc/tags.c, Revision 1.1
1.1 ! kristaps 1: /* $Id: html.c,v 1.18 2008/12/09 19:57:26 kristaps Exp $ */
! 2: /*
! 3: * Copyright (c) 2008 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
! 7: * above copyright notice and this permission notice appear in all
! 8: * copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
! 11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
! 12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
! 13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
! 14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
! 15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
! 16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
! 17: * PERFORMANCE OF THIS SOFTWARE.
! 18: */
! 19: #include <assert.h>
! 20: #include <stdarg.h>
! 21: #include <stdlib.h>
! 22:
! 23: #include "html.h"
! 24:
! 25: static int html_tstart(struct md_mbuf *, enum ml_scope,
! 26: enum html_tag, size_t *);
! 27: static int html_tclose(struct md_mbuf *, size_t *);
! 28:
! 29: static const char * const tagnames[] = {
! 30: "span", "html", "head", "meta",
! 31: "title", "style", "link", "body",
! 32: "div", "table", "td", "tr",
! 33: "ol", "ul", "li", "h1",
! 34: "h2", "a",
! 35: };
! 36:
! 37: static const char * const attrnames[] = {
! 38: "class", "http-equiv", "content", "name",
! 39: "type", "rel", "href", "width",
! 40: };
! 41:
! 42: static const char * const typenames[] = {
! 43: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
! 44: "\"http://www.w3.org/TR/html4/strict.dtd\">",
! 45: };
! 46:
! 47:
! 48: /* FIXME: move into ml.c. */
! 49: static int
! 50: html_tstart(struct md_mbuf *mbuf, enum ml_scope scope,
! 51: enum html_tag tag, size_t *res)
! 52: {
! 53:
! 54: switch (scope) {
! 55: case (ML_OPEN):
! 56: if ( ! ml_nputs(mbuf, "<", 1, res))
! 57: return(0);
! 58: break;
! 59: case (ML_CLOSE):
! 60: if ( ! ml_nputs(mbuf, "</", 2, res))
! 61: return(0);
! 62: break;
! 63: default:
! 64: abort();
! 65: /* NOTREACHED */
! 66: }
! 67:
! 68: return(ml_puts(mbuf, tagnames[tag], res));
! 69: }
! 70:
! 71:
! 72: /* FIXME: move into ml.c. */
! 73: static int
! 74: html_tclose(struct md_mbuf *mbuf, size_t *res)
! 75: {
! 76:
! 77: return(ml_nputs(mbuf, ">", 1, res));
! 78: }
! 79:
! 80:
! 81:
! 82: int
! 83: html_stput(struct md_mbuf *mbuf, enum html_tag tag, size_t *res)
! 84: {
! 85:
! 86: return(ml_puts(mbuf, tagnames[tag], res));
! 87: }
! 88:
! 89:
! 90: int
! 91: html_saput(struct md_mbuf *mbuf, enum html_tag tag,
! 92: size_t *res, int sz, const struct html_pair *p)
! 93: {
! 94: int i;
! 95:
! 96: if ( ! ml_puts(mbuf, tagnames[tag], res))
! 97: return(0);
! 98:
! 99: assert(sz >= 0);
! 100: for (i = 0; i < sz; i++) {
! 101:
! 102: /* FIXME: move into ml.c. */
! 103:
! 104: if ( ! ml_nputs(mbuf, " ", 1, res))
! 105: return(0);
! 106: if ( ! ml_puts(mbuf, attrnames[p[i].attr], res))
! 107: return(0);
! 108: if ( ! ml_nputs(mbuf, "=\"", 2, res))
! 109: return(0);
! 110: if ( ! ml_putstring(mbuf, p[i].val, res))
! 111: return(0);
! 112: if ( ! ml_nputs(mbuf, "\"", 1, res))
! 113: return(0);
! 114: }
! 115:
! 116: return(1);
! 117: }
! 118:
! 119:
! 120: int
! 121: html_tput(struct md_mbuf *mbuf, enum ml_scope scope,
! 122: enum html_tag tag, size_t *res)
! 123: {
! 124:
! 125: if ( ! html_tstart(mbuf, scope, tag, res))
! 126: return(0);
! 127: return(html_tclose(mbuf, res));
! 128: }
! 129:
! 130:
! 131: int
! 132: html_aput(struct md_mbuf *mbuf, enum ml_scope scope,
! 133: enum html_tag tag, size_t *res,
! 134: int sz, const struct html_pair *p)
! 135: {
! 136: int i;
! 137:
! 138: if ( ! html_tstart(mbuf, scope, tag, res))
! 139: return(0);
! 140:
! 141: assert(sz >= 0);
! 142: for (i = 0; i < sz; i++) {
! 143:
! 144: /* FIXME: move into ml.c. */
! 145:
! 146: if ( ! ml_nputs(mbuf, " ", 1, res))
! 147: return(0);
! 148: if ( ! ml_puts(mbuf, attrnames[p[i].attr], res))
! 149: return(0);
! 150: if ( ! ml_nputs(mbuf, "=\"", 2, res))
! 151: return(0);
! 152: if ( ! ml_putstring(mbuf, p[i].val, res))
! 153: return(0);
! 154: if ( ! ml_nputs(mbuf, "\"", 1, res))
! 155: return(0);
! 156: }
! 157:
! 158: return(html_tclose(mbuf, res));
! 159: }
! 160:
! 161:
! 162: int
! 163: html_typeput(struct md_mbuf *mbuf,
! 164: enum html_type type, size_t *res)
! 165: {
! 166:
! 167: return(ml_puts(mbuf, typenames[type], res));
! 168: }
! 169:
! 170:
! 171: int
! 172: html_commentput(struct md_mbuf *mbuf,
! 173: enum ml_scope scope, size_t *res)
! 174: {
! 175:
! 176: switch (scope) {
! 177: case (ML_OPEN):
! 178: return(ml_nputs(mbuf, "<!--", 4, res));
! 179: case (ML_CLOSE):
! 180: return(ml_nputs(mbuf, "-->", 3, res));
! 181: default:
! 182: break;
! 183: }
! 184:
! 185: abort();
! 186: /* NOTREACHED */
! 187: }
CVSweb