* Initial work on internationalization of the program code. po/ikiwiki.pot
[ikiwiki.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9
10 my %backlinks;
11 my $backlinks_calculated=0;
12
13 sub calculate_backlinks () { #{{{
14         return if $backlinks_calculated;
15         %backlinks=();
16         foreach my $page (keys %links) {
17                 foreach my $link (@{$links{$page}}) {
18                         my $bestlink=bestlink($page, $link);
19                         if (length $bestlink && $bestlink ne $page) {
20                                 $backlinks{$bestlink}{$page}=1;
21                         }
22                 }
23         }
24         $backlinks_calculated=1;
25 } #}}}
26
27 sub backlinks ($) { #{{{
28         my $page=shift;
29
30         calculate_backlinks();
31
32         my @links;
33         return unless $backlinks{$page};
34         foreach my $p (keys %{$backlinks{$page}}) {
35                 my $href=abs2rel(htmlpage($p), dirname($page));
36                         
37                 # Trim common dir prefixes from both pages.
38                 my $p_trimmed=$p;
39                 my $page_trimmed=$page;
40                 my $dir;
41                 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
42                         defined $dir &&
43                         $p_trimmed=~s/^\Q$dir\E// &&
44                         $page_trimmed=~s/^\Q$dir\E//;
45                                
46                 push @links, { url => $href, page => pagetitle($p_trimmed) };
47         }
48
49         return sort { $a->{page} cmp $b->{page} } @links;
50 } #}}}
51
52 sub parentlinks ($) { #{{{
53         my $page=shift;
54         
55         my @ret;
56         my $pagelink="";
57         my $path="";
58         my $skip=1;
59         return if $page eq 'index'; # toplevel
60         foreach my $dir (reverse split("/", $page)) {
61                 if (! $skip) {
62                         $path.="../";
63                         unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
64                 }
65                 else {
66                         $skip=0;
67                 }
68         }
69         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
70         return @ret;
71 } #}}}
72
73 sub genpage ($$$) { #{{{
74         my $page=shift;
75         my $content=shift;
76         my $mtime=shift;
77
78         my $template=template("page.tmpl", blind_cache => 1);
79         my $actions=0;
80
81         if (length $config{cgiurl}) {
82                 $template->param(editurl => cgiurl(do => "edit", page => $page));
83                 $template->param(prefsurl => cgiurl(do => "prefs"));
84                 if ($config{rcs}) {
85                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
86                 }
87                 $actions++;
88         }
89
90         if (length $config{historyurl}) {
91                 my $u=$config{historyurl};
92                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
93                 $template->param(historyurl => $u);
94                 $actions++;
95         }
96         if ($config{discussion} && (length $config{cgiurl} || exists $links{$page."/".gettext("discussion")})) {
97                 $template->param(discussionlink => htmllink($page, $page, gettext("Discussion"), 1, 1));
98                 $actions++;
99         }
100
101         if ($actions) {
102                 $template->param(have_actions => 1);
103         }
104
105         $template->param(
106                 title => $page eq 'index' 
107                         ? $config{wikiname} 
108                         : pagetitle(basename($page)),
109                 wikiname => $config{wikiname},
110                 parentlinks => [parentlinks($page)],
111                 content => $content,
112                 backlinks => [backlinks($page)],
113                 mtime => displaytime($mtime),
114                 baseurl => baseurl($page),
115         );
116
117         run_hooks(pagetemplate => sub {
118                 shift->(page => $page, destpage => $page, template => $template);
119         });
120         
121         $content=$template->output;
122
123         run_hooks(format => sub {
124                 $content=shift->(
125                         page => $page,
126                         content => $content,
127                 );
128         });
129
130         return $content;
131 } #}}}
132
133 sub mtime ($) { #{{{
134         my $file=shift;
135         
136         return (stat($file))[9];
137 } #}}}
138
139 sub scan ($) { #{{{
140         my $file=shift;
141
142         my $type=pagetype($file);
143         if (defined $type) {
144                 my $srcfile=srcfile($file);
145                 my $content=readfile($srcfile);
146                 my $page=pagename($file);
147                 will_render($page, htmlpage($page), 1);
148
149                 # Always needs to be done, since filters might add links
150                 # to the content.
151                 $content=filter($page, $content);
152
153                 my @links;
154                 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
155                         push @links, titlepage($2);
156                 }
157                 if ($config{discussion}) {
158                         # Discussion links are a special case since they're
159                         # not in the text of the page, but on its template.
160                         push @links, $page."/".gettext("discussion");
161                 }
162                 $links{$page}=\@links;
163                 
164                 # Preprocess in scan-only mode.
165                 preprocess($page, $page, $content, 1);
166         }
167         else {
168                 will_render($file, $file, 1);
169         }
170 } #}}}
171
172 sub render ($) { #{{{
173         my $file=shift;
174         
175         my $type=pagetype($file);
176         my $srcfile=srcfile($file);
177         if (defined $type) {
178                 my $content=readfile($srcfile);
179                 my $page=pagename($file);
180                 delete $depends{$page};
181                 will_render($page, htmlpage($page), 1);
182                 
183                 $content=filter($page, $content);
184                 $content=preprocess($page, $page, $content);
185                 $content=linkify($page, $page, $content);
186                 $content=htmlize($page, $type, $content);
187                 
188                 writefile(htmlpage($page), $config{destdir},
189                         genpage($page, $content, mtime($srcfile)));
190                 $oldpagemtime{$page}=time;
191         }
192         else {
193                 my $content=readfile($srcfile, 1);
194                 delete $depends{$file};
195                 will_render($file, $file, 1);
196                 writefile($file, $config{destdir}, $content, 1);
197                 $oldpagemtime{$file}=time;
198         }
199 } #}}}
200
201 sub prune ($) { #{{{
202         my $file=shift;
203
204         unlink($file);
205         my $dir=dirname($file);
206         while (rmdir($dir)) {
207                 $dir=dirname($dir);
208         }
209 } #}}}
210
211 sub refresh () { #{{{
212         # find existing pages
213         my %exists;
214         my @files;
215         eval q{use File::Find};
216         error($@) if $@;
217         find({
218                 no_chdir => 1,
219                 wanted => sub {
220                         $_=decode_utf8($_);
221                         if (file_pruned($_, $config{srcdir})) {
222                                 $File::Find::prune=1;
223                         }
224                         elsif (! -d $_ && ! -l $_) {
225                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
226                                 if (! defined $f) {
227                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
228                                 }
229                                 else {
230                                         $f=~s/^\Q$config{srcdir}\E\/?//;
231                                         push @files, $f;
232                                         $exists{pagename($f)}=1;
233                                 }
234                         }
235                 },
236         }, $config{srcdir});
237         find({
238                 no_chdir => 1,
239                 wanted => sub {
240                         $_=decode_utf8($_);
241                         if (file_pruned($_, $config{underlaydir})) {
242                                 $File::Find::prune=1;
243                         }
244                         elsif (! -d $_ && ! -l $_) {
245                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
246                                 if (! defined $f) {
247                                         warn(sprintf(gettext("skipping bad filename %s"), $_)."\n");
248                                 }
249                                 else {
250                                         # Don't add pages that are in the
251                                         # srcdir.
252                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
253                                         if (! -e "$config{srcdir}/$f" && 
254                                             ! -l "$config{srcdir}/$f") {
255                                                 my $page=pagename($f);
256                                                 if (! $exists{$page}) {
257                                                         push @files, $f;
258                                                         $exists{$page}=1;
259                                                 }
260                                         }
261                                 }
262                         }
263                 },
264         }, $config{underlaydir});
265
266         my %rendered;
267
268         # check for added or removed pages
269         my @add;
270         foreach my $file (@files) {
271                 my $page=pagename($file);
272                 $pagesources{$page}=$file;
273                 if (! $oldpagemtime{$page}) {
274                         push @add, $file;
275                         $pagecase{lc $page}=$page;
276                         if ($config{getctime} && -e "$config{srcdir}/$file") {
277                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
278                         }
279                         elsif (! exists $pagectime{$page}) {
280                                 $pagectime{$page}=mtime(srcfile($file));
281                         }
282                 }
283         }
284         my @del;
285         foreach my $page (keys %oldpagemtime) {
286                 if (! $exists{$page}) {
287                         debug(sprintf(gettext("removing old page %s"), $page));
288                         push @del, $pagesources{$page};
289                         $links{$page}=[];
290                         $renderedfiles{$page}=[];
291                         $oldpagemtime{$page}=0;
292                         prune($config{destdir}."/".$_)
293                                 foreach @{$oldrenderedfiles{$page}};
294                         delete $pagesources{$page};
295                 }
296         }
297
298         # scan changed and new files
299         my @changed;
300         foreach my $file (@files) {
301                 my $page=pagename($file);
302                 
303                 if (! exists $oldpagemtime{$page} ||
304                     mtime(srcfile($file)) > $oldpagemtime{$page} ||
305                     $forcerebuild{$page}) {
306                         debug(sprintf(gettext("scanning %s"), $file));
307                         push @changed, $file;
308                         scan($file);
309                 }
310         }
311         calculate_backlinks();
312
313         # render changed and new pages
314         foreach my $file (@changed) {
315                 debug(sprintf(gettext("rendering %s"), $file));
316                 render($file);
317                 $rendered{$file}=1;
318         }
319         
320         # rebuild pages that link to added or removed pages
321         if (@add || @del) {
322                 foreach my $f (@add, @del) {
323                         my $p=pagename($f);
324                         foreach my $page (keys %{$backlinks{$p}}) {
325                                 my $file=$pagesources{$page};
326                                 next if $rendered{$file};
327                                 debug(sprintf(gettext("rendering %s, which links to %s"), $file, $p));
328                                 render($file);
329                                 $rendered{$file}=1;
330                         }
331                 }
332         }
333
334         if (%rendered || @del) {
335                 # rebuild dependant pages
336                 foreach my $f (@files) {
337                         next if $rendered{$f};
338                         my $p=pagename($f);
339                         if (exists $depends{$p}) {
340                                 foreach my $file (keys %rendered, @del) {
341                                         next if $f eq $file;
342                                         my $page=pagename($file);
343                                         if (pagespec_match($page, $depends{$p})) {
344                                                 debug(sprintf(gettext("rendering %s, which depends on %s"), $f, $page));
345                                                 render($f);
346                                                 $rendered{$f}=1;
347                                                 last;
348                                         }
349                                 }
350                         }
351                 }
352                 
353                 # handle backlinks; if a page has added/removed links,
354                 # update the pages it links to
355                 my %linkchanged;
356                 foreach my $file (keys %rendered, @del) {
357                         my $page=pagename($file);
358                         
359                         if (exists $links{$page}) {
360                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
361                                         if (length $link &&
362                                             (! exists $oldlinks{$page} ||
363                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
364                                                 $linkchanged{$link}=1;
365                                         }
366                                 }
367                         }
368                         if (exists $oldlinks{$page}) {
369                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
370                                         if (length $link &&
371                                             (! exists $links{$page} || 
372                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
373                                                 $linkchanged{$link}=1;
374                                         }
375                                 }
376                         }
377                 }
378                 foreach my $link (keys %linkchanged) {
379                         my $linkfile=$pagesources{$link};
380                         if (defined $linkfile) {
381                                 next if $rendered{$linkfile};
382                                 debug(sprintf(gettext("rendering %s, to update its backlinks"), $linkfile));
383                                 render($linkfile);
384                                 $rendered{$linkfile}=1;
385                         }
386                 }
387         }
388
389         # remove no longer rendered files
390         foreach my $src (keys %rendered) {
391                 my $page=pagename($src);
392                 foreach my $file (@{$oldrenderedfiles{$page}}) {
393                         if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
394                                 debug(sprintf(gettext("removing %s, no longer rendered by %s"), $file, $page));
395                                 prune($config{destdir}."/".$file);
396                         }
397                 }
398         }
399
400         if (@del) {
401                 run_hooks(delete => sub { shift->(@del) });
402         }
403         if (%rendered) {
404                 run_hooks(change => sub { shift->(keys %rendered) });
405         }
406 } #}}}
407
408 sub commandline_render () { #{{{
409         loadplugins();
410         checkconfig();
411         lockwiki();
412         loadindex();
413         unlockwiki();
414
415         my $srcfile=possibly_foolish_untaint($config{render});
416         my $file=$srcfile;
417         $file=~s/\Q$config{srcdir}\E\/?//;
418
419         my $type=pagetype($file);
420         die sprintf(gettext("ikiwiki: cannot render %s"), $srcfile)."\n" unless defined $type;
421         my $content=readfile($srcfile);
422         my $page=pagename($file);
423         $pagesources{$page}=$file;
424         $content=filter($page, $content);
425         $content=preprocess($page, $page, $content);
426         $content=linkify($page, $page, $content);
427         $content=htmlize($page, $type, $content);
428
429         print genpage($page, $content, mtime($srcfile));
430         exit 0;
431 } #}}}
432
433 1