]>
git.cameronkatri.com Git - mandoc.git/blob - xml.c
1 /* $Id: xml.c,v 1.16 2008/12/05 19:45:15 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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
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.
23 #include "libmdocml.h"
28 static int xml_alloc(void **);
29 static void xml_free(void *);
30 static ssize_t
xml_endtag(struct md_mbuf
*, void *,
31 const struct md_args
*,
33 static ssize_t
xml_begintag(struct md_mbuf
*, void *,
34 const struct md_args
*,
36 const int *, const char **);
37 static int xml_begin(struct md_mbuf
*,
38 const struct md_args
*,
40 const char *, const char *,
41 const char *, const char *);
42 static int xml_end(struct md_mbuf
*,
43 const struct md_args
*);
44 static ssize_t
xml_printtagname(struct md_mbuf
*,
46 static ssize_t
xml_printtagargs(struct md_mbuf
*,
47 const int *, const char **);
51 xml_printtagargs(struct md_mbuf
*mbuf
, const int *argc
,
57 if (NULL
== argc
|| NULL
== argv
)
61 for (res
= 0, i
= 0; ROFF_ARGMAX
!= (c
= argc
[i
]); i
++) {
62 if ( ! ml_nputs(mbuf
, " ", 1, &res
))
65 if ( ! ml_puts(mbuf
, tokargnames
[c
], &res
))
67 if ( ! ml_nputs(mbuf
, "=\"", 2, &res
))
70 if ( ! ml_putstring(mbuf
, argv
[i
], &res
))
72 } else if ( ! ml_nputs(mbuf
, "true", 4, &res
))
74 if ( ! ml_nputs(mbuf
, "\"", 1, &res
))
83 xml_printtagname(struct md_mbuf
*mbuf
, enum md_ns ns
, int tok
)
90 if ( ! ml_nputs(mbuf
, "block:", 6, &res
))
94 if ( ! ml_nputs(mbuf
, "inline:", 7, &res
))
98 if ( ! ml_nputs(mbuf
, "body:", 5, &res
))
102 if ( ! ml_nputs(mbuf
, "head:", 5, &res
))
109 if ( ! ml_puts(mbuf
, toknames
[tok
], &res
))
111 return((ssize_t
)res
);
117 xml_begin(struct md_mbuf
*mbuf
, const struct md_args
*args
,
118 const struct tm
*tm
, const char *os
,
119 const char *title
, const char *section
,
123 if ( ! ml_puts(mbuf
, "<?xml version=\"1.0\" "
124 "encoding=\"UTF-8\"?>\n", NULL
))
126 return(ml_puts(mbuf
, "<mdoc xmlns:block=\"block\" "
127 "xmlns:special=\"special\" "
128 "xmlns:inline=\"inline\">", NULL
));
134 xml_end(struct md_mbuf
*mbuf
, const struct md_args
*args
)
137 return(ml_puts(mbuf
, "</mdoc>", NULL
));
143 xml_begintag(struct md_mbuf
*mbuf
, void *data
,
144 const struct md_args
*args
, enum md_ns ns
,
145 int tok
, const int *argc
, const char **argv
)
149 if (-1 == (res
= xml_printtagname(mbuf
, ns
, tok
)))
151 if (-1 == (sz
= xml_printtagargs(mbuf
, argc
, argv
)))
159 xml_endtag(struct md_mbuf
*mbuf
, void *data
,
160 const struct md_args
*args
, enum md_ns ns
, int tok
)
163 return(xml_printtagname(mbuf
, ns
, tok
));
186 md_line_xml(void *data
, char *buf
)
189 return(mlg_line((struct md_mlg
*)data
, buf
));
194 md_exit_xml(void *data
, int flush
)
197 return(mlg_exit((struct md_mlg
*)data
, flush
));
202 md_init_xml(const struct md_args
*args
,
203 struct md_mbuf
*mbuf
, const struct md_rbuf
*rbuf
)
207 cbs
.ml_alloc
= xml_alloc
;
208 cbs
.ml_free
= xml_free
;
209 cbs
.ml_begintag
= xml_begintag
;
210 cbs
.ml_endtag
= xml_endtag
;
211 cbs
.ml_begin
= xml_begin
;
212 cbs
.ml_end
= xml_end
;
214 return(mlg_alloc(args
, rbuf
, mbuf
, &cbs
));