Skip to content

Commit

Permalink
Merge pull request microformats#123 from aaronpk/master
Browse files Browse the repository at this point in the history
Moves language parsing behind feature flag, and prep for v0.3.2 release
  • Loading branch information
aaronpk authored May 27, 2017
2 parents 84bd6ef + 9138ea4 commit dc0d90d
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 108 deletions.
19 changes: 13 additions & 6 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -811,9 +814,11 @@ public function parseE(\DOMElement $e) {
'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;
Expand Down Expand Up @@ -1073,9 +1078,11 @@ public function parseH(\DOMElement $e, $is_backcompat = false) {
}
}

// 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
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Loading

0 comments on commit dc0d90d

Please sign in to comment.