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