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.15 2002-07-25 12:52:53 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, "<");
53 wrbuf_puts (b, "&");
57 wrbuf_putc(b, msg[i]);
62 static void wrbuf_put_cdata(WRBUF b, const char *msg)
64 wrbuf_write_cdata (b, msg, strlen(msg));
67 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col,
72 for (c = n->child; c; c = c->next)
76 if (c->which == DATA1N_preprocess)
83 wrbuf_put_cdata (b, c->u.preprocess.target);
84 for (p = c->u.preprocess.attributes; p; p = p->next)
87 wrbuf_put_cdata (b, p->name);
90 wrbuf_put_cdata (b, p->value);
95 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
98 wrbuf_puts (b, "?>\n");
100 else if (c->which == DATA1N_tag)
102 if (select && c->u.tag.node_selected)
105 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
107 if (nodetoidsgml(c, select, b, col, pretty_format) < 0)
117 wrbuf_put_cdata (b, tag);
118 for (p = c->u.tag.attributes; p; p = p->next)
121 wrbuf_put_cdata (b, p->name);
124 wrbuf_put_cdata (b, p->value);
130 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2,
136 wrbuf_put_cdata(b, tag);
139 wrbuf_puts (b, "\n");
142 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
144 char *p = c->u.data.data;
145 int l = c->u.data.len;
149 if (pretty_format && !c->u.data.formatted_text)
151 if (c->which == DATA1N_comment)
152 wrbuf_puts (b, "<!--");
153 switch (c->u.data.what)
156 if (!pretty_format || c->u.data.formatted_text)
158 wrbuf_write_cdata (b, p, l);
166 while (l && d1_isspace(*p))
170 /* break if we cross margin and word is not too long */
171 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
172 wlen < IDSGML_MARGIN)
174 wrbuf_puts (b, "\n");
184 while (l && !d1_isspace(*p))
197 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
202 wrbuf_write_cdata(b, c->u.data.data, c->u.data.len);
206 if (c->which == DATA1N_comment)
208 wrbuf_puts(b, "-->");
217 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
219 WRBUF b = data1_get_wrbuf (dh);
223 if (nodetoidsgml(n, select, b, 0, 0 /* no pretty format */))