Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #23 from deanblackborough/v0.80
Browse files Browse the repository at this point in the history
V0.80
  • Loading branch information
deanblackborough authored Apr 24, 2017
2 parents 6716677 + e1afe68 commit af7f1f4
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 88 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: php
php:
- '5.6'
- '7.0'
- '7.1'
- hhvm

notifications:
email:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

Full changelog for PHP Quill Renderer

## v0.80.0

* I'm now only testing against PHP 7+. [Tests]
* Added tests for setting attributes. [Tests]
* Switched to PSR4
* Minor change to API if using Quill (Render) class, after the PSR4 change I didn't like Quill/Quill.
* Basic support for images (outputting the base64 directly via <img> src="")

## v0.70.0 - 2017-04-19

* Added the ability to set the HTML tag for the following Quill attributes, bold, italic, script, strike and underline. [Feature]
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ Created for use in [Dlayer](https://github.com/Dlayer/dlayer) but works as a sta
The easiest way to use the renderer is with composer. ```composer require deanblackborough/php-quill-renderer```,
alternatively include the classes in src/ in your library.

## Usage, using Quill API
## Usage
```
try {
$quill = new \DBlackborough\Quill($deltas, 'HTML');
$quill = new \DBlackborough\Quill\Render($deltas, 'HTML');
echo $quill->render();
} catch (\Exception $e) {
echo $e->getMessage();
}
```

## Usage, direct
## Usage, direct, parse and then render
```
$parser = new \DBlackborough\Quill\Parser\Html();
$parser->load($deltas);
Expand Down Expand Up @@ -59,13 +59,13 @@ Script:Sub | `<sub>`
Script:Super | `<sup>`
Underline | `<u>`
Header | `<h[n]>`
Image | `<img>`

## Planned features in possible order
## Planned features

* Markdown support
* Lists (Bullets and Ordered)
* Formatting
* Formatting options (justification etc.)
* Improved newline and paragraph support
* External images
* Remaining toolbar options
* Missing tests (options)
* Markdown support
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "deanblackborough/php-quill-renderer",
"description": "Render quill insert deltas to HTML",
"description": "Render quill insert deltas to HTML and soon markdown",
"autoload": {
"psr-0" : {
"DBlackborough\\Quill" : "src"
"psr-4" : {
"DBlackborough\\Quill\\" : "src/DBlackborough/Quill"
}
},
"homepage": "https://github.com/deanblackborough/php-quill-renderer",
Expand Down
12 changes: 6 additions & 6 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

require_once '../src/DBlackborough/Quill.php';
require_once '../src/DBlackborough/Quill/Renderer.php';
require_once '../src/DBlackborough/Quill/Renderer/Html.php';
require_once '../src/DBlackborough/Quill/Parser.php';
require_once '../src/DBlackborough/Quill/Parser/Html.php';
require_once '../src/Render.php';
require_once '../src/Renderer/Render.php';
require_once '../src/Renderer/Html.php';
require_once '../src/Parser/Parse.php';
require_once '../src/Parser/Html.php';

$deltas = '{"ops":[{"insert":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed efficitur nibh tempor augue lobortis, nec eleifend velit venenatis. Nullam fringilla dui eget lectus mattis tincidunt. \nDonec sollicitudin, lacus sed luctus ultricies, "},{"attributes":{"strike":true,"italic":true},"insert":"quam sapien "},{"attributes":{"strike":true},"insert":"sollicitudin"},{"insert":" quam, nec auctor eros felis elementum quam. Fusce vel mollis enim. \n\n"},{"attributes":{"bold":true},"insert":"Sed ac augue tincidunt,"},{"insert":" cursus urna a, tempus ipsum. Donec pretium fermentum erat a "},{"attributes":{"underline":true},"insert":"elementum"},{"insert":". In est odio, mattis sed dignissim sed, porta ac nisl. Nunc et tellus imperdiet turpis placerat tristique nec quis justo. Aenean nisi libero, auctor a laoreet sed, fermentum vel massa.\n\nEtiam ultricies leo eget purus tempor dapibus. Integer ac sapien eros. Suspendisse convallis ex."}]}';

try {
$quill = new \DBlackborough\Quill($deltas, 'HTML');
$quill = new \DBlackborough\Quill\Render($deltas, 'HTML');
echo $quill->render();
} catch (\Exception $e) {
echo $e->getMessage();
Expand Down
24 changes: 16 additions & 8 deletions src/DBlackborough/Quill/Parser/Html.php → src/Parser/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace DBlackborough\Quill\Parser;

use DBlackborough\Quill\Parser;
use PHPUnit\Runner\Exception;
use \Exception;

/**
* Parser for HTML, parses the deltas to generate a content array for deltas into a html redy array
Expand All @@ -12,7 +11,7 @@
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Html extends Parser
class Html extends Parse
{
/**
* Renderer constructor.
Expand Down Expand Up @@ -199,7 +198,9 @@ private function splitDeltas()
$this->deltas = array();

foreach ($deltas as $delta) {
if (array_key_exists('insert', $delta) === true && array_key_exists('attributes', $delta) === false &&
if (array_key_exists('insert', $delta) === true &&
array_key_exists('attributes', $delta) === false &&
is_array($delta['insert']) === false &&
preg_match("/[\n]{2}/", $delta['insert']) !== 0) {

foreach (explode("\n\n", $delta['insert']) as $match) {
Expand Down Expand Up @@ -281,8 +282,15 @@ private function assignTags()
}
}

if (array_key_exists('insert', $insert) === true && strlen(trim($insert['insert'])) > 0) {
$this->content[$i]['content'] = $insert['insert'];
if (array_key_exists('insert', $insert) === true) {
if (is_array($insert['insert']) === false && strlen(trim($insert['insert'])) > 0) {
$this->content[$i]['content'] = $insert['insert'];
} else {
if (is_array($insert['insert']) === true &&
array_key_exists('image', $insert['insert']) === true) {
$this->content[$i]['content'] = [ 'image' => $insert['insert']['image'] ];
}
}
}

$i++;
Expand Down Expand Up @@ -357,7 +365,7 @@ private function removeEmptyElements()
$existing_content = $this->content;
$this->content = array();
foreach ($existing_content as $content) {
if (strlen($content['content']) !== 0) {
if (is_array($content['content']) === true || strlen($content['content']) !== 0) {
$this->content[] = $content;
}
}
Expand Down Expand Up @@ -461,7 +469,7 @@ private function validateAndSetAttributeOption($option, $value)
* @param mixed $value New Attribute option value
*
* @return boolean
* @throws Exception
* @throws \Exception
*/
public function setAttributeOption($option, $value)
{
Expand Down
6 changes: 3 additions & 3 deletions src/DBlackborough/Quill/Parser.php → src/Parser/Parse.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DBlackborough\Quill;
namespace DBlackborough\Quill\Parser;

/**
* Quill parser, parses deltas json array and generates a content array for the renderer
Expand All @@ -9,7 +9,7 @@
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
abstract class Parser
abstract class Parse
{
/**
* Delta inserts
Expand Down Expand Up @@ -147,7 +147,7 @@ public function getOptions()
* Set all the attribute options for the parser/renderer
*
* @param array $options
* @return \DBlackborough\Quill\Parser
* @return \DBlackborough\Quill\Parse
*/
abstract public function setAttributeOptions(array $options);

Expand Down
10 changes: 5 additions & 5 deletions src/DBlackborough/Quill.php → src/Render.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DBlackborough;
namespace DBlackborough\Quill;

/**
* Parse Quill generated deltas to the requested format
Expand All @@ -9,15 +9,15 @@
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Quill
class Render
{
/**
* @var \DBlackborough\Quill\Renderer
* @var \DBlackborough\Quill\Renderer\Render
*/
private $renderer;

/**
* @var \DBlackborough\Quill\Parser
* @var \DBlackborough\Quill\Parser\Parse
*/
private $parser;

Expand Down Expand Up @@ -62,7 +62,7 @@ public function __construct($deltas, $format='HTML')
*/
public function setAttributeOption($option, $value)
{
if (is_a($this->parser, '\DBlackborough\Quill\Parser') === true) {
if (is_a($this->parser, '\DBlackborough\Quill\Parser\Parse') === true) {
return $this->parser->setAttributeOption($option, $value);
} else {
throw new \Exception('Parser not instantiated, can only set options after instantiating object');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace DBlackborough\Quill\Renderer;

use DBlackborough\Quill\Renderer;

/**
* Quill renderer, converts quill delta inserts into HTML
*
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Html extends Renderer
class Html extends Render
{
/**
* The generated HTML, string generated by the render method from the content array
Expand Down Expand Up @@ -46,7 +44,11 @@ public function render()
}
}

$this->html .= $content['content'];
if (is_array($content['content']) === false) {
$this->html .= $content['content'];
} else {
$this->html .= '<img src="' . $content['content']['image'] . '" />';
}

foreach (array_reverse($content['tags']) as $tag) {
if (array_key_exists('close', $tag) === true && $tag['close'] !== null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DBlackborough\Quill;
namespace DBlackborough\Quill\Renderer;

/**
* Quill renderer, iterates over the content data array and creates html
Expand All @@ -9,7 +9,7 @@
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
abstract class Renderer
abstract class Render
{
/**
* @var array
Expand Down
16 changes: 8 additions & 8 deletions tests/BlockTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

require_once __DIR__ . '../../src/DBlackborough/Quill.php';
require_once __DIR__ . '../../src/DBlackborough/Quill/Renderer.php';
require_once __DIR__ . '../../src/DBlackborough/Quill/Renderer/Html.php';
require_once __DIR__ . '../../src/DBlackborough/Quill/Parser.php';
require_once __DIR__ . '../../src/DBlackborough/Quill/Parser/Html.php';
require_once __DIR__ . '../../src/Render.php';
require_once __DIR__ . '../../src/Renderer/Render.php';
require_once __DIR__ . '../../src/Renderer/Html.php';
require_once __DIR__ . '../../src/Parser/Parse.php';
require_once __DIR__ . '../../src/Parser/Html.php';

final class BlockTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -14,7 +14,7 @@ final class BlockTest extends \PHPUnit\Framework\TestCase
public function testValidDeltasSimpleString()
{
try {
$quill = new \DBlackborough\Quill($this->deltas_simple_string, 'HTML');
$quill = new \DBlackborough\Quill\Render($this->deltas_simple_string, 'HTML');
$this->assertTrue(true);
} catch (\Exception $e) {
$this->fail(__METHOD__ . ' failure');
Expand All @@ -24,7 +24,7 @@ public function testValidDeltasSimpleString()
public function testInvalidDeltasCaught()
{
try {
$quill = new \DBlackborough\Quill($this->deltas_missing_quote, 'HTML');
$quill = new \DBlackborough\Quill\Render($this->deltas_missing_quote, 'HTML');
$this->fail(__METHOD__ . ' failure');
} catch (\Exception $e) {
$this->assertTrue(true);
Expand All @@ -35,7 +35,7 @@ public function testOutputSimpleString()
{
$expected = '<p>Lorem ipsum dolor sit amet</p>';

$quill = new \DBlackborough\Quill($this->deltas_simple_string);
$quill = new \DBlackborough\Quill\Render($this->deltas_simple_string);
$this->assertEquals($expected, $quill->render());
}
}
Loading

0 comments on commit af7f1f4

Please sign in to comment.