"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/web_plugins/movabletype/highlight.pl" (12 May 2020, 2803 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #Plugin Name: Highlight
    2 #Plugin URI: http://www.andre-simon.de
    3 #Description: Plugin which uses the highlight utility to generate coloured source code
    4 #Author: André  Simon
    5 #Version: 1.2
    6 #Author URI: http://www.andre-simon.de/
    7 
    8 #This file is part of Highlight.
    9 #
   10 #Highlight is free software: you can redistribute it and/or modify
   11 #it under the terms of the GNU General Public License as published by
   12 #the Free Software Foundation, either version 3 of the License, or
   13 #(at your option) any later version.
   14 #
   15 #Highlight is distributed in the hope that it will be useful,
   16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
   17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   18 #GNU General Public License for more details.
   19 #
   20 #You should have received a copy of the GNU General Public License
   21 #along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   22 
   23 package MT::Plugin::Highlight;
   24 
   25 use strict;
   26 use IPC::Open3;
   27 use MT;
   28 use MT::Entry;
   29 use MT::Plugin;
   30 use MT::Template::Context;
   31 use MT::WeblogPublisher;
   32 
   33 my $plugin = new MT::Plugin();
   34 $plugin->name("MT Highlight");
   35 $plugin->description('Source code highlighter using highlight (http://www.andre-simon.de)');
   36 $plugin->doc_link('http://wiki.andre-simon.de/');
   37 $plugin->author_name('Andre Simon');
   38 $plugin->author_link('http://www.andre-simon.de/');
   39 $plugin->plugin_link('http://wiki.andre-simon.de/');
   40 $plugin->version('1.3');
   41 
   42 MT->add_callback("BuildPage", 1, $plugin, \&highlight_callback);
   43 MT->add_plugin($plugin);
   44 
   45 sub highlight_callback
   46 {
   47    my ($cb, %args) = @_;
   48    use Data::Dumper;
   49    my $html = ${$args{'Content'}};
   50    $html =~ s/<highlight([^>]*?)lang="([^"]+?)"([^>]*?)>(.*?)<\/highlight>/highlight_code($2,$4)/ges;
   51    ${$args{'Content'}} = $html;
   52    return 1;
   53 }
   54 
   55 sub highlight_code
   56 {
   57    my ($lang, $source) = @_;
   58 
   59    $lang =~ s/[^a-zA-Z]//g;   # delete special chars in user input
   60    $source =~ s/\<br \/\>//g; # get rid of <br> Tags inserted by MT
   61    $source =~ s/\<\/?p\>//g;  # MT inserts <p> Tags if <, > are present in input code
   62 
   63    local(*HIS_IN, *HIS_OUT, *HIS_ERR);
   64 
   65    my @hl_args = ('-f', "-S$lang");
   66    push (@hl_args, '--inline-css'); # use inline css definitions
   67    push (@hl_args, '-sedit-kwrite');     # coloring theme
   68    push (@hl_args, '-l');    # linenumbers
   69    push (@hl_args, '-j2');   # linenumber length
   70    push (@hl_args, '-z');    # linenumber zeroes
   71    push (@hl_args, '-Fgnu'); # reformat
   72    push (@hl_args, '-t4');   # replace tabs
   73    my $childpid = IPC::Open3::open3(*HIS_IN, *HIS_OUT, *HIS_ERR, 'highlight', @hl_args);
   74    print HIS_IN $source;
   75    close(HIS_IN);            # Give end of file to kid.
   76    my @outlines = <HIS_OUT>; # Read till EOF.
   77 
   78    close HIS_OUT;
   79    close HIS_ERR;
   80    waitpid($childpid, 0);
   81 
   82    my $htext = join "", @outlines;
   83 
   84    return "<pre>".$htext."</pre>";
   85 }
   86 
   87 
   88