Annotation of mandoc/tags.c, Revision 1.2
1.2 ! kristaps 1: /* $Id: tags.c,v 1.1 2008/12/10 00:52:46 kristaps Exp $ */
1.1 kristaps 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",
1.2 ! kristaps 40: "align",
1.1 kristaps 41: };
42:
43: static const char * const typenames[] = {
44: "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\""
45: "\"http://www.w3.org/TR/html4/strict.dtd\">",
46: };
47:
48:
49: /* FIXME: move into ml.c. */
50: static int
51: html_tstart(struct md_mbuf *mbuf, enum ml_scope scope,
52: enum html_tag tag, size_t *res)
53: {
54:
55: switch (scope) {
56: case (ML_OPEN):
57: if ( ! ml_nputs(mbuf, "<", 1, res))
58: return(0);
59: break;
60: case (ML_CLOSE):
61: if ( ! ml_nputs(mbuf, "</", 2, res))
62: return(0);
63: break;
64: default:
65: abort();
66: /* NOTREACHED */
67: }
68:
69: return(ml_puts(mbuf, tagnames[tag], res));
70: }
71:
72:
73: /* FIXME: move into ml.c. */
74: static int
75: html_tclose(struct md_mbuf *mbuf, size_t *res)
76: {
77:
78: return(ml_nputs(mbuf, ">", 1, res));
79: }
80:
81:
82:
83: int
84: html_stput(struct md_mbuf *mbuf, enum html_tag tag, size_t *res)
85: {
86:
87: return(ml_puts(mbuf, tagnames[tag], res));
88: }
89:
90:
91: int
92: html_saput(struct md_mbuf *mbuf, enum html_tag tag,
93: size_t *res, int sz, const struct html_pair *p)
94: {
95: int i;
96:
97: if ( ! ml_puts(mbuf, tagnames[tag], res))
98: return(0);
99:
100: assert(sz >= 0);
101: for (i = 0; i < sz; i++) {
102:
103: /* FIXME: move into ml.c. */
104:
105: if ( ! ml_nputs(mbuf, " ", 1, res))
106: return(0);
107: if ( ! ml_puts(mbuf, attrnames[p[i].attr], res))
108: return(0);
109: if ( ! ml_nputs(mbuf, "=\"", 2, res))
110: return(0);
111: if ( ! ml_putstring(mbuf, p[i].val, res))
112: return(0);
113: if ( ! ml_nputs(mbuf, "\"", 1, res))
114: return(0);
115: }
116:
117: return(1);
118: }
119:
120:
121: int
122: html_tput(struct md_mbuf *mbuf, enum ml_scope scope,
123: enum html_tag tag, size_t *res)
124: {
125:
126: if ( ! html_tstart(mbuf, scope, tag, res))
127: return(0);
128: return(html_tclose(mbuf, res));
129: }
130:
131:
132: int
133: html_aput(struct md_mbuf *mbuf, enum ml_scope scope,
134: enum html_tag tag, size_t *res,
135: int sz, const struct html_pair *p)
136: {
137: int i;
138:
139: if ( ! html_tstart(mbuf, scope, tag, res))
140: return(0);
141:
142: assert(sz >= 0);
143: for (i = 0; i < sz; i++) {
144:
145: /* FIXME: move into ml.c. */
146:
147: if ( ! ml_nputs(mbuf, " ", 1, res))
148: return(0);
149: if ( ! ml_puts(mbuf, attrnames[p[i].attr], res))
150: return(0);
151: if ( ! ml_nputs(mbuf, "=\"", 2, res))
152: return(0);
153: if ( ! ml_putstring(mbuf, p[i].val, res))
154: return(0);
155: if ( ! ml_nputs(mbuf, "\"", 1, res))
156: return(0);
157: }
158:
159: return(html_tclose(mbuf, res));
160: }
161:
162:
163: int
164: html_typeput(struct md_mbuf *mbuf,
165: enum html_type type, size_t *res)
166: {
167:
168: return(ml_puts(mbuf, typenames[type], res));
169: }
170:
171:
172: int
173: html_commentput(struct md_mbuf *mbuf,
174: enum ml_scope scope, size_t *res)
175: {
176:
177: switch (scope) {
178: case (ML_OPEN):
179: return(ml_nputs(mbuf, "<!--", 4, res));
180: case (ML_CLOSE):
181: return(ml_nputs(mbuf, "-->", 3, res));
182: default:
183: break;
184: }
185:
186: abort();
187: /* NOTREACHED */
188: }
CVSweb