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

Commit

Permalink
Merge 35a39e3 into f665c52
Browse files Browse the repository at this point in the history
  • Loading branch information
deanblackborough committed May 29, 2018
2 parents f665c52 + 35a39e3 commit 3d1e448
Show file tree
Hide file tree
Showing 33 changed files with 495 additions and 167 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@

Full changelog for PHP Quill Renderer

## v3.11.0 - 2018-xx-xx

* `load()` and `loadMultple()` now support method chaining, closes #72, required
a minor change to throw exceptions.
* Generated HTML now has newlines where expected, closes #69
* Updated one test, expected and actual the wrong way around.
* Added a `Options` class.
* Added two tests to catch exceptions.
* Added a trim option to `render`, strips any extra whitespace.

## v3.10.2 - 2018-05-28

* Line breaks missing, added where expected, a couple of tests where updated
to include the `<br />` where it should have been in the expected output.
to include the `<br />` where it should have been in the expected output,
closes #67.

## v3.10.1 - 2018-05-21

Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ features and add additional parsers and renderers, I expect Markdown will be nex
## PHP < 7.2

Please use version v1.01.1 or v2.03.1 if you are using a version of PHP below 7.2, versions 1 and 2 are not feature
with version 3 and are unlikely to ever be updated, the v3 is so much more flexible.
complete with version 3 and are unlikely to ever be updated, the v3 code is so much more flexible.

## Installation

