File Coverage

blib/lib/Pod/Simple/Wiki/Markdown.pm
Criterion Covered Total %
statement 51 51 100.0
branch 13 14 92.8
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 75 76 98.6


line stmt bran cond sub pod time code
1             package Pod::Simple::Wiki::Markdown;
2              
3             ###############################################################################
4             #
5             # Pod::Simple::Wiki::Markdown - A class for creating Pod to Markdown filters.
6             #
7             #
8             # Copyright 2003-2014, John McNamara, jmcnamara@cpan.org, Daniel T. Staal,
9             # DStaal@usa.net
10             #
11             # Documentation after __END__
12             #
13              
14             # perltidy with the following options: -mbl=2 -pt=0 -nola
15              
16 5     5   21 use Pod::Simple::Wiki;
  5         6  
  5         144  
17 5     5   18 use strict;
  5         6  
  5         143  
18 5     5   17 use vars qw(@ISA $VERSION);
  5         6  
  5         2599  
19              
20              
21             @ISA = qw(Pod::Simple::Wiki);
22             $VERSION = '0.18';
23              
24             ###############################################################################
25             #
26             # The tag to wiki mappings.
27             #
28             my $tags = {
29             '' => '**',
30             '' => '**',
31             '' => '_',
32             '' => '_',
33             '' => '`',
34             '' => '`',
35             '
'  => "\n```\n", 
36             '' => "\n```\n",
37              
38             '

' => '# ',

39             '' => "\n\n",
40             '

' => '## ',

41             '' => "\n\n",
42             '

' => '### ',

43             '' => "\n\n",
44             '

' => '#### ',

45             '' => "\n\n",
46             };
47              
48             ###############################################################################
49             #
50             # new()
51             #
52             # Simple constructor inheriting from Pod::Simple::Wiki.
53             #
54             sub new {
55              
56 30     30 1 34 my $class = shift;
57 30         78 my $self = Pod::Simple::Wiki->new( 'wiki', @_ );
58 30         60 $self->{_tags} = $tags;
59              
60 30         41 bless $self, $class;
61 30         66 return $self;
62             }
63              
64             ###############################################################################
65             #
66             # _indent_item()
67             #
68             # Indents an "over-item" to the correct level.
69             #
70             sub _indent_item {
71              
72 37     37   28 my $self = shift;
73 37         34 my $item_type = $_[0];
74 37         27 my $item_param = $_[1];
75 37         47 my $indent_level = $self->{_item_indent} - 1;
76              
77 37 100       80 if ( $item_type eq 'bullet' ) {
    100          
    50          
78 12         37 $self->_append( ' ' x $indent_level . '* ' );
79             }
80             elsif ( $item_type eq 'number' ) {
81 12         31 $self->_append( ' ' x $indent_level . '1 ' );
82             }
83              
84             # In theory Markdown supports nested definition lists - but *everything* has to be indented.
85             elsif ( $item_type eq 'text' ) {
86 13         34 $self->_append( ' ' x $indent_level . '' );
87             }
88             }
89              
90             ###############################################################################
91             #
92             # _start_L()
93             #
94             # Handle the start of a link element.
95             #
96             sub _start_L {
97              
98 4     4   3 my $self = shift;
99 4         4 my $link_attrs = shift;
100              
101 4         4 $self->{_link_attrs} = $link_attrs;
102              
103             # Ouput start of Confluence link and flush the _wiki_text buffer.
104 4         10 $self->_output( '[' );
105             }
106              
107              
108             ###############################################################################
109             #
110             # _end_L()
111             #
112             # Handle the end of a link element.
113             #
114             sub _end_L {
115              
116 4     4   3 my $self = shift;
117 4         4 my $link_attrs = $self->{_link_attrs};
118 4         4 my $link_target = $link_attrs->{to};
119 4         4 my $link_section = $link_attrs->{section};
120              
121 4 100       7 $link_target = '' if( !defined($link_target));
122              
123             # Handle links that are parsed as Pod links.
124 4 100       7 if ( defined $link_section ) {
125 2         5 $link_target = "$link_target#$link_section";
126             }
127              
128 4         36 $self->_append( "]($link_target)" );
129             }
130              
131             ###############################################################################
132             #
133             # _handle_text()
134             #
135             # Perform any necessary transforms on the text. This is mainly used to escape
136             # inadvertent CamelCase words.
137             #
138             sub _handle_text {
139              
140 69     69   377 my $self = shift;
141 69         57 my $text = $_[0];
142              
143             # Only escape words in paragraphs
144 69 100       127 if ( not $self->{_in_Para} ) {
145 41         44 $self->{_wiki_text} .= $text;
146 41         58 return;
147             }
148              
149             # Split the text into tokens but maintain the whitespace
150 28         135 my @tokens = split /(\s+)/, $text;
151              
152             # Escape any tokens here, if necessary.
153             # The following characters are escaped by prepending a backslash: \`*_
154             # (Markdown has other escapes as well, but these cover most cases, and the others
155             # are usually optional.)
156 28         41 @tokens = map { s/([\\`\*\_])/\\$1/g; $_ } @tokens;
  164         149  
  164         219  
157              
158             # Rejoin the tokens and whitespace.
159 28         104 $self->{_wiki_text} .= join '', @tokens;
160             }
161              
162              
163             ###############################################################################
164             #
165             # Functions to deal with =over ... =back regions for
166             #
167             # Bulleted lists
168             # Numbered lists
169             # Text lists
170             # Block lists
171             #
172             sub _end_item_text {
173 13     13   8 my $self = shift;
174 13         14 my $indent_level = $self->{_item_indent} - 1;
175              
176 13         31 $self->_output( "\n" . ' ' x $indent_level . ': ' );
177             }
178              
179              
180             ###############################################################################
181             #
182             # _start_Para()
183             #
184             # Special handling for paragraphs that are part of an "over" block.
185             #
186             sub _start_Para {
187              
188 27     27   57 my $self = shift;
189 27         33 my $indent_level = $self->{_item_indent} - 1;
190              
191 27 100       78 if ( $self->{_in_over_block} ) {
192 1         5 $self->_append( ' ' x $indent_level . '' );
193             }
194             }
195              
196              
197             1;
198              
199              
200             __END__