first commit

This commit is contained in:
pakerpale
2020-06-10 06:21:34 +09:00
commit 20c9739ba9
1913 changed files with 266257 additions and 0 deletions

67
application/third_party/Sioen/Converter.php vendored Executable file
View File

@@ -0,0 +1,67 @@
<?php
/**
* Class Converter
*
* A Sir Trevor to HTML conversion helper for PHP
*
* @version 1.1.0
* @author Wouter Sioen <wouter@woutersioen.be>
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/
class Converter
{
/**
* Converts the outputted json from Sir Trevor to html
*
* @param string $json
* @return string
*/
public function toHtml($json)
{
// convert the json to an associative array
$input = json_decode($json, true);
$html = '';
// loop trough the data blocks
foreach ($input['data'] as $block) {
$toHtmlContext = new ToHtmlContext($block['type']);
$html .= $toHtmlContext->getHtml($block['data']);
}
return $html;
}
/**
* Converts html to the json Sir Trevor requires
*
* @param string $html
* @return string The json string
*/
public function toJson($html)
{
// Strip white space between tags to prevent creation of empty #text nodes
$html = preg_replace('~>\s+<~', '><', $html);
$document = new \DOMDocument();
// Load UTF-8 HTML hack (from http://bit.ly/pVDyCt)
$document->loadHTML('<?xml encoding="UTF-8">' . $html);
$document->encoding = 'UTF-8';
// fetch the body of the document. All html is stored in there
$body = $document->getElementsByTagName("body")->item(0);
$data = array();
// loop trough the child nodes and convert them
if ($body) {
foreach ($body->childNodes as $node) {
$toJsonContext = new ToJsonContext($node->nodeName);
$data[] = $toJsonContext->getData($node);
}
}
return json_encode(array('data' => $data));
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Sioen\Tests;
use Sioen\Converter;
class ConverterTest extends \PHPUnit_Framework_TestCase
{
public function testToHtml()
{
$converter = new Converter();
// let's try a basic json
$json = json_encode(array(
'data' => array(array(
'type' => 'quote',
'data' => array(
'text' => 'Text',
'cite' => 'Cite'
)
))
));
$html = $converter->toHtml($json);
$this->assertEquals(
$html,
"<blockquote><p>Text</p>\n<cite><p>Cite</p>\n</cite></blockquote>"
);
// Lets convert a normal text type that uses the default converter
$json = json_encode(array(
'data' => array(array(
'type' => 'text',
'data' => array(
'text' => 'test'
)
))
));
$html = $converter->toHtml($json);
$this->assertEquals($html, "<p>test</p>\n");
// the video conversion
$json = json_encode(array(
'data' => array(array(
'type' => 'video',
'data' => array(
'source' => 'youtube',
'remote_id' => '4BXpi7056RM'
)
))
));
$html = $converter->toHtml($json);
$this->assertEquals(
$html,
"<iframe src=\"//www.youtube.com/embed/4BXpi7056RM?rel=0\" frameborder=\"0\" allowfullscreen></iframe>\n"
);
// The heading
$json = json_encode(array(
'data' => array(array(
'type' => 'heading',
'data' => array(
'text' => 'test'
)
))
));
$html = $converter->toHtml($json);
$this->assertEquals($html, "<h2>test</h2>\n");
// images
$json = json_encode(array(
'data' => array(array(
'type' => 'image',
'data' => array(
'file' => array(
'url' => '/frontend/files/sir-trevor/images/IMG_3867.JPG'
)
)
))
));
$html = $converter->toHtml($json);
$this->assertEquals($html, "<img src=\"/frontend/files/sir-trevor/images/IMG_3867.JPG\" />\n");
}
public function testToJson()
{
$converter = new Converter();
$this->assertEquals(
$converter->toJson('<h2>Test</h2>'),
'{"data":[{"type":"heading","data":{"text":" Test"}}]}'
);
// a quote
$this->assertEquals(
$converter->toJson('<blockquote><p>Text</p><cite>Cite</cite></blockquote>'),
'{"data":[{"type":"quote","data":{"text":" Text","cite":" Cite"}}]}'
);
$this->assertEquals(
$converter->toJson('<blockquote><p>Text</p></blockquote>'),
'{"data":[{"type":"quote","data":{"text":" Text","cite":""}}]}'
);
$this->assertEquals(
$converter->toJson('<img src="/path/to/img.jpg" />'),
'{"data":[{"type":"image","data":{"file":{"url":"\/path\/to\/img.jpg"}}}]}'
);
}
}

View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>

View File

@@ -0,0 +1,49 @@
<?php
class ToHtmlContext {
protected $converter = null;
public function __construct($type) {
switch ($type) {
case 'heading':
$this->converter = new HeadingConverter();
break;
case 'columns':
$this->converter = new ColumnsConverter();
break;
case 'button':
$this->converter = new ButtonConverter();
break;
case 'accordion':
$this->converter = new AccordionConverter();
break;
case 'list':
$this->converter = new ListConverter();
break;
case 'quote':
$this->converter = new BlockquoteConverter();
break;
case 'video':
$this->converter = new IframeConverter();
break;
case 'iframe':
$this->converter = new IframeExtendedConverter();
break;
case 'image':
$this->converter = new ImageConverter();
break;
case 'image_extended':
$this->converter = new ImageExtendedConverter();
break;
default:
$this->converter = new BaseConverter();
break;
}
}
public function getHtml(array $data) {
return $this->converter->toHtml($data);
}
}

View File

@@ -0,0 +1,40 @@
<?php
class ToJsonContext
{
protected $converter = null;
public function __construct($nodeName)
{
switch ($nodeName) {
case 'p':
$this->converter = new ParagraphConverter();
break;
case 'h2':
$this->converter = new HeadingConverter();
break;
case 'ul':
$this->converter = new ListConverter();
break;
case 'blockquote':
$this->converter = new BlockquoteConverter();
break;
case 'iframe':
$this->converter = new IframeConverter();
break;
case 'img':
$this->converter = new ImageConverter();
break;
default:
$this->converter = new BaseConverter();
break;
}
}
public function getData(\DOMElement $node)
{
return $this->converter->toJson($node);
}
}

View File

@@ -0,0 +1,36 @@
<?php
class AccordionConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
return array(
'type' => 'image',
'data' => array(
'file' => array(
'url' => $node->getAttribute('src')
)
)
);
}
public function toHtml(array $data)
{
$html = "<div id='accordion' class='panel-group'>";
$i = 0;
foreach ($data['panels'] as $panel) {
$html .= "<div class='panel panel-default'><div class='panel-heading'>";
$html .= "<h4 class='panel-title'><a data-toggle='collapse' data-parent='#accordion' href='#collapse".$i."'>".$panel['title']."</a></h4></div>";
$html .= "<div id='collapse".$i."' class='panel-collapse collapse ";
if ($i == 0){ $html .= "in"; } else { $html .= "out"; }
$html .= "' role='tabpanel'><div class='panel-body'><p>".$panel['body']."</p></div>";
$html .= "</div></div>";
$i++;
}
$html .= "</div>";
return $html;
}
}

