2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: d1_write.c,v 1.17 2002-08-23 14:25:07 adam Exp $
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
14 #define IDSGML_MARGIN 75
16 #define PRETTY_FORMAT 0
18 static int wordlen(char *b, int max)
22 while (l < max && !d1_isspace(*b))
27 static void indent (WRBUF b, int col)
30 for (i = 0; i<col; i++)
34 static void wrbuf_write_cdata(WRBUF b, const char *msg, int len)
38 for (i = 0; i < len; i++)
43 wrbuf_puts (b, """);
46 wrbuf_puts (b, "'");
49 wrbuf_puts (b, ">");
52 wrbuf_puts (b, "<");
55 wrbuf_puts (b, "&");
58 wrbuf_putc(b, msg[i]);
63 static void wrbuf_put_cdata(WRBUF b, const char *msg)
65 wrbuf_write_cdata (b, msg, strlen(msg));
68 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
73 for (c = n->child; c; c = c->next)
77 if (c->which == DATA1N_preprocess)
84 wrbuf_put_cdata (b, c->u.preprocess.target);
85 for (p = c->u.preprocess.attributes; p; p = p->next)
88 wrbuf_put_cdata (b, p->name);
91 wrbuf_put_cdata (b, p->value);
96 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
99 wrbuf_puts (b, "?>\n");
101 else if (c->which == DATA1N_tag)
103 if (select && c->u.tag.node_selected)
106 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
108 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
118 wrbuf_put_cdata (b, tag);
119 for (p = c->u.tag.attributes; p; p = p->next)
122 wrbuf_put_cdata (b, p->name);
125 wrbuf_put_cdata (b, p->value);
131 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
137 wrbuf_put_cdata(b, tag);
140 wrbuf_puts (b, "\n");
143 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
145 char *p = c->u.data.data;
146 int l = c->u.data.len;
150 if (pretty_format && !c->u.data.formatted_text)
152 if (c->which == DATA1N_comment)
153 wrbuf_puts (b, "<!--");
154 switch (c->u.data.what)
157 if (!pretty_format || c->u.data.formatted_text)
159 wrbuf_write_cdata (b, p, l);
167 while (l && d1_isspace(*p))
171 /* break if we cross margin and word is not too long */
172 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
173 wlen < IDSGML_MARGIN)
175 wrbuf_puts (b, "\n");
185 while (l && !d1_isspace(*p))
198 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
203 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
207 if (c->which == DATA1N_comment)
209 wrbuf_puts(b, "-->");
218 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
220 WRBUF b = data1_get_wrbuf (dh);
224 if (!data1_is_xmlmode (dh))
227 wrbuf_puts (b, n->u.root.type);
228 wrbuf_puts (b, ">\n");
230 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))
232 if (!data1_is_xmlmode (dh))
234 wrbuf_puts (b, "</");
235 wrbuf_puts (b, n->u.root.type);
236 wrbuf_puts (b, ">\n");