Skip to content

Commit

Permalink
Merge branch 'release/v1.03'
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed May 13, 2015
2 parents b84177b + 75a0619 commit 879825f
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 12 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Currently supported backends:

With [Composer](http://getcomposer.org/):

$ composer require mekras/php-speller:~1.00
$ composer require mekras/php-speller:~1.03

## Usage

Expand Down Expand Up @@ -64,3 +64,25 @@ $speller = new Hunspell();
$speller->setDictionaryPath('/my_app/spelling');
$speller->setCustomDictionaries(['tech', 'titles']);
```

## Sources

Sources — is an abstraction layer allowing spellers receive text from different sources like strings
or files.

Supported sources:

* [StringSource](src/Source/StringSource.php) — simple PHP string;
* [FileSource](src/Source/FileSource.php) — generic file source;
* [XliffSource](src/Source/XliffSource.php)
[XLIFF](http://docs.oasis-open.org/xliff/xliff-core/v2.0/xliff-core-v2.0.html) files.

## Filters

Filters used internally to filter out all non text contents received from source. In order to save
original word location (line and column numbers) all filters replaces non text content with spaces.

Available filters:

* [StripAllFilter](src/Source/Filter/StripAllFilter.php) — strips all input text;
* [HtmlFilter](src/Source/Filter/HtmlFilter.php) — strips HTML tags.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.02
1.03
135 changes: 135 additions & 0 deletions src/Source/Filter/HtmlFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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 replaces HTML tags with spaces
*
* @since 1.03
*/
class HtmlFilter implements Filter
{
/**
* Attrs with text contents
*
* @var string[]
*/
private $textAttrs = ['title'];

/**
* Filter string
*
* @param string $string string to be filtered
*
* @return string filtered string
*
* @since 1.03
*/
public function filter($string)
{
$result = '';

// Current/last tag name
$tagName = null;
// Current/last attribute name
$attrName = null;
// Current context
$context = null;
// Expected context
$expecting = null;

$length = mb_strlen($string);
for ($i = 0; $i < $length; $i++) {
$ch = mb_substr($string, $i, 1);
switch ($ch) {

case '<':
$context = 'tag_name';
$tagName = null;
$ch = ' ';
break;

case '>':
$context = null;
$expecting = null;
$ch = ' ';
break;

case ' ':
case "\n":
case "\t":
switch ($context) {

case 'tag_name':
$context = 'tag_attrs';
break;

case 'attr_name':
$context = 'tag_attrs';
break;
}
break;

case '=':
if ('attr_name' === $context || 'tag_attrs' === $context) {
$expecting = 'attr_value';
$ch = ' ';
}
break;

case '"':
case "'":
switch (true) {

case 'attr_value' === $expecting:
$context = 'attr_value';
if (in_array(strtolower($attrName), $this->textAttrs, true)) {
$context = 'attr_text';
}
$expecting = null;
$ch = ' ';
break;

case 'attr_value' === $context:
case 'attr_text' === $context:
$context = 'tag_attrs';
$ch = ' ';
break;
}
break;

default:
switch ($context) {

case 'tag_name':
$tagName .= $ch;
$ch = ' ';
break;

/** @noinspection PhpMissingBreakStatementInspection */
case 'tag_attrs':
$context = 'attr_name';
$attrName = null;
// no break needed
case 'attr_name':
$attrName .= $ch;
$ch = ' ';
break;

case 'attr_value':
$ch = ' ';
break;
}
}
$result .= $ch;
}

return $result;
}
}
15 changes: 5 additions & 10 deletions src/Source/XliffSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Mekras\Speller\Source;

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

/**
Expand Down Expand Up @@ -57,20 +58,14 @@ public function getAsString()
$text = parent::getAsString();

$stripAll = new StripAllFilter();
$htmlFilter = new HtmlFilter();

/* 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
);
function ($match) use ($htmlFilter) {
$string = $htmlFilter->filter($match[1]);
// <![CDATA[ ]]
return ' ' . $string . ' ';
},
$text
Expand Down
31 changes: 31 additions & 0 deletions tests/Source/Filter/HtmlFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\HtmlFilter;
use PHPUnit_Framework_TestCase as TestCase;

/**
* Tests for Mekras\Speller\Source\Filter\HtmlFilter
*
* @covers Mekras\Speller\Source\Filter\HtmlFilter
*/
class HtmlFilterTest extends TestCase
{
/**
* Test basic functional
*/
public function testBasics()
{
$filter = new HtmlFilter();
$html = "<br>foo <a\nhref = '#' title='bar'>\nbaz</a>";
$text = " foo \n bar \nbaz ";
static::assertEquals($text, $filter->filter($html));
}
}

0 comments on commit 879825f

Please sign in to comment.