Markdown/Emitter.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 167 CC 14 SCAR 28.0
CC
14
Cov
0%
end_link
56.0
text_node
12.0
begin_node
6.0
CRAP 16.5
1 ##
2 # name: WikiText::Markdown::Emitter
3 # abstract: A WikiText Receiver That Generates Markdown
4 # author: Ingy döt Net <ingy@cpan.org>
5 # license: perl
6 # copyright: 2008, 2011
7  
8 package WikiText::Markdown::Emitter;
9 use strict;
10 use warnings;
11 use base 'WikiText::Emitter';
12  
13 # use XXX;
14  
15 use constant N => "\n";
16 use constant NN => "\n\n";
17 my $pre = 0;
18 my $link = '';
19 my $list_stack = [];
20 my $list_depth = 0;
21  
22 sub reset {
23 $pre = $list_depth = 0;
24 $list_stack = [];
25 $link = '';
26 }
27  
28 use constant markdown => {
29 h1 => ['# ', NN],
30 h2 => ['## ', NN],
31 h3 => ['### ', NN],
32 h4 => ['#### ', NN],
33 h5 => ['##### ', NN],
34 h6 => ['###### ', NN],
35 p => ['', N],
36 hr => ['---', NN],
37 ul => [undef, undef],
38 ol => [undef, undef],
39 li => [undef, ''],
40 text => ['', ''],
41 link => [undef, undef],
42 b => ['**', '**'],
43 i => ['_', '_'],
44 tt => ['`', '`'],
45 table => ['', N],
46 tr => ['', "|\n"],
47 td => ['| ', ' '],
48 pre => [undef, N],
49 };
50  
51 sub begin_node {
52 my ($self, $node) = @_;
53 my $type = $node->{type};
54 # print "BEGIN $type\n";
55 my $markdown = markdown()->{$type}
56 or die "Unhandled markup '$type'";
57 my $method = "begin_$type";
58 $self->{output} .= defined $markdown->[0]
59 ? $markdown->[0]
60 : $self->$method($node);
61 }
62  
63 sub end_node {
64 my ($self, $node) = @_;
65 my $type = $node->{type};
66 # print "END $type\n";
67 my $markdown = markdown()->{$type}
68 or die "Unhandled markup '$type'";
69 my $method = "end_$type";
70 $self->{output} .= defined $markdown->[1]
71 ? $markdown->[1]
72 : $self->$method($node);
73 }
74  
75 sub text_node {
76 my ($self, $text) = @_;
77 # print "TEXT $text\n";
78 if ($link) {
79 return;
80 }
81 elsif ($pre) {
82 $pre = 0;
83 $text =~ s/^/ /gm;
84 $text =~ s/^ *$//gm;
85 $self->{output} .= $text;
86 }
87 else {
88 $self->{output} .= $text;
89 }
90 }
91  
92 sub begin_pre {
93 $pre = 1;
94 '';
95 }
96  
97 sub begin_link {
98 my ($self, $node) = @_;
99 $link = $node->{attributes}{target};
100 return '';
101 }
102  
103 sub end_link {
104 my ($self, $node) = @_;
105 my $l = $link;
106 $link = '';
107 if ($l =~ /^\w+$/) {
108 return "[$l](/$l)";
109 }
110 elsif ($l =~ /^(https?|ftp|irc|file):[^\ ]+$/) {
111 return $l;
112 }
113 elsif ($l =~ /(.*?) *((?:https?|ftp|irc|file):[^\ ]+) *(.*)/) {
114 my $t = $1;
115 $t .= " " if length $t and length $3;
116 $t .= $3 if length $3;
117 return "[$t]($2)";
118 }
119 elsif ($l =~ /^(?:\w+)(\ \w+)+$/) {
120 my $t = $l;
121 $l =~ s/ /_/g;
122 return "[$t](/$l)";
123 }
124 else {
125 warn "Invalid link: '$l'";
126 return '$l';
127 }
128 }
129  
130 sub begin_ol {
131 my ($self) = @_;
132 $list_stack->[$list_depth++] = 'ol';
133 '';
134 }
135  
136 sub begin_ul {
137 my ($self) = @_;
138 $list_stack->[$list_depth++] = 'ul';
139 '';
140 }
141  
142 sub begin_li {
143 my ($self) = @_;
144 my $indent = ' ' x (($list_depth - 1) * 2);
145 return $indent . '* '
146 if $list_stack->[$list_depth - 1] eq 'ul';
147 return $indent . '1. ';
148 }
149  
150 sub end_ol {
151 my ($self) = @_;
152 $list_depth--;
153 return($list_depth ? "" : "\n");
154 }
155  
156 sub end_ul {
157 my ($self) = @_;
158 $list_depth--;
159 return($list_depth ? "" : "\n");
160 }
161  
162 1;
163  
164 =head1 SYNOPSIS
165  
166 use WikiText::Markdown::Emitter;
167  
168 =head1 DESCRIPTION
169  
170 This receiver module, when hooked up to a parser, produces Markdown.