summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/txt.pm
blob: 1ed9f0856fe0d8589aede718651110a228e60a4a (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. section => "format",
  27. },
  28. }
  29. # We use filter to convert raw text to HTML
  30. # (htmlize is called after other plugins insert HTML)
  31. sub filter (@) {
  32. my %params = @_;
  33. my $content = $params{content};
  34. if (defined $pagesources{$params{page}} && $pagesources{$params{page}} =~ /\.txt$/) {
  35. encode_entities($content, "<>&");
  36. if ($findurl) {
  37. my $finder = URI::Find->new(sub {
  38. my ($uri, $orig_uri) = @_;
  39. return qq|<a href="$uri">$orig_uri</a>|;
  40. });
  41. $finder->find(\$content);
  42. }
  43. $content = "<pre>" . $content . "</pre>";
  44. }
  45. return $content;
  46. }
  47. # We need this to register the .txt file extension
  48. sub htmlize (@) {
  49. my %params=@_;
  50. return $params{content};
  51. }
  52. 1