my $page = $params{page};
my $content = $params{content};
- my $masterfile = srcfile($pagesources{masterpage($page)});
+
+ # ignore PO files this plugin did not create
+ return $content unless istranslation($page);
# force content to be htmlize'd as if it was the same type as the master page
- return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
+ return IkiWiki::htmlize($page, $page,
+ pagetype(srcfile($pagesources{masterpage($page)})),
+ $content);
} #}}}
sub pagetemplate (@) { #{{{
if ($template->query(name => "istranslatable")) {
$template->param(istranslatable => istranslatable($page));
}
+ if ($template->query(name => "HOMEPAGEURL")) {
+ $template->param(homepageurl => homepageurl($page));
+ }
if ($template->query(name => "otherlanguages")) {
$template->param(otherlanguages => [otherlanguagesloop($page)]);
map add_depends($page, $_), (values %{otherlanguages($page)});
resettranslationscache();
# Trigger a wiki refresh.
require IkiWiki::Render;
+ # without preliminary saveindex/loadindex, refresh()
+ # complains about a lot of uninitialized variables
+ IkiWiki::saveindex();
+ IkiWiki::loadindex();
IkiWiki::refresh();
IkiWiki::saveindex();
}
} #}}}
+# As we're previewing or saving a page, the content may have
+# changed, so tell the next filter() invocation it must not be lazy.
sub editcontent () { #{{{
my %params=@_;
- # as we're previewing or saving a page, the content may have
- # changed, so tell the next filter() invocation it must not be lazy
+
unsetalreadyfiltered($params{page}, $params{page});
return $params{content};
} #}}}
# | Injected functions
# `----
+# Implement po_link_to 'current' and 'negotiated' settings.
sub mybestlink ($$) { #{{{
my $page=shift;
my $link=shift;
- my $res=$origsubs{'bestlink'}->($page, $link);
- if (length $res) {
- if ($config{po_link_to} eq "current"
- && istranslatable($res)
- && istranslation($page)) {
- return $res . "." . lang($page);
- }
- else {
- return $res;
- }
+ my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
+ if (length $res
+ && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
+ && istranslatable($res)
+ && istranslation($page)) {
+ return $res . "." . lang($page);
}
- return "";
+ return $res;
} #}}}
sub mybeautify_urlpath ($) { #{{{
my $res=$origsubs{'beautify_urlpath'}->($url);
if ($config{po_link_to} eq "negotiated") {
$res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
+ $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
+ map {
+ $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
+ } (keys %{$config{po_slave_languages}});
}
return $res;
} #}}}
my $page=shift;
my $ext=shift;
- if (istranslation($page)) {
+ if (istranslation($page) || istranslatable($page)) {
my ($masterpage, $lang) = (masterpage($page), lang($page));
if (! $config{usedirs} || $masterpage eq 'index') {
return $masterpage . "." . $lang . "." . $ext;
return $masterpage . "/index." . $lang . "." . $ext;
}
}
- elsif (istranslatable($page)) {
- if (! $config{usedirs} || $page eq 'index') {
- return $page . "." . $config{po_master_language}{code} . "." . $ext;
- }
- else {
- return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
- }
- }
return $origsubs{'targetpage'}->($page, $ext);
} #}}}
# workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
if (! length $to
&& $config{po_link_to} eq "current"
- && istranslation($from)
&& istranslatable('index')) {
return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
}
# | Helper functions
# `----
+sub maybe_add_leading_slash ($;$) { #{{{
+ my $str=shift;
+ my $add=shift;
+ $add=1 unless defined $add;
+ return '/' . $str if $add;
+ return $str;
+} #}}}
+
sub istranslatable ($) { #{{{
my $page=shift;
+ $page=~s#^/##;
my $file=$pagesources{$page};
- if (! defined $file
- || (defined pagetype($file) && pagetype($file) eq 'po')
- || $file =~ /\.pot$/) {
- return 0;
- }
+ return 0 unless defined $file;
+ return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
+ return 0 if $file =~ /\.pot$/;
return pagespec_match($page, $config{po_translatable_pages});
} #}}}
sub _istranslation ($) { #{{{
my $page=shift;
+ my $hasleadingslash = ($page=~s#^/##);
my $file=$pagesources{$page};
- if (! defined $file) {
- return IkiWiki::FailReason->new("no file specified");
- }
-
- if (! defined $file
- || ! defined pagetype($file)
- || ! pagetype($file) eq 'po'
- || $file =~ /\.pot$/) {
- return 0;
- }
+ return 0 unless (defined $file
+ && defined pagetype($file)
+ && pagetype($file) eq 'po');
+ return 0 if $file =~ /\.pot$/;
my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
- if (! defined $masterpage || ! defined $lang
- || ! (length($masterpage) > 0) || ! (length($lang) > 0)
- || ! defined $pagesources{$masterpage}
- || ! defined $config{po_slave_languages}{$lang}) {
- return 0;
- }
+ return 0 unless (defined $masterpage && defined $lang
+ && length $masterpage && length $lang
+ && defined $pagesources{$masterpage}
+ && defined $config{po_slave_languages}{$lang});
- return ($masterpage, $lang) if istranslatable($masterpage);
+ return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
+ if istranslatable($masterpage);
} #}}}
sub istranslation ($) { #{{{
my $page=shift;
if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
+ my $hasleadingslash = ($masterpage=~s#^/##);
$translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
- return ($masterpage, $lang);
+ return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
+ if istranslatable($masterpage);
}
return;
} #}}}
sub islanguagecode ($) { #{{{
my $code=shift;
+
return ($code =~ /^[a-z]{2}$/);
} #}}}
my %ret;
if (istranslatable($page)) {
- %ret = %{$translations{$page}};
+ %ret = %{$translations{$page}} if defined $translations{$page};
}
elsif (istranslation($page)) {
my $masterpage = masterpage($page);
sub pofiles ($) { #{{{
my $masterfile=shift;
+
return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
} #}}}
}
else {
push @ret, {
- url => urlto($otherpage, $page),
+ url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
code => $lang,
language => languagename($lang),
percent => percenttranslated($otherpage),
} @ret;
} #}}}
+sub homepageurl (;$) { #{{{
+ my $page=shift;
+
+ return urlto('', $page);
+} #}}}
# ,----
# | PageSpec's