wiki.pl: Port some fixes from upstream
[Orgmuse.git] / modules / beautify.pl
bloba1e0afbe9c9a6c342ac1e161d181a8acf5e4480a
1 # Copyright (C) 2004 Xavier Maillard
2 # Copyright (C) 2004 Alex Schroeder <alex@emacswiki.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the
16 # Free Software Foundation, Inc.
17 # 59 Temple Place, Suite 330
18 # Boston, MA 02111-1307 USA
20 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/beautify.pl">beautify.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/Source_Code_Beautification_Extension">Source Code Beautification Extension</a></p>';
22 use Beautifier::Core;
23 use Output::HTML;
25 push(@MyRules, \&BeautificationRule);
27 sub BeautificationRule {
28 if ($bol and m/\G&lt;source\s+([a-zA-Z0-9]+)\s*&gt;\n?(.*?\n)&lt;\/source&gt;[ \t]*\n?/cgs) {
29 my $old_ = $_;
30 my $oldpos = pos;
31 my $lang = $1;
32 my $source= $2;
33 my $result = $source;
34 eval { $result = Beautify($lang, $source); };
35 $result = $@ if $@;
36 $_ = $old_;
37 pos = $oldpos;
38 return CloseHtmlEnvironments() . $q->div({-class=>'beauty'}, $result) . AddHtmlEnvironment('p');
40 return undef;
43 sub Beautify {
44 my ($lang, $source) = @_;
45 eval "use HFile::HFile_$lang";
46 my $Hfile = eval "new HFile::HFile_$lang";
47 return $q->strong(Ts('Cannot highlight the language %s.', $lang)) . "\n\n" . $source if $@;
48 my $highlighter = new Beautifier::Core($Hfile, new Output::HTML);
49 return $highlighter->highlight_text(UnquoteHtml($source));