summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 89bd9947028fb759ca7ee5d62b267bffa8d3c06d (plain)
  1. #!/usr/bin/perl
  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;
  8. use warnings;
  9. use strict;
  10. use IkiWiki 2.00;
  11. use Encode;
  12. use Locale::Po4a::Chooser;
  13. use Locale::Po4a::Po;
  14. use File::Basename;
  15. use File::Copy;
  16. use File::Spec;
  17. use File::Temp;
  18. use Memoize;
  19. use UNIVERSAL;
  20. my %translations;
  21. my @origneedsbuild;
  22. our %filtered;
  23. memoize("_istranslation");
  24. memoize("percenttranslated");
  25. # FIXME: memoizing istranslatable() makes some test cases fail once every
  26. # two tries; this may be related to the artificial way the testsuite is
  27. # run, or not.
  28. # memoize("istranslatable");
  29. # backup references to subs that will be overriden
  30. my %origsubs;
  31. sub import { #{{{
  32. hook(type => "getsetup", id => "po", call => \&getsetup);
  33. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  34. hook(type => "needsbuild", id => "po", call => \&needsbuild);
  35. hook(type => "scan", id => "po", call => \&scan, last =>1);
  36. hook(type => "filter", id => "po", call => \&filter);
  37. hook(type => "htmlize", id => "po", call => \&htmlize);
  38. hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
  39. hook(type => "change", id => "po", call => \&change);
  40. hook(type => "editcontent", id => "po", call => \&editcontent);
  41. $origsubs{'bestlink'}=\&IkiWiki::bestlink;
  42. inject(name => "IkiWiki::bestlink", call => \&mybestlink);
  43. $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
  44. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  45. $origsubs{'targetpage'}=\&IkiWiki::targetpage;
  46. inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
  47. $origsubs{'urlto'}=\&IkiWiki::urlto;
  48. inject(name => "IkiWiki::urlto", call => \&myurlto);
  49. } #}}}
  50. sub getsetup () { #{{{
  51. return
  52. plugin => {
  53. safe => 0,
  54. rebuild => 1, # format plugin & changes html filenames
  55. },
  56. po_master_language => {
  57. type => "string",
  58. example => {
  59. 'code' => 'en',
  60. 'name' => 'English'
  61. },
  62. description => "master language (non-PO files)",
  63. safe => 1,
  64. rebuild => 1,
  65. },
  66. po_slave_languages => {
  67. type => "string",
  68. example => {
  69. 'fr' => 'Français',
  70. 'es' => 'Castellano',
  71. 'de' => 'Deutsch'
  72. },
  73. description => "slave languages (PO files)",
  74. safe => 1,
  75. rebuild => 1,
  76. },
  77. po_translatable_pages => {
  78. type => "pagespec",
  79. example => "!*/Discussion",
  80. description => "PageSpec controlling which pages are translatable",
  81. link => "ikiwiki/PageSpec",
  82. safe => 1,
  83. rebuild => 1,
  84. },
  85. po_link_to => {
  86. type => "string",
  87. example => "current",
  88. description => "internal linking behavior (default/current/negotiated)",
  89. safe => 1,
  90. rebuild => 1,
  91. },
  92. } #}}}
  93. sub islanguagecode ($) { #{{{
  94. my $code=shift;
  95. return ($code =~ /^[a-z]{2}$/);
  96. } #}}}
  97. sub checkconfig () { #{{{
  98. foreach my $field (qw{po_master_language po_slave_languages}) {
  99. if (! exists $config{$field} || ! defined $config{$field}) {
  100. error(sprintf(gettext("Must specify %s"), $field));
  101. }
  102. }
  103. if (! (keys %{$config{po_slave_languages}})) {
  104. error(gettext("At least one slave language must be defined in po_slave_languages"));
  105. }
  106. map {
  107. islanguagecode($_)
  108. or error(sprintf(gettext("%s is not a valid language code"), $_));
  109. } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
  110. if (! exists $config{po_translatable_pages} ||
  111. ! defined $config{po_translatable_pages}) {
  112. $config{po_translatable_pages}="";
  113. }
  114. if (! exists $config{po_link_to} ||
  115. ! defined $config{po_link_to}) {
  116. $config{po_link_to}='default';
  117. }
  118. elsif (! grep {
  119. $config{po_link_to} eq $_
  120. } ('default', 'current', 'negotiated')) {
  121. warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
  122. $config{po_link_to}));
  123. $config{po_link_to}='default';
  124. }
  125. elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  126. warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
  127. $config{po_link_to}='default';
  128. }
  129. push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
  130. } #}}}
  131. sub potfile ($) { #{{{
  132. my $masterfile=shift;
  133. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  134. $dir='' if $dir eq './';
  135. return File::Spec->catpath('', $dir, $name . ".pot");
  136. } #}}}
  137. sub pofile ($$) { #{{{
  138. my $masterfile=shift;
  139. my $lang=shift;
  140. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  141. $dir='' if $dir eq './';
  142. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  143. } #}}}
  144. sub pofiles ($) { #{{{
  145. my $masterfile=shift;
  146. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  147. } #}}}
  148. sub refreshpot ($) { #{{{
  149. my $masterfile=shift;
  150. my $potfile=potfile($masterfile);
  151. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  152. my $doc=Locale::Po4a::Chooser::new('text',%options);
  153. $doc->read($masterfile);
  154. $doc->{TT}{utf_mode} = 1;
  155. $doc->{TT}{file_in_charset} = 'utf-8';
  156. $doc->{TT}{file_out_charset} = 'utf-8';
  157. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  158. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  159. # compulsory since this module prevents us from using the porefs option.
  160. my %po_options = ('porefs' => 'none');
  161. $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
  162. $doc->{TT}{po_out}->set_charset('utf-8');
  163. # do the actual work
  164. $doc->parse;
  165. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  166. $doc->writepo($potfile);
  167. } #}}}
  168. sub refreshpofiles ($@) { #{{{
  169. my $masterfile=shift;
  170. my @pofiles=@_;
  171. my $potfile=potfile($masterfile);
  172. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  173. foreach my $pofile (@pofiles) {
  174. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  175. if (-e $pofile) {
  176. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  177. or error("[po/refreshpofiles:$pofile] failed to update");
  178. }
  179. else {
  180. File::Copy::syscopy($potfile,$pofile)
  181. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  182. }
  183. }
  184. } #}}}
  185. sub needsbuild () { #{{{
  186. my $needsbuild=shift;
  187. # backup @needsbuild content so that change() can know whether
  188. # a given master page was rendered because its source file was changed
  189. @origneedsbuild=(@$needsbuild);
  190. # build %translations, using istranslation's side-effect
  191. map istranslation($_), (keys %pagesources);
  192. # make existing translations depend on the corresponding master page
  193. foreach my $master (keys %translations) {
  194. foreach my $slave (values %{$translations{$master}}) {
  195. add_depends($slave, $master);
  196. }
  197. }
  198. } #}}}
  199. sub scan (@) { #{{{
  200. my %params=@_;
  201. my $page=$params{page};
  202. my $content=$params{content};
  203. return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
  204. if (istranslation($page)) {
  205. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  206. foreach my $destpage (@{$links{$page}}) {
  207. if (istranslatable($destpage)) {
  208. # replace one occurence of $destpage in $links{$page}
  209. # (we only want to replace the one that was added by
  210. # IkiWiki::Plugin::link::scan, other occurences may be
  211. # there for other reasons)
  212. for (my $i=0; $i<@{$links{$page}}; $i++) {
  213. if (@{$links{$page}}[$i] eq $destpage) {
  214. @{$links{$page}}[$i] = $destpage . '.' . $curlang;
  215. last;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. elsif (! istranslatable($page) && ! istranslation($page)) {
  222. foreach my $destpage (@{$links{$page}}) {
  223. if (istranslatable($destpage)) {
  224. map {
  225. push @{$links{$page}}, $destpage . '.' . $_;
  226. } (keys %{$config{po_slave_languages}});
  227. }
  228. }
  229. }
  230. } #}}}
  231. sub mytargetpage ($$) { #{{{
  232. my $page=shift;
  233. my $ext=shift;
  234. if (istranslation($page)) {
  235. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  236. if (! $config{usedirs} || $masterpage eq 'index') {
  237. return $masterpage . "." . $lang . "." . $ext;
  238. }
  239. else {
  240. return $masterpage . "/index." . $lang . "." . $ext;
  241. }
  242. }
  243. elsif (istranslatable($page)) {
  244. if (! $config{usedirs} || $page eq 'index') {
  245. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  246. }
  247. else {
  248. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  249. }
  250. }
  251. return $origsubs{'targetpage'}->($page, $ext);
  252. } #}}}
  253. sub mybeautify_urlpath ($) { #{{{
  254. my $url=shift;
  255. my $res=$origsubs{'beautify_urlpath'}->($url);
  256. if ($config{po_link_to} eq "negotiated") {
  257. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  258. }
  259. return $res;
  260. } #}}}
  261. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  262. my $to=shift;
  263. my $from=shift;
  264. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  265. my $res=urlto($to, $from);
  266. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  267. return $res;
  268. } #}}}
  269. sub myurlto ($$;$) { #{{{
  270. my $to=shift;
  271. my $from=shift;
  272. my $absolute=shift;
  273. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  274. if (! length $to
  275. && $config{po_link_to} eq "current"
  276. && istranslation($from)
  277. && istranslatable('index')) {
  278. my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
  279. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
  280. }
  281. return $origsubs{'urlto'}->($to,$from,$absolute);
  282. } #}}}
  283. sub mybestlink ($$) { #{{{
  284. my $page=shift;
  285. my $link=shift;
  286. my $res=$origsubs{'bestlink'}->($page, $link);
  287. if (length $res) {
  288. if ($config{po_link_to} eq "current"
  289. && istranslatable($res)
  290. && istranslation($page)) {
  291. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  292. return $res . "." . $curlang;
  293. }
  294. else {
  295. return $res;
  296. }
  297. }
  298. return "";
  299. } #}}}
  300. # We use filter to convert PO to the master page's format,
  301. # since the rest of ikiwiki should not work on PO files.
  302. sub filter (@) { #{{{
  303. my %params = @_;
  304. my $page = $params{page};
  305. my $destpage = $params{destpage};
  306. my $content = decode_utf8(encode_utf8($params{content}));
  307. return $content if ( ! istranslation($page)
  308. || ( exists $filtered{$page}{$destpage}
  309. && $filtered{$page}{$destpage} eq 1 ));
  310. # CRLF line terminators make poor Locale::Po4a feel bad
  311. $content=~s/\r\n/\n/g;
  312. # Implementation notes
  313. #
  314. # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
  315. # to learn how to disguise a variable as a file.
  316. # 2. There are incompatibilities between some File::Temp versions
  317. # (including 0.18, bundled with Lenny's perl-modules package)
  318. # and others (e.g. 0.20, previously present in the archive as
  319. # a standalone package): under certain circumstances, some
  320. # return a relative filename, whereas others return an absolute one;
  321. # we here use this module in a way that is at least compatible
  322. # with 0.18 and 0.20. Beware, hit'n'run refactorers!
  323. my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
  324. DIR => File::Spec->tmpdir,
  325. UNLINK => 1)->filename;
  326. my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
  327. DIR => File::Spec->tmpdir,
  328. UNLINK => 1)->filename;
  329. writefile(basename($infile), File::Spec->tmpdir, $content);
  330. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  331. my $masterfile = srcfile($pagesources{$masterpage});
  332. my (@pos,@masters);
  333. push @pos,$infile;
  334. push @masters,$masterfile;
  335. my %options = (
  336. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  337. );
  338. my $doc=Locale::Po4a::Chooser::new('text',%options);
  339. $doc->process(
  340. 'po_in_name' => \@pos,
  341. 'file_in_name' => \@masters,
  342. 'file_in_charset' => 'utf-8',
  343. 'file_out_charset' => 'utf-8',
  344. ) or error("[po/filter:$infile]: failed to translate");
  345. $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
  346. $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
  347. # Unlinking should happen automatically, thanks to File::Temp,
  348. # but it does not work here, probably because of the way writefile()
  349. # and Locale::Po4a::write() work.
  350. unlink $infile, $outfile;
  351. $filtered{$page}{$destpage}=1;
  352. return $content;
  353. } #}}}
  354. sub htmlize (@) { #{{{
  355. my %params=@_;
  356. my $page = $params{page};
  357. my $content = $params{content};
  358. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  359. my $masterfile = srcfile($pagesources{$masterpage});
  360. # force content to be htmlize'd as if it was the same type as the master page
  361. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  362. } #}}}
  363. sub percenttranslated ($) { #{{{
  364. my $page=shift;
  365. return gettext("N/A") unless (istranslation($page));
  366. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  367. my $file=srcfile($pagesources{$page});
  368. my $masterfile = srcfile($pagesources{$masterpage});
  369. my (@pos,@masters);
  370. push @pos,$file;
  371. push @masters,$masterfile;
  372. my %options = (
  373. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  374. );
  375. my $doc=Locale::Po4a::Chooser::new('text',%options);
  376. $doc->process(
  377. 'po_in_name' => \@pos,
  378. 'file_in_name' => \@masters,
  379. 'file_in_charset' => 'utf-8',
  380. 'file_out_charset' => 'utf-8',
  381. ) or error("[po/percenttranslated:$file]: failed to translate");
  382. my ($percent,$hit,$queries) = $doc->stats();
  383. return $percent;
  384. } #}}}
  385. sub otherlanguages ($) { #{{{
  386. my $page=shift;
  387. my @ret;
  388. if (istranslatable($page)) {
  389. foreach my $lang (sort keys %{$translations{$page}}) {
  390. my $translation = $translations{$page}{$lang};
  391. push @ret, {
  392. url => urlto($translation, $page),
  393. code => $lang,
  394. language => $config{po_slave_languages}{$lang},
  395. percent => percenttranslated($translation),
  396. };
  397. }
  398. }
  399. elsif (istranslation($page)) {
  400. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  401. push @ret, {
  402. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  403. code => $config{po_master_language}{code},
  404. language => $config{po_master_language}{name},
  405. master => 1,
  406. };
  407. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  408. push @ret, {
  409. url => urlto($translations{$masterpage}{$lang}, $page),
  410. code => $lang,
  411. language => $config{po_slave_languages}{$lang},
  412. percent => percenttranslated($translations{$masterpage}{$lang}),
  413. } unless ($lang eq $curlang);
  414. }
  415. }
  416. return @ret;
  417. } #}}}
  418. sub pagetemplate (@) { #{{{
  419. my %params=@_;
  420. my $page=$params{page};
  421. my $destpage=$params{destpage};
  422. my $template=$params{template};
  423. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
  424. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  425. $template->param(percenttranslated => percenttranslated($page));
  426. }
  427. if ($template->query(name => "istranslation")) {
  428. $template->param(istranslation => istranslation($page));
  429. }
  430. if ($template->query(name => "istranslatable")) {
  431. $template->param(istranslatable => istranslatable($page));
  432. }
  433. if ($template->query(name => "otherlanguages")) {
  434. $template->param(otherlanguages => [otherlanguages($page)]);
  435. if (istranslatable($page)) {
  436. foreach my $translation (values %{$translations{$page}}) {
  437. add_depends($page, $translation);
  438. }
  439. }
  440. elsif (istranslation($page)) {
  441. add_depends($page, $masterpage);
  442. foreach my $translation (values %{$translations{$masterpage}}) {
  443. add_depends($page, $translation);
  444. }
  445. }
  446. }
  447. # Rely on IkiWiki::Render's genpage() to decide wether
  448. # a discussion link should appear on $page; this is not
  449. # totally accurate, though: some broken links may be generated
  450. # when cgiurl is disabled.
  451. # This compromise avoids some code duplication, and will probably
  452. # prevent future breakage when ikiwiki internals change.
  453. # Known limitations are preferred to future random bugs.
  454. if ($template->param('discussionlink') && istranslation($page)) {
  455. $template->param('discussionlink' => htmllink(
  456. $page,
  457. $destpage,
  458. $masterpage . '/' . gettext("Discussion"),
  459. noimageinline => 1,
  460. forcesubpage => 0,
  461. linktext => gettext("Discussion"),
  462. ));
  463. }
  464. # remove broken parentlink to ./index.html on home page's translations
  465. if ($template->param('parentlinks')
  466. && istranslation($page)
  467. && $masterpage eq "index") {
  468. $template->param('parentlinks' => []);
  469. }
  470. } # }}}
  471. sub change(@) { #{{{
  472. my @rendered=@_;
  473. my $updated_po_files=0;
  474. # Refresh/create POT and PO files as needed.
  475. foreach my $page (map pagename($_), @rendered) {
  476. next unless istranslatable($page);
  477. my $file=srcfile($pagesources{$page});
  478. my $updated_pot_file=0;
  479. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  480. || ! -e potfile($file)) {
  481. refreshpot($file);
  482. $updated_pot_file=1;
  483. }
  484. my @pofiles;
  485. foreach my $lang (keys %{$config{po_slave_languages}}) {
  486. my $pofile=pofile($file, $lang);
  487. if ($updated_pot_file || ! -e $pofile) {
  488. push @pofiles, $pofile;
  489. }
  490. }
  491. if (@pofiles) {
  492. refreshpofiles($file, @pofiles);
  493. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  494. $updated_po_files=1;
  495. }
  496. }
  497. if ($updated_po_files) {
  498. # Check staged changes in.
  499. if ($config{rcs}) {
  500. IkiWiki::disable_commit_hook();
  501. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  502. "IkiWiki::Plugin::po::change", "127.0.0.1");
  503. IkiWiki::enable_commit_hook();
  504. IkiWiki::rcs_update();
  505. }
  506. # Reinitialize module's private variables.
  507. undef %filtered;
  508. undef %translations;
  509. # Trigger a wiki refresh.
  510. require IkiWiki::Render;
  511. IkiWiki::refresh();
  512. IkiWiki::saveindex();
  513. }
  514. } #}}}
  515. sub editcontent () { #{{{
  516. my %params=@_;
  517. # as we're previewing or saving a page, the content may have
  518. # changed, so tell the next filter() invocation it must not be lazy
  519. if (exists $filtered{$params{page}}{$params{page}}) {
  520. delete $filtered{$params{page}}{$params{page}};
  521. }
  522. return $params{content};
  523. } #}}}
  524. sub istranslatable ($) { #{{{
  525. my $page=shift;
  526. my $file=$pagesources{$page};
  527. if (! defined $file
  528. || (defined pagetype($file) && pagetype($file) eq 'po')
  529. || $file =~ /\.pot$/) {
  530. return 0;
  531. }
  532. return pagespec_match($page, $config{po_translatable_pages});
  533. } #}}}
  534. sub _istranslation ($) { #{{{
  535. my $page=shift;
  536. my $file=$pagesources{$page};
  537. if (! defined $file) {
  538. return IkiWiki::FailReason->new("no file specified");
  539. }
  540. if (! defined $file
  541. || ! defined pagetype($file)
  542. || ! pagetype($file) eq 'po'
  543. || $file =~ /\.pot$/) {
  544. return 0;
  545. }
  546. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  547. if (! defined $masterpage || ! defined $lang
  548. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  549. || ! defined $pagesources{$masterpage}
  550. || ! defined $config{po_slave_languages}{$lang}) {
  551. return 0;
  552. }
  553. return istranslatable($masterpage);
  554. } #}}}
  555. sub istranslation ($) { #{{{
  556. my $page=shift;
  557. if (_istranslation($page)) {
  558. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  559. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  560. return 1;
  561. }
  562. return 0;
  563. } #}}}
  564. package IkiWiki::PageSpec;
  565. use warnings;
  566. use strict;
  567. use IkiWiki 2.00;
  568. sub match_istranslation ($;@) { #{{{
  569. my $page=shift;
  570. if (IkiWiki::Plugin::po::istranslation($page)) {
  571. return IkiWiki::SuccessReason->new("is a translation page");
  572. }
  573. else {
  574. return IkiWiki::FailReason->new("is not a translation page");
  575. }
  576. } #}}}
  577. sub match_istranslatable ($;@) { #{{{
  578. my $page=shift;
  579. if (IkiWiki::Plugin::po::istranslatable($page)) {
  580. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  581. }
  582. else {
  583. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  584. }
  585. } #}}}
  586. sub match_lang ($$;@) { #{{{
  587. my $page=shift;
  588. my $wanted=shift;
  589. my $regexp=IkiWiki::glob2re($wanted);
  590. my $lang;
  591. my $masterpage;
  592. if (IkiWiki::Plugin::po::istranslation($page)) {
  593. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  594. }
  595. else {
  596. $lang = $config{po_master_language}{code};
  597. }
  598. if ($lang!~/^$regexp$/i) {
  599. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  600. }
  601. else {
  602. return IkiWiki::SuccessReason->new("file language is $wanted");
  603. }
  604. } #}}}
  605. sub match_currentlang ($$;@) { #{{{
  606. my $page=shift;
  607. shift;
  608. my %params=@_;
  609. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  610. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  611. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  612. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  613. }
  614. else {
  615. $currentlang = $config{po_master_language}{code};
  616. }
  617. if (IkiWiki::Plugin::po::istranslation($page)) {
  618. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  619. }
  620. else {
  621. $lang = $config{po_master_language}{code};
  622. }
  623. if ($lang eq $currentlang) {
  624. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  625. }
  626. else {
  627. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  628. }
  629. } #}}}
  630. 1