line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Swagger2::Editor; |
2
|
1
|
|
|
1
|
|
913
|
use Mojo::Base 'Mojolicious'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
89859
|
use Mojo::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
4
|
1
|
|
|
1
|
|
5
|
use File::Basename; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
82
|
|
5
|
1
|
|
|
1
|
|
593
|
use Swagger2; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
10
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has specification_file => sub { $ENV{SWAGGER_API_FILE} || '' }; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
has _swagger => sub { Swagger2->new }; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub _get { |
12
|
1
|
|
|
1
|
|
10361
|
my $c = shift; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
$c->respond_to( |
15
|
|
|
|
|
|
|
txt => {data => $c->app->_swagger->pod->to_string, layout => undef}, |
16
|
|
|
|
|
|
|
any => sub { |
17
|
1
|
|
|
1
|
|
675
|
my $c = shift; |
18
|
1
|
50
|
|
|
|
3
|
$c->stash(layout => undef) if $c->req->is_xhr; |
19
|
1
|
|
|
|
|
39
|
$c->render(template => 'editor'); |
20
|
|
|
|
|
|
|
} |
21
|
1
|
|
|
|
|
5
|
); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _post { |
25
|
2
|
|
|
2
|
|
57849
|
my $c = shift; |
26
|
2
|
|
50
|
|
|
10
|
my $spec = $c->req->body || '{}'; |
27
|
2
|
|
|
|
|
70
|
my $file = $c->app->specification_file; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
eval { |
30
|
2
|
|
|
|
|
23
|
my $s = Swagger2->new->parse($spec); |
31
|
2
|
50
|
33
|
|
|
128
|
Mojo::Util::spurt($spec, $file) if $file and -w $file; |
32
|
2
|
|
|
|
|
585
|
$c->render(text => $c->podify($s->pod), layout => undef); |
33
|
2
|
50
|
|
|
|
21
|
} or do { |
34
|
0
|
|
|
|
|
0
|
my $e = $@; |
35
|
0
|
|
|
|
|
0
|
$c->app->log->error($e); |
36
|
0
|
|
|
|
|
0
|
$e =~ s!^(Could not.*?:)\s+!$1\n\n!s; |
37
|
0
|
|
|
|
|
0
|
$e =~ s!\s+at \S+\.pm line \d\S+!!g; |
38
|
0
|
|
|
|
|
0
|
$c->render(template => 'error', error => $e); |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub startup { |
43
|
1
|
|
|
1
|
1
|
13551
|
my $self = shift; |
44
|
1
|
|
|
|
|
3
|
my $raw = ''; |
45
|
|
|
|
|
|
|
|
46
|
1
|
50
|
|
|
|
6
|
if (my $file = $self->specification_file) { |
47
|
1
|
50
|
|
|
|
29
|
$raw |
48
|
|
|
|
|
|
|
= $file =~ /^https?:/ |
49
|
|
|
|
|
|
|
? $self->ua->get($file)->res->body |
50
|
|
|
|
|
|
|
: Mojo::Util::slurp(File::Spec->catfile(split '/', $file)); |
51
|
1
|
|
|
|
|
121
|
$self->_swagger->parse($raw, $file); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
2
|
unshift @{$self->renderer->classes}, __PACKAGE__; |
|
1
|
|
|
|
|
5
|
|
55
|
1
|
|
|
|
|
18
|
unshift @{$self->static->paths}, File::Spec->catdir(File::Basename::dirname(__FILE__), 'public'); |
|
1
|
|
|
|
|
4
|
|
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
95
|
$self->routes->get('/' => \&_get); |
58
|
1
|
|
|
|
|
314
|
$self->routes->post('/' => \&_post); |
59
|
1
|
|
|
|
|
114
|
$self->defaults(raw => $raw, swagger => $self->_swagger, layout => 'default'); |
60
|
1
|
|
|
|
|
38
|
$self->plugin('PODRenderer'); |
61
|
1
|
|
|
|
|
23703
|
$self->helper(podify => \&_podify); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _podify { |
65
|
3
|
|
|
3
|
|
162
|
my ($c, $pod) = @_; |
66
|
3
|
|
|
|
|
16
|
my $dom = Mojo::DOM->new($c->pod_to_html($pod->to_string)); |
67
|
3
|
|
|
|
|
76788
|
my $ul = ' |
68
|
3
|
|
|
|
|
9
|
my ($sub, @parts); |
69
|
|
|
|
|
|
|
|
70
|
3
|
|
|
|
|
22
|
for my $e ($dom->find('h1, h2')->each) { |
71
|
30
|
|
|
|
|
23851
|
my $id = $e->{id}; |
72
|
30
|
|
|
|
|
504
|
my $text = $e->all_text; |
73
|
30
|
|
|
30
|
|
1014
|
my $anchor = $c->tag(a => href => "#$id", sub {$text}); |
|
30
|
|
|
|
|
1162
|
|
74
|
|
|
|
|
|
|
|
75
|
30
|
50
|
|
|
|
1725
|
if ($e->type eq 'h1') { |
76
|
0
|
0
|
|
|
|
0
|
$ul .= '' if $sub; |
77
|
0
|
|
|
|
|
0
|
$sub = 0; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
30
|
100
|
|
|
|
391
|
$ul .= ' |
81
|
30
|
|
|
|
|
43
|
$sub = 1; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
30
|
|
|
|
|
90
|
$ul .= "$anchor"; |
85
|
|
|
|
|
|
|
|
86
|
30
|
|
|
|
|
233
|
$e->content($c->link_to($text => Mojo::URL->new->fragment('toc'), id => $id)); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
3
|
50
|
|
|
|
1533
|
$ul .= '' if $ul; |
90
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
19
|
return $c->b(qq(TABLE OF CONTENTS$ul$dom)); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$ENV{SWAGGER_LOAD_EDITOR} ? __PACKAGE__->new : 1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__DATA__ |