File Coverage

blib/lib/Pod/AsciiDoctor.pm
Criterion Covered Total %
statement 94 103 91.2
branch 30 42 71.4
condition 5 7 71.4
subroutine 16 17 94.1
pod 10 10 100.0
total 155 179 86.5


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