]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/source_link.mdwn
(no commit message)
[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/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 ----
31
32     diff --git a/templates/page.tmpl b/templates/page.tmpl
33     index f2f9c34..3176bed 100644
34     --- a/templates/page.tmpl
35     +++ b/templates/page.tmpl
36     @@ -46,6 +46,9 @@
37      <TMPL_IF NAME="HISTORYURL">
38      <li><a href="<TMPL_VAR HISTORYURL>">History</a></li>
39      </TMPL_IF>
40     +<TMPL_IF NAME="GETSOURCEURL">
41     +<li><a href="<TMPL_VAR GETSOURCEURL>">Get Source</a></li>
42     +</TMPL_IF>
43      <TMPL_IF NAME="PREFSURL">
44      <li><a href="<TMPL_VAR PREFSURL>">Preferences</a></li>
45      </TMPL_IF>
46
47 ----
48
49     #!/usr/bin/perl
50     package IkiWiki::Plugin::getsource;
51     
52     use warnings;
53     use strict;
54     use IkiWiki;
55     use open qw{:utf8 :std};
56     
57     sub import {
58         hook(type => "getsetup", id => "getsource", call => \&getsetup);
59         hook(type => "pagetemplate", id => "getsource", call => \&pagetemplate);
60         hook(type => "sessioncgi", id => "getsource", call => \&cgi_getsource);
61     }
62     
63     sub getsetup () {
64         return
65                 plugin => {
66                         safe => 1,
67                         rebuild => 1,
68                 },
69                 getsource_mimetype => {
70                         type => "string",
71                         example => "application/octet-stream",
72                         description => "Mime type for returned source.",
73                         safe => 1,
74                         rebuild => 0,
75                 },
76     }
77     
78     sub pagetemplate (@) {
79         my %params=@_;
80     
81         my $page=$params{page};
82         my $template=$params{template};
83     
84         if (length $config{cgiurl}) {
85                 $template->param(getsourceurl => IkiWiki::cgiurl(do => "getsource", page => $page));
86                 $template->param(have_actions => 1);
87         }
88     }
89     
90     sub cgi_getsource ($$) {
91         my $cgi=shift;
92         my $session=shift;
93     
94         # Note: we use sessioncgi rather than just cgi
95         # because we need $IkiWiki::pagesources{} to be
96         # populated.
97         
98         return unless (defined $cgi->param('do') &&
99                                         $cgi->param("do") eq "getsource");
100     
101         IkiWiki::decode_cgi_utf8($cgi);
102     
103         my $page=$cgi->param('page');
104     
105         if ($IkiWiki::pagesources{$page}) {
106                 
107                 my $data = IkiWiki::readfile(IkiWiki::srcfile($IkiWiki::pagesources{$page}));
108                 
109                 if (! $config{getsource_mimetype}) {
110                         $config{getsource_mimetype} = "text/plain";
111                 }
112                 
113                 print "Content-Type: $config{getsource_mimetype}\r\n";
114                 
115                 print ("\r\n");
116                 
117                 print $data;
118                 
119                 exit 0;
120         }
121         
122         error("Unable to find page source for page: $page");
123     
124         exit 0;
125     }
126     
127     1