10 sub linkify ($$$) { #{{{
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 if (exists $hooks{sanitize}) {
35 foreach my $id (keys %{$hooks{sanitize}}) {
36 $content=$hooks{sanitize}{$id}{call}->($content);
43 sub backlinks ($) { #{{{
47 foreach my $p (keys %links) {
48 next if bestlink($page, $p) eq $page;
49 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
50 my $href=abs2rel(htmlpage($p), dirname($page));
52 # Trim common dir prefixes from both pages.
54 my $page_trimmed=$page;
56 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
58 $p_trimmed=~s/^\Q$dir\E// &&
59 $page_trimmed=~s/^\Q$dir\E//;
61 push @links, { url => $href, page => $p_trimmed };
65 return sort { $a->{page} cmp $b->{page} } @links;
68 sub parentlinks ($) { #{{{
75 return if $page eq 'index'; # toplevel
76 foreach my $dir (reverse split("/", $page)) {
79 unshift @ret, { url => "$path$dir.html", page => $dir };
85 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
89 sub preprocess ($$;$) { #{{{
92 my $onlystrip=shift || 0; # strip directives without processing
99 return "[[$command $params]]";
104 elsif (exists $hooks{preprocess}{$command}) {
105 # Note: preserve order of params, some plugins may
106 # consider it significant.
108 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
109 push @params, $1, $2;
111 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
114 return "[[$command not processed]]";
118 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
122 sub add_depends ($$) { #{{{
126 if (! exists $depends{$page}) {
127 $depends{$page}=$globlist;
130 $depends{$page}=globlist_merge($depends{$page}, $globlist);
134 sub globlist_merge ($$) { #{{{
139 # Only add negated globs if they are not matched by the other globlist.
140 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
141 (map { [ $b, $_ ] } split(" ", $a))) {
142 if ($i->[1]=~/^!(.*)/) {
143 if (! globlist_match($1, $i->[0])) {
155 sub genpage ($$$) { #{{{
160 my $template=template("page.tmpl", blind_cache => 1);
163 if (length $config{cgiurl}) {
164 $template->param(editurl => cgiurl(do => "edit", page => $page));
165 $template->param(prefsurl => cgiurl(do => "prefs"));
167 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
172 if (length $config{historyurl}) {
173 my $u=$config{historyurl};
174 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
175 $template->param(historyurl => $u);
178 if ($config{discussion}) {
179 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
184 $template->param(have_actions => 1);
188 title => $page eq 'index'
190 : pagetitle(basename($page)),
191 wikiname => $config{wikiname},
192 parentlinks => [parentlinks($page)],
194 backlinks => [backlinks($page)],
195 mtime => displaytime($mtime),
196 styleurl => styleurl($page),
199 if (exists $hooks{pagetemplate}) {
200 foreach my $id (keys %{$hooks{pagetemplate}}) {
201 $hooks{pagetemplate}{$id}{call}->($page, $template);
205 return $template->output;
208 sub check_overwrite ($$) { #{{{
209 # Important security check. Make sure to call this before saving
210 # any files to the source directory.
214 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
215 error("$dest already exists and was not rendered from $src before");
219 sub displaytime ($) { #{{{
223 # strftime doesn't know about encodings, so make sure
224 # its output is properly treated as utf8
225 return decode_utf8(POSIX::strftime(
226 $config{timeformat}, localtime($time)));
232 return (stat($file))[9];
235 sub findlinks ($$) { #{{{
240 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
241 push @links, titlepage($2);
243 if ($config{discussion}) {
244 # Discussion links are a special case since they're not in the
245 # text of the page, but on its template.
246 return @links, "$page/discussion";
257 if (exists $hooks{filter}) {
258 foreach my $id (keys %{$hooks{filter}}) {
259 $content=$hooks{filter}{$id}{call}->(
269 sub render ($) { #{{{
272 my $type=pagetype($file);
273 my $srcfile=srcfile($file);
275 my $content=readfile($srcfile);
276 my $page=pagename($file);
277 delete $depends{$page};
279 $content=filter($page, $content);
281 $links{$page}=[findlinks($page, $content)];
283 $content=linkify($page, $page, $content);
284 $content=preprocess($page, $content);
285 $content=htmlize($type, $content);
287 check_overwrite("$config{destdir}/".htmlpage($page), $page);
288 writefile(htmlpage($page), $config{destdir},
289 genpage($page, $content, mtime($srcfile)));
290 $oldpagemtime{$page}=time;
291 $renderedfiles{$page}=htmlpage($page);
294 my $content=readfile($srcfile, 1);
296 delete $depends{$file};
297 check_overwrite("$config{destdir}/$file", $file);
298 writefile($file, $config{destdir}, $content, 1);
299 $oldpagemtime{$file}=time;
300 $renderedfiles{$file}=$file;
308 my $dir=dirname($file);
309 while (rmdir($dir)) {
314 sub refresh () { #{{{
315 # find existing pages
318 eval q{use File::Find};
323 if (/$config{wiki_file_prune_regexp}/) {
324 $File::Find::prune=1;
326 elsif (! -d $_ && ! -l $_) {
327 my ($f)=/$config{wiki_file_regexp}/; # untaint
329 warn("skipping bad filename $_\n");
332 $f=~s/^\Q$config{srcdir}\E\/?//;
334 $exists{pagename($f)}=1;
343 if (/$config{wiki_file_prune_regexp}/) {
344 $File::Find::prune=1;
346 elsif (! -d $_ && ! -l $_) {
347 my ($f)=/$config{wiki_file_regexp}/; # untaint
349 warn("skipping bad filename $_\n");
352 # Don't add files that are in the
354 $f=~s/^\Q$config{underlaydir}\E\/?//;
355 if (! -e "$config{srcdir}/$f" &&
356 ! -l "$config{srcdir}/$f") {
358 $exists{pagename($f)}=1;
363 }, $config{underlaydir});
367 # check for added or removed pages
369 foreach my $file (@files) {
370 my $page=pagename($file);
371 if (! $oldpagemtime{$page}) {
372 debug("new page $page") unless exists $pagectime{$page};
375 $pagesources{$page}=$file;
376 if ($config{getctime} && -e "$config{srcdir}/$file") {
377 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
379 elsif (! exists $pagectime{$page}) {
380 $pagectime{$page}=mtime(srcfile($file));
385 foreach my $page (keys %oldpagemtime) {
386 if (! $exists{$page}) {
387 debug("removing old page $page");
388 push @del, $pagesources{$page};
389 prune($config{destdir}."/".$renderedfiles{$page});
390 delete $renderedfiles{$page};
391 $oldpagemtime{$page}=0;
392 delete $pagesources{$page};
396 # render any updated files
397 foreach my $file (@files) {
398 my $page=pagename($file);
400 if (! exists $oldpagemtime{$page} ||
401 mtime(srcfile($file)) > $oldpagemtime{$page}) {
402 debug("rendering $file");
408 # if any files were added or removed, check to see if each page
409 # needs an update due to linking to them or inlining them.
410 # TODO: inefficient; pages may get rendered above and again here;
411 # problem is the bestlink may have changed and we won't know until
414 FILE: foreach my $file (@files) {
415 my $page=pagename($file);
416 foreach my $f (@add, @del) {
418 foreach my $link (@{$links{$page}}) {
419 if (bestlink($page, $link) eq $p) {
420 debug("rendering $file, which links to $p");
430 # Handle backlinks; if a page has added/removed links, update the
431 # pages it links to. Also handles rebuilding dependant pages.
432 # TODO: inefficient; pages may get rendered above and again here;
433 # problem is the backlinks could be wrong in the first pass render
435 if (%rendered || @del) {
436 foreach my $f (@files) {
438 if (exists $depends{$p}) {
439 foreach my $file (keys %rendered, @del) {
441 my $page=pagename($file);
442 if (globlist_match($page, $depends{$p})) {
443 debug("rendering $f, which depends on $page");
453 foreach my $file (keys %rendered, @del) {
454 my $page=pagename($file);
456 if (exists $links{$page}) {
457 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
459 (! exists $oldlinks{$page} ||
460 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
461 $linkchanged{$link}=1;
465 if (exists $oldlinks{$page}) {
466 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
468 (! exists $links{$page} ||
469 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
470 $linkchanged{$link}=1;
475 foreach my $link (keys %linkchanged) {
476 my $linkfile=$pagesources{$link};
477 if (defined $linkfile) {
478 debug("rendering $linkfile, to update its backlinks");
480 $rendered{$linkfile}=1;
485 if (@del && exists $hooks{delete}) {
486 foreach my $id (keys %{$hooks{delete}}) {
487 $hooks{delete}{$id}{call}->(@del);
490 if (%rendered && exists $hooks{change}) {
491 foreach my $id (keys %{$hooks{change}}) {
492 $hooks{change}{$id}{call}->(keys %rendered);