summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/txt.pm
blob: 3aad5fa07a6aa3a349fab19ba7b182fe667f60ca (plain)
  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. package IkiWiki::Plugin::txt;
  7. use warnings;
  8. use strict;
  9. use IkiWiki 2.00;
  10. use HTML::Entities;
  11. my $findurl=0;
  12. sub import {
  13. hook(type => "filter", id => "txt", call => \&filter);
  14. hook(type => "htmlize", id => "txt", call => \&htmlize);
  15. eval q{use URI::Find};
  16. if (! $@) {
  17. $findurl=1;
  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. if (defined $pagesources{$params{page}} && $pagesources{$params{page}} =~ /\.txt$/) {
  26. encode_entities($content);
  27. if ($findurl) {
  28. my $finder = URI::Find->new(sub {
  29. my ($uri, $orig_uri) = @_;
  30. return qq|<a href="$uri">$orig_uri</a>|;
  31. });
  32. $finder->find(\$content);
  33. }
  34. $content = "<pre>" . $content . "</pre>";
  35. }
  36. return $content;
  37. }
  38. # We need this to register the .txt file extension
  39. sub htmlize (@) {
  40. my %params=@_;
  41. return $params{content};
  42. }
  43. 1