X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/cdc3576c8d1efb2593cac2d9da3f2393a2afe26e..bcad00b0467c9435636dff3feb540d17fb2296d4:/IkiWiki/Plugin/po.pm

diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm
index 12f41f6de..5d0d9e79d 100644
--- a/IkiWiki/Plugin/po.pm
+++ b/IkiWiki/Plugin/po.pm
@@ -51,6 +51,8 @@ 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;
@@ -59,6 +61,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);
 }
 
 
@@ -151,10 +155,6 @@ 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') {
-		$origsubs{'bestlink'}=\&IkiWiki::bestlink;
-		inject(name => "IkiWiki::bestlink", call => \&mybestlink);
-	}
 
 	push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
 
@@ -531,10 +531,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) ;
 			}
 		}
 	}
@@ -545,15 +558,20 @@ sub formbuilder (@) {
 # `----
 
 # Implement po_link_to 'current' and 'negotiated' settings.
-# Not injected otherwise.
 sub mybestlink ($$) {
 	my $page=shift;
 	my $link=shift;
 
+	return $origsubs{'bestlink'}->($page, $link)
+		if $config{po_link_to} eq "default";
+
 	my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
+	my @caller = caller(1);
 	if (length $res
 	    && 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;
@@ -636,6 +654,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
 # `----