Getopt::Long is a huge, heavy perl module. So why use it?
[ikiwiki.git] / 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=basename($page);
88         $title=~s/_/ /g;
89         
90         my $template=HTML::Template->new(blind_cache => 1,
91                 filename => "$config{templatedir}/page.tmpl");
92         
93         if (length $config{cgiurl}) {
94                 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
95                 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
96                 if ($config{rcs}) {
97                         $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
98                 }
99         }
100
101         if (length $config{historyurl}) {
102                 my $u=$config{historyurl};
103                 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
104                 $template->param(historyurl => $u);
105         }
106         
107         $template->param(
108                 title => $title,
109                 wikiname => $config{wikiname},
110                 parentlinks => [parentlinks($page)],
111                 content => $content,
112                 backlinks => [backlinks($page)],
113                 discussionlink => htmllink($page, "Discussion", 1, 1),
114                 mtime => scalar(gmtime($mtime)),
115         );
116         
117         return $template->output;
118 } #}}}
119
120 sub check_overwrite ($$) { #{{{
121         # Important security check. Make sure to call this before saving
122         # any files to the source directory.
123         my $dest=shift;
124         my $src=shift;
125         
126         if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
127                 error("$dest already exists and was rendered from ".
128                         join(" ",(grep { $renderedfiles{$_} eq $dest } keys
129                                 %renderedfiles)).
130                         ", before, so not rendering from $src");
131         }
132 } #}}}
133
134 sub mtime ($) { #{{{
135         my $page=shift;
136         
137         return (stat($page))[9];
138 } #}}}
139
140 sub findlinks ($$) { #{{{
141         my $content=shift;
142         my $page=shift;
143
144         my @links;
145         while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
146                 push @links, lc($1);
147         }
148         # Discussion links are a special case since they're not in the text
149         # of the page, but on its template.
150         return @links, "$page/discussion";
151 } #}}}
152
153 sub render ($) { #{{{
154         my $file=shift;
155         
156         my $type=pagetype($file);
157         my $content=readfile("$config{srcdir}/$file");
158         if ($type ne 'unknown') {
159                 my $page=pagename($file);
160                 
161                 $links{$page}=[findlinks($content, $page)];
162                 
163                 $content=linkify($content, $page);
164                 $content=htmlize($type, $content);
165                 $content=finalize($content, $page,
166                         mtime("$config{srcdir}/$file"));
167                 
168                 check_overwrite("$config{destdir}/".htmlpage($page), $page);
169                 writefile("$config{destdir}/".htmlpage($page), $content);
170                 $oldpagemtime{$page}=time;
171                 $renderedfiles{$page}=htmlpage($page);
172         }
173         else {
174                 $links{$file}=[];
175                 check_overwrite("$config{destdir}/$file", $file);
176                 writefile("$config{destdir}/$file", $content);
177                 $oldpagemtime{$file}=time;
178                 $renderedfiles{$file}=$file;
179         }
180 } #}}}
181
182 sub prune ($) { #{{{
183         my $file=shift;
184
185         unlink($file);
186         my $dir=dirname($file);
187         while (rmdir($dir)) {
188                 $dir=dirname($dir);
189         }
190 } #}}}
191
192 sub refresh () { #{{{
193         # find existing pages
194         my %exists;
195         my @files;
196         eval q{use File::Find};
197         find({
198                 no_chdir => 1,
199                 wanted => sub {
200                         if (/$config{wiki_file_prune_regexp}/) {
201                                 no warnings 'once';
202                                 $File::Find::prune=1;
203                                 use warnings "all";
204                         }
205                         elsif (! -d $_ && ! -l $_) {
206                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
207                                 if (! defined $f) {
208                                         warn("skipping bad filename $_\n");
209                                 }
210                                 else {
211                                         $f=~s/^\Q$config{srcdir}\E\/?//;
212                                         push @files, $f;
213                                         $exists{pagename($f)}=1;
214                                 }
215                         }
216                 },
217         }, $config{srcdir});
218
219         my %rendered;
220
221         # check for added or removed pages
222         my @add;
223         foreach my $file (@files) {
224                 my $page=pagename($file);
225                 if (! $oldpagemtime{$page}) {
226                         debug("new page $page");
227                         push @add, $file;
228                         $links{$page}=[];
229                         $pagesources{$page}=$file;
230                 }
231         }
232         my @del;
233         foreach my $page (keys %oldpagemtime) {
234                 if (! $exists{$page}) {
235                         debug("removing old page $page");
236                         push @del, $pagesources{$page};
237                         prune($config{destdir}."/".$renderedfiles{$page});
238                         delete $renderedfiles{$page};
239                         $oldpagemtime{$page}=0;
240                         delete $pagesources{$page};
241                 }
242         }
243         
244         # render any updated files
245         foreach my $file (@files) {
246                 my $page=pagename($file);
247                 
248                 if (! exists $oldpagemtime{$page} ||
249                     mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
250                         debug("rendering changed file $file");
251                         render($file);
252                         $rendered{$file}=1;
253                 }
254         }
255         
256         # if any files were added or removed, check to see if each page
257         # needs an update due to linking to them
258         # TODO: inefficient; pages may get rendered above and again here;
259         # problem is the bestlink may have changed and we won't know until
260         # now
261         if (@add || @del) {
262 FILE:           foreach my $file (@files) {
263                         my $page=pagename($file);
264                         foreach my $f (@add, @del) {
265                                 my $p=pagename($f);
266                                 foreach my $link (@{$links{$page}}) {
267                                         if (bestlink($page, $link) eq $p) {
268                                                 debug("rendering $file, which links to $p");
269                                                 render($file);
270                                                 $rendered{$file}=1;
271                                                 next FILE;
272                                         }
273                                 }
274                         }
275                 }
276         }
277
278         # handle backlinks; if a page has added/removed links, update the
279         # pages it links to
280         # TODO: inefficient; pages may get rendered above and again here;
281         # problem is the backlinks could be wrong in the first pass render
282         # above
283         if (%rendered) {
284                 my %linkchanged;
285                 foreach my $file (keys %rendered, @del) {
286                         my $page=pagename($file);
287                         if (exists $links{$page}) {
288                                 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
289                                         if (length $link &&
290                                             ! exists $oldlinks{$page} ||
291                                             ! grep { $_ eq $link } @{$oldlinks{$page}}) {
292                                                 $linkchanged{$link}=1;
293                                         }
294                                 }
295                         }
296                         if (exists $oldlinks{$page}) {
297                                 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
298                                         if (length $link &&
299                                             ! exists $links{$page} ||
300                                             ! grep { $_ eq $link } @{$links{$page}}) {
301                                                 $linkchanged{$link}=1;
302                                         }
303                                 }
304                         }
305                 }
306                 foreach my $link (keys %linkchanged) {
307                         my $linkfile=$pagesources{$link};
308                         if (defined $linkfile) {
309                                 debug("rendering $linkfile, to update its backlinks");
310                                 render($linkfile);
311                         }
312                 }
313         }
314 } #}}}
315
316 1