ikiwiki/IkiWiki/Plugin/txt.pm

89 lines
1.8 KiB
Perl
Raw Normal View History

2008-06-25 02:33:03 +02:00
#!/usr/bin/perl
2008-06-25 02:33:41 +02:00
# .txt as a wiki page type - links WikiLinks and URIs.
2008-06-25 02:33:03 +02:00
#
# Copyright (C) 2008 Gabriel McManus <gmcmanus@gmail.com>
# Licensed under the GNU General Public License, version 2 or later
2008-06-25 02:33:41 +02:00
package IkiWiki::Plugin::txt;
2008-06-25 02:33:03 +02:00
use warnings;
use strict;
use IkiWiki 3.00;
2008-06-25 02:33:03 +02:00
use HTML::Entities;
my $findurl=0;
2008-06-25 02:33:03 +02:00
sub import {
hook(type => "getsetup", id => "txt", call => \&getsetup);
hook(type => "filter", id => "txt", call => \&filter);
2008-06-25 02:35:21 +02:00
hook(type => "htmlize", id => "txt", call => \&htmlize);
hook(type => "htmlizeformat", id => "txt", call => \&htmlizeformat);
eval q{use URI::Find};
if (! $@) {
$findurl=1;
}
2008-06-25 02:33:03 +02:00
}
sub getsetup () {
return
plugin => {
safe => 1,
rebuild => 1, # format plugin
2010-02-12 07:10:36 +01:00
section => "format",
},
}
2008-06-25 02:33:03 +02:00
# We use filter to convert raw text to HTML
# (htmlize is called after other plugins insert HTML)
sub filter (@) {
2008-06-25 02:35:21 +02:00
my %params = @_;
my $content = $params{content};
if (defined $pagesources{$params{page}} &&
$pagesources{$params{page}} =~ /\.txt$/) {
if ($pagesources{$params{page}} eq 'robots.txt' &&
$params{page} eq $params{destpage}) {
will_render($params{page}, 'robots.txt');
writefile('robots.txt', $config{destdir}, $content);
}
return txt2html($content);
2008-06-25 02:35:21 +02:00
}
return $content;
2008-06-25 02:33:03 +02:00
}
sub txt2html ($) {
my $content=shift;
encode_entities($content, "<>&");
if ($findurl) {
my $finder = URI::Find->new(sub {
my ($uri, $orig_uri) = @_;
return qq|<a href="$uri">$orig_uri</a>|;
});
$finder->find(\$content);
}
return "<pre>" . $content . "</pre>";
}
2008-06-25 02:33:03 +02:00
# We need this to register the .txt file extension
sub htmlize (@) {
2008-06-25 02:35:21 +02:00
my %params=@_;
return $params{content};
2008-06-25 02:33:03 +02:00
}
sub htmlizeformat ($$) {
my $format=shift;
my $content=shift;
if ($format eq 'txt') {
return txt2html($content);
}
else {
return;
}
}
2008-06-25 02:33:03 +02:00
1