-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathXliffSource.php
133 lines (118 loc) · 3.54 KB
/
XliffSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* PHP Speller.
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Mekras\Speller\Source;
use Mekras\Speller\Source\Filter\Filter;
use Mekras\Speller\Source\Filter\HtmlFilter;
use Mekras\Speller\Source\Filter\StripAllFilter;
/**
* XLIFF translations as text source.
*
* @since 1.6 derived from MetaSource.
* @since 1.2
*
* @link http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html
*/
class XliffSource extends MetaSource
{
/**
* Text filters
*
* @var Filter[]|null[]
*/
private $filters = [
'#<!--.*?-->#ums' => null, // Comments
'#<[^>]+>#ums' => null // Top level tags
];
/**
* Create text source from XLIFF document.
*
* @param EncodingAwareSource|string $source Source of filename
*
* @throws \Mekras\Speller\Exception\SourceException
*
* @since 1.6 Accepts EncodingAwareSource
*
* @todo deprecate string $source in version 2.0
*/
public function __construct($source)
{
if (!$source instanceof EncodingAwareSource) {
$source = new FileSource($source);
}
parent::__construct($source);
}
/**
* Add custom pattern to be filtered.
*
* Matched text will be filtered with a given filter or with
* {@link Mekras\Speller\Source\Filter\StripAllFilter} if $filter is null.
*
* @param string $pattern PCRE pattern. It is recommended to use "ums" PCRE modifiers.
* @param Filter $filter Filter to be applied.
*
* @since 1.2
*/
public function addFilter(string $pattern, Filter $filter = null): void
{
$this->filters[$pattern] = $filter;
}
/**
* Return text as one string
*
* @return string
*
* @throws \Mekras\Speller\Exception\SourceException
* @since 1.2
*/
public function getAsString(): string
{
$text = $this->source->getAsString();
$stripAll = new StripAllFilter();
$htmlFilter = new HtmlFilter();
/* Removing CDATA tags */
$text = preg_replace_callback(
'#<!\[CDATA\[(.*?)\]\]>#ums',
function ($match) use ($htmlFilter) {
$string = $htmlFilter->filter($match[1]);
// <![CDATA[ ]]
return ' ' . $string . ' ';
},
$text
);
/* Processing bottom level tags */
$text = preg_replace_callback(
'#(<(\w+)(\s[^>]*?)?>)([^<>]*)(</\w+\s*>)#um',
function ($match) use ($stripAll) {
if (strtolower($match[2]) === 'target') {
$replace = $stripAll->filter($match[1]) . $match[4]
. $stripAll->filter($match[5]);
} else {
$replace = $stripAll->filter($match[0]);
}
return $replace;
},
$text
);
/* Other replacements */
foreach ($this->filters as $pattern => $filter) {
if (null === $filter) {
$filter = $stripAll;
}
$text = preg_replace_callback(
$pattern,
function ($match) use ($filter) {
return $filter->filter($match[0]);
},
$text
);
}
return $text;
}
}