summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: acc133042829a609f1af26ac5041770e41a2247e (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. return unless $config{'po_link_to'} eq 'negotiated';
  205. if (istranslation($page)) {
  206. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  207. foreach my $destpage (@{$links{$page}}) {
  208. if (istranslatable($destpage)) {
  209. # replace one occurence of $destpage in $links{$page}
  210. # (we only want to replace the one that was added by
  211. # IkiWiki::Plugin::link::scan, other occurences may be
  212. # there for other reasons)
  213. for (my $i=0; $i<@{$links{$page}}; $i++) {
  214. if (@{$links{$page}}[$i] eq $destpage) {
  215. @{$links{$page}}[$i] = $destpage . '.' . $curlang;
  216. last;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. elsif (! istranslatable($page) && ! istranslation($page)) {
  223. foreach my $destpage (@{$links{$page}}) {
  224. if (istranslatable($destpage)) {
  225. map {
  226. push @{$links{$page}}, $destpage . '.' . $_;
  227. } (keys %{$config{po_slave_languages}});
  228. }
  229. }
  230. }
  231. } #}}}
  232. sub mytargetpage ($$) { #{{{
  233. my $page=shift;
  234. my $ext=shift;
  235. if (istranslation($page)) {
  236. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  237. if (! $config{usedirs} || $masterpage eq 'index') {
  238. return $masterpage . "." . $lang . "." . $ext;
  239. }
  240. else {
  241. return $masterpage . "/index." . $lang . "." . $ext;
  242. }
  243. }
  244. elsif (istranslatable($page)) {
  245. if (! $config{usedirs} || $page eq 'index') {
  246. return $page . "." . $config{po_master_language}{code} . "." . $ext;
  247. }
  248. else {
  249. return $page . "/index." . $config{po_master_language}{code} . "." . $ext;
  250. }
  251. }
  252. return $origsubs{'targetpage'}->($page, $ext);
  253. } #}}}
  254. sub mybeautify_urlpath ($) { #{{{
  255. my $url=shift;
  256. my $res=$origsubs{'beautify_urlpath'}->($url);
  257. if ($config{po_link_to} eq "negotiated") {
  258. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  259. }
  260. return $res;
  261. } #}}}
  262. sub urlto_with_orig_beautiful_urlpath($$) { #{{{
  263. my $to=shift;
  264. my $from=shift;
  265. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  266. my $res=urlto($to, $from);
  267. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  268. return $res;
  269. } #}}}
  270. sub myurlto ($$;$) { #{{{
  271. my $to=shift;
  272. my $from=shift;
  273. my $absolute=shift;
  274. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  275. if (! length $to
  276. && $config{po_link_to} eq "current"
  277. && istranslation($from)
  278. && istranslatable('index')) {
  279. my ($masterpage, $curlang) = ($from =~ /(.*)[.]([a-z]{2})$/);
  280. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . $curlang . ".$config{htmlext}");
  281. }
  282. return $origsubs{'urlto'}->($to,$from,$absolute);
  283. } #}}}
  284. sub mybestlink ($$) { #{{{
  285. my $page=shift;
  286. my $link=shift;
  287. my $res=$origsubs{'bestlink'}->($page, $link);
  288. if (length $res) {
  289. if ($config{po_link_to} eq "current"
  290. && istranslatable($res)
  291. && istranslation($page)) {
  292. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  293. return $res . "." . $curlang;
  294. }
  295. else {
  296. return $res;
  297. }
  298. }
  299. return "";
  300. } #}}}
  301. # We use filter to convert PO to the master page's format,
  302. # since the rest of ikiwiki should not work on PO files.
  303. sub filter (@) { #{{{
  304. my %params = @_;
  305. my $page = $params{page};
  306. my $destpage = $params{destpage};
  307. my $content = decode_utf8(encode_utf8($params{content}));
  308. return $content if ( ! istranslation($page)
  309. || ( exists $filtered{$page}{$destpage}
  310. && $filtered{$page}{$destpage} eq 1 ));
  311. # CRLF line terminators make poor Locale::Po4a feel bad
  312. $content=~s/\r\n/\n/g;
  313. # Implementation notes
  314. #
  315. # 1. Locale::Po4a reads/writes from/to files, and I'm too lazy
  316. # to learn how to disguise a variable as a file.
  317. # 2. There are incompatibilities between some File::Temp versions
  318. # (including 0.18, bundled with Lenny's perl-modules package)
  319. # and others (e.g. 0.20, previously present in the archive as
  320. # a standalone package): under certain circumstances, some
  321. # return a relative filename, whereas others return an absolute one;
  322. # we here use this module in a way that is at least compatible
  323. # with 0.18 and 0.20. Beware, hit'n'run refactorers!
  324. my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
  325. DIR => File::Spec->tmpdir,
  326. UNLINK => 1)->filename;
  327. my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
  328. DIR => File::Spec->tmpdir,
  329. UNLINK => 1)->filename;
  330. writefile(basename($infile), File::Spec->tmpdir, $content);
  331. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  332. my $masterfile = srcfile($pagesources{$masterpage});
  333. my (@pos,@masters);
  334. push @pos,$infile;
  335. push @masters,$masterfile;
  336. my %options = (
  337. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  338. );
  339. my $doc=Locale::Po4a::Chooser::new('text',%options);
  340. $doc->process(
  341. 'po_in_name' => \@pos,
  342. 'file_in_name' => \@masters,
  343. 'file_in_charset' => 'utf-8',
  344. 'file_out_charset' => 'utf-8',
  345. ) or error("[po/filter:$infile]: failed to translate");
  346. $doc->write($outfile) or error("[po/filter:$infile] could not write $outfile");
  347. $content = readfile($outfile) or error("[po/filter:$infile] could not read $outfile");
  348. # Unlinking should happen automatically, thanks to File::Temp,
  349. # but it does not work here, probably because of the way writefile()
  350. # and Locale::Po4a::write() work.
  351. unlink $infile, $outfile;
  352. $filtered{$page}{$destpage}=1;
  353. return $content;
  354. } #}}}
  355. sub htmlize (@) { #{{{
  356. my %params=@_;
  357. my $page = $params{page};
  358. my $content = $params{content};
  359. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  360. my $masterfile = srcfile($pagesources{$masterpage});
  361. # force content to be htmlize'd as if it was the same type as the master page
  362. return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
  363. } #}}}
  364. sub percenttranslated ($) { #{{{
  365. my $page=shift;
  366. return gettext("N/A") unless (istranslation($page));
  367. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  368. my $file=srcfile($pagesources{$page});
  369. my $masterfile = srcfile($pagesources{$masterpage});
  370. my (@pos,@masters);
  371. push @pos,$file;
  372. push @masters,$masterfile;
  373. my %options = (
  374. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  375. );
  376. my $doc=Locale::Po4a::Chooser::new('text',%options);
  377. $doc->process(
  378. 'po_in_name' => \@pos,
  379. 'file_in_name' => \@masters,
  380. 'file_in_charset' => 'utf-8',
  381. 'file_out_charset' => 'utf-8',
  382. ) or error("[po/percenttranslated:$file]: failed to translate");
  383. my ($percent,$hit,$queries) = $doc->stats();
  384. return $percent;
  385. } #}}}
  386. sub otherlanguages ($) { #{{{
  387. my $page=shift;
  388. my @ret;
  389. if (istranslatable($page)) {
  390. foreach my $lang (sort keys %{$translations{$page}}) {
  391. my $translation = $translations{$page}{$lang};
  392. push @ret, {
  393. url => urlto($translation, $page),
  394. code => $lang,
  395. language => $config{po_slave_languages}{$lang},
  396. percent => percenttranslated($translation),
  397. };
  398. }
  399. }
  400. elsif (istranslation($page)) {
  401. my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  402. push @ret, {
  403. url => urlto_with_orig_beautiful_urlpath($masterpage, $page),
  404. code => $config{po_master_language}{code},
  405. language => $config{po_master_language}{name},
  406. master => 1,
  407. };
  408. foreach my $lang (sort keys %{$translations{$masterpage}}) {
  409. push @ret, {
  410. url => urlto($translations{$masterpage}{$lang}, $page),
  411. code => $lang,
  412. language => $config{po_slave_languages}{$lang},
  413. percent => percenttranslated($translations{$masterpage}{$lang}),
  414. } unless ($lang eq $curlang);
  415. }
  416. }
  417. return @ret;
  418. } #}}}
  419. sub pagetemplate (@) { #{{{
  420. my %params=@_;
  421. my $page=$params{page};
  422. my $destpage=$params{destpage};
  423. my $template=$params{template};
  424. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/) if istranslation($page);
  425. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  426. $template->param(percenttranslated => percenttranslated($page));
  427. }
  428. if ($template->query(name => "istranslation")) {
  429. $template->param(istranslation => istranslation($page));
  430. }
  431. if ($template->query(name => "istranslatable")) {
  432. $template->param(istranslatable => istranslatable($page));
  433. }
  434. if ($template->query(name => "otherlanguages")) {
  435. $template->param(otherlanguages => [otherlanguages($page)]);
  436. if (istranslatable($page)) {
  437. foreach my $translation (values %{$translations{$page}}) {
  438. add_depends($page, $translation);
  439. }
  440. }
  441. elsif (istranslation($page)) {
  442. add_depends($page, $masterpage);
  443. foreach my $translation (values %{$translations{$masterpage}}) {
  444. add_depends($page, $translation);
  445. }
  446. }
  447. }
  448. # Rely on IkiWiki::Render's genpage() to decide wether
  449. # a discussion link should appear on $page; this is not
  450. # totally accurate, though: some broken links may be generated
  451. # when cgiurl is disabled.
  452. # This compromise avoids some code duplication, and will probably
  453. # prevent future breakage when ikiwiki internals change.
  454. # Known limitations are preferred to future random bugs.
  455. if ($template->param('discussionlink') && istranslation($page)) {
  456. $template->param('discussionlink' => htmllink(
  457. $page,
  458. $destpage,
  459. $masterpage . '/' . gettext("Discussion"),
  460. noimageinline => 1,
  461. forcesubpage => 0,
  462. linktext => gettext("Discussion"),
  463. ));
  464. }
  465. # remove broken parentlink to ./index.html on home page's translations
  466. if ($template->param('parentlinks')
  467. && istranslation($page)
  468. && $masterpage eq "index") {
  469. $template->param('parentlinks' => []);
  470. }
  471. } # }}}
  472. sub change(@) { #{{{
  473. my @rendered=@_;
  474. my $updated_po_files=0;
  475. # Refresh/create POT and PO files as needed.
  476. foreach my $page (map pagename($_), @rendered) {
  477. next unless istranslatable($page);
  478. my $file=srcfile($pagesources{$page});
  479. my $updated_pot_file=0;
  480. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  481. || ! -e potfile($file)) {
  482. refreshpot($file);
  483. $updated_pot_file=1;
  484. }
  485. my @pofiles;
  486. foreach my $lang (keys %{$config{po_slave_languages}}) {
  487. my $pofile=pofile($file, $lang);
  488. if ($updated_pot_file || ! -e $pofile) {
  489. push @pofiles, $pofile;
  490. }
  491. }
  492. if (@pofiles) {
  493. refreshpofiles($file, @pofiles);
  494. map { IkiWiki::rcs_add($_); } @pofiles if ($config{rcs});
  495. $updated_po_files=1;
  496. }
  497. }
  498. if ($updated_po_files) {
  499. # Check staged changes in.
  500. if ($config{rcs}) {
  501. IkiWiki::disable_commit_hook();
  502. IkiWiki::rcs_commit_staged(gettext("updated PO files"),
  503. "IkiWiki::Plugin::po::change", "127.0.0.1");
  504. IkiWiki::enable_commit_hook();
  505. IkiWiki::rcs_update();
  506. }
  507. # Reinitialize module's private variables.
  508. undef %filtered;
  509. undef %translations;
  510. # Trigger a wiki refresh.
  511. require IkiWiki::Render;
  512. IkiWiki::refresh();
  513. IkiWiki::saveindex();
  514. }
  515. } #}}}
  516. sub editcontent () { #{{{
  517. my %params=@_;
  518. # as we're previewing or saving a page, the content may have
  519. # changed, so tell the next filter() invocation it must not be lazy
  520. if (exists $filtered{$params{page}}{$params{page}}) {
  521. delete $filtered{$params{page}}{$params{page}};
  522. }
  523. return $params{content};
  524. } #}}}
  525. sub istranslatable ($) { #{{{
  526. my $page=shift;
  527. my $file=$pagesources{$page};
  528. if (! defined $file
  529. || (defined pagetype($file) && pagetype($file) eq 'po')
  530. || $file =~ /\.pot$/) {
  531. return 0;
  532. }
  533. return pagespec_match($page, $config{po_translatable_pages});
  534. } #}}}
  535. sub _istranslation ($) { #{{{
  536. my $page=shift;
  537. my $file=$pagesources{$page};
  538. if (! defined $file) {
  539. return IkiWiki::FailReason->new("no file specified");
  540. }
  541. if (! defined $file
  542. || ! defined pagetype($file)
  543. || ! pagetype($file) eq 'po'
  544. || $file =~ /\.pot$/) {
  545. return 0;
  546. }
  547. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  548. if (! defined $masterpage || ! defined $lang
  549. || ! (length($masterpage) > 0) || ! (length($lang) > 0)
  550. || ! defined $pagesources{$masterpage}
  551. || ! defined $config{po_slave_languages}{$lang}) {
  552. return 0;
  553. }
  554. return istranslatable($masterpage);
  555. } #}}}
  556. sub istranslation ($) { #{{{
  557. my $page=shift;
  558. if (_istranslation($page)) {
  559. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  560. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  561. return 1;
  562. }
  563. return 0;
  564. } #}}}
  565. package IkiWiki::PageSpec;
  566. use warnings;
  567. use strict;
  568. use IkiWiki 2.00;
  569. sub match_istranslation ($;@) { #{{{
  570. my $page=shift;
  571. if (IkiWiki::Plugin::po::istranslation($page)) {
  572. return IkiWiki::SuccessReason->new("is a translation page");
  573. }
  574. else {
  575. return IkiWiki::FailReason->new("is not a translation page");
  576. }
  577. } #}}}
  578. sub match_istranslatable ($;@) { #{{{
  579. my $page=shift;
  580. if (IkiWiki::Plugin::po::istranslatable($page)) {
  581. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  582. }
  583. else {
  584. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  585. }
  586. } #}}}
  587. sub match_lang ($$;@) { #{{{
  588. my $page=shift;
  589. my $wanted=shift;
  590. my $regexp=IkiWiki::glob2re($wanted);
  591. my $lang;
  592. my $masterpage;
  593. if (IkiWiki::Plugin::po::istranslation($page)) {
  594. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  595. }
  596. else {
  597. $lang = $config{po_master_language}{code};
  598. }
  599. if ($lang!~/^$regexp$/i) {
  600. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  601. }
  602. else {
  603. return IkiWiki::SuccessReason->new("file language is $wanted");
  604. }
  605. } #}}}
  606. sub match_currentlang ($$;@) { #{{{
  607. my $page=shift;
  608. shift;
  609. my %params=@_;
  610. my ($currentmasterpage, $currentlang, $masterpage, $lang);
  611. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  612. if (IkiWiki::Plugin::po::istranslation($params{location})) {
  613. ($currentmasterpage, $currentlang) = ($params{location} =~ /(.*)[.]([a-z]{2})$/);
  614. }
  615. else {
  616. $currentlang = $config{po_master_language}{code};
  617. }
  618. if (IkiWiki::Plugin::po::istranslation($page)) {
  619. ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  620. }
  621. else {
  622. $lang = $config{po_master_language}{code};
  623. }
  624. if ($lang eq $currentlang) {
  625. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  626. }
  627. else {
  628. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  629. }
  630. } #}}}
  631. 1