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