| 1 | package Document::Stembolt::Content; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | |||
| 5 | use IO::Scalar; | ||
| 6 | use IO::AtomicFile; | ||
| 7 | use Carp; | ||
| 8 | use Path::Class(); | ||
| 9 | use YAML::Tiny(); | ||
| 10 | |||
| 11 | has separator => qw/is rw required 1 isa Str/, default => '---'; | ||
| 12 | |||
| 13 | has _preamble_content => qw/is rw isa Maybe[ScalarRef]/; | ||
| 14 | sub preamble_as_string { | ||
| 15 | my $self = shift; | ||
| 16 | return undef unless $self->_preamble_content; | ||
| 17 | return ${ $self->_preamble_content }; | ||
| 18 | } | ||
| 19 | |||
| 20 | has _header_content => qw/is rw isa Maybe[ScalarRef]/; | ||
| 21 | sub header_as_string { | ||
| 22 | my $self = shift; | ||
| 23 | return undef unless $self->_header_content; | ||
| 24 | return ${ $self->_header_content }; | ||
| 25 | } | ||
| 26 | |||
| 27 | has _body_content => qw/is rw isa Maybe[ScalarRef]/; | ||
| 28 | sub body_as_string { | ||
| 29 | my $self = shift; | ||
| 30 | return undef unless $self->_body_content; | ||
| 31 | return ${ $self->_body_content }; | ||
| 32 | } | ||
| 33 | |||
| 34 | has _header => qw/is ro lazy_build 1 isa Maybe[HashRef]/; | ||
| 35 | sub _build__header { | ||
| 36 | my $self = shift; | ||
| 37 | return {} unless defined $self->_header_content; | ||
| 38 | return $self->_parse_header($self->_header_content); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub preamble { | ||
| 42 | my $self = shift; | ||
| 43 | if (@_) { | ||
| 44 | my $value = shift; | ||
| 45 | if (defined $value) { | ||
| 46 | $value = $$value if ref $value eq "SCALAR"; | ||
| 47 | $value = \"$value"; | ||
| 48 | } | ||
| 49 | $self->_preamble_content($value); | ||
| 50 | } | ||
| 51 | return undef unless defined $self->_preamble_content; | ||
| 52 | return ${ $self->_preamble_content }; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub header { | ||
| 56 | my $self = shift; | ||
| 57 | if (@_) { | ||
| 58 | my $value = shift; | ||
| 59 | if (defined $value) { | ||
| 60 | $value = $$value if ref $value eq "SCALAR"; | ||
| 61 | $value = $self->_format_header($value) if ref $value eq "HASH"; | ||
| 62 | $value = \"$value"; | ||
| 63 | } | ||
| 64 | $self->_header_content($value); | ||
| 65 | $self->_clear_header; | ||
| 66 | } | ||
| 67 | return $self->_header; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub body { | ||
| 71 | my $self = shift; | ||
| 72 | if (@_) { | ||
| 73 | my $value; | ||
| 74 | $value = "" unless defined $value; | ||
| 75 | $value = $$value if ref $value eq "SCALAR"; | ||
| 76 | $value = \"$value"; | ||
| 77 | $self->_body_content($value); | ||
| 78 | } | ||
| 79 | return "" unless defined $self->_body_content; | ||
| 80 | return ${ $self->_body_content }; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub read_string { | ||
| 84 | return shift->read(\shift()); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub read { | ||
| 88 | my $self = shift; | ||
| 89 | return $self->new->read(@_) unless blessed $self; | ||
| 90 | my $read = shift; | ||
| 91 | |||
| 92 | croak "Wasn't given something to read" unless defined $read; | ||
| 93 | |||
| 94 | if (ref $read eq "SCALAR") { | ||
| 95 | $read = IO::Scalar->new($read); | ||
| 96 | } | ||
| 97 | elsif (UNIVERSAL::isa($read => 'IO::Handle')) { | ||
| 98 | } | ||
| 99 | elsif (ref $read eq "GLOB") { | ||
| 100 | my $io = IO::Handle->new; | ||
| 101 | $io->fdopen( fileno($read), "r" ); | ||
| 102 | $read = $io; | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | $read = Path::Class::file("$read")->openr; | ||
| 106 | } | ||
| 107 | |||
| 108 | # croak "Don't know how to read $read" unless UNIVERSAL::isa($read => 'IO::Handle'); | ||
| 109 | |||
| 110 | my @part; | ||
| 111 | while (1) { | ||
| 112 | my ($more, $content); | ||
| 113 | if (2 > @part) { | ||
| 114 | ($more, $content) = $self->_read_until_separator($read, qr/^-{3}\s*$/); | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $content = $self->_read($read); | ||
| 118 | } | ||
| 119 | |||
| 120 | push @part, $content; | ||
| 121 | last unless $more; | ||
| 122 | } | ||
| 123 | |||
| 124 | my $body = pop @part; | ||
| 125 | my $header = pop @part; | ||
| 126 | my $preamble = pop @part; | ||
| 127 | |||
| 128 | $self->_clear_header; | ||
| 129 | |||
| 130 | $self->{_body_content} = $body; | ||
| 131 | $self->{_header_content} = $header; | ||
| 132 | $self->{_preamble_content} = $preamble; | ||
| 133 | |||
| 134 | return $self; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub write { | ||
| 138 | my $self = shift; | ||
| 139 | my $file = shift; | ||
| 140 | |||
| 141 | croak "Wasn't given file to write to" unless defined $file && length $file; | ||
| 142 | |||
| 143 | $file = Path::Class::file($file); | ||
| 144 | $file->parent->mkpath unless -d $file->parent; | ||
| 145 | |||
| 146 | my $handle = IO::AtomicFile->open("$file", 'w') or croak "Unable to write to $file since: $!"; | ||
| 147 | $handle->print($self->write_string); | ||
| 148 | $handle->close or die "Couldn't atomically write $file since: $!"; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub write_string { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | my @part = map { chomp; $_ } grep { defined } | ||
| 155 | $self->preamble, | ||
| 156 | $self->_format_header($self->_header), | ||
| 157 | ; | ||
| 158 | |||
| 159 | my $separator = $self->separator; | ||
| 160 | |||
| 161 | return join "\n$separator\n", @part, $self->body; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _read_until_separator { | ||
| 165 | my $self = shift; | ||
| 166 | my $handle = shift; | ||
| 167 | my $separator = shift; | ||
| 168 | |||
| 169 | my $content; | ||
| 170 | $separator = $self->separator; | ||
| 171 | my $match = qr/^$separator\s*$/; | ||
| 172 | my $got_separator; | ||
| 173 | while (<$handle>) { | ||
| 174 | last if $got_separator = $_ =~ $match; | ||
| 175 | $content .= $_; | ||
| 176 | } | ||
| 177 | return ($got_separator => \$content); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _read { | ||
| 181 | my $self = shift; | ||
| 182 | my $handle = shift; | ||
| 183 | |||
| 184 | local $/ = undef; | ||
| 185 | my $content; | ||
| 186 | $content = <$handle>; | ||
| 187 | return \$content; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _parse_header { | ||
| 191 | my $self = shift; | ||
| 192 | my $content = shift; | ||
| 193 | |||
| 194 | return YAML::Tiny->read_string($$content)->[0]; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _format_header { | ||
| 198 | my $self = shift; | ||
| 199 | my $header = shift; | ||
| 200 | |||
| 201 | return undef unless defined $header; | ||
| 202 | |||
| 203 | my $string = YAML::Tiny::Dump($header); | ||
| 204 | $string =~ s/^---\s*//; | ||
| 205 | return $string; | ||
| 206 | } | ||
| 207 | |||
| 208 | 1; |