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