NAME

Org::Parser - Parse Org documents

VERSION

This document describes version 0.561 of Org::Parser (from Perl distribution Org-Parser), released on 2023-11-06.

SYNOPSIS

 use 5.010;
 use Org::Parser;
 my $orgp = Org::Parser->new();

 # parse a file
 my $doc = $orgp->parse_file("$ENV{HOME}/todo.org");

 # parse a string
 $doc = $orgp->parse(<<EOF);
 #+TODO: TODO | DONE CANCELLED
 <<<radio target>>>
 * heading1a
 ** TODO heading2a
 SCHEDULED: <2011-03-31 Thu>
 [[some][link]]
 ** DONE heading2b
 [2011-03-18 ]
 this will become a link: radio target
 * TODO heading1b *bold*
 - some
 - plain
 - list
 - [ ] with /checkbox/
   * and
   * sublist
 * CANCELLED heading1c
 + definition :: list
 + another :: def
 EOF

 # walk the document tree
 $doc->walk(sub {
     my ($el) = @_;
     return unless $el->isa('Org::Element::Headline');
     say "heading level ", $el->level, ": ", $el->title->as_string;
 });

will print something like:

 heading level 1: heading1a
 heading level 2: heading2a
 heading level 2: heading2b *bold*
 heading level 1: heading1b
 heading level 1: heading1c

A command-line utility (in a separate distribution: App::OrgUtils) is available for debugging:

 % dump-org-structure ~/todo.org
 Document:
   Setting: "#+TODO: TODO | DONE CANCELLED\n"
   RadioTarget: "<<<radio target>>>"
   Text: "\n"
   Headline: l=1
     (title)
     Text: "heading1a"
     (children)
     Headline: l=2 todo=TODO
       (title)
       Text: "heading2a"
       (children)
       Text: "SCHEDULED: "
 ...

DESCRIPTION

This module parses Org documents. See http://orgmode.org/ for more details on Org documents.

See todo.org in the distribution for the list of already- and not yet implemented stuffs.

ATTRIBUTES

METHODS

new()

Create a new parser instance.

$orgp->parse($str | $arrayref | $coderef | $filehandle, \%opts) => $doc

Parse document (which can be contained in a scalar $str, an arrayref of lines $arrayref, a subroutine which will be called for chunks until it returns undef, or a filehandle).

Returns Org::Document object.

If 'handler' attribute is specified, will call handler repeatedly during parsing. See the 'handler' attribute for more details.

Will die if there are syntax errors in documents.

Known options:

  • time_zone => STR

    Will be passed to Org::Document's constructor.

$orgp->parse_file($filename, \%opts) => $doc

Just like parse(), but will load document from file instead.

Known options (aside from those known by parse()):

  • cache => bool (default: from PERL_ORG_PARSER_CACHE, or 0)

    Since Org::Parser can spend some time to parse largish Org files, this is an option to store the parse result (using Storable). If caching is turned on, then after the first parse, the result will be stored in:

     ~/.cache/perl-org-parser/<filename>.<md5-digest-of-file-absolute-path>.storable

    and subsequent calls to this function can directly use this cache, as long as the cache is not stale.

FAQ

Why? Just as only perl can parse Perl, only org-mode can parse Org anyway!

True. I'm only targetting good enough. As long as I can parse/process all my Org notes and todo files, I have no complaints.

It's too slow!

Parser is completely regex-based at the moment (I plan to use Marpa someday). Performance is quite lousy but I'm not annoyed enough at the moment to overhaul it.

ENVIRONMENT

PERL_ORG_PARSER_CACHE => bool

Set default for cache option in parse_file().

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Org-Parser.

SOURCE

Source repository is at https://github.com/perlancar/perl-Org-Parser.

SEE ALSO

Org::Document

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTORS

  • Alex White <VVu@geekfarm.org>

  • Karl Williamson <khw@cpan.org>

  • Steven Haryanto <stevenharyanto@gmail.com>

  • Tekki <tekki@tekki.ch>

  • Trent Fisher <trent@cs.pdx.edu>

  • William Lindley <wlindley@wlindley.com>

  • Wong Meng Weng <mengwong@pobox.com>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.