blib/lib/Padre/Plugin/CSSComb.pm

filter: Statementstmt 60.0% 12 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 4 / 8 Podpod n/a totaltotal 57.1% 16 / 28 CC 1 SCAR 4.1
CC
1
Cov
60%
plugin_name
2.0
padre_interfaces
2.0
menu_plugins
2.0
CRAP 1.5
↑ Summary
1 package Padre::Plugin::CSSComb;
2 {
3 $Padre::Plugin::CSSComb::VERSION = '0.001';
4 }
5  
6 # ABSTRACT: CSSComb plugin for Padre
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base 'Padre::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub plugin_name {
Subroutine plugin_name: not called CC=1 SCAR=6.9
13 0 'CSSComb';
14 }
15  
16 0 ▶sub padre_interfaces {
Subroutine padre_interfaces: not called CC=1 SCAR=6.9
17 0 'Padre::Plugin' => 0.96,
18 ;
19 }
20  
21 0 ▶sub menu_plugins {
Subroutine menu_plugins: not called CC=1 SCAR=6.9
22 0 my $self = shift;
23 0 my $main = $self->main;
24 0 my $menu_item = Wx::MenuItem->new(
25 undef,
26 -1,
27 Wx::gettext('CSSComb current document/selection')
28 . "...\tAlt+Shift+C",
29 );
30  
31 Wx::Event::EVT_MENU(
32 $main,
33 $menu_item,
34 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 $self->_comb_css;
36 },
37 0 );
38  
39 0 return $menu_item;
40 }
41  
42 sub _comb_css {
43 my ($self) = @_;
44 my $main = $self->main;
45  
46 my $src = $main->current->text;
47 my $doc = $main->current->document;
48 return unless $doc;
49 my $code = $src ? $src : $doc->text_get;
50 return unless ( defined $code and length($code) );
51  
52 # NOTE: this is synchronous for a reason, the active document/selection
53 # might change if made asynchronously
54 require LWP::UserAgent;
55 my $useragent = LWP::UserAgent->new(
56 agent => "Padre::Plugin::CSSComb",
57 timeout => 10,
58 );
59 unless (Padre::Constant::WIN32) {
60 $useragent->env_proxy;
61 }
62  
63 my $response = $useragent->request(
64 POST(
65 'http://csscomb.com/gate/gate.php',
66 Referer => 'http://csscomb.com/online/',
67 Content => [ code => $code ],
68 )
69 );
70 if ( !$response->is_success ) {
71 my $error_text = Wx::gettext(
72 "CSSComb encountered an error: " . $response->status_line );
73 $self->_output($error_text);
74 return;
75 }
76  
77 my $css = $response->decoded_content;
78  
79 # make sure the line endings match the document
80 # the service always returns Windows CR/LF
81 # TODO: try to use Wx::Scintilla's ConvertEOLs
82 if ( $doc->newline_type eq 'UNIX' ) {
83 $css =~ s/\015\012/\012/g;
84 }
85 elsif ( $doc->newline_type eq 'MAC' ) {
86 $css =~ s/\015\012/\015/g;
87 }
88  
89 if ($src) {
90 my $editor = $main->current->editor;
91 $editor->ReplaceSelection($css);
92 }
93 else {
94 $doc->text_set($css);
95 }
96 }
97  
98 sub _output {
99 my ( $self, $text ) = @_;
100 my $main = $self->main;
101  
102 $main->show_output(1);
103 $main->output->clear;
104 $main->output->AppendText($text);
105 }
106  
107  
108 1;
109  
110 __END__
↑ Summary