]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/source_link.mdwn
Merge commit 'smcv/ready/getsource'
[ikiwiki.git] / doc / todo / source_link.mdwn
1 How about a direct link from the page header to the source of the latest version, to avoid the need to either use edit or navigate to the current version via the history link?
2
3  I'd like this too (and might try to implement it). -- [[users/jon]]
4
5 I just implemented this.  There is one [[patch]] to the default page template, and a new plugin.  -- [[Will]]
6
7 All of this code is licensed under the GPLv2+. -- [[Will]]
8
9 > The use of sessioncgi here seems undesirable: on wikis where anonymity is
10 > not allowed, you'll be asked to log in. Couldn't you achieve the same thing
11 > by loading the index with IkiWiki::loadindex, like [[plugins/goto]] does?
12 > --[[smcv]]
13
14 [[!template id=gitbranch branch=smcv/ready/getsource
15   author="[[Will]]/[[smcv]]"]]
16
17 >> I've applied the patch below in a git branch, fixed my earlier criticism,
18 >> and also fixed a couple of other issues I noticed:
19 >>
20 >> * missing pages could be presented better as a real 404 page
21 >> * the default Content-type should probably be UTF-8 since the rest of
22 >>   IkiWiki tends to assume that
23 >> * emitting attachments (images, etc.) as text/plain isn't going to work :-)
24 >>
25 >> Any opinions on my branch? I think it's ready for merge, if Joey approves.
26 >>
27 >> --[[smcv]]
28
29 >>> I need a copyright&license statement, so debian/copyright can be updated for
30 >>> the plugin, before I can merge this. Otherwise ready. --[[Joey]]
31
32 >>> That looks like a nice set of fixes.  One more that might be worthwhile: instead of reading the page source into a var, and then writing it out later, it might be nice to just
33 >>>  `print readfile(srcfile(pagesources{$page}));` at the appropriate point. -- [[Will]]
34
35 >>>> OK, I've committed that. --[[smcv]]
36
37 ----
38
39     diff --git a/templates/page.tmpl b/templates/page.tmpl
40     index f2f9c34..3176bed 100644
41     --- a/templates/page.tmpl
42     +++ b/templates/page.tmpl
43     @@ -46,6 +46,9 @@
44      <TMPL_IF NAME="HISTORYURL">
45      <li><a href="<TMPL_VAR HISTORYURL>">History</a></li>
46      </TMPL_IF>
47     +<TMPL_IF NAME="GETSOURCEURL">
48     +<li><a href="<TMPL_VAR GETSOURCEURL>">Get Source</a></li>
49     +</TMPL_IF>
50      <TMPL_IF NAME="PREFSURL">
51      <li><a href="<TMPL_VAR PREFSURL>">Preferences</a></li>
52      </TMPL_IF>
53
54 ----
55
56     #!/usr/bin/perl
57     package IkiWiki::Plugin::getsource;
58     
59     use warnings;
60     use strict;
61     use IkiWiki;
62     use open qw{:utf8 :std};
63     
64     sub import {
65         hook(type => "getsetup", id => "getsource", call => \&getsetup);
66         hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
67         hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource);
68     }
69     
70     sub getsetup () {
71         return
72                 plugin => {
73                         safe => 1,
74                         rebuild => 1,
75                 },
76                 getsource_mimetype => {
77                         type => "string",
78                         example => "application/octet-stream",
79                         description => "Mime type for returned source.",
80                         safe => 1,
81                         rebuild => 0,
82                 },
83     }
84     
85     sub pagetemplate (@) {
86         my %params=@_;
87     
88         my $page=$params{page};
89         my $template=$params{template};
90     
91         if (length $config{cgiurl}) {
92                 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
93                 $template->param(have_actions => 1);
94         }
95     }
96     
97     sub cgi_getsource ($$) {
98         my $cgi=shift;
99         my $session=shift;
100     
101         # Note: we use sessioncgi rather than just cgi
102         # because we need $IkiWiki::pagesources{} to be
103         # populated.
104         
105         return unless (defined $cgi->param('do') &&
106                                         $cgi->param("do") eq "getsource");
107     
108         IkiWiki::decode_cgi_utf8($cgi);
109     
110         my $page=$cgi->param('page');
111     
112         if ($IkiWiki::pagesources{$page}) {
113                 
114                 my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
115                 
116                 if (! $config{getsource_mimetype}) {
117                         $config{getsource_mimetype} = "text/plain";
118                 }
119                 
120                 print "Content-Type: $config{getsource_mimetype}\r\n";
121                 
122                 print ("\r\n");
123                 
124                 print $data;
125                 
126                 exit 0;
127         }
128         
129         error("Unable to find page source for page: $page");
130     
131         exit 0;
132     }
133     
134     1
135
136 [[done]] --[[smcv]]