]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/po.pm
Merge commit 'intrigeri/po'
[ikiwiki.git] / IkiWiki / Plugin / po.pm
index 1b313a97931eb23df15bb3a4a5d169b9ba5d432f..792d84261855ccd074421717207d0276cadc1928 100644 (file)
@@ -51,8 +51,6 @@ sub import {
        hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup, last => 1);
        hook(type => "formbuilder", id => "po", call => \&formbuilder);
 
-       $origsubs{'bestlink'}=\&IkiWiki::bestlink;
-       inject(name => "IkiWiki::bestlink", call => \&mybestlink);
        $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
        inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
        $origsubs{'targetpage'}=\&IkiWiki::targetpage;
@@ -61,6 +59,8 @@ sub import {
        inject(name => "IkiWiki::urlto", call => \&myurlto);
        $origsubs{'cgiurl'}=\&IkiWiki::cgiurl;
        inject(name => "IkiWiki::cgiurl", call => \&mycgiurl);
+       $origsubs{'rootpage'}=\&IkiWiki::rootpage;
+       inject(name => "IkiWiki::rootpage", call => \&myrootpage);
 }
 
 
@@ -153,6 +153,12 @@ sub checkconfig () {
                warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
                $config{po_link_to}='default';
        }
+       unless ($config{po_link_to} eq 'default') {
+               if (! exists $origsubs{'bestlink'}) {
+                       $origsubs{'bestlink'}=\&IkiWiki::bestlink;
+                       inject(name => "IkiWiki::bestlink", call => \&mybestlink);
+               }
+       }
 
        push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
 
@@ -529,10 +535,23 @@ sub formbuilder (@) {
        if ($form->field("do") eq "create") {
                foreach my $field ($form->field) {
                        next unless "$field" eq "type";
-                       if ($field->type eq 'select') {
-                               # remove po from the list of types
-                               my @types = grep { $_ ne 'po' } $field->options;
-                               $field->options(\@types) if @types;
+                       next unless $field->type eq 'select';
+                       my $orig_value = $field->value;
+                       # remove po from the list of types
+                       my @types = grep { $_->[0] ne 'po' } $field->options;
+                       $field->options(\@types) if @types;
+                       # favor the type of linking page's masterpage
+                       if ($orig_value eq 'po') {
+                               my ($from, $type);
+                               if (defined $form->field('from')) {
+                                       ($from)=$form->field('from')=~/$config{wiki_file_regexp}/;
+                                       $from = masterpage($from);
+                               }
+                               if (defined $from && exists $pagesources{$from}) {
+                                       $type=pagetype($pagesources{$from});
+                               }
+                               $type=$config{default_pageext} unless defined $type;
+                               $field->value($type) ;
                        }
                }
        }
@@ -543,15 +562,18 @@ sub formbuilder (@) {
 # `----
 
 # Implement po_link_to 'current' and 'negotiated' settings.
+# Not injected otherwise.
 sub mybestlink ($$) {
        my $page=shift;
        my $link=shift;
 
        my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
+       my @caller = caller(1);
        if (length $res
-           && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
            && istranslatable($res)
-           && istranslation($page)) {
+           && istranslation($page)
+           &&  !(exists $caller[3] && defined $caller[3]
+                 && ($caller[3] eq "IkiWiki::PageSpec::match_link"))) {
                return $res . "." . lang($page);
        }
        return $res;
@@ -634,6 +656,22 @@ sub mycgiurl (@) {
        return $origsubs{'cgiurl'}->(%params);
 }
 
+sub myrootpage (@) {
+       my %params=@_;
+
+       my $rootpage;
+       if (exists $params{rootpage}) {
+               $rootpage=$origsubs{'bestlink'}->($params{page}, $params{rootpage});
+               if (!length $rootpage) {
+                       $rootpage=$params{rootpage};
+               }
+       }
+       else {
+               $rootpage=masterpage($params{page});
+       }
+       return $rootpage;
+}
+
 # ,----
 # | Blackboxes for private data
 # `----