leilukin-tumbleblog/includes/lib/xenocrat/markdown/inline/SupSubTrait.php

92 lines
1.6 KiB
PHP

<?php
/**
* @copyright Copyright 2023 Daniel Pimley
* @license https://github.com/xenocrat/chyrp-markdown/blob/master/LICENSE
* @link https://github.com/xenocrat/chyrp-markdown#readme
*/
namespace xenocrat\markdown\inline;
/**
* Adds superscript and subscript inline elements.
*/
trait SupSubTrait
{
protected function parseSupMarkers(): array
{
return array('++');
}
/**
* Parses the superscript feature.
*
* @marker ++
*/
protected function parseSup($markdown): array
{
if (
preg_match(
'/^\+\+(?!\+)(.*?([^\+\\\\]|(?<=\\\\)\+|(?<=\\\\)\\\\))\+\+(?!\+)/s',
$markdown,
$matches
)
) {
return [
[
'sup',
$this->parseInline($matches[1])
],
strlen($matches[0])
];
}
return [['text', $markdown[0] . $markdown[1]], 2];
}
protected function renderSup($block): string
{
return '<sup>'
. $this->renderAbsy($block[1])
. '</sup>';
}
protected function parseSubMarkers(): array
{
return array('--');
}
/**
* Parses the subscript feature.
*
* @marker --
*/
protected function parseSub($markdown): array
{
if (
preg_match(
'/^--(?!-)(.*?([^-\\\\]|(?<=\\\\)-|(?<=\\\\)\\\\))--(?!-)/s',
$markdown,
$matches
)
) {
return [
[
'sub',
$this->parseInline($matches[1])
],
strlen($matches[0])
];
}
return [['text', $markdown[0] . $markdown[1]], 2];
}
protected function renderSub($block): string
{
return '<sub>'
. $this->renderAbsy($block[1])
. '</sub>';
}
abstract protected function parseInline($text);
abstract protected function renderAbsy($blocks);
}