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 (! $INC{"/usr/bin/markdown"}) {
29 $blosxom::version="is a proper perl module too much to ask?";
31 do "/usr/bin/markdown";
35 if ($type eq '.mdwn') {
36 # Workaround for perl bug (#376329)
37 $content=Encode::encode_utf8($content);
38 $content=Encode::encode_utf8($content);
39 $content=Markdown::Markdown($content);
40 $content=Encode::decode_utf8($content);
41 $content=Encode::decode_utf8($content);
44 error("htmlization of $type not supported");
47 if (exists $hooks{sanitize}) {
48 foreach my $id (keys %{$hooks{sanitize}}) {
49 $content=$hooks{sanitize}{$id}{call}->($content);
56 sub backlinks ($) { #{{{
60 foreach my $p (keys %links) {
61 next if bestlink($page, $p) eq $page;
62 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
63 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
65 # Trim common dir prefixes from both pages.
67 my $page_trimmed=$page;
69 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
71 $p_trimmed=~s/^\Q$dir\E// &&
72 $page_trimmed=~s/^\Q$dir\E//;
74 push @links, { url => $href, page => $p_trimmed };
78 return sort { $a->{page} cmp $b->{page} } @links;
81 sub parentlinks ($) { #{{{
88 foreach my $dir (reverse split("/", $page)) {
91 unshift @ret, { url => "$path$dir.html", page => $dir };
97 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
101 sub preprocess ($$;$) { #{{{
104 my $onlystrip=shift || 0; # strip directives without processing
110 if (length $escape) {
111 return "[[$command $params]]";
116 elsif (exists $hooks{preprocess}{$command}) {
117 # Note: preserve order of params, some plugins may
118 # consider it significant.
120 while ($params =~ /(\w+)=\"?([^"]+)"?(\s+|$)/g) {
121 push @params, $1, $2;
123 return $hooks{preprocess}{$command}{call}->(@params, page => $page);
126 return "[[$command not processed]]";
130 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
134 sub add_depends ($$) { #{{{
138 if (! exists $depends{$page}) {
139 $depends{$page}=$globlist;
142 $depends{$page}=globlist_merge($depends{$page}, $globlist);
146 sub globlist_merge ($$) { #{{{
151 # Only add negated globs if they are not matched by the other globlist.
152 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
153 (map { [ $b, $_ ] } split(" ", $a))) {
154 if ($i->[1]=~/^!(.*)/) {
155 if (! globlist_match($1, $i->[0])) {
167 sub genpage ($$$) { #{{{
172 my $title=pagetitle(basename($page));
174 my $template=HTML::Template->new(blind_cache => 1,
175 filename => "$config{templatedir}/page.tmpl");
178 if (length $config{cgiurl}) {
179 $template->param(editurl => cgiurl(do => "edit", page => $page));
180 $template->param(prefsurl => cgiurl(do => "prefs"));
182 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
187 if (length $config{historyurl}) {
188 my $u=$config{historyurl};
189 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
190 $template->param(historyurl => $u);
193 if ($config{discussion}) {
194 $template->param(discussionlink => htmllink($page, $page, "Discussion", 1, 1));
199 $template->param(have_actions => 1);
204 wikiname => $config{wikiname},
205 parentlinks => [parentlinks($page)],
207 backlinks => [backlinks($page)],
208 mtime => displaytime($mtime),
209 styleurl => styleurl($page),
212 if (exists $hooks{pagetemplate}) {
213 foreach my $id (keys %{$hooks{pagetemplate}}) {
214 $hooks{pagetemplate}{$id}{call}->($page, $template);
218 return $template->output;
221 sub check_overwrite ($$) { #{{{
222 # Important security check. Make sure to call this before saving
223 # any files to the source directory.
227 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
228 error("$dest already exists and was rendered from ".
229 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
231 ", before, so not rendering from $src");
235 sub displaytime ($) { #{{{
239 # strftime doesn't know about encodings, so make sure
240 # its output is properly treated as utf8
241 return Encode::decode_utf8(POSIX::strftime(
242 $config{timeformat}, localtime($time)));
248 return (stat($file))[9];
251 sub findlinks ($$) { #{{{
256 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
257 push @links, titlepage($2);
259 if ($config{discussion}) {
260 # Discussion links are a special case since they're not in the
261 # text of the page, but on its template.
262 return @links, "$page/discussion";
269 sub render ($) { #{{{
272 my $type=pagetype($file);
273 my $srcfile=srcfile($file);
274 if ($type ne 'unknown') {
275 my $content=readfile($srcfile);
276 my $page=pagename($file);
277 delete $depends{$page};
279 if (exists $hooks{filter}) {
280 foreach my $id (keys %{$hooks{filter}}) {
281 $content=$hooks{filter}{$id}{call}->(
288 $links{$page}=[findlinks($page, $content)];
290 $content=linkify($page, $page, $content);
291 $content=preprocess($page, $content);
292 $content=htmlize($type, $content);
294 check_overwrite("$config{destdir}/".htmlpage($page), $page);
295 writefile(htmlpage($page), $config{destdir},
296 genpage($page, $content, mtime($srcfile)));
297 $oldpagemtime{$page}=time;
298 $renderedfiles{$page}=htmlpage($page);
301 my $content=readfile($srcfile, 1);
303 delete $depends{$file};
304 check_overwrite("$config{destdir}/$file", $file);
305 writefile($file, $config{destdir}, $content, 1);
306 $oldpagemtime{$file}=time;
307 $renderedfiles{$file}=$file;
315 my $dir=dirname($file);
316 while (rmdir($dir)) {
321 sub refresh () { #{{{
322 # find existing pages
325 eval q{use File::Find};
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 $f=~s/^\Q$config{srcdir}\E\/?//;
340 $exists{pagename($f)}=1;
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 changed file $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);