wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / syntax-highlight.pl
blobdef6a50e6ffd814dfbc83556f5e237d1c81f703a
1 #!/usr/bin/env perl
2 # ====================[ syntax-highlight.pl ]====================
4 =head1 NAME
6 syntax-highlight - An Oddmuse module for syntax highlighting of source
7 snippets.
9 =head1 INSTALLATION
11 syntax-highlight is easily installable; move this file into the
12 B<wiki/modules/> directory for your Oddmuse Wiki.
14 =head1 DEPENDENCIES
16 This module relies on L<Syntax::Highlight::Engine::Kate> to do the
17 actual coloring. On a Debian, the Kate syntax highlighter is
18 available as package C<libsyntax-highlight-engine-kate-perl>.
20 =cut
21 package OddMuse;
23 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/syntax-highlight.pl">syntax-highlight.pl</a>, see <a href="http://www.oddmuse.org/wiki/Syntax_Highlighting">Syntax Highlight Extension</a></p>';
25 use Syntax::Highlight::Engine::Kate;
27 =head1 VARIABLES
29 =head2 %NativeLanguageToKateLanguage
31 Languages supported by the Kate syntax highlighter. Use the value of
32 this variable as a reference to setup language mapper for your markup
33 module. See C<HighlightSyntax>.
35 =cut
37 my %NativeLanguageToKateLanguage =
39 ".desktop" => ".desktop",
40 "4GL" => "4GL",
41 "4GL-PER" => "4GL-PER",
42 "ABC" => "ABC",
43 "AHDL" => "AHDL",
44 "ANSI C89" => "ANSI C89",
45 "ASP" => "ASP",
46 "AVR Assembler" => "AVR Assembler",
47 "AWK" => "AWK",
48 "Ada" => "Ada",
49 "Alerts" => "Alerts",
50 "Ansys" => "Ansys",
51 "Apache Configuration"=> "Apache Configuration",
52 "Asm6502" => "Asm6502",
53 "Bash" => "Bash",
54 "BibTeX" => "BibTeX",
55 "C" => "C",
56 "C#" => "C#",
57 "C++" => "C++",
58 "CGiS" => "CGiS",
59 "CMake" => "CMake",
60 "CSS" => "CSS",
61 "CSS/PHP" => "CSS/PHP",
62 "CUE Sheet" => "CUE Sheet",
63 "Cg" => "Cg",
64 "ChangeLog" => "ChangeLog",
65 "Cisco" => "Cisco",
66 "Clipper" => "Clipper",
67 "ColdFusion" => "ColdFusion",
68 "Common Lisp" => "Common Lisp",
69 "Component-Pascal" => "Component-Pascal",
70 "D" => "D",
71 "Debian Changelog" => "Debian Changelog",
72 "Debian Control" => "Debian Control",
73 "Diff" => "Diff",
74 "Doxygen" => "Doxygen",
75 "E Language" => "E Language",
76 "Eiffel" => "Eiffel",
77 "Email" => "Email",
78 "Euphoria" => "Euphoria",
79 "Fortran" => "Fortran",
80 "FreeBASIC" => "FreeBASIC",
81 "GDL" => "GDL",
82 "GLSL" => "GLSL",
83 "GNU Assembler" => "GNU Assembler",
84 "GNU Gettext" => "GNU Gettext",
85 "HTML" => "HTML",
86 "Haskell" => "Haskell",
87 "IDL" => "IDL",
88 "ILERPG" => "ILERPG",
89 "INI Files" => "INI Files",
90 "Inform" => "Inform",
91 "Intel x86 (NASM)" => "Intel x86 (NASM)",
92 "JSP" => "JSP",
93 "Java" => "Java",
94 "JavaScript" => "JavaScript",
95 "JavaScript/PHP" => "JavaScript/PHP",
96 "Javadoc" => "Javadoc",
97 "KBasic" => "KBasic",
98 "Kate File Template" => "Kate File Template",
99 "LDIF" => "LDIF",
100 "LPC" => "LPC",
101 "LaTeX" => "LaTeX",
102 "Lex/Flex" => "Lex/Flex",
103 "LilyPond" => "LilyPond",
104 "Literate Haskell" => "Literate Haskell",
105 "Logtalk" => "Logtalk",
106 "Lua" => "Lua",
107 "M3U" => "M3U",
108 "MAB-DB" => "MAB-DB",
109 "MIPS Assembler" => "MIPS Assembler",
110 "Makefile" => "Makefile",
111 "Mason" => "Mason",
112 "Matlab" => "Matlab",
113 "Modula-2" => "Modula-2",
114 "Music Publisher" => "Music Publisher",
115 "Objective Caml" => "Objective Caml",
116 "Objective-C" => "Objective-C",
117 "Octave" => "Octave",
118 "PHP (HTML)" => "PHP (HTML)",
119 "PHP/PHP" => "PHP/PHP",
120 "POV-Ray" => "POV-Ray",
121 "Pascal" => "Pascal",
122 "Perl" => "Perl",
123 "PicAsm" => "PicAsm",
124 "Pike" => "Pike",
125 "PostScript" => "PostScript",
126 "Prolog" => "Prolog",
127 "PureBasic" => "PureBasic",
128 "Python" => "Python",
129 "Quake Script" => "Quake Script",
130 "R Script" => "R Script",
131 "REXX" => "REXX",
132 "RPM Spec" => "RPM Spec",
133 "RSI IDL" => "RSI IDL",
134 "RenderMan RIB" => "RenderMan RIB",
135 "Ruby" => "Ruby",
136 "SGML" => "SGML",
137 "SML" => "SML",
138 "SQL (MySQL)" => "SQL (MySQL)",
139 "SQL (PostgreSQL)" => "SQL (PostgreSQL)",
140 "SQL" => "SQL",
141 "Sather" => "Sather",
142 "Scheme" => "Scheme",
143 "Sieve" => "Sieve",
144 "Spice" => "Spice",
145 "Stata" => "Stata",
146 "TI Basic" => "TI Basic",
147 "TaskJuggler" => "TaskJuggler",
148 "Tcl/Tk" => "Tcl/Tk",
149 "UnrealScript" => "UnrealScript",
150 "VHDL" => "VHDL",
151 "VRML" => "VRML",
152 "Velocity" => "Velocity",
153 "Verilog" => "Verilog",
154 "WINE Config" => "WINE Config",
155 "Wikimedia" => "Wikimedia",
156 "XML" => "XML",
157 "Yacc/Bison" => "Yacc/Bison",
158 "de_DE" => "de_DE",
159 "en_US" => "en_US",
160 "ferite" => "ferite",
161 "nl" => "nl",
162 "progress" => "progress",
163 "scilab" => "scilab",
164 "txt2tags" => "txt2tags",
165 "x.org Configuration" => "x.org Configuration",
166 "xHarbour" => "xHarbour",
167 "xslt" => "xslt",
168 "yacas" => "yacas"
171 my $syntax_hl =
172 new Syntax::Highlight::Engine::Kate
173 (language => 'Perl',
174 substitutions =>
176 "<" => "&lt;",
177 ">" => "&gt;",
178 "&" => "&amp;",
179 # Oddmuse encloses src blocks within <pre>...</pre>.
180 # " " => "&nbsp;",
181 # "\t" => "&nbsp;&nbsp;&nbsp;",
182 # "\n" => "<br/>\n",
184 format_table =>
186 Alert => ["<span style=\"color: #0000ff\">" , "</span>"],
187 BaseN => ["<span style=\"color: #007f00\">" , "</span>"],
188 # BString => ["<span style=\"color: #c9a7ff\">" , "</span>"],
189 BString => ["" , ""],
190 Char => ["<span style=\"color: #ff00ff\">" , "</span>"],
191 Comment => ["<span style=\"color: #7f7f7f\"><em>" , "</em></span>"],
192 DataType => ["<span style=\"color: #0000ff\">" , "</span>"],
193 DecVal => ["<span style=\"color: #00007f\">" , "</span>"],
194 Error => ["<span style=\"color: #ff0000\"><strong><em>", "</em></strong></span>"],
195 Float => ["<span style=\"color: #00007f\">" , "</span>"],
196 Function => ["<span style=\"color: #007f00\">" , "</span>"],
197 IString => ["<span style=\"color: #ff0000\">" , "<//span>"],
198 Keyword => ["<strong>" , "</strong>"],
199 Normal => ["" , ""],
200 Operator => ["<span style=\"color: #ffa500\">" , "</span>"],
201 Others => ["<span style=\"color: #b03060\">" , "</span>"],
202 RegionMarker=> ["<span style=\"color: #96b9ff\"><em>" , "</em></span>"],
203 Reserved => ["<span style=\"color: #9b30ff\"><strong>" , "</strong></span>"],
204 String => ["<span style=\"color: #ff0000\">" , "</span>"],
205 Variable => ["<span style=\"color: #0000ff\"><strong>" , "</strong></span>"],
206 Warning => ["<span style=\"color: #0000ff\"><strong><em>", "</strong></em></span>"],
210 =head1 FUNCTIONS
212 =head2 HighlightSyntax($src, $kateLang)
214 Markup C<$quoted_src> in C<$kateLang> and return the resulting HTML.
215 If C<$kateLang> is unknown to Kate, return C<$quoted_src>. Assume
216 C<$quoted_src> as quoted. Unquote the source block with
217 C<UnquoteHtml> before passing it to Kate for highlighting.
219 Before using this function, you may have to setup up a hash that maps
220 from the name of the language as it is designated by your markup to
221 the how the language itself is called by Kate. Use
222 C<%NativeLanguageToKateLanguage> as a template to setup this hash.
224 Invoke C<HighlightSyntax> as below.
226 my %OrgLanguageToKateLanguage = ("perl"=>"Perl");
227 # Do Syntax Highlighting
228 if (defined(&HighlightSyntax)) {
229 $lines = HighlightSyntax($src, $OrgLanguageToKateLanguage{$lang});
232 Here, the hash C<%OrgLanguageToKateLanguage> maps the language from
233 Emacs/Org-mode to Kate. As seen above, C<Perl> blocks in
234 Emacs/Org-mode markup are designated by C<perl> while in Kate they are
235 designated by C<Perl>.
237 =cut
239 sub HighlightSyntax {
240 my ($quoted_src, $kateLang) = @_;
242 # No highlighter for current language
243 return $quoted_src
244 unless $syntax_hl->languagePlug($kateLang);
246 # Use highlighter
247 my $unquoted_src = UnquoteHtml($quoted_src);
248 $syntax_hl->language($kateLang);
249 return $syntax_hl->highlightText($unquoted_src);
252 =head1 COPYRIGHT AND LICENSE
254 Copyright 2013 by Jambunathan K <kjambunathan@gmail.com>.
256 This program is free software; you can redistribute it and/or modify
257 it under the terms of the GNU General Public License as published by
258 the Free Software Foundation; either version 3 of the License, or
259 (at your option) any later version.
261 This program is distributed in the hope that it will be useful,
262 but WITHOUT ANY WARRANTY; without even the implied warranty of
263 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
264 GNU General Public License for more details.
266 You should have received a copy of the GNU General Public License
267 along with this program. If not, see L<http://www.gnu.org/licenses/>.
269 =cut