summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/txt.pm
blob: fcfb68be90891fca4a46ef512b43548362ffb9e1 (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. hook(type => "htmlizeformat", id => "txt", call => \&htmlizeformat);
  17. eval q{use URI::Find};
  18. if (! $@) {
  19. $findurl=1;
  20. }
  21. }
  22. sub getsetup () {
  23. return
  24. plugin => {
  25. safe => 1,
  26. rebuild => 1, # format plugin
  27. section => "format",
  28. },
  29. }
  30. # We use filter to convert raw text to HTML
  31. # (htmlize is called after other plugins insert HTML)
  32. sub filter (@) {
  33. my %params = @_;
  34. my $content = $params{content};
  35. if (defined $pagesources{$params{page}} &&
  36. $pagesources{$params{page}} =~ /\.txt$/) {
  37. if ($pagesources{$params{page}} eq 'robots.txt' &&
  38. $params{page} eq $params{destpage}) {
  39. will_render($params{page}, 'robots.txt');
  40. writefile('robots.txt', $config{destdir}, $content);
  41. }
  42. return txt2html($content);
  43. }
  44. return $content;
  45. }
  46. sub txt2html ($) {
  47. my $content=shift;
  48. encode_entities($content, "<>&");
  49. if ($findurl) {
  50. my $finder = URI::Find->new(sub {
  51. my ($uri, $orig_uri) = @_;
  52. return qq|<a href="$uri">$orig_uri</a>|;
  53. });
  54. $finder->find(\$content);
  55. }
  56. return "<pre>" . $content . "</pre>";
  57. }
  58. # We need this to register the .txt file extension
  59. sub htmlize (@) {
  60. my %params=@_;
  61. return $params{content};
  62. }
  63. sub htmlizeformat ($$) {
  64. my $format=shift;
  65. my $content=shift;
  66. if ($format eq 'txt') {
  67. return txt2html($content);
  68. }
  69. else {
  70. return;
  71. }
  72. }
  73. 1