first version
This commit is contained in:
127
kirby/src/Text/KirbyTag.php
Executable file
127
kirby/src/Text/KirbyTag.php
Executable file
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Text;
|
||||
|
||||
use Closure;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Exception\BadMethodCallException;
|
||||
|
||||
/**
|
||||
* Representation and parse of a single KirbyTag.
|
||||
*/
|
||||
class KirbyTag
|
||||
{
|
||||
public static $aliases = [];
|
||||
public static $types = [];
|
||||
|
||||
public $attrs = [];
|
||||
public $data = [];
|
||||
public $options = [];
|
||||
public $type = null;
|
||||
public $value = null;
|
||||
|
||||
public function __call(string $name, array $arguments = [])
|
||||
{
|
||||
return $this->data[$name] ?? $this->$name;
|
||||
}
|
||||
|
||||
public static function __callStatic(string $type, array $arguments = [])
|
||||
{
|
||||
return (new static($type, ...$arguments))->render();
|
||||
}
|
||||
|
||||
public function __construct(string $type, string $value = null, array $attrs = [], array $data = [], array $options = [])
|
||||
{
|
||||
if (isset(static::$types[$type]) === false) {
|
||||
if (isset(static::$aliases[$type]) === false) {
|
||||
throw new InvalidArgumentException('Undefined tag type: ' . $type);
|
||||
}
|
||||
|
||||
$type = static::$aliases[$type];
|
||||
}
|
||||
|
||||
foreach ($attrs as $attrName => $attrValue) {
|
||||
$this->$attrName = $attrValue;
|
||||
}
|
||||
|
||||
$this->attrs = $attrs;
|
||||
$this->data = $data;
|
||||
$this->options = $options;
|
||||
$this->$type = $value;
|
||||
$this->type = $type;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function __get(string $attr)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function attr(string $name, $default = null)
|
||||
{
|
||||
return $this->$name ?? $default;
|
||||
}
|
||||
|
||||
public static function factory(...$arguments)
|
||||
{
|
||||
return (new static(...$arguments))->render();
|
||||
}
|
||||
|
||||
public static function parse(string $string, array $data = [], array $options = []): self
|
||||
{
|
||||
// remove the brackets, extract the first attribute (the tag type)
|
||||
$tag = trim(rtrim(ltrim($string, '('), ')'));
|
||||
$type = trim(substr($tag, 0, strpos($tag, ':')));
|
||||
$attr = static::$types[$type]['attr'] ?? [];
|
||||
|
||||
// the type should be parsed as an attribute, so we add it here
|
||||
// to the list of possible attributes
|
||||
array_unshift($attr, $type);
|
||||
|
||||
// extract all attributes
|
||||
$regex = sprintf('/(%s):/i', implode('|', $attr));
|
||||
$search = preg_split($regex, $tag, false, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
|
||||
|
||||
// $search is now an array with alternating keys and values
|
||||
// convert it to arrays of keys and values
|
||||
$chunks = array_chunk($search, 2);
|
||||
$keys = array_column($chunks, 0);
|
||||
$values = array_map('trim', array_column($chunks, 1));
|
||||
|
||||
// ensure that there is a value for each key
|
||||
// otherwise combining won't work
|
||||
if (count($values) < count($keys)) {
|
||||
$values[] = '';
|
||||
}
|
||||
|
||||
// combine the two arrays to an associative array
|
||||
$attributes = array_combine($keys, $values);
|
||||
|
||||
// the first attribute is the type attribute
|
||||
// extract and pass its value separately
|
||||
$value = array_shift($attributes);
|
||||
|
||||
return new static($type, $value, $attributes, $data, $options);
|
||||
}
|
||||
|
||||
public function option(string $key, $default = null)
|
||||
{
|
||||
return $this->options[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$callback = static::$types[$this->type]['html'] ?? null;
|
||||
|
||||
if (is_a($callback, Closure::class) === true) {
|
||||
return (string)$callback($this);
|
||||
}
|
||||
|
||||
throw new BadMethodCallException('Invalid tag render function in tag: ' . $this->type);
|
||||
}
|
||||
|
||||
public function type(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
27
kirby/src/Text/KirbyTags.php
Executable file
27
kirby/src/Text/KirbyTags.php
Executable file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Text;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Parses and converts custom kirbytags in any
|
||||
* given string. KiryTags are defined via
|
||||
* `KirbyTag::$types`. The default tags for the
|
||||
* Cms are located in `kirby/config/tags.php`
|
||||
*/
|
||||
class KirbyTags
|
||||
{
|
||||
protected static $tagClass = KirbyTag::class;
|
||||
|
||||
public static function parse(string $text = null, array $data = [], array $options = []): string
|
||||
{
|
||||
return preg_replace_callback('!(?=[^\]])\([a-z0-9_-]+:.*?\)!is', function ($match) use ($data, $options) {
|
||||
try {
|
||||
return static::$tagClass::parse($match[0], $data, $options)->render();
|
||||
} catch (Exception $e) {
|
||||
return $match[0];
|
||||
}
|
||||
}, $text);
|
||||
}
|
||||
}
|
76
kirby/src/Text/Markdown.php
Executable file
76
kirby/src/Text/Markdown.php
Executable file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Text;
|
||||
|
||||
use Parsedown;
|
||||
use ParsedownExtra;
|
||||
|
||||
/**
|
||||
* The Markdown class is a wrapper around all sorts of Markdown
|
||||
* parser libraries and is meant to standardize the Markdown parser
|
||||
* API for all Kirby packages.
|
||||
*
|
||||
* It uses Parsedown and ParsedownExtra by default.
|
||||
*
|
||||
* @package Kirby Text
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Markdown
|
||||
{
|
||||
|
||||
/**
|
||||
* Array with all configured options
|
||||
* for the parser
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Returns default values for all
|
||||
* available parser options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function defaults(): array
|
||||
{
|
||||
return [
|
||||
'extra' => false,
|
||||
'breaks' => true
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Markdown parser
|
||||
* with the given options
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$this->options = array_merge($this->defaults(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given text and returns the HTML
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
public function parse(string $text): string
|
||||
{
|
||||
if ($this->options['extra'] === true) {
|
||||
$parser = new ParsedownExtra;
|
||||
} else {
|
||||
$parser = new Parsedown;
|
||||
}
|
||||
|
||||
$parser->setBreaksEnabled($this->options['breaks']);
|
||||
|
||||
// we need the @ here, because parsedown has some notice issues :(
|
||||
return @$parser->text($text);
|
||||
}
|
||||
}
|
130
kirby/src/Text/SmartyPants.php
Executable file
130
kirby/src/Text/SmartyPants.php
Executable file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Text;
|
||||
|
||||
use Michelf\SmartyPantsTypographer;
|
||||
|
||||
/**
|
||||
* Wrapper for Michelf's SmartyPants
|
||||
* parser, to improve the configurability
|
||||
* of the parser with default options and
|
||||
* a simple way to set your own options.
|
||||
*
|
||||
* @package Kirby Text
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class SmartyPants
|
||||
{
|
||||
|
||||
/**
|
||||
* Array with all configured options
|
||||
* for the parser
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Michelf's parser object
|
||||
*
|
||||
* @var SmartyPantsTypographer
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Returns default values for all
|
||||
* available parser options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function defaults(): array
|
||||
{
|
||||
return [
|
||||
'attr' => 1,
|
||||
'doublequote.open' => '“',
|
||||
'doublequote.close' => '”',
|
||||
'doublequote.low' => '„',
|
||||
'singlequote.open' => '‘',
|
||||
'singlequote.close' => '’',
|
||||
'backtick.doublequote.open' => '“',
|
||||
'backtick.doublequote.close' => '”',
|
||||
'backtick.singlequote.open' => '‘',
|
||||
'backtick.singlequote.close' => '’',
|
||||
'emdash' => '—',
|
||||
'endash' => '–',
|
||||
'ellipsis' => '…',
|
||||
'space' => '(?: | | |�*160;|�*[aA]0;)',
|
||||
'space.emdash' => ' ',
|
||||
'space.endash' => ' ',
|
||||
'space.colon' => ' ',
|
||||
'space.semicolon' => ' ',
|
||||
'space.marks' => ' ',
|
||||
'space.frenchquote' => ' ',
|
||||
'space.thousand' => ' ',
|
||||
'space.unit' => ' ',
|
||||
'guillemet.leftpointing' => '«',
|
||||
'guillemet.rightpointing' => '»',
|
||||
'geresh' => '׳',
|
||||
'gershayim' => '״',
|
||||
'skip' => 'pre|code|kbd|script|style|math',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SmartyPants parser
|
||||
* with the given options
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$this->options = array_merge($this->defaults(), $options);
|
||||
$this->parser = new SmartyPantsTypographer($this->options['attr']);
|
||||
|
||||
// configuration
|
||||
$this->parser->smart_doublequote_open = $this->options['doublequote.open'];
|
||||
$this->parser->smart_doublequote_close = $this->options['doublequote.close'];
|
||||
$this->parser->smart_singlequote_open = $this->options['singlequote.open'];
|
||||
$this->parser->smart_singlequote_close = $this->options['singlequote.close'];
|
||||
$this->parser->backtick_doublequote_open = $this->options['backtick.doublequote.open'];
|
||||
$this->parser->backtick_doublequote_close = $this->options['backtick.doublequote.close'];
|
||||
$this->parser->backtick_singlequote_open = $this->options['backtick.singlequote.open'];
|
||||
$this->parser->backtick_singlequote_close = $this->options['backtick.singlequote.close'];
|
||||
$this->parser->em_dash = $this->options['emdash'];
|
||||
$this->parser->en_dash = $this->options['endash'];
|
||||
$this->parser->ellipsis = $this->options['ellipsis'];
|
||||
$this->parser->tags_to_skip = $this->options['skip'];
|
||||
$this->parser->space_emdash = $this->options['space.emdash'];
|
||||
$this->parser->space_endash = $this->options['space.endash'];
|
||||
$this->parser->space_colon = $this->options['space.colon'];
|
||||
$this->parser->space_semicolon = $this->options['space.semicolon'];
|
||||
$this->parser->space_marks = $this->options['space.marks'];
|
||||
$this->parser->space_frenchquote = $this->options['space.frenchquote'];
|
||||
$this->parser->space_thousand = $this->options['space.thousand'];
|
||||
$this->parser->space_unit = $this->options['space.unit'];
|
||||
$this->parser->doublequote_low = $this->options['doublequote.low'];
|
||||
$this->parser->guillemet_leftpointing = $this->options['guillemet.leftpointing'];
|
||||
$this->parser->guillemet_rightpointing = $this->options['guillemet.rightpointing'];
|
||||
$this->parser->geresh = $this->options['geresh'];
|
||||
$this->parser->gershayim = $this->options['gershayim'];
|
||||
$this->parser->space = $this->options['space'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given text
|
||||
*
|
||||
* @param string $text
|
||||
* @return string
|
||||
*/
|
||||
public function parse(string $text): string
|
||||
{
|
||||
// prepare the text
|
||||
$text = str_replace('"', '"', $text);
|
||||
|
||||
// parse the text
|
||||
return $this->parser->transform($text);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user