]> git.donarmstrong.com Git - liborg-parser-perl.git/blob - lib/Org/Dump.pm
Import original source of Org-Parser 0.23
[liborg-parser-perl.git] / lib / Org / Dump.pm
1 package Org::Dump;
2
3 use 5.010;
4 use strict;
5 use warnings;
6 use Log::Any qw($log);
7
8 use String::Escape qw(elide printable);
9
10 our $VERSION = '0.23'; # VERSION
11
12 sub _dump_ts {
13     my ($self, $ts) = @_;
14     my $dump = "";
15     $dump .= "A " if $ts->is_active;
16     my $dt = $ts->datetime;
17     my $tz = $dt->time_zone;
18     $dump .= $dt.
19         ($tz->is_floating ? "F" : $tz->short_name_for_datetime($dt));
20     $dump;
21 }
22
23 sub dump_element {
24     my ($el, $indent_level) = @_;
25     __PACKAGE__->new->_dump($el, $indent_level);
26 }
27
28 sub new {
29     my ($class) = @_;
30     bless {}, $class;
31 }
32
33 sub _dump {
34     my ($self, $el, $indent_level) = @_;
35     $indent_level //= 0;
36     my @res;
37
38     my $line = "  " x $indent_level;
39     my $type = ref($el);
40     $type =~ s/^Org::(?:Element::)?//;
41     $line .= "$type:";
42     # per-element important info
43     if ($type eq 'Headline') {
44         $line .= " l=".$el->level;
45         $line .= " tags ".join(",", @{$el->tags}) if $el->tags;
46         $line .= " todo=".$el->todo_state if $el->todo_state;
47     } elsif ($type eq 'Footnote') {
48         $line .= " name=".($el->name // "");
49     } elsif ($type eq 'Block') {
50         $line .= " name=".($el->name // "");
51     } elsif ($type eq 'List') {
52         $line .= " ".$el->type;
53         $line .= "(".$el->bullet_style.")";
54         $line .= " indent=".length($el->indent);
55     } elsif ($type eq 'ListItem') {
56         $line .= " ".$el->bullet;
57         $line .= " [".$el->check_state."]" if $el->check_state;
58     } elsif ($type eq 'Text') {
59         #$line .= " mu_start" if $el->{_mu_start}; #TMP
60         #$line .= " mu_end" if $el->{_mu_end}; #TMP
61         $line .= " ".$el->style if $el->style;
62     } elsif ($type eq 'Timestamp') {
63         $line .= " ".$self->_dump_ts($el);
64     } elsif ($type eq 'TimeRange') {
65     } elsif ($type eq 'Drawer') {
66         $line .= " ".$el->name;
67         $line .= " "._format_properties($el->properties)
68             if $el->name eq 'PROPERTIES' && $el->properties;
69     }
70     unless ($el->children) {
71         $line .= " \"".
72             printable(elide(($el->_str // $el->as_string), 50))."\"";
73     }
74     push @res, $line, "\n";
75
76     if ($type eq 'Headline') {
77         push @res, "  " x ($indent_level+1), "(title)\n";
78         push @res, $self->_dump($el->title, $indent_level+1);
79         push @res, "  " x ($indent_level+1), "(children)\n" if $el->children;
80     } elsif ($type eq 'Footnote') {
81         if ($el->def) {
82             push @res, "  " x ($indent_level+1), "(definition)\n";
83             push @res, $self->_dump($el->def, $indent_level+1);
84         }
85         push @res, "  " x ($indent_level+1), "(children)\n" if $el->children;
86     } elsif ($type eq 'ListItem') {
87         if ($el->desc_term) {
88             push @res, "  " x ($indent_level+1), "(description term)\n";
89             push @res, $self->_dump($el->desc_term, $indent_level+1);
90         }
91         push @res, "  " x ($indent_level+1), "(children)\n" if $el->children;
92     }
93
94     if ($el->children) {
95         push @res, $self->_dump($_, $indent_level+1) for @{ $el->children };
96     }
97
98     join "", @res;
99 }
100
101 sub _format_properties {
102     my ($props) = @_;
103     #use Data::Dump::OneLine qw(dump1); return dump1($props);
104     my @s;
105     for my $k (sort keys %$props) {
106         my $v = $props->{$k};
107         if (ref($v) eq 'ARRAY') {
108             $v = "[" . join(",", map {printable($_)} @$v). "]";
109         } else {
110             $v = printable($v);
111         }
112         push @s, "$k=$v";
113     }
114     "{" . join(", ", @s) . "}";
115 }
116
117 1;
118 #ABSTRACT: Show Org document/element object in a human-friendly format
119
120
121
122 __END__
123 =pod
124
125 =head1 NAME
126
127 Org::Dump - Show Org document/element object in a human-friendly format
128
129 =head1 VERSION
130
131 version 0.23
132
133 =head1 FUNCTIONS
134
135 None are exported.
136
137 =for Pod::Coverage new
138
139 =head2 dump_element($elem) => STR
140
141 =head1 AUTHOR
142
143 Steven Haryanto <stevenharyanto@gmail.com>
144
145 =head1 COPYRIGHT AND LICENSE
146
147 This software is copyright (c) 2012 by Steven Haryanto.
148
149 This is free software; you can redistribute it and/or modify it under
150 the same terms as the Perl 5 programming language system itself.
151
152 =cut
153