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.14 2002-07-05 12:42:52 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 int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
32 for (c = n->child; c; c = c->next)
36 if (c->which == DATA1N_preprocess)
41 sprintf (line, "%*s", col, "");
45 wrbuf_puts (b, c->u.preprocess.target);
46 for (p = c->u.preprocess.attributes; p; p = p->next)
49 wrbuf_puts (b, p->name);
52 wrbuf_puts (b, p->value);
57 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
59 wrbuf_puts (b, "?>\n");
61 else if (c->which == DATA1N_tag)
63 if (select && c->u.tag.node_selected)
66 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
68 if (nodetoidsgml(c, select, b, col) < 0)
76 sprintf (line, "%*s", col, "");
81 for (p = c->u.tag.attributes; p; p = p->next)
84 wrbuf_puts (b, p->name);
87 wrbuf_puts (b, p->value);
94 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
97 sprintf (line, "%*s", col);
104 wrbuf_puts (b, "\n");
108 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
110 char *p = c->u.data.data;
111 int l = c->u.data.len;
116 if (!c->u.data.formatted_text)
118 sprintf(line, "%*s", col, "");
119 wrbuf_write(b, line, strlen(line));
122 if (c->which == DATA1N_comment)
124 wrbuf_write (b, "<!--", 4);
126 switch (c->u.data.what)
130 if (c->u.data.formatted_text)
132 wrbuf_write (b, p, l);
140 while (l && d1_isspace(*p))
144 /* break if we cross margin and word is not too long */
145 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
146 wlen < IDSGML_MARGIN)
148 sprintf(line, "\n%*s", col, "");
150 wrbuf_write(b, line, strlen(line));
158 while (l && !d1_isspace(*p))
167 wrbuf_write(b, "\n", 1);
170 wrbuf_write (b, p, l);
174 wrbuf_write(b, c->u.data.data, c->u.data.len);
180 wrbuf_write(b, c->u.data.data, c->u.data.len);
185 if (c->which == DATA1N_comment)
187 wrbuf_puts(b, "-->");
197 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
199 WRBUF b = data1_get_wrbuf (dh);
203 if (nodetoidsgml(n, select, b, 0))