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

92 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2024-06-20 14:10:42 +00:00
<?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(
2024-09-05 17:51:48 +00:00
'/^\+\+(?!\+)(.*?([^\+\\\\]|(?<=\\\\)\+|(?<=\\\\)\\\\))\+\+(?!\+)/s',
2024-06-20 14:10:42 +00:00
$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(
2024-09-05 17:51:48 +00:00
'/^--(?!-)(.*?([^-\\\\]|(?<=\\\\)-|(?<=\\\\)\\\\))--(?!-)/s',
2024-06-20 14:10:42 +00:00
$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);
}