10 sub backlinks ($) { #{{{
14 foreach my $p (keys %links) {
15 next if bestlink($page, $p) eq $page;
17 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
18 my $href=abs2rel(htmlpage($p), dirname($page));
20 # Trim common dir prefixes from both pages.
22 my $page_trimmed=$page;
24 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
26 $p_trimmed=~s/^\Q$dir\E// &&
27 $page_trimmed=~s/^\Q$dir\E//;
29 push @links, { url => $href, page => pagetitle($p_trimmed) };
33 return sort { $a->{page} cmp $b->{page} } @links;
36 sub parentlinks ($) { #{{{
43 return if $page eq 'index'; # toplevel
44 foreach my $dir (reverse split("/", $page)) {
47 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
53 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
57 sub genpage ($$$) { #{{{
62 my $template=template("page.tmpl", blind_cache => 1);
65 if (length $config{cgiurl}) {
66 $template->param(editurl => cgiurl(do => "edit", page => $page));
67 $template->param(prefsurl => cgiurl(do => "prefs"));
69 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
74 if (length $config{historyurl}) {
75 my $u=$config{historyurl};
76 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
77 $template->param(historyurl => $u);
80 if ($config{discussion}) {
81 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
86 $template->param(have_actions => 1);
90 title => $page eq 'index'
92 : pagetitle(basename($page)),
93 wikiname => $config{wikiname},
94 parentlinks => [parentlinks($page)],
96 backlinks => [backlinks($page)],
97 mtime => displaytime($mtime),
98 baseurl => baseurl($page),
101 run_hooks(pagetemplate => sub {
102 shift->(page => $page, destpage => $page, template => $template);
105 $content=$template->output;
107 run_hooks(format => sub {
120 return (stat($file))[9];
126 my $type=pagetype($file);
128 my $srcfile=srcfile($file);
129 my $content=readfile($srcfile);
130 my $page=pagename($file);
133 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
134 push @links, titlepage($2);
136 if ($config{discussion}) {
137 # Discussion links are a special case since they're not in the
138 # text of the page, but on its template.
139 push @links, "$page/discussion";
141 $links{$page}=\@links;
145 sub render ($) { #{{{
148 my $type=pagetype($file);
149 my $srcfile=srcfile($file);
151 my $content=readfile($srcfile);
152 my $page=pagename($file);
153 delete $depends{$page};
154 will_render($page, htmlpage($page), 1);
156 $content=filter($page, $content);
157 $content=preprocess($page, $page, $content);
158 $content=linkify($page, $page, $content);
159 $content=htmlize($page, $type, $content);
161 writefile(htmlpage($page), $config{destdir},
162 genpage($page, $content, mtime($srcfile)));
163 $oldpagemtime{$page}=time;
166 my $content=readfile($srcfile, 1);
167 delete $depends{$file};
168 will_render($file, $file, 1);
169 writefile($file, $config{destdir}, $content, 1);
170 $oldpagemtime{$file}=time;
178 my $dir=dirname($file);
179 while (rmdir($dir)) {
184 sub refresh () { #{{{
185 # find existing pages
188 eval q{use File::Find};
193 if (/$config{wiki_file_prune_regexp}/) {
194 $File::Find::prune=1;
196 elsif (! -d $_ && ! -l $_) {
197 my ($f)=/$config{wiki_file_regexp}/; # untaint
199 warn("skipping bad filename $_\n");
202 $f=~s/^\Q$config{srcdir}\E\/?//;
204 $exists{pagename($f)}=1;
213 if (/$config{wiki_file_prune_regexp}/) {
214 $File::Find::prune=1;
216 elsif (! -d $_ && ! -l $_) {
217 my ($f)=/$config{wiki_file_regexp}/; # untaint
219 warn("skipping bad filename $_\n");
222 # Don't add files that are in the
224 $f=~s/^\Q$config{underlaydir}\E\/?//;
225 if (! -e "$config{srcdir}/$f" &&
226 ! -l "$config{srcdir}/$f") {
228 $exists{pagename($f)}=1;
233 }, $config{underlaydir});
237 # check for added or removed pages
239 foreach my $file (@files) {
240 my $page=pagename($file);
241 if (! $oldpagemtime{$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};
260 $renderedfiles{$page}=[];
261 $oldpagemtime{$page}=0;
262 prune($config{destdir}."/".$_)
263 foreach @{$oldrenderedfiles{$page}};
264 delete $pagesources{$page};
268 # scan updated files to update info about them
269 foreach my $file (@files) {
270 my $page=pagename($file);
272 if (! exists $oldpagemtime{$page} ||
273 mtime(srcfile($file)) > $oldpagemtime{$page} ||
274 $forcerebuild{$page}) {
275 debug("scanning $file");
280 # render any updated files
281 foreach my $file (@files) {
282 my $page=pagename($file);
284 if (! exists $oldpagemtime{$page} ||
285 mtime(srcfile($file)) > $oldpagemtime{$page} ||
286 $forcerebuild{$page}) {
287 debug("rendering $file");
293 # if any files were added or removed, check to see if each page
294 # needs an update due to linking to them or inlining them
296 FILE: foreach my $file (@files) {
297 next if $rendered{$file};
298 my $page=pagename($file);
299 foreach my $f (@add, @del) {
301 foreach my $link (@{$links{$page}}) {
302 if (bestlink($page, $link) eq $p) {
303 debug("rendering $file, which links to $p");
313 # Handle backlinks; if a page has added/removed links, update the
314 # pages it links to. Also handles rebuilding dependant pages.
315 if (%rendered || @del) {
316 foreach my $f (@files) {
317 next if $rendered{$f};
319 if (exists $depends{$p}) {
320 foreach my $file (keys %rendered, @del) {
322 my $page=pagename($file);
323 if (pagespec_match($page, $depends{$p})) {
324 debug("rendering $f, which depends on $page");
334 foreach my $file (keys %rendered, @del) {
335 my $page=pagename($file);
337 if (exists $links{$page}) {
338 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
340 (! exists $oldlinks{$page} ||
341 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
342 $linkchanged{$link}=1;
346 if (exists $oldlinks{$page}) {
347 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
349 (! exists $links{$page} ||
350 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
351 $linkchanged{$link}=1;
356 foreach my $link (keys %linkchanged) {
357 my $linkfile=$pagesources{$link};
358 if (defined $linkfile) {
359 next if $rendered{$linkfile};
360 debug("rendering $linkfile, to update its backlinks");
362 $rendered{$linkfile}=1;
367 # Remove no longer rendered files.
368 foreach my $src (keys %rendered) {
369 my $page=pagename($src);
370 foreach my $file (@{$oldrenderedfiles{$page}}) {
371 if (! grep { $_ eq $file } @{$renderedfiles{$page}}) {
372 debug("removing $file, no longer rendered by $page");
373 prune($config{destdir}."/".$file);
379 run_hooks(delete => sub { shift->(@del) });
382 run_hooks(change => sub { shift->(keys %rendered) });
386 sub commandline_render () { #{{{
393 my $srcfile=possibly_foolish_untaint($config{render});
395 $file=~s/\Q$config{srcdir}\E\/?//;
397 my $type=pagetype($file);
398 die "ikiwiki: cannot render $srcfile\n" unless defined $type;
399 my $content=readfile($srcfile);
400 my $page=pagename($file);
401 $pagesources{$page}=$file;
402 $content=filter($page, $content);
403 $content=preprocess($page, $page, $content);
404 $content=linkify($page, $page, $content);
405 $content=htmlize($page, $type, $content);
407 print genpage($page, $content, mtime($srcfile));