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)
91 my $onlystrip=shift || 0; # strip directives without processing
98 return "[[$command $params]]";
103 elsif (exists $hooks{preprocess}{$command}) {
104 # Note: preserve order of params, some plugins may
105 # consider it significant.
107 while ($params =~ /(?:(\w+)=)?(?:"([^"]+)"|(\S+))(?:\s+|$)/g) {
109 push @params, $1, (defined $2 ? $2 : $3);
112 push @params, (defined $2 ? $2 : $3), '';
115 return $hooks{preprocess}{$command}{call}->(
118 destpage => $destpage,
122 return "[[$command not processed]]";
126 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
130 sub add_depends ($$) { #{{{
134 if (! exists $depends{$page}) {
135 $depends{$page}=$pagespec;
138 $depends{$page}=pagespec_merge($depends{$page}, $pagespec);
142 sub genpage ($$$) { #{{{
147 my $template=template("page.tmpl", blind_cache => 1);
150 if (length $config{cgiurl}) {
151 $template->param(editurl => cgiurl(do => "edit", page => $page));
152 $template->param(prefsurl => cgiurl(do => "prefs"));
154 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
159 if (length $config{historyurl}) {
160 my $u=$config{historyurl};
161 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
162 $template->param(historyurl => $u);
165 if ($config{discussion}) {
166 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
171 $template->param(have_actions => 1);
175 title => $page eq 'index'
177 : pagetitle(basename($page)),
178 wikiname => $config{wikiname},
179 parentlinks => [parentlinks($page)],
181 backlinks => [backlinks($page)],
182 mtime => displaytime($mtime),
183 styleurl => styleurl($page),
186 run_hooks(pagetemplate => sub {
187 shift->(page => $page, destpage => $page, template => $template);
190 $content=$template->output;
192 run_hooks(format => sub {
193 $content=shift->($content);
199 sub check_overwrite ($$) { #{{{
200 # Important security check. Make sure to call this before saving
201 # any files to the source directory.
205 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
206 error("$dest already exists and was not rendered from $src before");
210 sub displaytime ($) { #{{{
214 # strftime doesn't know about encodings, so make sure
215 # its output is properly treated as utf8
216 return decode_utf8(POSIX::strftime(
217 $config{timeformat}, localtime($time)));
223 return (stat($file))[9];
226 sub findlinks ($$) { #{{{
231 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
232 push @links, titlepage($2);
234 if ($config{discussion}) {
235 # Discussion links are a special case since they're not in the
236 # text of the page, but on its template.
237 return @links, "$page/discussion";
248 run_hooks(filter => sub {
249 $content=shift->(page => $page, content => $content);
255 sub render ($) { #{{{
258 my $type=pagetype($file);
259 my $srcfile=srcfile($file);
261 my $content=readfile($srcfile);
262 my $page=pagename($file);
263 delete $depends{$page};
265 $content=filter($page, $content);
267 $links{$page}=[findlinks($page, $content)];
269 $content=linkify($page, $page, $content);
270 $content=preprocess($page, $page, $content);
271 $content=htmlize($type, $content);
273 check_overwrite("$config{destdir}/".htmlpage($page), $page);
274 writefile(htmlpage($page), $config{destdir},
275 genpage($page, $content, mtime($srcfile)));
276 $oldpagemtime{$page}=time;
277 $renderedfiles{$page}=htmlpage($page);
280 my $content=readfile($srcfile, 1);
282 delete $depends{$file};
283 check_overwrite("$config{destdir}/$file", $file);
284 writefile($file, $config{destdir}, $content, 1);
285 $oldpagemtime{$file}=time;
286 $renderedfiles{$file}=$file;
294 my $dir=dirname($file);
295 while (rmdir($dir)) {
300 sub refresh () { #{{{
301 # find existing pages
304 eval q{use File::Find};
309 if (/$config{wiki_file_prune_regexp}/) {
310 $File::Find::prune=1;
312 elsif (! -d $_ && ! -l $_) {
313 my ($f)=/$config{wiki_file_regexp}/; # untaint
315 warn("skipping bad filename $_\n");
318 $f=~s/^\Q$config{srcdir}\E\/?//;
320 $exists{pagename($f)}=1;
329 if (/$config{wiki_file_prune_regexp}/) {
330 $File::Find::prune=1;
332 elsif (! -d $_ && ! -l $_) {
333 my ($f)=/$config{wiki_file_regexp}/; # untaint
335 warn("skipping bad filename $_\n");
338 # Don't add files that are in the
340 $f=~s/^\Q$config{underlaydir}\E\/?//;
341 if (! -e "$config{srcdir}/$f" &&
342 ! -l "$config{srcdir}/$f") {
344 $exists{pagename($f)}=1;
349 }, $config{underlaydir});
353 # check for added or removed pages
355 foreach my $file (@files) {
356 my $page=pagename($file);
357 if (! $oldpagemtime{$page}) {
358 debug("new page $page") unless exists $pagectime{$page};
361 $pagecase{lc $page}=$page;
362 $pagesources{$page}=$file;
363 if ($config{getctime} && -e "$config{srcdir}/$file") {
364 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
366 elsif (! exists $pagectime{$page}) {
367 $pagectime{$page}=mtime(srcfile($file));
372 foreach my $page (keys %oldpagemtime) {
373 if (! $exists{$page}) {
374 debug("removing old page $page");
375 push @del, $pagesources{$page};
376 prune($config{destdir}."/".$renderedfiles{$page});
377 delete $renderedfiles{$page};
378 $oldpagemtime{$page}=0;
379 delete $pagesources{$page};
383 # render any updated files
384 foreach my $file (@files) {
385 my $page=pagename($file);
387 if (! exists $oldpagemtime{$page} ||
388 mtime(srcfile($file)) > $oldpagemtime{$page} ||
389 $forcerebuild{$page}) {
390 debug("rendering $file");
396 # if any files were added or removed, check to see if each page
397 # needs an update due to linking to them or inlining them.
398 # TODO: inefficient; pages may get rendered above and again here;
399 # problem is the bestlink may have changed and we won't know until
402 FILE: foreach my $file (@files) {
403 my $page=pagename($file);
404 foreach my $f (@add, @del) {
406 foreach my $link (@{$links{$page}}) {
407 if (bestlink($page, $link) eq $p) {
408 debug("rendering $file, which links to $p");
418 # Handle backlinks; if a page has added/removed links, update the
419 # pages it links to. Also handles rebuilding dependant pages.
420 # TODO: inefficient; pages may get rendered above and again here;
421 # problem is the backlinks could be wrong in the first pass render
423 if (%rendered || @del) {
424 foreach my $f (@files) {
426 if (exists $depends{$p}) {
427 foreach my $file (keys %rendered, @del) {
429 my $page=pagename($file);
430 if (pagespec_match($page, $depends{$p})) {
431 debug("rendering $f, which depends on $page");
441 foreach my $file (keys %rendered, @del) {
442 my $page=pagename($file);
444 if (exists $links{$page}) {
445 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
447 (! exists $oldlinks{$page} ||
448 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
449 $linkchanged{$link}=1;
453 if (exists $oldlinks{$page}) {
454 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
456 (! exists $links{$page} ||
457 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
458 $linkchanged{$link}=1;
463 foreach my $link (keys %linkchanged) {
464 my $linkfile=$pagesources{$link};
465 if (defined $linkfile) {
466 debug("rendering $linkfile, to update its backlinks");
468 $rendered{$linkfile}=1;
474 run_hooks(delete => sub { shift->(@del) });
477 run_hooks(change => sub { shift->(keys %rendered) });