Subject: | patch for writer, introducing line breaks and indentation |
XML::Fast is indeed a very fast parser, and provides support for all that is needed
in the project I am working on. However, the writer doesn't break lines on the XML tags,
and doesn't indent - which makes the XML that the writer generates hardly readable.
Please find attached a patch that implements line breaks and a configurable indentation.
Caveat: it's a quick hack, and uses a static buffer of 4096 characters for the indent,
so this may need to be improved. Nonetheless I hope you'll find this patch useful and
can integrate that in a new release.
Many thanks and best regards,
Marek
Subject: | XML-Fast.diff |
diff -ruN XML-Fast-0.17/Fast.xs XML-Fast-0.17-GWp80S/Fast.xs
--- XML-Fast-0.17/Fast.xs 2017-06-30 01:04:20.000000000 +0200
+++ XML-Fast-0.17-GWp80S/Fast.xs 2018-09-26 17:12:24.913028000 +0200
@@ -96,7 +96,6 @@
char * attr; STRLEN attl;
char * text;
- char * join;
char * cdata;
char * comm;
@@ -122,7 +121,8 @@
I32 ix;
SV * result;
SV * nested;
-
+
+ I32 indent;
} compstate;
@@ -925,6 +925,7 @@
}
}
+static char indent_buffer[4096];
void kv2x ( char *key, SV *val, compstate *p );
void kv2x ( char *key, SV *val, compstate *p ) {
@@ -939,6 +940,14 @@
SV **avv;
debug("key=%s, val=%s",key, SvPV_nolen(val));
+
+ nlen = p->indent * p->depth;
+ for ( i = 0; i < nlen; i++ ) {
+ indent_buffer[i] = ' ';
+ }
+ indent_buffer[nlen] = (char)0;
+ h2xp(p, "%s", indent_buffer);
+
if ( mystrcmp( key, p->text ) == 0 ) {
h2xpe(p, SvPV_nolen( val ));
}
@@ -983,7 +992,7 @@
debug("Nested %s->%s", nkey, SvPV_nolen(HeVAL(ent)));
if (!closed) {
closed = 1;
- h2xp(p,">");
+ h2xp(p,">\n");
}
p->depth++;
kv2x( nkey, HeVAL(ent), p );
@@ -991,9 +1000,9 @@
}
if (!closed) {
- h2xp(p, "/>");
+ h2xp(p, "/>\n");
} else {
- h2xp(p, "</%s>",key);
+ h2xp(p, "</%s>\n",key);
}
@@ -1318,8 +1327,8 @@
PROTOTYPE: $$
PPCODE:
compstate ctx;
- memset(&ctx,0,sizeof(parsestate));
-
+ memset(&ctx,0,sizeof(compstate));
+
SV **key;
if ((key = hv_fetch(conf, "order", 5, 0)) && SvTRUE(*key)) {
ctx.flags |= MODE_ORDER;
@@ -1360,6 +1369,10 @@
} else {
ctx.comm = 0;
}
+ ctx.indent = 2;
+ if ((key = hv_fetch(conf, "indent", 6, 0)) && SvIOK(*key)) {
+ ctx.indent = SvIV(*key);
+ }
/*
if ((key = hv_fetch(conf, "array", 5, 0)) && SvOK(*key)) {
if (SvROK(*key) && SvTYPE( SvRV(*key) ) == SVt_PVAV) {
diff -ruN XML-Fast-0.17/lib/XML/Fast.pm XML-Fast-0.17-GWp80S/lib/XML/Fast.pm
--- XML-Fast-0.17/lib/XML/Fast.pm 2017-06-30 01:04:20.000000000 +0200
+++ XML-Fast-0.17-GWp80S/lib/XML/Fast.pm 2018-09-26 17:04:58.297971000 +0200
@@ -141,6 +141,10 @@
Trim leading and trailing whitespace from text nodes
+=item indent [ = 2 ]
+
+Indent nested tags by that many space characters.
+
=item cdata [ = undef ]
When defined, CDATA sections will be stored under this key