| 1 | package Document::Stembolt; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Document::Stembolt - Read & edit a document with YAML-ish meta-data | ||
| 9 | |||
| 10 | =head1 VERSION | ||
| 11 | |||
| 12 | Version 0.012 | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | our $VERSION = '0.012'; | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $content; | ||
| 21 | $content = Document::Stembolt::Content->read_string(<<_END_); | ||
| 22 | # vim: # | ||
| 23 | --- | ||
| 24 | hello: world | ||
| 25 | --- | ||
| 26 | This is the body | ||
| 27 | _END_ | ||
| 28 | |||
| 29 | $content->preamble "# vim: #\n" | ||
| 30 | $content->header { hello => world } | ||
| 31 | $content->body "This is the body\n" | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This distribution is meant to take the headache out of reading, writing, and editing | ||
| 36 | "interesting" documents. That is, documents with both content and meta-data (via YAML::Tiny) | ||
| 37 | |||
| 38 | More documentation coming soon, check out the code and tests for usage and examples. This is pretty beta, so | ||
| 39 | the interface might change. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use Moose; | ||
| 44 | |||
| 45 | use Document::Stembolt::Content; | ||
| 46 | |||
| 47 | use MooseX::Types::Path::Class qw/Dir File/; | ||
| 48 | |||
| 49 | has content => qw/is ro lazy_build 1 isa Document::Stembolt::Content/, handles => [qw/preamble header body/]; | ||
| 50 | sub _build_content { | ||
| 51 | my $self = shift; | ||
| 52 | return Document::Stembolt::Content->new; | ||
| 53 | } | ||
| 54 | |||
| 55 | has file => qw/is ro coerce 1 required 1/, isa => File; | ||
| 56 | |||
| 57 | sub BUILD { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | $self->read if -e $self->file; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub read { | ||
| 64 | my $self = shift; | ||
| 65 | |||
| 66 | $self->content->read($self->file); | ||
| 67 | } | ||
| 68 | sub write { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | $self->content->write($self->file); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _editor { | ||
| 75 | return [ split m/\s+/, ($ENV{VISUAL} || $ENV{EDITOR}) ]; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _edit_file { | ||
| 79 | my $file = shift; | ||
| 80 | die "Don't know what editor" unless my $editor = _editor; | ||
| 81 | my $rc = system @$editor, $file; | ||
| 82 | unless ($rc == 0) { | ||
| 83 | my ($exit_value, $signal, $core_dump); | ||
| 84 | $exit_value = $? >> 8; | ||
| 85 | $signal = $? & 127; | ||
| 86 | $core_dump = $? & 128; | ||
| 87 | die "Error during edit (@$editor): exit value ($exit_value), signal ($signal), core_dump($core_dump): $!"; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub edit { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | $self->write; | ||
| 95 | |||
| 96 | _edit_file $self->file; | ||
| 97 | |||
| 98 | $self->read; | ||
| 99 | } | ||
| 100 | |||
| 101 | =head1 AUTHOR | ||
| 102 | |||
| 103 | Robert Krimen, C<< <rkrimen at cpan.org> >> | ||
| 104 | |||
| 105 | =head1 BUGS | ||
| 106 | |||
| 107 | Please report any bugs or feature requests to C<bug-document-stembolt at rt.cpan.org>, or through | ||
| 108 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Document-Stembolt>. I will be notified, and then you'll | ||
| 109 | automatically be notified of progress on your bug as I make changes. | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | |||
| 114 | =head1 SUPPORT | ||
| 115 | |||
| 116 | You can find documentation for this module with the perldoc command. | ||
| 117 | |||
| 118 | perldoc Document::Stembolt | ||
| 119 | |||
| 120 | |||
| 121 | You can also look for information at: | ||
| 122 | |||
| 123 | =over 4 | ||
| 124 | |||
| 125 | =item * RT: CPAN's request tracker | ||
| 126 | |||
| 127 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Document-Stembolt> | ||
| 128 | |||
| 129 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 130 | |||
| 131 | L<http://annocpan.org/dist/Document-Stembolt> | ||
| 132 | |||
| 133 | =item * CPAN Ratings | ||
| 134 | |||
| 135 | L<http://cpanratings.perl.org/d/Document-Stembolt> | ||
| 136 | |||
| 137 | =item * Search CPAN | ||
| 138 | |||
| 139 | L<http://search.cpan.org/dist/Document-Stembolt> | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | |||
| 144 | =head1 ACKNOWLEDGEMENTS | ||
| 145 | |||
| 146 | |||
| 147 | =head1 COPYRIGHT & LICENSE | ||
| 148 | |||
| 149 | Copyright 2008 Robert Krimen, all rights reserved. | ||
| 150 | |||
| 151 | This program is free software; you can redistribute it and/or modify it | ||
| 152 | under the same terms as Perl itself. | ||
| 153 | |||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | 1; # End of Document::Stembolt |