summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/txt.pm
blob: 8599bdc8ec881429472ef19f760113f27d0a678f (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 3.00;
  10. use HTML::Entities;
  11. my $findurl=0;
  12. sub import {
  13. hook(type => "getsetup", id => "txt", call => \&getsetup);
  14. hook(type => "filter", id => "txt", call => \&filter);
  15. hook(type => "htmlize", id => "txt", call => \&htmlize);
  16. eval q{use URI::Find};
  17. if (! $@) {
  18. $findurl=1;
  19. }
  20. }
  21. sub getsetup () {
  22. return
  23. plugin => {
  24. safe => 1,
  25. rebuild => 1, # format plugin
  26. },
  27. }
  28. # We use filter to convert raw text to HTML
  29. # (htmlize is called after other plugins insert HTML)
  30. sub filter (@) {
  31. my %params = @_;
  32. my $content = $params{content};
  33. if (defined $pagesources{$params{page}} && $pagesources{$params{page}} =~ /\.txt$/) {
  34. encode_entities($content, "<>&");
  35. if ($findurl) {
  36. my $finder = URI::Find->new(sub {
  37. my ($uri, $orig_uri) = @_;
  38. return qq|<a href="$uri">$orig_uri</a>|;
  39. });
  40. $finder->find(\$content);
  41. }
  42. $content = "<pre>" . $content . "</pre>";
  43. }
  44. return $content;
  45. }
  46. # We need this to register the .txt file extension
  47. sub htmlize (@) {
  48. my %params=@_;
  49. return $params{content};
  50. }
  51. 1