config files now based on perl modules
[ikiwiki.git] / ikiwiki
diff --git a/ikiwiki b/ikiwiki
index de71414ede7e6a33d2d565bfaf0b31edebb589cc..dd417780096502b756e2d15b7026828edc413941 100755 (executable)
--- a/ikiwiki
+++ b/ikiwiki
@@ -3,7 +3,6 @@ $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
 
 use warnings;
 use strict;
-use File::Find;
 use Memoize;
 use File::Spec;
 use HTML::Template;
@@ -11,7 +10,8 @@ use Getopt::Long;
 
 my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
 
-my %config=( #{{{
+# Holds global config settings, also used by some modules.
+our %config=( #{{{
        wiki_file_prune_regexp => qr{((^|/).svn/|\.\.|^\.|\/\.|\.html?$)},
        wiki_link_regexp => qr/\[\[([^\s]+)\]\]/,
        wiki_file_regexp => qr/(^[-A-Za-z0-9_.:\/+]+$)/,
@@ -19,23 +19,27 @@ my %config=( #{{{
        wikiname => "wiki",
        default_pageext => ".mdwn",
        cgi => 0,
-       url => "",
-       cgiurl => "",
-       historyurl => "",
        svn => 1,
+       url => '',
+       cgiurl => '',
+       historyurl => '',
        anonok => 0,
        rebuild => 0,
-       wrapper => 0,
+       wrapper => undef,
+       wrappermode => undef,
        srcdir => undef,
        destdir => undef,
        templatedir => undef,
+       setup => undef,
 ); #}}}
 
 GetOptions( #{{{
+       "setup=s" => \$config{setup},
        "wikiname=s" => \$config{wikiname},
        "verbose|v!" => \$config{verbose},
        "rebuild!" => \$config{rebuild},
-       "wrapper!" => \$config{wrapper},
+       "wrapper=s" => sub { $config{wrapper}=$_[1] ? $_[1] : "ikiwiki-wrap" },
+       "wrappermode=i" => \$config{wrappermode},
        "svn!" => \$config{svn},
        "anonok!" => \$config{anonok},
        "cgi!" => \$config{cgi},
@@ -47,13 +51,16 @@ GetOptions( #{{{
        },
 ) || usage();
 
-usage() unless @ARGV == 3;
-$config{srcdir} = possibly_foolish_untaint(shift);
-$config{templatedir} = possibly_foolish_untaint(shift);
-$config{destdir} = possibly_foolish_untaint(shift);
-if ($config{cgi} && ! length $config{url}) {
-       error("Must specify url to wiki with --url when using --cgi");
-} #}}}
+if (! $config{setup}) {
+       usage() unless @ARGV == 3;
+       $config{srcdir} = possibly_foolish_untaint(shift);
+       $config{templatedir} = possibly_foolish_untaint(shift);
+       $config{destdir} = possibly_foolish_untaint(shift);
+       if ($config{cgi} && ! length $config{url}) {
+               error("Must specify url to wiki with --url when using --cgi");
+       }
+}
+#}}}
 
 sub usage { #{{{
        die "usage: ikiwiki [options] source templates dest\n";
@@ -63,11 +70,8 @@ sub error { #{{{
        if ($config{cgi}) {
                print "Content-type: text/html\n\n";
                print misctemplate("Error", "<p>Error: @_</p>");
-               exit 1;
-       }
-       else {
-               die @_;
        }
+       die @_;
 } #}}}
 
 sub debug ($) { #{{{
@@ -162,14 +166,17 @@ sub writefile ($$) { #{{{
        close OUT;
 } #}}}
 
-sub findlinks ($) { #{{{
+sub findlinks ($$) { #{{{
        my $content=shift;
+       my $page=shift;
 
        my @links;
-       while ($content =~ /$config{wiki_link_regexp}/g) {
+       while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
                push @links, lc($1);
        }
-       return @links;
+       # Discussion links are a special case since they're not in the text
+       # of the page, but on its template.
+       return @links, "$page/discussion";
 } #}}}
 
 sub bestlink ($$) { #{{{
@@ -207,9 +214,15 @@ sub htmllink { #{{{
        my $page=shift;
        my $link=shift;
        my $noimageinline=shift; # don't turn links into inline html images
-       my $createsubpage=shift; # force creation of a subpage if page DNE
+       my $forcesubpage=shift; # force a link to a subpage
 
-       my $bestlink=bestlink($page, $link);
+       my $bestlink;
+       if (! $forcesubpage) {
+               $bestlink=bestlink($page, $link);
+       }
+       else {
+               $bestlink="$page/".lc($link);
+       }
 
        return $link if length $bestlink && $page eq $bestlink;
        
@@ -221,12 +234,7 @@ sub htmllink { #{{{
                $bestlink=htmlpage($bestlink);
        }
        if (! grep { $_ eq $bestlink } values %renderedfiles) {
-               if (! $createsubpage) {
-                       return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
-               }
-               else {
-                       return "<a href=\"$config{cgiurl}?do=create&page=$page/$link\">?</a>$link"
-               }
+               return "<a href=\"$config{cgiurl}?do=create&page=$link&from=$page\">?</a>$link"
        }
        
        $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
@@ -241,7 +249,9 @@ sub linkify ($$) { #{{{
        my $content=shift;
        my $file=shift;
 
-       $content =~ s/$config{wiki_link_regexp}/htmllink(pagename($file), $1)/eg;
+       $content =~ s{(\\?)$config{wiki_link_regexp}}{
+               $1 ? "[[$2]]" : htmllink(pagename($file), $2)
+       }eg;
        
        return $content;
 } #}}}
@@ -251,7 +261,9 @@ sub htmlize ($$) { #{{{
        my $content=shift;
        
        if (! $INC{"/usr/bin/markdown"}) {
+               no warnings 'once';
                $blosxom::version="is a proper perl module too much to ask?";
+               use warnings 'all';
                do "/usr/bin/markdown";
        }
        
@@ -297,14 +309,14 @@ sub parentlinks ($) { #{{{
        my $skip=1;
        foreach my $dir (reverse split("/", $page)) {
                if (! $skip) {
+                       $path.="../";
                        unshift @ret, { url => "$path$dir.html", page => $dir };
                }
                else {
                        $skip=0;
                }
-               $path.="../";
        }
-       unshift @ret, { url => $path , page => $config{wikiname} };
+       unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
        return @ret;
 } #}}}
 
@@ -354,10 +366,10 @@ sub check_overwrite ($$) { #{{{
        my $src=shift;
        
        if (! exists $renderedfiles{$src} && -e $dest && ! $config{rebuild}) {
-               error("$dest exists and was rendered from ".
+               error("$dest already exists and was rendered from ".
                        join(" ",(grep { $renderedfiles{$_} eq $dest } keys
                                %renderedfiles)).
-                       ", not from $src before not overwriting");
+                       ", before, so not rendering from $src");
        }
 } #}}}
                
@@ -369,7 +381,7 @@ sub render ($) { #{{{
        if ($type ne 'unknown') {
                my $page=pagename($file);
                
-               $links{$page}=[findlinks($content)];
+               $links{$page}=[findlinks($content, $page)];
                
                $content=linkify($content, $file);
                $content=htmlize($type, $content);
@@ -535,11 +547,15 @@ sub refresh () { #{{{
        # Find existing pages.
        my %exists;
        my @files;
+       
+       eval q{use File::Find};
        find({
                no_chdir => 1,
                wanted => sub {
                        if (/$config{wiki_file_prune_regexp}/) {
+                               no warnings 'once';
                                $File::Find::prune=1;
+                               use warnings "all";
                        }
                        elsif (! -d $_) {
                                my ($f)=/$config{wiki_file_regexp}/; # untaint
@@ -624,8 +640,7 @@ FILE:               foreach my $file (@files) {
                foreach my $file (keys %rendered, @del) {
                        my $page=pagename($file);
                        if (exists $links{$page}) {
-                               foreach my $link (@{$links{$page}}) {
-                                       $link=bestlink($page, $link);
+                               foreach my $link (map { bestlink($page, $_) } @{$links{$page}}) {
                                        if (length $link &&
                                            ! exists $oldlinks{$page} ||
                                            ! grep { $_ eq $link } @{$oldlinks{$page}}) {
@@ -634,8 +649,7 @@ FILE:               foreach my $file (@files) {
                                }
                        }
                        if (exists $oldlinks{$page}) {
-                               foreach my $link (@{$oldlinks{$page}}) {
-                                       $link=bestlink($page, $link);
+                               foreach my $link (map { bestlink($page, $_) } @{$oldlinks{$page}}) {
                                        if (length $link &&
                                            ! exists $links{$page} ||
                                            ! grep { $_ eq $link } @{$links{$page}}) {
@@ -654,7 +668,8 @@ FILE:               foreach my $file (@files) {
        }
 } #}}}
 
-sub gen_wrapper () { #{{{
+sub gen_wrapper (@) { #{{{
+       my %config=(@_);
        eval q{use Cwd 'abs_path'};
        $config{srcdir}=abs_path($config{srcdir});
        $config{destdir}=abs_path($config{destdir});
@@ -663,6 +678,10 @@ sub gen_wrapper () { #{{{
                error("$this doesn't seem to be executable");
        }
 
+       if ($config{setup}) {
+               error("cannot create a wrapper that uses a setup file");
+       }
+       
        my @params=($config{srcdir}, $config{templatedir}, $config{destdir},
                "--wikiname=$config{wikiname}");
        push @params, "--verbose" if $config{verbose};
@@ -673,7 +692,7 @@ sub gen_wrapper () { #{{{
        push @params, "--cgiurl=$config{cgiurl}" if length $config{cgiurl};
        push @params, "--historyurl=$config{historyurl}" if length $config{historyurl};
        push @params, "--anonok" if $config{anonok};
-       my $params=join(" ", map { "\'$_\'" } @params);
+       my $params=join(" ", @params);
        my $call='';
        foreach my $p ($this, $this, @params) {
                $call.=qq{"$p", };
@@ -724,12 +743,15 @@ $envsave
 }
 EOF
        close OUT;
-       if (system("gcc", "ikiwiki-wrap.c", "-o", "ikiwiki-wrap") != 0) {
+       if (system("gcc", "ikiwiki-wrap.c", "-o", possibly_foolish_untaint($config{wrapper})) != 0) {
                error("failed to compile ikiwiki-wrap.c");
        }
        unlink("ikiwiki-wrap.c");
-       print "successfully generated ikiwiki-wrap\n";
-       exit 0;
+       if (defined $config{wrappermode} &&
+           ! chmod(oct($config{wrappermode}), possibly_foolish_untaint($config{wrapper}))) {
+               error("chmod $config{wrapper}: $!");
+       }
+       print "successfully generated $config{wrapper}\n";
 } #}}}
                
 sub misctemplate ($$) { #{{{
@@ -1000,26 +1022,31 @@ sub cgi_editpage ($$) { #{{{
                        }
                        
                        my @page_locs;
+                       my $best_loc;
                        my ($from)=$form->param('from')=~/$config{wiki_file_regexp}/;
                        if (! defined $from || ! length $from ||
                            $from ne $form->param('from') ||
                            $from=~/$config{wiki_file_prune_regexp}/ || $from=~/^\//) {
-                               @page_locs=$page;
+                               @page_locs=$best_loc=$page;
                        }
                        else {
                                my $dir=$from."/";
                                $dir=~s![^/]+/$!!;
                                push @page_locs, $dir.$page;
                                push @page_locs, "$from/$page";
+                               $best_loc="$from/$page";
                                while (length $dir) {
                                        $dir=~s![^/]+/$!!;
                                        push @page_locs, $dir.$page;
                                }
+
+                               @page_locs = grep { ! exists
+                                       $pagesources{lc($_)} } @page_locs;
                        }
 
                        $form->tmpl_param("page_select", 1);
                        $form->field(name => "page", type => 'select',
-                               options => \@page_locs);
+                               options => \@page_locs, value => $best_loc);
                        $form->title("creating $page");
                }
                elsif ($form->field("do") eq "edit") {
@@ -1074,7 +1101,9 @@ sub cgi_editpage ($$) { #{{{
                        rcs_commit($message);
                }
                else {
+                       loadindex();
                        refresh();
+                       saveindex();
                }
                
                # The trailing question mark tries to avoid broken
@@ -1128,8 +1157,26 @@ sub cgi () { #{{{
        }
 } #}}}
 
+sub setup () { # {{{
+       my $setup=possibly_foolish_untaint($config{setup});
+       delete $config{setup};
+       open (IN, $setup) || error("read $setup: $!\n");
+       local $/=undef;
+       my $code=<IN>;
+       ($code)=$code=~/(.*)/s;
+       close IN;
+
+       eval $code;
+       error($@) if $@;
+       exit;
+} #}}}
+
 # main {{{
-gen_wrapper() if $config{wrapper};
+setup() if $config{setup};
+if ($config{wrapper}) {
+       gen_wrapper(%config);
+       exit;
+}
 memoize('pagename');
 memoize('bestlink');
 loadindex() unless $config{rebuild};