| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Padre::Plugin::JSONUtilities - Adds buttons to beautify or compress json. | ||
| 4 | |||
| 5 | =cut | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | Padre::Plugin::JSONUtilities is designed to compress or expand JSON. Enough said. Also to canonically sort, if you want. | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | package Padre::Plugin::JSONUtilities; | ||
| 14 | |||
| 15 | use 5.010; | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | use utf8; | ||
| 19 | use Padre::Plugin(); | ||
| 20 | use Padre::Role::Task; | ||
| 21 | |||
| 22 | our $VERSION = '0.10'; | ||
| 23 | use base qw(Padre::Plugin); | ||
| 24 | |||
| 25 | sub padre_interfaces { | ||
| 26 | return ( | ||
| 27 | 'Padre::Plugin' => 0.94, | ||
| 28 | 'Padre::Constant' => 0.94, | ||
| 29 | 'Padre::Unload' => 0.94, | ||
| 30 | ); | ||
| 31 | } | ||
| 32 | |||
| 33 | use constant CHILDREN => qw{Padre::Plugin::JSONUtilities}; | ||
| 34 | |||
| 35 | sub plugin_name { return Wx::gettext('JSON Utility Plugin'); } | ||
| 36 | |||
| 37 | use Padre::Wx::Dialog::OpenResource; | ||
| 38 | use Padre::Wx (); | ||
| 39 | use JSON; | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | sub plugin_enable { | ||
| 44 | my ($self) = @_; | ||
| 45 | $self->{canonicalize} = 1; | ||
| 46 | return $self->SUPER::plugin_enable; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub canonicalize { $_[0]->{canonicalize} = $_[1] if @_ > 1; return $_[0]->{canonicalize}; } | ||
| 50 | |||
| 51 | sub beautify { | ||
| 52 | my ($self) = @_; | ||
| 53 | my $document = Padre::Current->document; | ||
| 54 | my $json = JSON->new->canonical($self->canonicalize)->utf8->pretty; | ||
| 55 | my $data = eval { $json->decode($document->text_get) }; | ||
| 56 | if (my $exp = $@) { | ||
| 57 | Padre::Current->main->error("Error parsing JSON: $exp."); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | $document->text_set($json->encode($data)); | ||
| 61 | Padre::Current->editor->GotoPos(0); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub compress { | ||
| 65 | my ($self) = @_; | ||
| 66 | my $document = Padre::Current->document; | ||
| 67 | my $json = JSON->new->canonical($self->canonicalize)->utf8; | ||
| 68 | my $data = eval { $json->decode($document->text_get) }; | ||
| 69 | if (my $exp = $@) { | ||
| 70 | Padre::Current->main->error("Error parsing JSON: $exp."); | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | $document->text_set($json->encode($data)); | ||
| 74 | Padre::Current->editor->GotoPos(0); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub menu_plugins_simple { | ||
| 78 | my $self = shift; | ||
| 79 | return $self->plugin_name => [ | ||
| 80 | 'About' => sub { $self->show_about }, | ||
| 81 | "JSON Beautify\tCtrl+Shift+D" => sub { | ||
| 82 | $self->beautify; | ||
| 83 | }, | ||
| 84 | "JSON Compress\tCtrl+Shift+S" => sub { | ||
| 85 | $self->compress; | ||
| 86 | } | ||
| 87 | ]; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub plugin_icon { | ||
| 91 | require Padre::Wx::Icon; | ||
| 92 | |||
| 93 | require File::ShareDir; | ||
| 94 | my $sharedir = File::ShareDir::dist_dir("Padre-Plugin-JSONUtilities"); | ||
| 95 | my $file = File::Spec->catfile( | ||
| 96 | $sharedir, | ||
| 97 | "logo" | ||
| 98 | ) . ".png"; | ||
| 99 | return Padre::Wx::Icon::find('logo') unless -f $file; | ||
| 100 | my $image = Wx::Bitmap->new($file, Wx::BITMAP_TYPE_PNG ); | ||
| 101 | my $icon = Wx::Icon->new; | ||
| 102 | $icon->CopyFromBitmap($image); | ||
| 103 | return $icon; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub show_about { | ||
| 107 | my $self = shift; | ||
| 108 | my $about = Wx::AboutDialogInfo->new; | ||
| 109 | $about->SetName('JSON Utilities Plugin'); | ||
| 110 | $about->SetDescription( <<"END_MESSAGE" ); | ||
| 111 | |||
| 112 | Plugin designed to make handling JSON easier. | ||
| 113 | |||
| 114 | END_MESSAGE | ||
| 115 | Wx::AboutBox($about); | ||
| 116 | |||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub plugin_disable { | ||
| 121 | my $self = shift; | ||
| 122 | for my $package (CHILDREN) { | ||
| 123 | require Padre::Unload; | ||
| 124 | Padre::Unload->unload($package); | ||
| 125 | } | ||
| 126 | $self->SUPER::plugin_disable(@_); | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Adam Harrison <adamdharrison@gmail.com> | ||
| 135 | |||
| 136 | =cut |