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 #37 from deanblackborough/v2.02.0
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
deanblackborough authored Sep 18, 2017
2 parents a40c138 + 945f03f commit 000467f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 160 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

Full changelog for PHP Quill Renderer

## 2.02.0 - 2017-09-18

* Removed settings code, new parser/renderer should be created to change options.
* Refactoring, updated method names to better match the containing logic.
* Updated README, Quill attribute support.

## 2.01.0 - 2017-09-17

* Organised tests by renderer type prior to markdown development.
Expand Down
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

# PHP Quill Renderer

*Render quill insert deltas to HTML and Markdown.*
*Render quill insert deltas to HTML and soon Markdown.*

## Description

Quill deltas renderer, converts deltas to HTML, the Quill attributes supported are listed in a table below.
Version 2.00.0 is in development, I'm working on support for additional Quill attributes, markdown support and a
full rewrite of the parser, version 2.00.0 will only support PHP7.1+.
Quill deltas renderer, converts deltas to HTML, the Quill attributes supported are listed in a table below.
I'm working on support for the remaining attributes and additional parsers (markdown etc.)

## PHP 5.6

Expand Down Expand Up @@ -42,18 +41,29 @@ $renderer = new \DBlackborough\Quill\Renderer\Html($parser->content());
echo $renderer->render();
```

## Options
The HTML tag to use for Quill attributes can be set along with the HTML tags for the container.

### Default options
## Quill attributes

Separator | HTML Tag
Attribute | Support
--- | ---
Container | `<p>`

#### Default attribute options

Quill Attribute | HTML Tag
Bold | Yes
Italic | Yes
Link | Yes
Strike | Yes
Script:Sub | Yes
Script:Super | Yes
Underline | Yes
Header | Yes
Image | Yes
List | Yes
Indent/Outdent | Not yet
Text direction | Not yet
Color | Not yet
Font | Not yet
Text align | Not yet
Block quote | Not yet
Code block | Not yet

Attribute | HTML Tag
--- | ---
Bold | `<strong>`
Italic | `<em>`
Expand All @@ -70,13 +80,6 @@ List | `<ul>` `<ol>`

carlos https://github.com/sald19 [Bugfix] v1.01.0

## Planned features for version 2.00.0

* Parser logic rework
* Markdown support
* Formatting options (justification etc.)
* Remaining Quill toolbar options

## Warnings

### Image support
Expand Down
91 changes: 18 additions & 73 deletions src/Parser/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function __construct(array $options = array(), string $block_element = ''
}

/**
* Default block element attribute
* Break attribute options
*
* @return array
*/
protected function defaultBlockElementOption() : array
protected function breakAttributeOptions() : array
{
return array(
'tag' => 'p',
Expand All @@ -37,11 +37,11 @@ protected function defaultBlockElementOption() : array
}

/**
* Default attribute options for the HTML renderer/parser
* Attribute options, HTML tag to map to each attribute
*
* @return array
*/
protected function defaultAttributeOptions() : array
protected function attributeOptions() : array
{
return array(
'bold' => array(
Expand Down Expand Up @@ -174,7 +174,7 @@ private function getTagAndAttributes($attribute, $value)
*
* @return void
*/
private function lastItemClosed()
private function checkLastItemClosed()
{
$last_item = count($this->content) - 1;
$assigned_tags = $this->content[$last_item]['tags'];
Expand Down Expand Up @@ -208,7 +208,7 @@ private function lastItemClosed()
*
* @return void
*/
private function splitDeltas()
private function convertMultipleNewlines()
{
$deltas = $this->deltas;
$this->deltas = array();
Expand Down Expand Up @@ -238,7 +238,7 @@ private function splitDeltas()
* Looks to see if the next item is the start of a list, if this item contains a new line we need
* to split it.
*/
private function listHack()
private function localLists()
{
$deltas = $this->deltas['ops'];
$this->deltas = array();
Expand Down Expand Up @@ -269,7 +269,7 @@ private function listHack()
*
* @return void
*/
private function assignTags()
private function assignHtmlTags()
{
$i = 0;

Expand Down Expand Up @@ -388,7 +388,7 @@ private function openParagraphs()
*
* @return void
*/
private function convertBreaks()
private function convertNewlines()
{
foreach ($this->content as $i => $content) {
if (array_key_exists('break', $content) === true) {
Expand Down Expand Up @@ -416,21 +416,21 @@ public function parse() : bool
{
if ($this->json_valid === true && array_key_exists('ops', $this->deltas) === true) {

$this->listHack();
$this->localLists();

$this->splitDeltas();
$this->convertMultipleNewlines();

$this->assignTags();
$this->assignHtmlTags();

$this->removeEmptyElements();
$this->removeEmptyContentElements();

$this->openParagraphs();

$this->closeOpenParagraphs();
$this->closeParagraphs();

$this->lastItemClosed();
$this->checkLastItemClosed();

$this->convertBreaks();
$this->convertNewlines();

$this->removeRedundantParentTags();

Expand All @@ -455,7 +455,7 @@ public function content() : array
*
* @return void
*/
private function removeEmptyElements()
private function removeEmptyContentElements()
{
$existing_content = $this->content;
$this->content = array();
Expand All @@ -471,7 +471,7 @@ private function removeEmptyElements()
*
* @return void
*/
private function closeOpenParagraphs()
private function closeParagraphs()
{
$open_paragraph = false;
$opened_at = null;
Expand Down Expand Up @@ -501,30 +501,6 @@ private function closeOpenParagraphs()
}
}

/**
* Set all the attribute options for the parser/renderer
*
* @param array $options
*
* @return void
*/
public function setAttributeOptions(array $options)
{
$this->options['attributes'] = $options;
}

/**
* Set the block element for the parser/renderer
*
* @param array $options Block element options
*
* @return void
*/
public function setBlockOptions(array $options)
{
$this->options['block'] = $options;
}

/**
* Validate the option request and set the value
*
Expand Down Expand Up @@ -557,37 +533,6 @@ private function validateAndSetAttributeOption(string $option, $value) : bool
}
}

/**
* Set a new attribute option
*
* @param string $option Attribute option to replace
* @param mixed $value New Attribute option value
*
* @return boolean
* @throws \Exception
*/
public function setAttributeOption(string $option, $value) : bool
{
switch ($option) {
case 'bold':
case 'italic':
case 'script':
case 'strike':
case 'underline':
return $this->validateAndSetAttributeOption($option, $value);
break;
case 'header':
case 'link':
case 'list':
return false;
break;
default:
return false;
break;

}
}

/**
* Remove any redundant parent tags. Using lists as an example, each LI will have open and close
* parent tags assigned to it.
Expand Down
53 changes: 5 additions & 48 deletions src/Parser/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace DBlackborough\Quill\Parser;

/**
* Quill parser, parses deltas json array and generates a content array to be used by the relevant renederer
* Quill parser, parses deltas json array and generates a content array to be used by the relevant renderer
*
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough
Expand Down Expand Up @@ -48,33 +48,24 @@ public function __construct(array $options = array(), string $block_element = ''
{
$this->content = array();

if (count($options) === 0) {
$options = $this->defaultAttributeOptions();
}

$this->setAttributeOptions($options);

$block_options = $this->defaultBlockElementOption();
if ($block_element !== '') {
$block_options['tag'] = $block_element;
}
$this->options['attributes'] = $this->attributeOptions();;

$this->setBlockOptions($this->defaultBlockElementOption());
$this->options['block'] = $this->breakAttributeOptions();
}

/**
* Set the default options for the parser/renderer
*
* @return array
*/
abstract protected function defaultAttributeOptions() : array;
abstract protected function attributeOptions() : array;

/**
* Set the default block element for the parser/renderer
*
* @return array
*/
abstract protected function defaultBlockElementOption() : array;
abstract protected function breakAttributeOptions() : array;

/**
* Check to see if the requested attribute is valid, needs to be a known attribute and have an option set
Expand Down Expand Up @@ -141,40 +132,6 @@ protected function isAttributeValid($attribute, $value) : bool
return $valid;
}

/**
* Get the currently defined options
*
* @return array
*/
public function getOptions() : array
{
return $this->options;
}

/**
* Set all the attribute options for the parser/renderer
*
* @param array $options
*/
abstract public function setAttributeOptions(array $options);

/**
* Set the block element for the parser/renderer
*
* @param array $options Block options
*/
abstract public function setBlockOptions(array $options);

/**
* Set a new attribute option
*
* @param string $option Attribute option to replace
* @param mixed $value New Attribute option value
*
* @return boolean
*/
abstract public function setAttributeOption(string $option, $value) : bool;

/**
* LOad the deltas, checks the json is valid
*
Expand Down
18 changes: 0 additions & 18 deletions src/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,6 @@ public function __construct(string $deltas, string $format='HTML')
}
}

/**
* Set a new attribute option
*
* @param string $option Attribute option to replace
* @param mixed $value New Attribute option value
*
* @return boolean
* @throws \Exception
*/
public function setAttributeOption(string $option, $value) : bool
{
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');
}
}

/**
* Pass content array to renderer and return output
*
Expand Down

0 comments on commit 000467f

Please sign in to comment.