View File

@@ -0,0 +1,39 @@
<?php
class BaseConverter implements ConverterInterface
{
/**
* The options we use for html to markdown
*
* @var array
*/
protected $options = array(
'header_style' => 'atx',
'bold_style' => '__',
'italic_style' => '_',
);
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
return array(
'type' => 'text',
'data' => array(
'text' => ' ' . $this->htmlToMarkdown($html)
)
);
}
public function toHtml(array $data)
{
return Markdown::defaultTransform($data['text']);
}
protected function htmlToMarkdown($html)
{
$markdown = new \HTML_To_Markdown($html, $this->options);
return $markdown->output();
}
}

View File

@@ -0,0 +1,52 @@
<?php
class BlockquoteConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
// check if the quote contains a cite
$cite = '';
foreach ($node->childNodes as $child) {
// if it contains a 'cite' node, we should add it in the cite property
if ($child->nodeName == 'cite') {
$html = $child->ownerDocument->saveXML($child);
$html = preg_replace('/<(\/|)cite>/i', '', $html);
$child->parentNode->removeChild($child);
$cite = ' ' . $this->htmlToMarkdown($html);
}
}
// we use the remaining html to create the remaining text
$html = $node->ownerDocument->saveXML($node);
$html = preg_replace('/<(\/|)blockquote>/i', '', $html);
return array(
'type' => 'quote',
'data' => array(
'text' => ' ' . $this->htmlToMarkdown($html),
'cite' => $cite
)
);
}
public function toHtml(array $data)
{
$text = $data['text'];
$html = '<blockquote>';
$html .= Markdown::defaultTransform($text);
// Add the cite if necessary
if (isset($data['cite']) && !empty($data['cite'])) {
// remove the indent thats added by Sir Trevor
$cite = ltrim($data['cite'], '>');
$html .= '<cite>' . Markdown::defaultTransform($cite) . '</cite>';
}
$html .= '</blockquote>';
return $html;
}
}

