Skip to content

Commit bd299e6

Browse files
committed
v0.1
0 parents  commit bd299e6

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

LICENCE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Nicolas Liautaud
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

PicoOutput.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Output the page content or data as raw, html, json or xml with `?output`.
4+
*
5+
* @author Nicolas Liautaud
6+
* @link https://github.com/nliautaud/pico-content-output
7+
* @link http://picocms.org
8+
* @license http://opensource.org/licenses/MIT The MIT License
9+
* @version 0.1.0
10+
*/
11+
final class PicoOutput extends AbstractPicoPlugin
12+
{
13+
private $serveContent;
14+
private $contentFormat;
15+
16+
/**
17+
* Look for ?output=format in url (format as `content` by default)
18+
*
19+
* Triggered after Pico has evaluated the request URL
20+
*
21+
* @see Pico::getRequestUrl()
22+
* @param string &$url part of the URL describing the requested contents
23+
* @return void
24+
*/
25+
public function onRequestUrl(&$url)
26+
{
27+
$this->serveContent = isset($_GET['output']);
28+
if ($this->serveContent)
29+
$this->contentFormat = $_GET['output'] ? $_GET['output'] : 'content';
30+
}
31+
/**
32+
* Output the page data in the defined format.
33+
*
34+
* Triggered after Pico has rendered the page
35+
*
36+
* @param string &$output contents which will be sent to the user
37+
* @return void
38+
*/
39+
public function onPageRendered(&$output)
40+
{
41+
if ($this->serveContent && $this->enabledFormat())
42+
$output = $this->contentOutput();
43+
}
44+
/**
45+
* Check if the requested format is enabled in config.
46+
*
47+
* @return bool
48+
*/
49+
public function enabledFormat()
50+
{
51+
$enabledFormats = $this->getPico()->getConfig('PicoOutput.enabledFormats');
52+
return is_array($enabledFormats) && in_array($this->contentFormat, $enabledFormats);
53+
}
54+
55+
/**
56+
* Return the current page data in the defined format.
57+
* @return string
58+
*/
59+
private function contentOutput()
60+
{
61+
$pico = $this->getPico();
62+
switch ($this->contentFormat) {
63+
case 'raw':
64+
return $pico->getRawContent();
65+
case 'prepared':
66+
return $pico->prepareFileContent($pico->getRawContent(), $pico->getFileMeta());
67+
case 'json':
68+
header('Content-Type: application/json;charset=utf-8');
69+
return json_encode($pico->getCurrentPage());
70+
case 'xml':
71+
header("Content-type: text/xml");
72+
$xml = new SimpleXMLElement('<page/>');
73+
$this->array_to_xml($pico->getCurrentPage(), $xml);
74+
return $xml->asXML();
75+
default:
76+
return $pico->getFileContent();
77+
}
78+
}
79+
80+
// function defination to convert array to xml
81+
private function array_to_xml( $data, &$xml_data )
82+
{
83+
foreach( $data as $key => $value ) {
84+
if( is_numeric($key) ){
85+
$key = 'item'.$key; //dealing with <0/>..<n/> issues
86+
}
87+
if( is_array($value) ) {
88+
$subnode = $xml_data->addChild($key);
89+
$this->array_to_xml($value, $subnode);
90+
} else {
91+
$xml_data->addChild("$key",htmlspecialchars("$value"));
92+
}
93+
}
94+
}
95+
}
96+
?>

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Pico Page Output
2+
3+
Output the page content or data as raw, html, json or xml with `?output` in [Pico CMS](http://picocms.org).
4+
5+
## Installation
6+
7+
Copy `PicoPageOutput.php` to the `plugins/` directory of your Pico Project.
8+
9+
## Usage
10+
11+
Enable the plugin and the output formats in Pico `config.php`.
12+
13+
```php
14+
$config['PicoPageOutput.enabled'] = true; // by default
15+
$config['PicoPageOutput.enabledFormats'] = array('content', 'prepared');
16+
```
17+
18+
Then add `?output=format` to any url.
19+
20+
format|desc.|example
21+
---|---|---
22+
`content`|The html content.|`<p>Some content. <emph>base_url</emph>: http://monsite.com</p>`
23+
`raw`|The raw page, with meta header and raw variables.|`---\nTitle:My title\n---\nSome content. *base_url*: %base_url%`
24+
`prepared`|The page content without the meta header and with parsed variables.|`Some content. *base_url*: http://monsite.com`
25+
`json`|The page data in json format.|`{"id":"index","url":"http:\/\/monsite.com\/index","title":"My title","author":"","time":"","date":"","date_formatted":"","raw_content":"---\nTitle:My title\n---\nSome content. *base_url*: %base_url%","content":"<p>Some content. <emph>base_url</emph>: http://monsite.com</p>"}`
26+
`xml`|The page data in xml format.|`{"id":"index","url":"http:\/\/monsite.com\/index","title":"My title","author":"","time":"","date":"","date_formatted":"","raw_content":"---\nTitle:My title\n---\nSome content. *base_url*: %base_url%","content":"<p>Some content. <emph>base_url</emph>: http://monsite.com</p>"}`
27+
28+
Note that some formats output the page meta or basic internal data.

0 commit comments

Comments
 (0)