diff --git a/README.md b/README.md
index 70b698e..7dfb477 100644
--- a/README.md
+++ b/README.md
@@ -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 = '
Hello World
';
+$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.
diff --git a/lib/XRay.php b/lib/XRay.php
index f656b3c..9670b4c 100644
--- a/lib/XRay.php
+++ b/lib/XRay.php
@@ -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;
+ }
+
}
diff --git a/lib/XRay/Parser.php b/lib/XRay/Parser.php
index 252d537..9be03fd 100644
--- a/lib/XRay/Parser.php
+++ b/lib/XRay/Parser.php
@@ -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) == 'http, $opts);
}
}
diff --git a/tests/LibraryTest.php b/tests/LibraryTest.php
new file mode 100644
index 0000000..f121da3
--- /dev/null
+++ b/tests/LibraryTest.php
@@ -0,0 +1,37 @@
+Hello World
';
+ $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 = 'Hello World
';
+ $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 = 'Barnaby Walters';
+ $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']);
+ }
+
+}
diff --git a/tests/ParseTest.php b/tests/ParseTest.php
index ff4dde5..991b23d 100644
--- a/tests/ParseTest.php
+++ b/tests/ParseTest.php
@@ -595,4 +595,22 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://one.example.com/test', $data['data']['url']);
}
+ public function testInputIsParsedMf2() {
+ $html = 'Hello World
';
+ $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]);
+ }
+
}