+ # Re-run the preprocess hooks in scan mode, then the scan hooks,
+ # over the po-to-markup converted content
+ return if $run_by_po; # avoid looping endlessly
+ return unless istranslation($page);
+ $content = po_to_markup($page, $content);
+ require IkiWiki;
+ IkiWiki::preprocess($page, $page, $content, 1);
+ IkiWiki::run_hooks(scan => sub {
+ shift->(
+ page => $page,
+ content => $content,
+ run_by_po => 1,
+ );
+ });
+}
+
+# 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 => scalar 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_pages($page)});
+ }
+ if ($config{discussion} && istranslation($page)) {
+ if ($page !~ /.*\/\Q$config{discussionpage}\E$/i &&
+ (length $config{cgiurl} ||
+ exists $links{$masterpage."/".lc($config{discussionpage})})) {
+ $template->param('discussionlink' => htmllink(
+ $page,
+ $destpage,
+ $masterpage . '/' . $config{discussionpage},
+ noimageinline => 1,
+ forcesubpage => 0,
+ linktext => $config{discussionpage},
+ ));
+ }
+ }
+ # 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' => []);
+ }
+ if (ishomepage($page) && $template->query(name => "title")) {
+ $template->param(title => $config{wikiname});
+ }
+}
+
+# Add the renamed page translations to the list of to-be-renamed pages.
+sub renamepages (@) {
+ my %params = @_;
+
+ my %torename = %{$params{torename}};
+ my $session = $params{session};
+
+ # 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 the list of pages
+ # to rename is modified by other plugins.
+ my @orig_torename;
+ @orig_torename=@{$session->param("po_orig_torename")}
+ if defined $session->param("po_orig_torename");
+ push @orig_torename, $torename{src};
+ $session->param(po_orig_torename => \@orig_torename);
+ IkiWiki::cgi_savesession($session);
+
+ return () unless istranslatable($torename{src});
+
+ my @ret;
+ my %otherpages=%{otherlanguages_pages($torename{src})};
+ while (my ($lang, $otherpage) = each %otherpages) {
+ push @ret, {
+ src => $otherpage,
+ srcfile => $pagesources{$otherpage},
+ dest => otherlanguage_page($torename{dest}, $lang),
+ destfile => $torename{dest}.".".$lang.".po",
+ required => 0,
+ };
+ }
+ return @ret;
+}
+
+sub mydelete (@) {
+ my @deleted=@_;
+
+ map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
+}
+
+sub change (@) {
+ my @rendered=@_;
+
+ my $updated_po_files=0;
+
+ # Refresh/create POT and PO files as needed.
+ foreach my $file (grep {istranslatablefile($_)} @rendered) {
+ my $masterfile=srcfile($file);
+ my $page=pagename($file);
+ my $updated_pot_file=0;
+
+ # Avoid touching underlay files.
+ next if $masterfile ne "$config{srcdir}/$file";
+
+ # Only refresh POT file if it does not exist, or if
+ # the source 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;
+ foreach my $po (pofiles($masterfile)) {
+ next if ! $updated_pot_file && -e $po;
+ next if grep { $po=~/\Q$_\E/ } @{$config{underlaydirs}};
+ push @pofiles, $po;
+ }
+ if (@pofiles) {
+ refreshpofiles($masterfile, @pofiles);
+ map { s/^\Q$config{srcdir}\E\/*//; IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
+ $updated_po_files=1;
+ }
+ }
+