mirror of
https://github.com/flextype/flextype.git
synced 2025-08-07 05:36:54 +02:00
feat(parsers): add Textile parser
This commit is contained in:
@@ -61,7 +61,8 @@
|
||||
"symfony/console": "^5.4.3",
|
||||
"symfony/var-exporter": "^5.4.3",
|
||||
"thermage/thermage": "^0.19.0",
|
||||
"colinodell/json5": "^2.2"
|
||||
"colinodell/json5": "^2.2",
|
||||
"netcarver/textile": "^3.7"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-zend-opcache": "Recommended for better performance",
|
||||
|
@@ -37,4 +37,12 @@ class Parsers
|
||||
{
|
||||
return Markdown::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Textile instance.
|
||||
*/
|
||||
public function textile(): Textile
|
||||
{
|
||||
return Textile::getInstance();
|
||||
}
|
||||
}
|
||||
|
137
src/flextype/core/Parsers/Textile.php
Normal file
137
src/flextype/core/Parsers/Textile.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
namespace Flextype\Parsers;
|
||||
|
||||
use Exception;
|
||||
use Netcarver\Textile\Parser;
|
||||
|
||||
use function cache;
|
||||
use function registry;
|
||||
use function strings;
|
||||
|
||||
final class Textile
|
||||
{
|
||||
/**
|
||||
* Markdown instance.
|
||||
*/
|
||||
private static ?Textile $instance = null;
|
||||
|
||||
/**
|
||||
* Textile Environment
|
||||
*/
|
||||
private $environment = null;
|
||||
|
||||
/**
|
||||
* Textile should not be cloneable.
|
||||
*/
|
||||
protected function __clone()
|
||||
{
|
||||
throw new Exception('Cannot clone a Textile.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Textile should not be restorable from strings.
|
||||
*/
|
||||
public function __wakeup(): void
|
||||
{
|
||||
throw new Exception('Cannot unserialize a Textile.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Textile construct
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$parser = new Parser();
|
||||
|
||||
foreach (registry()->get('flextype.settings.parsers.textile') as $key => $value) {
|
||||
if ($key == 'cache') continue;
|
||||
if ($key == 'symbol') {
|
||||
if (count($value) > 0 && is_array($value)) {
|
||||
foreach ($value as $name => $val) {
|
||||
$parser->setSymbol($name, $val);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$parser->{'set' . strings($key)->camel()->ucfirst()}($value);
|
||||
}
|
||||
|
||||
$this->environment = $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the instance via lazy initialization (created on first usage).
|
||||
*/
|
||||
public static function getInstance(): Textile
|
||||
{
|
||||
if (static::$instance === null) {
|
||||
static::$instance = new self();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Textile Environment
|
||||
*/
|
||||
public function environment(): Parser
|
||||
{
|
||||
return $this->environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a TEXTILE encoded string and converts it into a PHP variable.
|
||||
*
|
||||
* @param string $input A string containing TEXTILE
|
||||
*
|
||||
* @return mixed The TEXTILE converted to a PHP value
|
||||
*/
|
||||
public function parse(string $input)
|
||||
{
|
||||
$cache = registry()->get('flextype.settings.parsers.textile.cache');
|
||||
|
||||
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {
|
||||
$key = $this->getCacheID($input);
|
||||
|
||||
if ($dataFromCache = cache()->get($key)) {
|
||||
return $dataFromCache;
|
||||
}
|
||||
|
||||
$data = $this->environment()->parse($input);
|
||||
cache()->set($key, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $this->environment()->parse($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Cache ID for textile.
|
||||
*
|
||||
* @param string $input Input.
|
||||
*
|
||||
* @return string Cache ID.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getCacheID(string $input): string
|
||||
{
|
||||
return strings('textile' . $input)->hash()->toString();
|
||||
}
|
||||
}
|
@@ -549,6 +549,21 @@ parsers:
|
||||
max_nesting_level: 9223372036854775807
|
||||
slug_normalizer:
|
||||
max_length: 255
|
||||
textile:
|
||||
cache: true
|
||||
restricted: false
|
||||
document_type: 'xhtml'
|
||||
document_root_directory: ''
|
||||
lite: false
|
||||
images: true
|
||||
link_relation_ship: ''
|
||||
raw_blocks: false
|
||||
block_tags: true
|
||||
line_wrap: true
|
||||
image_prefix: ''
|
||||
link_prefix: ''
|
||||
symbol: []
|
||||
dimensionless_images: true
|
||||
shortcodes:
|
||||
cache: true
|
||||
shortcodes:
|
||||
|
15
tests/fixtures/settings/settings.yaml
vendored
15
tests/fixtures/settings/settings.yaml
vendored
@@ -536,6 +536,21 @@ parsers:
|
||||
max_nesting_level: 9223372036854775807
|
||||
slug_normalizer:
|
||||
max_length: 255
|
||||
textile:
|
||||
cache: true
|
||||
restricted: false
|
||||
document_type: 'xhtml'
|
||||
document_root_directory: ''
|
||||
lite: false
|
||||
images: true
|
||||
link_relation_ship: ''
|
||||
raw_blocks: false
|
||||
block_tags: true
|
||||
line_wrap: true
|
||||
image_prefix: ''
|
||||
link_prefix: ''
|
||||
symbol: []
|
||||
dimensionless_images: true
|
||||
shortcodes:
|
||||
cache: true
|
||||
shortcodes:
|
||||
|
16
tests/src/flextype/core/Parsers/TextileTest.php
Normal file
16
tests/src/flextype/core/Parsers/TextileTest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
test('get textile instance', function () {
|
||||
$this->assertInstanceOf(Flextype\Parsers\Textile::class, parsers()->textile()->getInstance());
|
||||
});
|
||||
|
||||
test('parse', function () {
|
||||
$this->assertEquals('<p><b>Bold</b></p>', trim(parsers()->textile()->parse('**Bold**')));
|
||||
});
|
||||
|
||||
test('get cache ID', function () {
|
||||
$this->assertNotEquals(parsers()->textile()->getCacheID('**Bold**'),
|
||||
parsers()->textile()->getCacheID('**Bold Text**'));
|
||||
});
|
Reference in New Issue
Block a user