10 sub linkify ($$$) { #{{{
11 my $lpage=shift; # the page containing the links
12 my $page=shift; # the page the link will end up on (different for inline)
15 $content =~ s{(\\?)$config{wiki_link_regexp}}{
16 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
17 : ( $1 ? "[[$3]]" : htmllink($lpage, $page, titlepage($3)))
23 sub htmlize ($$) { #{{{
27 if (exists $hooks{htmlize}{$type}) {
28 $content=$hooks{htmlize}{$type}{call}->($content);
31 error("htmlization of $type not supported");
34 run_hooks(sanitize => sub {
35 $content=shift->($content);
41 sub backlinks ($) { #{{{
45 foreach my $p (keys %links) {
46 next if bestlink($page, $p) eq $page;
47 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
48 my $href=abs2rel(htmlpage($p), dirname($page));
50 # Trim common dir prefixes from both pages.
52 my $page_trimmed=$page;
54 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
56 $p_trimmed=~s/^\Q$dir\E// &&
57 $page_trimmed=~s/^\Q$dir\E//;
59 push @links, { url => $href, page => pagetitle($p_trimmed) };
63 return sort { $a->{page} cmp $b->{page} } @links;
66 sub parentlinks ($) { #{{{
73 return if $page eq 'index'; # toplevel
74 foreach my $dir (reverse split("/", $page)) {
77 unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
83 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
87 sub preprocess ($$$) { #{{{
88 my $page=shift; # the page the data comes from
89 my $destpage=shift; # the page the data will appear in (different for inline)
97 return "[[$command $params]]";
99 elsif (exists $hooks{preprocess}{$command}) {
100 # Note: preserve order of params, some plugins may
101 # consider it significant.
103 while ($params =~ /(?:(\w+)=)?(?:"""(.*?)"""|"([^"]+)"|(\S+))(?:\s+|$)/sg) {
120 push @params, $key, $val;
123 push @params, $val, '';
126 return $hooks{preprocess}{$command}{call}->(
129 destpage => $destpage,
133 return "[[$command not processed]]";
137 $content =~ s{(\\?)\[\[(\w+)\s+((?:(?:\w+=)?(?:""".*?"""|"[^"]+"|[^\s\]]+)\s*)*)\]\]}{$handle->($1, $2, $3)}seg;
141 sub add_depends ($$) { #{{{
145 if (! exists $depends{$page}) {
146 $depends{$page}=$pagespec;
149 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
153 sub genpage ($$$) { #{{{
158 my $template=template("page.tmpl", blind_cache => 1);
161 if (length $config{cgiurl}) {
162 $template->param(editurl => cgiurl(do => "edit", page => $page));
163 $template->param(prefsurl => cgiurl(do => "prefs"));
165 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
170 if (length $config{historyurl}) {
171 my $u=$config{historyurl};
172 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
173 $template->param(historyurl => $u);
176 if ($config{discussion}) {
177 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
182 $template->param(have_actions => 1);
186 title => $page eq 'index'
188 : pagetitle(basename($page)),
189 wikiname => $config{wikiname},
190 parentlinks => [parentlinks($page)],
192 backlinks => [backlinks($page)],
193 mtime => displaytime($mtime),
194 baseurl => baseurl($page),
197 run_hooks(pagetemplate => sub {
198 shift->(page => $page, destpage => $page, template => $template);
201 $content=$template->output;
203 run_hooks(format => sub {
204 $content=shift->($content);
210 sub check_overwrite ($$) { #{{{
211 # Important security check. Make sure to call this before saving
212 # any files to the source directory.
216 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
217 error("$dest already exists and was not rendered from $src before");
221 sub displaytime ($) { #{{{
225 # strftime doesn't know about encodings, so make sure
226 # its output is properly treated as utf8
227 return decode_utf8(POSIX::strftime(
228 $config{timeformat}, localtime($time)));
234 return (stat($file))[9];
237 sub findlinks ($$) { #{{{
242 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
243 push @links, titlepage($2);
245 if ($config{discussion}) {
246 # Discussion links are a special case since they're not in the
247 # text of the page, but on its template.
248 return @links, "$page/discussion";
259 run_hooks(filter => sub {
260 $content=shift->(page => $page, content => $content);
266 sub render ($) { #{{{
269 my $type=pagetype($file);
270 my $srcfile=srcfile($file);
272 my $content=readfile($srcfile);
273 my $page=pagename($file);
274 delete $depends{$page};
276 $content=filter($page, $content);
278 $links{$page}=[findlinks($page, $content)];
280 $content=preprocess($page, $page, $content);
281 $content=linkify($page, $page, $content);
282 $content=htmlize($type, $content);
284 check_overwrite("$config{destdir}/".htmlpage($page), $page);
285 writefile(htmlpage($page), $config{destdir},
286 genpage($page, $content, mtime($srcfile)));
287 $oldpagemtime{$page}=time;
288 $renderedfiles{$page}=htmlpage($page);
291 my $content=readfile($srcfile, 1);
293 delete $depends{$file};
294 check_overwrite("$config{destdir}/$file", $file);
295 writefile($file, $config{destdir}, $content, 1);
296 $oldpagemtime{$file}=time;
297 $renderedfiles{$file}=$file;
305 my $dir=dirname($file);
306 while (rmdir($dir)) {
311 sub refresh () { #{{{
312 # find existing pages
315 eval q{use File::Find};
320 if (/$config{wiki_file_prune_regexp}/) {
321 $File::Find::prune=1;
323 elsif (! -d $_ && ! -l $_) {
324 my ($f)=/$config{wiki_file_regexp}/; # untaint
326 warn("skipping bad filename $_\n");
329 $f=~s/^\Q$config{srcdir}\E\/?//;
331 $exists{pagename($f)}=1;
340 if (/$config{wiki_file_prune_regexp}/) {
341 $File::Find::prune=1;
343 elsif (! -d $_ && ! -l $_) {
344 my ($f)=/$config{wiki_file_regexp}/; # untaint
346 warn("skipping bad filename $_\n");
349 # Don't add files that are in the
351 $f=~s/^\Q$config{underlaydir}\E\/?//;
352 if (! -e "$config{srcdir}/$f" &&
353 ! -l "$config{srcdir}/$f") {
355 $exists{pagename($f)}=1;
360 }, $config{underlaydir});
364 # check for added or removed pages
366 foreach my $file (@files) {
367 my $page=pagename($file);
368 if (! $oldpagemtime{$page}) {
369 debug("new page $page") unless exists $pagectime{$page};
372 $pagecase{lc $page}=$page;
373 $pagesources{$page}=$file;
374 if ($config{getctime} && -e "$config{srcdir}/$file") {
375 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
377 elsif (! exists $pagectime{$page}) {
378 $pagectime{$page}=mtime(srcfile($file));
383 foreach my $page (keys %oldpagemtime) {
384 if (! $exists{$page}) {
385 debug("removing old page $page");
386 push @del, $pagesources{$page};
387 prune($config{destdir}."/".$renderedfiles{$page});
388 delete $renderedfiles{$page};
389 $oldpagemtime{$page}=0;
390 delete $pagesources{$page};
394 # render any updated files
395 foreach my $file (@files) {
396 my $page=pagename($file);
398 if (! exists $oldpagemtime{$page} ||
399 mtime(srcfile($file)) > $oldpagemtime{$page} ||
400 $forcerebuild{$page}) {
401 debug("rendering $file");
407 # if any files were added or removed, check to see if each page
408 # needs an update due to linking to them or inlining them.
409 # TODO: inefficient; pages may get rendered above and again here;
410 # problem is the bestlink may have changed and we won't know until
413 FILE: foreach my $file (@files) {
414 my $page=pagename($file);
415 foreach my $f (@add, @del) {
417 foreach my $link (@{$links{$page}}) {
418 if (bestlink($page, $link) eq $p) {
419 debug("rendering $file, which links to $p");
429 # Handle backlinks; if a page has added/removed links, update the
430 # pages it links to. Also handles rebuilding dependant pages.
431 # TODO: inefficient; pages may get rendered above and again here;
432 # problem is the backlinks could be wrong in the first pass render
434 if (%rendered || @del) {
435 foreach my $f (@files) {
437 if (exists $depends{$p}) {
438 foreach my $file (keys %rendered, @del) {
440 my $page=pagename($file);
441 if (pagespec_match($page, $depends{$p})) {
442 debug("rendering $f, which depends on $page");
452 foreach my $file (keys %rendered, @del) {
453 my $page=pagename($file);
455 if (exists $links{$page}) {
456 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
458 (! exists $oldlinks{$page} ||
459 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
460 $linkchanged{$link}=1;
464 if (exists $oldlinks{$page}) {
465 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
467 (! exists $links{$page} ||
468 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
469 $linkchanged{$link}=1;
474 foreach my $link (keys %linkchanged) {
475 my $linkfile=$pagesources{$link};
476 if (defined $linkfile) {
477 debug("rendering $linkfile, to update its backlinks");
479 $rendered{$linkfile}=1;
485 run_hooks(delete => sub { shift->(@del) });
488 run_hooks(change => sub { shift->(keys %rendered) });