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