Perl6.pm untested

filter: Statementstmt 0.0% 0 / 303 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 454 CC 20 SCAR 24.4
CC
20
Cov
0%
show_perl6_doc
30.0
export_html
30.0
generate_p6_pir
30.0
CRAP 11.4
1 package Padre::Plugin::Perl6;
2 BEGIN {
3 $Padre::Plugin::Perl6::VERSION = '0.71';
4 }
5  
6 # ABSTRACT: Perl 6 Support for Padre
7  
8 use 5.010;
9 use strict;
10 use warnings;
11 use Carp;
12 use Padre::Wx ();
13 use Padre::Constant ();
14 use Padre::Util ();
15 use base 'Padre::Plugin';
16 use Padre::Plugin::Perl6::Util;
17  
18 # constants for html exporting
19 my $FULL_HTML = 'full_html';
20 my $SIMPLE_HTML = 'simple_html';
21 my $SNIPPET_HTML = 'snippet_html';
22  
23 use Class::XSAccessor accessors => {
24 config => 'config', # plugin configuration object
25 };
26  
27 # static field to contain reference to current plugin configuration
28 my $config;
29  
30 sub plugin_config {
31 return $config;
32 }
33  
34 # Returns the plugin name to Padre
35 sub plugin_name {
36 return Wx::gettext("Perl 6");
37 }
38  
39 sub padre_interfaces {
40 'Padre::Plugin' => 0.43;
41 }
42  
43 #
44 # Returns the current share directory location
45 #
46 sub _sharedir {
47 return Padre::Util::share('Perl6');
48 }
49  
50 # plugin's real icon object
51 sub logo_icon {
52 my ($self) = @_;
53  
54 my $icon = Wx::Icon->new;
55 $icon->CopyFromBitmap( $self->plugin_icon );
56  
57 return $icon;
58 }
59  
60 # plugin bitmap
61 sub plugin_icon {
62 my $self = shift;
63  
64 # find resource path
65 my $icon_path = File::Spec->catfile( $self->_sharedir, 'icons', 'camelia.png' );
66  
67 # create and return icon
68 return Wx::Bitmap->new( $icon_path, Wx::wxBITMAP_TYPE_PNG );
69 }
70  
71 # called when the plugin is enabled
72 sub plugin_enable {
73 my $self = shift;
74  
75 # Read the plugin configuration, and create it if it is not there
76 $config = $self->config_read;
77 if ( not $config ) {
78  
79 # no configuration, let us write some defaults
80 $config = {};
81 }
82  
83 # make sure defaults are respected if they are undefined.
84 if ( not defined $config->{colorizer} ) {
85 $config->{colorizer} = 'STD';
86 }
87 if ( not defined $config->{p6_highlight} ) {
88 $config->{p6_highlight} = 1;
89 }
90  
91 # and write the plugin's configuration
92 $self->config_write($config);
93  
94 # update configuration attribute
95 $self->config($config);
96  
97 return 1;
98 }
99  
100 sub menu_plugins {
101 my $self = shift;
102 my $main = shift;
103  
104 # plugin menu
105 $self->{menu} = Wx::Menu->new;
106  
107 # New Perl 6... menu
108 my $file_menu = Wx::Menu->new();
109 Wx::Event::EVT_MENU(
110 $main,
111 $self->{menu}->Append( -1, Wx::gettext("Create Perl 6..."), $file_menu ),
112 sub { },
113 );
114 Wx::Event::EVT_MENU(
115 $main,
116 $file_menu->Append( -1, Wx::gettext("Script"), ),
117 sub { $self->_create_from_template( 'p6_script', 'p6' ) },
118 );
119 Wx::Event::EVT_MENU(
120 $main,
121 $file_menu->Append( -1, Wx::gettext("Class"), ),
122 sub { $self->_create_from_template( 'p6_class', 'p6' ) },
123 );
124 Wx::Event::EVT_MENU(
125 $main,
126 $file_menu->Append( -1, Wx::gettext("Grammar"), ),
127 sub { $self->_create_from_template( 'p6_grammar', 'p6' ) },
128 );
129 Wx::Event::EVT_MENU(
130 $main,
131 $file_menu->Append( -1, Wx::gettext("Package"), ),
132 sub { $self->_create_from_template( 'p6_package', 'p6' ) },
133 );
134 Wx::Event::EVT_MENU(
135 $main,
136 $file_menu->Append( -1, Wx::gettext("Module"), ),
137 sub { $self->_create_from_template( 'p6_module', 'p6' ) },
138 );
139 Wx::Event::EVT_MENU(
140 $main,
141 $file_menu->Append( -1, Wx::gettext("Role"), ),
142 sub { $self->_create_from_template( 'p6_role', 'p6' ) },
143 );
144 Wx::Event::EVT_MENU(
145 $main,
146 $file_menu->Append( -1, Wx::gettext("Inlined in Perl 5"), ),
147 sub { $self->_create_from_template( 'p6_inline_in_p5', 'p5' ) },
148 );
149  
150 # Rakudo sub menu
151 my $rakudo_menu = Wx::Menu->new();
152 Wx::Event::EVT_MENU(
153 $main,
154 $self->{menu}->Append( -1, Wx::gettext("Rakudo"), $rakudo_menu ),
155 sub { },
156 );
157  
158 # Generate Perl 6 PIR
159 Wx::Event::EVT_MENU(
160 $main,
161 $rakudo_menu->Append( -1, Wx::gettext("Perl 6 PIR"), ),
162 sub { $self->generate_p6_pir; },
163 );
164  
165 # Export sub menu
166 my $export_menu = Wx::Menu->new();
167 Wx::Event::EVT_MENU(
168 $main,
169 $self->{menu}->Append( -1, Wx::gettext("Export..."), $export_menu ),
170 sub { },
171 );
172  
173 # Export into HTML
174 Wx::Event::EVT_MENU(
175 $main,
176 $export_menu->Append( -1, Wx::gettext("Full html"), ),
177 sub { $self->export_html($FULL_HTML); },
178 );
179 Wx::Event::EVT_MENU(
180 $main,
181 $export_menu->Append( -1, Wx::gettext("Simple html"), ),
182 sub { $self->export_html($SIMPLE_HTML); },
183 );
184 Wx::Event::EVT_MENU(
185 $main,
186 $export_menu->Append( -1, Wx::gettext("Snippet html"), ),
187 sub { $self->export_html($SNIPPET_HTML); },
188 );
189  
190 $self->{menu}->AppendSeparator;
191  
192 # More Help? sub menu
193 my $more_help_menu = Wx::Menu->new();
194 Wx::Event::EVT_MENU(
195 $main,
196 $self->{menu}->Append( -1, Wx::gettext("More Help?"), $more_help_menu ),
197 sub { },
198 );
199  
200 my %topics = (
201 '01perlintro' => Wx::gettext('Perl 6 Introduction'),
202 '02perlsyn' => Wx::gettext('Perl 6 Syntax'),
203 '03perlobjintro' => Wx::gettext('Perl 6 Objects Introduction'),
204 '04perlreintro' => Wx::gettext('Perl 6 Regexes Introduction'),
205 );
206  
207 for my $topic ( sort keys %topics ) {
208 my $topic_text = $topic;
209 $topic_text =~ s/^\d+//;
210 Wx::Event::EVT_MENU(
211 $main,
212 $more_help_menu->Append( -1, $topics{$topic} ),
213 sub { $main->help_search($topic_text); },
214 );
215 }
216  
217 $more_help_menu->AppendSeparator;
218  
219 # Goto #padre link
220 Wx::Event::EVT_MENU(
221 $main,
222 $more_help_menu->Append( -1, Wx::gettext("Visit #padre for Padre Help"), ),
223 sub { Wx::LaunchDefaultBrowser("http://padre.perlide.org/irc.html?channel=padre"); },
224 );
225  
226 # Goto #perl6 link
227 Wx::Event::EVT_MENU(
228 $main,
229 $more_help_menu->Append( -1, Wx::gettext("Visit #perl6 for Perl 6 Help"), ),
230 sub { Wx::LaunchDefaultBrowser("http://padre.perlide.org/irc.html?channel=perl6"); },
231 );
232  
233 # Perl 6 link
234 Wx::Event::EVT_MENU(
235 $main,
236 $more_help_menu->Append( -1, Wx::gettext("Perl 6 Website"), ),
237 sub { Wx::LaunchDefaultBrowser("http://perl6.org"); },
238 );
239  
240 $self->{menu}->AppendSeparator;
241  
242 # Preferences
243 Wx::Event::EVT_MENU(
244 $main,
245 $self->{menu}->Append( -1, Wx::gettext("Preferences"), ),
246 sub { $self->plugin_preferences; },
247 );
248  
249 # the famous about menu item...
250 Wx::Event::EVT_MENU(
251 $main,
252 $self->{menu}->Append( -1, Wx::gettext("About"), ),
253 sub { $self->show_about },
254 );
255  
256 # Return our plugin with its label
257 return ( $self->plugin_name => $self->{menu} );
258 }
259  
260 sub registered_documents {
261 'application/x-perl6' => 'Padre::Plugin::Perl6::Document',;
262 }
263  
264 sub provided_highlighters {
265 return (
266 [ 'Padre::Plugin::Perl6::StdColorizer', 'STD.pm', 'Larry Wall\'s Perl 6 reference grammar' ],
267 );
268 }
269  
270 sub highlighting_mime_types {
271 return (
272 'Padre::Plugin::Perl6::StdColorizer' => ['application/x-perl6'],
273  
274 # 'Padre::Plugin::Perl6::Perl6PgeColorizer' => ['application/x-perl6'],
275 );
276 }
277  
278 # create a Perl 6 file from the template
279 sub _create_from_template {
280 my ( $self, $template, $extension ) = @_;
281  
282 $self->main->on_new;
283  
284 my $editor = $self->current->editor or return;
285 my $file = File::Spec->catdir( $self->_sharedir, "templates", "$template.$extension" );
286  
287 if ( $editor->insert_from_file($file) ) {
288 my $document = $editor->{Document};
289 $document->{original_content} = $document->text_get;
290 $document->set_mimetype( $document->guess_mimetype );
291 $document->editor->padre_setup;
292 $document->rebless;
293 $document->colourize;
294 } else {
295 $self->main->message( sprintf( Wx::gettext("Error loading template file '%s'"), $file ) );
296 }
297  
298 return;
299 }
300  
301 sub plugin_preferences {
302 my $self = shift;
303  
304 require Padre::Plugin::Perl6::Preferences;
305 my $prefs = Padre::Plugin::Perl6::Preferences->new($self);
306 $prefs->Show;
307 }
308  
309 sub show_about {
310 my $self = shift;
311  
312 require Syntax::Highlight::Perl6;
313 require App::Grok;
314  
315 my $about = Wx::AboutDialogInfo->new;
316 $about->SetName("Padre::Plugin::Perl6");
317 $about->SetDescription( Wx::gettext("This plugin enables useful Perl 6 features on Padre IDE.") . "\n"
318 . Wx::gettext("It integrates coloring and easy access to Perl 6 help documents.") . "\n\n"
319 . Wx::gettext("The following modules are used:") . "\n"
320 . "Syntax::Highlight::Perl6 "
321 . $Syntax::Highlight::Perl6::VERSION . "\n"
322 . "App::Grok "
323 . $App::Grok::VERSION
324 . "\n" );
325 $about->SetVersion($Padre::Plugin::Perl6::VERSION);
326  
327 # create and return the camelia icon
328 my $camelia_path = File::Spec->catfile( $self->_sharedir, 'icons', 'camelia-big.png' );
329 my $camelia_bmp = Wx::Bitmap->new( $camelia_path, Wx::wxBITMAP_TYPE_PNG );
330 my $camelia_icon = Wx::Icon->new();
331 $camelia_icon->CopyFromBitmap($camelia_bmp);
332 $about->SetIcon($camelia_icon);
333  
334 Wx::AboutBox($about);
335 return;
336 }
337  
338 sub show_perl6_doc {
339 my $self = shift;
340  
341 # find the word under the current cursor position
342 my $topic = '';
343 my $doc = $self->current->document;
344 if ( $doc && $doc->mimetype eq q{application/x-perl6} ) {
345  
346 # make sure it is a Perl6 document
347 my $editor = $doc->editor;
348 $topic = $editor->GetSelectedText;
349 if ( not $topic ) {
350 my $lineno = $editor->GetCurrentLine();
351 my $line = $editor->GetLine($lineno);
352 my $current_pos = $editor->GetCurrentPos() - $editor->PositionFromLine($lineno);
353 my $current_word = '';
354 while ( $line =~ m/\G.*?(\S+)/g ) {
355 if ( pos($line) >= $current_pos ) {
356 $current_word = $1;
357 last;
358 }
359 }
360 if ( $current_word =~ /^.*?(\S+)/ ) {
361 $topic = $1;
362 }
363 }
364 }
365  
366 }
367  
368 sub highlight {
369 my $self = shift;
370 my $doc = Padre::Current->document or return;
371  
372 if ( $doc->can('colorize') ) {
373 my $text = $doc->text_get;
374 $doc->{_text} = $text;
375 $doc->{force_p6_highlight} = 1;
376 $doc->colorize;
377 $doc->{force_p6_highlight} = 0;
378 }
379 }
380  
381 sub text_with_one_nl {
382 my $self = shift;
383 my $doc = shift;
384 my $text = $doc->text_get // '';
385  
386 my $nlchar = "\n";
387 if ( $doc->newline_type eq 'WIN' ) {
388 $nlchar = "\r\n";
389 } elsif ( $doc->newline_type eq 'MAC' ) {
390 $nlchar = "\r";
391 }
392 $text =~ s/$nlchar/\n/g;
393 return $text;
394 }
395  
396 sub export_html {
397 my ( $self, $type ) = @_;
398  
399 my $main = $self->main;
400  
401 my $doc = $main->current->document;
402 if ( not defined $doc ) {
403 Wx::MessageBox( Wx::gettext('No document'), Wx::gettext('Error'), Wx::wxOK, $main, );
404 return;
405 }
406  
407 if ( $doc->mimetype ne q{application/x-perl6} ) {
408 Wx::MessageBox(
409 Wx::gettext('Not a Perl 6 file'),
410 Wx::gettext('Operation cancelled'),
411 Wx::wxOK,
412 $main,
413 );
414 return;
415 }
416  
417 my $text = $self->text_with_one_nl($doc);
418  
419 require File::Temp;
420 my $tmp_in = File::Temp->new( SUFFIX => '.p6_in.txt' );
421 binmode( $tmp_in, ":utf8" );
422 print $tmp_in $text;
423 close $tmp_in or warn "cannot close $tmp_in\n";
424  
425 my $tmp_out = File::Temp->new( SUFFIX => '.p6_out.txt' );
426 binmode( $tmp_out, ":utf8" );
427 close $tmp_out or warn "cannot close $tmp_out\n";
428  
429 my $tmp_err = File::Temp->new( SUFFIX => '.p6_err.txt' );
430 binmode( $tmp_err, ":utf8" );
431 close $tmp_err or warn "cannot close $tmp_err\n";
432  
433 # construct the command
434 require File::Which;
435 my $hilitep6 = File::Which::which('hilitep6');
436 my @cmd = (
437 $hilitep6,
438 $tmp_in,
439 );
440  
441 given ($type) {
442 when ($FULL_HTML) { push @cmd, "--full-html=$tmp_out 2>$tmp_err"; }
443 when ($SIMPLE_HTML) { push @cmd, "--simple-html=$tmp_out 2>$tmp_err"; }
444 when ($SNIPPET_HTML) { push @cmd, "--snippet-html=$tmp_out 2>$tmp_err"; }
445 default {
446  
447 # default is full html
448 push @cmd, "--full-html=$tmp_out 2>$tmp_err";
449 }
450 }
451  
452  
453 # execute the command...
454 my $cmd = join ' ', @cmd;
455 `$cmd`;
456  
457 # and read its output...
458 my ( $out, $err );
459 {
460 local $/ = undef; #enable localized slurp mode
461  
462 # slurp the process output...
463 open CHLD_OUT, $tmp_out or warn "Could not open $tmp_out";
464 $out = <CHLD_OUT>;
465 close CHLD_OUT or warn "Could not close $tmp_out\n";
466  
467 open CHLD_ERR, $tmp_err or warn "Could not open $tmp_err\n";
468 $err = <CHLD_ERR>;
469 close CHLD_ERR or warn "Could not close $tmp_err\n";
470 }
471  
472 my $html;
473 if ($err) {
474  
475 # remove ANSI color escape sequences...
476 $err =~ s/\033\[(\d+)(?:;(\d+)(?:;(\d+))?)?m//g;
477 Wx::MessageBox(
478 qq{STD.pm warning/error:\n$err},
479 'Operation cancelled',
480 Wx::wxOK,
481 $main,
482 );
483 print "\nSTD.pm Parsing error\n" . $err . "\n";
484 return;
485 } else {
486 $html = $out;
487 }
488  
489 # create a temporary HTML file
490 my $tmp = File::Temp->new( SUFFIX => '.html' );
491 $tmp->unlink_on_destroy(0);
492 my $filename = $tmp->filename;
493 print $tmp $html;
494 close $tmp
495 or croak "Could not close $filename";
496  
497 # try to open the HTML file
498 $main->setup_editor($filename);
499  
500 # ask the user if he/she wants to open it in the default browser
501 my $ret = Wx::MessageBox(
502 "Saved to $filename. Do you want to open it now?",
503 "Done",
504 Wx::wxYES_NO | Wx::wxCENTRE,
505 $main,
506 );
507 if ( $ret == Wx::wxYES ) {
508  
509 # launch the HTML file in your default browser
510 require URI::file;
511 my $file_url = URI::file->new($filename);
512 Wx::LaunchDefaultBrowser($file_url);
513 }
514  
515 return;
516 }
517  
518 # Generates Parrot PIR code from rakudo
519 # and displays it to the user
520 sub generate_p6_pir {
521 my $self = shift;
522 my $main = $self->main;
523 my $doc = $main->current->document;
524  
525 if ( not defined $doc ) {
526 Wx::MessageBox( Wx::gettext('No document'), Wx::gettext('Error'), Wx::wxOK, $main, );
527 return;
528 }
529 if ( $doc->mimetype ne q{application/x-perl6} ) {
530 Wx::MessageBox(
531 'Not a Perl 6 file',
532 'Operation cancelled',
533 Wx::wxOK,
534 $main,
535 );
536 return;
537 }
538  
539 # Check for perl6 existance and that it is executable.
540 require Padre::Plugin::Perl6::Util;
541 my $perl6 = Padre::Plugin::Perl6::Util::perl6_exe();
542 unless ( $perl6 && -x $perl6 ) {
543 Wx::MessageBox(
544 'Cannot find a perl6 executable',
545 'Error',
546 Wx::wxOK,
547 $main,
548 );
549 return;
550 }
551  
552 require File::Temp;
553 my $tmp_dir = File::Temp->newdir( CLEANUP => 0 );
554 my $hello_pl = File::Spec->catfile( $tmp_dir, 'hello.pl' );
555 my $hello_pir = File::Spec->catfile( $tmp_dir, 'hello.pir' );
556  
557 #XXX- quote all those files in win32
558 my $perl6_to_pir_cmd = "$perl6 --target=PIR --output=$hello_pir $hello_pl";
559  
560 # Tell the user about the commands that are going to be executed.
561 Wx::MessageBox(
562 "The following command is going to be executed:\n\n$perl6_to_pir_cmd\n",
563 'Error',
564 Wx::wxOK,
565 $main,
566 );
567  
568  
569 open HELLO_PL, ">$hello_pl"
570 or die "Cannot open $hello_pl\n";
571 binmode HELLO_PL, ":utf8";
572 my $text = $doc->text_get;
573  
574 #XXX- check text_get return value
575 print HELLO_PL $text;
576 close HELLO_PL
577 or die "Cannot close $hello_pl\n";
578  
579 my $cmd_output = File::Spec->catfile( $tmp_dir, "output.txt" );
580  
581 # Prepare the output window for the output
582 $main->show_output(1);
583 my $outpanel = $main->output;
584 $outpanel->Remove( 0, $outpanel->GetLastPosition );
585  
586 #enable localized slurp mode
587 local $/ = undef;
588 my $out;
589  
590 # Run command:
591 # perl6 --target=PIR --output=hello.pir hello.pl
592 print "Executing:\n $perl6_to_pir_cmd\n";
593 `$perl6_to_pir_cmd 1>$cmd_output 2>&1`;
594 $outpanel->style_neutral;
595  
596 # slurp the process output...
597 open OUTPUT, $cmd_output or warn "Could not open $cmd_output\n";
598 $out = <OUTPUT>;
599 close OUTPUT or warn "Could not close $cmd_output\n";
600 $outpanel->AppendText($out);
601  
602 unless ( -f $hello_pir ) {
603 Wx::MessageBox(
604 'Operation failed. Please check the output.',
605 'Error',
606 Wx::wxOK,
607 $main,
608 );
609 return;
610 }
611  
612 # try to open the HTML file
613 $main->setup_editor($hello_pir);
614  
615 }
616  
617 1;
618  
619  
620  
621 =pod
622  
623 =head1 NAME
624  
625 Padre::Plugin::Perl6 - Perl 6 Support for Padre
626  
627 =head1 VERSION
628  
629 version 0.71
630  
631 =head1 SYNOPSIS
632  
633 After installation when you run Padre there should be a menu option Plugins/Perl 6.
634  
635 =head1 ACKNOWLEDGEMENTS
636  
637 The Camelia image is copyright 2009 by Larry Wall. Permission to use
638 is granted under the Artistic License 2.0, or any subsequent version
639 of the Artistic License.
640  
641 =head1 AUTHORS
642  
643 =over 4
644  
645 =item *
646  
647 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
648  
649 =item *
650  
651 Gabor Szabo L<http://szabgab.com/>
652  
653 =back
654  
655 =head1 COPYRIGHT AND LICENSE
656  
657 This software is copyright (c) 2010 by Ahmad M. Zawawi.
658  
659 This is free software; you can redistribute it and/or modify it under
660 the same terms as the Perl 5 programming language system itself.
661  
662 =cut
663  
664  
665 __END__