11 sub linkify ($$$) { #{{{
16 $content =~ s{(\\?)$config{wiki_link_regexp}}{
17 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($lpage, $page, titlepage($3), 0, 0, pagetitle($2)))
18 : ( $1 ? "[[$3]]" : htmllink($lpage, $page, titlepage($3)))
24 sub htmlize ($$) { #{{{
28 if (! $INC{"/usr/bin/markdown"}) {
29 # Note: a proper perl module is available in Debian
30 # for markdown, but not upstream yet.
32 $blosxom::version="is a proper perl module too much to ask?";
34 do "/usr/bin/markdown";
37 if ($type eq '.mdwn') {
38 # Workaround for perl bug (#376329)
39 $content=Encode::encode_utf8($content);
40 $content=Encode::encode_utf8($content);
41 $content=Markdown::Markdown($content);
42 $content=Encode::decode_utf8($content);
43 $content=Encode::decode_utf8($content);
46 error("htmlization of $type not supported");
49 if (exists $hooks{sanitize}) {
50 foreach my $id (keys %{$hooks{sanitize}}) {
51 $content=$hooks{sanitize}{$id}{call}->($content);
58 sub backlinks ($) { #{{{
62 foreach my $p (keys %links) {
63 next if bestlink($page, $p) eq $page;
64 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
65 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
67 # Trim common dir prefixes from both pages.
69 my $page_trimmed=$page;
71 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
73 $p_trimmed=~s/^\Q$dir\E// &&
74 $page_trimmed=~s/^\Q$dir\E//;
76 push @links, { url => $href, page => $p_trimmed };
80 return sort { $a->{page} cmp $b->{page} } @links;
83 sub parentlinks ($) { #{{{
90 foreach my $dir (reverse split("/", $page)) {
93 unshift @ret, { url => "$path$dir.html", page => $dir };
99 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
103 sub preprocess ($$;$) { #{{{
106 my $onlystrip=shift || 0; # strip directives without processing
112 if (length $escape) {
113 return "[[$command $params]]";
118 elsif (exists $hooks{preprocess}{$command}) {
119 # Note: preserve order of params, some plugins may
120 # consider it significant.
122 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
123 push @params, $1, $2;
125 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
128 return "[[$command not processed]]";
132 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
136 sub add_depends ($$) { #{{{
140 if (! exists $depends{$page}) {
141 $depends{$page}=$globlist;
144 $depends{$page}=globlist_merge($depends{$page}, $globlist);
148 sub globlist_merge ($$) { #{{{
153 # Only add negated globs if they are not matched by the other globlist.
154 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
155 (map { [ $b, $_ ] } split(" ", $a))) {
156 if ($i->[1]=~/^!(.*)/) {
157 if (! globlist_match($1, $i->[0])) {
169 sub genpage ($$$) { #{{{
174 my $title=pagetitle(basename($page));
176 my $template=template("page.tmpl", blind_cache => 1);
179 if (length $config{cgiurl}) {
180 $template->param(editurl => cgiurl(do => "edit", page => $page));
181 $template->param(prefsurl => cgiurl(do => "prefs"));
183 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
188 if (length $config{historyurl}) {
189 my $u=$config{historyurl};
190 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
191 $template->param(historyurl => $u);
194 if ($config{discussion}) {
195 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
200 $template->param(have_actions => 1);
205 wikiname => $config{wikiname},
206 parentlinks => [parentlinks($page)],
208 backlinks => [backlinks($page)],
209 mtime => displaytime($mtime),
210 styleurl => styleurl($page),
213 if (exists $hooks{pagetemplate}) {
214 foreach my $id (keys %{$hooks{pagetemplate}}) {
215 $hooks{pagetemplate}{$id}{call}->($page, $template);
219 return $template->output;
222 sub check_overwrite ($$) { #{{{
223 # Important security check. Make sure to call this before saving
224 # any files to the source directory.
228 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
229 error("$dest already exists and was not rendered from $src before");
233 sub displaytime ($) { #{{{
237 # strftime doesn't know about encodings, so make sure
238 # its output is properly treated as utf8
239 return Encode::decode_utf8(POSIX::strftime(
240 $config{timeformat}, localtime($time)));
246 return (stat($file))[9];
249 sub findlinks ($$) { #{{{
254 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
255 push @links, titlepage($2);
257 if ($config{discussion}) {
258 # Discussion links are a special case since they're not in the
259 # text of the page, but on its template.
260 return @links, "$page/discussion";
267 sub render ($) { #{{{
270 my $type=pagetype($file);
271 my $srcfile=srcfile($file);
272 if ($type ne 'unknown') {
273 my $content=readfile($srcfile);
274 my $page=pagename($file);
275 delete $depends{$page};
277 if (exists $hooks{filter}) {
278 foreach my $id (keys %{$hooks{filter}}) {
279 $content=$hooks{filter}{$id}{call}->(
286 $links{$page}=[findlinks($page, $content)];
288 $content=linkify($page, $page, $content);
289 $content=preprocess($page, $content);
290 $content=htmlize($type, $content);
292 check_overwrite("$config{destdir}/".htmlpage($page), $page);
293 writefile(htmlpage($page), $config{destdir},
294 genpage($page, $content, mtime($srcfile)));
295 $oldpagemtime{$page}=time;
296 $renderedfiles{$page}=htmlpage($page);
299 my $content=readfile($srcfile, 1);
301 delete $depends{$file};
302 check_overwrite("$config{destdir}/$file", $file);
303 writefile($file, $config{destdir}, $content, 1);
304 $oldpagemtime{$file}=time;
305 $renderedfiles{$file}=$file;
313 my $dir=dirname($file);
314 while (rmdir($dir)) {
319 sub refresh () { #{{{
320 # find existing pages
323 eval q{use File::Find};
327 $_ = Encode::decode_utf8($_);
328 if (/$config{wiki_file_prune_regexp}/) {
329 $File::Find::prune=1;
331 elsif (! -d $_ && ! -l $_) {
332 my ($f)=/$config{wiki_file_regexp}/; # untaint
334 warn("skipping bad filename $_\n");
337 $f=~s/^\Q$config{srcdir}\E\/?//;
339 $exists{pagename($f)}=1;
347 $_ = Encode::decode_utf8($_);
348 if (/$config{wiki_file_prune_regexp}/) {
349 $File::Find::prune=1;
351 elsif (! -d $_ && ! -l $_) {
352 my ($f)=/$config{wiki_file_regexp}/; # untaint
354 warn("skipping bad filename $_\n");
357 # Don't add files that are in the
359 $f=~s/^\Q$config{underlaydir}\E\/?//;
360 if (! -e "$config{srcdir}/$f" &&
361 ! -l "$config{srcdir}/$f") {
363 $exists{pagename($f)}=1;
368 }, $config{underlaydir});
372 # check for added or removed pages
374 foreach my $file (@files) {
375 my $page=pagename($file);
376 if (! $oldpagemtime{$page}) {
377 debug("new page $page") unless exists $pagectime{$page};
380 $pagesources{$page}=$file;
381 if ($config{getctime} && -e "$config{srcdir}/$file") {
382 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
384 elsif (! exists $pagectime{$page}) {
385 $pagectime{$page}=mtime(srcfile($file));
390 foreach my $page (keys %oldpagemtime) {
391 if (! $exists{$page}) {
392 debug("removing old page $page");
393 push @del, $pagesources{$page};
394 prune($config{destdir}."/".$renderedfiles{$page});
395 delete $renderedfiles{$page};
396 $oldpagemtime{$page}=0;
397 delete $pagesources{$page};
401 # render any updated files
402 foreach my $file (@files) {
403 my $page=pagename($file);
405 if (! exists $oldpagemtime{$page} ||
406 mtime(srcfile($file)) > $oldpagemtime{$page}) {
407 debug("rendering $file");
413 # if any files were added or removed, check to see if each page
414 # needs an update due to linking to them or inlining them.
415 # TODO: inefficient; pages may get rendered above and again here;
416 # problem is the bestlink may have changed and we won't know until
419 FILE: foreach my $file (@files) {
420 my $page=pagename($file);
421 foreach my $f (@add, @del) {
423 foreach my $link (@{$links{$page}}) {
424 if (bestlink($page, $link) eq $p) {
425 debug("rendering $file, which links to $p");
435 # Handle backlinks; if a page has added/removed links, update the
436 # pages it links to. Also handles rebuilding dependant pages.
437 # TODO: inefficient; pages may get rendered above and again here;
438 # problem is the backlinks could be wrong in the first pass render
440 if (%rendered || @del) {
441 foreach my $f (@files) {
443 if (exists $depends{$p}) {
444 foreach my $file (keys %rendered, @del) {
446 my $page=pagename($file);
447 if (globlist_match($page, $depends{$p})) {
448 debug("rendering $f, which depends on $page");
458 foreach my $file (keys %rendered, @del) {
459 my $page=pagename($file);
461 if (exists $links{$page}) {
462 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
464 (! exists $oldlinks{$page} ||
465 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
466 $linkchanged{$link}=1;
470 if (exists $oldlinks{$page}) {
471 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
473 (! exists $links{$page} ||
474 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
475 $linkchanged{$link}=1;
480 foreach my $link (keys %linkchanged) {
481 my $linkfile=$pagesources{$link};
482 if (defined $linkfile) {
483 debug("rendering $linkfile, to update its backlinks");
485 $rendered{$linkfile}=1;
490 if (@del && exists $hooks{delete}) {
491 foreach my $id (keys %{$hooks{delete}}) {
492 $hooks{delete}{$id}{call}->(@del);
495 if (%rendered && exists $hooks{change}) {
496 foreach my $id (keys %{$hooks{change}}) {
497 $hooks{change}{$id}{call}->(keys %rendered);