Skip to content

Commit

Permalink
Merge branch 'release/v1.02'
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed May 12, 2015
2 parents 6de6ff6 + 7f3d72b commit 7fdef8b
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 6 deletions.
3 changes: 0 additions & 3 deletions .idea/dictionaries/mekras.xml

This file was deleted.

2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.01
1.02
3 changes: 2 additions & 1 deletion src/Hunspell/Hunspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function checkText(Source $source, array $languages)

$process = $this->createProcess(
[
'-a',
'-i UTF-8', // Input encoding
'-a', // Machine readable output
'-d ' . implode(',', $dictionaries)
]
);
Expand Down
63 changes: 63 additions & 0 deletions src/Source/FileSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Source;

/**
* File as text source
*
* @since 1.02
*/
class FileSource implements Source
{
/**
* File name
*
* @var string
* @since 1.02
*/
protected $filename;

/**
* Create new source
*
* @param string $filename
*
* @since 1.02
*/
public function __construct($filename)
{
$this->filename = $filename;
}

/**
* Return text as one string
*
* @return string
*
* @since 1.02
*/
public function getAsString()
{
return file_get_contents($this->filename);
}

/**
* Return file name with text to check
*
* This can be used for backends with file checking support
*
* @return string
*
* @since 1.02
*/
public function getFilename()
{
return (string) $this->filename;
}
}
30 changes: 30 additions & 0 deletions src/Source/Filter/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Source\Filter;

/**
* Filter interface
*
* Filters are used to filter out text, which does not require checking
*
* @since 1.02
*/
interface Filter
{
/**
* Filter string
*
* @param string $string string to be filtered
*
* @return string filtered string
*
* @since 1.02
*/
public function filter($string);
}
33 changes: 33 additions & 0 deletions src/Source/Filter/StripAllFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Source\Filter;

/**
* Filter which strips all input text
*
* All characters except new lines (\n), tabs (\t) and spaces will be replaces with spaces.
*
* @since 1.02
*/
class StripAllFilter implements Filter
{
/**
* Filter string
*
* @param string $string string to be filtered
*
* @return string filtered string
*
* @since 1.02
*/
public function filter($string)
{
return preg_replace('/\S/', ' ', $string);
}
}
110 changes: 110 additions & 0 deletions src/Source/XliffSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Source;

use Mekras\Speller\Source\Filter\Filter;
use Mekras\Speller\Source\Filter\StripAllFilter;

/**
* XLIFF translations as text source
*
* @link http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html
* @since 1.02
*/
class XliffSource extends FileSource
{
/**
* Text filters
*
* @var Filter[]|null[]
*/
private $filters = [
'#<!--.*?-->#ums' => null, // Comments
'#<[^>]+>#ums' => null // Top level tags
];

/**
* 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.02
*/
public function addFilter($pattern, Filter $filter = null)
{
$this->filters[(string) $pattern] = $filter;
}

/**
* Return text as one string
*
* @return string
*
* @since 1.02
*/
public function getAsString()
{
$text = parent::getAsString();

$stripAll = new StripAllFilter();

/* Removing CDATA tags */
$text = preg_replace_callback(
'#<!\[CDATA\[(.*?)\]\]>#ums',
function ($match) use ($stripAll) {
$string = $match[1];
$string = preg_replace_callback(
'#(<[^>]+>)([^<]*)(</[^>]+>)#',
function ($match) use ($stripAll) {
return $stripAll->filter($match[1]) . $match[2]
. $stripAll->filter($match[3]);
},
$string
);
return ' ' . $string . ' ';
},
$text
);

/* Processing bottom level tags */
$text = preg_replace_callback(
'#(<(\w+)(\s[^>]*?)?>)([^<>]*)(</\w+\s*>)#ums',
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;
}
}
2 changes: 1 addition & 1 deletion tests/Hunspell/fixtures/hunspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
exit(0);
}

if ($argc > 1 && '-a' === $argv[1]) {
if (in_array('-a', $argv, true)) {
fwrite(STDOUT, file_get_contents(__DIR__ . '/check.txt'));
exit(0);
}
29 changes: 29 additions & 0 deletions tests/Source/FileSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Tests\Source;

use Mekras\Speller\Source\FileSource;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Tests for Mekras\Speller\Source\FileSource
*
* @covers Mekras\Speller\Source\FileSource
*/
class FileSourceTest extends TestCase
{
/**
* Test basic functions
*/
public function testBasics()
{
$source = new FileSource(__DIR__ . '/fixtures/test.txt');
static::assertEquals('Tiger, tiger, burning bright', $source->getAsString());
}
}
29 changes: 29 additions & 0 deletions tests/Source/Filter/StripAllFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Tests\Source\Filter;

use Mekras\Speller\Source\Filter\StripAllFilter;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Tests for Mekras\Speller\Source\Filter\StripAllFilter
*
* @covers Mekras\Speller\Source\Filter\StripAllFilter
*/
class StripAllFilterTest extends TestCase
{
/**
* Test basic functional
*/
public function testBasics()
{
$filter = new StripAllFilter();
static::assertEquals(" \n\t ", $filter->filter("foo\n\tbar"));
}
}
36 changes: 36 additions & 0 deletions tests/Source/XliffSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* PHP Speller
*
* @copyright 2015, Михаил Красильников <m.krasilnikov@yandex.ru>
* @author Михаил Красильников <m.krasilnikov@yandex.ru>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mekras\Speller\Tests\Source;

use Mekras\Speller\Source\XliffSource;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Tests for Mekras\Speller\Source\XliffSource
*
* @covers Mekras\Speller\Source\XliffSource
*/
class XliffSourceTest extends TestCase
{
/**
* Test basic functional
*/
public function testBasics()
{
$source = new XliffSource(__DIR__ . '/fixtures/test.xliff');
$source->addFilter('#\{\{[^}]+\}\}#ums');
$lines = explode("\n", $source->getAsString());
static::assertCount(36, $lines);
static::assertEquals('Foo', substr($lines[7], 17, 3));
static::assertEquals('Bar', substr($lines[12], 20, 3));
static::assertEquals('Bar', substr($lines[13], 20, 3));
static::assertNotContains('var', $lines[14]);
static::assertEquals('Baz', substr($lines[27], 42, 3));
}
}
1 change: 1 addition & 0 deletions tests/Source/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tiger, tiger, burning bright
Loading

0 comments on commit 7fdef8b

Please sign in to comment.