Expand Down Expand Up @@ -63,8 +63,7 @@ echo $result_two;
$parser = new \DBlackborough\Quill\Parser\Html();
$renderer = new \DBlackborough\Quill\Renderer\Html();
$parser->load($quill_json);
$parser->parse();
$parser->load($quill_json)->parse();
echo $renderer->load($parser->deltas())->render();
```
Expand All @@ -75,8 +74,7 @@ echo $renderer->load($parser->deltas())->render();
$parser = new \DBlackborough\Quill\Parser\Html();
$renderer = new \DBlackborough\Quill\Renderer\Html();
$parser->loadMultiple([ 'one'=> $quill_json_1, 'two' => $quill_json_2);
$parser->parseMultiple();
$parser->loadMultiple(['one'=> $quill_json_1, 'two' => $quill_json_2)->parseMultiple();
echo $renderer->load($parser->deltasByIndex('one'))->render();
echo $renderer->load($parser->deltasByIndex('two'))->render();
Expand Down
6 changes: 4 additions & 2 deletions src/Delta/Html/Bold.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'bold' attribute
*
Expand All @@ -21,7 +23,7 @@ class Bold extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 'strong';
$this->tag = Options::TAG_BOLD;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
20 changes: 11 additions & 9 deletions src/Delta/Html/Compound.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Compound HTML delta, collects all the attributes for a compound insert and returns the generated HTML
*
Expand Down Expand Up @@ -49,24 +51,24 @@ private function tags(): void
{
foreach ($this->attributes as $attribute => $value) {
switch ($attribute) {
case 'bold':
$this->tags[] = 'strong';
case Options::ATTRIBUTE_BOLD:
$this->tags[] = Options::TAG_BOLD;
break;

case 'italic':
$this->tags[] = 'em';
case Options::ATTRIBUTE_ITALIC:
$this->tags[] = Options::TAG_ITALIC;
break;

case 'script':
case Options::ATTRIBUTE_SCRIPT:
$this->tags[] = $value;
break;

case 'strike':
$this->tags[] = 's';
case Options::ATTRIBUTE_STRIKE:
$this->tags[] = Options::TAG_STRIKE;
break;

case 'underline':
$this->tags[] = 'u';
case Options::ATTRIBUTE_UNDERLINE:
$this->tags[] = Options::TAG_UNDERLINE;
break;

default:
Expand Down
14 changes: 14 additions & 0 deletions src/Delta/Html/Delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,18 @@ public function setNewLine(bool $value = true): Delta

return $this;
}

/**
* Generate the HTML fragment for a simple insert replacement
*
* @param string $tag HTML tag to wrap around insert
* @param string $insert Insert for tag
* @param boolean $new_line Append a new line
*
* @return string
*/
protected function renderSimpleTag($tag, $insert, $new_line = false): string
{
return "<{$tag}>{$insert}</{$tag}>" . ($new_line === true ? "\n" : null);
}
}
6 changes: 4 additions & 2 deletions src/Delta/Html/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'Header' attribute
*
Expand All @@ -24,7 +26,7 @@ public function __construct(string $insert, array $attributes = [])
$this->insert = $insert;
$this->attributes = $attributes;

$this->tag = 'h' . $this->attributes['header'];
$this->tag = Options::TAG_HEADER . $this->attributes['header'];
}

/**
Expand All @@ -44,6 +46,6 @@ public function displayType(): string
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert, true);
}
}
2 changes: 1 addition & 1 deletion src/Delta/Html/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function render(): string
}

if ($this->newLine() === true) {
$html .= '<br />';
$html .= "<br />\n";
}

return $html;
Expand Down
6 changes: 4 additions & 2 deletions src/Delta/Html/Italic.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'italic' attribute
*
Expand All @@ -21,7 +23,7 @@ class Italic extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 'em';
$this->tag = Options::TAG_ITALIC;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
16 changes: 9 additions & 7 deletions src/Delta/Html/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'list' attribute
*
Expand All @@ -24,7 +26,7 @@ public function __construct(string $insert, array $attributes = [])
$this->insert = $insert;
$this->attributes = $attributes;

$this->tag = 'li';
$this->tag = Options::TAG_LIST_ITEM;
}

/**
Expand Down Expand Up @@ -55,15 +57,15 @@ public function isChild(): bool
public function parentTag(): ?string
{
switch ($this->attributes['list']) {
case 'ordered':
return 'ol';
case Options::ATTRIBUTE_LIST_ORDERED:
return Options::TAG_LIST_ORDERED;
break;
case 'bullet':
return 'ul';
case Options::ATTRIBUTE_LIST_BULLET:
return Options::TAG_LIST_UNORDERED;
break;

default:
return 'ul';
return Options::TAG_LIST_UNORDERED;
break;
}
}
Expand All @@ -75,6 +77,6 @@ public function parentTag(): ?string
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert, true);
}
}
6 changes: 4 additions & 2 deletions src/Delta/Html/Strike.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'strike' attribute
*
Expand All @@ -21,7 +23,7 @@ class Strike extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 's';
$this->tag = Options::TAG_STRIKE;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
6 changes: 4 additions & 2 deletions src/Delta/Html/SubScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'script' attribute set to 'sub'
*
Expand All @@ -21,7 +23,7 @@ class SubScript extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 'sub';
$this->tag = Options::TAG_SUB_SCRIPT;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
6 changes: 4 additions & 2 deletions src/Delta/Html/SuperScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'script' attribute set to 'super'
*
Expand All @@ -21,7 +23,7 @@ class SuperScript extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 'sup';
$this->tag = Options::TAG_SUPER_SCRIPT;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
6 changes: 4 additions & 2 deletions src/Delta/Html/Underline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DBlackborough\Quill\Delta\Html;

use DBlackborough\Quill\Options;

/**
* Default delta class for inserts with the 'underline' attribute
*
Expand All @@ -21,7 +23,7 @@ class Underline extends Delta
*/
public function __construct(string $insert, array $attributes = [])
{
$this->tag = 'u';
$this->tag = Options::TAG_UNDERLINE;

$this->insert = $insert;
$this->attributes = $attributes;
Expand All @@ -34,6 +36,6 @@ public function __construct(string $insert, array $attributes = [])
*/
public function render(): string
{
return "<{$this->tag}>{$this->insert}</{$this->tag}>";
return $this->renderSimpleTag($this->tag, $this->insert);
}
}
41 changes: 41 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace DBlackborough\Quill;

/**
* Options for Quill
*
* @author Dean Blackborough <dean@g3d-development.com>
* @copyright Dean Blackborough
* @license https://github.com/deanblackborough/php-quill-renderer/blob/master/LICENSE
*/
class Options
{
public const FORMAT_HTML = 'HTML';

public const TAG_BOLD = 'strong';
public const TAG_HEADER = 'h';
public const TAG_ITALIC = 'em';
public const TAG_LIST_ITEM = 'li';
public const TAG_STRIKE = 's';
public const TAG_SUB_SCRIPT = 'sub';
public const TAG_SUPER_SCRIPT = 'sup';
public const TAG_UNDERLINE = 'u';

public const TAG_LIST_ORDERED = 'ol';
public const TAG_LIST_UNORDERED = 'ul';

public const ATTRIBUTE_BOLD = 'bold';
public const ATTRIBUTE_HEADER = 'header';
public const ATTRIBUTE_ITALIC = 'italic';
public const ATTRIBUTE_LINK = 'link';
public const ATTRIBUTE_LIST = 'list';
public const ATTRIBUTE_LIST_ORDERED = 'ordered';
public const ATTRIBUTE_LIST_BULLET = 'bullet';
public const ATTRIBUTE_SCRIPT = 'script';
public const ATTRIBUTE_SCRIPT_SUB = 'sub';
public const ATTRIBUTE_SCRIPT_SUPER = 'super';
public const ATTRIBUTE_STRIKE = 'strike';
public const ATTRIBUTE_UNDERLINE = 'underline';
}

0 comments on commit 3d1e448

Please sign in to comment.