View File

@@ -0,0 +1,27 @@
<?php
class ButtonConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
return array(
'type' => 'image',
'data' => array(
'file' => array(
'url' => $node->getAttribute('src')
)
)
);
}
public function toHtml(array $data)
{
if ($data['is_block']==1){
$block=" btn-block";
} else {
$block = "";
}
return '<a href="' . $data['url'] . '" class="btn ' . $data['style'] . ' ' . $data['size'] . $block .'">' . $data['html'] . '</a>' . "\n";
}
}

View File

@@ -0,0 +1,35 @@
<?php
class CodeConverter implements ConverterInterface
{
/**
* The options we use for html to markdown
*
* @var array
*/
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
return array(
'type' => 'text',
'data' => array(
'text' => ' ' . $this->htmlToMarkdown($html)
)
);
}
public function toHtml(array $data)
{
print_r($data);
die();
return Markdown::defaultTransform($data['text']);
}
protected function htmlToMarkdown($html)
{
$markdown = new \HTML_To_Markdown($html, $this->options);
return $markdown->output();
}
}

View File

@@ -0,0 +1,33 @@
<?php
class ColumnsConverter extends BaseConverter implements ConverterInterface {
public function toJson(\DOMElement $node) {
return array(
'type' => 'image',
'data' => array(
'file' => array(
'url' => $node->getAttribute('src')
)
)
);
}
public function toHtml(array $data) {
$html = "<div class='row'>";
// echo "<pre>";
// print_r($data);
// die();
foreach ($data['columns'] as $column) {
$html .= "<div class='col-md-".$column['width']."'>";
$converter = new Converter();
$HTMLContent = $converter->toHtml(json_encode(array("data"=>$column["blocks"])));
$html .= $HTMLContent;
$html .= "</div>";
}
$html .= "</div>";
return $html;
}
}

View File

@@ -0,0 +1,9 @@
<?php
interface ConverterInterface
{
public function toJson(\DOMElement $node);
public function toHtml(array $data);
}

View File

@@ -0,0 +1,24 @@
<?php
class HeadingConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
// remove the h2 tags from the text. We just need the inner text.
$html = preg_replace('/<(\/|)h2>/i', '', $html);
return array(
'type' => 'heading',
'data' => array(
'text' => ' ' . $this->htmlToMarkdown($html)
)
);
}
public function toHtml(array $data)
{
return "<".$data['heading'].">".stripslashes($data['text']) . "</".$data['heading'].">";
}
}

View File

