#!/usr/bin/perl

# a simple example script to dump org structure

# workaround for dzil
package dump_org_structure;
BEGIN {
  $dump_org_structure::VERSION = '0.03';
}

use 5.010;
use strict;
use warnings;
use Log::Any qw($log);

use String::Escape qw(elide printable);
use Org::Parser;

my $doc = Org::Parser->new->parse([<>]);
dump_element($doc);

sub dump_element {
    my ($el, $indent_level) = @_;
    $indent_level //= 0;

    my $line = "  " x $indent_level;
    my $type = ref($el);
    $type =~ s/^Org::(?:Element::)?//;
    $line .= "$type:";
    # per-element important info
    if ($type eq 'Headline') {
        $line .= " l=".$el->level;
        $line .= ",todo=".$el->todo_state if $el->todo_state;
    } elsif ($type eq 'Text') {
        $line .= " ".$el->style if $el->style;
    } elsif ($type =~ /^(Schedule)?Timestamp$/) {
        $line .= " dt=".$el->datetime;
    }
    $line .= " \"".
        printable elide($el->_raw // $el->as_string, 80-length($line)-2);
    say $line, "\"";

    if ($el->children) {
        dump_element($_, $indent_level+1) for @{ $el->children };
    }
}

__END__
=pod

=head1 NAME

dump_org_structure

=head1 VERSION

version 0.03

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Steven Haryanto.

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

=cut

