allow format to use any language supported by highlight
format: Provide a htmlizefallback hook that other plugins can use to handle formats that are not suitable for general-purpose htmlize hooks. highlight: Use the hook to allow formatting of any language/extension, without it needing to be enabled for standalone source files. highlight: If the highlight perl binding is not available, fallback safely to a passthrough mode.master
parent
cdd1b58b38
commit
47298b01c1
|
@ -10,21 +10,33 @@ sub import {
|
||||||
}
|
}
|
||||||
|
|
||||||
sub preprocess (@) {
|
sub preprocess (@) {
|
||||||
my $format=$_[0];
|
|
||||||
shift; shift;
|
|
||||||
my $text=$_[0];
|
|
||||||
shift; shift;
|
|
||||||
my %params=@_;
|
my %params=@_;
|
||||||
|
my $format=shift;
|
||||||
|
shift;
|
||||||
|
my $text=IkiWiki::preprocess($params{page}, $params{destpage}, shift);
|
||||||
|
shift;
|
||||||
|
|
||||||
if (! defined $format || ! defined $text) {
|
if (! defined $format || ! defined $text) {
|
||||||
error(gettext("must specify format and text"));
|
error(gettext("must specify format and text"));
|
||||||
}
|
}
|
||||||
elsif (! exists $IkiWiki::hooks{htmlize}{$format}) {
|
elsif (exists $IkiWiki::hooks{htmlize}{$format}) {
|
||||||
|
return IkiWiki::htmlize($params{page}, $params{destpage},
|
||||||
|
$format, $text);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# Other plugins can register htmlizefallback
|
||||||
|
# hooks to add support for page types
|
||||||
|
# not suitable for htmlize. Try them until
|
||||||
|
# one succeeds.
|
||||||
|
my $ret;
|
||||||
|
IkiWiki::run_hooks(htmlizefallback => sub {
|
||||||
|
$ret=shift->($format, $text)
|
||||||
|
unless defined $ret;
|
||||||
|
});
|
||||||
|
return $ret if defined $ret;
|
||||||
|
|
||||||
error(sprintf(gettext("unsupported page format %s"), $format));
|
error(sprintf(gettext("unsupported page format %s"), $format));
|
||||||
}
|
}
|
||||||
|
|
||||||
return IkiWiki::htmlize($params{page}, $params{destpage}, $format,
|
|
||||||
IkiWiki::preprocess($params{page}, $params{destpage}, $text));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1
|
1
|
||||||
|
|
|
@ -4,7 +4,6 @@ package IkiWiki::Plugin::highlight;
|
||||||
use warnings;
|
use warnings;
|
||||||
use strict;
|
use strict;
|
||||||
use IkiWiki 3.00;
|
use IkiWiki 3.00;
|
||||||
use highlight;
|
|
||||||
|
|
||||||
# locations of highlight's files
|
# locations of highlight's files
|
||||||
my $filetypes="/etc/highlight/filetypes.conf";
|
my $filetypes="/etc/highlight/filetypes.conf";
|
||||||
|
@ -13,6 +12,9 @@ my $langdefdir="/usr/share/highlight/langDefs";
|
||||||
sub import {
|
sub import {
|
||||||
hook(type => "getsetup", id => "highlight", call => \&getsetup);
|
hook(type => "getsetup", id => "highlight", call => \&getsetup);
|
||||||
hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
|
hook(type => "checkconfig", id => "highlight", call => \&checkconfig);
|
||||||
|
# this hook is used by the format plugin
|
||||||
|
hook(type => "htmlizefallback", id => "highlight", call =>
|
||||||
|
\&htmlizefallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub getsetup () {
|
sub getsetup () {
|
||||||
|
@ -59,6 +61,17 @@ sub checkconfig () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub htmlizefallback {
|
||||||
|
my $format=lc shift;
|
||||||
|
my $langfile=ext2langfile($format);
|
||||||
|
|
||||||
|
if (! defined $langfile) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return highlight($langfile, shift);
|
||||||
|
}
|
||||||
|
|
||||||
my %ext2lang;
|
my %ext2lang;
|
||||||
my $filetypes_read=0;
|
my $filetypes_read=0;
|
||||||
|
|
||||||
|
@ -103,6 +116,12 @@ sub highlight ($$) {
|
||||||
my $langfile=shift;
|
my $langfile=shift;
|
||||||
my $input=shift;
|
my $input=shift;
|
||||||
|
|
||||||
|
eval q{use highlight};
|
||||||
|
if ($@) {
|
||||||
|
print STDERR gettext("warning: highlight perl module not available; falling back to pass through");
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
my $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
|
my $gen = highlightc::CodeGenerator_getInstance($highlightc::XHTML);
|
||||||
$gen->setFragmentCode(1); # generate html fragment
|
$gen->setFragmentCode(1); # generate html fragment
|
||||||
$gen->setHTMLEnclosePreTag(1); # include stylish <pre>
|
$gen->setHTMLEnclosePreTag(1); # include stylish <pre>
|
||||||
|
|
|
@ -5,6 +5,9 @@ ikiwiki (3.14) UNRELEASED; urgency=low
|
||||||
* debian/control: Add suggests for libhighlight-perl, although
|
* debian/control: Add suggests for libhighlight-perl, although
|
||||||
that package is not yet created by Debian's highlight source package.
|
that package is not yet created by Debian's highlight source package.
|
||||||
(See #529869)
|
(See #529869)
|
||||||
|
* format: Provide a htmlizefallback hook that other plugins
|
||||||
|
can use to handle formats that are not suitable for general-purpose
|
||||||
|
htmlize hooks. Used by highlight.
|
||||||
|
|
||||||
-- Joey Hess <joeyh@debian.org> Fri, 22 May 2009 22:03:12 -0400
|
-- Joey Hess <joeyh@debian.org> Fri, 22 May 2009 22:03:12 -0400
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
[[!template id=plugin name=highlight author="[[Joey]]"]]
|
[[!template id=plugin name=highlight author="[[Joey]]"]]
|
||||||
[[!tag type/format]]
|
[[!tag type/format]]
|
||||||
|
|
||||||
This plugin allows ikiwiki to syntax highlight source files, using
|
This plugin allows ikiwiki to syntax highlight source code, using
|
||||||
a fast syntax highlighter that supports over a hundred programming
|
a fast syntax highlighter that supports over a hundred programming
|
||||||
languages and file formats.
|
languages and file formats.
|
||||||
|
|
||||||
|
@ -11,26 +11,10 @@ You will need to install the perl bindings to the
|
||||||
[highlight library](http://www.andre-simon.de/), which in Debian
|
[highlight library](http://www.andre-simon.de/), which in Debian
|
||||||
are in the [[!debpkg libhighlight-perl]] package.
|
are in the [[!debpkg libhighlight-perl]] package.
|
||||||
|
|
||||||
## configuration
|
|
||||||
|
|
||||||
Nothing will be highlighted by default.
|
|
||||||
To enable syntax highlighting, use the `tohighlight` setting in your
|
|
||||||
setup file to control which files should be syntax highlighted.
|
|
||||||
Here is a typical setting for it, enabling highlighting for files
|
|
||||||
with the extensions .c, etc, and also for any files named "Makefile".
|
|
||||||
|
|
||||||
tohighlight => ".c .h .cpp .pl .py Makefile:make",
|
|
||||||
|
|
||||||
It knows what language to use for most filename extensions (see
|
|
||||||
`/etc/highlight/filetypes.conf` for a partial list), but if you want to
|
|
||||||
bind an unusual filename extension, or any file without an extension
|
|
||||||
(such as a Makefile), to a language, you can do so by appending a colon
|
|
||||||
and the name of the language, as illustrated for Makefiles above.
|
|
||||||
|
|
||||||
## embedding highlighted code
|
## embedding highlighted code
|
||||||
|
|
||||||
To embed highlighted code on a page, you can use the
|
To embed highlighted code on a page, you can use the
|
||||||
[[ikiwiki/directive/format]] directive.
|
[[format]] plugin.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
|
@ -40,21 +24,36 @@ For example:
|
||||||
}
|
}
|
||||||
"""]]
|
"""]]
|
||||||
|
|
||||||
You can do this for any of the extensions/filenames enabled in
|
\[[!format diff """
|
||||||
`tohighlight`.
|
-bar
|
||||||
|
+foo
|
||||||
|
"""]]
|
||||||
|
|
||||||
## colors
|
You can do this for any extension or language name supported by
|
||||||
|
the [highlight library](http://www.andre-simon.de/) -- basically anything
|
||||||
|
you can think of should work.
|
||||||
|
|
||||||
The colors etc used for the syntax highlighting are entirely configurable
|
## highlighting entire source files
|
||||||
by CSS. See ikiwiki's [[style.css]] for the defaults.
|
|
||||||
|
|
||||||
## limitations
|
To enable syntax highlighting of entire standalone source files, use the
|
||||||
|
`tohighlight` setting in your setup file to control which files should be
|
||||||
|
syntax highlighted. Here is a typical setting for it, enabling highlighting
|
||||||
|
for files with the extensions .c, etc, and also for any files named
|
||||||
|
"Makefile".
|
||||||
|
|
||||||
With this plugin enabled, source files become full-fledged ikiwiki pages,
|
tohighlight => ".c .h .cpp .pl .py Makefile:make",
|
||||||
which means they can include [[WikiLinks|ikiwiki/wikilink]] and
|
|
||||||
[[directives|ikiwiki/directive]] like any other page can, and are also
|
It knows what language to use for most filename extensions (see
|
||||||
affected by the [[smiley]] plugin, if it is enabled. This can be
|
`/etc/highlight/filetypes.conf` for a partial list), but if you want to
|
||||||
annoying if your code accidentially contains things that look like those.
|
bind an unusual filename extension, or any file without an extension
|
||||||
|
(such as a Makefile), to a language, you can do so by appending a colon
|
||||||
|
and the name of the language, as illustrated for Makefiles above.
|
||||||
|
|
||||||
|
With the plugin configured this way, source files become full-fledged
|
||||||
|
wiki pages, which means they can include [[WikiLinks|ikiwiki/wikilink]]
|
||||||
|
and [[directives|ikiwiki/directive]] like any other page can, and are also
|
||||||
|
affected by the [[smiley]] plugin, if it is enabled. This can be annoying
|
||||||
|
if your code accidentially contains things that look like those.
|
||||||
|
|
||||||
On the other hand, this also allows your syntax highlighed
|
On the other hand, this also allows your syntax highlighed
|
||||||
source code to contain markdown formatted comments and hyperlinks
|
source code to contain markdown formatted comments and hyperlinks
|
||||||
|
@ -66,7 +65,11 @@ to other code files, like this:
|
||||||
See \[[bar.h]].
|
See \[[bar.h]].
|
||||||
""]] */
|
""]] */
|
||||||
|
|
||||||
## security
|
Finally, bear in mind that this lets anyone who can edit a page in your
|
||||||
|
wiki also edit source code files that are in your wiki. Use appropriate
|
||||||
|
caution.
|
||||||
|
|
||||||
This lets anyone who can edit a page in your wiki also edit
|
## colors
|
||||||
source code files that are in your wiki. Use appropriate caution.
|
|
||||||
|
The colors etc used for the syntax highlighting are entirely configurable
|
||||||
|
by CSS. See ikiwiki's [[style.css]] for the defaults.
|
||||||
|
|
|
@ -36,8 +36,10 @@ work as source-highlight, but in perl. I plan to package the base module for de
|
||||||
releases the 5 or 6 language definitions he has running on his web site, it might be suitable for inclusion in ikiwiki. [[DavidBremner]]
|
releases the 5 or 6 language definitions he has running on his web site, it might be suitable for inclusion in ikiwiki. [[DavidBremner]]
|
||||||
|
|
||||||
* [[plugins/highlight]] uses [highlight](http://www.andre-simon.de) via
|
* [[plugins/highlight]] uses [highlight](http://www.andre-simon.de) via
|
||||||
its swig bindings. It supports whole files only. It uses either
|
its swig bindings. It optionally supports whole files, but also
|
||||||
keepextension or noextension, as appropriate for the type of file.
|
integrates with the format directive to allow formatting of *any* of
|
||||||
|
highlight's supported formats. (For whole files, it uses either
|
||||||
|
keepextension or noextension, as appropriate for the type of file.)
|
||||||
|
|
||||||
## General problems / requirements
|
## General problems / requirements
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue