use
version;
our
$VERSION
= qv(
'v0.4.7'
);
sub
new
{
my
$class
=
shift
;
my
$arg
=
shift
;
my
$self
= {};
$self
->{objects} = [];
bless
(
$self
,
$class
);
return
$self
;
}
sub
fetch_line {
my
(
$self
,
$pos
) =
@_
;
my
$noobjs
= $
my
$position
= (0 >
$pos
) ?
$noobjs
: (
$pos
>
$noobjs
) ? -1
:
$pos
;
if
(-1 ==
$position
) {
return
;
}
return
$self
->{objects}->[
$position
]->{value};
}
sub
delete_line {
my
(
$self
,
$pos
) =
@_
;
my
$objects
= [];
my
$noobjs
= $
my
$position
= (0 >
$pos
) ?
$noobjs
: (
$pos
>
$noobjs
) ? -1
:
$pos
;
if
(-1 ==
$position
) {
return
;
}
while
(
$position
) {
push
@$objects
,
shift
@{
$self
->{objects}};
$position
--;
}
shift
@{
$self
->{objects}};
push
@$objects
, @{
$self
->{objects}};
$self
->{objects} =
$objects
;
}
sub
insert_line {
my
(
$self
,
$pos
,
$line
) =
@_
;
my
$objects
= [];
my
$noobjs
= 1 + $
my
$position
= (0 >
$pos
) ?
$noobjs
: (
$pos
>
$noobjs
) ?
$noobjs
:
$pos
;
while
(
$position
) {
push
@$objects
,
shift
@{
$self
->{objects}};
$position
--;
}
push
@$objects
, {
value
=>
$line
}, @{
$self
->{objects}};
$self
->{objects} =
$objects
;
}
sub
read_file {
my
(
$self
,
$filename
,
$filter
) =
@_
;
if
(
open
my
$input
,
'<'
,
$filename
) {
$self
->{objects} = [];
while
(<
$input
>) {
if
(/^(\t*)(.*)$/) {
$self
->_add_something($1, {
children
=> [],
value
=> $2
},
$filter
);
}
else
{
die
"unknown line: $_"
;
}
}
close
$input
;
return
1 + $
}
return
;
}
sub
read_file_no_checked_boxes {
my
(
$self
,
$filename
) =
@_
;
my
$in_checked_box
= 0;
my
$cbl
= 0;
my
$filter
=
sub
{
my
(
$object
,
$indent
) =
@_
;
if
(
$in_checked_box
&&
$indent
>
$cbl
) {
return
0;
}
elsif
(_checked_box(
$object
)) {
$in_checked_box
= 1;
$cbl
=
$indent
;
return
0;
}
else
{
$in_checked_box
= 0;
return
1;
}
};
$self
->read_file(
$filename
,
$filter
);
}
sub
read_file_unchecked_boxes {
my
(
$self
,
$filename
) =
@_
;
my
$unchecked_box
= 0;
my
$cbl
= 0;
my
$filter
=
sub
{
my
(
$object
,
$indent
) =
@_
;
if
(
$unchecked_box
&&
$indent
>
$cbl
) {
return
1;
}
elsif
(_unchecked_box(
$object
)) {
$unchecked_box
= 1;
$cbl
=
$indent
;
return
1;
}
else
{
$unchecked_box
= 0;
return
0;
}
};
$self
->read_file(
$filename
,
$filter
);
}
sub
write_file {
my
(
$self
,
$filename
,
$filter
) =
@_
;
if
(
open
my
$output
,
'>'
,
$filename
) {
foreach
my
$object
(@{
$self
->{objects}}) {
_write_object(
$object
,0,
$output
,
$filter
);
}
close
$output
;
}
}
sub
write_file_no_checked_boxes {
my
(
$self
,
$filename
) =
@_
;
my
$filter
=
sub
{
my
(
$object
) =
@_
;
return
! _checked_box(
$object
);
};
$self
->write_file(
$filename
,
$filter
);
}
sub
write_file_unchecked_boxes {
my
(
$self
,
$filename
) =
@_
;
my
$filter
=
sub
{
my
(
$object
,
$indent
) =
@_
;
return
$indent
? 1 : _unchecked_box(
$object
);
};
$self
->write_file(
$filename
,
$filter
);
}
sub
_add_something {
my
(
$self
,
$tabs
,
$newobject
,
$filter
) =
@_
;
my
$indent
=
length
$tabs
;
if
(
defined
$filter
) {
return
unless
(
$filter
->(
$newobject
,
$indent
));
}
my
$objects
=
$self
->_descend_objects(
$indent
);
push
@$objects
,
$newobject
;
}
sub
_checked_box {
my
(
$object
) =
@_
;
return
(
$object
->{value} =~ /^\[X\]/);
}
sub
_descend_objects {
my
(
$self
,
$indent
) =
@_
;
my
$objects
=
$self
->{objects};
while
(0 <
$indent
) {
if
(0 >
$#$objects
) {
my
$newobject
= {
children
=> [],
};
push
@$objects
,
$newobject
;
$objects
=
$newobject
->{children};
}
else
{
$objects
=
$objects
->[
$#$objects
]->{children};
}
$indent
--;
}
return
$objects
;
}
sub
_unchecked_box {
my
(
$object
) =
@_
;
return
(
$object
->{value} =~ /^\[_\]/);
}
sub
_write_object {
my
(
$object
,
$indent
,
$outfh
,
$filter
) =
@_
;
if
(
defined
$filter
) {
return
unless
(
$filter
->(
$object
,
$indent
));
}
print
$outfh
"\t"
x
$indent
,
$object
->{value},
"\n"
;
foreach
my
$co
(@{
$object
->{children}}) {
_write_object(
$co
,
$indent
+ 1,
$outfh
,
$filter
);
}
}
1;
=head1 FORMAT OF VIMOUTLINER FILES
Vimoutliner files are text files
with
a hierarchical structure.
The hierarchical structure is characterized by the number of tabulator
signs (0x09) at the beginning of the line.
A line can be a simple-heading or an object, depending on the first
nontabulator sign of the line.
A simple heading starts
with
any non-whitespace character except
C<< : ; | < > >>.
A checkbox is a special form of a heading that starts
with
either
C<< [_] >> or C<< [X] >>
after
the leading tabulator signs.
A checkbox may contain a percent sign (C<%>) as a placeholder
for
the percentage completed.
This percent sign must follow the initial C<< [_] >>
after
a separating
whitespace.
The following text objects are
defined
for
vimoutliner files:
=over 4
=item C<:> - body text
The text following the C<:> will be wrapped automatically.
=item C<;> - preformatted body text
This text won't be wrapped automatically.
=item C<|> - table
The table headings can be marked
with
C<||>.
=item C<< > >> - user
defined
text.
This text will also be wrapped automatically.
=item C<< < >> - user
defined
preformatted text.
This text won't be wrapped automatically.
=back
=head1 AUTHOR
Mathias Weidner, C<< <mamawe at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-app-vojournal at rt.cpan.org>, or through
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation
for
this module
with
the perldoc command.
perldoc App::VOJournal::VOTL
You can also look
for
information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
=item * AnnoCPAN: Annotated CPAN documentation
=item * CPAN Ratings
=item * Search CPAN
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2015 Mathias Weidner.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
Any
use
, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you
accept
this license. Do not
use
, modify,
or distribute the Package,
if
you
do
not
accept
this license.
If your Modified Version
has
been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies
with
the requirements of this license.
This license does not grant you the right to
use
any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made,
use
, offer to sell, sell,
import
and
otherwise transfer the Package
with
respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.