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";
253 sub render ($) { #{{{
256 my $type=pagetype($file);
257 my $srcfile=srcfile($file);
259 my $content=readfile($srcfile);
260 my $page=pagename($file);
261 delete $depends{$page};
263 if (exists $hooks{filter}) {
264 foreach my $id (keys %{$hooks{filter}}) {
265 $content=$hooks{filter}{$id}{call}->(
272 $links{$page}=[findlinks($page, $content)];
274 $content=linkify($page, $page, $content);
275 $content=preprocess($page, $content);
276 $content=htmlize($type, $content);
278 check_overwrite("$config{destdir}/".htmlpage($page), $page);
279 writefile(htmlpage($page), $config{destdir},
280 genpage($page, $content, mtime($srcfile)));
281 $oldpagemtime{$page}=time;
282 $renderedfiles{$page}=htmlpage($page);
285 my $content=readfile($srcfile, 1);
287 delete $depends{$file};
288 check_overwrite("$config{destdir}/$file", $file);
289 writefile($file, $config{destdir}, $content, 1);
290 $oldpagemtime{$file}=time;
291 $renderedfiles{$file}=$file;
299 my $dir=dirname($file);
300 while (rmdir($dir)) {
305 sub refresh () { #{{{
306 # find existing pages
309 eval q{use File::Find};
314 if (/$config{wiki_file_prune_regexp}/) {
315 $File::Find::prune=1;
317 elsif (! -d $_ && ! -l $_) {
318 my ($f)=/$config{wiki_file_regexp}/; # untaint
320 warn("skipping bad filename $_\n");
323 $f=~s/^\Q$config{srcdir}\E\/?//;
325 $exists{pagename($f)}=1;
334 if (/$config{wiki_file_prune_regexp}/) {
335 $File::Find::prune=1;
337 elsif (! -d $_ && ! -l $_) {
338 my ($f)=/$config{wiki_file_regexp}/; # untaint
340 warn("skipping bad filename $_\n");
343 # Don't add files that are in the
345 $f=~s/^\Q$config{underlaydir}\E\/?//;
346 if (! -e "$config{srcdir}/$f" &&
347 ! -l "$config{srcdir}/$f") {
349 $exists{pagename($f)}=1;
354 }, $config{underlaydir});
358 # check for added or removed pages
360 foreach my $file (@files) {
361 my $page=pagename($file);
362 if (! $oldpagemtime{$page}) {
363 debug("new page $page") unless exists $pagectime{$page};
366 $pagesources{$page}=$file;
367 if ($config{getctime} && -e "$config{srcdir}/$file") {
368 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
370 elsif (! exists $pagectime{$page}) {
371 $pagectime{$page}=mtime(srcfile($file));
376 foreach my $page (keys %oldpagemtime) {
377 if (! $exists{$page}) {
378 debug("removing old page $page");
379 push @del, $pagesources{$page};
380 prune($config{destdir}."/".$renderedfiles{$page});
381 delete $renderedfiles{$page};
382 $oldpagemtime{$page}=0;
383 delete $pagesources{$page};
387 # render any updated files
388 foreach my $file (@files) {
389 my $page=pagename($file);
391 if (! exists $oldpagemtime{$page} ||
392 mtime(srcfile($file)) > $oldpagemtime{$page}) {
393 debug("rendering $file");
399 # if any files were added or removed, check to see if each page
400 # needs an update due to linking to them or inlining them.
401 # TODO: inefficient; pages may get rendered above and again here;
402 # problem is the bestlink may have changed and we won't know until
405 FILE: foreach my $file (@files) {
406 my $page=pagename($file);
407 foreach my $f (@add, @del) {
409 foreach my $link (@{$links{$page}}) {
410 if (bestlink($page, $link) eq $p) {
411 debug("rendering $file, which links to $p");
421 # Handle backlinks; if a page has added/removed links, update the
422 # pages it links to. Also handles rebuilding dependant pages.
423 # TODO: inefficient; pages may get rendered above and again here;
424 # problem is the backlinks could be wrong in the first pass render
426 if (%rendered || @del) {
427 foreach my $f (@files) {
429 if (exists $depends{$p}) {
430 foreach my $file (keys %rendered, @del) {
432 my $page=pagename($file);
433 if (globlist_match($page, $depends{$p})) {
434 debug("rendering $f, which depends on $page");
444 foreach my $file (keys %rendered, @del) {
445 my $page=pagename($file);
447 if (exists $links{$page}) {
448 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
450 (! exists $oldlinks{$page} ||
451 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
452 $linkchanged{$link}=1;
456 if (exists $oldlinks{$page}) {
457 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
459 (! exists $links{$page} ||
460 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
461 $linkchanged{$link}=1;
466 foreach my $link (keys %linkchanged) {
467 my $linkfile=$pagesources{$link};
468 if (defined $linkfile) {
469 debug("rendering $linkfile, to update its backlinks");
471 $rendered{$linkfile}=1;
476 if (@del && exists $hooks{delete}) {
477 foreach my $id (keys %{$hooks{delete}}) {
478 $hooks{delete}{$id}{call}->(@del);
481 if (%rendered && exists $hooks{change}) {
482 foreach my $id (keys %{$hooks{change}}) {
483 $hooks{change}{$id}{call}->(keys %rendered);