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