| 1 | package Text::vFile::asData; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base qw( Class::Accessor::Chained::Fast ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | __PACKAGE__->mk_accessors(qw( preserve_params )); | |||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | Text::vFile::asData - parse vFile formatted files into data structures | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | use Text::vFile::asData; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | open my $fh, "foo.ics" | ||||||||||||||||||||||||||||||||||||||||||
| 17 | or die "couldn't open ics: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | my $data = Text::vFile::asData->new->parse( $fh ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Text::vFile::asData reads vFile format files, such as vCard (RFC 2426) and | ||||||||||||||||||||||||||||||||||||||||||
| 23 | vCalendar (RFC 2445). | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 116 | ▶ | sub _unwrap_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unwrap_lines: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 116 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 116 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 116 | for (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 31 | 2017 | my $line = $_; # $_ may be readonly | |||||||||||||||||||||||||||||||||||||||||
| 32 | 2017 | $line =~ s{[\r\n]+$}{}; # lines SHOULD end CRLF | |||||||||||||||||||||||||||||||||||||||||
| 33 | 2017 | ▶ | if ($line =~ /^[ \t](.*)/) { # Continuation line (RFC Sect. 4.1) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 717 | ▶ | die "Continuation line, but no preceding line" unless @lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 717 | $lines[-1] .= $1; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 717 | next; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 1300 | push @lines, $line; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 116 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 50 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 50 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 50 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 50 | return $self->parse_lines( <$fh> ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # like Text::ParseWords' parse_line, only C-style so the regex engine doesn't | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # blow its stack, and it's also got a $limit like split | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | # this only took a trainride, so I'm pretty sure there are lurking | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # corner cases - when I get a tuit I'll take the Text::ParseWords | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # tests and run them through it | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1938 | ▶ | sub parse_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_line: called CC=13 SCAR=27.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1938 | my ($delim, $keep, $text, $limit) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1938 | my ($current, @parts); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1938 | my ($quote, $escaped); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 1938 | ▶ | while (length $text) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 21417 | ▶ | if ($text =~ s{^(\\)}{}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $current .= $1 if $escaped || $keep; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$escaped or $keep
MC/DC0%0/2
$escaped or $keep
$escaped
$keep
Truth table0%0/3
A or B
A= $escapedB= $keep
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $escaped = !$escaped; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 21417 | ▶ | if (!$quote && !$escaped && $text =~ s{^$delim}{}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $quote || $escaped and $text =~ s/^$delim//
$quote || $escaped
MC/DC66%2/3
not $quote || $escaped and $text =~ s/^$delim//
$quote
$escaped
$text =~ s/^$delim//
Truth table75%3/4
not(A || B) and C
A= $quoteB= $escapedC= $text =~ s/^$delim//
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1360 | push @parts, $current; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1360 | $current = undef; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 1360 | ▶ | if (defined $limit && @parts == $limit -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $limit and @parts == $limit - 1
MC/DC50%1/2
defined $limit and @parts == $limit - 1
defined $limit
@parts == $limit - 1
Truth table66%2/3
A and B
A= defined $limitB= @parts == $limit - 1
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 963 | return @parts, $text; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # pull the character off to take a looksee | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 20057 | $text =~ s{(.)}{}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 20057 | my $char = $1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 20057 | ▶ | if ($char eq '"' && !$escaped) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$char eq "\"" and not $escaped
MC/DC50%1/2
$char eq "\"" and not $escaped
$char eq "\""
not $escaped
Truth table66%2/3
A and not(B)
A= $char eq "\""B= not $escaped
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | # either it's defined and matches, in which case we | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # clear the quote variable, or it's undefined which | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # makes this quote an opening quote | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 28 | $quote = !$quote; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 28 | ▶ | $current .= $char if $keep; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 20029 | $current .= $char; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 20454 | $escaped = 0; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 975 | return @parts, $current; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 115 | ▶ | sub parse_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_lines: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 115 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 115 | my @path; | |||||||||||||||||||||||||||||||||||||||||
| 99 | my $current; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 115 | for ($self->_unwrap_lines( @_ )) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | # Ignore leading or trailing blank lines at the top/bottom of the | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # input. Not sure about completely blank lines within the input | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 1299 | ▶ | next if scalar @path == 0 and $_ =~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @path == 0 and $_ =~ /^\s*$/
MC/DC100%2/2
scalar @path == 0 and $_ =~ /^\s*$/
scalar @path == 0
$_ =~ /^\s*$/
Truth table100%3/3
A and B
A= scalar @path == 0B= $_ =~ /^\s*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 1294 | ▶ | if (/^BEGIN:(.*)/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 165 | push @path, $current; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 165 | $current = { type => $1 }; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 165 | push @{ $path[-1]{objects} }, $current; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 165 | next; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1129 | ▶ | if (/^END:(.*)/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | die "END $1 in $current->{type}" | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 164 | ▶ | unless lc $current->{type} eq lc $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 163 | $current = pop @path; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 163 | next; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # we'd use Text::ParseWords here, but it likes to segfault. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 965 | my ($name, $value) = parse_line( ':', 1, $_, 2); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 965 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 965 | my @params = parse_line( ';', 0, $name ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 965 | $name = shift @params; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 965 | $value = { value => $value }; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 965 | foreach my $param (@params) { | |||||||||||||||||||||||||||||||||||||||||
| 127 | 389 | my ($p_name, $p_value) = split /=/, $param; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 389 | ▶ | push @{ $value->{params} }, { $p_name => $p_value } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | if $self->preserve_params; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 389 | $value->{param}{ $p_name } = $p_value; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 965 | push @{ $current->{properties}{ $name } }, $value; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # something did a BEGIN but no END - TODO, unwind this nicely as | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # it may be more than one level | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 114 | ▶ | die "BEGIN $current->{type} without matching END" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | if @path; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 113 | return $current; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # this might not strictly comply | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 126 | ▶ | sub generate_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_lines: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 126 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 126 | my $this = shift; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 126 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 149 | # XXX all the existence checks are to prevent auto-vivification | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # breaking if_diff tests - do we mind, or should the fields have been | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # there anyway? | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 126 | ▶ | push @lines, "BEGIN:$this->{type}" if exists $this->{type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 126 | ▶ | if (exists $this->{properties}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 78 | ▶ | while (my ($name, $v) = each %{ $this->{properties} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 406 | for my $value (@$v) { | |||||||||||||||||||||||||||||||||||||||||
| 157 | # XXX so we're taking params in preference to param, | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # let's be sure to document that when we document this | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # method | ||||||||||||||||||||||||||||||||||||||||||
| 160 | my $param = join ';', '', map { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 498 | my $hash = $_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 498 | ▶ | "$_" . (defined $hash->{$_} ? "=" . $hash->{$_} : "") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } keys %$hash | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 468 | ▶ | } @{ $value->{params} || [ $value->{param} ] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$value->{'params'} || [$value->{'param'}]
MC/DC100%1/1
$value->{'params'} || [$value->{'param'}]
$value->{'params'}
Truth table100%2/2
A
A= $value->{'params'}
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 468 | my $line = "$name$param:$value->{value}"; | |||||||||||||||||||||||||||||||||||||||||
| 167 | # wrapping, but done ugly | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 468 | my @chunks = $line =~ m/(.{1,72})/g; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 468 | push @lines, shift @chunks; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 468 | push @lines, map { " $_" } @chunks; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 126 | ▶ | if (exists $this->{objects}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 58 | push @lines, $self->generate_lines( $_ ) for @{ $this->{objects} } | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 126 | ▶ | push @lines, "END:$this->{type}" if exists $this->{type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 126 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||||||||||||||||