2 # ====================[ syntax-highlight.pl ]====================
6 syntax-highlight - An Oddmuse module for syntax highlighting of source
11 syntax-highlight is easily installable; move this file into the
12 B<wiki/modules/> directory for your Oddmuse Wiki.
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>.
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
;
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>.
37 my %NativeLanguageToKateLanguage =
39 ".desktop" => ".desktop",
41 "4GL-PER" => "4GL-PER",
44 "ANSI C89" => "ANSI C89",
46 "AVR Assembler" => "AVR Assembler",
51 "Apache Configuration"=> "Apache Configuration",
52 "Asm6502" => "Asm6502",
61 "CSS/PHP" => "CSS/PHP",
62 "CUE Sheet" => "CUE Sheet",
64 "ChangeLog" => "ChangeLog",
66 "Clipper" => "Clipper",
67 "ColdFusion" => "ColdFusion",
68 "Common Lisp" => "Common Lisp",
69 "Component-Pascal" => "Component-Pascal",
71 "Debian Changelog" => "Debian Changelog",
72 "Debian Control" => "Debian Control",
74 "Doxygen" => "Doxygen",
75 "E Language" => "E Language",
78 "Euphoria" => "Euphoria",
79 "Fortran" => "Fortran",
80 "FreeBASIC" => "FreeBASIC",
83 "GNU Assembler" => "GNU Assembler",
84 "GNU Gettext" => "GNU Gettext",
86 "Haskell" => "Haskell",
89 "INI Files" => "INI Files",
91 "Intel x86 (NASM)" => "Intel x86 (NASM)",
94 "JavaScript" => "JavaScript",
95 "JavaScript/PHP" => "JavaScript/PHP",
96 "Javadoc" => "Javadoc",
98 "Kate File Template" => "Kate File Template",
102 "Lex/Flex" => "Lex/Flex",
103 "LilyPond" => "LilyPond",
104 "Literate Haskell" => "Literate Haskell",
105 "Logtalk" => "Logtalk",
108 "MAB-DB" => "MAB-DB",
109 "MIPS Assembler" => "MIPS Assembler",
110 "Makefile" => "Makefile",
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",
123 "PicAsm" => "PicAsm",
125 "PostScript" => "PostScript",
126 "Prolog" => "Prolog",
127 "PureBasic" => "PureBasic",
128 "Python" => "Python",
129 "Quake Script" => "Quake Script",
130 "R Script" => "R Script",
132 "RPM Spec" => "RPM Spec",
133 "RSI IDL" => "RSI IDL",
134 "RenderMan RIB" => "RenderMan RIB",
138 "SQL (MySQL)" => "SQL (MySQL)",
139 "SQL (PostgreSQL)" => "SQL (PostgreSQL)",
141 "Sather" => "Sather",
142 "Scheme" => "Scheme",
146 "TI Basic" => "TI Basic",
147 "TaskJuggler" => "TaskJuggler",
148 "Tcl/Tk" => "Tcl/Tk",
149 "UnrealScript" => "UnrealScript",
152 "Velocity" => "Velocity",
153 "Verilog" => "Verilog",
154 "WINE Config" => "WINE Config",
155 "Wikimedia" => "Wikimedia",
157 "Yacc/Bison" => "Yacc/Bison",
160 "ferite" => "ferite",
162 "progress" => "progress",
163 "scilab" => "scilab",
164 "txt2tags" => "txt2tags",
165 "x.org Configuration" => "x.org Configuration",
166 "xHarbour" => "xHarbour",
172 new Syntax
::Highlight
::Engine
::Kate
179 # Oddmuse encloses src blocks within <pre>...</pre>.
181 # "\t" => " ",
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>"],
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>"],
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>.
239 sub HighlightSyntax
{
240 my ($quoted_src, $kateLang) = @_;
242 # No highlighter for current language
244 unless $syntax_hl->languagePlug($kateLang);
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/>.