1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
package Highlighting;
# This file is part of the pnopaste program
# Copyright (C) 2008-2021 Patrick Matthäi <pmatthaei@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
use warnings;
no warnings 'once';
use strict;
use Syntax::Highlight::Engine::Kate;
use conf::Syntax_Languages;
# Language object.
my $Language = '';
### Sets the language object.
sub Language {
my($Lang) = @_;
if(Is_Valid($Lang)){
$Language = $Lang;
return 1;
}
return 0;
}
### Checks if the given language is available.
sub Is_Valid {
my($Check) = @_;
if(!$Check){
# No value given, invalid.
return 0;
}
elsif($Syntax_Languages::Langs{$Check} == 1){
# Valid.
return 1;
}
return 0;
}
### Gets the available languages.
sub Get_Languages {
my @List;
while(my($Code_Lang, $Code_Enable) = each(%Syntax_Languages::Langs)){
if($Code_Enable){
# Just add activated languages.
push(@List, $Code_Lang);
}
}
return @List;
}
### Creates the highlighting using the Kate module.
sub Highlight {
my($Code) = @_;
if(!$Language){
return 'No language defined.';
}
my $hl = new Syntax::Highlight::Engine::Kate(
language => $Language,
substitutions => {
"<" => "<",
">" => ">",
"&" => "&",
"\t" => " ",
# All hail to <pre>! (ToDo: Does this work with trailing spaces?)
# " " => " ",
# "\t" => " ",
"\r" => "",
"\n" => "\n",
},
format_table => {
Alert => ["<span class=\"hl_Alert\">", "</span>"],
BaseN => ["<span class=\"hl_BaseN\">", "</span>"],
BString => ["<span class=\"hl_BString\">", "</span>"],
Char => ["<span class=\"hl_Char\">", "</span>"],
Comment => ["<span class=\"hl_Comment\">", "</span>"],
DataType => ["<span class=\"hl_DataType\">", "</span>"],
DecVal => ["<span class=\"hl_DecVal\">", "</span>"],
Error => ["<span class=\"hl_Error\">", "</span>"],
Float => ["<span class=\"hl_Float\">", "</span>"],
Function => ["<span class=\"hl_Function\">", "</span>"],
IString => ["<span class=\"hl_IString\">", "</span>"],
Keyword => ["<span class=\"hl_Keyword\">", "</span>"],
Normal => ["", ""],
Operator => ["<span class=\"hl_Operator\">", "</span>"],
Others => ["<span class=\"hl_Others\">", "</span>"],
RegionMarker => ["<span class=\"hl_RegionMarker\">", "</span>"],
Reserved => ["<span class=\"hl_Reserved\">", "</span>"],
String => ["<span class=\"hl_String\">", "</span>"],
Variable => ["<span class=\"hl_Variable\">", "</span>"],
Warning => ["<span class=\"hl_Warning\">", "</span>"],
},
);
$Code = $hl->highlightText($Code);
return $Code;
}
### Creates the time list with option tags for HTML.
sub Build_Syntax_List_HTML {
my @List = Get_Languages();
my $String = '';
@List = sort(@List);
foreach my $Tmp (@List){
if($Tmp eq $Syntax_Languages::Selected){
$String .= '<option selected="selected">' . $Tmp . '</option>' . "\n";
} else {
$String .= '<option>' . $Tmp . '</option>' . "\n";
}
}
return $String;
}
1;
|