10 sub backlinks ($) { #{{{
14 foreach my $p (keys %links) {
15 next if bestlink($page, $p) eq $page;
16 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
17 my $href=abs2rel(htmlpage($p), dirname($page));
19 # Trim common dir prefixes from both pages.
21 my $page_trimmed=$page;
23 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
25 $p_trimmed=~s/^\Q$dir\E// &&
26 $page_trimmed=~s/^\Q$dir\E//;
28 push @links, { url => $href, page => pagetitle($p_trimmed) };
32 return sort { $a->{page} cmp $b->{page} } @links;
35 sub parentlinks ($) { #{{{
42 return if $page eq 'index'; # toplevel
43 foreach my $dir (reverse split("/", $page)) {
46 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
52 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
56 sub genpage ($$$) { #{{{
61 my $template=template("page.tmpl", blind_cache => 1);
64 if (length $config{cgiurl}) {
65 $template->param(editurl => cgiurl(do => "edit", page => $page));
66 $template->param(prefsurl => cgiurl(do => "prefs"));
68 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
73 if (length $config{historyurl}) {
74 my $u=$config{historyurl};
75 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
76 $template->param(historyurl => $u);
79 if ($config{discussion}) {
80 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
85 $template->param(have_actions => 1);
89 title => $page eq 'index'
91 : pagetitle(basename($page)),
92 wikiname => $config{wikiname},
93 parentlinks => [parentlinks($page)],
95 backlinks => [backlinks($page)],
96 mtime => displaytime($mtime),
97 baseurl => baseurl($page),
100 run_hooks(pagetemplate => sub {
101 shift->(page => $page, destpage => $page, template => $template);
104 $content=$template->output;
106 run_hooks(format => sub {
119 return (stat($file))[9];
122 sub findlinks ($$) { #{{{
127 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
128 push @links, titlepage($2);
130 if ($config{discussion}) {
131 # Discussion links are a special case since they're not in the
132 # text of the page, but on its template.
133 return @links, "$page/discussion";
140 sub render ($) { #{{{
143 my $type=pagetype($file);
144 my $srcfile=srcfile($file);
146 my $content=readfile($srcfile);
147 my $page=pagename($file);
148 delete $depends{$page};
149 will_render($page, htmlpage($page), 1);
151 $content=filter($page, $content);
153 $links{$page}=[findlinks($page, $content)];
155 $content=preprocess($page, $page, $content);
156 $content=linkify($page, $page, $content);
157 $content=htmlize($page, $type, $content);
159 writefile(htmlpage($page), $config{destdir},
160 genpage($page, $content, mtime($srcfile)));
161 $oldpagemtime{$page}=time;
164 my $content=readfile($srcfile, 1);
166 delete $depends{$file};
167 will_render($file, $file, 1);
168 writefile($file, $config{destdir}, $content, 1);
169 $oldpagemtime{$file}=time;
177 my $dir=dirname($file);
178 while (rmdir($dir)) {
183 sub refresh () { #{{{
184 # find existing pages
187 eval q{use File::Find};
192 if (/$config{wiki_file_prune_regexp}/) {
193 $File::Find::prune=1;
195 elsif (! -d $_ && ! -l $_) {
196 my ($f)=/$config{wiki_file_regexp}/; # untaint
198 warn("skipping bad filename $_\n");
201 $f=~s/^\Q$config{srcdir}\E\/?//;
203 $exists{pagename($f)}=1;
212 if (/$config{wiki_file_prune_regexp}/) {
213 $File::Find::prune=1;
215 elsif (! -d $_ && ! -l $_) {
216 my ($f)=/$config{wiki_file_regexp}/; # untaint
218 warn("skipping bad filename $_\n");
221 # Don't add files that are in the
223 $f=~s/^\Q$config{underlaydir}\E\/?//;
224 if (! -e "$config{srcdir}/$f" &&
225 ! -l "$config{srcdir}/$f") {
227 $exists{pagename($f)}=1;
232 }, $config{underlaydir});
236 # check for added or removed pages
238 foreach my $file (@files) {
239 my $page=pagename($file);
240 if (! $oldpagemtime{$page}) {
241 debug("new page $page") unless exists $pagectime{$page};
244 $pagecase{lc $page}=$page;
245 $pagesources{$page}=$file;
246 if ($config{getctime} && -e "$config{srcdir}/$file") {
247 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
249 elsif (! exists $pagectime{$page}) {
250 $pagectime{$page}=mtime(srcfile($file));
255 foreach my $page (keys %oldpagemtime) {
256 if (! $exists{$page}) {
257 debug("removing old page $page");
258 push @del, $pagesources{$page};
259 $renderedfiles{$page}=[];
260 $oldpagemtime{$page}=0;
261 prune($config{destdir}."/".$_)
262 foreach @{$oldrenderedfiles{$page}};
263 delete $pagesources{$page};
267 # render any updated files
268 foreach my $file (@files) {
269 my $page=pagename($file);
271 if (! exists $oldpagemtime{$page} ||
272 mtime(srcfile($file)) > $oldpagemtime{$page} ||
273 $forcerebuild{$page}) {
274 debug("rendering $file");
280 # if any files were added or removed, check to see if each page
281 # needs an update due to linking to them or inlining them.
282 # TODO: inefficient; pages may get rendered above and again here;
283 # problem is the bestlink may have changed and we won't know until
286 FILE: foreach my $file (@files) {
287 my $page=pagename($file);
288 foreach my $f (@add, @del) {
290 foreach my $link (@{$links{$page}}) {
291 if (bestlink($page, $link) eq $p) {
292 debug("rendering $file, which links to $p");
302 # Handle backlinks; if a page has added/removed links, update the
303 # pages it links to. Also handles rebuilding dependant pages.
304 # TODO: inefficient; pages may get rendered above and again here;
305 # problem is the backlinks could be wrong in the first pass render
307 if (%rendered || @del) {
308 foreach my $f (@files) {
310 if (exists $depends{$p}) {
311 foreach my $file (keys %rendered, @del) {
313 my $page=pagename($file);
314 if (pagespec_match($page, $depends{$p})) {
315 debug("rendering $f, which depends on $page");
325 foreach my $file (keys %rendered, @del) {
326 my $page=pagename($file);
328 if (exists $links{$page}) {
329 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
331 (! exists $oldlinks{$page} ||
332 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
333 $linkchanged{$link}=1;
337 if (exists $oldlinks{$page}) {
338 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
340 (! exists $links{$page} ||
341 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
342 $linkchanged{$link}=1;
347 foreach my $link (keys %linkchanged) {
348 my $linkfile=$pagesources{$link};
349 if (defined $linkfile) {
350 debug("rendering $linkfile, to update its backlinks");
352 $rendered{$linkfile}=1;
357 # Remove no longer rendered files.
358 foreach my $src (keys %rendered) {
359 my $page=pagename($src);
360 foreach my $file (@{$oldrenderedfiles{$page}}) {
361 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
362 debug("removing $file, no longer rendered by $page");
363 prune($config{destdir}."/".$file);
369 run_hooks(delete => sub { shift->(@del) });
372 run_hooks(change => sub { shift->(keys %rendered) });
376 sub commandline_render () { #{{{
383 my $srcfile=possibly_foolish_untaint($config{render});
385 $file=~s/\Q$config{srcdir}\E\/?//;
387 my $type=pagetype($file);
388 die "ikiwiki: cannot render $srcfile\n" unless defined $type;
389 my $content=readfile($srcfile);
390 my $page=pagename($file);
391 $pagesources{$page}=$file;
392 $content=filter($page, $content);
393 $content=preprocess($page, $page, $content);
394 $content=linkify($page, $page, $content);
395 $content=htmlize($page, $type, $content);
397 print genpage($page, $content, mtime($srcfile));