avoid linkifying escaped wikilinks
[ikiwiki.git] / ikiwiki
diff --git a/ikiwiki b/ikiwiki
index 729f8628b4d6bcd61ae3da2a99cf66c97f7f8fc0..111ef54fce0a5c19d3f1ec68da9f2894269eadaf 100755 (executable)
--- a/ikiwiki
+++ b/ikiwiki
@@ -3,15 +3,10 @@ $ENV{PATH}="/usr/local/bin:/usr/bin:/bin";
 
 use warnings;
 use strict;
-use File::Find;
 use Memoize;
 use File::Spec;
 use HTML::Template;
 use Getopt::Long;
-BEGIN {
-       $blosxom::version="is a proper perl module too much to ask?";
-       do "/usr/bin/markdown";
-}
 
 my (%links, %oldlinks, %oldpagemtime, %renderedfiles, %pagesources);
 
@@ -23,23 +18,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},
@@ -51,13 +50,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";
@@ -170,7 +172,7 @@ sub findlinks ($) { #{{{
        my $content=shift;
 
        my @links;
-       while ($content =~ /$config{wiki_link_regexp}/g) {
+       while ($content =~ /(?<!\\)$config{wiki_link_regexp}/g) {
                push @links, lc($1);
        }
        return @links;
@@ -245,7 +247,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;
 } #}}}
@@ -254,6 +258,13 @@ sub htmlize ($$) { #{{{
        my $type=shift;
        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";
+       }
+       
        if ($type eq '.mdwn') {
                return Markdown::Markdown($content);
        }
@@ -534,11 +545,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
@@ -653,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});
@@ -662,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};
@@ -723,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 ($$) { #{{{
@@ -1127,8 +1150,25 @@ sub cgi () { #{{{
        }
 } #}}}
 
+sub setup () { # {{{
+       my $setup=possibly_foolish_untaint($config{setup});
+       open (IN, $setup) || error("read $setup: $!\n");
+       local $/=undef;
+       my $code=<IN>;
+       ($code)=$code=~/(.*)/s;
+       close IN;
+       eval $code;
+       error($@) if $@;
+       print "ikiwiki setup complete\n";
+       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};