2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008 intrigeri <intrigeri@boum.org>
5 # inspired by the GPL'd po4a-translate,
6 # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
7 package IkiWiki::Plugin::po;
13 use Locale::Po4a::Chooser;
26 memoize("istranslatable");
27 memoize("_istranslation");
28 memoize("percenttranslated");
31 hook(type => "getsetup", id => "po", call => \&getsetup);
32 hook(type => "checkconfig", id => "po", call => \&checkconfig);
33 hook(type => "needsbuild", id => "po", call => \&needsbuild);
34 hook(type => "scan", id => "po", call => \&scan, last =>1);
35 hook(type => "filter", id => "po", call => \&filter);
36 hook(type => "htmlize", id => "po", call => \&htmlize);
37 hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
38 hook(type => "postscan", id => "po", call => \&postscan);
39 hook(type => "rename", id => "po", call => \&renamepages, first => 1);
40 hook(type => "delete", id => "po", call => \&mydelete);
41 hook(type => "change", id => "po", call => \&change);
42 hook(type => "cansave", id => "po", call => \&cansave);
43 hook(type => "canremove", id => "po", call => \&canremove);
44 hook(type => "canrename", id => "po", call => \&canrename);
45 hook(type => "editcontent", id => "po", call => \&editcontent);
46 hook(type => "formbuilder_setup", id => "po", call => \&formbuilder_setup);
48 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
49 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
50 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
51 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
52 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
53 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
54 $origsubs{'urlto'}=\&IkiWiki::urlto;
55 inject(name => "IkiWiki::urlto", call => \&myurlto);
56 $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
57 inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
66 # 2. Injected functions
67 # 3. Blackboxes for private data
82 po_master_language => {
88 description => "master language (non-PO files)",
92 po_slave_languages => {
99 description => "slave languages (PO files)",
103 po_translatable_pages => {
105 example => "!*/Discussion",
106 description => "PageSpec controlling which pages are translatable",
107 link => "ikiwiki/PageSpec",
113 example => "current",
114 description => "internal linking behavior (default/current/negotiated)",
118 po_translation_status_in_links => {
121 description => "display translation status in links to translations",
128 foreach my $field (qw{po_master_language po_slave_languages}) {
129 if (! exists $config{$field} || ! defined $config{$field}) {
130 error(sprintf(gettext("Must specify %s"), $field));
133 if (! (keys %{$config{po_slave_languages}})) {
134 error(gettext("At least one slave language must be defined in po_slave_languages"));
138 or error(sprintf(gettext("%s is not a valid language code"), $_));
139 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
140 if (! exists $config{po_translatable_pages} ||
141 ! defined $config{po_translatable_pages}) {
142 $config{po_translatable_pages}="";
144 if (! exists $config{po_link_to} ||
145 ! defined $config{po_link_to}) {
146 $config{po_link_to}='default';
149 $config{po_link_to} eq $_
150 } ('default', 'current', 'negotiated')) {
151 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
152 $config{po_link_to}));
153 $config{po_link_to}='default';
155 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
156 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
157 $config{po_link_to}='default';
159 if (! exists $config{po_translation_status_in_links} ||
160 ! defined $config{po_translation_status_in_links}) {
161 $config{po_translation_status_in_links}=1;
163 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
167 my $needsbuild=shift;
169 # backup @needsbuild content so that change() can know whether
170 # a given master page was rendered because its source file was changed
171 @origneedsbuild=(@$needsbuild);
174 buildtranslationscache();
176 # make existing translations depend on the corresponding master page
177 foreach my $master (keys %translations) {
178 map add_depends($_, $master), values %{otherlanguages($master)};
182 # Massage the recorded state of internal links so that:
183 # - it matches the actually generated links, rather than the links as written
184 # in the pages' source
185 # - backlinks are consistent in all cases
188 my $page=$params{page};
189 my $content=$params{content};
191 return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
193 if (istranslation($page)) {
194 foreach my $destpage (@{$links{$page}}) {
195 if (istranslatable($destpage)) {
196 # replace one occurence of $destpage in $links{$page}
197 # (we only want to replace the one that was added by
198 # IkiWiki::Plugin::link::scan, other occurences may be
199 # there for other reasons)
200 for (my $i=0; $i<@{$links{$page}}; $i++) {
201 if (@{$links{$page}}[$i] eq $destpage) {
202 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
209 elsif (! istranslatable($page) && ! istranslation($page)) {
210 foreach my $destpage (@{$links{$page}}) {
211 if (istranslatable($destpage)) {
212 # make sure any destpage's translations has
213 # $page in its backlinks
214 push @{$links{$page}},
215 values %{otherlanguages($destpage)};
221 # We use filter to convert PO to the master page's format,
222 # since the rest of ikiwiki should not work on PO files.
226 my $page = $params{page};
227 my $destpage = $params{destpage};
228 my $content = $params{content};
229 if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
230 $content = po_to_markup($page, $content);
231 setalreadyfiltered($page, $destpage);
239 my $page = $params{page};
240 my $content = $params{content};
242 # ignore PO files this plugin did not create
243 return $content unless istranslation($page);
245 # force content to be htmlize'd as if it was the same type as the master page
246 return IkiWiki::htmlize($page, $page,
247 pagetype(srcfile($pagesources{masterpage($page)})),
251 sub pagetemplate (@) {
253 my $page=$params{page};
254 my $destpage=$params{destpage};
255 my $template=$params{template};
257 my ($masterpage, $lang) = istranslation($page);
259 if (istranslation($page) && $template->query(name => "percenttranslated")) {
260 $template->param(percenttranslated => percenttranslated($page));
262 if ($template->query(name => "istranslation")) {
263 $template->param(istranslation => scalar istranslation($page));
265 if ($template->query(name => "istranslatable")) {
266 $template->param(istranslatable => istranslatable($page));
268 if ($template->query(name => "HOMEPAGEURL")) {
269 $template->param(homepageurl => homepageurl($page));
271 if ($template->query(name => "otherlanguages")) {
272 $template->param(otherlanguages => [otherlanguagesloop($page)]);
273 map add_depends($page, $_), (values %{otherlanguages($page)});
275 # Rely on IkiWiki::Render's genpage() to decide wether
276 # a discussion link should appear on $page; this is not
277 # totally accurate, though: some broken links may be generated
278 # when cgiurl is disabled.
279 # This compromise avoids some code duplication, and will probably
280 # prevent future breakage when ikiwiki internals change.
281 # Known limitations are preferred to future random bugs.
282 if ($template->param('discussionlink') && istranslation($page)) {
283 $template->param('discussionlink' => htmllink(
286 $masterpage . '/' . gettext("Discussion"),
289 linktext => gettext("Discussion"),
292 # Remove broken parentlink to ./index.html on home page's translations.
293 # It works because this hook has the "last" parameter set, to ensure it
294 # runs after parentlinks' own pagetemplate hook.
295 if ($template->param('parentlinks')
296 && istranslation($page)
297 && $masterpage eq "index") {
298 $template->param('parentlinks' => []);
304 my $page = $params{page};
306 # backlinks involve back-dependencies, so that nicepagetitle effects,
307 # such as translation status displayed in links, are updated
309 map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
312 # Add the renamed page translations to the list of to-be-renamed pages.
313 sub renamepages($$$) {
314 my ($torename, $cgi, $session) = (shift, shift, shift);
316 # copy the initial array, so that we can iterate on it AND
317 # modify it at the same time, without iterating on the items we
318 # pushed on it ourselves
319 my @torename=@{$torename};
321 # Save the page(s) the user asked to rename, so that our
322 # canrename hook can tell the difference between:
323 # - a translation being renamed as a consequence of its master page
325 # - a user trying to directly rename a translation
326 # This is why this hook has to be run first, before @torename is modified
328 $session->param(po_orig_torename => [ @torename ]);
329 IkiWiki::cgi_savesession($session);
331 foreach my $rename (@torename) {
332 next unless istranslatable($rename->{src});
333 my %otherpages=%{otherlanguages($rename->{src})};
334 while (my ($lang, $otherpage) = each %otherpages) {
337 srcfile => $pagesources{$otherpage},
338 dest => otherlanguage($rename->{dest}, $lang),
339 destfile => $rename->{dest}.".".$lang.".po",
349 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
355 my $updated_po_files=0;
357 # Refresh/create POT and PO files as needed.
358 foreach my $file (grep {istranslatablefile($_)} @rendered) {
359 my $page=pagename($file);
360 my $masterfile=srcfile($file);
361 my $updated_pot_file=0;
362 # Only refresh Pot file if it does not exist, or if
363 # $pagesources{$page} was changed: don't if only the HTML was
364 # refreshed, e.g. because of a dependency.
365 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
366 || ! -e potfile($masterfile)) {
367 refreshpot($masterfile);
372 push @pofiles, $_ if ($updated_pot_file || ! -e $_);
373 } (pofiles($masterfile));
375 refreshpofiles($masterfile, @pofiles);
376 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
381 if ($updated_po_files) {
383 gettext("updated PO files"),
384 "IkiWiki::Plugin::po::change");
389 my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
391 if (istranslation($page)) {
392 my $res = isvalidpo($content);
403 sub canremove ($$$) {
404 my ($page, $cgi, $session) = (shift, shift, shift);
406 if (istranslation($page)) {
407 return gettext("Can not remove a translation. Removing the master page, ".
408 "though, removes its translations as well.");
413 sub canrename ($$@) {
414 my ($cgi, $session) = (shift, shift);
417 if (istranslation($params{src})) {
418 my $masterpage = masterpage($params{src});
419 # Tell the difference between:
420 # - a translation being renamed as a consequence of its master page
421 # being renamed, which is allowed
422 # - a user trying to directly rename a translation, which is forbidden
423 # by looking for the master page in the list of to-be-renamed pages we
424 # saved early in the renaming process.
425 my $orig_torename = $session->param("po_orig_torename");
426 unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
427 return gettext("Can not rename a translation. Renaming the master page, ".
428 "though, renames its translations as well.");
434 # As we're previewing or saving a page, the content may have
435 # changed, so tell the next filter() invocation it must not be lazy.
439 unsetalreadyfiltered($params{page}, $params{page});
440 return $params{content};
443 sub formbuilder_setup (@) {
445 my $form=$params{form};
448 return unless (defined $form->field("do") && $form->field("do") eq "create");
450 my $template=template("pocreatepage.tmpl");
451 $template->param(LANG => $config{po_master_language}{name});
452 $form->tmpl_param(message => $template->output);
457 # | Injected functions
460 # Implement po_link_to 'current' and 'negotiated' settings.
461 sub mybestlink ($$) {
465 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
467 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
468 && istranslatable($res)
469 && istranslation($page)) {
470 return $res . "." . lang($page);
475 sub mybeautify_urlpath ($) {
478 my $res=$origsubs{'beautify_urlpath'}->($url);
479 if ($config{po_link_to} eq "negotiated") {
480 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
481 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
483 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
484 } (keys %{$config{po_slave_languages}});
489 sub mytargetpage ($$) {
493 if (istranslation($page) || istranslatable($page)) {
494 my ($masterpage, $lang) = (masterpage($page), lang($page));
495 if (! $config{usedirs} || $masterpage eq 'index') {
496 return $masterpage . "." . $lang . "." . $ext;
499 return $masterpage . "/index." . $lang . "." . $ext;
502 return $origsubs{'targetpage'}->($page, $ext);
510 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
512 && $config{po_link_to} eq "current"
513 && istranslatable('index')) {
514 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
516 # avoid using our injected beautify_urlpath if run by cgi_editpage,
517 # so that one is redirected to the just-edited page rather than to the
518 # negociated translation; to prevent unnecessary fiddling with caller/inject,
519 # we only do so when our beautify_urlpath would actually do what we want to
520 # avoid, i.e. when po_link_to = negotiated
521 if ($config{po_link_to} eq "negotiated") {
522 my @caller = caller(1);
523 my $run_by_editpage = 0;
524 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
525 && $caller[3] eq "IkiWiki::cgi_editpage");
526 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
528 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
529 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
534 return $origsubs{'urlto'}->($to,$from,$absolute)
538 sub mynicepagetitle ($;$) {
539 my ($page, $unescaped) = (shift, shift);
541 my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
542 return $res unless istranslation($page);
543 return $res unless $config{po_translation_status_in_links};
544 return $res.' ('.percenttranslated($page).' %)';
548 # | Blackboxes for private data
554 sub alreadyfiltered($$) {
558 return ( exists $filtered{$page}{$destpage}
559 && $filtered{$page}{$destpage} eq 1 );
562 sub setalreadyfiltered($$) {
566 $filtered{$page}{$destpage}=1;
569 sub unsetalreadyfiltered($$) {
573 if (exists $filtered{$page}{$destpage}) {
574 delete $filtered{$page}{$destpage};
578 sub resetalreadyfiltered() {
587 sub maybe_add_leading_slash ($;$) {
590 $add=1 unless defined $add;
591 return '/' . $str if $add;
595 sub istranslatablefile ($) {
598 return 0 unless defined $file;
599 return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
600 return 0 if $file =~ /\.pot$/;
601 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
605 sub istranslatable ($) {
609 return 1 if istranslatablefile($pagesources{$page});
613 sub _istranslation ($) {
616 my $hasleadingslash = ($page=~s#^/##);
617 my $file=$pagesources{$page};
618 return 0 unless (defined $file
619 && defined pagetype($file)
620 && pagetype($file) eq 'po');
621 return 0 if $file =~ /\.pot$/;
623 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
624 return 0 unless (defined $masterpage && defined $lang
625 && length $masterpage && length $lang
626 && defined $pagesources{$masterpage}
627 && defined $config{po_slave_languages}{$lang});
629 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
630 if istranslatable($masterpage);
633 sub istranslation ($) {
636 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
637 my $hasleadingslash = ($masterpage=~s#^/##);
638 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
639 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
647 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
656 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
659 return $config{po_master_language}{code};
662 sub islanguagecode ($) {
665 return ($code =~ /^[a-z]{2}$/);
668 sub otherlanguage ($$) {
672 return masterpage($page) if $code eq $config{po_master_language}{code};
673 return masterpage($page) . '.' . $code;
676 sub otherlanguages ($) {
680 return \%ret unless (istranslation($page) || istranslatable($page));
681 my $curlang=lang($page);
683 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
684 next if $lang eq $curlang;
685 $ret{$lang}=otherlanguage($page, $lang);
691 my $masterfile=shift;
693 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
694 $dir='' if $dir eq './';
695 return File::Spec->catpath('', $dir, $name . ".pot");
699 my $masterfile=shift;
702 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
703 $dir='' if $dir eq './';
704 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
708 my $masterfile=shift;
710 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
714 my $masterfile=shift;
716 my $potfile=potfile($masterfile);
717 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
718 my $doc=Locale::Po4a::Chooser::new('text',%options);
719 $doc->{TT}{utf_mode} = 1;
720 $doc->{TT}{file_in_charset} = 'utf-8';
721 $doc->{TT}{file_out_charset} = 'utf-8';
722 $doc->read($masterfile);
723 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
724 # this is undocument use of internal Locale::Po4a::TransTractor's data,
725 # compulsory since this module prevents us from using the porefs option.
726 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
727 $doc->{TT}{po_out}->set_charset('utf-8');
730 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
731 $doc->writepo($potfile);
734 sub refreshpofiles ($@) {
735 my $masterfile=shift;
738 my $potfile=potfile($masterfile);
739 error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
741 foreach my $pofile (@pofiles) {
742 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
744 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
745 or error("[po/refreshpofiles:$pofile] failed to update");
748 File::Copy::syscopy($potfile,$pofile)
749 or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
754 sub buildtranslationscache() {
755 # use istranslation's side-effect
756 map istranslation($_), (keys %pagesources);
759 sub resettranslationscache() {
763 sub flushmemoizecache() {
764 Memoize::flush_cache("istranslatable");
765 Memoize::flush_cache("_istranslation");
766 Memoize::flush_cache("percenttranslated");
769 sub urlto_with_orig_beautiful_urlpath($$) {
773 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
774 my $res=urlto($to, $from);
775 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
780 sub percenttranslated ($) {
784 return gettext("N/A") unless istranslation($page);
785 my $file=srcfile($pagesources{$page});
786 my $masterfile = srcfile($pagesources{masterpage($page)});
788 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
790 my $doc=Locale::Po4a::Chooser::new('text',%options);
792 'po_in_name' => [ $file ],
793 'file_in_name' => [ $masterfile ],
794 'file_in_charset' => 'utf-8',
795 'file_out_charset' => 'utf-8',
796 ) or error("[po/percenttranslated:$page]: failed to translate");
797 my ($percent,$hit,$queries) = $doc->stats();
801 sub languagename ($) {
804 return $config{po_master_language}{name}
805 if $code eq $config{po_master_language}{code};
806 return $config{po_slave_languages}{$code}
807 if defined $config{po_slave_languages}{$code};
811 sub otherlanguagesloop ($) {
815 my %otherpages=%{otherlanguages($page)};
816 while (my ($lang, $otherpage) = each %otherpages) {
817 if (istranslation($page) && masterpage($page) eq $otherpage) {
819 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
821 language => languagename($lang),
827 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
829 language => languagename($lang),
830 percent => percenttranslated($otherpage),
835 return -1 if $a->{code} eq $config{po_master_language}{code};
836 return 1 if $b->{code} eq $config{po_master_language}{code};
837 return $a->{language} cmp $b->{language};
841 sub homepageurl (;$) {
844 return urlto('', $page);
847 sub deletetranslations ($) {
848 my $deletedmasterfile=shift;
850 my $deletedmasterpage=pagename($deletedmasterfile);
853 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
854 my $absfile = "$config{srcdir}/$file";
855 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
856 push @todelete, $file;
858 } keys %{$config{po_slave_languages}};
862 IkiWiki::rcs_remove($_);
865 IkiWiki::prune("$config{srcdir}/$_");
869 if (scalar @todelete) {
871 gettext("removed obsolete PO files"),
872 "IkiWiki::Plugin::po::deletetranslations");
876 sub commit_and_refresh ($$) {
877 my ($msg, $author) = (shift, shift);
880 IkiWiki::disable_commit_hook();
881 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
882 IkiWiki::enable_commit_hook();
883 IkiWiki::rcs_update();
885 # Reinitialize module's private variables.
886 resetalreadyfiltered();
887 resettranslationscache();
889 # Trigger a wiki refresh.
890 require IkiWiki::Render;
891 # without preliminary saveindex/loadindex, refresh()
892 # complains about a lot of uninitialized variables
893 IkiWiki::saveindex();
894 IkiWiki::loadindex();
896 IkiWiki::saveindex();
899 # on success, returns the filtered content.
900 # on error, if $nonfatal, warn and return undef; else, error out.
901 sub po_to_markup ($$;$) {
902 my ($page, $content) = (shift, shift);
903 my $nonfatal = shift;
905 $content = '' unless defined $content;
906 $content = decode_utf8(encode_utf8($content));
907 # CRLF line terminators make poor Locale::Po4a feel bad
908 $content=~s/\r\n/\n/g;
910 # There are incompatibilities between some File::Temp versions
911 # (including 0.18, bundled with Lenny's perl-modules package)
912 # and others (e.g. 0.20, previously present in the archive as
913 # a standalone package): under certain circumstances, some
914 # return a relative filename, whereas others return an absolute one;
915 # we here use this module in a way that is at least compatible
916 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
917 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
918 DIR => File::Spec->tmpdir,
919 UNLINK => 1)->filename;
920 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
921 DIR => File::Spec->tmpdir,
922 UNLINK => 1)->filename;
925 my $msg = '[po/po_to_markup:'.$page.'] ' . shift;
930 error($msg, sub { unlink $infile, $outfile});
933 writefile(basename($infile), File::Spec->tmpdir, $content)
934 or return failure("failed to write $infile");
936 my $masterfile = srcfile($pagesources{masterpage($page)});
938 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
940 my $doc=Locale::Po4a::Chooser::new('text',%options);
942 'po_in_name' => [ $infile ],
943 'file_in_name' => [ $masterfile ],
944 'file_in_charset' => 'utf-8',
945 'file_out_charset' => 'utf-8',
946 ) or return failure("failed to translate");
947 $doc->write($outfile) or return failure("could not write $outfile");
949 $content = readfile($outfile) or return failure("could not read $outfile");
951 # Unlinking should happen automatically, thanks to File::Temp,
952 # but it does not work here, probably because of the way writefile()
953 # and Locale::Po4a::write() work.
954 unlink $infile, $outfile;
959 # returns a SuccessReason or FailReason object
963 # NB: we don't use po_to_markup here, since Po4a parser does
964 # not mind invalid PO content
965 $content = '' unless defined $content;
966 $content = decode_utf8(encode_utf8($content));
968 # There are incompatibilities between some File::Temp versions
969 # (including 0.18, bundled with Lenny's perl-modules package)
970 # and others (e.g. 0.20, previously present in the archive as
971 # a standalone package): under certain circumstances, some
972 # return a relative filename, whereas others return an absolute one;
973 # we here use this module in a way that is at least compatible
974 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
975 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
976 DIR => File::Spec->tmpdir,
977 UNLINK => 1)->filename;
980 my $msg = '[po/isvalidpo] ' . shift;
982 return IkiWiki::FailReason->new("$msg");
985 writefile(basename($infile), File::Spec->tmpdir, $content)
986 or return failure("failed to write $infile");
988 my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
990 # Unlinking should happen automatically, thanks to File::Temp,
991 # but it does not work here, probably because of the way writefile()
992 # and Locale::Po4a::write() work.
996 return IkiWiki::SuccessReason->new("valid gettext data");
998 return IkiWiki::FailReason->new("invalid gettext data");
1005 package IkiWiki::PageSpec;
1010 sub match_istranslation ($;@) {
1013 if (IkiWiki::Plugin::po::istranslation($page)) {
1014 return IkiWiki::SuccessReason->new("is a translation page");
1017 return IkiWiki::FailReason->new("is not a translation page");
1021 sub match_istranslatable ($;@) {
1024 if (IkiWiki::Plugin::po::istranslatable($page)) {
1025 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1028 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1032 sub match_lang ($$;@) {
1036 my $regexp=IkiWiki::glob2re($wanted);
1037 my $lang=IkiWiki::Plugin::po::lang($page);
1038 if ($lang!~/^$regexp$/i) {
1039 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1042 return IkiWiki::SuccessReason->new("file language is $wanted");
1046 sub match_currentlang ($$;@) {
1051 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1053 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1054 my $lang=IkiWiki::Plugin::po::lang($page);
1056 if ($lang eq $currentlang) {
1057 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1060 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");