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