File Coverage

blib/lib/Pod/AsciiDoctor.pm
Criterion Covered Total %
statement 65 101 64.3
branch 21 40 52.5
condition 4 7 57.1
subroutine 13 17 76.4
pod 10 10 100.0
total 113 175 64.5


line stmt bran cond sub pod time code
1             package Pod::AsciiDoctor;
2             $Pod::AsciiDoctor::VERSION = '0.102000';
3 2     2   126877 use 5.014;
  2         28  
4 2     2   12 use strict;
  2         3  
  2         45  
5 2     2   10 use warnings FATAL => 'all';
  2         5  
  2         71  
6              
7 2     2   27 use Pod::Parser 1.65 ();
  2         40  
  2         43  
8 2     2   840 use parent 'Pod::Parser';
  2         552  
  2         10  
9              
10              
11             sub initialize
12             {
13 1     1 1 108 my $self = shift;
14 1         8 $self->SUPER::initialize(@_);
15 1         3 $self->_prop;
16 1         4 return $self;
17             }
18              
19              
20             sub adoc
21             {
22 4     4 1 14 my $self = shift;
23 4         10 my $data = $self->_prop;
24 4         7 return join "\n", @{ $data->{text} };
  4         48  
25             }
26              
27              
28             sub _prop
29             {
30 14     14   24 my $self = shift;
31             return $self->{prop} //= {
32 14   100     54 'text' => [],
33             'headers' => "",
34             'topheaders' => {},
35             'command' => '',
36             'indent' => 0
37             };
38             }
39              
40              
41             sub _sanitise
42             {
43 1     1   3 my $self = shift;
44 1         2 my $p = shift;
45 1         3 chomp($p);
46 1         3 return $p;
47             }
48              
49              
50             sub append
51             {
52 8     8 1 22 my ( $self, $doc ) = @_;
53 8         18 my $data = $self->_prop;
54 8         11 push @{ $data->{text} }, $doc;
  8         449  
55             }
56              
57              
58             sub command
59             {
60 1     1 1 268 my ( $self, $command, $paragraph, $lineno ) = @_;
61 1         4 my $data = $self->_prop;
62 1         3 $data->{command} = $command;
63              
64             # _sanitise: Escape AsciiDoctor syntax chars that appear in the paragraph.
65 1         6 $paragraph = $self->_sanitise($paragraph);
66              
67 1 50       6 if ( my ($input_level) = $command =~ /head([0-9])/ )
68             {
69 0         0 my $level = $input_level;
70 0   0     0 $level //= 2;
71 0         0 $data->{command} = 'head';
72             $data->{topheaders}{$input_level} =
73             defined( $data->{topheaders}{$input_level} )
74 0 0       0 ? $data->{topheaders}{$input_level}++
75             : 1;
76 0         0 $paragraph = $self->set_formatting($paragraph);
77 0         0 $self->append( $self->make_header( $command, $level, $paragraph ) );
78             }
79              
80 1 50       4 if ( $command =~ /over/ )
81             {
82 0         0 $data->{indent}++;
83             }
84 1 50       3 if ( $command =~ /back/ )
85             {
86 0         0 $data->{indent}--;
87             }
88 1 50       2 if ( $command =~ /item/ )
89             {
90 0         0 $self->append( $self->make_text( $paragraph, 1 ) );
91             }
92 1         70 return;
93             }
94              
95              
96             sub verbatim
97             {
98 0     0 1 0 my $self = shift;
99 0         0 my $paragraph = shift;
100 0         0 chomp($paragraph);
101 0         0 $self->append($paragraph);
102 0         0 return;
103             }
104              
105              
106             sub textblock
107             {
108 8     8 1 22 my $self = shift;
109 8         30 my ( $paragraph, $lineno ) = @_;
110 8         23 chomp($paragraph);
111 8         687 $paragraph = $self->interpolate($paragraph);
112 8         27 $self->append($paragraph);
113             }
114              
115              
116             sub interior_sequence
117             {
118 29     29 1 92 my ( $parser, $seq_command, $seq_argument ) = @_;
119             ## Expand an interior sequence; sample actions might be:
120 29 100       138 return "*$seq_argument*" if ( $seq_command eq 'B' );
121 28 100       1084 return "`$seq_argument`" if ( $seq_command eq 'C' );
122 10 100 66     217 return "_${seq_argument}_'"
123             if ( $seq_command eq 'I' || $seq_command eq 'F' );
124 8 100       291 if ( $seq_command eq 'L' )
125             {
126 2         4 my $ret = "";
127 2         3 my $text;
128             my $link;
129 2 100       13 if ( $seq_argument =~ /(.+)\|(.+)/ )
    50          
130             {
131 1         4 $text = $1;
132 1         2 $link = $2;
133             }
134             elsif ( $seq_argument =~ /(.+)/ )
135             {
136 1         3 $text = "";
137 1         2 $link = $1;
138             }
139 2 100       10 if ( $link =~ /(.+?\:\/\/)(.+)/ )
    50          
140             {
141 1         3 $ret .= "$link";
142 1 50       5 $ret .= " [$text]" if ( length($text) );
143             }
144             elsif ( length($link) )
145             {
146             # Internal link
147 1 50       4 if ( my ( $s, $e ) = $link =~ /(.+)\/(.+)/ )
148             {
149 0         0 $ret = "<< $s#$e >>";
150 0 0       0 $ret = "<< $s#$e,$text >>" if ($text);
151             }
152             else
153             {
154 1         4 $ret = "<< $link >>";
155 1 50       3 $ret = "<< $link,$text >>" if ($text);
156             }
157             }
158 2         92 return $ret;
159             }
160             }
161              
162              
163             sub make_header
164             {
165 0     0 1   my ( $self, $command, $level, $paragraph ) = @_;
166 0 0         if ( $command =~ /head/ )
    0          
167             {
168 0           my $h = sprintf( "%s %s", "=" x ( $level + 1 ), $paragraph );
169 0           return $h;
170             }
171             elsif ( $command =~ /item/ )
172             {
173 0           return "* $paragraph";
174             }
175 0           die "unimplemented";
176             }
177              
178              
179             sub make_text
180             {
181 0     0 1   my ( $self, $paragraph, $list ) = @_;
182 0           my @lines = split "\n", $paragraph;
183 0           my $data = $self->_prop;
184 0           my @i_paragraph;
185 0 0         my $pnt = $list ? "*" : "";
186 0           for my $line (@lines)
187             {
188 0           push @i_paragraph, $pnt x $data->{indent} . " " . $line . "\n";
189             }
190 0           return join "\n", @i_paragraph;
191             }
192              
193              
194             sub set_formatting
195             {
196 0     0 1   my $self = shift;
197 0           my $paragraph = shift;
198 0           $paragraph =~ s/I<(.*)>/_$1_/;
199 0           $paragraph =~ s/B<(.*)>/*$1*/;
200              
201             # $paragraph =~ s/B<(.*)>/*$1*/;
202 0           $paragraph =~ s/C<(.*)>/\`$1\`/xms;
203 0           return $paragraph;
204             }
205              
206              
207             1;
208              
209             __END__