Commit dc0d90d4 authored by Aaron Parecki's avatar Aaron Parecki Committed by GitHub

Merge pull request #123 from aaronpk/master

Moves language parsing behind feature flag, and prep for v0.3.2 release
parents 84bd6efa 9138ea41
......@@ -277,6 +277,9 @@ class Parser {
public $jsonMode;
/** @var boolean Whether to include experimental language parsing in the result */
public $lang = false;
/**
* Elements upgraded to mf2 during backcompat
* @var SplObjectStorage
......@@ -811,9 +814,11 @@ class Parser {
'value' => unicodeTrim($this->innerText($e)),
);
// Language
if ( $html_lang = $this->language($e) ) {
$return['html-lang'] = $html_lang;
if($this->lang) {
// Language
if ( $html_lang = $this->language($e) ) {
$return['html-lang'] = $html_lang;
}
}
return $return;
......@@ -1073,9 +1078,11 @@ class Parser {
}
}
// Language
if ( $html_lang = $this->language($e) ) {
$return['html-lang'] = $html_lang;
if($this->lang) {
// Language
if ( $html_lang = $this->language($e) ) {
$return['html-lang'] = $html_lang;
}
}
// Make sure things are in alphabetical order
......
......@@ -229,6 +229,24 @@ $parser->parse(true, $elementIWant); // returns a document with only the Microfo
```
### Experimental Language Parsing
There is still [ongoing brainstorming](http://microformats.org/wiki/microformats2-parsing-brainstorming#Parse_language_information) around how HTML language attributes should be added to the parsed result. In order to use this feature, you will need to set a flag to opt in.
```php
$doc = '<div class="h-entry" lang="sv" id="postfrag123">
<h1 class="p-name">En svensk titel</h1>
<div class="e-content" lang="en">With an <em>english</em> summary</div>
<div class="e-content">Och <em>svensk</em> huvudtext</div>
</div>';
$parser = new Mf2\Parser($doc);
$parser->lang = true;
$result = $parser->parse();
```
Note that this option is still considered experimental and in development, and the parsed output may change between minor releases.
### Generating output for JSON serialization with JSON-mode
Due to a quirk with the way PHP arrays work, there is an edge case ([reported](https://github.com/indieweb/php-mf2/issues/29) by Tom Morris) in which a document with no rel values, when serialised as JSON, results in an empty object as the rels value rather than an empty array. Replacing this in code with a stdClass breaks PHP iteration over the values.
......@@ -289,6 +307,14 @@ Currently php-mf2 passes the majority of it’s own test case, and a good percen
### Changelog
#### v0.3.2
2017-05-27
* Fixed how the Microformats tests repo is loaded via composer
* Moved experimental language parsing feature behind an opt-in flag
* [#121](https://github.com/indieweb/php-mf2/pull/121) Fixed language detection to support parsing of HTML fragments
#### v0.3.1
2017-05-24
......
......@@ -9,20 +9,14 @@
"homepage": "http://waterpigs.co.uk"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/microformats/tests"
}
],
"bin": ["bin/fetch-mf2", "bin/parse-mf2"],
"require": {
"php": ">=5.4.0",
"microformats/test": "@dev"
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.8.*",
"phpdocumentor/phpdocumentor": "v2.8.4"
"phpdocumentor/phpdocumentor": "v2.8.4",
"mf2/tests": "@dev"
},
"autoload": {
"files": ["Mf2/Parser.php"]
......
This diff is collapsed.
......@@ -23,6 +23,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html lang="en"> <div class="h-entry">This test is in English.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
......@@ -35,6 +36,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html> <div class="h-entry" lang="en">This test is in English.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
......@@ -47,6 +49,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html lang="en"> <div class="h-entry" lang="es">Esta prueba está en español.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
......@@ -59,6 +62,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<div class="h-entry" lang="en">This test is in English.</div>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
......@@ -71,6 +75,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<div class="h-entry">This test is in English.</div>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertFalse(isset($result['items'][0]['properties']['html-lang']));
......@@ -97,6 +102,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html lang="en"> <div class="h-entry">This test is in English.</div> <div class="h-entry" lang="es">Esta prueba está en español.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
......@@ -111,6 +117,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html> <div class="h-feed" lang="en"> <h1 class="p-name">Test Feed</h1> <div class="h-entry">This test is in English.</div> <div class="h-entry" lang="es">Esta prueba está en español.</div> <div class="h-entry" lang="fr">Ce test est en français.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('en', $result['items'][0]['properties']['html-lang']);
......@@ -126,6 +133,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
{
$input = '<html> <meta http-equiv="Content-Language" content="es"/> <div class="h-entry">Esta prueba está en español.</div> </html>';
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('es', $result['items'][0]['properties']['html-lang']);
......@@ -219,6 +227,7 @@ class ParseLanguageTest extends PHPUnit_Framework_TestCase {
</html>
END;
$parser = new Parser($input);
$parser->lang = true;
$result = $parser->parse();
$this->assertEquals('sv', $result['items'][0]['properties']['html-lang']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment