]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/po.pm
po(htmlize): ignore PO files not managed by this plugin
[ikiwiki.git] / IkiWiki / Plugin / po.pm
index 7deddf9abad71cd053dbb7e3887ccda9d556885d..1212181ca39b5747cbc9ac635e66f1af6ea885c8 100644 (file)
@@ -262,10 +262,14 @@ sub htmlize (@) { #{{{
 
        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 (@) { #{{{
@@ -285,6 +289,9 @@ 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)});
@@ -359,15 +366,20 @@ sub change(@) { #{{{
                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};
 } #}}}
@@ -377,22 +389,19 @@ sub editcontent () { #{{{
 # | 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 ($) { #{{{
@@ -401,6 +410,10 @@ 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;
 } #}}}
@@ -409,7 +422,7 @@ sub mytargetpage ($$) { #{{{
        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;
@@ -418,14 +431,6 @@ sub mytargetpage ($$) { #{{{
                        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);
 } #}}}
 
@@ -437,7 +442,6 @@ sub myurlto ($$;$) { #{{{
        # 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}");
        }
@@ -486,51 +490,54 @@ sub myurlto ($$;$) { #{{{
 # | 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;
 } #}}}
@@ -555,6 +562,7 @@ sub lang ($) { #{{{
 
 sub islanguagecode ($) { #{{{
        my $code=shift;
+
        return ($code =~ /^[a-z]{2}$/);
 } #}}}
 
@@ -563,7 +571,7 @@ sub otherlanguages($) { #{{{
 
        my %ret;
        if (istranslatable($page)) {
-               %ret = %{$translations{$page}};
+               %ret = %{$translations{$page}} if defined $translations{$page};
        }
        elsif (istranslation($page)) {
                my $masterpage = masterpage($page);
@@ -595,6 +603,7 @@ sub pofile ($$) { #{{{
 
 sub pofiles ($) { #{{{
        my $masterfile=shift;
+
        return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
 } #}}}
 
@@ -709,7 +718,7 @@ sub otherlanguagesloop ($) { #{{{
                }
                else {
                        push @ret, {
-                               url => urlto($otherpage, $page),
+                               url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
                                code => $lang,
                                language => languagename($lang),
                                percent => percenttranslated($otherpage),
@@ -723,6 +732,11 @@ sub otherlanguagesloop ($) { #{{{
                } @ret;
 } #}}}
 
+sub homepageurl (;$) { #{{{
+       my $page=shift;
+
+       return urlto('', $page);
+} #}}}
 
 # ,----
 # | PageSpec's