+# We use filter to convert PO to the master page's format,
+# since the rest of ikiwiki should not work on PO files.
+sub filter (@) {
+ my %params = @_;
+
+ my $page = $params{page};
+ my $destpage = $params{destpage};
+ my $content = $params{content};
+ if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
+ $content = po_to_markup($page, $content);
+ setalreadyfiltered($page, $destpage);
+ }
+ return $content;
+}
+
+sub htmlize (@) {
+ my %params=@_;
+
+ my $page = $params{page};
+ my $content = $params{content};
+
+ # 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(srcfile($pagesources{masterpage($page)})),
+ $content);
+}
+
+sub pagetemplate (@) {
+ my %params=@_;
+ my $page=$params{page};
+ my $destpage=$params{destpage};
+ my $template=$params{template};
+
+ my ($masterpage, $lang) = istranslation($page);
+
+ if (istranslation($page) && $template->query(name => "percenttranslated")) {
+ $template->param(percenttranslated => percenttranslated($page));
+ }
+ if ($template->query(name => "istranslation")) {
+ $template->param(istranslation => istranslation($page));
+ }
+ 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)});
+ }
+ # Rely on IkiWiki::Render's genpage() to decide wether
+ # a discussion link should appear on $page; this is not
+ # totally accurate, though: some broken links may be generated
+ # when cgiurl is disabled.
+ # This compromise avoids some code duplication, and will probably
+ # prevent future breakage when ikiwiki internals change.
+ # Known limitations are preferred to future random bugs.
+ if ($template->param('discussionlink') && istranslation($page)) {
+ $template->param('discussionlink' => htmllink(
+ $page,
+ $destpage,
+ $masterpage . '/' . gettext("Discussion"),
+ noimageinline => 1,
+ forcesubpage => 0,
+ linktext => gettext("Discussion"),
+ ));
+ }
+ # Remove broken parentlink to ./index.html on home page's translations.
+ # It works because this hook has the "last" parameter set, to ensure it
+ # runs after parentlinks' own pagetemplate hook.
+ if ($template->param('parentlinks')
+ && istranslation($page)
+ && $masterpage eq "index") {
+ $template->param('parentlinks' => []);
+ }
+} # }}}
+
+sub postscan (@) {
+ my %params = @_;
+ my $page = $params{page};
+
+ # backlinks involve back-dependencies, so that nicepagetitle effects,
+ # such as translation status displayed in links, are updated
+ use IkiWiki::Render;
+ map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
+}
+
+# Add the renamed page translations to the list of to-be-renamed pages.
+sub renamepages($$$) {
+ my ($torename, $cgi, $session) = (shift, shift, shift);
+
+ # copy the initial array, so that we can iterate on it AND
+ # modify it at the same time, without iterating on the items we
+ # pushed on it ourselves
+ my @torename=@{$torename};
+
+ # Save the page(s) the user asked to rename, so that our
+ # canrename hook can tell the difference between:
+ # - a translation being renamed as a consequence of its master page
+ # being renamed
+ # - a user trying to directly rename a translation
+ # This is why this hook has to be run first, before @torename is modified
+ # by other plugins.
+ $session->param(po_orig_torename => [ @torename ]);
+ IkiWiki::cgi_savesession($session);
+
+ foreach my $rename (@torename) {
+ next unless istranslatable($rename->{src});
+ my %otherpages=%{otherlanguages($rename->{src})};
+ while (my ($lang, $otherpage) = each %otherpages) {
+ push @{$torename}, {
+ src => $otherpage,
+ srcfile => $pagesources{$otherpage},
+ dest => otherlanguage($rename->{dest}, $lang),
+ destfile => $rename->{dest}.".".$lang.".po",
+ required => 0,
+ };
+ }
+ }
+}
+
+sub mydelete(@) {
+ my @deleted=@_;
+
+ map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
+}
+
+sub change(@) {
+ my @rendered=@_;
+
+ # All meta titles are first extracted at scan time, i.e. before we turn
+ # PO files back into translated markdown; escaping of double-quotes in
+ # PO files breaks the meta plugin's parsing enough to save ugly titles
+ # to %pagestate at this time.
+ #
+ # Then, at render time, every page's passes on row through the Great
+ # Rendering Chain (filter->preprocess->linkify->htmlize), and the meta
+ # plugin's preprocess hook is this time in a position to correctly
+ # extract the titles from slave pages.
+ #
+ # This is, unfortunately, too late: if the page A, linking to the page B,
+ # is rendered before B, it will display the wrongly-extracted meta title
+ # as the link text to B.
+ #
+ # On the one hand, such a corner case only happens on rebuild: on
+ # refresh, every rendered page is fixed to contain correct meta titles.
+ # On the other hand, it can take some time to get every page fixed.
+ # We therefore re-render every rendered page after a rebuild to fix them
+ # at once. As this more or less doubles the time needed to rebuild the
+ # wiki, we do so only when really needed.