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 genpage ($$$) { #{{{
93 my $title=pagetitle(basename($page));
95 my $template=HTML::Template->new(blind_cache => 1,
96 filename => "$config{templatedir}/page.tmpl");
98 if (length $config{cgiurl}) {
99 $template->param(editurl => "$config{cgiurl}?do=edit&page=$page");
100 $template->param(prefsurl => "$config{cgiurl}?do=prefs");
102 $template->param(recentchangesurl => "$config{cgiurl}?do=recentchanges");
106 if (length $config{historyurl}) {
107 my $u=$config{historyurl};
108 $u=~s/\[\[file\]\]/$pagesources{$page}/g;
109 $template->param(historyurl => $u);
113 $template->param(rssurl => rsspage($page));
118 wikiname => $config{wikiname},
119 parentlinks => [parentlinks($page)],
121 backlinks => [backlinks($page)],
122 discussionlink => htmllink($page, "Discussion", 1, 1),
123 mtime => scalar(gmtime($mtime)),
126 return $template->output;
129 sub date_822 ($) { #{{{
133 return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
136 sub absolute_urls ($$) { #{{{
142 $content=~s{<a\s+href="([^"]+)"}{
145 $content=~s{<img\s+src="([^"]+)"}{
146 "<img src=\"$url$1\""
151 sub genrss ($$$) { #{{{
156 my $url="$config{url}/".htmlpage($page);
158 my $template=HTML::Template->new(blind_cache => 1,
159 filename => "$config{templatedir}/rsspage.tmpl");
161 # Regular page gets a feed that is updated every time the
162 # page is changed, so the mtime is encoded in the guid.
165 itemtitle => pagetitle(basename($page)),
166 itemguid => "$url?mtime=$mtime",
168 itempubdate => date_822($mtime),
169 itemcontent => absolute_urls($content, $url), # rss sucks
174 title => pagetitle(basename($page)),
179 return $template->output;
182 sub check_overwrite ($$) { #{{{
183 # Important security check. Make sure to call this before saving
184 # any files to the source directory.
188 if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
189 error("$dest already exists and was rendered from ".
190 join(" ",(grep { $renderedfiles{$_} eq $dest } keys
192 ", before, so not rendering from $src");
199 return (stat($page))[9];
202 sub findlinks ($$) { #{{{
207 while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
210 # Discussion links are a special case since they're not in the text
211 # of the page, but on its template.
212 return @links, "$page/discussion";
215 sub render ($) { #{{{
218 my $type=pagetype($file);
219 my $content=readfile("$config{srcdir}/$file");
220 if ($type ne 'unknown') {
221 my $page=pagename($file);
223 $links{$page}=[findlinks($content, $page)];
225 $content=linkify($content, $page);
226 $content=htmlize($type, $content);
228 check_overwrite("$config{destdir}/".htmlpage($page), $page);
229 writefile("$config{destdir}/".htmlpage($page),
230 genpage($content, $page, mtime("$config{srcdir}/$file")));
231 $oldpagemtime{$page}=time;
232 $renderedfiles{$page}=htmlpage($page);
234 # TODO: should really add this to renderedfiles and call
235 # check_overwrite, as above, but currently renderedfiles
236 # only supports listing one file per page.
238 writefile("$config{destdir}/".rsspage($page),
239 genrss($content, $page, mtime("$config{srcdir}/$file")));
244 check_overwrite("$config{destdir}/$file", $file);
245 writefile("$config{destdir}/$file", $content);
246 $oldpagemtime{$file}=time;
247 $renderedfiles{$file}=$file;
255 my $dir=dirname($file);
256 while (rmdir($dir)) {
261 sub refresh () { #{{{
262 # find existing pages
265 eval q{use File::Find};
269 if (/$config{wiki_file_prune_regexp}/) {
271 $File::Find::prune=1;
274 elsif (! -d $_ && ! -l $_) {
275 my ($f)=/$config{wiki_file_regexp}/; # untaint
277 warn("skipping bad filename $_\n");
280 $f=~s/^\Q$config{srcdir}\E\/?//;
282 $exists{pagename($f)}=1;
290 # check for added or removed pages
292 foreach my $file (@files) {
293 my $page=pagename($file);
294 if (! $oldpagemtime{$page}) {
295 debug("new page $page");
298 $pagesources{$page}=$file;
302 foreach my $page (keys %oldpagemtime) {
303 if (! $exists{$page}) {
304 debug("removing old page $page");
305 push @del, $pagesources{$page};
306 prune($config{destdir}."/".$renderedfiles{$page});
307 delete $renderedfiles{$page};
308 $oldpagemtime{$page}=0;
309 delete $pagesources{$page};
313 # render any updated files
314 foreach my $file (@files) {
315 my $page=pagename($file);
317 if (! exists $oldpagemtime{$page} ||
318 mtime("$config{srcdir}/$file") > $oldpagemtime{$page}) {
319 debug("rendering changed file $file");
325 # if any files were added or removed, check to see if each page
326 # needs an update due to linking to them
327 # TODO: inefficient; pages may get rendered above and again here;
328 # problem is the bestlink may have changed and we won't know until
331 FILE: foreach my $file (@files) {
332 my $page=pagename($file);
333 foreach my $f (@add, @del) {
335 foreach my $link (@{$links{$page}}) {
336 if (bestlink($page, $link) eq $p) {
337 debug("rendering $file, which links to $p");
347 # handle backlinks; if a page has added/removed links, update the
349 # TODO: inefficient; pages may get rendered above and again here;
350 # problem is the backlinks could be wrong in the first pass render
354 foreach my $file (keys %rendered, @del) {
355 my $page=pagename($file);
356 if (exists $links{$page}) {
357 foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
359 ! exists $oldlinks{$page} ||
360 ! grep { $_ eq $link } @{$oldlinks{$page}}) {
361 $linkchanged{$link}=1;
365 if (exists $oldlinks{$page}) {
366 foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
368 ! exists $links{$page} ||
369 ! grep { $_ eq $link } @{$links{$page}}) {
370 $linkchanged{$link}=1;
375 foreach my $link (keys %linkchanged) {
376 my $linkfile=$pagesources{$link};
377 if (defined $linkfile) {
378 debug("rendering $linkfile, to update its backlinks");