]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/recentchangesdiff.pm
Forum: Ask about strange link appearance with trail and actiontabs
[git.ikiwiki.info.git] / IkiWiki / Plugin / recentchangesdiff.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::recentchangesdiff;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7 use HTML::Entities;
9 my $maxlines=200;
11 sub import {
12         add_underlay("javascript");
13         hook(type => "getsetup", id => "recentchangesdiff",
14                 call => \&getsetup);
15         hook(type => "pagetemplate", id => "recentchangesdiff",
16                 call => \&pagetemplate);
17         hook(type => "format", id => "recentchangesdiff.pm", call => \&format);
18 }
20 sub getsetup () {
21         return 
22                 plugin => {
23                         safe => 1,
24                         rebuild => 1,
25                 },
26 }
28 sub pagetemplate (@) {
29         my %params=@_;
30         my $template=$params{template};
31         if ($config{rcs} && exists $params{rev} && length $params{rev} &&
32             $template->query(name => "diff")) {
33                 my @lines=IkiWiki::rcs_diff($params{rev}, $maxlines+1);
34                 if (@lines) {
35                         my $diff;
36                         my $trunc=0;
37                         if (@lines > $maxlines) {
38                                 $diff=join("", @lines[0..($maxlines-1)]);
39                                 $trunc=1;
40                         }
41                         else {
42                                 $diff=join("", @lines);
43                         }
44                         if (length $diff > 102400) {
45                                 $diff=substr($diff, 0, 10240);
46                                 $trunc=1;
47                         }
48                         if ($trunc) {
49                                 $diff.="\n".gettext("(Diff truncated)");
50                         }
51                         # escape html
52                         $diff = encode_entities($diff);
53                         # escape links and preprocessor stuff
54                         $diff = encode_entities($diff, '\[\]');
55                         $template->param(diff => $diff);
56                 }
57         }
58 }
60 sub format (@) {
61         my %params=@_;
63         if (! ($params{content}=~s!^(<body[^>]*>)!$1.include_javascript($params{page})!em)) {
64                 # no <body> tag, probably in preview mode
65                 $params{content}=include_javascript(undef).$params{content};
66         }
67         return $params{content};
68 }
70 # taken verbatim from toggle.pm
71 sub include_javascript ($) {
72         my $from=shift;
73         
74         return '<script src="'.urlto("ikiwiki/ikiwiki.js", $from).
75                 '" type="text/javascript" charset="utf-8"></script>'."\n".
76                 '<script src="'.urlto("ikiwiki/toggle.js", $from).
77                 '" type="text/javascript" charset="utf-8"></script>';
78 }
80 1