Skip to content

Moves language parsing behind feature flag, and prep for v0.3.2 release #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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