]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/plaintext.pm
eaa2725b9aee5439bb8368727bc40853d2cca276
[ikiwiki.git] / IkiWiki / Plugin / plaintext.pm
1 #!/usr/bin/perl
2 # Plaintext 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::plaintext;
8
9 use warnings;
10 use strict;
11 use IkiWiki 2.00;
12 use HTML::Entities;
13 require URI::Find;
14
15 sub import {
16         hook(type => "filter",  id => "txt", call => \&filter);
17         hook(type => "htmlize", id => "txt", call => \&htmlize);
18 }
19
20 # We use filter to convert raw text to HTML
21 # (htmlize is called after other plugins insert HTML)
22 sub filter (@) {
23     my %params = @_;
24     my $content = $params{content};
25
26     if ($pagesources{$params{page}} =~ /.txt$/) {
27         encode_entities($content);
28         my $finder = URI::Find->new(
29             sub {
30                 my($uri, $orig_uri) = @_;
31                 return qq|<a href="$uri">$orig_uri</a>|;
32             });
33         $finder->find(\$content);
34         $content = "<pre>" . $content . "</pre>";
35     }
36     return $content;
37 }
38
39 # We need this to register the .txt file extension
40 sub htmlize (@) {
41     my %params=@_;
42     return $params{content};
43 }
44
45 1