fix for already relative urls
[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 rsspage ($) { #{{{
83         my $page=shift;
84
85         return $page.".rss";
86 } #}}}
87
88 sub genpage ($$$) { #{{{
89         my $content=shift;
90         my $page=shift;
91         my $mtime=shift;
92
93         my $title=pagetitle(basename($page));
94         
95         my $template=HTML::Template->new(blind_cache => 1,
96                 filename => "$config{templatedir}/page.tmpl");
97         
98         if (length $config{cgiurl}) {
99                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
100                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
101                 if ($config{rcs}) {
102                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
103                 }
104         }
105
106         if (length $config{historyurl}) {
107                 my $u=$config{historyurl};
108                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
109                 $template->param(historyurl => $u);
110         }
111
112         if ($config{rss}) {
113                 $template->param(rssurl => rsspage($page));
114         }
115         
116         $template->param(
117                 title => $title,
118                 wikiname => $config{wikiname},
119                 parentlinks => [parentlinks($page)],
120                 content => $content,
121                 backlinks => [backlinks($page)],
122                 discussionlink => htmllink($page, "Discussion", 1, 1),
123                 mtime => scalar(gmtime($mtime)),
124         );
125         
126         return $template->output;
127 } #}}}
128
129 sub date_822 ($) { #{{{
130         my $time=shift;
131
132         eval q{use POSIX};
133         return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
134 } #}}}
135
136 sub absolute_urls ($$) { #{{{
137         my $content=shift;
138         my $url=shift;
139
140         $url=~s/[^\/]+$//;
141         
142         $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
143         $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
144         return $content;
145 } #}}}
146
147 sub genrss ($$$) { #{{{
148         my $content=shift;
149         my $page=shift;
150         my $mtime=shift;
151
152         my $url="$config{url}/".htmlpage($page);
153         
154         my $template=HTML::Template->new(blind_cache => 1,
155                 filename => "$config{templatedir}/rsspage.tmpl");
156         
157         # Regular page gets a feed that is updated every time the
158         # page is changed, so the mtime is encoded in the guid.
159         my @items=(
160                 {
161                         itemtitle => pagetitle(basename($page)),
162                         itemguid => "$url?mtime=$mtime",
163                         itemurl => $url,
164                         itempubdate => date_822($mtime),
165                         itemcontent => absolute_urls($content, $url), # rss sucks
166                 },
167         );
168         
169         $template->param(
170                 title => $config{wikiname},
171                 pageurl => $url,
172                 items => \@items,
173         );
174         
175         return $template->output;
176 } #}}}
177
178 sub check_overwrite ($$) { #{{{
179         # Important security check. Make sure to call this before saving
180         # any files to the source directory.
181         my $dest=shift;
182         my $src=shift;
183         
184         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
185                 error("$dest already exists and was rendered from ".
186                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
187                                 %renderedfiles)).
188                         ", before, so not rendering from $src");
189         }
190 } #}}}
191
192 sub mtime ($) { #{{{
193         my $page=shift;
194         
195         return (stat($page))[9];
196 } #}}}
197
198 sub findlinks ($$) { #{{{
199         my $content=shift;
200         my $page=shift;
201
202         my @links;
203         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
204                 push @links, lc($1);
205         }
206         # Discussion links are a special case since they're not in the text
207         # of the page, but on its template.
208         return @links, "$page/discussion";
209 } #}}}
210
211 sub render ($) { #{{{
212         my $file=shift;
213         
214         my $type=pagetype($file);
215         my $content=readfile("$config{srcdir}/$file");
216         if ($type ne 'unknown') {
217                 my $page=pagename($file);
218                 
219                 $links{$page}=[findlinks($content, $page)];
220                 
221                 $content=linkify($content, $page);
222                 $content=htmlize($type, $content);
223                 
224                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
225                 writefile("$config{destdir}/".htmlpage($page),
226                         genpage($content, $page, mtime("$config{srcdir}/$file")));              
227                 $oldpagemtime{$page}=time;
228                 $renderedfiles{$page}=htmlpage($page);
229
230                 # TODO: should really add this to renderedfiles and call
231                 # check_overwrite, as above, but currently renderedfiles
232                 # only supports listing one file per page.
233                 if ($config{rss}) {
234                         writefile("$config{destdir}/".rsspage($page),
235                                 genrss($content, $page, mtime("$config{srcdir}/$file")));
236                 }
237         }
238         else {
239                 $links{$file}=[];
240                 check_overwrite("$config{destdir}/$file", $file);
241                 writefile("$config{destdir}/$file", $content);
242                 $oldpagemtime{$file}=time;
243                 $renderedfiles{$file}=$file;
244         }
245 } #}}}
246
247 sub prune ($) { #{{{
248         my $file=shift;
249
250         unlink($file);
251         my $dir=dirname($file);
252         while (rmdir($dir)) {
253                 $dir=dirname($dir);
254         }
255 } #}}}
256
257 sub refresh () { #{{{
258         # find existing pages
259         my %exists;
260         my @files;
261         eval q{use File::Find};
262         find({
263                 no_chdir => 1,
264                 wanted => sub {
265                         if (/$config{wiki_file_prune_regexp}/) {
266                                 no warnings 'once';
267                                 $File::Find::prune=1;
268                                 use warnings "all";
269                         }
270                         elsif (! -d $_ && ! -l $_) {
271                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
272                                 if (! defined $f) {
273                                         warn("skipping bad filename $_\n");
274                                 }
275                                 else {
276                                         $f=~s/^\Q$config{srcdir}\E\/?//;
277                                         push @files, $f;
278                                         $exists{pagename($f)}=1;
279                                 }
280                         }
281                 },
282         }, $config{srcdir});
283
284         my %rendered;
285
286         # check for added or removed pages
287         my @add;
288         foreach my $file (@files) {
289                 my $page=pagename($file);
290                 if (! $oldpagemtime{$page}) {
291                         debug("new page $page");
292                         push @add, $file;
293                         $links{$page}=[];
294                         $pagesources{$page}=$file;
295                 }
296         }
297         my @del;
298         foreach my $page (keys %oldpagemtime) {
299                 if (! $exists{$page}) {
300                         debug("removing old page $page");
301                         push @del, $pagesources{$page};
302                         prune($config{destdir}."/".$renderedfiles{$page});
303                         delete $renderedfiles{$page};
304                         $oldpagemtime{$page}=0;
305                         delete $pagesources{$page};
306                 }
307         }
308         
309         # render any updated files
310         foreach my $file (@files) {
311                 my $page=pagename($file);
312                 
313                 if (! exists $oldpagemtime{$page} ||
314                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
315                         debug("rendering changed file $file");
316                         render($file);
317                         $rendered{$file}=1;
318                 }
319         }
320         
321         # if any files were added or removed, check to see if each page
322         # needs an update due to linking to them
323         # TODO: inefficient; pages may get rendered above and again here;
324         # problem is the bestlink may have changed and we won't know until
325         # now
326         if (@add || @del) {
327 FILE:           foreach my $file (@files) {
328                         my $page=pagename($file);
329                         foreach my $f (@add, @del) {
330                                 my $p=pagename($f);
331                                 foreach my $link (@{$links{$page}}) {
332                                         if (bestlink($page, $link) eq $p) {
333                                                 debug("rendering $file, which links to $p");
334                                                 render($file);
335                                                 $rendered{$file}=1;
336                                                 next FILE;
337                                         }
338                                 }
339                         }
340                 }
341         }
342
343         # handle backlinks; if a page has added/removed links, update the
344         # pages it links to
345         # TODO: inefficient; pages may get rendered above and again here;
346         # problem is the backlinks could be wrong in the first pass render
347         # above
348         if (%rendered) {
349                 my %linkchanged;
350                 foreach my $file (keys %rendered, @del) {
351                         my $page=pagename($file);
352                         if (exists $links{$page}) {
353                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
354                                         if (length $link &&
355                                             ! exists $oldlinks{$page} ||
356                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
357                                                 $linkchanged{$link}=1;
358                                         }
359                                 }
360                         }
361                         if (exists $oldlinks{$page}) {
362                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
363                                         if (length $link &&
364                                             ! exists $links{$page} ||
365                                             ! grep { $_ eq $link } @{$links{$page}}) {
366                                                 $linkchanged{$link}=1;
367                                         }
368                                 }
369                         }
370                 }
371                 foreach my $link (keys %linkchanged) {
372                         my $linkfile=$pagesources{$link};
373                         if (defined $linkfile) {
374                                 debug("rendering $linkfile, to update its backlinks");
375                                 render($linkfile);
376                         }
377                 }
378         }
379 } #}}}
380
381 1