3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
24 # lib/RT/Interface/REST.pm
27 package RT::Interface::REST;
33 use vars qw($VERSION @ISA @EXPORT);
35 $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d"x$#r, @r };
38 @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
41 my $field = '[a-zA-Z][a-zA-Z0-9_-]*';
45 my ($elt, @elts, %elts);
47 foreach $elt (split /,/, $list) {
48 if ($elt =~ /^(\d+)-(\d+)$/) { push @elts, ($1..$2) }
49 else { push @elts, $elt }
53 return sort {$a<=>$b} keys %elts;
56 # Returns a reference to an array of parsed forms.
60 my @lines = split /\n/, $_[0];
61 my ($c, $o, $k, $e) = ("", [], {}, "");
65 my $line = shift @lines;
67 next LINE if $line eq '';
70 # We reached the end of one form. We'll ignore it if it was
71 # empty, and store it otherwise, errors and all.
72 if ($e || $c || @$o) {
73 push @forms, [ $c, $o, $k, $e ];
74 $c = ""; $o = []; $k = {}; $e = "";
78 elsif ($state != -1) {
79 if ($state == 0 && $line =~ /^#/) {
80 # Read an optional block of comments (only) at the start
84 while (@lines && $lines[0] =~ /^#/) {
85 $c .= "\n".shift @lines;
89 elsif ($state <= 1 && $line =~ /^($field):(?:\s+(.*))?$/) {
90 # Read a field: value specification.
94 # Read continuation lines, if any.
95 while (@lines && ($lines[0] eq '' || $lines[0] =~ /^\s+/)) {
96 push @v, shift @lines;
98 pop @v while (@v && $v[-1] eq '');
100 # Strip longest common leading indent from text.
101 my ($ws, $ls) = ("");
102 foreach $ls (map {/^(\s+)/} @v[1..$#v]) {
103 $ws = $ls if (!$ws || length($ls) < length($ws));
107 push(@$o, $f) unless exists $k->{$f};
108 vpush($k, $f, join("\n", @v));
112 elsif ($line !~ /^#/) {
113 # We've found a syntax error, so we'll reconstruct the
114 # form parsed thus far, and add an error marker. (>>)
116 $e = form_compose([[ "", $o, $k, "" ]]);
117 $e.= $line =~ /^>>/ ? "$line\n" : ">> $line\n";
121 # We saw a syntax error earlier, so we'll accumulate the
122 # contents of this form until the end.
126 push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
129 foreach $l (keys %$k) {
130 $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
136 # Returns text representing a set of forms.
141 foreach $form (@$forms) {
142 my ($c, $o, $k, $e) = @$form;
157 my @values = (ref $k->{$key} eq 'ARRAY') ?
161 $sp = " "x(length("$key: "));
162 $sp = " "x4 if length($sp) > 16;
164 foreach $v (@values) {
170 push @lines, "$line\n\n";
173 elsif (@lines && $lines[-1] !~ /\n\n$/) {
176 push @lines, "$key: $v\n\n";
179 length($line)+length($v)-rindex($line, "\n") >= 70)
184 $line = $line ? "$line, $v" : "$key: $v";
188 $line = "$key:" unless @values;
191 if (@lines && $lines[-1] !~ /\n\n$/) {
196 push @lines, "$line\n";
200 $text .= join "", @lines;
208 return join "\n--\n\n", @text;
211 # Add a value to a (possibly multi-valued) hash key.
213 my ($hash, $key, $val) = @_;
214 my @val = ref $val eq 'ARRAY' ? @$val : $val;
216 if (exists $hash->{$key}) {
217 unless (ref $hash->{$key} eq 'ARRAY') {
218 my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
221 push @{ $hash->{$key} }, @val;
224 $hash->{$key} = $val;
228 # "Normalise" a hash key that's known to be multi-valued.
231 my ($line, $word, @words);
233 foreach $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : $val)
235 # XXX: This should become a real parser, à la Text::ParseWords.
238 push @words, split /\s*,\s*/, $line;
248 RT::Interface::REST - helper functions for the REST interface.
252 Only the REST should use this module.