]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Render.pm
web commit by joey: revert
[git.ikiwiki.info.git] / IkiWiki / Render.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use File::Spec;
8 use IkiWiki;
10 sub linkify ($$$) { #{{{
11         my $lpage=shift;
12         my $page=shift;
13         my $content=shift;
15         $content =~ s{(\\?)$config{wiki_link_regexp}}{
16                 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
17                    : ( $1 ? "[[$3]]" :    htmllink($lpage, $page, titlepage($3)))
18         }eg;
19         
20         return $content;
21 } #}}}
23 sub htmlize ($$) { #{{{
24         my $type=shift;
25         my $content=shift;
26         
27         if (! $INC{"/usr/bin/markdown"}) {
28                 no warnings 'once';
29                 $blosxom::version="is a proper perl module too much to ask?";
30                 use warnings 'all';
31                 do "/usr/bin/markdown";
32                 use Encode;
33         }
34         
35         if ($type eq '.mdwn') {
36                 # Workaround for perl bug (#376329)
37                 $content=Encode::encode_utf8($content);
38                 $content=Encode::encode_utf8($content);
39                 $content=Markdown::Markdown($content);
40                 $content=Encode::decode_utf8($content);
41                 $content=Encode::decode_utf8($content);
42         }
43         else {
44                 error("htmlization of $type not supported");
45         }
47         if (exists $hooks{sanitize}) {
48                 foreach my $id (keys %{$hooks{sanitize}}) {
49                         $content=$hooks{sanitize}{$id}{call}->($content);
50                 }
51         }
52         
53         return $content;
54 } #}}}
56 sub backlinks ($) { #{{{
57         my $page=shift;
59         my @links;
60         foreach my $p (keys %links) {
61                 next if bestlink($page, $p) eq $page;
62                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
63                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
64                         
65                         # Trim common dir prefixes from both pages.
66                         my $p_trimmed=$p;
67                         my $page_trimmed=$page;
68                         my $dir;
69                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
70                                 defined $dir &&
71                                 $p_trimmed=~s/^\Q$dir\E// &&
72                                 $page_trimmed=~s/^\Q$dir\E//;
73                                        
74                         push @links, { url => $href, page => $p_trimmed };
75                 }
76         }
78         return sort { $a->{page} cmp $b->{page} } @links;
79 } #}}}
81 sub parentlinks ($) { #{{{
82         my $page=shift;
83         
84         my @ret;
85         my $pagelink="";
86         my $path="";
87         my $skip=1;
88         foreach my $dir (reverse split("/", $page)) {
89                 if (! $skip) {
90                         $path.="../";
91                         unshift @ret, { url => "$path$dir.html", page => $dir };
92                 }
93                 else {
94                         $skip=0;
95                 }
96         }
97         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
98         return @ret;
99 } #}}}
101 sub preprocess ($$;$) { #{{{
102         my $page=shift;
103         my $content=shift;
104         my $onlystrip=shift || 0; # strip directives without processing
106         my $handle=sub {
107                 my $escape=shift;
108                 my $command=shift;
109                 my $params=shift;
110                 if (length $escape) {
111                         return "[[$command $params]]";
112                 }
113                 elsif ($onlystrip) {
114                         return "";
115                 }
116                 elsif (exists $hooks{preprocess}{$command}) {
117                         # Note: preserve order of params, some plugins may
118                         # consider it significant.
119                         my @params;
120                         while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
121                                 push @params, $1, $2;
122                         }
123                         return $hooks{preprocess}{$command}{call}->(@params, page => $page);
124                 }
125                 else {
126                         return "[[$command not processed]]";
127                 }
128         };
129         
130         $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
131         return $content;
132 } #}}}
134 sub add_depends ($$) { #{{{
135         my $page=shift;
136         my $globlist=shift;
137         
138         if (! exists $depends{$page}) {
139                 $depends{$page}=$globlist;
140         }
141         else {
142                 $depends{$page}=globlist_merge($depends{$page}, $globlist);
143         }
144 } # }}}
146 sub globlist_merge ($$) { #{{{
147         my $a=shift;
148         my $b=shift;
150         my $ret="";
151         # Only add negated globs if they are not matched by the other globlist.
152         foreach my $i ((map { [ $a, $_ ] } split(" ", $b)), 
153                        (map { [ $b, $_ ] } split(" ", $a))) {
154                 if ($i->[1]=~/^!(.*)/) {
155                         if (! globlist_match($1, $i->[0])) {
156                                 $ret.=" ".$i->[1];
157                         }
158                 }
159                 else {
160                         $ret.=" ".$i->[1];
161                 }
162         }
163         
164         return $ret;
165 } #}}}
167 sub genpage ($$$) { #{{{
168         my $page=shift;
169         my $content=shift;
170         my $mtime=shift;
172         my $title=pagetitle(basename($page));
173         
174         my $template=HTML::Template->new(blind_cache => 1,
175                 filename => "$config{templatedir}/page.tmpl");
176         my $actions=0;
178         if (length $config{cgiurl}) {
179                 $template->param(editurl => cgiurl(do => "edit", page => $page));
180                 $template->param(prefsurl => cgiurl(do => "prefs"));
181                 if ($config{rcs}) {
182                         $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
183                 }
184                 $actions++;
185         }
187         if (length $config{historyurl}) {
188                 my $u=$config{historyurl};
189                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
190                 $template->param(historyurl => $u);
191                 $actions++;
192         }
193         if ($config{discussion}) {
194                 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
195                 $actions++;
196         }
198         if ($actions) {
199                 $template->param(have_actions => 1);
200         }
202         $template->param(
203                 title => $title,
204                 wikiname => $config{wikiname},
205                 parentlinks => [parentlinks($page)],
206                 content => $content,
207                 backlinks => [backlinks($page)],
208                 mtime => displaytime($mtime),
209                 styleurl => styleurl($page),
210         );
212         if (exists $hooks{pagetemplate}) {
213                 foreach my $id (keys %{$hooks{pagetemplate}}) {
214                         $hooks{pagetemplate}{$id}{call}->($page, $template);
215                 }
216         }
217         
218         return $template->output;
219 } #}}}
221 sub check_overwrite ($$) { #{{{
222         # Important security check. Make sure to call this before saving
223         # any files to the source directory.
224         my $dest=shift;
225         my $src=shift;
226         
227         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
228                 error("$dest already exists and was rendered from ".
229                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
230                                 %renderedfiles)).
231                         ", before, so not rendering from $src");
232         }
233 } #}}}
235 sub displaytime ($) { #{{{
236         my $time=shift;
238         eval q{use POSIX};
239         # strftime doesn't know about encodings, so make sure
240         # its output is properly treated as utf8
241         return Encode::decode_utf8(POSIX::strftime(
242                         $config{timeformat}, localtime($time)));
243 } #}}}
245 sub mtime ($) { #{{{
246         my $file=shift;
247         
248         return (stat($file))[9];
249 } #}}}
251 sub findlinks ($$) { #{{{
252         my $page=shift;
253         my $content=shift;
255         my @links;
256         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
257                 push @links, titlepage($2);
258         }
259         if ($config{discussion}) {
260                 # Discussion links are a special case since they're not in the
261                 # text of the page, but on its template.
262                 return @links, "$page/discussion";
263         }
264         else {
265                 return @links;
266         }
267 } #}}}
269 sub render ($) { #{{{
270         my $file=shift;
271         
272         my $type=pagetype($file);
273         my $srcfile=srcfile($file);
274         if ($type ne 'unknown') {
275                 my $content=readfile($srcfile);
276                 my $page=pagename($file);
277                 delete $depends{$page};
278                 
279                 if (exists $hooks{filter}) {
280                         foreach my $id (keys %{$hooks{filter}}) {
281                                 $content=$hooks{filter}{$id}{call}->(
282                                         page => $page,
283                                         content => $content
284                                 );
285                         }
286                 }
287                 
288                 $links{$page}=[findlinks($page, $content)];
289                 
290                 $content=linkify($page, $page, $content);
291                 $content=preprocess($page, $content);
292                 $content=htmlize($type, $content);
293                 
294                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
295                 writefile(htmlpage($page), $config{destdir},
296                         genpage($page, $content, mtime($srcfile)));
297                 $oldpagemtime{$page}=time;
298                 $renderedfiles{$page}=htmlpage($page);
299         }
300         else {
301                 my $content=readfile($srcfile, 1);
302                 $links{$file}=[];
303                 delete $depends{$file};
304                 check_overwrite("$config{destdir}/$file", $file);
305                 writefile($file, $config{destdir}, $content, 1);
306                 $oldpagemtime{$file}=time;
307                 $renderedfiles{$file}=$file;
308         }
309 } #}}}
311 sub prune ($) { #{{{
312         my $file=shift;
314         unlink($file);
315         my $dir=dirname($file);
316         while (rmdir($dir)) {
317                 $dir=dirname($dir);
318         }
319 } #}}}
321 sub refresh () { #{{{
322         # find existing pages
323         my %exists;
324         my @files;
325         eval q{use File::Find};
326         find({
327                 no_chdir => 1,
328                 wanted => sub {
329                         if (/$config{wiki_file_prune_regexp}/) {
330                                 $File::Find::prune=1;
331                         }
332                         elsif (! -d $_ && ! -l $_) {
333                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
334                                 if (! defined $f) {
335                                         warn("skipping bad filename $_\n");
336                                 }
337                                 else {
338                                         $f=~s/^\Q$config{srcdir}\E\/?//;
339                                         push @files, $f;
340                                         $exists{pagename($f)}=1;
341                                 }
342                         }
343                 },
344         }, $config{srcdir});
345         find({
346                 no_chdir => 1,
347                 wanted => sub {
348                         if (/$config{wiki_file_prune_regexp}/) {
349                                 $File::Find::prune=1;
350                         }
351                         elsif (! -d $_ && ! -l $_) {
352                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
353                                 if (! defined $f) {
354                                         warn("skipping bad filename $_\n");
355                                 }
356                                 else {
357                                         # Don't add files that are in the
358                                         # srcdir.
359                                         $f=~s/^\Q$config{underlaydir}\E\/?//;
360                                         if (! -e "$config{srcdir}/$f" && 
361                                             ! -l "$config{srcdir}/$f") {
362                                                 push @files, $f;
363                                                 $exists{pagename($f)}=1;
364                                         }
365                                 }
366                         }
367                 },
368         }, $config{underlaydir});
370         my %rendered;
372         # check for added or removed pages
373         my @add;
374         foreach my $file (@files) {
375                 my $page=pagename($file);
376                 if (! $oldpagemtime{$page}) {
377                         debug("new page $page") unless exists $pagectime{$page};
378                         push @add, $file;
379                         $links{$page}=[];
380                         $pagesources{$page}=$file;
381                         if ($config{getctime} && -e "$config{srcdir}/$file") {
382                                 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
383                         }
384                         elsif (! exists $pagectime{$page}) {
385                                 $pagectime{$page}=mtime(srcfile($file));
386                         }
387                 }
388         }
389         my @del;
390         foreach my $page (keys %oldpagemtime) {
391                 if (! $exists{$page}) {
392                         debug("removing old page $page");
393                         push @del, $pagesources{$page};
394                         prune($config{destdir}."/".$renderedfiles{$page});
395                         delete $renderedfiles{$page};
396                         $oldpagemtime{$page}=0;
397                         delete $pagesources{$page};
398                 }
399         }
400         
401         # render any updated files
402         foreach my $file (@files) {
403                 my $page=pagename($file);
404                 
405                 if (! exists $oldpagemtime{$page} ||
406                     mtime(srcfile($file)) > $oldpagemtime{$page}) {
407                         debug("rendering changed file $file");
408                         render($file);
409                         $rendered{$file}=1;
410                 }
411         }
412         
413         # if any files were added or removed, check to see if each page
414         # needs an update due to linking to them or inlining them.
415         # TODO: inefficient; pages may get rendered above and again here;
416         # problem is the bestlink may have changed and we won't know until
417         # now
418         if (@add || @del) {
419 FILE:           foreach my $file (@files) {
420                         my $page=pagename($file);
421                         foreach my $f (@add, @del) {
422                                 my $p=pagename($f);
423                                 foreach my $link (@{$links{$page}}) {
424                                         if (bestlink($page, $link) eq $p) {
425                                                 debug("rendering $file, which links to $p");
426                                                 render($file);
427                                                 $rendered{$file}=1;
428                                                 next FILE;
429                                         }
430                                 }
431                         }
432                 }
433         }
435         # Handle backlinks; if a page has added/removed links, update the
436         # pages it links to. Also handles rebuilding dependant pages.
437         # TODO: inefficient; pages may get rendered above and again here;
438         # problem is the backlinks could be wrong in the first pass render
439         # above
440         if (%rendered || @del) {
441                 foreach my $f (@files) {
442                         my $p=pagename($f);
443                         if (exists $depends{$p}) {
444                                 foreach my $file (keys %rendered, @del) {
445                                         next if $f eq $file;
446                                         my $page=pagename($file);
447                                         if (globlist_match($page, $depends{$p})) {
448                                                 debug("rendering $f, which depends on $page");
449                                                 render($f);
450                                                 $rendered{$f}=1;
451                                                 last;
452                                         }
453                                 }
454                         }
455                 }
456                 
457                 my %linkchanged;
458                 foreach my $file (keys %rendered, @del) {
459                         my $page=pagename($file);
460                         
461                         if (exists $links{$page}) {
462                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
463                                         if (length $link &&
464                                             (! exists $oldlinks{$page} ||
465                                              ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
466                                                 $linkchanged{$link}=1;
467                                         }
468                                 }
469                         }
470                         if (exists $oldlinks{$page}) {
471                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
472                                         if (length $link &&
473                                             (! exists $links{$page} || 
474                                              ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
475                                                 $linkchanged{$link}=1;
476                                         }
477                                 }
478                         }
479                 }
480                 foreach my $link (keys %linkchanged) {
481                         my $linkfile=$pagesources{$link};
482                         if (defined $linkfile) {
483                                 debug("rendering $linkfile, to update its backlinks");
484                                 render($linkfile);
485                                 $rendered{$linkfile}=1;
486                         }
487                 }
488         }
490         if (@del && exists $hooks{delete}) {
491                 foreach my $id (keys %{$hooks{delete}}) {
492                         $hooks{delete}{$id}{call}->(@del);
493                 }
494         }
495         if (%rendered && exists $hooks{change}) {
496                 foreach my $id (keys %{$hooks{change}}) {
497                         $hooks{change}{$id}{call}->(keys %rendered);
498                 }
499         }
500 } #}}}