]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/txt.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[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         hook(type => "htmlizeformat", id => "txt", call => \&htmlizeformat);
21
22         eval q{use URI::Find};
23         if (! $@) {
24                 $findurl=1;
25         }
26 }
27
28 sub getsetup () {
29         return
30                 plugin => {
31                         safe => 1,
32                         rebuild => 1, # format plugin
33                         section => "format",
34                 },
35 }
36
37 # We use filter to convert raw text to HTML
38 # (htmlize is called after other plugins insert HTML)
39 sub filter (@) {
40         my %params = @_;
41         my $content = $params{content};
42
43         if (defined $pagesources{$params{page}} &&
44             $pagesources{$params{page}} =~ /\.txt$/) {
45                 if ($pagesources{$params{page}} eq 'robots.txt' &&
46                     $params{page} eq $params{destpage}) {
47                         will_render($params{page}, 'robots.txt');
48                         writefile('robots.txt', $config{destdir}, $content);
49                 }
50                 return txt2html($content);
51         }
52
53         return $content;
54 }
55
56 sub txt2html ($) {
57         my $content=shift;
58         
59         encode_entities($content, "<>&");
60         if ($findurl) {
61                 my $finder = URI::Find->new(sub {
62                         my ($uri, $orig_uri) = @_;
63                         return qq|<a href="$uri">$orig_uri</a>|;
64                 });
65                 $finder->find(\$content);
66         }
67         return "<pre>" . $content . "</pre>";
68 }
69
70 # We need this to register the .txt file extension
71 sub htmlize (@) {
72         my %params=@_;
73         return $params{content};
74 }
75
76 sub htmlizeformat ($$) {
77         my $format=shift;
78         my $content=shift;
79
80         if ($format eq 'txt') {
81                 return txt2html($content);
82         }
83         else {
84                 return;
85         }
86 }
87
88 1