]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/txt.pm
misc.tmpl: Revert to upstream (this is going away)
[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 3.00;
12 use HTML::Entities;
13
14 my $findurl=0;
15
16 sub import {
17         hook(type => "getsetup", id => "txt", call => \&getsetup);
18         hook(type => "filter", id => "txt", call => \&filter);
19         hook(type => "htmlize", id => "txt", call => \&htmlize);
20
21         eval q{use URI::Find};
22         if (! $@) {
23                 $findurl=1;
24         }
25 }
26
27 sub getsetup () {
28         return
29                 plugin => {
30                         safe => 1,
31                         rebuild => 1, # format plugin
32                         section => "format",
33                 },
34 }
35
36 # We use filter to convert raw text to HTML
37 # (htmlize is called after other plugins insert HTML)
38 sub filter (@) {
39         my %params = @_;
40         my $content = $params{content};
41
42         if (defined $pagesources{$params{page}} && $pagesources{$params{page}} =~ /\.txt$/) {
43                 encode_entities($content, "<>&");
44                 if ($findurl) {
45                         my $finder = URI::Find->new(sub {
46                                 my ($uri, $orig_uri) = @_;
47                                 return qq|<a href="$uri">$orig_uri</a>|;
48                         });
49                         $finder->find(\$content);
50                 }
51                 $content = "<pre>" . $content . "</pre>";
52         }
53
54         return $content;
55 }
56
57 # We need this to register the .txt file extension
58 sub htmlize (@) {
59         my %params=@_;
60         return $params{content};
61 }
62
63 1