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