summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/txt.pm
blob: 0d9a0b35bd8abf6be895c4d1ce85f64a6077ded1 (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}} &&
  35. $pagesources{$params{page}} =~ /\.txt$/) {
  36. if ($pagesources{$params{page}} eq 'robots.txt' &&
  37. $params{page} eq $params{destpage}) {
  38. will_render($params{page}, 'robots.txt');
  39. writefile('robots.txt', $config{destdir}, $content);
  40. }
  41. encode_entities($content, "<>&");
  42. if ($findurl) {
  43. my $finder = URI::Find->new(sub {
  44. my ($uri, $orig_uri) = @_;
  45. return qq|<a href="$uri">$orig_uri</a>|;
  46. });
  47. $finder->find(\$content);
  48. }
  49. $content = "<pre>" . $content . "</pre>";
  50. }
  51. return $content;
  52. }
  53. # We need this to register the .txt file extension
  54. sub htmlize (@) {
  55. my %params=@_;
  56. return $params{content};
  57. }
  58. 1