No call by reference anymore

This commit is contained in:
Michael 2023-04-01 23:17:39 +00:00
parent 70092a1aff
commit 0c7be66d79

View File

@ -32,6 +32,8 @@ use Friendica\Model\Post;
*/ */
class NPF class NPF
{ {
static $heading_subtype = [];
static public function fromBBCode(string $bbcode, int $uri_id): array static public function fromBBCode(string $bbcode, int $uri_id): array
{ {
$bbcode = self::prepareBody($bbcode); $bbcode = self::prepareBody($bbcode);
@ -47,18 +49,30 @@ class NPF
return []; return [];
} }
self::setHeadingSubStyles($doc);
$element = $doc->getElementsByTagName('body')->item(0); $element = $doc->getElementsByTagName('body')->item(0);
echo $element->ownerDocument->saveHTML($element) . "\n"; // echo $element->ownerDocument->saveHTML($element) . "\n";
$npf = []; list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, true, []);
$text = '';
$formatting = [];
self::routeChildren($element, $uri_id, true, [], $npf, $text, $formatting);
return self::addLinkBlockForUriId($uri_id, 0, $npf); return self::addLinkBlockForUriId($uri_id, 0, $npf);
} }
static function setHeadingSubStyles($doc)
{
self::$heading_subtype = [];
foreach (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'] as $element) {
if ($doc->getElementsByTagName($element)->count() > 0) {
if (empty(self::$heading_subtype)) {
self::$heading_subtype[$element] = 'heading1';
} else {
self::$heading_subtype[$element] = 'heading2';
}
}
}
}
static private function prepareBody(string $body): string static private function prepareBody(string $body): string
{ {
$shared = BBCode::fetchShareAttributes($body); $shared = BBCode::fetchShareAttributes($body);
@ -100,10 +114,10 @@ class NPF
return trim($body); return trim($body);
} }
static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array &$npf, string &$text, array &$formatting) static private function routeChildren(DOMElement $element, int $uri_id, bool $parse_structure, array $callstack, array $npf = [], string $text = '', array $formatting = []): array
{ {
if ($parse_structure && $text) { if ($parse_structure && $text) {
self::addBlock($text, $formatting, $npf, $callstack); list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
} }
$callstack[] = $element->nodeName; $callstack[] = $element->nodeName;
@ -113,21 +127,21 @@ class NPF
switch ($child->nodeName) { switch ($child->nodeName) {
case 'b': case 'b':
case 'strong': case 'strong':
self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'bold', $callstack, $npf, $text, $formatting);
break; break;
case 'i': case 'i':
case 'em': case 'em':
self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'italic', $callstack, $npf, $text, $formatting);
break; break;
case 's': case 's':
self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, 'strikethrough', $callstack, $npf, $text, $formatting);
break; break;
case 'u': case 'u':
case 'span': case 'span':
self::addFormatting($child, $uri_id, '', $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::addFormatting($child, $uri_id, '', $callstack, $npf, $text, $formatting);
break; break;
case 'hr': case 'hr':
@ -148,7 +162,7 @@ class NPF
case 'a': case 'a':
if ($text) { if ($text) {
self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::addInlineLink($child, $uri_id, $callstack, $npf, $text, $formatting);
} else { } else {
$npf = self::addLinkBlock($child, $uri_id, $level, $npf); $npf = self::addLinkBlock($child, $uri_id, $level, $npf);
} }
@ -173,7 +187,7 @@ class NPF
case 'ul': case 'ul':
case 'li': case 'li':
case 'details': case 'details':
self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::routeChildren($child, $uri_id, true, $callstack, $npf, $text, $formatting);
break; break;
default: default:
@ -184,8 +198,9 @@ class NPF
} }
if ($parse_structure && $text) { if ($parse_structure && $text) {
self::addBlock($text, $formatting, $npf, $callstack); list($npf, $text, $formatting) = self::addBlock($text, $formatting, $npf, $callstack);
} }
return [$npf, $text, $formatting];
} }
static private function getLevelByCallstack($callstack): int static private function getLevelByCallstack($callstack): int
@ -199,7 +214,7 @@ class NPF
return max(0, $level - 1); return max(0, $level - 1);
} }
static private function getSubTypeByCallstack($callstack): string static private function getSubTypeByCallstack($callstack, string $text): string
{ {
$subtype = ''; $subtype = '';
foreach ($callstack as $entry) { foreach ($callstack as $entry) {
@ -213,43 +228,49 @@ class NPF
break; break;
case 'h1': case 'h1':
$subtype = 'heading1'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'h2': case 'h2':
$subtype = 'heading1'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'h3': case 'h3':
$subtype = 'heading1'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'h4': case 'h4':
$subtype = 'heading2'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'h5': case 'h5':
$subtype = 'heading2'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'h6': case 'h6':
$subtype = 'heading2'; $subtype = self::$heading_subtype[$entry];
break; break;
case 'blockquote': case 'blockquote':
$subtype = strlen($text) < 100 ? 'quote' : 'indented';
break;
case 'pre': case 'pre':
case 'code':
$subtype = 'indented'; $subtype = 'indented';
break; break;
case 'code':
$subtype = 'chat';
break;
} }
} }
return $subtype; return $subtype;
} }
static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array &$npf, string &$text, array &$formatting) static private function addFormatting(DOMElement $element, int $uri_id, string $type, array $callstack, array $npf, string $text, array $formatting): array
{ {
$start = mb_strlen($text); $start = mb_strlen($text);
self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
if (!empty($type)) { if (!empty($type)) {
$formatting[] = [ $formatting[] = [
@ -258,12 +279,13 @@ class NPF
'type' => $type 'type' => $type
]; ];
} }
return [$npf, $text, $formatting];
} }
static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array &$npf, string &$text, array &$formatting) static private function addInlineLink(DOMElement $element, int $uri_id, array $callstack, array $npf, string $text, array $formatting): array
{ {
$start = mb_strlen($text); $start = mb_strlen($text);
self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting); list($npf, $text, $formatting) = self::routeChildren($element, $uri_id, false, $callstack, $npf, $text, $formatting);
$attributes = []; $attributes = [];
foreach ($element->attributes as $key => $attribute) { foreach ($element->attributes as $key => $attribute) {
@ -277,13 +299,14 @@ class NPF
'url' => $attributes['href'] 'url' => $attributes['href']
]; ];
} }
return [$npf, $text, $formatting];
} }
static private function addBlock(string &$text, array &$formatting, array &$npf, array $callstack) static private function addBlock(string $text, array $formatting, array $npf, array $callstack): array
{ {
$block = [ $block = [
'callstack' => $callstack,
'type' => 'text', 'type' => 'text',
'subtype' => '',
'text' => $text, 'text' => $text,
]; ];
@ -296,14 +319,17 @@ class NPF
$block['indent_level'] = $level; $block['indent_level'] = $level;
} }
$subtype = self::getSubTypeByCallstack($callstack); $subtype = self::getSubTypeByCallstack($callstack, $text);
if ($subtype) { if ($subtype) {
$block['subtype'] = $subtype; $block['subtype'] = $subtype;
} else {
unset($block['subtype']);
} }
$npf[] = $block; $npf[] = $block;
$text = ''; $text = '';
$formatting = []; $formatting = [];
return [$npf, $text, $formatting];
} }
static private function addPoster(array $media, array $block): array static private function addPoster(array $media, array $block): array