Browse Source

adds an option to process a parsed mf2 page

pull/60/head v1.4.8
Aaron Parecki 6 years ago
parent
commit
171ca175f2
No known key found for this signature in database GPG Key ID: 276C2817346D6056
5 changed files with 112 additions and 0 deletions
  1. +40
    -0
      README.md
  2. +9
    -0
      lib/XRay.php
  3. +8
    -0
      lib/XRay/Parser.php
  4. +37
    -0
      tests/LibraryTest.php
  5. +18
    -0
      tests/ParseTest.php

+ 40
- 0
README.md View File

@ -53,6 +53,13 @@ $html = '....';
$parsed = $xray->parse('https://aaronparecki.com/2017/04/28/9/', $html);
```
```php
$xray = new p3k\XRay();
$jsonfeed = '{"version":"https://jsonfeed.org/version/1","title":"Manton Reece", ... ';
// Note that the JSON document must be passed in as a string in this case
$parsed = $xray->parse('https://manton.micro.blog/feed.json', $jsonfeed);
```
In both cases, you can add an additional parameter to configure various options of how XRay will behave. Below is a list of the options.
* `timeout` - The timeout in seconds to wait for any HTTP requests
@ -93,6 +100,39 @@ $parsed = Array
)
```
### Processing Microformats2 JSON
If you already have a parsed Microformats2 document as an array, you can use a special function to process it into XRay's native format. Make sure you pass the entire parsed document, not just the single item.
```php
$html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
$mf2 = Mf2\parse($html, 'http://example.com/entry');
$xray = new p3k\XRay();
$parsed = $xray->process('http://example.com/entry', $mf2); // note the use of `process` not `parse`
Array
(
[data] => Array
(
[type] => entry
[photo] => Array
(
[0] => http://example.com/photo.jpg
)
[content] => Array
(
[text] => Hello World
)
)
[url] => http://example.com/entry
)
```
### Rels
You can also use XRay to fetch all the rel values on a page, merging the list of HTTP `Link` headers with rel values with the HTML rel values on the page.

+ 9
- 0
lib/XRay.php View File

@ -43,5 +43,14 @@ class XRay {
return $result;
}
public function process($url, $mf2json, $opts=[]) {
$parser = new XRay\Parser($this->http);
$result = $parser->parse($mf2json, $url, $opts);
if(!isset($opts['include_original']) || !$opts['include_original'])
unset($result['original']);
$result['url'] = $url;
return $result;
}
}

+ 8
- 0
lib/XRay/Parser.php View File

@ -42,6 +42,11 @@ class Parser {
return Formats\Hackernews::parse($body, $url);
}
// Check if an mf2 JSON object was passed in
if(is_array($body) && isset($body['items'][0]['type']) && isset($body['items'][0]['properties'])) {
return Formats\Mf2::parse($body, $url, $this->http, $opts);
}
if(substr($body, 0, 5) == '<?xml') {
return Formats\XML::parse($body, $url);
}
@ -50,6 +55,9 @@ class Parser {
$feeddata = json_decode($body, true);
if($feeddata && isset($feeddata['version']) && $feeddata['version'] == 'https://jsonfeed.org/version/1') {
return Formats\JSONFeed::parse($feeddata, $url);
} elseif($feeddata && isset($feeddata['items'][0]['type']) && isset($feeddata['items'][0]['properties'])) {
// Check if an mf2 JSON object was passed in
return Formats\Mf2::parse($feeddata, $url, $this->http, $opts);
}
}

+ 37
- 0
tests/LibraryTest.php View File

@ -0,0 +1,37 @@
<?php
class LibraryTest extends PHPUnit_Framework_TestCase {
public function testInputIsParsedMf2Array() {
$html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
$mf2 = Mf2\parse($html, 'http://example.com/entry');
$xray = new p3k\XRay();
$data = $xray->process('http://example.com/entry', $mf2);
$this->assertEquals('Hello World', $data['data']['content']['text']);
$this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
}
public function testInputIsParsedMf2JSON() {
$html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
$mf2 = Mf2\parse($html, 'http://example.com/entry');
$xray = new p3k\XRay();
$data = $xray->process('http://example.com/entry', json_encode($mf2));
$this->assertEquals('Hello World', $data['data']['content']['text']);
$this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
}
public function testInputIsParsedMf2HCard() {
$url = 'https://waterpigs.co.uk/';
$html = '<a class="h-card" href="https://waterpigs.co.uk/">Barnaby Walters</a>';
$mf2 = Mf2\parse($html, $url);
$xray = new p3k\XRay();
$data = $xray->process($url, $mf2);
$this->assertEquals('card', $data['data']['type']);
$this->assertEquals('Barnaby Walters', $data['data']['name']);
}
}

+ 18
- 0
tests/ParseTest.php View File

@ -595,4 +595,22 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://one.example.com/test', $data['data']['url']);
}
public function testInputIsParsedMf2() {
$html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
$mf2 = Mf2\parse($html, 'http://example.com/entry');
$url = 'http://example.com/entry';
$response = $this->parse([
'url' => $url,
'body' => json_encode($mf2)
]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals('Hello World', $data['data']['content']['text']);
$this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
}
}

Loading…
Cancel
Save