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"}) {
28 # Note: a proper perl module is available in Debian
29 # for markdown, but not upstream yet.
31 $blosxom::version="is a proper perl module too much to ask?";
33 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 rendered from ".
230 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
232 ", before, so not rendering from $src");
236 sub displaytime ($) { #{{{
240 # strftime doesn't know about encodings, so make sure
241 # its output is properly treated as utf8
242 return Encode::decode_utf8(POSIX::strftime(
243 $config{timeformat}, localtime($time)));
249 return (stat($file))[9];
252 sub findlinks ($$) { #{{{
257 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
258 push @links, titlepage($2);
260 if ($config{discussion}) {
261 # Discussion links are a special case since they're not in the
262 # text of the page, but on its template.
263 return @links, "$page/discussion";
270 sub render ($) { #{{{
273 my $type=pagetype($file);
274 my $srcfile=srcfile($file);
275 if ($type ne 'unknown') {
276 my $content=readfile($srcfile);
277 my $page=pagename($file);
278 delete $depends{$page};
280 if (exists $hooks{filter}) {
281 foreach my $id (keys %{$hooks{filter}}) {
282 $content=$hooks{filter}{$id}{call}->(
289 $links{$page}=[findlinks($page, $content)];
291 $content=linkify($page, $page, $content);
292 $content=preprocess($page, $content);
293 $content=htmlize($type, $content);
295 check_overwrite("$config{destdir}/".htmlpage($page), $page);
296 writefile(htmlpage($page), $config{destdir},
297 genpage($page, $content, mtime($srcfile)));
298 $oldpagemtime{$page}=time;
299 $renderedfiles{$page}=htmlpage($page);
302 my $content=readfile($srcfile, 1);
304 delete $depends{$file};
305 check_overwrite("$config{destdir}/$file", $file);
306 writefile($file, $config{destdir}, $content, 1);
307 $oldpagemtime{$file}=time;
308 $renderedfiles{$file}=$file;
316 my $dir=dirname($file);
317 while (rmdir($dir)) {
322 sub refresh () { #{{{
323 # find existing pages
326 eval q{use File::Find};
331 $_ = Encode::decode_utf8($_);
332 if (/$config{wiki_file_prune_regexp}/) {
333 $File::Find::prune=1;
335 elsif (! -d $_ && ! -l $_) {
336 my ($f)=/$config{wiki_file_regexp}/; # untaint
338 warn("skipping bad filename $_\n");
341 $f=~s/^\Q$config{srcdir}\E\/?//;
343 $exists{pagename($f)}=1;
351 $_ = Encode::decode_utf8($_);
352 if (/$config{wiki_file_prune_regexp}/) {
353 $File::Find::prune=1;
355 elsif (! -d $_ && ! -l $_) {
356 my ($f)=/$config{wiki_file_regexp}/; # untaint
358 warn("skipping bad filename $_\n");
361 # Don't add files that are in the
363 $f=~s/^\Q$config{underlaydir}\E\/?//;
364 if (! -e "$config{srcdir}/$f" &&
365 ! -l "$config{srcdir}/$f") {
367 $exists{pagename($f)}=1;
372 }, $config{underlaydir});
376 # check for added or removed pages
378 foreach my $file (@files) {
379 my $page=pagename($file);
380 if (! $oldpagemtime{$page}) {
381 debug("new page $page") unless exists $pagectime{$page};
384 $pagesources{$page}=$file;
385 if ($config{getctime} && -e "$config{srcdir}/$file") {
386 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
388 elsif (! exists $pagectime{$page}) {
389 $pagectime{$page}=mtime(srcfile($file));
394 foreach my $page (keys %oldpagemtime) {
395 if (! $exists{$page}) {
396 debug("removing old page $page");
397 push @del, $pagesources{$page};
398 prune($config{destdir}."/".$renderedfiles{$page});
399 delete $renderedfiles{$page};
400 $oldpagemtime{$page}=0;
401 delete $pagesources{$page};
405 # render any updated files
406 foreach my $file (@files) {
407 my $page=pagename($file);
409 if (! exists $oldpagemtime{$page} ||
410 mtime(srcfile($file)) > $oldpagemtime{$page}) {
411 debug("rendering $file");
417 # if any files were added or removed, check to see if each page
418 # needs an update due to linking to them or inlining them.
419 # TODO: inefficient; pages may get rendered above and again here;
420 # problem is the bestlink may have changed and we won't know until
423 FILE: foreach my $file (@files) {
424 my $page=pagename($file);
425 foreach my $f (@add, @del) {
427 foreach my $link (@{$links{$page}}) {
428 if (bestlink($page, $link) eq $p) {
429 debug("rendering $file, which links to $p");
439 # Handle backlinks; if a page has added/removed links, update the
440 # pages it links to. Also handles rebuilding dependant pages.
441 # TODO: inefficient; pages may get rendered above and again here;
442 # problem is the backlinks could be wrong in the first pass render
444 if (%rendered || @del) {
445 foreach my $f (@files) {
447 if (exists $depends{$p}) {
448 foreach my $file (keys %rendered, @del) {
450 my $page=pagename($file);
451 if (globlist_match($page, $depends{$p})) {
452 debug("rendering $f, which depends on $page");
462 foreach my $file (keys %rendered, @del) {
463 my $page=pagename($file);
465 if (exists $links{$page}) {
466 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
468 (! exists $oldlinks{$page} ||
469 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
470 $linkchanged{$link}=1;
474 if (exists $oldlinks{$page}) {
475 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
477 (! exists $links{$page} ||
478 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
479 $linkchanged{$link}=1;
484 foreach my $link (keys %linkchanged) {
485 my $linkfile=$pagesources{$link};
486 if (defined $linkfile) {
487 debug("rendering $linkfile, to update its backlinks");
489 $rendered{$linkfile}=1;
494 if (@del && exists $hooks{delete}) {
495 foreach my $id (keys %{$hooks{delete}}) {
496 $hooks{delete}{$id}{call}->(@del);
499 if (%rendered && exists $hooks{change}) {
500 foreach my $id (keys %{$hooks{change}}) {
501 $hooks{change}{$id}{call}->(keys %rendered);