From dd5a4c4c528c4aab71b336d8dc4f5827cfbc9e0c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 2 May 2009 15:01:05 -0500 Subject: initial import --- lib/IkiWiki/Plugin/highlight.pm | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 lib/IkiWiki/Plugin/highlight.pm diff --git a/lib/IkiWiki/Plugin/highlight.pm b/lib/IkiWiki/Plugin/highlight.pm new file mode 100644 index 0000000..f2de12d --- /dev/null +++ b/lib/IkiWiki/Plugin/highlight.pm @@ -0,0 +1,93 @@ +#!/usr/bin/perl +use strict; +use warnings; +package IkiWiki::Plugin::highlight; +use IkiWiki '3.00'; +use Syntax::Highlight::Engine::Kate; + +# variables {{{ +my %hl_args = ( + substitutions => { }, + format_table => { + Alert => ["", ""], + BaseN => ["", ""], + BString => ["", ""], + Char => ["", ""], + Comment => ["", ""], + DataType => ["", ""], + DecVal => ["", ""], + Error => ["", ""], + Float => ["", ""], + Function => ["", ""], + IString => ["", ""], + Keyword => ["", ""], + Normal => ["", "" ], + Operator => ["", ""], + Others => ["", ""], + RegionMarker => ["", ""], + Reserved => ["", ""], + String => ["", ""], + Variable => ["", ""], + Warning => ["", ""], + }, +); +my %syntaxes; +# }}} + +sub import { # {{{ + hook(type => 'sanitize', id => __PACKAGE__, call => \&sanitize, first => 1); +} # }}} + +sub sanitize { # {{{ + my %args = @_; + my $content = ''; + my $in_code = 0; + for my $line (split /^/m, $args{content}) { + if ($in_code) { + if ($line =~ s{(.*)}{}s) { + my $rest_line = $1; + $code_block .= $line; + $content .= highlight($code_block) . $rest_line; + $code_block = ''; + $in_code = 0; + } + else { + $code_block .= $line; + } + } + else { + if ($line =~ s{(.*
)}{}s) {
+                $content .= $1;
+                $code_block .= $line;
+                $in_code = 1;
+            }
+            else {
+                $content .= $line;
+            }
+        }
+    }
+    $content .= $code_block;
+    return $content;
+} # }}}
+
+sub highlight { # {{{
+    my ($code_block) = @_;
+    my $filetype = guess_filetype($code_block);
+    return unless $filetype;
+    my $hl = Syntax::Highlight::Engine::Kate->new(
+        language => $filetype, %hl_args
+    );
+    %syntaxes ||= map { lc($_) => $hl->syntaxes->{$_} } keys %$hl->syntaxes;
+    return $hl->highlightText($code_block);
+} # }}}
+
+sub guess_filetype { # {{{
+    my ($code_block) = @_;
+    my ($first_line) = split /\n/, $code_block;
+    if ($first_line =~ /^#!(\w+)/) {
+        return $syntaxes{$1};
+    }
+    return;
+} # }}}
+
+1;
-- 
cgit v1.2.3