support arbitrary characters in page titles, via some ugly use of unicode
[ikiwiki] / IkiWiki / Render.pm
1 package IkiWiki;
2
3 use warnings;
4 use strict;
5 use File::Spec;
6
7 sub linkify ($$) { #{{{
8         my $content=shift;
9         my $page=shift;
10
11         $content =~ s{(\\?)$config{wiki_link_regexp}}{
12                 $1 ? "[[$2]]" : htmllink($page, $2)
13         }eg;
14         
15         return $content;
16 } #}}}
17
18 sub htmlize ($$) { #{{{
19         my $type=shift;
20         my $content=shift;
21         
22         if (! $INC{"/usr/bin/markdown"}) {
23                 no warnings 'once';
24                 $blosxom::version="is a proper perl module too much to ask?";
25                 use warnings 'all';
26                 do "/usr/bin/markdown";
27         }
28         
29         if ($type eq '.mdwn') {
30                 return Markdown::Markdown($content);
31         }
32         else {
33                 error("htmlization of $type not supported");
34         }
35 } #}}}
36
37 sub backlinks ($) { #{{{
38         my $page=shift;
39
40         my @links;
41         foreach my $p (keys %links) {
42                 next if bestlink($page, $p) eq $page;
43                 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
44                         my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
45                         
46                         # Trim common dir prefixes from both pages.
47                         my $p_trimmed=$p;
48                         my $page_trimmed=$page;
49                         my $dir;
50                         1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
51                                 defined $dir &&
52                                 $p_trimmed=~s/^\Q$dir\E// &&
53                                 $page_trimmed=~s/^\Q$dir\E//;
54                                        
55                         push @links, { url => $href, page => $p_trimmed };
56                 }
57         }
58
59         return sort { $a->{page} cmp $b->{page} } @links;
60 } #}}}
61
62 sub parentlinks ($) { #{{{
63         my $page=shift;
64         
65         my @ret;
66         my $pagelink="";
67         my $path="";
68         my $skip=1;
69         foreach my $dir (reverse split("/", $page)) {
70                 if (! $skip) {
71                         $path.="../";
72                         unshift @ret, { url => "$path$dir.html", page => $dir };
73                 }
74                 else {
75                         $skip=0;
76                 }
77         }
78         unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
79         return @ret;
80 } #}}}
81
82 sub finalize ($$$) { #{{{
83         my $content=shift;
84         my $page=shift;
85         my $mtime=shift;
86
87         my $title=pagetitle(basename($page));
88         
89         my $template=HTML::Template->new(blind_cache => 1,
90                 filename => "$config{templatedir}/page.tmpl");
91         
92         if (length $config{cgiurl}) {
93                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
94                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
95                 if ($config{rcs}) {
96                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
97                 }
98         }
99
100         if (length $config{historyurl}) {
101                 my $u=$config{historyurl};
102                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
103                 $template->param(historyurl => $u);
104         }
105         
106         $template->param(
107                 title => $title,
108                 wikiname => $config{wikiname},
109                 parentlinks => [parentlinks($page)],
110                 content => $content,
111                 backlinks => [backlinks($page)],
112                 discussionlink => htmllink($page, "Discussion", 1, 1),
113                 mtime => scalar(gmtime($mtime)),
114         );
115         
116         return $template->output;
117 } #}}}
118
119 sub check_overwrite ($$) { #{{{
120         # Important security check. Make sure to call this before saving
121         # any files to the source directory.
122         my $dest=shift;
123         my $src=shift;
124         
125         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
126                 error("$dest already exists and was rendered from ".
127                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
128                                 %renderedfiles)).
129                         ", before, so not rendering from $src");
130         }
131 } #}}}
132
133 sub mtime ($) { #{{{
134         my $page=shift;
135         
136         return (stat($page))[9];
137 } #}}}
138
139 sub findlinks ($$) { #{{{
140         my $content=shift;
141         my $page=shift;
142
143         my @links;
144         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
145                 push @links, lc($1);
146         }
147         # Discussion links are a special case since they're not in the text
148         # of the page, but on its template.
149         return @links, "$page/discussion";
150 } #}}}
151
152 sub render ($) { #{{{
153         my $file=shift;
154         
155         my $type=pagetype($file);
156         my $content=readfile("$config{srcdir}/$file");
157         if ($type ne 'unknown') {
158                 my $page=pagename($file);
159                 
160                 $links{$page}=[findlinks($content, $page)];
161                 
162                 $content=linkify($content, $page);
163                 $content=htmlize($type, $content);
164                 $content=finalize($content, $page,
165                         mtime("$config{srcdir}/$file"));
166                 
167                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
168                 writefile("$config{destdir}/".htmlpage($page), $content);
169                 $oldpagemtime{$page}=time;
170                 $renderedfiles{$page}=htmlpage($page);
171         }
172         else {
173                 $links{$file}=[];
174                 check_overwrite("$config{destdir}/$file", $file);
175                 writefile("$config{destdir}/$file", $content);
176                 $oldpagemtime{$file}=time;
177                 $renderedfiles{$file}=$file;
178         }
179 } #}}}
180
181 sub prune ($) { #{{{
182         my $file=shift;
183
184         unlink($file);
185         my $dir=dirname($file);
186         while (rmdir($dir)) {
187                 $dir=dirname($dir);
188         }
189 } #}}}
190
191 sub refresh () { #{{{
192         # find existing pages
193         my %exists;
194         my @files;
195         eval q{use File::Find};
196         find({
197                 no_chdir => 1,
198                 wanted => sub {
199                         if (/$config{wiki_file_prune_regexp}/) {
200                                 no warnings 'once';
201                                 $File::Find::prune=1;
202                                 use warnings "all";
203                         }
204                         elsif (! -d $_ && ! -l $_) {
205                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
206                                 if (! defined $f) {
207                                         warn("skipping bad filename $_\n");
208                                 }
209                                 else {
210                                         $f=~s/^\Q$config{srcdir}\E\/?//;
211                                         push @files, $f;
212                                         $exists{pagename($f)}=1;
213                                 }
214                         }
215                 },
216         }, $config{srcdir});
217
218         my %rendered;
219
220         # check for added or removed pages
221         my @add;
222         foreach my $file (@files) {
223                 my $page=pagename($file);
224                 if (! $oldpagemtime{$page}) {
225                         debug("new page $page");
226                         push @add, $file;
227                         $links{$page}=[];
228                         $pagesources{$page}=$file;
229                 }
230         }
231         my @del;
232         foreach my $page (keys %oldpagemtime) {
233                 if (! $exists{$page}) {
234                         debug("removing old page $page");
235                         push @del, $pagesources{$page};
236                         prune($config{destdir}."/".$renderedfiles{$page});
237                         delete $renderedfiles{$page};
238                         $oldpagemtime{$page}=0;
239                         delete $pagesources{$page};
240                 }
241         }
242         
243         # render any updated files
244         foreach my $file (@files) {
245                 my $page=pagename($file);
246                 
247                 if (! exists $oldpagemtime{$page} ||
248                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
249                         debug("rendering changed file $file");
250                         render($file);
251                         $rendered{$file}=1;
252                 }
253         }
254         
255         # if any files were added or removed, check to see if each page
256         # needs an update due to linking to them
257         # TODO: inefficient; pages may get rendered above and again here;
258         # problem is the bestlink may have changed and we won't know until
259         # now
260         if (@add || @del) {
261 FILE:           foreach my $file (@files) {
262                         my $page=pagename($file);
263                         foreach my $f (@add, @del) {
264                                 my $p=pagename($f);
265                                 foreach my $link (@{$links{$page}}) {
266                                         if (bestlink($page, $link) eq $p) {
267                                                 debug("rendering $file, which links to $p");
268                                                 render($file);
269                                                 $rendered{$file}=1;
270                                                 next FILE;
271                                         }
272                                 }
273                         }
274                 }
275         }
276
277         # handle backlinks; if a page has added/removed links, update the
278         # pages it links to
279         # TODO: inefficient; pages may get rendered above and again here;
280         # problem is the backlinks could be wrong in the first pass render
281         # above
282         if (%rendered) {
283                 my %linkchanged;
284                 foreach my $file (keys %rendered, @del) {
285                         my $page=pagename($file);
286                         if (exists $links{$page}) {
287                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
288                                         if (length $link &&
289                                             ! exists $oldlinks{$page} ||
290                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
291                                                 $linkchanged{$link}=1;
292                                         }
293                                 }
294                         }
295                         if (exists $oldlinks{$page}) {
296                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
297                                         if (length $link &&
298                                             ! exists $links{$page} ||
299                                             ! grep { $_ eq $link } @{$links{$page}}) {
300                                                 $linkchanged{$link}=1;
301                                         }
302                                 }
303                         }
304                 }
305                 foreach my $link (keys %linkchanged) {
306                         my $linkfile=$pagesources{$link};
307                         if (defined $linkfile) {
308                                 debug("rendering $linkfile, to update its backlinks");
309                                 render($linkfile);
310                         }
311                 }
312         }
313 } #}}}
314
315 1