+sub change(@) { #{{{
+ my @rendered=@_;
+
+ my $updated_po_files=0;
+
+ # Refresh/create POT and PO files as needed.
+ foreach my $page (map pagename($_), @rendered) {
+ next unless istranslatable($page);
+ my $file=srcfile($pagesources{$page});
+ my $updated_pot_file=0;
+ # Only refresh Pot file if it does not exist, or if
+ # $pagesources{$page} was changed: don't if only the HTML was
+ # refreshed, e.g. because of a dependency.
+ if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
+ || ! -e potfile($file)) {
+ refreshpot($file);
+ $updated_pot_file=1;
+ }
+ my @pofiles;
+ map {
+ push @pofiles, $_ if ($updated_pot_file || ! -e $_);
+ } (pofiles($file));
+ if (@pofiles) {
+ refreshpofiles($file, @pofiles);
+ map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
+ $updated_po_files=1;
+ }
+ }
+
+ if ($updated_po_files) {
+ # Check staged changes in.
+ if ($config{rcs}) {
+ IkiWiki::disable_commit_hook();
+ IkiWiki::rcs_commit_staged(gettext("updated PO files"),
+ "IkiWiki::Plugin::po::change", "127.0.0.1");
+ IkiWiki::enable_commit_hook();
+ IkiWiki::rcs_update();
+ }
+ # Reinitialize module's private variables.
+ resetalreadyfiltered();
+ resettranslationscache();
+ # Trigger a wiki refresh.
+ require IkiWiki::Render;
+ IkiWiki::refresh();
+ IkiWiki::saveindex();
+ }
+} #}}}
+
+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
+# `----
+
+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;
+ }
+ }
+ return "";
+} #}}}
+
+sub mybeautify_urlpath ($) { #{{{
+ my $url=shift;
+
+ my $res=$origsubs{'beautify_urlpath'}->($url);
+ if ($config{po_link_to} eq "negotiated") {
+ $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
+ }
+ return $res;
+} #}}}
+
+sub mytargetpage ($$) { #{{{
+ my $page=shift;
+ my $ext=shift;
+
+ if (istranslation($page)) {
+ my ($masterpage, $lang) = (masterpage($page), lang($page));
+ if (! $config{usedirs} || $masterpage eq 'index') {
+ return $masterpage . "." . $lang . "." . $ext;
+ }
+ else {
+ 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);
+} #}}}
+
+sub myurlto ($$;$) { #{{{
+ my $to=shift;
+ my $from=shift;
+ my $absolute=shift;
+
+ # 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}");
+ }
+ return $origsubs{'urlto'}->($to,$from,$absolute);
+} #}}}
+
+
+# ,----
+# | Blackboxes for private data
+# `----
+
+{
+ my %filtered;
+
+ sub alreadyfiltered($$) { #{{{
+ my $page=shift;
+ my $destpage=shift;
+
+ return ( exists $filtered{$page}{$destpage}
+ && $filtered{$page}{$destpage} eq 1 );
+ } #}}}
+
+ sub setalreadyfiltered($$) { #{{{
+ my $page=shift;
+ my $destpage=shift;
+
+ $filtered{$page}{$destpage}=1;
+ } #}}}
+
+ sub unsetalreadyfiltered($$) { #{{{
+ my $page=shift;
+ my $destpage=shift;
+
+ if (exists $filtered{$page}{$destpage}) {
+ delete $filtered{$page}{$destpage};
+ }
+ } #}}}
+
+ sub resetalreadyfiltered() { #{{{
+ undef %filtered;
+ } #}}}
+}
+
+
+# ,----
+# | Helper functions
+# `----
+