config files now based on perl modules
[ikiwiki.git] / ikiwiki
diff --git a/ikiwiki b/ikiwiki
index 53a86c1edc3ef5f3d4498fa2a55d559e8887df3a..dd417780096502b756e2d15b7026828edc413941 100755 (executable)
--- a/ikiwiki
+++ b/ikiwiki
@@ -10,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_.:\/+]+$)/,
@@ -165,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) {
                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 ($$) { #{{{
@@ -377,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);
@@ -636,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}}) {
@@ -646,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}}) {
@@ -1020,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") {
@@ -1159,21 +1166,8 @@ sub setup () { # {{{
        ($code)=$code=~/(.*)/s;
        close IN;
 
-       my (%setup);
        eval $code;
        error($@) if $@;
-       
-       gen_wrapper(%config, %setup, %{$setup{cgiwrapper}}) if $setup{cgiwrapper};
-       gen_wrapper(%config, %setup, %{$setup{svnwrapper}}) if $setup{svnwrapper};
-       
-       print "$setup{wikiname} setup complete, now forcing a rebuild\n";
-       foreach my $c (keys %setup) {
-               $config{$c}=possibly_foolish_untaint($setup{$c})
-                       if defined $setup{$c} && ! ref $setup{$c};
-       }
-       $config{rebuild}=1;
-       refresh();
-       saveindex();
        exit;
 } #}}}