From 2b3b705d069f30858d781ecf8b43f29e24203cec Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 5 Oct 2010 08:34:16 -0400 Subject: [PATCH] Extend inlining to handle raw files (files with unrecognized extensions). Example usage: [[!inline pagenames="somefile.txt" template="raw" feeds="no"]] Also raise an error in IkiWiki::pagetype($file) if $file is blank, which avoids trying to do much with missing files, etc. --- IkiWiki.pm | 3 +++ IkiWiki/Plugin/inline.pm | 47 ++++++++++++++++++++++++++-------------- t/pagetype.t | 16 ++++++++++++++ templates/raw.tmpl | 25 +++++++++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) mode change 100644 => 100755 IkiWiki/Plugin/inline.pm create mode 100755 t/pagetype.t create mode 100644 templates/raw.tmpl diff --git a/IkiWiki.pm b/IkiWiki.pm index 52da3c112..943d98d6e 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -772,6 +772,9 @@ sub isinternal ($) { sub pagetype ($) { my $file=shift; + if (! $file) { + error("Missing file."); + } if ($file =~ /\.([^.]+)$/) { return $1 if exists $hooks{htmlize}{$1}; } diff --git a/IkiWiki/Plugin/inline.pm b/IkiWiki/Plugin/inline.pm old mode 100644 new mode 100755 index 8eb033951..7537ae4ba --- a/IkiWiki/Plugin/inline.pm +++ b/IkiWiki/Plugin/inline.pm @@ -86,6 +86,13 @@ sub getsetup () { safe => 1, rebuild => 0, }, + raw_templates => { + type => "string", + example => [qw{raw}], + description => "templates for which you want raw content", + safe => 0, + rebuild => 1, + }, } sub checkconfig () { @@ -393,11 +400,13 @@ sub preprocess_inline (@) { my $file = $pagesources{$page}; my $type = pagetype($file); if (! $raw) { + # is $params{template} in $config{raw_templates}? + my $read_raw = grep {$_ eq $params{template}} @{$config{raw_templates}}; if ($needcontent) { # Get the content before populating the # template, since getting the content uses # the same template if inlines are nested. - my $content=get_inline_content($page, $params{destpage}); + my $content=get_inline_content($page, $params{destpage}, $read_raw); $template->param(content => $content); } $template->param(pageurl => urlto($page, $params{destpage})); @@ -502,10 +511,11 @@ sub pagetemplate_inline (@) { my %inline_content; my $cached_destpage=""; -sub get_inline_content ($$) { +sub get_inline_content ($$$) { my $page=shift; my $destpage=shift; - + my $read_raw=shift; + if (exists $inline_content{$page} && $cached_destpage eq $destpage) { return $inline_content{$page}; } @@ -513,21 +523,23 @@ sub get_inline_content ($$) { my $file=$pagesources{$page}; my $type=pagetype($file); my $ret=""; + $nested++; if (defined $type) { - $nested++; $ret=htmlize($page, $destpage, $type, linkify($page, $destpage, preprocess($page, $destpage, filter($page, $destpage, readfile(srcfile($file)))))); - $nested--; - if (isinternal($page)) { - # make inlined text of internal pages searchable - run_hooks(indexhtml => sub { - shift->(page => $page, destpage => $destpage, - content => $ret); - }); - } + } elsif ($read_raw) { + $ret=readfile(srcfile($file)); + } + $nested--; + if (isinternal($page)) { + # make inlined text of internal pages searchable + run_hooks(indexhtml => sub { + shift->(page => $page, destpage => $destpage, + content => $ret); + }); } if ($cached_destpage ne $destpage) { @@ -620,13 +632,16 @@ sub genfeed ($$$$$@) { my @pages=@_; my $url=URI->new(encode_utf8(urlto($page,"",1))); - - my $itemtemplate=template_depends($feedtype."item.tmpl", $page, blind_cache => 1); + + my $template=$feedtype."item"; + my $itemtemplate=template_depends($template.".tmpl", $page, blind_cache => 1); my $content=""; my $lasttime = 0; foreach my $p (@pages) { my $u=URI->new(encode_utf8(urlto($p, "", 1))); - my $pcontent = absolute_urls(get_inline_content($p, $page), $url); + # is $params{template} in $config{raw_templates}? + my $read_raw = grep {$_ eq $template} @{$config{raw_templates}}; + my $pcontent = absolute_urls(get_inline_content($p, $page, $read_raw), $url); $itemtemplate->param( title => pagetitle(basename($p)), @@ -685,7 +700,7 @@ sub genfeed ($$$$$@) { $lasttime = $pagemtime{$p} if $pagemtime{$p} > $lasttime; } - my $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1); + $template=template_depends($feedtype."page.tmpl", $page, blind_cache => 1); $template->param( title => $page ne "index" ? pagetitle($page) : $config{wikiname}, wikiname => $config{wikiname}, diff --git a/t/pagetype.t b/t/pagetype.t new file mode 100755 index 000000000..442313ed1 --- /dev/null +++ b/t/pagetype.t @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 3; +use Test::Exception; + +BEGIN { use_ok("IkiWiki"); } + +%config=IkiWiki::defaultconfig(); +$config{srcdir}=$config{destdir}="/dev/null"; +IkiWiki::loadplugins(); +IkiWiki::checkconfig(); + +dies_ok(sub { pagetype("") }, "missing file") or diag(pagetype("")); +is(pagetype("t/test1.mdwn"), "mdwn", "markdown file"); + diff --git a/templates/raw.tmpl b/templates/raw.tmpl new file mode 100644 index 000000000..e25a827c2 --- /dev/null +++ b/templates/raw.tmpl @@ -0,0 +1,25 @@ +
+ +
+
+ + + + + +
+
+ +
+

+
+
+
+ +
+ +Posted + +
+ +
-- 2.26.2