7 sub linkify ($$) { #{{{
11 $content =~ s{(\\?)$config{wiki_link_regexp}}{
12 $2 ? ( $1 ? "[[$2|$3]]" : htmllink($page, titlepage($3), 0, 0, pagetitle($2)))
13 : ( $1 ? "[[$3]]" : htmllink($page, titlepage($3)))
19 sub htmlize ($$) { #{{{
23 if (! $INC{"/usr/bin/markdown"}) {
25 $blosxom::version="is a proper perl module too much to ask?";
27 do "/usr/bin/markdown";
30 if ($type eq '.mdwn') {
31 return Markdown::Markdown($content);
34 error("htmlization of $type not supported");
38 sub backlinks ($) { #{{{
42 foreach my $p (keys %links) {
43 next if bestlink($page, $p) eq $page;
44 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
45 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
47 # Trim common dir prefixes from both pages.
49 my $page_trimmed=$page;
51 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
53 $p_trimmed=~s/^\Q$dir\E// &&
54 $page_trimmed=~s/^\Q$dir\E//;
56 push @links, { url => $href, page => $p_trimmed };
60 return sort { $a->{page} cmp $b->{page} } @links;
63 sub parentlinks ($) { #{{{
70 foreach my $dir (reverse split("/", $page)) {
73 unshift @ret, { url => "$path$dir.html", page => $dir };
79 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
83 sub rsspage ($) { #{{{
89 sub postprocess { #{{{
90 # Takes content to postprocess followed by a list of postprocessor
91 # commands and subroutine references to run for the commands.
100 if (length $escape) {
101 "[[$command $params]]";
103 elsif (exists $commands{$command}) {
105 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
108 $commands{$command}->($page, %params);
111 "[[bad directive $command]]";
115 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
119 sub blog_list ($$) { #{{{
124 foreach my $page (keys %pagesources) {
125 if (globlist_match($page, $globlist)) {
130 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
131 return @list if ! $maxitems || @list <= $maxitems;
132 return @list[0..$maxitems - 1];
135 sub get_inline_content ($$) { #{{{
136 my $parentpage=shift;
139 my $file=$pagesources{$page};
140 my $type=pagetype($file);
141 if ($type ne 'unknown') {
142 return htmlize($type, linkify(readfile(srcfile($file)), $parentpage));
149 sub postprocess_html_inline { #{{{
150 my $parentpage=shift;
153 if (! exists $params{pages}) {
156 if (! exists $params{archive}) {
157 $params{archive}="no";
159 if (! exists $params{show} && $params{archive} eq "no") {
162 $inlinepages{$parentpage}=$params{pages};
166 if (exists $params{rootpage}) {
167 my $formtemplate=HTML::Template->new(blind_cache => 1,
168 filename => "$config{templatedir}/blogpost.tmpl");
169 $formtemplate->param(cgiurl => $config{cgiurl});
170 $formtemplate->param(rootpage => $params{rootpage});
171 my $form=$formtemplate->output;
175 my $template=HTML::Template->new(blind_cache => 1,
176 filename => (($params{archive} eq "no")
177 ? "$config{templatedir}/inlinepage.tmpl"
178 : "$config{templatedir}/inlinepagetitle.tmpl"));
180 foreach my $page (blog_list($params{pages}, $params{show})) {
181 next if $page eq $parentpage;
182 $template->param(pagelink => htmllink($parentpage, $page));
183 $template->param(content => get_inline_content($parentpage, $page))
184 if $params{archive} eq "no";
185 $template->param(ctime => scalar(gmtime($pagectime{$page})));
186 $ret.=$template->output;
189 return "</p>$ret<p>";
192 sub genpage ($$$) { #{{{
197 $content = postprocess($page, $content, inline => \&postprocess_html_inline);
199 my $title=pagetitle(basename($page));
201 my $template=HTML::Template->new(blind_cache => 1,
202 filename => "$config{templatedir}/page.tmpl");
204 if (length $config{cgiurl}) {
205 $template->param(editurl => cgiurl(do => "edit", page => $page));
206 $template->param(prefsurl => cgiurl(do => "prefs"));
208 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
212 if (length $config{historyurl}) {
213 my $u=$config{historyurl};
214 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
215 $template->param(historyurl => $u);
218 if ($config{rss} && $inlinepages{$page}) {
219 $template->param(rssurl => rsspage(basename($page)));
224 wikiname => $config{wikiname},
225 parentlinks => [parentlinks($page)],
227 backlinks => [backlinks($page)],
228 discussionlink => htmllink($page, "Discussion", 1, 1),
229 mtime => scalar(gmtime($mtime)),
230 styleurl => styleurl($page),
233 return $template->output;
236 sub date_822 ($) { #{{{
240 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
243 sub absolute_urls ($$) { #{{{
244 # sucky sub because rss sucks
250 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
251 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
255 sub genrss ($$$) { #{{{
260 my $url="$config{url}/".htmlpage($page);
262 my $template=HTML::Template->new(blind_cache => 1,
263 filename => "$config{templatedir}/rsspage.tmpl");
268 my $parentpage=shift;
271 if (! exists $params{show}) {
274 if (! exists $params{pages}) {
279 foreach my $page (blog_list($params{pages}, $params{show})) {
280 next if $page eq $parentpage;
282 itemtitle => pagetitle(basename($page)),
283 itemurl => "$config{url}/$renderedfiles{$page}",
284 itempubdate => date_822($pagectime{$page}),
285 itemcontent => absolute_urls(get_inline_content($parentpage, $page), $url),
286 } if exists $renderedfiles{$page};
292 $content = postprocess($page, $content, inline => $gen_blog);
295 title => $config{wikiname},
300 return $template->output;
303 sub check_overwrite ($$) { #{{{
304 # Important security check. Make sure to call this before saving
305 # any files to the source directory.
309 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
310 error("$dest already exists and was rendered from ".
311 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
313 ", before, so not rendering from $src");
320 return (stat($file))[9];
323 sub findlinks ($$) { #{{{
328 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
329 push @links, titlepage($2);
331 # Discussion links are a special case since they're not in the text
332 # of the page, but on its template.
333 return @links, "$page/discussion";
336 sub render ($) { #{{{
339 my $type=pagetype($file);
340 my $srcfile=srcfile($file);
341 my $content=readfile($srcfile);
342 if ($type ne 'unknown') {
343 my $page=pagename($file);
345 $links{$page}=[findlinks($content, $page)];
346 delete $inlinepages{$page};
348 $content=linkify($content, $page);
349 $content=htmlize($type, $content);
351 check_overwrite("$config{destdir}/".htmlpage($page), $page);
352 writefile("$config{destdir}/".htmlpage($page),
353 genpage($content, $page, mtime($srcfile)));
354 $oldpagemtime{$page}=time;
355 $renderedfiles{$page}=htmlpage($page);
357 # TODO: should really add this to renderedfiles and call
358 # check_overwrite, as above, but currently renderedfiles
359 # only supports listing one file per page.
360 if ($config{rss} && exists $inlinepages{$page}) {
361 writefile("$config{destdir}/".rsspage($page),
362 genrss($content, $page, mtime($srcfile)));
367 check_overwrite("$config{destdir}/$file", $file);
368 writefile("$config{destdir}/$file", $content);
369 $oldpagemtime{$file}=time;
370 $renderedfiles{$file}=$file;
378 my $dir=dirname($file);
379 while (rmdir($dir)) {
384 sub refresh () { #{{{
385 # find existing pages
388 eval q{use File::Find};
392 if (/$config{wiki_file_prune_regexp}/) {
393 $File::Find::prune=1;
395 elsif (! -d $_ && ! -l $_) {
396 my ($f)=/$config{wiki_file_regexp}/; # untaint
398 warn("skipping bad filename $_\n");
401 $f=~s/^\Q$config{srcdir}\E\/?//;
403 $exists{pagename($f)}=1;
411 if (/$config{wiki_file_prune_regexp}/) {
412 $File::Find::prune=1;
414 elsif (! -d $_ && ! -l $_) {
415 my ($f)=/$config{wiki_file_regexp}/; # untaint
417 warn("skipping bad filename $_\n");
420 # Don't add files that are in the
422 $f=~s/^\Q$config{underlaydir}\E\/?//;
423 if (! -e "$config{srcdir}/$f" &&
424 ! -l "$config{srcdir}/$f") {
426 $exists{pagename($f)}=1;
431 }, $config{underlaydir});
435 # check for added or removed pages
437 foreach my $file (@files) {
438 my $page=pagename($file);
439 if (! $oldpagemtime{$page}) {
440 debug("new page $page") unless exists $pagectime{$page};
443 $pagesources{$page}=$file;
444 $pagectime{$page}=mtime(srcfile($file))
445 unless exists $pagectime{$page};
449 foreach my $page (keys %oldpagemtime) {
450 if (! $exists{$page}) {
451 debug("removing old page $page");
452 push @del, $pagesources{$page};
453 prune($config{destdir}."/".$renderedfiles{$page});
454 delete $renderedfiles{$page};
455 $oldpagemtime{$page}=0;
456 delete $pagesources{$page};
460 # render any updated files
461 foreach my $file (@files) {
462 my $page=pagename($file);
464 if (! exists $oldpagemtime{$page} ||
465 mtime(srcfile($file)) > $oldpagemtime{$page}) {
466 debug("rendering changed file $file");
472 # if any files were added or removed, check to see if each page
473 # needs an update due to linking to them or inlining them.
474 # TODO: inefficient; pages may get rendered above and again here;
475 # problem is the bestlink may have changed and we won't know until
478 FILE: foreach my $file (@files) {
479 my $page=pagename($file);
480 foreach my $f (@add, @del) {
482 foreach my $link (@{$links{$page}}) {
483 if (bestlink($page, $link) eq $p) {
484 debug("rendering $file, which links to $p");
494 # Handle backlinks; if a page has added/removed links, update the
495 # pages it links to. Also handle inlining here.
496 # TODO: inefficient; pages may get rendered above and again here;
497 # problem is the backlinks could be wrong in the first pass render
499 if (%rendered || @del) {
500 foreach my $f (@files) {
502 if (exists $inlinepages{$p}) {
503 foreach my $file (keys %rendered, @del) {
504 my $page=pagename($file);
505 if (globlist_match($page, $inlinepages{$p})) {
506 debug("rendering $f, which inlines $page");
515 foreach my $file (keys %rendered, @del) {
516 my $page=pagename($file);
518 if (exists $links{$page}) {
519 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
521 ! exists $oldlinks{$page} ||
522 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
523 $linkchanged{$link}=1;
527 if (exists $oldlinks{$page}) {
528 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
530 ! exists $links{$page} ||
531 ! grep { $_ eq $link } @{$links{$page}}) {
532 $linkchanged{$link}=1;
537 foreach my $link (keys %linkchanged) {
538 my $linkfile=$pagesources{$link};
539 if (defined $linkfile) {
540 debug("rendering $linkfile, to update its backlinks");