Changeset 2187


Ignore:
Timestamp:
12/23/08 12:11:42 (3 years ago)
Author:
hjansen
Message:

Added HTML export feature

Location:
trunk/Padre-Plugin-PerlTidy
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Padre-Plugin-PerlTidy/Changes

    r1939 r2187  
    11Revision history for Perl extension Padre::Plugin::PerlTidy. 
     2 
     30.04 
     4    - Add HTML export feature (HJANSEN) 
    25 
    360.03  Mon Dec 15 2008 
  • trunk/Padre-Plugin-PerlTidy/lib/Padre/Plugin/PerlTidy.pm

    r1962 r2187  
    3030sub menu_plugins_simple { 
    3131    PerlTidy => [ 
    32         'Tidy the active document' => \&tidy_document, 
    33         'Tidy the selected text'   => \&tidy_selection, 
     32        Wx::gettext('Tidy the active document') => \&tidy_document, 
     33        Wx::gettext('Tidy the selected text')   => \&tidy_selection, 
     34        Wx::gettext('Export active document to HTML file') => \&export_document, 
     35        Wx::gettext('Export selected text to HTML file')   => \&export_selection, 
    3436    ]; 
    3537} 
     
    108110} 
    109111 
     112sub _get_filename { 
     113    my $self = shift; 
     114 
     115    my $doc     = $self->selected_document or return; 
     116    my $current = $doc->filename; 
     117    my $default_dir = ''; 
     118 
     119    if ( defined $current ) { 
     120        require File::Basename; 
     121        $default_dir = File::Basename::dirname($current); 
     122    } 
     123 
     124    require File::Spec; 
     125 
     126    while (1) { 
     127        my $dialog = Wx::FileDialog->new( 
     128            $self, 
     129            Wx::gettext("Save file as..."), 
     130            $default_dir, 
     131            $doc->filename . '.html', 
     132            "*.*", 
     133            Wx::wxFD_SAVE, 
     134        ); 
     135        if ( $dialog->ShowModal == Wx::wxID_CANCEL ) { 
     136            return; 
     137        } 
     138        my $filename = $dialog->GetFilename; 
     139        $default_dir = $dialog->GetDirectory; 
     140        my $path = File::Spec->catfile($default_dir, $filename); 
     141        if ( -e $path ) { 
     142            my $res = Wx::MessageBox( 
     143                Wx::gettext("File already exists. Overwrite it?"), 
     144                Wx::gettext("Exist"), 
     145                Wx::wxYES_NO, 
     146                $self, 
     147            ); 
     148            if ( $res == Wx::wxYES ) { 
     149                return $path; 
     150            } 
     151        } else { 
     152            return $path; 
     153        } 
     154    } 
     155} 
     156 
     157sub _export { 
     158    my ( $self, $src ) = @_; 
     159 
     160    require Perl::Tidy; 
     161 
     162    return unless defined $src; 
     163 
     164    my $doc = $self->selected_document; 
     165 
     166    if ( !$doc->isa( 'Padre::Document::Perl' ) ) { 
     167        return Wx::MessageBox( 'Document is not a Perl document', 
     168            "Error", Wx::wxOK | Wx::wxCENTRE, $self ); 
     169    } 
     170 
     171    my $filename = _get_filename($self); 
     172 
     173    return unless defined $filename; 
     174 
     175    my ( $output, $error ); 
     176 
     177    # TODO: suppress the senseless warning from PerlTidy 
     178    eval { 
     179        Perl::Tidy::perltidy( 
     180            argv        => \'-html -nnn', 
     181            source      => \$src, 
     182            destination => $filename, 
     183            errorfile   => \$error, 
     184        ); 
     185    }; 
     186 
     187    if ( $@ ) { 
     188        my $error_string = $@; 
     189        Wx::MessageBox( 
     190            $error_string, 
     191            "PerlTidy Error", 
     192            Wx::wxOK | Wx::wxCENTRE, $self 
     193        ); 
     194        return; 
     195    } 
     196 
     197    if ( defined $error ) { 
     198        my $width = length( $doc->filename ) + 2; 
     199        $self->{ gui }->{ output_panel }->AppendText( 
     200            "\n\n" . "-" x $width . "\n" . $doc->filename . "\n" . "-" x $width . "\n" ); 
     201        $self->{ gui }->{ output_panel }->AppendText( "$error\n" ); 
     202        $self->{ gui }->{ output_panel }->select; 
     203    } 
     204 
     205    return; 
     206} 
     207 
     208sub export_selection { 
     209    my ( $self, $event ) = @_; 
     210    my $src = $self->selected_text; 
     211 
     212    _export( $self, $src ); 
     213    return; 
     214} 
     215 
     216sub export_document { 
     217    my ( $self, $event ) = @_; 
     218 
     219    my $doc = $self->selected_document; 
     220    my $src = $doc->text_get; 
     221 
     222    _export( $self, $src ); 
     223    return; 
     224} 
     225 
     226 
    110227=head1 INSTALLATION 
    111228 
Note: See TracChangeset for help on using the changeset viewer.