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