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 preprocess ($$) { #{{{
93 my %commands=(inline => \&preprocess_inline);
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(srcfile($file)), $parentpage));
148 sub preprocess_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"));
180 foreach my $page (blog_list($params{pages}, $params{show})) {
181 next if $page eq $parentpage;
183 $template->param(pagelink => htmllink($parentpage, $page));
184 $template->param(content => get_inline_content($parentpage, $page))
185 if $params{archive} eq "no";
186 $template->param(ctime => scalar(gmtime($pagectime{$page})));
187 $ret.=$template->output;
190 # TODO: should really add this to renderedfiles and call
191 # check_overwrite, but currently renderedfiles
192 # only supports listing one file per page.
194 writefile(rsspage($parentpage), $config{destdir},
195 genrss($parentpage, @pages));
201 sub genpage ($$$) { #{{{
206 my $title=pagetitle(basename($page));
208 my $template=HTML::Template->new(blind_cache => 1,
209 filename => "$config{templatedir}/page.tmpl");
211 if (length $config{cgiurl}) {
212 $template->param(editurl => cgiurl(do => "edit", page => $page));
213 $template->param(prefsurl => cgiurl(do => "prefs"));
215 $template->param(recentchangesurl => cgiurl(do => "recentchanges"));
219 if (length $config{historyurl}) {
220 my $u=$config{historyurl};
221 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
222 $template->param(historyurl => $u);
224 if ($config{hyperestraier}) {
225 $template->param(hyperestraierurl => cgiurl());
228 if ($config{rss} && $inlinepages{$page}) {
229 $template->param(rssurl => rsspage(basename($page)));
234 wikiname => $config{wikiname},
235 parentlinks => [parentlinks($page)],
237 backlinks => [backlinks($page)],
238 discussionlink => htmllink($page, "Discussion", 1, 1),
239 mtime => scalar(gmtime($mtime)),
240 styleurl => styleurl($page),
243 return $template->output;
246 sub date_822 ($) { #{{{
250 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
253 sub absolute_urls ($$) { #{{{
254 # sucky sub because rss sucks
260 $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
261 $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
265 sub genrss ($@) { #{{{
269 my $url="$config{url}/".htmlpage($page);
271 my $template=HTML::Template->new(blind_cache => 1,
272 filename => "$config{templatedir}/rsspage.tmpl");
275 foreach my $p (@pages) {
277 itemtitle => pagetitle(basename($p)),
278 itemurl => "$config{url}/$renderedfiles{$p}",
279 itempubdate => date_822($pagectime{$p}),
280 itemcontent => absolute_urls(get_inline_content($page, $p), $url),
281 } if exists $renderedfiles{$p};
285 title => $config{wikiname},
290 return $template->output;
293 sub check_overwrite ($$) { #{{{
294 # Important security check. Make sure to call this before saving
295 # any files to the source directory.
299 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
300 error("$dest already exists and was rendered from ".
301 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
303 ", before, so not rendering from $src");
310 return (stat($file))[9];
313 sub findlinks ($$) { #{{{
318 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
319 push @links, titlepage($2);
321 # Discussion links are a special case since they're not in the text
322 # of the page, but on its template.
323 return @links, "$page/discussion";
326 sub render ($) { #{{{
329 my $type=pagetype($file);
330 my $srcfile=srcfile($file);
331 if ($type ne 'unknown') {
332 my $content=readfile($srcfile);
333 my $page=pagename($file);
335 $links{$page}=[findlinks($content, $page)];
336 delete $inlinepages{$page};
338 $content=linkify($content, $page);
339 $content=preprocess($page, $content);
340 $content=htmlize($type, $content);
342 check_overwrite("$config{destdir}/".htmlpage($page), $page);
343 writefile(htmlpage($page), $config{destdir},
344 genpage($content, $page, mtime($srcfile)));
345 $oldpagemtime{$page}=time;
346 $renderedfiles{$page}=htmlpage($page);
349 my $content=readfile($srcfile, 1);
351 check_overwrite("$config{destdir}/$file", $file);
352 writefile($file, $config{destdir}, $content, 1);
353 $oldpagemtime{$file}=time;
354 $renderedfiles{$file}=$file;
362 my $dir=dirname($file);
363 while (rmdir($dir)) {
369 my $estdir="$config{wikistatedir}/hyperestraier";
370 my $cgi=basename($config{cgiurl});
372 open(TEMPLATE, ">$estdir/$cgi.tmpl") ||
373 error("write $estdir/$cgi.tmpl: $!");
374 print TEMPLATE misctemplate("search",
375 "<!--ESTFORM-->\n\n<!--ESTRESULT-->\n\n<!--ESTINFO-->\n\n");
377 open(TEMPLATE, ">$estdir/$cgi.conf") ||
378 error("write $estdir/$cgi.conf: $!");
379 my $template=HTML::Template->new(
380 filename => "$config{templatedir}/estseek.conf"
382 eval q{use Cwd 'abs_path'};
385 tmplfile => "$estdir/$cgi.tmpl",
386 destdir => abs_path($config{destdir}),
389 print TEMPLATE $template->output;
391 $cgi="$estdir/".basename($config{cgiurl});
393 symlink("/usr/lib/estraier/estseek.cgi", $cgi) ||
394 error("symlink $cgi: $!");
397 sub estcmd ($;@) { #{{{
398 my @params=split(' ', shift);
399 push @params, "-cl", "$config{wikistatedir}/hyperestraier";
404 my $pid=open(CHILD, "|-");
410 close(CHILD) || error("estcmd @params exited nonzero: $?");
414 open(STDOUT, "/dev/null"); # shut it up (closing won't work)
415 exec("estcmd", @params) || error("can't run estcmd");
419 sub refresh () { #{{{
420 # find existing pages
423 eval q{use File::Find};
427 if (/$config{wiki_file_prune_regexp}/) {
428 $File::Find::prune=1;
430 elsif (! -d $_ && ! -l $_) {
431 my ($f)=/$config{wiki_file_regexp}/; # untaint
433 warn("skipping bad filename $_\n");
436 $f=~s/^\Q$config{srcdir}\E\/?//;
438 $exists{pagename($f)}=1;
446 if (/$config{wiki_file_prune_regexp}/) {
447 $File::Find::prune=1;
449 elsif (! -d $_ && ! -l $_) {
450 my ($f)=/$config{wiki_file_regexp}/; # untaint
452 warn("skipping bad filename $_\n");
455 # Don't add files that are in the
457 $f=~s/^\Q$config{underlaydir}\E\/?//;
458 if (! -e "$config{srcdir}/$f" &&
459 ! -l "$config{srcdir}/$f") {
461 $exists{pagename($f)}=1;
466 }, $config{underlaydir});
470 # check for added or removed pages
472 foreach my $file (@files) {
473 my $page=pagename($file);
474 if (! $oldpagemtime{$page}) {
475 debug("new page $page") unless exists $pagectime{$page};
478 $pagesources{$page}=$file;
479 $pagectime{$page}=mtime(srcfile($file))
480 unless exists $pagectime{$page};
484 foreach my $page (keys %oldpagemtime) {
485 if (! $exists{$page}) {
486 debug("removing old page $page");
487 push @del, $pagesources{$page};
488 prune($config{destdir}."/".$renderedfiles{$page});
489 delete $renderedfiles{$page};
490 $oldpagemtime{$page}=0;
491 delete $pagesources{$page};
495 # render any updated files
496 foreach my $file (@files) {
497 my $page=pagename($file);
499 if (! exists $oldpagemtime{$page} ||
500 mtime(srcfile($file)) > $oldpagemtime{$page}) {
501 debug("rendering changed file $file");
507 # if any files were added or removed, check to see if each page
508 # needs an update due to linking to them or inlining them.
509 # TODO: inefficient; pages may get rendered above and again here;
510 # problem is the bestlink may have changed and we won't know until
513 FILE: foreach my $file (@files) {
514 my $page=pagename($file);
515 foreach my $f (@add, @del) {
517 foreach my $link (@{$links{$page}}) {
518 if (bestlink($page, $link) eq $p) {
519 debug("rendering $file, which links to $p");
529 # Handle backlinks; if a page has added/removed links, update the
530 # pages it links to. Also handle inlining here.
531 # TODO: inefficient; pages may get rendered above and again here;
532 # problem is the backlinks could be wrong in the first pass render
534 if (%rendered || @del) {
535 foreach my $f (@files) {
537 if (exists $inlinepages{$p}) {
538 foreach my $file (keys %rendered, @del) {
539 my $page=pagename($file);
540 if (globlist_match($page, $inlinepages{$p})) {
541 debug("rendering $f, which inlines $page");
551 foreach my $file (keys %rendered, @del) {
552 my $page=pagename($file);
554 if (exists $links{$page}) {
555 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
557 ! exists $oldlinks{$page} ||
558 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
559 $linkchanged{$link}=1;
563 if (exists $oldlinks{$page}) {
564 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
566 ! exists $links{$page} ||
567 ! grep { $_ eq $link } @{$links{$page}}) {
568 $linkchanged{$link}=1;
573 foreach my $link (keys %linkchanged) {
574 my $linkfile=$pagesources{$link};
575 if (defined $linkfile) {
576 debug("rendering $linkfile, to update its backlinks");
578 $rendered{$linkfile}=1;
583 if ($config{hyperestraier} && (%rendered || @del)) {
584 debug("updating hyperestraier search index");
586 estcmd("gather -cm -bc -cl -sd",
587 map { $config{destdir}."/".$renderedfiles{pagename($_)} }
594 debug("generating hyperestraier cgi config");