]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/txt.pm
Merge branch 'master' into tova
[ikiwiki.git] / IkiWiki / Plugin / txt.pm
1 #!/usr/bin/perl
2 # .txt as a wiki page type - links WikiLinks and URIs.
3 #
4 # Copyright (C) 2008 Gabriel McManus <gmcmanus@gmail.com>
5 # Licensed under the GNU General Public License, version 2 or later
6
7 package IkiWiki::Plugin::txt;
8
9 use warnings;
10 use strict;
11 use IkiWiki 2.00;
12 use HTML::Entities;
13
14 my $findurl=0;
15
16 sub import {
17         hook(type => "filter",  id => "txt", call => \&filter);
18         hook(type => "htmlize", id => "txt", call => \&htmlize);
19
20         eval q{use URI::Find};
21         if (! $@) {
22                 $findurl=1;
23         }
24 }
25
26 # We use filter to convert raw text to HTML
27 # (htmlize is called after other plugins insert HTML)
28 sub filter (@) {
29         my %params = @_;
30         my $content = $params{content};
31
32         if (defined $pagesources{$params{page}} && $pagesources{$params{page}} =~ /\.txt$/) {
33                 encode_entities($content);
34                 if ($findurl) {
35                         my $finder = URI::Find->new(sub {
36                                 my ($uri, $orig_uri) = @_;
37                                 return qq|<a href="$uri">$orig_uri</a>|;
38                         });
39                         $finder->find(\$content);
40                 }
41                 $content = "<pre>" . $content . "</pre>";
42         }
43
44         return $content;
45 }
46
47 # We need this to register the .txt file extension
48 sub htmlize (@) {
49         my %params=@_;
50         return $params{content};
51 }
52
53 1