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.13 2002-07-03 10:04:04 adam Exp $
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
14 #define IDSGML_MARGIN 75
16 static int wordlen(char *b, int max)
20 while (l < max && !d1_isspace(*b))
25 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
30 for (c = n->child; c; c = c->next)
34 if (c->which == DATA1N_preprocess)
38 sprintf (line, "%*s<?", col, "");
40 wrbuf_puts (b, c->u.preprocess.target);
41 for (p = c->u.preprocess.attributes; p; p = p->next)
44 wrbuf_puts (b, p->name);
47 wrbuf_puts (b, p->value);
52 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
54 wrbuf_puts (b, "?>\n");
56 else if (c->which == DATA1N_tag)
58 if (select && c->u.tag.node_selected)
61 if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
63 if (nodetoidsgml(c, select, b, col) < 0)
70 sprintf (line, "%*s<", col, "");
73 for (p = c->u.tag.attributes; p; p = p->next)
76 wrbuf_puts (b, p->name);
79 wrbuf_puts (b, p->value);
83 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
85 sprintf (line, "%*s</%s>\n", col, "", tag);
86 wrbuf_write(b, line, strlen(line));
89 else if (c->which == DATA1N_data || c->which == DATA1N_comment)
91 char *p = c->u.data.data;
92 int l = c->u.data.len;
96 if (!c->u.data.formatted_text)
98 sprintf(line, "%*s", col, "");
99 wrbuf_write(b, line, strlen(line));
101 if (c->which == DATA1N_comment)
103 wrbuf_write (b, "<!--", 4);
105 switch (c->u.data.what)
108 if (c->u.data.formatted_text)
110 wrbuf_write (b, p, l);
118 while (l && d1_isspace(*p))
122 /* break if we cross margin and word is not too long */
123 if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
124 wlen < IDSGML_MARGIN)
126 sprintf(line, "\n%*s", col, "");
128 wrbuf_write(b, line, strlen(line));
136 while (l && !d1_isspace(*p))
145 wrbuf_write(b, "\n", 1);
149 wrbuf_write(b, c->u.data.data, c->u.data.len);
150 wrbuf_write(b, "\n", 1);
153 wrbuf_write(b, c->u.data.data, c->u.data.len);
154 wrbuf_write(b, "\n", 1);
156 if (c->which == DATA1N_comment)
158 wrbuf_write (b, "-->\n", 4);
165 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
167 WRBUF b = data1_get_wrbuf (dh);
171 if (nodetoidsgml(n, select, b, 0))