add dumpsetup option; refactor
[ikiwiki.git] / IkiWiki / Setup / Standard.pm
index 25f038a064ac8ceab4333f0d93ba13b1203262f2..dcb278bf48e2e7faae969cba7515e9ace98e149f 100644 (file)
@@ -3,74 +3,95 @@
 # Parameters to import should be all the standard ikiwiki config stuff,
 # plus an array of wrappers to set up.
 
+package IkiWiki::Setup::Standard;
+
 use warnings;
 use strict;
-use IkiWiki::Wrapper;
-use IkiWiki::Render;
 
-package IkiWiki::Setup::Standard;
+sub import { #{{{
+       $IkiWiki::Setup::raw_setup=$_[1];
+} #}}}
 
-sub import {
-       IkiWiki::setup_standard(@_);
-}
-       
-package IkiWiki;
+package IkiWiki::Setup;
 
-sub setup_standard {
-       my %setup=%{$_[1]};
+sub dumpline ($$$) { #{{{
+       my $key=shift;
+       my $value=shift;
+       my $prefix=shift;
+       
+       eval q{use Data::Dumper};
+       error($@) if $@;
+       local $Data::Dumper::Terse=1;
+       local $Data::Dumper::Indent=1;
+       local $Data::Dumper::Pad="\t";
+       local $Data::Dumper::Sortkeys=1;
+       local $Data::Dumper::Quotekeys=0;
+       
+       my $dumpedvalue=Dumper($value);
+       chomp $dumpedvalue;
+       $dumpedvalue=~s/^\t//;
+       
+       return "\t$prefix$key=$dumpedvalue,";
+} #}}}
 
-       $setup{plugin}=$config{plugin};
-       if (exists $setup{add_plugins}) {
-               push @{$setup{plugin}}, @{$setup{add_plugins}};
-               delete $setup{add_plugins};
-       }
-       if (exists $setup{disable_plugins}) {
-               foreach my $plugin (@{$setup{disable_plugins}}) {
-                       $setup{plugin}=[grep { $_ ne $plugin } @{$setup{plugin}}];
+sub dumpvalues ($@) { #{{{
+       my $setup=shift;
+       my @ret;
+       while (@_) {
+               my $key=shift;
+               my %info=%{shift()};
+               
+               push @ret, "\t# ".$info{description} if exists $info{description};
+               
+               if (exists $setup->{$key} && defined $setup->{$key}) {
+                       push @ret, dumpline($key, $setup->{$key}, "");
+                       delete $setup->{$key};
                }
-               delete $setup{disable_plugins};
-       }
-
-       debug("generating wrappers..");
-       my @wrappers=@{$setup{wrappers}};
-       delete $setup{wrappers};
-       my %startconfig=(%config);
-       foreach my $wrapper (@wrappers) {
-               %config=(%startconfig, verbose => 0, %setup, %{$wrapper});
-               checkconfig();
-               gen_wrapper();
-       }
-       %config=(%startconfig);
-       
-       foreach my $c (keys %setup) {
-               if (defined $setup{$c}) {
-                       if (! ref $setup{$c}) {
-                               $config{$c}=possibly_foolish_untaint($setup{$c});
-                       }
-                       elsif (ref $setup{$c} eq 'ARRAY') {
-                               $config{$c}=[map { possibly_foolish_untaint($_) } @{$setup{$c}}]
-                       }
+               elsif (exists $info{default}) {
+                       push @ret, dumpline($key, $info{default}, "#");
                }
-               else {
-                       $config{$c}=undef;
+               elsif (exists $info{example}) {
+                       push @ret, dumpline($key, $info{example}, "#");
                }
        }
+       return @ret;
+} #}}}
+
+sub dump ($) { #{{{
+       my $file=shift;
+       
+       my %setup=(%config);
+       my @ret;
 
-       if (! $config{refresh}) {
-               $config{rebuild}=1;
-               debug("rebuilding wiki..");
+       foreach my $id (sort keys %{$IkiWiki::hooks{getsetup}}) {
+               # use an array rather than a hash, to preserve order
+               my @s=$IkiWiki::hooks{getsetup}{$id}{call}->();
+               return unless @s;
+               push @ret, "\t# $id plugin";
+               push @ret, dumpvalues(\%setup, @s);
+               push @ret, "";
        }
-       else {
-               debug("refreshing wiki..");
+       
+       if (%setup) {
+               push @ret, "\t# other";
+               foreach my $key (sort keys %setup) {
+                       push @ret, dumpline($key, $setup{$key}, "");
+               }
        }
+       
+       unshift @ret, "#!/usr/bin/perl
+# Setup file for ikiwiki.
+# Passing this to ikiwiki --setup will make ikiwiki generate wrappers and
+# build the wiki.
+#
+# Remember to re-run ikiwiki --setup any time you edit this file.
 
-       checkconfig();
-       lockwiki();
-       loadindex();
-       refresh();
+use IkiWiki::Setup::Standard {";
+       push @ret, "}";
 
-       debug("done");
-       saveindex();
-}
+       open (OUT, ">", $file) || die "$file: $!";
+       print OUT "$_\n" foreach @ret;
+       close OUT;
+} #}}}
 
 1