/
pmtidyapp.pl
93 lines (75 loc) · 2.02 KB
/
pmtidyapp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use Mojolicious::Lite;
use HTML::Entities;
use lib 'lib';
use CodeBlock;
sub _decodews
{
my ($cr) = @_;
decode_entities($$cr);
$$cr =~ tr{\xA0}{ }; #
$$cr =~ s{<br */?>}{}g;
return;
}
sub _forcews
{
#   must be intermixed with spaces because two or more spaces
# are truncated to one inside a <p> html tag...
s{ ( ^ [ ]+ | # Lines starting with spaces
[ ]{2,} ) } # Two or more spaces
{ ' ' x (length($1) / 2) . (length($1) % 2 ? ' ' : '') }gexms;
s{\n}{<br />\n}g;
}
sub _rmlinenums
{
s{^\d+: }{}gm;
}
get '/' => sub { shift->render_static('index.html') };
post '/' => sub {
my $c = shift;
my $dom = Mojo::DOM->new($c->req->body);
my $req = $dom->at('tidyreq');
unless($req && $dom->at('tidyreq > code') && $dom->at('tidyreq > tag')){
$c->render('text' => "500 Invalid Request\n", 'status' => 500);
return;
}
my $code = $dom->at('tidyreq > code')->text;
my $tag = $dom->at('tidyreq > tag')->text;
my $numbered;
_decodews(\$code);
$numbered = _rmlinenums() for($code);
my $b = new CodeBlock($code);
my($hilited, $tidied);
eval { $hilited = $b->hilited(); $tidied = $b->tidied(); };
if($@){
my $err = $@;
if($err =~ /^Perl::Tidy error:/){
$c->app->log->debug($err);
$c->stash('errmsg' => $err);
$c->render('template' => 'error', format => 'xml');
return;
}
die; # rethrow
}
# TODO: move to CodeBlock class?
if(uc $tag eq 'P'){
_forcews() for($hilited, $tidied);
}
$c->stash('hilited' => $hilited);
$c->stash('tidied' => $tidied);
$c->render('resp', 'format' => 'xml');
};
app->start;
__DATA__
@@ error.xml.ep
<?xml version="1.0" encoding="UTF-8" ?>
<tidyresp>
<error>
<%= $errmsg %>
</error>
</tidyresp>
@@ resp.xml.ep
<?xml version="1.0" encoding="UTF-8" ?>
<tidyresp>
<hilited><![CDATA[<%== $hilited %>]]></hilited>
<tidied><![CDATA[<%== $tidied %>]]></tidied>
</tidyresp>