diff --git a/composer.json b/composer.json index e1dbad29..242d7d9d 100755 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/flextype/core/Parsers/Parsers.php b/src/flextype/core/Parsers/Parsers.php index c18c965b..351590a1 100644 --- a/src/flextype/core/Parsers/Parsers.php +++ b/src/flextype/core/Parsers/Parsers.php @@ -37,4 +37,12 @@ class Parsers { return Markdown::getInstance(); } + + /** + * Create a Textile instance. + */ + public function textile(): Textile + { + return Textile::getInstance(); + } } diff --git a/src/flextype/core/Parsers/Textile.php b/src/flextype/core/Parsers/Textile.php new file mode 100644 index 00000000..2621e32a --- /dev/null +++ b/src/flextype/core/Parsers/Textile.php @@ -0,0 +1,137 @@ +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(); + } +} diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 3b598daf..c209e97d 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -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: diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 0be4cc09..4e9b40c5 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -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: diff --git a/tests/src/flextype/core/Parsers/TextileTest.php b/tests/src/flextype/core/Parsers/TextileTest.php new file mode 100644 index 00000000..589e438b --- /dev/null +++ b/tests/src/flextype/core/Parsers/TextileTest.php @@ -0,0 +1,16 @@ +assertInstanceOf(Flextype\Parsers\Textile::class, parsers()->textile()->getInstance()); +}); + +test('parse', function () { + $this->assertEquals('
Bold
', trim(parsers()->textile()->parse('**Bold**'))); +}); + +test('get cache ID', function () { + $this->assertNotEquals(parsers()->textile()->getCacheID('**Bold**'), + parsers()->textile()->getCacheID('**Bold Text**')); +});