Extend inlining to handle raw files (files with unrecognized extensions).
authorW. Trevor King <wking@drexel.edu>
Tue, 5 Oct 2010 12:34:16 +0000 (08:34 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 13 Jan 2013 12:12:02 +0000 (07:12 -0500)
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
IkiWiki/Plugin/inline.pm [changed mode: 0644->0755]
t/pagetype.t [new file with mode: 0755]
templates/raw.tmpl [new file with mode: 0644]

index 52da3c112b26289298ef2a4a1faf7636ec6ff119..943d98d6e1d0f59a83c6cffae1039ab5e1fa3858 100644 (file)
@@ -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};
        }
old mode 100644 (file)
new mode 100755 (executable)
index 8eb0339..7537ae4
@@ -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 (executable)
index 0000000..442313e
--- /dev/null
@@ -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 (file)
index 0000000..e25a827
--- /dev/null
@@ -0,0 +1,25 @@
+<TMPL_IF HTML5><article class="inlinepage"><TMPL_ELSE><div class="inlinepage"></TMPL_IF>
+
+<TMPL_IF HTML5><section class="inlineheader"><TMPL_ELSE><div class="inlineheader"></TMPL_IF>
+<TMPL_IF HTML5><header class="header"><TMPL_ELSE><span class="header"></TMPL_IF>
+<TMPL_IF PERMALINK>
+<a href="<TMPL_VAR PERMALINK>"><TMPL_VAR TITLE></a>
+<TMPL_ELSE>
+<a href="<TMPL_VAR PAGEURL>"><TMPL_VAR TITLE></a>
+</TMPL_IF>
+<TMPL_IF HTML5></header><TMPL_ELSE></span></TMPL_IF>
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF HTML5><section class="inlinecontent"><TMPL_ELSE><div class="inlinecontent"></TMPL_IF>
+<pre><code>
+<TMPL_VAR CONTENT>
+</code></pre>
+<TMPL_IF HTML5></section><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF HTML5><footer class="inlinefooter"><TMPL_ELSE><div class="inlinefooter"></TMPL_IF>
+<span class="pagedate">
+Posted <TMPL_VAR CTIME>
+</span>
+<TMPL_IF HTML5></footer><TMPL_ELSE></div></TMPL_IF>
+
+<TMPL_IF HTML5></article><TMPL_ELSE></div></TMPL_IF>