8 use Scalar::Util qw(blessed);
10 our $VERSION = '0.23'; # VERSION
13 my ($self, $arg, $opts) = @_;
14 die "Please specify a defined argument to parse()\n" unless defined($arg);
22 } elsif ($r eq 'ARRAY') {
23 $str = join "", @$arg;
24 } elsif ($r eq 'GLOB' || blessed($arg) && $arg->isa('IO::Handle')) {
25 $str = join "", <$arg>;
26 } elsif ($r eq 'CODE') {
28 while (defined(my $chunk = $arg->())) {
31 $str = join "", @chunks;
33 die "Invalid argument, please supply a ".
34 "string|arrayref|coderef|filehandle\n";
36 Org::Document->new(from_string=>$str, time_zone=>$opts->{time_zone});
40 my ($self, $filename, $opts) = @_;
41 $self->parse(scalar read_file($filename), $opts);
45 # ABSTRACT: Parse Org documents
52 Org::Parser - Parse Org documents
62 my $orgp = Org::Parser->new();
65 my $doc = $orgp->parse_file("$ENV{HOME}/todo.org");
68 $doc = $orgp->parse(<<EOF);
69 #+TODO: TODO | DONE CANCELLED
73 SCHEDULED: <2011-03-31 Thu>
77 this will become a link: radio target
78 * TODO heading1b *bold*
90 # walk the document tree
93 return unless $el->isa('Org::Element::Headline');
94 say "heading level ", $el->level, ": ", $el->title->as_string;
97 will print something like:
99 heading level 1: heading1a
100 heading level 2: heading2a
101 heading level 2: heading2b *bold*
102 heading level 1: heading1b
103 heading level 1: heading1c
105 A command-line utility (in a separate distribution: L<App::OrgUtils>) is
106 available for debugging:
108 % dump-org-structure ~/todo.org
110 Setting: "#+TODO: TODO | DONE CANCELLED\n"
111 RadioTarget: "<<<radio target>>>"
117 Headline: l=2 todo=TODO
126 This module parses Org documents. See http://orgmode.org/ for more details on
129 This module uses L<Log::Any> logging framework.
131 This module uses L<Moo> object system.
133 See C<todo.org> in the distribution for the list of already- and not yet
142 Create a new parser instance.
144 =head2 $orgp->parse($str | $arrayref | $coderef | $filehandle, $opts) => $doc
146 Parse document (which can be contained in a scalar $str, an array of lines
147 $arrayref, a subroutine which will be called for chunks until it returns undef,
150 Returns L<Org::Document> object.
152 If 'handler' attribute is specified, will call handler repeatedly during
153 parsing. See the 'handler' attribute for more details.
155 Will die if there are syntax errors in documents.
157 $opts is a hashref and can contain these keys: C<time_zone> (will be passed to
158 Org::Document's constructor).
160 =head2 $orgp->parse_file($filename, $opts) => $doc
162 Just like parse(), but will load document from file instead.
166 =head2 Why? Just as only perl can parse Perl, only org-mode can parse Org anyway!
168 True. I'm only targetting good enough. As long as I can parse/process all my Org
169 notes and todo files, I have no complaints.
171 =head2 It's too slow!
173 Parser is completely regex-based at the moment (I plan to use L<Marpa> someday).
174 Performance is quite lousy but I'm not annoyed enough at the moment to overhaul
183 Steven Haryanto <stevenharyanto@gmail.com>
185 =head1 COPYRIGHT AND LICENSE
187 This software is copyright (c) 2012 by Steven Haryanto.
189 This is free software; you can redistribute it and/or modify it under
190 the same terms as the Perl 5 programming language system itself.