7 sub linkify ($$) { #{{{
11 $content =~ s{(\\?)$config{wiki_link_regexp}}{
12 $1 ? "[[$2]]" : htmllink($page, $2)
18 sub htmlize ($$) { #{{{
22 if (! $INC{"/usr/bin/markdown"}) {
24 $blosxom::version="is a proper perl module too much to ask?";
26 do "/usr/bin/markdown";
29 if ($type eq '.mdwn') {
30 return Markdown::Markdown($content);
33 error("htmlization of $type not supported");
37 sub backlinks ($) { #{{{
41 foreach my $p (keys %links) {
42 next if bestlink($page, $p) eq $page;
43 if (grep { length $_ && bestlink($p, $_) eq $page } @{$links{$p}}) {
44 my $href=File::Spec->abs2rel(htmlpage($p), dirname($page));
46 # Trim common dir prefixes from both pages.
48 my $page_trimmed=$page;
50 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
52 $p_trimmed=~s/^\Q$dir\E// &&
53 $page_trimmed=~s/^\Q$dir\E//;
55 push @links, { url => $href, page => $p_trimmed };
59 return sort { $a->{page} cmp $b->{page} } @links;
62 sub parentlinks ($) { #{{{
69 foreach my $dir (reverse split("/", $page)) {
72 unshift @ret, { url => "$path$dir.html", page => $dir };
78 unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
82 sub rsspage ($) { #{{{
88 sub postprocess { #{{{
89 # Takes content to postprocess followed by a list of postprocessor
90 # commands and subroutine references to run for the commands.
100 "[[$command $params]]";
102 elsif (exists $commands{$command}) {
104 while ($params =~ /(\w+)=\"([^"]+)"(\s+|$)/g) {
107 $commands{$command}->($page, %params);
110 "[[bad directive $command]]";
114 $content =~ s{(\\?)$config{wiki_processor_regexp}}{$handle->($1, $2, $3)}eg;
118 sub blog_list ($$) { #{{{
123 foreach my $page (keys %pagesources) {
124 if (globlist_match($page, $globlist)) {
129 @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
130 return @list if ! $maxitems || @list <= $maxitems;
131 return @list[0..$maxitems - 1];
134 sub get_inline_content ($$) { #{{{
135 my $parentpage=shift;
138 my $file=$pagesources{$page};
139 my $type=pagetype($file);
140 if ($type ne 'unknown') {
141 return htmlize($type, linkify(readfile("$config{srcdir}/$file"), $parentpage));
148 sub postprocess_html_inline { #{{{
149 my $parentpage=shift;
152 if (! exists $params{pages}) {
155 if (! exists $params{archive}) {
156 $params{archive}="no";
158 if (! exists $params{show} && $params{archive} eq "no") {
161 $inlinepages{$parentpage}=$params{pages};
165 if (exists $params{rootpage}) {
166 my $formtemplate=HTML::Template->new(blind_cache => 1,
167 filename => "$config{templatedir}/blogpost.tmpl");
168 $formtemplate->param(cgiurl => $config{cgiurl});
169 $formtemplate->param(rootpage => $params{rootpage});
170 my $form=$formtemplate->output;
174 my $template=HTML::Template->new(blind_cache => 1,
175 filename => (($params{archive} eq "no")
176 ? "$config{templatedir}/inlinepage.tmpl"
177 : "$config{templatedir}/inlinepagetitle.tmpl"));
179 foreach my $page (blog_list($params{pages}, $params{show})) {
180 next if $page eq $parentpage;
181 $template->param(pagelink => htmllink($parentpage, $page));
182 $template->param(content => get_inline_content($parentpage, $page))
183 if $params{archive} eq "no";
184 $template->param(ctime => scalar(gmtime($pagectime{$page})));
185 $ret.=$template->output;
191 sub genpage ($$$) { #{{{
196 $content = postprocess($page, $content, inline => \&postprocess_html_inline);
198 my $title=pagetitle(basename($page));
200 my $template=HTML::Template->new(blind_cache => 1,
201 filename => "$config{templatedir}/page.tmpl");
203 if (length $config{cgiurl}) {
204 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
205 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
207 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
211 if (length $config{historyurl}) {
212 my $u=$config{historyurl};
213 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
214 $template->param(historyurl => $u);
217 if ($config{rss} && $inlinepages{$page}) {
218 $template->param(rssurl => rsspage($page));
223 wikiname => $config{wikiname},
224 parentlinks => [parentlinks($page)],
226 backlinks => [backlinks($page)],
227 discussionlink => htmllink($page, "Discussion", 1, 1),
228 mtime => scalar(gmtime($mtime)),
231 return $template->output;
234 sub date_822 ($) { #{{{
238 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
241 sub absolute_urls ($$) { #{{{
242 # sucky sub because rss sucks
248 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
249 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
253 sub genrss ($$$) { #{{{
258 my $url="$config{url}/".htmlpage($page);
260 my $template=HTML::Template->new(blind_cache => 1,
261 filename => "$config{templatedir}/rsspage.tmpl");
266 my $parentpage=shift;
269 return "" if exists $params{archive} && $params{archive} eq 'yes';
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($page))[9];
323 sub findlinks ($$) { #{{{
328 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
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 $content=readfile("$config{srcdir}/$file");
341 if ($type ne 'unknown') {
342 my $page=pagename($file);
344 $links{$page}=[findlinks($content, $page)];
345 delete $inlinepages{$page};
347 $content=linkify($content, $page);
348 $content=htmlize($type, $content);
350 check_overwrite("$config{destdir}/".htmlpage($page), $page);
351 writefile("$config{destdir}/".htmlpage($page),
352 genpage($content, $page, mtime("$config{srcdir}/$file")));
353 $oldpagemtime{$page}=time;
354 $renderedfiles{$page}=htmlpage($page);
356 # TODO: should really add this to renderedfiles and call
357 # check_overwrite, as above, but currently renderedfiles
358 # only supports listing one file per page.
359 if ($config{rss} && exists $inlinepages{$page}) {
360 writefile("$config{destdir}/".rsspage($page),
361 genrss($content, $page, mtime("$config{srcdir}/$file")));
366 check_overwrite("$config{destdir}/$file", $file);
367 writefile("$config{destdir}/$file", $content);
368 $oldpagemtime{$file}=time;
369 $renderedfiles{$file}=$file;
377 my $dir=dirname($file);
378 while (rmdir($dir)) {
383 sub refresh () { #{{{
384 # find existing pages
387 eval q{use File::Find};
391 if (/$config{wiki_file_prune_regexp}/) {
393 $File::Find::prune=1;
396 elsif (! -d $_ && ! -l $_) {
397 my ($f)=/$config{wiki_file_regexp}/; # untaint
399 warn("skipping bad filename $_\n");
402 $f=~s/^\Q$config{srcdir}\E\/?//;
404 $exists{pagename($f)}=1;
412 # check for added or removed pages
414 foreach my $file (@files) {
415 my $page=pagename($file);
416 if (! $oldpagemtime{$page}) {
417 debug("new page $page") unless exists $pagectime{$page};
420 $pagesources{$page}=$file;
421 $pagectime{$page}=time unless exists $pagectime{$page};
425 foreach my $page (keys %oldpagemtime) {
426 if (! $exists{$page}) {
427 debug("removing old page $page");
428 push @del, $pagesources{$page};
429 prune($config{destdir}."/".$renderedfiles{$page});
430 delete $renderedfiles{$page};
431 $oldpagemtime{$page}=0;
432 delete $pagesources{$page};
436 # render any updated files
437 foreach my $file (@files) {
438 my $page=pagename($file);
440 if (! exists $oldpagemtime{$page} ||
441 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
442 debug("rendering changed file $file");
448 # if any files were added or removed, check to see if each page
449 # needs an update due to linking to them or inlining them.
450 # TODO: inefficient; pages may get rendered above and again here;
451 # problem is the bestlink may have changed and we won't know until
454 FILE: foreach my $file (@files) {
455 my $page=pagename($file);
456 foreach my $f (@add, @del) {
458 foreach my $link (@{$links{$page}}) {
459 if (bestlink($page, $link) eq $p) {
460 debug("rendering $file, which links to $p");
470 # Handle backlinks; if a page has added/removed links, update the
471 # pages it links to. Also handle inlining here.
472 # TODO: inefficient; pages may get rendered above and again here;
473 # problem is the backlinks could be wrong in the first pass render
475 if (%rendered || @del) {
476 foreach my $f (@files) {
478 if (exists $inlinepages{$p}) {
479 foreach my $file (keys %rendered, @del) {
480 my $page=pagename($file);
481 if (globlist_match($page, $inlinepages{$p})) {
482 debug("rendering $f, which inlines $page");
491 foreach my $file (keys %rendered, @del) {
492 my $page=pagename($file);
494 if (exists $links{$page}) {
495 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
497 ! exists $oldlinks{$page} ||
498 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
499 $linkchanged{$link}=1;
503 if (exists $oldlinks{$page}) {
504 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
506 ! exists $links{$page} ||
507 ! grep { $_ eq $link } @{$links{$page}}) {
508 $linkchanged{$link}=1;
513 foreach my $link (keys %linkchanged) {
514 my $linkfile=$pagesources{$link};
515 if (defined $linkfile) {
516 debug("rendering $linkfile, to update its backlinks");