2 # .po as a wiki page type
3 # Licensed under GPL v2 or greater
4 # Copyright (C) 2008-2009 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, last => 1);
47 hook(type => "formbuilder", id => "po", call => \&formbuilder);
49 $origsubs{'bestlink'}=\&IkiWiki::bestlink;
50 inject(name => "IkiWiki::bestlink", call => \&mybestlink);
51 $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
52 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
53 $origsubs{'targetpage'}=\&IkiWiki::targetpage;
54 inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
55 $origsubs{'urlto'}=\&IkiWiki::urlto;
56 inject(name => "IkiWiki::urlto", call => \&myurlto);
57 $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
58 inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
67 # 2. Injected functions
68 # 3. Blackboxes for private data
83 po_master_language => {
89 description => "master language (non-PO files)",
93 po_slave_languages => {
100 description => "slave languages (PO files)",
104 po_translatable_pages => {
106 example => "!*/Discussion",
107 description => "PageSpec controlling which pages are translatable",
108 link => "ikiwiki/PageSpec",
114 example => "current",
115 description => "internal linking behavior (default/current/negotiated)",
119 po_translation_status_in_links => {
122 description => "display translation status in links to translations",
129 foreach my $field (qw{po_master_language po_slave_languages}) {
130 if (! exists $config{$field} || ! defined $config{$field}) {
131 error(sprintf(gettext("Must specify %s when using the %s plugin"), $field, 'po'));
134 if (! (keys %{$config{po_slave_languages}})) {
135 error(gettext("At least one slave language must be defined ".
136 "in po_slave_languages when using the po plugin"));
140 or error(sprintf(gettext("%s is not a valid language code"), $_));
141 } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
142 if (! exists $config{po_translatable_pages} ||
143 ! defined $config{po_translatable_pages}) {
144 $config{po_translatable_pages}="";
146 if (! exists $config{po_link_to} ||
147 ! defined $config{po_link_to}) {
148 $config{po_link_to}='default';
151 $config{po_link_to} eq $_
152 } ('default', 'current', 'negotiated')) {
153 warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
154 $config{po_link_to}));
155 $config{po_link_to}='default';
157 elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
158 warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
159 $config{po_link_to}='default';
161 if (! exists $config{po_translation_status_in_links} ||
162 ! defined $config{po_translation_status_in_links}) {
163 $config{po_translation_status_in_links}=1;
165 push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
169 my $needsbuild=shift;
171 # backup @needsbuild content so that change() can know whether
172 # a given master page was rendered because its source file was changed
173 @origneedsbuild=(@$needsbuild);
176 buildtranslationscache();
178 # make existing translations depend on the corresponding master page
179 foreach my $master (keys %translations) {
180 map add_depends($_, $master), values %{otherlanguages($master)};
184 # Massage the recorded state of internal links so that:
185 # - it matches the actually generated links, rather than the links as written
186 # in the pages' source
187 # - backlinks are consistent in all cases
190 my $page=$params{page};
191 my $content=$params{content};
193 return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
195 if (istranslation($page)) {
196 foreach my $destpage (@{$links{$page}}) {
197 if (istranslatable($destpage)) {
198 # replace one occurence of $destpage in $links{$page}
199 # (we only want to replace the one that was added by
200 # IkiWiki::Plugin::link::scan, other occurences may be
201 # there for other reasons)
202 for (my $i=0; $i<@{$links{$page}}; $i++) {
203 if (@{$links{$page}}[$i] eq $destpage) {
204 @{$links{$page}}[$i] = $destpage . '.' . lang($page);
211 elsif (! istranslatable($page) && ! istranslation($page)) {
212 foreach my $destpage (@{$links{$page}}) {
213 if (istranslatable($destpage)) {
214 # make sure any destpage's translations has
215 # $page in its backlinks
216 push @{$links{$page}},
217 values %{otherlanguages($destpage)};
223 # We use filter to convert PO to the master page's format,
224 # since the rest of ikiwiki should not work on PO files.
228 my $page = $params{page};
229 my $destpage = $params{destpage};
230 my $content = $params{content};
231 if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
232 $content = po_to_markup($page, $content);
233 setalreadyfiltered($page, $destpage);
241 my $page = $params{page};
242 my $content = $params{content};
244 # ignore PO files this plugin did not create
245 return $content unless istranslation($page);
247 # force content to be htmlize'd as if it was the same type as the master page
248 return IkiWiki::htmlize($page, $page,
249 pagetype(srcfile($pagesources{masterpage($page)})),
253 sub pagetemplate (@) {
255 my $page=$params{page};
256 my $destpage=$params{destpage};
257 my $template=$params{template};
259 my ($masterpage, $lang) = istranslation($page);
261 if (istranslation($page) && $template->query(name => "percenttranslated")) {
262 $template->param(percenttranslated => percenttranslated($page));
264 if ($template->query(name => "istranslation")) {
265 $template->param(istranslation => scalar istranslation($page));
267 if ($template->query(name => "istranslatable")) {
268 $template->param(istranslatable => istranslatable($page));
270 if ($template->query(name => "HOMEPAGEURL")) {
271 $template->param(homepageurl => homepageurl($page));
273 if ($template->query(name => "otherlanguages")) {
274 $template->param(otherlanguages => [otherlanguagesloop($page)]);
275 map add_depends($page, $_), (values %{otherlanguages($page)});
277 # Rely on IkiWiki::Render's genpage() to decide wether
278 # a discussion link should appear on $page; this is not
279 # totally accurate, though: some broken links may be generated
280 # when cgiurl is disabled.
281 # This compromise avoids some code duplication, and will probably
282 # prevent future breakage when ikiwiki internals change.
283 # Known limitations are preferred to future random bugs.
284 if ($template->param('discussionlink') && istranslation($page)) {
285 $template->param('discussionlink' => htmllink(
288 $masterpage . '/' . gettext("Discussion"),
291 linktext => gettext("Discussion"),
294 # Remove broken parentlink to ./index.html on home page's translations.
295 # It works because this hook has the "last" parameter set, to ensure it
296 # runs after parentlinks' own pagetemplate hook.
297 if ($template->param('parentlinks')
298 && istranslation($page)
299 && $masterpage eq "index") {
300 $template->param('parentlinks' => []);
306 my $page = $params{page};
308 # backlinks involve back-dependencies, so that nicepagetitle effects,
309 # such as translation status displayed in links, are updated
311 map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
314 # Add the renamed page translations to the list of to-be-renamed pages.
315 sub renamepages($$$) {
316 my ($torename, $cgi, $session) = (shift, shift, shift);
318 # copy the initial array, so that we can iterate on it AND
319 # modify it at the same time, without iterating on the items we
320 # pushed on it ourselves
321 my @torename=@{$torename};
323 # Save the page(s) the user asked to rename, so that our
324 # canrename hook can tell the difference between:
325 # - a translation being renamed as a consequence of its master page
327 # - a user trying to directly rename a translation
328 # This is why this hook has to be run first, before @torename is modified
330 $session->param(po_orig_torename => [ @torename ]);
331 IkiWiki::cgi_savesession($session);
333 foreach my $rename (@torename) {
334 next unless istranslatable($rename->{src});
335 my %otherpages=%{otherlanguages($rename->{src})};
336 while (my ($lang, $otherpage) = each %otherpages) {
339 srcfile => $pagesources{$otherpage},
340 dest => otherlanguage($rename->{dest}, $lang),
341 destfile => $rename->{dest}.".".$lang.".po",
351 map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
357 my $updated_po_files=0;
359 # Refresh/create POT and PO files as needed.
360 foreach my $file (grep {istranslatablefile($_)} @rendered) {
361 my $page=pagename($file);
362 my $masterfile=srcfile($file);
363 my $updated_pot_file=0;
364 # Only refresh Pot file if it does not exist, or if
365 # $pagesources{$page} was changed: don't if only the HTML was
366 # refreshed, e.g. because of a dependency.
367 if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
368 || ! -e potfile($masterfile)) {
369 refreshpot($masterfile);
374 push @pofiles, $_ if ($updated_pot_file || ! -e $_);
375 } (pofiles($masterfile));
377 refreshpofiles($masterfile, @pofiles);
378 map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
383 if ($updated_po_files) {
385 gettext("updated PO files"),
386 "IkiWiki::Plugin::po::change");
391 my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
393 if (istranslation($page)) {
394 my $res = isvalidpo($content);
405 sub canremove ($$$) {
406 my ($page, $cgi, $session) = (shift, shift, shift);
408 if (istranslation($page)) {
409 return gettext("Can not remove a translation. Removing the master page, ".
410 "though, removes its translations as well.");
415 sub canrename ($$@) {
416 my ($cgi, $session) = (shift, shift);
419 if (istranslation($params{src})) {
420 my $masterpage = masterpage($params{src});
421 # Tell the difference between:
422 # - a translation being renamed as a consequence of its master page
423 # being renamed, which is allowed
424 # - a user trying to directly rename a translation, which is forbidden
425 # by looking for the master page in the list of to-be-renamed pages we
426 # saved early in the renaming process.
427 my $orig_torename = $session->param("po_orig_torename");
428 unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
429 return gettext("Can not rename a translation. Renaming the master page, ".
430 "though, renames its translations as well.");
436 # As we're previewing or saving a page, the content may have
437 # changed, so tell the next filter() invocation it must not be lazy.
441 unsetalreadyfiltered($params{page}, $params{page});
442 return $params{content};
445 sub formbuilder_setup (@) {
447 my $form=$params{form};
450 return unless defined $form->field("do");
452 if ($form->field("do") eq "create") {
453 # Warn the user: new pages must be written in master language.
454 my $template=template("pocreatepage.tmpl");
455 $template->param(LANG => $config{po_master_language}{name});
456 $form->tmpl_param(message => $template->output);
458 elsif ($form->field("do") eq "edit") {
459 # Remove the rename/remove buttons on slave pages.
460 # This has to be done after the rename/remove plugins have added
461 # their buttons, which is why this hook must be run last.
462 # The canrename/canremove hooks already ensure this is forbidden
463 # at the backend level, so this is only UI sugar.
464 if (istranslation($form->field("page"))) {
466 for (my $i = 0; $i < @{$params{buttons}}; $i++) {
467 if (@{$params{buttons}}[$i] eq $_) {
468 delete @{$params{buttons}}[$i];
477 # Do not allow to create pages of type po: they are automatically created.
478 # The main reason to do so is to bypass the "favor the type of linking page
479 # on page creation" logic, which is unsuitable when a broken link is clicked
480 # on a slave (PO) page.
481 sub formbuilder (@) {
483 my $form=$params{form};
486 return unless (defined $form->field("do") && $form->field("do") eq "create");
488 for my $field ($form->field) {
489 next unless "$field" eq "type";
490 if ($field->type eq 'select') {
491 # remove po from the types list
492 my @types = grep { $_ ne 'po' } $field->options;
493 $field->options(\@types) if scalar @types;
496 # make sure the default value is not po;
497 # does this case actually happen?
498 debug sprintf("po(formbuilder) type field is not select - not implemented yet");
504 # | Injected functions
507 # Implement po_link_to 'current' and 'negotiated' settings.
508 sub mybestlink ($$) {
512 my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
514 && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
515 && istranslatable($res)
516 && istranslation($page)) {
517 return $res . "." . lang($page);
522 sub mybeautify_urlpath ($) {
525 my $res=$origsubs{'beautify_urlpath'}->($url);
526 if ($config{po_link_to} eq "negotiated") {
527 $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
528 $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
530 $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
531 } (keys %{$config{po_slave_languages}});
536 sub mytargetpage ($$) {
540 if (istranslation($page) || istranslatable($page)) {
541 my ($masterpage, $lang) = (masterpage($page), lang($page));
542 if (! $config{usedirs} || $masterpage eq 'index') {
543 return $masterpage . "." . $lang . "." . $ext;
546 return $masterpage . "/index." . $lang . "." . $ext;
549 return $origsubs{'targetpage'}->($page, $ext);
557 # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
559 && $config{po_link_to} eq "current"
560 && istranslatable('index')) {
561 return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
563 # avoid using our injected beautify_urlpath if run by cgi_editpage,
564 # so that one is redirected to the just-edited page rather than to the
565 # negociated translation; to prevent unnecessary fiddling with caller/inject,
566 # we only do so when our beautify_urlpath would actually do what we want to
567 # avoid, i.e. when po_link_to = negotiated
568 if ($config{po_link_to} eq "negotiated") {
569 my @caller = caller(1);
570 my $run_by_editpage = 0;
571 $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
572 && $caller[3] eq "IkiWiki::cgi_editpage");
573 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
575 my $res = $origsubs{'urlto'}->($to,$from,$absolute);
576 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
581 return $origsubs{'urlto'}->($to,$from,$absolute)
585 sub mynicepagetitle ($;$) {
586 my ($page, $unescaped) = (shift, shift);
588 my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
589 return $res unless istranslation($page);
590 return $res unless $config{po_translation_status_in_links};
591 return $res.' ('.percenttranslated($page).' %)';
595 # | Blackboxes for private data
601 sub alreadyfiltered($$) {
605 return ( exists $filtered{$page}{$destpage}
606 && $filtered{$page}{$destpage} eq 1 );
609 sub setalreadyfiltered($$) {
613 $filtered{$page}{$destpage}=1;
616 sub unsetalreadyfiltered($$) {
620 if (exists $filtered{$page}{$destpage}) {
621 delete $filtered{$page}{$destpage};
625 sub resetalreadyfiltered() {
634 sub maybe_add_leading_slash ($;$) {
637 $add=1 unless defined $add;
638 return '/' . $str if $add;
642 sub istranslatablefile ($) {
645 return 0 unless defined $file;
646 return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
647 return 0 if $file =~ /\.pot$/;
648 return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
652 sub istranslatable ($) {
656 return 1 if istranslatablefile($pagesources{$page});
660 sub _istranslation ($) {
663 my $hasleadingslash = ($page=~s#^/##);
664 my $file=$pagesources{$page};
665 return 0 unless (defined $file
666 && defined pagetype($file)
667 && pagetype($file) eq 'po');
668 return 0 if $file =~ /\.pot$/;
670 my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
671 return 0 unless (defined $masterpage && defined $lang
672 && length $masterpage && length $lang
673 && defined $pagesources{$masterpage}
674 && defined $config{po_slave_languages}{$lang});
676 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
677 if istranslatable($masterpage);
680 sub istranslation ($) {
683 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
684 my $hasleadingslash = ($masterpage=~s#^/##);
685 $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
686 return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
694 if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
703 if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
706 return $config{po_master_language}{code};
709 sub islanguagecode ($) {
712 return ($code =~ /^[a-z]{2}$/);
715 sub otherlanguage ($$) {
719 return masterpage($page) if $code eq $config{po_master_language}{code};
720 return masterpage($page) . '.' . $code;
723 sub otherlanguages ($) {
727 return \%ret unless (istranslation($page) || istranslatable($page));
728 my $curlang=lang($page);
730 ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
731 next if $lang eq $curlang;
732 $ret{$lang}=otherlanguage($page, $lang);
738 my $masterfile=shift;
740 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
741 $dir='' if $dir eq './';
742 return File::Spec->catpath('', $dir, $name . ".pot");
746 my $masterfile=shift;
749 (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
750 $dir='' if $dir eq './';
751 return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
755 my $masterfile=shift;
757 return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
761 my $masterfile=shift;
763 my $potfile=potfile($masterfile);
764 my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
765 my $doc=Locale::Po4a::Chooser::new('text',%options);
766 $doc->{TT}{utf_mode} = 1;
767 $doc->{TT}{file_in_charset} = 'utf-8';
768 $doc->{TT}{file_out_charset} = 'utf-8';
769 $doc->read($masterfile);
770 # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
771 # this is undocument use of internal Locale::Po4a::TransTractor's data,
772 # compulsory since this module prevents us from using the porefs option.
773 $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
774 $doc->{TT}{po_out}->set_charset('utf-8');
777 IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
778 $doc->writepo($potfile);
781 sub refreshpofiles ($@) {
782 my $masterfile=shift;
785 my $potfile=potfile($masterfile);
787 or error(sprintf(gettext("po(refreshpofiles) POT file (%s) does not exist"),
790 foreach my $pofile (@pofiles) {
791 IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
793 system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
794 or error(sprintf(gettext("po(refreshpofiles) failed to update %s"),
798 File::Copy::syscopy($potfile,$pofile)
799 or error(sprintf(gettext("po(refreshpofiles) failed to copy the POT file to %s"),
805 sub buildtranslationscache() {
806 # use istranslation's side-effect
807 map istranslation($_), (keys %pagesources);
810 sub resettranslationscache() {
814 sub flushmemoizecache() {
815 Memoize::flush_cache("istranslatable");
816 Memoize::flush_cache("_istranslation");
817 Memoize::flush_cache("percenttranslated");
820 sub urlto_with_orig_beautiful_urlpath($$) {
824 inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
825 my $res=urlto($to, $from);
826 inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
831 sub percenttranslated ($) {
835 return gettext("N/A") unless istranslation($page);
836 my $file=srcfile($pagesources{$page});
837 my $masterfile = srcfile($pagesources{masterpage($page)});
839 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
841 my $doc=Locale::Po4a::Chooser::new('text',%options);
843 'po_in_name' => [ $file ],
844 'file_in_name' => [ $masterfile ],
845 'file_in_charset' => 'utf-8',
846 'file_out_charset' => 'utf-8',
847 ) or error(sprintf(gettext("po(percenttranslated) failed to translate %s"),
849 my ($percent,$hit,$queries) = $doc->stats();
850 $percent =~ s/\.[0-9]+$//;
854 sub languagename ($) {
857 return $config{po_master_language}{name}
858 if $code eq $config{po_master_language}{code};
859 return $config{po_slave_languages}{$code}
860 if defined $config{po_slave_languages}{$code};
864 sub otherlanguagesloop ($) {
868 my %otherpages=%{otherlanguages($page)};
869 while (my ($lang, $otherpage) = each %otherpages) {
870 if (istranslation($page) && masterpage($page) eq $otherpage) {
872 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
874 language => languagename($lang),
880 url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
882 language => languagename($lang),
883 percent => percenttranslated($otherpage),
888 return -1 if $a->{code} eq $config{po_master_language}{code};
889 return 1 if $b->{code} eq $config{po_master_language}{code};
890 return $a->{language} cmp $b->{language};
894 sub homepageurl (;$) {
897 return urlto('', $page);
900 sub deletetranslations ($) {
901 my $deletedmasterfile=shift;
903 my $deletedmasterpage=pagename($deletedmasterfile);
906 my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
907 my $absfile = "$config{srcdir}/$file";
908 if (-e $absfile && ! -l $absfile && ! -d $absfile) {
909 push @todelete, $file;
911 } keys %{$config{po_slave_languages}};
915 IkiWiki::rcs_remove($_);
918 IkiWiki::prune("$config{srcdir}/$_");
922 if (scalar @todelete) {
924 gettext("removed obsolete PO files"),
925 "IkiWiki::Plugin::po::deletetranslations");
929 sub commit_and_refresh ($$) {
930 my ($msg, $author) = (shift, shift);
933 IkiWiki::disable_commit_hook();
934 IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
935 IkiWiki::enable_commit_hook();
936 IkiWiki::rcs_update();
938 # Reinitialize module's private variables.
939 resetalreadyfiltered();
940 resettranslationscache();
942 # Trigger a wiki refresh.
943 require IkiWiki::Render;
944 # without preliminary saveindex/loadindex, refresh()
945 # complains about a lot of uninitialized variables
946 IkiWiki::saveindex();
947 IkiWiki::loadindex();
949 IkiWiki::saveindex();
952 # on success, returns the filtered content.
953 # on error, if $nonfatal, warn and return undef; else, error out.
954 sub po_to_markup ($$;$) {
955 my ($page, $content) = (shift, shift);
956 my $nonfatal = shift;
958 $content = '' unless defined $content;
959 $content = decode_utf8(encode_utf8($content));
960 # CRLF line terminators make poor Locale::Po4a feel bad
961 $content=~s/\r\n/\n/g;
963 # There are incompatibilities between some File::Temp versions
964 # (including 0.18, bundled with Lenny's perl-modules package)
965 # and others (e.g. 0.20, previously present in the archive as
966 # a standalone package): under certain circumstances, some
967 # return a relative filename, whereas others return an absolute one;
968 # we here use this module in a way that is at least compatible
969 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
970 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
971 DIR => File::Spec->tmpdir,
972 UNLINK => 1)->filename;
973 my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
974 DIR => File::Spec->tmpdir,
975 UNLINK => 1)->filename;
978 my $msg = "po(po_to_markup) - $page : " . shift;
983 error($msg, sub { unlink $infile, $outfile});
986 writefile(basename($infile), File::Spec->tmpdir, $content)
987 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
989 my $masterfile = srcfile($pagesources{masterpage($page)});
991 "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
993 my $doc=Locale::Po4a::Chooser::new('text',%options);
995 'po_in_name' => [ $infile ],
996 'file_in_name' => [ $masterfile ],
997 'file_in_charset' => 'utf-8',
998 'file_out_charset' => 'utf-8',
999 ) or return $fail->(gettext("failed to translate"));
1000 $doc->write($outfile)
1001 or return $fail->(sprintf(gettext("failed to write %s"), $outfile));
1003 $content = readfile($outfile)
1004 or return $fail->(sprintf(gettext("failed to read %s"), $outfile));
1006 # Unlinking should happen automatically, thanks to File::Temp,
1007 # but it does not work here, probably because of the way writefile()
1008 # and Locale::Po4a::write() work.
1009 unlink $infile, $outfile;
1014 # returns a SuccessReason or FailReason object
1016 my $content = shift;
1018 # NB: we don't use po_to_markup here, since Po4a parser does
1019 # not mind invalid PO content
1020 $content = '' unless defined $content;
1021 $content = decode_utf8(encode_utf8($content));
1023 # There are incompatibilities between some File::Temp versions
1024 # (including 0.18, bundled with Lenny's perl-modules package)
1025 # and others (e.g. 0.20, previously present in the archive as
1026 # a standalone package): under certain circumstances, some
1027 # return a relative filename, whereas others return an absolute one;
1028 # we here use this module in a way that is at least compatible
1029 # with 0.18 and 0.20. Beware, hit'n'run refactorers!
1030 my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-isvalidpo.XXXXXXXXXX",
1031 DIR => File::Spec->tmpdir,
1032 UNLINK => 1)->filename;
1034 my $fail = sub ($) {
1035 my $msg = '[po/isvalidpo] ' . shift;
1037 return IkiWiki::FailReason->new("$msg");
1040 writefile(basename($infile), File::Spec->tmpdir, $content)
1041 or return $fail->(sprintf(gettext("failed to write %s"), $infile));
1043 my $res = (system("msgfmt", "--check", $infile, "-o", "/dev/null") == 0);
1045 # Unlinking should happen automatically, thanks to File::Temp,
1046 # but it does not work here, probably because of the way writefile()
1047 # and Locale::Po4a::write() work.
1051 return IkiWiki::SuccessReason->new("valid gettext data");
1053 return IkiWiki::FailReason->new("invalid gettext data");
1060 package IkiWiki::PageSpec;
1065 sub match_istranslation ($;@) {
1068 if (IkiWiki::Plugin::po::istranslation($page)) {
1069 return IkiWiki::SuccessReason->new("is a translation page");
1072 return IkiWiki::FailReason->new("is not a translation page");
1076 sub match_istranslatable ($;@) {
1079 if (IkiWiki::Plugin::po::istranslatable($page)) {
1080 return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
1083 return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
1087 sub match_lang ($$;@) {
1091 my $regexp=IkiWiki::glob2re($wanted);
1092 my $lang=IkiWiki::Plugin::po::lang($page);
1093 if ($lang!~/^$regexp$/i) {
1094 return IkiWiki::FailReason->new("file language is $lang, not $wanted");
1097 return IkiWiki::SuccessReason->new("file language is $wanted");
1101 sub match_currentlang ($$;@) {
1106 return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
1108 my $currentlang=IkiWiki::Plugin::po::lang($params{location});
1109 my $lang=IkiWiki::Plugin::po::lang($page);
1111 if ($lang eq $currentlang) {
1112 return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
1115 return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");