+ po_translatable_pages => {
+ type => "pagespec",
+ example => "* and !*/Discussion",
+ description => "PageSpec controlling which pages are translatable",
+ link => "ikiwiki/PageSpec",
+ safe => 1,
+ rebuild => 1,
+ },
+ po_link_to => {
+ type => "string",
+ example => "current",
+ description => "internal linking behavior (default/current/negotiated)",
+ safe => 1,
+ rebuild => 1,
+ },
+}
+
+sub checkconfig () {
+ foreach my $field (qw{po_master_language}) {
+ if (! exists $config{$field} || ! defined $config{$field}) {
+ error(sprintf(gettext("Must specify %s when using the %s plugin"),
+ $field, 'po'));
+ }
+ }
+
+ map {
+ islanguagecode($_)
+ or error(sprintf(gettext("%s is not a valid language code"), $_));
+ } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
+
+ if (! exists $config{po_translatable_pages} ||
+ ! defined $config{po_translatable_pages}) {
+ $config{po_translatable_pages}="";
+ }
+ if (! exists $config{po_link_to} ||
+ ! defined $config{po_link_to}) {
+ $config{po_link_to}='default';
+ }
+ elsif ($config{po_link_to} !~ /^(default|current|negotiated)$/) {
+ warn(sprintf(gettext('%s is not a valid value for po_link_to, falling back to po_link_to=default'),
+ $config{po_link_to}));
+ $config{po_link_to}='default';
+ }
+ elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
+ warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
+ $config{po_link_to}='default';
+ }
+
+ push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
+
+ # Translated versions of the underlays are added if available.
+ foreach my $underlay ("basewiki",
+ map { m/^\Q$config{underlaydirbase}\E\/*(.*)/ }
+ reverse @{$config{underlaydirs}}) {
+ next if $underlay=~/^locale\//;
+
+ # Underlays containing the po files for slave languages.
+ foreach my $ll (keys %{$config{po_slave_languages}}) {
+ add_underlay("po/$ll/$underlay")
+ if -d "$config{underlaydirbase}/po/$ll/$underlay";
+ }
+
+ if ($config{po_master_language}{code} ne 'en') {
+ # Add underlay containing translated source files
+ # for the master language.
+ add_underlay("locale/$config{po_master_language}{code}/$underlay");
+ }
+ }
+}
+
+sub needsbuild () {
+ my $needsbuild=shift;
+
+ # backup @needsbuild content so that change() can know whether
+ # a given master page was rendered because its source file was changed
+ @origneedsbuild=(@$needsbuild);
+
+ flushmemoizecache();
+ buildtranslationscache();
+
+ # make existing translations depend on the corresponding master page
+ foreach my $master (keys %translations) {
+ map add_depends($_, $master), values %{otherlanguages($master)};
+ }
+}
+
+# Massage the recorded state of internal links so that:
+# - it matches the actually generated links, rather than the links as written
+# in the pages' source
+# - backlinks are consistent in all cases
+sub scan (@) {
+ my %params=@_;
+ my $page=$params{page};
+ my $content=$params{content};
+
+ if (istranslation($page)) {
+ foreach my $destpage (@{$links{$page}}) {
+ if (istranslatable($destpage)) {
+ # replace the occurence of $destpage in $links{$page}
+ for (my $i=0; $i<@{$links{$page}}; $i++) {
+ if (@{$links{$page}}[$i] eq $destpage) {
+ @{$links{$page}}[$i] = $destpage . '.' . lang($page);
+ last;
+ }
+ }
+ }
+ }
+ }
+ elsif (! istranslatable($page) && ! istranslation($page)) {
+ foreach my $destpage (@{$links{$page}}) {
+ if (istranslatable($destpage)) {
+ # make sure any destpage's translations has
+ # $page in its backlinks
+ push @{$links{$page}},
+ values %{otherlanguages($destpage)};
+ }
+ }
+ }
+}
+
+# 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($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($torename{src})};
+ while (my ($lang, $otherpage) = each %otherpages) {
+ push @ret, {
+ src => $otherpage,
+ srcfile => $pagesources{$otherpage},
+ dest => otherlanguage($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=@_;
+
+ # 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 passes in turn 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("rebuilding all pages to fix meta titles")));
+ resetalreadyfiltered();
+ require IkiWiki::Render;
+ foreach my $file (@rendered) {
+ IkiWiki::render($file, sprintf(gettext("building %s"), $file));
+ }
+ }