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