Merge branch 'bug-540' into release
[cacert-devel.git] / locale / escape_special_chars.php
1 #!/usr/bin/php -q
2 <?php
3 /*
4 LibreSSL - CAcert web application
5 Copyright (C) 2004-2012 CAcert Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 of the License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* Convert special characters in UTF-8 encoded PO files to HTML entities */
22
23 define('MSGSTR', 'msgstr');
24 define('MSGSTR_LEN', strlen(MSGSTR));
25 define('MSGID', 'msgid');
26 define('MSGID_LEN', strlen(MSGID));
27
28 function is_msgstr($line) {
29 if (strlen($line) < MSGSTR_LEN) {
30 return false;
31 }
32
33 return substr_compare($line, MSGSTR, 0, MSGSTR_LEN) === 0;
34 }
35
36 function is_msgid($line) {
37 if (strlen($line) < MSGID_LEN) {
38 return false;
39 }
40
41 return substr_compare($line, MSGID, 0, MSGID_LEN) === 0;
42 }
43
44 // Skip the metadata (first msgid/msgstr pair)
45 while (!feof(STDIN)) {
46 $line = fgets(STDIN);
47
48 echo $line;
49
50 if (is_msgstr($line)) {
51 break;
52 }
53 }
54
55 // determines if the current line belongs to a msgid or a msgstr
56 $msgstr = false;
57
58 while (!feof(STDIN)) {
59 $line = fgets(STDIN);
60
61 if (is_msgstr($line)) {
62 $msgstr = true;
63 } elseif (is_msgid($line)) {
64 $msgstr = false;
65 }
66
67 if ($msgstr) {
68 $line = htmlentities($line, ENT_NOQUOTES, "UTF-8");
69 }
70 echo $line;
71 }