10 sub linkify ($$) { #{{{
14 $content =~ s{(\\?)$config{wiki_link_regexp}}{
15 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
16 : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
22 sub htmlize ($$) { #{{{
26 if (! $INC{"/usr/bin/markdown"}) {
28 $blosxom::version="is a proper perl module too much to ask?";
30 do "/usr/bin/markdown";
34 if ($type eq '.mdwn') {
35 # Markdown does character based stuff that does not work
36 # well with utf-8 strings.
37 $content=Encode::decode_utf8(Markdown::Markdown(Encode::encode_utf8($content)));
40 error("htmlization of $type not supported");
43 if (exists $hooks{sanitize}) {
44 foreach my $id (keys %{$hooks{sanitize}}) {
45 $content=$hooks{sanitize}{$id}{call}->($content);
52 sub backlinks ($) { #{{{
56 foreach my $p (keys %links) {
57 next if bestlink($page, $p) eq $page;
58 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
59 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
61 # Trim common dir prefixes from both pages.
63 my $page_trimmed=$page;
65 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
67 $p_trimmed=~s/^\Q$dir\E// &&
68 $page_trimmed=~s/^\Q$dir\E//;
70 push @links, { url => $href, page => $p_trimmed };
74 return sort { $a->{page} cmp $b->{page} } @links;
77 sub parentlinks ($) { #{{{
84 foreach my $dir (reverse split("/", $page)) {
87 unshift @ret, { url => "$path$dir.html", page => $dir };
93 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
97 sub preprocess ($$) { #{{{
105 if (length $escape) {
106 return "[[$command $params]]";
108 elsif (exists $hooks{preprocess}{$command}) {
110 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
113 return $hooks{preprocess}{$command}{call}->(page => $page, %params);
116 return "[[$command not processed]]";
120 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
124 sub add_depends ($$) { #{{{
128 if (! exists $depends{$page}) {
129 $depends{$page}=$globlist;
132 $depends{$page}=globlist_merge($depends{$page}, $globlist);
136 sub globlist_merge ($$) { #{{{
141 # Only add negated globs if they are not matched by the other globlist.
142 foreach my $i ((map { [ $a, $_ ] } split(" ", $b)),
143 (map { [ $b, $_ ] } split(" ", $a))) {
144 if ($i->[1]=~/^!(.*)/) {
145 if (! globlist_match($1, $i->[0])) {
157 sub genpage ($$$) { #{{{
162 my $title=pagetitle(basename($page));
164 my $template=HTML::Template->new(blind_cache => 1,
165 filename => "$config{templatedir}/page.tmpl");
168 if (length $config{cgiurl}) {
169 $template->param(editurl => cgiurl(do => "edit", page => $page));
170 $template->param(prefsurl => cgiurl(do => "prefs"));
172 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
177 if (length $config{historyurl}) {
178 my $u=$config{historyurl};
179 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
180 $template->param(historyurl => $u);
183 if ($config{discussion}) {
184 $template->param(discussionlink => htmllink($page, "Discussion", 1, 1));
189 $template->param(have_actions => 1);
192 if (exists $hooks{pagetemplate}) {
193 foreach my $id (keys %{$hooks{pagetemplate}}) {
194 $hooks{pagetemplate}{$id}{call}->($page, $template);
200 wikiname => $config{wikiname},
201 parentlinks => [parentlinks($page)],
203 backlinks => [backlinks($page)],
204 mtime => scalar(gmtime($mtime)),
205 styleurl => styleurl($page),
208 return $template->output;
211 sub check_overwrite ($$) { #{{{
212 # Important security check. Make sure to call this before saving
213 # any files to the source directory.
217 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
218 error("$dest already exists and was rendered from ".
219 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
221 ", before, so not rendering from $src");
228 return (stat($file))[9];
231 sub findlinks ($$) { #{{{
236 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
237 push @links, titlepage($2);
239 if ($config{discussion}) {
240 # Discussion links are a special case since they're not in the
241 # text of the page, but on its template.
242 return @links, "$page/discussion";
249 sub render ($) { #{{{
252 my $type=pagetype($file);
253 my $srcfile=srcfile($file);
254 if ($type ne 'unknown') {
255 my $content=readfile($srcfile);
256 my $page=pagename($file);
257 delete $depends{$page};
259 if (exists $hooks{filter}) {
260 foreach my $id (keys %{$hooks{filter}}) {
261 $content=$hooks{filter}{$id}{call}->(
268 $links{$page}=[findlinks($page, $content)];
270 $content=linkify($page, $content);
271 $content=preprocess($page, $content);
272 $content=htmlize($type, $content);
274 check_overwrite("$config{destdir}/".htmlpage($page), $page);
275 writefile(htmlpage($page), $config{destdir},
276 genpage($page, $content, mtime($srcfile)));
277 $oldpagemtime{$page}=time;
278 $renderedfiles{$page}=htmlpage($page);
281 my $content=readfile($srcfile, 1);
283 delete $depends{$file};
284 check_overwrite("$config{destdir}/$file", $file);
285 writefile($file, $config{destdir}, $content, 1);
286 $oldpagemtime{$file}=time;
287 $renderedfiles{$file}=$file;
295 my $dir=dirname($file);
296 while (rmdir($dir)) {
301 sub refresh () { #{{{
302 # find existing pages
305 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;
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 # Don't add files that are in the
339 $f=~s/^\Q$config{underlaydir}\E\/?//;
340 if (! -e "$config{srcdir}/$f" &&
341 ! -l "$config{srcdir}/$f") {
343 $exists{pagename($f)}=1;
348 }, $config{underlaydir});
352 # check for added or removed pages
354 foreach my $file (@files) {
355 my $page=pagename($file);
356 if (! $oldpagemtime{$page}) {
357 debug("new page $page") unless exists $pagectime{$page};
360 $pagesources{$page}=$file;
361 if ($config{getctime} && -e "$config{srcdir}/$file") {
362 $pagectime{$page}=rcs_getctime("$config{srcdir}/$file");
364 elsif (! exists $pagectime{$page}) {
365 $pagectime{$page}=mtime(srcfile($file));
370 foreach my $page (keys %oldpagemtime) {
371 if (! $exists{$page}) {
372 debug("removing old page $page");
373 push @del, $pagesources{$page};
374 prune($config{destdir}."/".$renderedfiles{$page});
375 delete $renderedfiles{$page};
376 $oldpagemtime{$page}=0;
377 delete $pagesources{$page};
381 # render any updated files
382 foreach my $file (@files) {
383 my $page=pagename($file);
385 if (! exists $oldpagemtime{$page} ||
386 mtime(srcfile($file)) > $oldpagemtime{$page}) {
387 debug("rendering changed file $file");
393 # if any files were added or removed, check to see if each page
394 # needs an update due to linking to them or inlining them.
395 # TODO: inefficient; pages may get rendered above and again here;
396 # problem is the bestlink may have changed and we won't know until
399 FILE: foreach my $file (@files) {
400 my $page=pagename($file);
401 foreach my $f (@add, @del) {
403 foreach my $link (@{$links{$page}}) {
404 if (bestlink($page, $link) eq $p) {
405 debug("rendering $file, which links to $p");
415 # Handle backlinks; if a page has added/removed links, update the
416 # pages it links to. Also handles rebuilding dependat pages.
417 # TODO: inefficient; pages may get rendered above and again here;
418 # problem is the backlinks could be wrong in the first pass render
420 if (%rendered || @del) {
421 foreach my $f (@files) {
423 if (exists $depends{$p}) {
424 foreach my $file (keys %rendered, @del) {
426 my $page=pagename($file);
427 if (globlist_match($page, $depends{$p})) {
428 debug("rendering $f, which depends on $page");
438 foreach my $file (keys %rendered, @del) {
439 my $page=pagename($file);
441 if (exists $links{$page}) {
442 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
444 (! exists $oldlinks{$page} ||
445 ! grep { bestlink($page, $_) eq $link } @{$oldlinks{$page}})) {
446 $linkchanged{$link}=1;
450 if (exists $oldlinks{$page}) {
451 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
453 (! exists $links{$page} ||
454 ! grep { bestlink($page, $_) eq $link } @{$links{$page}})) {
455 $linkchanged{$link}=1;
460 foreach my $link (keys %linkchanged) {
461 my $linkfile=$pagesources{$link};
462 if (defined $linkfile) {
463 debug("rendering $linkfile, to update its backlinks");
465 $rendered{$linkfile}=1;
470 if (@del && exists $hooks{delete}) {
471 foreach my $id (keys %{$hooks{delete}}) {
472 $hooks{delete}{$id}{call}->(@del);
475 if (%rendered && exists $hooks{change}) {
476 foreach my $id (keys %{$hooks{change}}) {
477 $hooks{change}{$id}{call}->(keys %rendered);