+ 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.
+
+ if (@rendered
+ && exists $config{rebuild} && defined $config{rebuild} && $config{rebuild}
+ && UNIVERSAL::can("IkiWiki::Plugin::meta", "getsetup")
+ && exists $config{meta_overrides_page_title}
+ && defined $config{meta_overrides_page_title}
+ && $config{meta_overrides_page_title}) {
+ debug(sprintf(gettext("re-rendering all pages to fix meta titles")));
+ resetalreadyfiltered();
+ require IkiWiki::Render;
+ foreach my $file (@rendered) {
+ debug(sprintf(gettext("rendering %s"), $file));
+ IkiWiki::render($file);
+ }
+ }
+
+ my $updated_po_files=0;
+
+ # Refresh/create POT and PO files as needed.
+ foreach my $file (grep {istranslatablefile($_)} @rendered) {
+ my $page=pagename($file);
+ my $masterfile=srcfile($file);
+ 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($masterfile)) {
+ refreshpot($masterfile);
+ $updated_pot_file=1;
+ }
+ my @pofiles;
+ map {
+ push @pofiles, $_ if ($updated_pot_file || ! -e $_);
+ } (pofiles($masterfile));
+ if (@pofiles) {
+ refreshpofiles($masterfile, @pofiles);
+ map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
+ $updated_po_files=1;
+ }
+ }
+
+ if ($updated_po_files) {
+ commit_and_refresh(
+ gettext("updated PO files"),
+ "IkiWiki::Plugin::po::change");
+ }
+}
+
+sub checkcontent (@) {
+ my %params=@_;
+
+ if (istranslation($params{page})) {
+ my $res = isvalidpo($params{content});
+ if ($res) {
+ return undef;
+ }
+ else {
+ return "$res";
+ }
+ }
+ return undef;
+}
+
+sub canremove ($$$) {
+ my ($page, $cgi, $session) = (shift, shift, shift);
+
+ if (istranslation($page)) {
+ return gettext("Can not remove a translation. Removing the master page, ".
+ "though, removes its translations as well.");
+ }
+ return undef;
+}
+
+sub canrename ($$@) {
+ my ($cgi, $session) = (shift, shift);
+ my %params = @_;
+
+ if (istranslation($params{src})) {
+ my $masterpage = masterpage($params{src});
+ # Tell the difference between:
+ # - a translation being renamed as a consequence of its master page
+ # being renamed, which is allowed
+ # - a user trying to directly rename a translation, which is forbidden
+ # by looking for the master page in the list of to-be-renamed pages we
+ # saved early in the renaming process.
+ my $orig_torename = $session->param("po_orig_torename");
+ unless (grep { $_->{src} eq $masterpage } @{$orig_torename}) {
+ return gettext("Can not rename a translation. Renaming the master page, ".
+ "though, renames its translations as well.");
+ }
+ }
+ return undef;
+}
+
+# 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=@_;
+
+ unsetalreadyfiltered($params{page}, $params{page});
+ return $params{content};
+}
+
+sub formbuilder_setup (@) {
+ my %params=@_;
+ my $form=$params{form};
+ my $q=$params{cgi};
+
+ return unless defined $form->field("do");
+
+ if ($form->field("do") eq "create") {
+ # Warn the user: new pages must be written in master language.
+ my $template=template("pocreatepage.tmpl");
+ $template->param(LANG => $config{po_master_language}{name});
+ $form->tmpl_param(message => $template->output);
+ }
+ elsif ($form->field("do") eq "edit") {
+ # Remove the rename/remove buttons on slave pages.
+ # This has to be done after the rename/remove plugins have added
+ # their buttons, which is why this hook must be run last.
+ # The canrename/canremove hooks already ensure this is forbidden
+ # at the backend level, so this is only UI sugar.
+ if (istranslation($form->field("page"))) {
+ map {
+ for (my $i = 0; $i < @{$params{buttons}}; $i++) {
+ if (@{$params{buttons}}[$i] eq $_) {
+ delete @{$params{buttons}}[$i];
+ last;
+ }
+ }
+ } qw(Rename Remove);
+ }
+ }
+}
+
+sub formbuilder (@) {
+ my %params=@_;
+ my $form=$params{form};
+ my $q=$params{cgi};
+
+ return unless defined $form->field("do");
+
+ # Do not allow to create pages of type po: they are automatically created.
+ # The main reason to do so is to bypass the "favor the type of linking page
+ # on page creation" logic, which is unsuitable when a broken link is clicked
+ # on a slave (PO) page.
+ # This cannot be done in the formbuilder_setup hook as the list of types is
+ # computed later.
+ 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;
+ }
+ else {
+ # make sure the default value is not po;
+ # does this case actually happen?
+ debug "po(formbuilder) ".gettext("type field is not selected - not implemented yet");
+ }
+ }
+ }
+}
+
+# ,----
+# | Injected functions
+# `----
+
+# Implement po_link_to 'current' and 'negotiated' settings.
+sub mybestlink ($$) {
+ my $page=shift;
+ my $link=shift;
+
+ 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 $res;
+}
+
+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$!/!;
+ $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
+ map {
+ $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
+ } (keys %{$config{po_slave_languages}});
+ }
+ return $res;
+}
+
+sub mytargetpage ($$) {
+ my $page=shift;
+ my $ext=shift;
+
+ if (istranslation($page) || istranslatable($page)) {
+ my ($masterpage, $lang) = (masterpage($page), lang($page));
+ if (! $config{usedirs} || $masterpage eq 'index') {