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;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2008-06-25 02:33:03 +02:00
|
|
|
use HTML::Entities;
|
2008-06-25 02:38:41 +02:00
|
|
|
|
|
|
|
my $findurl=0;
|
2008-06-25 02:33:03 +02:00
|
|
|
|
|
|
|
sub import {
|
2008-08-04 01:35:35 +02:00
|
|
|
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);
|
2010-10-26 04:37:34 +02:00
|
|
|
hook(type => "htmlizeformat", id => "txt", call => \&htmlizeformat);
|
2008-06-25 02:38:41 +02:00
|
|
|
|
|
|
|
eval q{use URI::Find};
|
|
|
|
if (! $@) {
|
|
|
|
$findurl=1;
|
|
|
|
}
|
2008-06-25 02:33:03 +02:00
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-04 01:35:35 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 1, # format plugin
|
2010-02-12 07:10:36 +01:00
|
|
|
section => "format",
|
2008-08-04 01:35:35 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-04 01:35:35 +02:00
|
|
|
|
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};
|
|
|
|
|
2010-04-05 23:02:10 +02:00
|
|
|
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);
|
|
|
|
}
|
2010-10-26 04:37:34 +02:00
|
|
|
return txt2html($content);
|
2008-06-25 02:35:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2008-06-25 02:33:03 +02:00
|
|
|
}
|
|
|
|
|
2010-10-26 04:37:34 +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
|
|
|
}
|
|
|
|
|
2010-10-26 04:37:34 +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
|