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 #5 from deanblackborough/lists
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
deanblackborough authored Feb 28, 2017
2 parents f164d52 + dd258f7 commit 5ca1c7b
Show file tree
Hide file tree
Showing 10 changed files with 437 additions and 169 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ Full changelog for PHP Quill Renderer

## Dev master

### v0.20 - 2017-02-26

* Reworked rendering code.
* Added base class so additional rendering class can be developed, for example a markdown renderer.

I got most of the way through adding basic support for lists and then stumbled on a problem, I need to rework
how new lines are handled and tidy up the code, needs to be come aware of block elements.

### v0.10 - 2017-02-26

* Newline correctly trimmed from final insert. [Bugfix]

### Initial release - 2017-02-25

Initial release, converts delta inserts into HTML, support four attributes,
* Initial release, converts delta inserts into HTML, support four attributes,
`bold`, `strike`, `italic` and `underline`. The HTML tag to be used for each
attribute can be set along with the attributes to use for newlines and paragraphs,
defauls to `br` and `p`.
defaults to `br` and `p`.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Latest Stable Version](https://img.shields.io/packagist/v/deanblackborough/php-quill-renderer.svg?style=flat-square)](https://packagist.org/packages/deanblackborough/php-quill-renderer)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Dlayer/dlayer/blob/master/LICENSE)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)](https://php.net/)
[![Build Status](https://travis-ci.org/deanblackborough/php-quill-renderer.svg?branch=master)](https://travis-ci.org/deanblackborough/php-quill-renderer)
Expand All @@ -10,8 +11,7 @@

## Description

Simple Quill delta insert renderer, currently it only supports the attributes I require, 'italic', 'bold', 'underline'
and 'strike', I will add support for additional attributes as I need them in Dlayer.
Simple Quill delta insert renderer, currently it only supports the attributes listed below, I will add support for additional attributes as I need them in Dlayer.

Created for use in [Dlayer](https://github.com/Dlayer/dlayer) but works as a stand-alone tool.

Expand All @@ -31,8 +31,10 @@ use for the `container` and `newline`.

### Default options

* `container`: `p`
* `newline`: `br`
Separator | HTML Tag
--- | ---
Container | `<p>`
Newline | `<br />`

#### Default attribute options

Expand Down
5 changes: 3 additions & 2 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

require_once '../src/DBlackborough/Quill/Renderer.php';
require_once '../src/DBlackborough/Quill/Renderer/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 \n"}]}';

$renderer = new \DBlackborough\Quill\Renderer();
$renderer = new \DBlackborough\Quill\Renderer\Html();

if ($renderer->load($deltas) === true) {
var_dump($renderer->toHtml());
var_dump($renderer->render());
}
134 changes: 37 additions & 97 deletions src/DBlackborough/Quill/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,47 @@
/**
* Quill renderer, converts quill delta inserts into html
*
* @todo Validate options
* @todo Validate $deltas
* @todo Log and return errors
* @todo Tests for each attribute
*
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Renderer
abstract class Renderer
{
/**
* Delta inserts
*
* @var array
*/
private $deltas;
protected $deltas;

/**
* Valid inserts json array
*
* @param boolean
*/
private $json_valid = false;
protected $json_valid = false;

/**
* Options data array
*
* @param array
*/
private $options = array();
protected $options = array();

/**
* @var array
*/
protected $errors;

/**
* @var string
* @var boolean
*/
private $html;
protected $content_valid = false;

/**
* @var array
*/
protected $content;

/**
* Renderer constructor.
Expand All @@ -49,6 +54,9 @@ class Renderer
*/
public function __construct(array $options = array())
{
$this->html = null;
$this->content = array();

if (count($options) === 0) {
$options = $this->defaultOptions();
}
Expand All @@ -61,29 +69,17 @@ public function __construct(array $options = array())
*
* @return array
*/
private function defaultOptions()
{
return array(
'attributes' => array(
'bold' => 'strong',
'italic' => 'em',
'underline' => 'u',
'strike' => 's'
),
'container' => 'p',
'newline' => 'br'
);
}
abstract protected function defaultOptions();

/**
* Validate the attribute value,
* Check to see if the requested attribute is valid, needs to be a known attribute and have an option set
*
* @param string $attribute
* @param string $value
*
* @return boolean
*/
private function validAttribute($attribute, $value)
protected function isAttributeValid($attribute, $value)
{
$valid = false;

Expand All @@ -93,7 +89,18 @@ private function validAttribute($attribute, $value)
case 'italic':
case 'underline':
case 'strike':
if($value === true) {
if(array_key_exists('attributes', $this->options) === true &&
array_key_exists($attribute, $this->options['attributes']) === true &&
$value === true) {

$valid = true;
}
break;
case 'list':
if(array_key_exists('attributes', $this->options) === true &&
array_key_exists('list', $this->options['attributes']) === true &&
array_key_exists($value, $this->options['attributes']['list']) === true) {

$valid = true;
}
break;
Expand Down Expand Up @@ -184,80 +191,13 @@ public function load($deltas)
}
}

/**
* Convert new lines
*
* @param string $subject
/**
* @return string
*/
private function convertNewlines($subject)
{
$patterns = array(
"/[\n]{2,} */",
"/[\n]{1}/"
);
$replacements = array(
'</' . $this->options['container'] . '><' . $this->options['container'] . '>',
'<' . $this->options['newline'] . ' />',
);

return preg_replace($patterns, $replacements, $subject);
}
abstract protected function parseDeltas();

/**
* @return string
*/
public function toHtml()
{
$this->html = null;

if ($this->json_valid === true && array_key_exists('ops', $this->deltas) === true) {

$inserts = count($this->deltas['ops']);

foreach ($this->deltas['ops'] as $k => $insert) {
if ($k === 0) {
$this->html .= '<' . $this->options['container'] . '>';
}

$tags = array();
$hasTags = false;

if (array_key_exists('attributes', $insert) === true && is_array($insert['attributes']) === true) {
foreach ($insert['attributes'] as $attribute => $value) {
if ($this->validAttribute($attribute, $value) === true) {
$tags[] = $this->options['attributes'][$attribute];
}
}
}

if (count($tags) > 0) {
$hasTags = true; // Set bool so we don't need to check array size again
}

if ($hasTags === true) {
foreach ($tags as $tag) {
$this->html .= '<' . $tag . '>';
}
}

if (array_key_exists('insert', $insert) === true) {
$this->html .= $this->convertNewlines($insert['insert']);
}

if ($hasTags === true) {
foreach (array_reverse($tags) as $tag) {
$this->html .= '</' . $tag . '>';
}
}

if ($k === ($inserts-1)) {
$this->html = rtrim($this->html, '<' . $this->options['newline'] . ' />');
$this->html .= '</' . $this->options['container'] . '>';
}
}
}

return $this->html;
}
abstract public function render();
}
Loading

0 comments on commit 5ca1c7b

Please sign in to comment.