@@ -0,0 +1,69 @@
<?php
class IframeConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
// youtube or vimeo
if (preg_match('~//www.youtube.com/embed/([^/\?]+).*\"~si', $html, $matches)) {
return array(
'type' => 'video',
'data' => array(
'source' => 'youtube',
'remote_id' => $matches[1]
)
);
} elseif (preg_match('~//player.vimeo.com/video/([^/\?]+).*\?~si', $html, $matches)) {
return array(
'type' => 'video',
'data' => array(
'source' => 'vimeo',
'remote_id' => $matches[1]
)
);
}
}
public function toHtml(array $data)
{
// youtube video's
$source = $data['source'];
$remoteId = $data['remote_id'];
if ($source == 'youtube') {
$html = '<div class="embed-responsive embed-responsive-16by9"><iframe src="//www.youtube.com/embed/' . $remoteId . '?rel=0" ';
$html .= 'frameborder="0" allowfullscreen></iframe></div>' . "\n";
return $html;
}
// vimeo videos
if ($source == 'vimeo') {
$html = '<div class="embed-responsive embed-responsive-16by9"><iframe src="//player.vimeo.com/video/' . $remoteId;
$html .= '?title=0&amp;byline=0" frameborder="0"></iframe></div>' . "\n";
return $html;
}
//dailymotion
if ($source == 'dailymotion') {
$html = '<div class="embed-responsive embed-responsive-16by9"><iframe src="//www.dailymotion.com/embed/video/' . $remoteId . '" ';
$html .= 'frameborder="0" allowfullscreen></iframe></div>' . "\n";
return $html;
}
// vine
if ($source == 'vine') {
$html = '<div class="embed-responsive embed-responsive-16by9"><iframe src="/vine.co/v/' . $remoteId;
$html .= '/embed/simple" frameborder="0"></iframe><script async src="http://platform.vine.co/static/scripts/embed.js" charset="utf-8"></script>' . "\n";
return $html;
}
// fallback
return '';
}
}

View File

@@ -0,0 +1,25 @@
<?php
class IframeExtendedConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
}
public function toHtml(array $data)
{
$height = $data['height'] * 1.3;
if ($data['width'] > $height){
$size = "embed-responsive-16by9";
} else {
$size = "embed-responsive-4by3";
}
$html = '<div class="embed-responsive '.$size.'"><iframe src="'.$data['src'].'" width="'.$data['width'].'" height="'.$data['height'].'" frameborder="0"></iframe></div>' . "\n";
return $html;
}
}

View File

@@ -0,0 +1,22 @@
<?php
class ImageConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
return array(
'type' => 'image',
'data' => array(
'file' => array(
'url' => $node->getAttribute('src')
)
)
);
}
public function toHtml(array $data)
{
return '<img class="img-responsive" src="' . $data['file']['url'] . '" />' . "\n";
}
}

View File

@@ -0,0 +1,31 @@
<?php
class ImageExtendedConverter implements ConverterInterface
{
/**
* The options we use for html to markdown
*
* @var array
*/
public function toJson(\DOMElement $node)
{
$html = $node->ownerDocument->saveXML($node);
return array(
'type' => 'text',
'data' => array(
'text' => ' ' . $this->htmlToMarkdown($html)
)
);
}
public function toHtml(array $data)
{
if (($data['source'] == "") || ($data['source'] == "http://")){
return '<img class="img-responsive" src="' . $data['file']['url'] . '" alt="' . $data['caption'] . '" />' . "\n";
} else {
return '<a href="' . $data['source'] . '" target="_blank"><img class="img-responsive" src="' . $data['file']['url'] . '" alt="' . $data['caption'] . '" /></a>' . "\n";
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
class ListConverter extends BaseConverter implements ConverterInterface
{
public function toJson(\DOMElement $node)
{
$markdown = $this->htmlToMarkdown($node->ownerDocument->saveXML($node));
// we need a space in the beginning of each line
$markdown = ' ' . str_replace("\n", "\n ", $markdown);
return array(
'type' => 'list',
'data' => array(
'text' => $markdown,
)
);
}
}

View File

@@ -0,0 +1,6 @@
<?php
class ParagraphConverter extends BaseConverter implements ConverterInterface
{
}

View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>

10
application/third_party/Sioen/index.html vendored Executable file
View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>