package Padre::Plugin::HTMLExport; use 5.006; use warnings; use strict; our $VERSION = '0.05'; use File::Basename (); use base 'Padre::Plugin'; use Padre::Wx (); use Wx::Locale qw(:default); our %KATE_ALL = ( 'text/x-adasrc' => 'Ada', 'text/asm' => 'Asm6502', 'text/x-c++src' => 'Cplusplus', 'text/css' => 'CSS', 'text/x-patch' => 'Diff', 'text/eiffel' => 'Eiffel', 'text/x-fortran' => 'Fortran', 'text/html' => 'HTML', 'text/ecmascript' => 'JavaScript', 'text/latex' => 'LaTeX', 'text/lisp' => 'Common_Lisp', 'text/lua' => 'Lua', 'text/x-makefile' => 'Makefile', 'text/matlab' => 'Matlab', 'text/x-pascal' => 'Pascal', 'application/x-perl' => 'Perl', 'text/x-python' => 'Python', 'application/x-php' => 'PHP_PHP', 'application/x-ruby' => 'Ruby', 'text/x-sql' => 'SQL', 'text/x-tcl' => 'Tcl_Tk', 'text/vbscript' => 'JavaScript', 'text/xml' => 'XML', ); sub padre_interfaces { 'Padre::Plugin' => '0.23', } sub menu_plugins_simple { my $self = shift; return ('Export Colorful HTML' => [ 'Export HTML', sub { $self->export_html }, 'Configure Color', sub { $self->configure_color }, ]); } sub export_html { my ( $self ) = @_; my $main = $self->main; my $doc = $main->current->document or return; my $current = $doc->filename; my $default_dir; if ( defined $current ) { $default_dir = File::Basename::dirname($current); } # ask where to save my $save_to_file; while (1) { my $dialog = Wx::FileDialog->new( $main, gettext("Save html as..."), $default_dir, "", "*.*", Wx::wxFD_SAVE, ); if ( $dialog->ShowModal == Wx::wxID_CANCEL ) { return 0; } my $filename = $dialog->GetFilename; $default_dir = $dialog->GetDirectory; my $path = File::Spec->catfile($default_dir, $filename); if ( -e $path ) { my $res = Wx::MessageBox( gettext("File already exists. Overwrite it?"), gettext("Exist"), Wx::wxYES_NO, $main, ); if ( $res == Wx::wxYES ) { $save_to_file = $path; last; } } else { $save_to_file = $path; last; } } # highlight my $mimetype = $doc->get_mimetype; unless ( exists $KATE_ALL{$mimetype} ) { $main->error("$mimetype is not supported"); return; } my $language = $KATE_ALL{$mimetype}; require Syntax::Highlight::Engine::Kate; my $hl = Syntax::Highlight::Engine::Kate->new( language => $language, substitutions => { "<" => "<", ">" => ">", "&" => "&", " " => " ", "\t" => "   ", "\n" => "
\n", }, format_table => { Alert => ["", ""], BaseN => ["", ""], BString => ["", ""], Char => ["", ""], Comment => ["", ""], DataType => ["", ""], DecVal => ["", ""], Error => ["", ""], Float => ["", ""], Function => ["", ""], IString => ["", ""], Keyword => ["", ""], Normal => ["", ""], Operator => ["", ""], Others => ["", ""], RegionMarker => ["", ""], Reserved => ["", ""], String => ["", ""], Variable => ["", ""], Warning => ["", ""], }, ); my $title = 'Highlight ' . $doc->filename . ' By Padre::Plugin::HTML::Export'; my $code = $doc->text_get; my $output = "\n\n$title\n\n\n"; $output .= $hl->highlightText($code); $output .= "\n\n"; open(my $fh, '>', $save_to_file); print $fh $output; close($fh); my $ret = Wx::MessageBox( "Saved to $save_to_file. Do you want to open it now?", gettext("Done"), Wx::wxYES_NO|Wx::wxCENTRE, $main, ); if ( $ret == Wx::wxYES ) { Wx::LaunchDefaultBrowser($save_to_file); } } sub configure_color { my ( $self ) = @_; my $main = $self->main; $main->error('Not implemented, TODO'); } 1; __END__ =head1 NAME Padre::Plugin::HTMLExport - export highlighted HTML in Padre =head1 SYNOPSIS $>padre Plugins -> Export Colorful HTML -> Export HTML Configure Color =head1 DESCRIPTION Export a HTML page by using L =head1 AUTHOR Fayland Lam, C<< >> =head1 COPYRIGHT & LICENSE Copyright 2008 Fayland Lam, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut