]> git.cameronkatri.com Git - mandoc.git/blob - xml.c
5ff28cb7f4cf73d76d76d21df6dad4f4bf38869f
[mandoc.git] / xml.c
1 /* $Id: xml.c,v 1.15 2008/12/05 17:43:14 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 <stdlib.h>
20 #include <string.h>
21
22 #include "libmdocml.h"
23 #include "private.h"
24 #include "ml.h"
25
26
27 static ssize_t xml_endtag(struct md_mbuf *, void *,
28 const struct md_args *,
29 enum md_ns, int);
30 static ssize_t xml_begintag(struct md_mbuf *, void *,
31 const struct md_args *,
32 enum md_ns, int,
33 const int *, const char **);
34 static int xml_begin(struct md_mbuf *,
35 const struct md_args *,
36 const struct tm *,
37 const char *, const char *,
38 const char *, const char *);
39 static int xml_end(struct md_mbuf *,
40 const struct md_args *);
41
42
43 /* ARGSUSED */
44 static int
45 xml_begin(struct md_mbuf *mbuf, const struct md_args *args,
46 const struct tm *tm, const char *os,
47 const char *title, const char *section,
48 const char *vol)
49 {
50 size_t res;
51
52 if ( ! ml_puts(mbuf, "<?xml version=\"1.0\" "
53 "encoding=\"UTF-8\"?>\n", &res))
54 return(0);
55 if ( ! ml_puts(mbuf, "<mdoc xmlns:block=\"block\" "
56 "xmlns:special=\"special\" "
57 "xmlns:inline=\"inline\">", &res))
58 return(0);
59
60 return(1);
61 }
62
63
64 /* ARGSUSED */
65 static int
66 xml_end(struct md_mbuf *mbuf, const struct md_args *args)
67 {
68 size_t res;
69
70 res = 0;
71 if ( ! ml_puts(mbuf, "</mdoc>", &res))
72 return(0);
73
74 return(1);
75 }
76
77
78 /* ARGSUSED */
79 static ssize_t
80 xml_begintag(struct md_mbuf *mbuf, void *data,
81 const struct md_args *args, enum md_ns ns,
82 int tok, const int *argc, const char **argv)
83 {
84 size_t res;
85
86 /* FIXME: doesn't print arguments! */
87
88 res = 0;
89
90 switch (ns) {
91 case (MD_NS_BLOCK):
92 if ( ! ml_nputs(mbuf, "block:", 6, &res))
93 return(-1);
94 break;
95 case (MD_NS_BODY):
96 if ( ! ml_nputs(mbuf, "body:", 5, &res))
97 return(-1);
98 break;
99 case (MD_NS_HEAD):
100 if ( ! ml_nputs(mbuf, "head:", 5, &res))
101 return(-1);
102 break;
103 case (MD_NS_INLINE):
104 if ( ! ml_nputs(mbuf, "inline:", 7, &res))
105 return(-1);
106 break;
107 default:
108 break;
109 }
110
111 if ( ! ml_puts(mbuf, toknames[tok], &res))
112 return(-1);
113
114 return((ssize_t)res);
115 }
116
117
118 /* ARGSUSED */
119 static ssize_t
120 xml_endtag(struct md_mbuf *mbuf, void *data,
121 const struct md_args *args, enum md_ns ns, int tok)
122 {
123 size_t res;
124
125 res = 0;
126
127 switch (ns) {
128 case (MD_NS_BLOCK):
129 if ( ! ml_nputs(mbuf, "block:", 6, &res))
130 return(-1);
131 break;
132 case (MD_NS_INLINE):
133 if ( ! ml_nputs(mbuf, "inline:", 7, &res))
134 return(-1);
135 break;
136 case (MD_NS_BODY):
137 if ( ! ml_nputs(mbuf, "body:", 5, &res))
138 return(-1);
139 break;
140 case (MD_NS_HEAD):
141 if ( ! ml_nputs(mbuf, "head:", 5, &res))
142 return(-1);
143 break;
144 default:
145 break;
146 }
147
148 if ( ! ml_puts(mbuf, toknames[tok], &res))
149 return(-1);
150
151 return((ssize_t)res);
152 }
153
154
155 int
156 md_line_xml(void *data, char *buf)
157 {
158
159 return(mlg_line((struct md_mlg *)data, buf));
160 }
161
162
163 int
164 md_exit_xml(void *data, int flush)
165 {
166
167 return(mlg_exit((struct md_mlg *)data, flush));
168 }
169
170
171 void *
172 md_init_xml(const struct md_args *args,
173 struct md_mbuf *mbuf, const struct md_rbuf *rbuf)
174 {
175
176 return(mlg_alloc(args, NULL, rbuf, mbuf, xml_begintag,
177 xml_endtag, xml_begin, xml_end));
178 }
179