Skip to content

Commit 9f7814d

Browse files
committed
0.07
1 parent 11e1c07 commit 9f7814d

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Changes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
0.07 2020-01-11
12
- switch to use Test::Snapshot
3+
- implement make_canonicaliser
24

35
0.06 2020-01-01
46
- stop putting element "type" in output

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ XML::Invisible - transform "invisible XML" documents into XML using a grammar
3434
'print ast2xml(make_parser(join "", <>)->("(a+b)"))->toStringC14N(1)' \
3535
examples/arith-grammar.ixml | xml_pp
3636

37+
# canonicalise a document
38+
use XML::Invisible qw(make_parser make_canonicaliser);
39+
my $ixml_grammar = from_file('examples/arith-grammar.ixml');
40+
my $transformer = make_parser($ixml_grammar);
41+
my $ast = $transformer->(from_file($ixml_input));
42+
my $canonicaliser = make_canonicaliser($ixml_grammar);
43+
my $canonical = $canonicaliser->($ast);
44+
3745
# DESCRIPTION
3846

3947
An implementation of Steven Pemberton's Invisible XML concept, using
@@ -111,6 +119,33 @@ Arguments:
111119

112120
- an AST from [XML::Invisible::Receiver](https://metacpan.org/pod/XML::Invisible::Receiver)
113121

122+
## make\_canonicaliser
123+
124+
Exportable. Returns a function that when called with an AST as produced
125+
from a document by a ["make\_parser"](#make_parser), returns a canonical version of
126+
the original document, or `undef` if it failed.
127+
128+
Arguments:
129+
130+
- an XML::Invisible grammar
131+
132+
It uses a few heuristics:
133+
134+
- literals that are 0-1 (`?`) or any number (`*`) will be omitted
135+
- literals that are at least one (`+`) will be inserted once
136+
- if an "any" group is given, the first one that matches will be selected
137+
138+
This last one means that if you want a canonical representation that is
139+
not the bare minimum, provide that as a literal first choice (see the
140+
`assign` rule below - while it will accept any or no whitespace, the
141+
"canonical" version is given):
142+
143+
expr: target .assign source
144+
target: +name
145+
assign: ' = ' | (- EQUAL -)
146+
source: -name
147+
name: /( ALPHA (: ALPHA | DIGIT )* )/
148+
114149
# DEBUGGING
115150

116151
To debug, set environment variable `XML_INVISIBLE_DEBUG` to a true value.

lib/XML/Invisible.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Pegex::Grammar;
77
use Pegex::Parser;
88
use XML::Invisible::Receiver;
99

10-
our $VERSION = '0.06';
10+
our $VERSION = '0.07';
1111
our @EXPORT_OK = qw(make_parser ast2xml make_canonicaliser);
1212

1313
use constant DEBUG => $ENV{XML_INVISIBLE_DEBUG};

0 commit comments

Comments
 (0)