', '#', '+', '-', '.', '!', '|', '?', '"', "'", '<', ]; /** * Regexes for html strong * * @var array * @since 1.0.0 */ protected array $strongRegex = [ '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', ]; /** * Regexes for html underline * * @var array * @since 1.0.0 */ protected array $underlineRegex = [ '_' => '/^[_]{2}((?:\\\\\_|[^_]|[_][^_]_+[_])+?)[_]{2}(?![_])/s', ]; /** * Regexes for html emphasizes * * @var array * @since 1.0.0 */ protected array $emRegex = [ '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', ]; /** * Regex for html attributes * * @var string * @since 1.0.0 */ protected string $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+'; /** * Regex for for classes and ids * * @var string * @since 1.0.0 */ protected string $regexAttribute = '(?:[#.][-\w]+[ ]*)'; /** * Elements without closing * * @var string[] * @since 1.0.0 */ protected array $voidElements = [ 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', ]; /** * Text elements * * @var string[] * @since 1.0.0 */ protected array $textLevelElements = [ 'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont', 'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing', 'i', 'rp', 'del', 'code', 'strike', 'marquee', 'q', 'rt', 'ins', 'font', 'strong', 's', 'tt', 'kbd', 'mark', 'u', 'xm', 'sub', 'nobr', 'sup', 'ruby', 'var', 'span', 'wbr', 'time', ]; /** * Inline special characters (not block elements) * * @var array * @since 1.0.0 */ protected array $inlineTypes = [ '!' => ['Image'], '*' => ['Emphasis'], '_' => ['Emphasis'], '&' => ['SpecialCharacter'], '[' => ['FootnoteMarker', 'Link'], ':' => ['Url'], '<' => ['UrlTag', 'EmailTag', 'Markup'], '`' => ['Code'], '~' => ['Strikethrough'], '\\' => ['EscapeSequence'], ]; /** * Inline special characters (not block elements) (see $inlineTypes) * * @var string * @since 1.0.0 */ protected string $inlineMarkerList = '!*_&[:<`~'; /** * Uses strict mode? * * Less forgiving with regards to formatting. * * @var bool * @since 1.0.0 */ public bool $strictMode = false; /** * Uses safe mode (true -> no html allowed)? * * Important for parsing or sanitizing html. * * @var bool * @since 1.0.0 */ public bool $safeMode = false; /** * Urls are always handled as links * * @var bool * @since 1.0.0 */ public bool $urlsLinked = true; /** * Should html get escaped? * * @var bool * @since 1.0.0 */ public bool $markupEscaped = false; /** * Replaces new lines with
in inline elements * * true -> replaces new line with br * false -> replaces double whitespace followed with new line with br * * @var bool * @since 1.0.0 */ public bool $breaksEnabled = false; /** * Save link prefixes * * @var string[] * @since 1.0.0 */ protected array $safeLinksWhitelist = [ 'http://', 'https://', 'ftp://', 'ftps://', 'mailto:', 'tel:', 'data:image/png;base64,', 'data:image/gif;base64,', 'data:image/jpeg;base64,', 'irc:', 'ircs:', 'git:', 'ssh:', 'news:', 'steam:', ]; /** * Block special characters * * @var array * @since 1.0.0 */ protected array $blockTypes = [ '#' => ['Header'], '*' => ['Rule', 'List', 'Abbreviation'], '+' => ['List'], '-' => ['SetextHeader', 'Table', 'Rule', 'List'], '0' => ['List'], '1' => ['List'], '2' => ['List'], '3' => ['List'], '4' => ['List'], '5' => ['List'], '6' => ['List'], '7' => ['List'], '8' => ['List'], '9' => ['List'], ':' => ['Table', 'DefinitionList'], '<' => ['Comment', 'Markup'], '=' => ['SetextHeader'], '>' => ['Quote'], '[' => ['Footnote', 'Reference'], '_' => ['Rule'], '`' => ['FencedCode'], '|' => ['Table'], ]; /** * Unmarked block types * * @var string[] * @since 1.0.0 */ protected array $unmarkedBlockTypes = [ 'Code', ]; /** * Is continuable * * @var string[] * @since 1.0.0 */ private const CONTINUABLE = [ 'Code', 'Comment', 'FencedCode', 'List', 'Quote', 'Table', 'Math', 'Spoiler', 'Checkbox', 'Footnote', 'DefinitionList', 'Markup', ]; /** * Is completable * * @var string[] * @since 1.0.0 */ private const COMPLETABLE = [ 'Math', 'Spoiler', 'Table', 'Checkbox', 'Footnote', 'Markup', 'Code', 'FencedCode', 'List', ]; /** * Parsing options * * @var array * @since 1.0.0 */ private array $options = []; /** * Definition data * * E.g. abbreviations, footnotes * * @var array * @since 1.0.0 */ protected array $definitionData = []; // TOC: start /** * Table of content id * * @var string * @since 1.0.0 */ public string $idToc = 'toc'; /** * TOC array after parsing headers * * @var array{}|array{text:string, id:string, level:string} * @since 1.0.0 */ protected array $contentsListArray = []; /** * TOC string after parsing headers * * @var string * @since 1.0.0 */ protected string $contentsListString = ''; /** * First head level * * @var int * @since 1.0.0 */ protected int $firstHeadLevel = 0; /** * Is header blacklist (for table of contents/TOC) initialized * * @var bool * @since 1.0.0 */ protected bool $isBlacklistInitialized = false; /** * Header duplicates (same header text) * * @var array * @since 1.0.0 */ protected array $anchorDuplicates = []; // TOC: end /** * Footnote count * * @var int * @since 1.0.0 */ private int $footnoteCount = 0; /** * Current abbreviation * * @var string * @since 1.0.0 */ private string $currentAbbreviation; /** * Current abbreviation meaning * * @var string * @since 1.0.0 */ private string $currentMeaning; /** * Instances * * @var array * @since 1.0.0 */ private static $instances = []; /** * Clean up state * * @return void * * @since 1.0.0 */ public function clean() : void { $this->definitionData = []; $this->contentsListArray = []; $this->contentsListString = ''; $this->firstHeadLevel = 0; $this->anchorDuplicates = []; $this->footnoteCount = 0; $this->currentAbbreviation = ''; $this->currentMeaning = ''; } /** * Create instance for static use * * @param string $name Instance name * * @return self * * @since 1.0.0 */ public static function getInstance(string $name = 'default') : self { if (isset(self::$instances[$name])) { $obj = self::$instances[$name]; $obj->clean(); return self::$instances[$name]; } $instance = new self(); self::$instances[$name] = $instance; return $instance; } /** * Constructor. * * @param array $params Parameters * * @since 1.0.0 */ public function __construct(array $params = []) { $this->options = $params; $this->options['toc'] = $this->options['toc'] ?? false; // Marks if ($this->options['mark'] ?? true) { $this->inlineTypes['='][] = 'Mark'; $this->inlineMarkerList .= '='; } // Keystrokes if ($this->options['keystrokes'] ?? true) { $this->inlineTypes['['][] = 'Keystrokes'; } // Spoiler if ($this->options['spoiler'] ?? false) { $this->inlineTypes['>'][] = 'Spoiler'; $this->inlineMarkerList .= '>'; } // Inline Math if ($this->options['math'] ?? false) { $this->inlineTypes['\\'][] = 'Math'; $this->inlineTypes['$'][] = 'Math'; $this->inlineMarkerList .= '$'; } // Superscript if ($this->options['sup'] ?? false) { $this->inlineTypes['^'][] = 'Superscript'; $this->inlineMarkerList .= '^'; } // Subscript if ($this->options['sub'] ?? false) { $this->inlineTypes['~'][] = 'Subscript'; } // Emojis if ($this->options['emojis'] ?? true) { $this->inlineTypes[':'][] = 'Emojis'; } // Typographer if ($this->options['typographer'] ?? false) { $this->inlineTypes['('][] = 'Typographer'; $this->inlineMarkerList .= '('; $this->inlineTypes['.'][] = 'Typographer'; $this->inlineMarkerList .= '.'; $this->inlineTypes['+'][] = 'Typographer'; $this->inlineMarkerList .= '+'; $this->inlineTypes['!'][] = 'Typographer'; $this->inlineTypes['?'][] = 'Typographer'; $this->inlineMarkerList .= '?'; } // Block Math if ($this->options['math'] ?? false) { $this->blockTypes['\\'][] = 'Math'; $this->blockTypes['$'][] = 'Math'; } // Block Spoiler if ($this->options['spoiler'] ?? false) { $this->blockTypes['?'][] = 'Spoiler'; } // Checkbox if ($this->options['lists']['checkbox'] ?? true) { $this->blockTypes['['][] = 'Checkbox'; } // Embedding if ($this->options['embedding'] ?? false) { $this->inlineTypes['['][] = 'Embedding'; } // Map if ($this->options['map'] ?? false) { $this->inlineTypes['['][] = 'Map'; } // Address if ($this->options['address'] ?? false) { $this->inlineTypes['['][] = 'Address'; } // Contact if ($this->options['contact'] ?? false) { $this->inlineTypes['['][] = 'Contact'; } // Progress if ($this->options['progress'] ?? false) { $this->inlineTypes['['][] = 'Progress'; } // Escaping needs to happen at the end $this->inlineMarkerList .= '\\'; } /** * Parses the given markdown string to a HTML * * @param string $text Markdown text to parse * * @return string * * @since 1.0.0 */ public static function parse(string $text) : string { $parsedown = self::getInstance(); return $parsedown->text($text); } /** * Parses the given markdown string to a HTML string but it ignores ToC * * @param string $text Markdown text to parse * * @return string * * @since 1.0.0 */ public function body(string $text) : string { $text = $this->encodeToCTagToHash($text); // Escapes ToC tag temporary $elements = $this->textElements($text); $html = $this->elements($elements); $html = \trim($html, "\n"); // Merge consecutive dl elements $html = \preg_replace('/<\/dl>\s+
\s+/', '', $html) ?? ''; // Add footnotes if (isset($this->definitionData['Footnote'])) { $element = $this->buildFootnoteElement(); $html .= "\n" . $this->element($element); } return $this->decodeToCTagFromHash($html); // Unescape the ToC tag } /** * Parses markdown string to HTML and also the "[toc]" tag as well. * * @param string $text Markdown text to parse * * @return string * * @since 1.0.0 */ public function text(string $text) : string { // Parses the markdown text except the ToC tag. This also searches // the list of contents and available to get from "contentsList()" // method. $html = $this->body($text); if (isset($this->options['toc']) && $this->options['toc'] === false) { return $html; } // Handle toc $tagOrigin = $this->options['toc']['set_toc_tag'] ?? '[toc]'; if (\strpos($text, $tagOrigin) === false) { return $html; } $tocData = $this->contentsList(); $needle = '

' . $tagOrigin . '

'; $replace = '
' . $tocData . '
'; return \str_replace($needle, $replace, $html); } /** * Returns the parsed ToC. * * @param string $typeReturn Type of the return format. "html" or "json". * * @return string HTML/JSON string of ToC * * @since 1.0.0 */ public function contentsList($typeReturn = 'html') : string { if (\strtolower($typeReturn) === 'json') { $json = \json_encode($this->contentsListArray); return $json === false ? '' : $json; } $result = ''; if (!empty($this->contentsListString)) { // Parses the ToC list in markdown to HTML $result = $this->body($this->contentsListString); } return $result; } /** * Handle inline code * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineCode(array $excerpt) : ?array { if (!($this->options['code']['inline'] ?? true) || !($this->options['code'] ?? true) ) { return null; } $marker = $excerpt['text'][0]; if (\preg_match( '/^([' . $marker . ']++)[ ]*+(.+?)[ ]*+(? \strlen($matches[0]), 'element' => [ 'name' => 'code', 'text' => $text, ], ]; } /** * Handle inline email * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineEmailTag(array $excerpt) : ?array { if (!($this->options['links'] ?? true) || !($this->options['links']['email_links'] ?? true) ) { return null; } $hostnameLabel = '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'; $commonMarkEmail = '[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]++@' . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; if (\strpos($excerpt['text'], '>') === false || \preg_match('/^<((mailto:)?' . $commonMarkEmail . ')>/i', $excerpt['text'], $matches) !== 1 ) { return null; } $url = UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $matches[1])); if (!isset($matches[2])) { $url = "mailto:{$url}"; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'a', 'text' => $matches[1], 'attributes' => [ 'href' => $url, ], ], ]; } /** * Inline emphasis * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineEmphasis(array $excerpt) : ?array { if (!($this->options['emphasis'] ?? true) || !isset($excerpt['text'][1]) ) { return null; } $marker = $excerpt['text'][0]; if ($excerpt['text'][1] === $marker && isset($this->strongRegex[$marker]) && \preg_match($this->strongRegex[$marker], $excerpt['text'], $matches) ) { $emphasis = 'strong'; } elseif ($excerpt['text'][1] === $marker && isset($this->underlineRegex[$marker]) && \preg_match($this->underlineRegex[$marker], $excerpt['text'], $matches) ) { $emphasis = 'u'; } elseif (\preg_match($this->emRegex[$marker], $excerpt['text'], $matches)) { $emphasis = 'em'; } else { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => $emphasis, 'handler' => [ 'function' => 'lineElements', 'argument' => $matches[1], 'destination' => 'elements', ], ], ]; } /** * Handle image * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineImage(array $excerpt) : ?array { if (!($this->options['images'] ?? true) || !\str_starts_with($excerpt['text'], '![') ) { return null; } $excerpt['text'] = \substr($excerpt['text'], 1); $link = $this->inlineLink($excerpt); if ($link === null) { return null; } $inline = [ 'extent' => $link['extent'] + 1, 'element' => [ 'name' => 'img', 'attributes' => [ 'src' => $link['element']['attributes']['href'], 'alt' => $link['element']['handler']['argument'], ], 'autobreak' => true, ], ]; $inline['element']['attributes'] += $link['element']['attributes']; unset($inline['element']['attributes']['href']); return $inline; } /** * Handle link * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array * * @since 1.0.0 */ protected function inlineLink(array $excerpt) : ?array { if (!($this->options['links'] ?? true)) { return null; } $link = $this->inlineLinkParent($excerpt); $remainder = $link !== null ? \substr($excerpt['text'], $link['extent']) : ''; if (\preg_match('/^[ ]*{(' . $this->regexAttribute . '+)}/', $remainder, $matches)) { $link['extent'] += \strlen($matches[0]); $link['element']['attributes'] += $this->parseAttributeData($matches[1]); } return $link; } /** * Handle markup * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineMarkup(array $excerpt) : ?array { if (!($this->options['markup'] ?? true) || $this->markupEscaped || $this->safeMode || \strpos($excerpt['text'], '>') === false ) { return null; } $matches = []; if (($excerpt['text'][1] === '/' && \preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $excerpt['text'], $matches)) || ($excerpt['text'][1] === '!' && \preg_match('/^/s', $excerpt['text'], $matches)) || ($excerpt['text'][1] !== ' ' && \preg_match('/^<\w[\w-]*+(?:[ ]*+' . $this->regexHtmlAttribute . ')*+[ ]*+\/?>/s', $excerpt['text'], $matches)) ) { return [ 'extent' => \strlen($matches[0]), 'element' => ['rawHtml' => $matches[0]], ]; } return null; } /** * Handle strikethrough * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineStrikethrough(array $excerpt) : ?array { if (!($this->options['strikethroughs'] ?? true) || !\str_starts_with($excerpt['text'], '~~') || \preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $excerpt['text'], $matches) !== 1 ) { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'del', 'handler' => [ 'function' => 'lineElements', 'argument' => $matches[1], 'destination' => 'elements', ], ], ]; } /** * Handle url * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, position:int, element:array} * * @since 1.0.0 */ protected function inlineUrl(array $excerpt) : ?array { if (!($this->options['links'] ?? true) || !$this->urlsLinked || !\str_starts_with($excerpt['text'], '://') || \strpos($excerpt['context'], 'http') === false || \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $excerpt['context'], $matches, \PREG_OFFSET_CAPTURE) !== 1 ) { return null; } $url = UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $matches[0][0])); return [ 'extent' => \strlen($matches[0][0]), 'position' => $matches[0][1], 'element' => [ 'name' => 'a', 'text' => $url, 'attributes' => [ 'href' => $url, ], ], ]; } /** * Handle url * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineUrlTag(array $excerpt) : ?array { if (!($this->options['links'] ?? true) || \strpos($excerpt['text'], '>') === false || \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $excerpt['text'], $matches) !== 1 ) { return null; } $url = UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $matches[1])); return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'a', 'text' => $url, 'attributes' => [ 'href' => $url, ], ], ]; } /** * Handle emojis * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineEmojis(array $excerpt) : ?array { if (\preg_match('/^(:)([^: ]*?)(:)/', $excerpt['text'], $matches) !== 1) { return null; } $emojiMap = [ ':smile:' => '๐Ÿ˜„', ':laughing:' => '๐Ÿ˜†', ':blush:' => '๐Ÿ˜Š', ':smiley:' => '๐Ÿ˜ƒ', ':relaxed:' => 'โ˜บ๏ธ', ':smirk:' => '๐Ÿ˜', ':heart_eyes:' => '๐Ÿ˜', ':kissing_heart:' => '๐Ÿ˜˜', ':kissing_closed_eyes:' => '๐Ÿ˜š', ':flushed:' => '๐Ÿ˜ณ', ':relieved:' => '๐Ÿ˜Œ', ':satisfied:' => '๐Ÿ˜†', ':grin:' => '๐Ÿ˜', ':wink:' => '๐Ÿ˜‰', ':stuck_out_tongue_winking_eye:' => '๐Ÿ˜œ', ':stuck_out_tongue_closed_eyes:' => '๐Ÿ˜', ':grinning:' => '๐Ÿ˜€', ':kissing:' => '๐Ÿ˜—', ':kissing_smiling_eyes:' => '๐Ÿ˜™', ':stuck_out_tongue:' => '๐Ÿ˜›', ':sleeping:' => '๐Ÿ˜ด', ':worried:' => '๐Ÿ˜Ÿ', ':frowning:' => '๐Ÿ˜ฆ', ':anguished:' => '๐Ÿ˜ง', ':open_mouth:' => '๐Ÿ˜ฎ', ':grimacing:' => '๐Ÿ˜ฌ', ':confused:' => '๐Ÿ˜•', ':hushed:' => '๐Ÿ˜ฏ', ':expressionless:' => '๐Ÿ˜‘', ':unamused:' => '๐Ÿ˜’', ':sweat_smile:' => '๐Ÿ˜…', ':sweat:' => '๐Ÿ˜“', ':disappointed_relieved:' => '๐Ÿ˜ฅ', ':weary:' => '๐Ÿ˜ฉ', ':pensive:' => '๐Ÿ˜”', ':disappointed:' => '๐Ÿ˜ž', ':confounded:' => '๐Ÿ˜–', ':fearful:' => '๐Ÿ˜จ', ':cold_sweat:' => '๐Ÿ˜ฐ', ':persevere:' => '๐Ÿ˜ฃ', ':cry:' => '๐Ÿ˜ข', ':sob:' => '๐Ÿ˜ญ', ':joy:' => '๐Ÿ˜‚', ':astonished:' => '๐Ÿ˜ฒ', ':scream:' => '๐Ÿ˜ฑ', ':tired_face:' => '๐Ÿ˜ซ', ':angry:' => '๐Ÿ˜ ', ':rage:' => '๐Ÿ˜ก', ':triumph:' => '๐Ÿ˜ค', ':sleepy:' => '๐Ÿ˜ช', ':yum:' => '๐Ÿ˜‹', ':mask:' => '๐Ÿ˜ท', ':sunglasses:' => '๐Ÿ˜Ž', ':dizzy_face:' => '๐Ÿ˜ต', ':imp:' => '๐Ÿ‘ฟ', ':smiling_imp:' => '๐Ÿ˜ˆ', ':neutral_face:' => '๐Ÿ˜', ':no_mouth:' => '๐Ÿ˜ถ', ':innocent:' => '๐Ÿ˜‡', ':alien:' => '๐Ÿ‘ฝ', ':yellow_heart:' => '๐Ÿ’›', ':blue_heart:' => '๐Ÿ’™', ':purple_heart:' => '๐Ÿ’œ', ':heart:' => 'โค๏ธ', ':green_heart:' => '๐Ÿ’š', ':broken_heart:' => '๐Ÿ’”', ':heartbeat:' => '๐Ÿ’“', ':heartpulse:' => '๐Ÿ’—', ':two_hearts:' => '๐Ÿ’•', ':revolving_hearts:' => '๐Ÿ’ž', ':cupid:' => '๐Ÿ’˜', ':sparkling_heart:' => '๐Ÿ’–', ':sparkles:' => 'โœจ', ':star:' => 'โญ๏ธ', ':star2:' => '๐ŸŒŸ', ':dizzy:' => '๐Ÿ’ซ', ':boom:' => '๐Ÿ’ฅ', ':collision:' => '๐Ÿ’ฅ', ':anger:' => '๐Ÿ’ข', ':exclamation:' => 'โ—๏ธ', ':question:' => 'โ“', ':grey_exclamation:' => 'โ•', ':grey_question:' => 'โ”', ':zzz:' => '๐Ÿ’ค', ':dash:' => '๐Ÿ’จ', ':sweat_drops:' => '๐Ÿ’ฆ', ':notes:' => '๐ŸŽถ', ':musical_note:' => '๐ŸŽต', ':fire:' => '๐Ÿ”ฅ', ':hankey:' => '๐Ÿ’ฉ', ':poop:' => '๐Ÿ’ฉ', ':shit:' => '๐Ÿ’ฉ', ':+1:' => '๐Ÿ‘', ':thumbsup:' => '๐Ÿ‘', ':-1:' => '๐Ÿ‘Ž', ':thumbsdown:' => '๐Ÿ‘Ž', ':ok_hand:' => '๐Ÿ‘Œ', ':punch:' => '๐Ÿ‘Š', ':facepunch:' => '๐Ÿ‘Š', ':fist:' => 'โœŠ', ':v:' => 'โœŒ๏ธ', ':wave:' => '๐Ÿ‘‹', ':hand:' => 'โœ‹', ':raised_hand:' => 'โœ‹', ':open_hands:' => '๐Ÿ‘', ':point_up:' => 'โ˜๏ธ', ':point_down:' => '๐Ÿ‘‡', ':point_left:' => '๐Ÿ‘ˆ', ':point_right:' => '๐Ÿ‘‰', ':raised_hands:' => '๐Ÿ™Œ', ':pray:' => '๐Ÿ™', ':point_up_2:' => '๐Ÿ‘†', ':clap:' => '๐Ÿ‘', ':muscle:' => '๐Ÿ’ช', ':metal:' => '๐Ÿค˜', ':fu:' => '๐Ÿ–•', ':walking:' => '๐Ÿšถ', ':runner:' => '๐Ÿƒ', ':running:' => '๐Ÿƒ', ':couple:' => '๐Ÿ‘ซ', ':family:' => '๐Ÿ‘ช', ':two_men_holding_hands:' => '๐Ÿ‘ฌ', ':two_women_holding_hands:' => '๐Ÿ‘ญ', ':dancer:' => '๐Ÿ’ƒ', ':dancers:' => '๐Ÿ‘ฏ', ':ok_woman:' => '๐Ÿ™†', ':no_good:' => '๐Ÿ™…', ':information_desk_person:' => '๐Ÿ’', ':raising_hand:' => '๐Ÿ™‹', ':bride_with_veil:' => '๐Ÿ‘ฐ', ':person_with_pouting_face:' => '๐Ÿ™Ž', ':person_frowning:' => '๐Ÿ™', ':bow:' => '๐Ÿ™‡', ':couple_with_heart:' => '๐Ÿ’‘', ':massage:' => '๐Ÿ’†', ':haircut:' => '๐Ÿ’‡', ':nail_care:' => '๐Ÿ’…', ':boy:' => '๐Ÿ‘ฆ', ':girl:' => '๐Ÿ‘ง', ':woman:' => '๐Ÿ‘ฉ', ':man:' => '๐Ÿ‘จ', ':baby:' => '๐Ÿ‘ถ', ':older_woman:' => '๐Ÿ‘ต', ':older_man:' => '๐Ÿ‘ด', ':person_with_blond_hair:' => '๐Ÿ‘ฑ', ':man_with_gua_pi_mao:' => '๐Ÿ‘ฒ', ':man_with_turban:' => '๐Ÿ‘ณ', ':construction_worker:' => '๐Ÿ‘ท', ':cop:' => '๐Ÿ‘ฎ', ':angel:' => '๐Ÿ‘ผ', ':princess:' => '๐Ÿ‘ธ', ':smiley_cat:' => '๐Ÿ˜บ', ':smile_cat:' => '๐Ÿ˜ธ', ':heart_eyes_cat:' => '๐Ÿ˜ป', ':kissing_cat:' => '๐Ÿ˜ฝ', ':smirk_cat:' => '๐Ÿ˜ผ', ':scream_cat:' => '๐Ÿ™€', ':crying_cat_face:' => '๐Ÿ˜ฟ', ':joy_cat:' => '๐Ÿ˜น', ':pouting_cat:' => '๐Ÿ˜พ', ':japanese_ogre:' => '๐Ÿ‘น', ':japanese_goblin:' => '๐Ÿ‘บ', ':see_no_evil:' => '๐Ÿ™ˆ', ':hear_no_evil:' => '๐Ÿ™‰', ':speak_no_evil:' => '๐Ÿ™Š', ':guardsman:' => '๐Ÿ’‚', ':skull:' => '๐Ÿ’€', ':feet:' => '๐Ÿพ', ':lips:' => '๐Ÿ‘„', ':kiss:' => '๐Ÿ’‹', ':droplet:' => '๐Ÿ’ง', ':ear:' => '๐Ÿ‘‚', ':eyes:' => '๐Ÿ‘€', ':nose:' => '๐Ÿ‘ƒ', ':tongue:' => '๐Ÿ‘…', ':love_letter:' => '๐Ÿ’Œ', ':bust_in_silhouette:' => '๐Ÿ‘ค', ':busts_in_silhouette:' => '๐Ÿ‘ฅ', ':speech_balloon:' => '๐Ÿ’ฌ', ':thought_balloon:' => '๐Ÿ’ญ', ':sunny:' => 'โ˜€๏ธ', ':umbrella:' => 'โ˜”๏ธ', ':cloud:' => 'โ˜๏ธ', ':snowflake:' => 'โ„๏ธ', ':snowman:' => 'โ›„๏ธ', ':zap:' => 'โšก๏ธ', ':cyclone:' => '๐ŸŒ€', ':foggy:' => '๐ŸŒ', ':ocean:' => '๐ŸŒŠ', ':cat:' => '๐Ÿฑ', ':dog:' => '๐Ÿถ', ':mouse:' => '๐Ÿญ', ':hamster:' => '๐Ÿน', ':rabbit:' => '๐Ÿฐ', ':wolf:' => '๐Ÿบ', ':frog:' => '๐Ÿธ', ':tiger:' => '๐Ÿฏ', ':koala:' => '๐Ÿจ', ':bear:' => '๐Ÿป', ':pig:' => '๐Ÿท', ':pig_nose:' => '๐Ÿฝ', ':cow:' => '๐Ÿฎ', ':boar:' => '๐Ÿ—', ':monkey_face:' => '๐Ÿต', ':monkey:' => '๐Ÿ’', ':horse:' => '๐Ÿด', ':racehorse:' => '๐ŸŽ', ':camel:' => '๐Ÿซ', ':sheep:' => '๐Ÿ‘', ':elephant:' => '๐Ÿ˜', ':panda_face:' => '๐Ÿผ', ':snake:' => '๐Ÿ', ':bird:' => '๐Ÿฆ', ':baby_chick:' => '๐Ÿค', ':hatched_chick:' => '๐Ÿฅ', ':hatching_chick:' => '๐Ÿฃ', ':chicken:' => '๐Ÿ”', ':penguin:' => '๐Ÿง', ':turtle:' => '๐Ÿข', ':bug:' => '๐Ÿ›', ':honeybee:' => '๐Ÿ', ':ant:' => '๐Ÿœ', ':beetle:' => '๐Ÿž', ':snail:' => '๐ŸŒ', ':octopus:' => '๐Ÿ™', ':tropical_fish:' => '๐Ÿ ', ':fish:' => '๐ŸŸ', ':whale:' => '๐Ÿณ', ':whale2:' => '๐Ÿ‹', ':dolphin:' => '๐Ÿฌ', ':cow2:' => '๐Ÿ„', ':ram:' => '๐Ÿ', ':rat:' => '๐Ÿ€', ':water_buffalo:' => '๐Ÿƒ', ':tiger2:' => '๐Ÿ…', ':rabbit2:' => '๐Ÿ‡', ':dragon:' => '๐Ÿ‰', ':goat:' => '๐Ÿ', ':rooster:' => '๐Ÿ“', ':dog2:' => '๐Ÿ•', ':pig2:' => '๐Ÿ–', ':mouse2:' => '๐Ÿ', ':ox:' => '๐Ÿ‚', ':dragon_face:' => '๐Ÿฒ', ':blowfish:' => '๐Ÿก', ':crocodile:' => '๐ŸŠ', ':dromedary_camel:' => '๐Ÿช', ':leopard:' => '๐Ÿ†', ':cat2:' => '๐Ÿˆ', ':poodle:' => '๐Ÿฉ', ':crab' => '๐Ÿฆ€', ':paw_prints:' => '๐Ÿพ', ':bouquet:' => '๐Ÿ’', ':cherry_blossom:' => '๐ŸŒธ', ':tulip:' => '๐ŸŒท', ':four_leaf_clover:' => '๐Ÿ€', ':rose:' => '๐ŸŒน', ':sunflower:' => '๐ŸŒป', ':hibiscus:' => '๐ŸŒบ', ':maple_leaf:' => '๐Ÿ', ':leaves:' => '๐Ÿƒ', ':fallen_leaf:' => '๐Ÿ‚', ':herb:' => '๐ŸŒฟ', ':mushroom:' => '๐Ÿ„', ':cactus:' => '๐ŸŒต', ':palm_tree:' => '๐ŸŒด', ':evergreen_tree:' => '๐ŸŒฒ', ':deciduous_tree:' => '๐ŸŒณ', ':chestnut:' => '๐ŸŒฐ', ':seedling:' => '๐ŸŒฑ', ':blossom:' => '๐ŸŒผ', ':ear_of_rice:' => '๐ŸŒพ', ':shell:' => '๐Ÿš', ':globe_with_meridians:' => '๐ŸŒ', ':sun_with_face:' => '๐ŸŒž', ':full_moon_with_face:' => '๐ŸŒ', ':new_moon_with_face:' => '๐ŸŒš', ':new_moon:' => '๐ŸŒ‘', ':waxing_crescent_moon:' => '๐ŸŒ’', ':first_quarter_moon:' => '๐ŸŒ“', ':waxing_gibbous_moon:' => '๐ŸŒ”', ':full_moon:' => '๐ŸŒ•', ':waning_gibbous_moon:' => '๐ŸŒ–', ':last_quarter_moon:' => '๐ŸŒ—', ':waning_crescent_moon:' => '๐ŸŒ˜', ':last_quarter_moon_with_face:' => '๐ŸŒœ', ':first_quarter_moon_with_face:' => '๐ŸŒ›', ':moon:' => '๐ŸŒ”', ':earth_africa:' => '๐ŸŒ', ':earth_americas:' => '๐ŸŒŽ', ':earth_asia:' => '๐ŸŒ', ':volcano:' => '๐ŸŒ‹', ':milky_way:' => '๐ŸŒŒ', ':partly_sunny:' => 'โ›…๏ธ', ':bamboo:' => '๐ŸŽ', ':gift_heart:' => '๐Ÿ’', ':dolls:' => '๐ŸŽŽ', ':school_satchel:' => '๐ŸŽ’', ':mortar_board:' => '๐ŸŽ“', ':flags:' => '๐ŸŽ', ':fireworks:' => '๐ŸŽ†', ':sparkler:' => '๐ŸŽ‡', ':wind_chime:' => '๐ŸŽ', ':rice_scene:' => '๐ŸŽ‘', ':jack_o_lantern:' => '๐ŸŽƒ', ':ghost:' => '๐Ÿ‘ป', ':santa:' => '๐ŸŽ…', ':christmas_tree:' => '๐ŸŽ„', ':gift:' => '๐ŸŽ', ':bell:' => '๐Ÿ””', ':no_bell:' => '๐Ÿ”•', ':tanabata_tree:' => '๐ŸŽ‹', ':tada:' => '๐ŸŽ‰', ':confetti_ball:' => '๐ŸŽŠ', ':balloon:' => '๐ŸŽˆ', ':crystal_ball:' => '๐Ÿ”ฎ', ':cd:' => '๐Ÿ’ฟ', ':dvd:' => '๐Ÿ“€', ':floppy_disk:' => '๐Ÿ’พ', ':camera:' => '๐Ÿ“ท', ':video_camera:' => '๐Ÿ“น', ':movie_camera:' => '๐ŸŽฅ', ':computer:' => '๐Ÿ’ป', ':tv:' => '๐Ÿ“บ', ':iphone:' => '๐Ÿ“ฑ', ':phone:' => 'โ˜Ž๏ธ', ':telephone:' => 'โ˜Ž๏ธ', ':telephone_receiver:' => '๐Ÿ“ž', ':pager:' => '๐Ÿ“Ÿ', ':fax:' => '๐Ÿ“ ', ':minidisc:' => '๐Ÿ’ฝ', ':vhs:' => '๐Ÿ“ผ', ':sound:' => '๐Ÿ”‰', ':speaker:' => '๐Ÿ”ˆ', ':mute:' => '๐Ÿ”‡', ':loudspeaker:' => '๐Ÿ“ข', ':mega:' => '๐Ÿ“ฃ', ':hourglass:' => 'โŒ›๏ธ', ':hourglass_flowing_sand:' => 'โณ', ':alarm_clock:' => 'โฐ', ':watch:' => 'โŒš๏ธ', ':radio:' => '๐Ÿ“ป', ':satellite:' => '๐Ÿ“ก', ':loop:' => 'โžฟ', ':mag:' => '๐Ÿ”', ':mag_right:' => '๐Ÿ”Ž', ':unlock:' => '๐Ÿ”“', ':lock:' => '๐Ÿ”’', ':lock_with_ink_pen:' => '๐Ÿ”', ':closed_lock_with_key:' => '๐Ÿ”', ':key:' => '๐Ÿ”‘', ':bulb:' => '๐Ÿ’ก', ':flashlight:' => '๐Ÿ”ฆ', ':high_brightness:' => '๐Ÿ”†', ':low_brightness:' => '๐Ÿ”…', ':electric_plug:' => '๐Ÿ”Œ', ':battery:' => '๐Ÿ”‹', ':calling:' => '๐Ÿ“ฒ', ':email:' => 'โœ‰๏ธ', ':mailbox:' => '๐Ÿ“ซ', ':postbox:' => '๐Ÿ“ฎ', ':bath:' => '๐Ÿ›€', ':bathtub:' => '๐Ÿ›', ':shower:' => '๐Ÿšฟ', ':toilet:' => '๐Ÿšฝ', ':wrench:' => '๐Ÿ”ง', ':nut_and_bolt:' => '๐Ÿ”ฉ', ':hammer:' => '๐Ÿ”จ', ':seat:' => '๐Ÿ’บ', ':moneybag:' => '๐Ÿ’ฐ', ':yen:' => '๐Ÿ’ด', ':dollar:' => '๐Ÿ’ต', ':pound:' => '๐Ÿ’ท', ':euro:' => '๐Ÿ’ถ', ':credit_card:' => '๐Ÿ’ณ', ':money_with_wings:' => '๐Ÿ’ธ', ':e-mail:' => '๐Ÿ“ง', ':inbox_tray:' => '๐Ÿ“ฅ', ':outbox_tray:' => '๐Ÿ“ค', ':envelope:' => 'โœ‰๏ธ', ':incoming_envelope:' => '๐Ÿ“จ', ':postal_horn:' => '๐Ÿ“ฏ', ':mailbox_closed:' => '๐Ÿ“ช', ':mailbox_with_mail:' => '๐Ÿ“ฌ', ':mailbox_with_no_mail:' => '๐Ÿ“ญ', ':door:' => '๐Ÿšช', ':smoking:' => '๐Ÿšฌ', ':bomb:' => '๐Ÿ’ฃ', ':gun:' => '๐Ÿ”ซ', ':hocho:' => '๐Ÿ”ช', ':pill:' => '๐Ÿ’Š', ':syringe:' => '๐Ÿ’‰', ':page_facing_up:' => '๐Ÿ“„', ':page_with_curl:' => '๐Ÿ“ƒ', ':bookmark_tabs:' => '๐Ÿ“‘', ':bar_chart:' => '๐Ÿ“Š', ':chart_with_upwards_trend:' => '๐Ÿ“ˆ', ':chart_with_downwards_trend:' => '๐Ÿ“‰', ':scroll:' => '๐Ÿ“œ', ':clipboard:' => '๐Ÿ“‹', ':calendar:' => '๐Ÿ“†', ':date:' => '๐Ÿ“…', ':card_index:' => '๐Ÿ“‡', ':file_folder:' => '๐Ÿ“', ':open_file_folder:' => '๐Ÿ“‚', ':scissors:' => 'โœ‚๏ธ', ':pushpin:' => '๐Ÿ“Œ', ':paperclip:' => '๐Ÿ“Ž', ':black_nib:' => 'โœ’๏ธ', ':pencil2:' => 'โœ๏ธ', ':straight_ruler:' => '๐Ÿ“', ':triangular_ruler:' => '๐Ÿ“', ':closed_book:' => '๐Ÿ“•', ':green_book:' => '๐Ÿ“—', ':blue_book:' => '๐Ÿ“˜', ':orange_book:' => '๐Ÿ“™', ':notebook:' => '๐Ÿ““', ':notebook_with_decorative_cover:' => '๐Ÿ“”', ':ledger:' => '๐Ÿ“’', ':books:' => '๐Ÿ“š', ':bookmark:' => '๐Ÿ”–', ':name_badge:' => '๐Ÿ“›', ':microscope:' => '๐Ÿ”ฌ', ':telescope:' => '๐Ÿ”ญ', ':newspaper:' => '๐Ÿ“ฐ', ':football:' => '๐Ÿˆ', ':basketball:' => '๐Ÿ€', ':soccer:' => 'โšฝ๏ธ', ':baseball:' => 'โšพ๏ธ', ':tennis:' => '๐ŸŽพ', ':8ball:' => '๐ŸŽฑ', ':rugby_football:' => '๐Ÿ‰', ':bowling:' => '๐ŸŽณ', ':golf:' => 'โ›ณ๏ธ', ':mountain_bicyclist:' => '๐Ÿšต', ':bicyclist:' => '๐Ÿšด', ':horse_racing:' => '๐Ÿ‡', ':snowboarder:' => '๐Ÿ‚', ':swimmer:' => '๐ŸŠ', ':surfer:' => '๐Ÿ„', ':ski:' => '๐ŸŽฟ', ':spades:' => 'โ™ ๏ธ', ':hearts:' => 'โ™ฅ๏ธ', ':clubs:' => 'โ™ฃ๏ธ', ':diamonds:' => 'โ™ฆ๏ธ', ':gem:' => '๐Ÿ’Ž', ':ring:' => '๐Ÿ’', ':trophy:' => '๐Ÿ†', ':musical_score:' => '๐ŸŽผ', ':musical_keyboard:' => '๐ŸŽน', ':violin:' => '๐ŸŽป', ':space_invader:' => '๐Ÿ‘พ', ':video_game:' => '๐ŸŽฎ', ':black_joker:' => '๐Ÿƒ', ':flower_playing_cards:' => '๐ŸŽด', ':game_die:' => '๐ŸŽฒ', ':dart:' => '๐ŸŽฏ', ':mahjong:' => '๐Ÿ€„๏ธ', ':clapper:' => '๐ŸŽฌ', ':memo:' => '๐Ÿ“', ':pencil:' => '๐Ÿ“', ':book:' => '๐Ÿ“–', ':art:' => '๐ŸŽจ', ':microphone:' => '๐ŸŽค', ':headphones:' => '๐ŸŽง', ':trumpet:' => '๐ŸŽบ', ':saxophone:' => '๐ŸŽท', ':guitar:' => '๐ŸŽธ', ':shoe:' => '๐Ÿ‘ž', ':sandal:' => '๐Ÿ‘ก', ':high_heel:' => '๐Ÿ‘ ', ':lipstick:' => '๐Ÿ’„', ':boot:' => '๐Ÿ‘ข', ':shirt:' => '๐Ÿ‘•', ':tshirt:' => '๐Ÿ‘•', ':necktie:' => '๐Ÿ‘”', ':womans_clothes:' => '๐Ÿ‘š', ':dress:' => '๐Ÿ‘—', ':running_shirt_with_sash:' => '๐ŸŽฝ', ':jeans:' => '๐Ÿ‘–', ':kimono:' => '๐Ÿ‘˜', ':bikini:' => '๐Ÿ‘™', ':ribbon:' => '๐ŸŽ€', ':tophat:' => '๐ŸŽฉ', ':crown:' => '๐Ÿ‘‘', ':womans_hat:' => '๐Ÿ‘’', ':mans_shoe:' => '๐Ÿ‘ž', ':closed_umbrella:' => '๐ŸŒ‚', ':briefcase:' => '๐Ÿ’ผ', ':handbag:' => '๐Ÿ‘œ', ':pouch:' => '๐Ÿ‘', ':purse:' => '๐Ÿ‘›', ':eyeglasses:' => '๐Ÿ‘“', ':fishing_pole_and_fish:' => '๐ŸŽฃ', ':coffee:' => 'โ˜•๏ธ', ':tea:' => '๐Ÿต', ':sake:' => '๐Ÿถ', ':baby_bottle:' => '๐Ÿผ', ':beer:' => '๐Ÿบ', ':beers:' => '๐Ÿป', ':cocktail:' => '๐Ÿธ', ':tropical_drink:' => '๐Ÿน', ':wine_glass:' => '๐Ÿท', ':fork_and_knife:' => '๐Ÿด', ':pizza:' => '๐Ÿ•', ':hamburger:' => '๐Ÿ”', ':fries:' => '๐ŸŸ', ':poultry_leg:' => '๐Ÿ—', ':meat_on_bone:' => '๐Ÿ–', ':spaghetti:' => '๐Ÿ', ':curry:' => '๐Ÿ›', ':fried_shrimp:' => '๐Ÿค', ':bento:' => '๐Ÿฑ', ':sushi:' => '๐Ÿฃ', ':fish_cake:' => '๐Ÿฅ', ':rice_ball:' => '๐Ÿ™', ':rice_cracker:' => '๐Ÿ˜', ':rice:' => '๐Ÿš', ':ramen:' => '๐Ÿœ', ':stew:' => '๐Ÿฒ', ':oden:' => '๐Ÿข', ':dango:' => '๐Ÿก', ':egg:' => '๐Ÿฅš', ':bread:' => '๐Ÿž', ':doughnut:' => '๐Ÿฉ', ':custard:' => '๐Ÿฎ', ':icecream:' => '๐Ÿฆ', ':ice_cream:' => '๐Ÿจ', ':shaved_ice:' => '๐Ÿง', ':birthday:' => '๐ŸŽ‚', ':cake:' => '๐Ÿฐ', ':cookie:' => '๐Ÿช', ':chocolate_bar:' => '๐Ÿซ', ':candy:' => '๐Ÿฌ', ':lollipop:' => '๐Ÿญ', ':honey_pot:' => '๐Ÿฏ', ':apple:' => '๐ŸŽ', ':green_apple:' => '๐Ÿ', ':tangerine:' => '๐ŸŠ', ':lemon:' => '๐Ÿ‹', ':cherries:' => '๐Ÿ’', ':grapes:' => '๐Ÿ‡', ':watermelon:' => '๐Ÿ‰', ':strawberry:' => '๐Ÿ“', ':peach:' => '๐Ÿ‘', ':melon:' => '๐Ÿˆ', ':banana:' => '๐ŸŒ', ':pear:' => '๐Ÿ', ':pineapple:' => '๐Ÿ', ':sweet_potato:' => '๐Ÿ ', ':eggplant:' => '๐Ÿ†', ':tomato:' => '๐Ÿ…', ':corn:' => '๐ŸŒฝ', ':house:' => '๐Ÿ ', ':house_with_garden:' => '๐Ÿก', ':school:' => '๐Ÿซ', ':office:' => '๐Ÿข', ':post_office:' => '๐Ÿฃ', ':hospital:' => '๐Ÿฅ', ':bank:' => '๐Ÿฆ', ':convenience_store:' => '๐Ÿช', ':love_hotel:' => '๐Ÿฉ', ':hotel:' => '๐Ÿจ', ':wedding:' => '๐Ÿ’’', ':church:' => 'โ›ช๏ธ', ':department_store:' => '๐Ÿฌ', ':european_post_office:' => '๐Ÿค', ':city_sunrise:' => '๐ŸŒ‡', ':city_sunset:' => '๐ŸŒ†', ':japanese_castle:' => '๐Ÿฏ', ':european_castle:' => '๐Ÿฐ', ':tent:' => 'โ›บ๏ธ', ':factory:' => '๐Ÿญ', ':tokyo_tower:' => '๐Ÿ—ผ', ':japan:' => '๐Ÿ—พ', ':mount_fuji:' => '๐Ÿ—ป', ':sunrise_over_mountains:' => '๐ŸŒ„', ':sunrise:' => '๐ŸŒ…', ':stars:' => '๐ŸŒ ', ':statue_of_liberty:' => '๐Ÿ—ฝ', ':bridge_at_night:' => '๐ŸŒ‰', ':carousel_horse:' => '๐ŸŽ ', ':rainbow:' => '๐ŸŒˆ', ':ferris_wheel:' => '๐ŸŽก', ':fountain:' => 'โ›ฒ๏ธ', ':roller_coaster:' => '๐ŸŽข', ':ship:' => '๐Ÿšข', ':speedboat:' => '๐Ÿšค', ':boat:' => 'โ›ต๏ธ', ':sailboat:' => 'โ›ต๏ธ', ':rowboat:' => '๐Ÿšฃ', ':anchor:' => 'โš“๏ธ', ':rocket:' => '๐Ÿš€', ':airplane:' => 'โœˆ๏ธ', ':helicopter:' => '๐Ÿš', ':steam_locomotive:' => '๐Ÿš‚', ':tram:' => '๐ŸšŠ', ':mountain_railway:' => '๐Ÿšž', ':bike:' => '๐Ÿšฒ', ':aerial_tramway:' => '๐Ÿšก', ':suspension_railway:' => '๐ŸšŸ', ':mountain_cableway:' => '๐Ÿš ', ':tractor:' => '๐Ÿšœ', ':blue_car:' => '๐Ÿš™', ':oncoming_automobile:' => '๐Ÿš˜', ':car:' => '๐Ÿš—', ':red_car:' => '๐Ÿš—', ':taxi:' => '๐Ÿš•', ':oncoming_taxi:' => '๐Ÿš–', ':articulated_lorry:' => '๐Ÿš›', ':bus:' => '๐ŸšŒ', ':oncoming_bus:' => '๐Ÿš', ':rotating_light:' => '๐Ÿšจ', ':police_car:' => '๐Ÿš“', ':oncoming_police_car:' => '๐Ÿš”', ':fire_engine:' => '๐Ÿš’', ':ambulance:' => '๐Ÿš‘', ':minibus:' => '๐Ÿš', ':truck:' => '๐Ÿšš', ':train:' => '๐Ÿš‹', ':station:' => '๐Ÿš‰', ':train2:' => '๐Ÿš†', ':bullettrain_front:' => '๐Ÿš…', ':bullettrain_side:' => '๐Ÿš„', ':light_rail:' => '๐Ÿšˆ', ':monorail:' => '๐Ÿš', ':railway_car:' => '๐Ÿšƒ', ':trolleybus:' => '๐ŸšŽ', ':ticket:' => '๐ŸŽซ', ':fuelpump:' => 'โ›ฝ๏ธ', ':vertical_traffic_light:' => '๐Ÿšฆ', ':traffic_light:' => '๐Ÿšฅ', ':warning:' => 'โš ๏ธ', ':construction:' => '๐Ÿšง', ':beginner:' => '๐Ÿ”ฐ', ':atm:' => '๐Ÿง', ':slot_machine:' => '๐ŸŽฐ', ':busstop:' => '๐Ÿš', ':barber:' => '๐Ÿ’ˆ', ':hotsprings:' => 'โ™จ๏ธ', ':checkered_flag:' => '๐Ÿ', ':crossed_flags:' => '๐ŸŽŒ', ':izakaya_lantern:' => '๐Ÿฎ', ':moyai:' => '๐Ÿ—ฟ', ':circus_tent:' => '๐ŸŽช', ':performing_arts:' => '๐ŸŽญ', ':round_pushpin:' => '๐Ÿ“', ':triangular_flag_on_post:' => '๐Ÿšฉ', ':jp:' => '๐Ÿ‡ฏ๐Ÿ‡ต', ':kr:' => '๐Ÿ‡ฐ๐Ÿ‡ท', ':cn:' => '๐Ÿ‡จ๐Ÿ‡ณ', ':us:' => '๐Ÿ‡บ๐Ÿ‡ธ', ':fr:' => '๐Ÿ‡ซ๐Ÿ‡ท', ':es:' => '๐Ÿ‡ช๐Ÿ‡ธ', ':it:' => '๐Ÿ‡ฎ๐Ÿ‡น', ':ru:' => '๐Ÿ‡ท๐Ÿ‡บ', ':gb:' => '๐Ÿ‡ฌ๐Ÿ‡ง', ':uk:' => '๐Ÿ‡ฌ๐Ÿ‡ง', ':de:' => '๐Ÿ‡ฉ๐Ÿ‡ช', ':one:' => '1๏ธโƒฃ', ':two:' => '2๏ธโƒฃ', ':three:' => '3๏ธโƒฃ', ':four:' => '4๏ธโƒฃ', ':five:' => '5๏ธโƒฃ', ':six:' => '6๏ธโƒฃ', ':seven:' => '7๏ธโƒฃ', ':eight:' => '8๏ธโƒฃ', ':nine:' => '9๏ธโƒฃ', ':keycap_ten:' => '๐Ÿ”Ÿ', ':1234:' => '๐Ÿ”ข', ':zero:' => '0๏ธโƒฃ', ':hash:' => '#๏ธโƒฃ', ':symbols:' => '๐Ÿ”ฃ', ':arrow_backward:' => 'โ—€๏ธ', ':arrow_down:' => 'โฌ‡๏ธ', ':arrow_forward:' => 'โ–ถ๏ธ', ':arrow_left:' => 'โฌ…๏ธ', ':capital_abcd:' => '๐Ÿ” ', ':abcd:' => '๐Ÿ”ก', ':abc:' => '๐Ÿ”ค', ':arrow_lower_left:' => 'โ†™๏ธ', ':arrow_lower_right:' => 'โ†˜๏ธ', ':arrow_right:' => 'โžก๏ธ', ':arrow_up:' => 'โฌ†๏ธ', ':arrow_upper_left:' => 'โ†–๏ธ', ':arrow_upper_right:' => 'โ†—๏ธ', ':arrow_double_down:' => 'โฌ', ':arrow_double_up:' => 'โซ', ':arrow_down_small:' => '๐Ÿ”ฝ', ':arrow_heading_down:' => 'โคต๏ธ', ':arrow_heading_up:' => 'โคด๏ธ', ':leftwards_arrow_with_hook:' => 'โ†ฉ๏ธ', ':arrow_right_hook:' => 'โ†ช๏ธ', ':left_right_arrow:' => 'โ†”๏ธ', ':arrow_up_down:' => 'โ†•๏ธ', ':arrow_up_small:' => '๐Ÿ”ผ', ':arrows_clockwise:' => '๐Ÿ”ƒ', ':arrows_counterclockwise:' => '๐Ÿ”„', ':rewind:' => 'โช', ':fast_forward:' => 'โฉ', ':information_source:' => 'โ„น๏ธ', ':ok:' => '๐Ÿ†—', ':twisted_rightwards_arrows:' => '๐Ÿ”€', ':repeat:' => '๐Ÿ”', ':repeat_one:' => '๐Ÿ”‚', ':new:' => '๐Ÿ†•', ':top:' => '๐Ÿ”', ':up:' => '๐Ÿ†™', ':cool:' => '๐Ÿ†’', ':free:' => '๐Ÿ†“', ':ng:' => '๐Ÿ†–', ':cinema:' => '๐ŸŽฆ', ':koko:' => '๐Ÿˆ', ':signal_strength:' => '๐Ÿ“ถ', ':u5272:' => '๐Ÿˆน', ':u5408:' => '๐Ÿˆด', ':u55b6:' => '๐Ÿˆบ', ':u6307:' => '๐Ÿˆฏ๏ธ', ':u6708:' => '๐Ÿˆท๏ธ', ':u6709:' => '๐Ÿˆถ', ':u6e80:' => '๐Ÿˆต', ':u7121:' => '๐Ÿˆš๏ธ', ':u7533:' => '๐Ÿˆธ', ':u7a7a:' => '๐Ÿˆณ', ':u7981:' => '๐Ÿˆฒ', ':sa:' => '๐Ÿˆ‚๏ธ', ':restroom:' => '๐Ÿšป', ':mens:' => '๐Ÿšน', ':womens:' => '๐Ÿšบ', ':baby_symbol:' => '๐Ÿšผ', ':no_smoking:' => '๐Ÿšญ', ':parking:' => '๐Ÿ…ฟ๏ธ', ':wheelchair:' => 'โ™ฟ๏ธ', ':metro:' => '๐Ÿš‡', ':baggage_claim:' => '๐Ÿ›„', ':accept:' => '๐Ÿ‰‘', ':wc:' => '๐Ÿšพ', ':potable_water:' => '๐Ÿšฐ', ':put_litter_in_its_place:' => '๐Ÿšฎ', ':secret:' => 'ใŠ™๏ธ', ':congratulations:' => 'ใŠ—๏ธ', ':m:' => 'โ“‚๏ธ', ':passport_control:' => '๐Ÿ›‚', ':left_luggage:' => '๐Ÿ›…', ':customs:' => '๐Ÿ›ƒ', ':ideograph_advantage:' => '๐Ÿ‰', ':cl:' => '๐Ÿ†‘', ':sos:' => '๐Ÿ†˜', ':id:' => '๐Ÿ†”', ':no_entry_sign:' => '๐Ÿšซ', ':underage:' => '๐Ÿ”ž', ':no_mobile_phones:' => '๐Ÿ“ต', ':do_not_litter:' => '๐Ÿšฏ', ':non-potable_water:' => '๐Ÿšฑ', ':no_bicycles:' => '๐Ÿšณ', ':no_pedestrians:' => '๐Ÿšท', ':children_crossing:' => '๐Ÿšธ', ':no_entry:' => 'โ›”๏ธ', ':eight_spoked_asterisk:' => 'โœณ๏ธ', ':eight_pointed_black_star:' => 'โœด๏ธ', ':heart_decoration:' => '๐Ÿ’Ÿ', ':vs:' => '๐Ÿ†š', ':vibration_mode:' => '๐Ÿ“ณ', ':mobile_phone_off:' => '๐Ÿ“ด', ':chart:' => '๐Ÿ’น', ':currency_exchange:' => '๐Ÿ’ฑ', ':aries:' => 'โ™ˆ๏ธ', ':taurus:' => 'โ™‰๏ธ', ':gemini:' => 'โ™Š๏ธ', ':cancer:' => 'โ™‹๏ธ', ':leo:' => 'โ™Œ๏ธ', ':virgo:' => 'โ™๏ธ', ':libra:' => 'โ™Ž๏ธ', ':scorpius:' => 'โ™๏ธ', ':sagittarius:' => 'โ™๏ธ', ':capricorn:' => 'โ™‘๏ธ', ':aquarius:' => 'โ™’๏ธ', ':pisces:' => 'โ™“๏ธ', ':ophiuchus:' => 'โ›Ž', ':six_pointed_star:' => '๐Ÿ”ฏ', ':negative_squared_cross_mark:' => 'โŽ', ':a:' => '๐Ÿ…ฐ๏ธ', ':b:' => '๐Ÿ…ฑ๏ธ', ':ab:' => '๐Ÿ†Ž', ':o2:' => '๐Ÿ…พ๏ธ', ':diamond_shape_with_a_dot_inside:' => '๐Ÿ’ ', ':recycle:' => 'โ™ป๏ธ', ':end:' => '๐Ÿ”š', ':on:' => '๐Ÿ”›', ':soon:' => '๐Ÿ”œ', ':clock1:' => '๐Ÿ•', ':clock130:' => '๐Ÿ•œ', ':clock10:' => '๐Ÿ•™', ':clock1030:' => '๐Ÿ•ฅ', ':clock11:' => '๐Ÿ•š', ':clock1130:' => '๐Ÿ•ฆ', ':clock12:' => '๐Ÿ•›', ':clock1230:' => '๐Ÿ•ง', ':clock2:' => '๐Ÿ•‘', ':clock230:' => '๐Ÿ•', ':clock3:' => '๐Ÿ•’', ':clock330:' => '๐Ÿ•ž', ':clock4:' => '๐Ÿ•“', ':clock430:' => '๐Ÿ•Ÿ', ':clock5:' => '๐Ÿ•”', ':clock530:' => '๐Ÿ• ', ':clock6:' => '๐Ÿ••', ':clock630:' => '๐Ÿ•ก', ':clock7:' => '๐Ÿ•–', ':clock730:' => '๐Ÿ•ข', ':clock8:' => '๐Ÿ•—', ':clock830:' => '๐Ÿ•ฃ', ':clock9:' => '๐Ÿ•˜', ':clock930:' => '๐Ÿ•ค', ':heavy_dollar_sign:' => '๐Ÿ’ฒ', ':copyright:' => 'ยฉ๏ธ', ':registered:' => 'ยฎ๏ธ', ':tm:' => 'โ„ข๏ธ', ':x:' => 'โŒ', ':heavy_exclamation_mark:' => 'โ—๏ธ', ':bangbang:' => 'โ€ผ๏ธ', ':interrobang:' => 'โ‰๏ธ', ':o:' => 'โญ•๏ธ', ':heavy_multiplication_x:' => 'โœ–๏ธ', ':heavy_plus_sign:' => 'โž•', ':heavy_minus_sign:' => 'โž–', ':heavy_division_sign:' => 'โž—', ':white_flower:' => '๐Ÿ’ฎ', ':100:' => '๐Ÿ’ฏ', ':heavy_check_mark:' => 'โœ”๏ธ', ':ballot_box_with_check:' => 'โ˜‘๏ธ', ':radio_button:' => '๐Ÿ”˜', ':link:' => '๐Ÿ”—', ':curly_loop:' => 'โžฐ', ':wavy_dash:' => 'ใ€ฐ๏ธ', ':part_alternation_mark:' => 'ใ€ฝ๏ธ', ':trident:' => '๐Ÿ”ฑ', ':white_check_mark:' => 'โœ…', ':black_square_button:' => '๐Ÿ”ฒ', ':white_square_button:' => '๐Ÿ”ณ', ':black_circle:' => 'โšซ๏ธ', ':white_circle:' => 'โšช๏ธ', ':red_circle:' => '๐Ÿ”ด', ':large_blue_circle:' => '๐Ÿ”ต', ':large_blue_diamond:' => '๐Ÿ”ท', ':large_orange_diamond:' => '๐Ÿ”ถ', ':small_blue_diamond:' => '๐Ÿ”น', ':small_orange_diamond:' => '๐Ÿ”ธ', ':small_red_triangle:' => '๐Ÿ”บ', ':small_red_triangle_down:' => '๐Ÿ”ป', ':black_small_square:' => 'โ–ช๏ธ', ':black_medium_small_square:' => 'โ—พ', ':black_medium_square:' => 'โ—ผ๏ธ', ':black_large_square:' => 'โฌ›', ':white_small_square:' => 'โ–ซ๏ธ', ':white_medium_small_square:' => 'โ—ฝ', ':white_medium_square:' => 'โ—ป๏ธ', ':white_large_square:' => 'โฌœ', ]; return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \str_replace(\array_keys($emojiMap), $emojiMap, $matches[0]), ], ]; } /** * Handle marks * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineMark(array $excerpt) : ?array { if (!\str_starts_with($excerpt['text'], '==') || \preg_match('/^(==)([^=]*?)(==)/', $excerpt['text'], $matches) !== 1 ) { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'mark', 'text' => $matches[2], ], ]; } /** * Handle marks * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineSpoiler(array $excerpt) : ?array { if (!\str_starts_with($excerpt['text'], '>!') || \preg_match('/^>!(.*?)! \strlen($matches[0]), 'element' => [ 'name' => 'span', 'attributes' => [ 'class' => 'spoiler', ], 'elements' => [ [ 'name' => 'input', 'attributes' => [ 'type' => 'checkbox', ], ], [ 'name' => 'span', 'text' => $matches[1], ], ], ], ]; } /** * Handle keystrokes * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineKeystrokes(array $excerpt) : ?array { if (!\str_starts_with($excerpt['text'], '[[') || \preg_match('/^(? \strlen($matches[0]), 'element' => [ 'name' => 'kbd', 'text' => $matches[1], ], ]; } /** * Handle embedding * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineEmbedding(array $excerpt) : ?array { $video = false; $audio = false; if (!($this->options['embedding'] ?? false) || (!\str_starts_with($excerpt['text'], '[video') && !\str_starts_with($excerpt['text'], '[audio')) || (!($video = (\preg_match('/\[video.*src="([^"]*)".*\]/', $excerpt['text'], $matches) === 1)) && !($audio = (\preg_match('/\[audio.*src="([^"]*)".*\]/', $excerpt['text'], $matches) === 1))) ) { return null; } $url = $matches[1]; if ($video) { $type = ''; $needles = ['youtube', 'vimeo', 'dailymotion']; foreach ($needles as $needle) { if (\strpos($url, $needle) !== false) { $type = $needle; } } switch ($type) { case 'youtube': $element = 'iframe'; $attributes = [ 'src' => \preg_replace('/.*\?v=([^\&\]]*).*/', 'https://www.youtube.com/embed/$1', $url), 'frameborder' => '0', 'allow' => 'autoplay', 'allowfullscreen' => '', 'sandbox' => 'allow-same-origin allow-scripts allow-forms', ]; break; case 'vimeo': $element = 'iframe'; $attributes = [ 'src' => \preg_replace('/(?:https?:\/\/(?:[\w]{3}\.|player\.)*vimeo\.com(?:[\/\w:]*(?:\/videos)?)?\/([0-9]+)[^\s]*)/', 'https://player.vimeo.com/video/$1', $url), 'frameborder' => '0', 'allow' => 'autoplay', 'allowfullscreen' => '', 'sandbox' => 'allow-same-origin allow-scripts allow-forms', ]; break; case 'dailymotion': $element = 'iframe'; $attributes = [ 'src' => $url, 'frameborder' => '0', 'allow' => 'autoplay', 'allowfullscreen' => '', 'sandbox' => 'allow-same-origin allow-scripts allow-forms', ]; break; default: $element = 'video'; $attributes = [ 'src' => UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $url)), 'controls' => '', ]; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => $element, 'text' => $matches[1], 'attributes' => $attributes, ], ]; } elseif ($audio) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'audio', 'text' => $matches[1], 'attributes' => [ 'src' => UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $url)), 'controls' => '', ], ], ]; } return null; } /** * Handle map * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineMap(array $excerpt) : ?array { if (!($this->options['map'] ?? false) || !\str_starts_with($excerpt['text'], '[map') || (\preg_match('/\[map(?:\s+(?:name="([^"]+)"|country="([^"]+)"|city="([^"]+)"|zip="([^"]+)"|address="([^"]+)"|lat="([^"]+)"|lon="([^"]+)")){0,7}\]/', $excerpt['text'], $matches) !== 1) ) { return null; } $name = $matches[1]; $country = $matches[2]; $city = $matches[3]; $zip = $matches[4]; $address = $matches[5]; $lat = $matches[6]; $lon = $matches[7]; if ($lat === '' || $lon === '') { [$lat, $lon] = \phpOMS\Api\Geocoding\Nominatim::geocoding($country, $city, $address, $zip); } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'div', 'text' => '', 'attributes' => [ 'id' => 'i' . \bin2hex(\random_bytes(4)), 'class' => 'map', 'data-lat' => $lat, 'data-lon' => $lon, ], ], ]; } /** * Handle address * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineAddress(array $excerpt) : ?array { if (!($this->options['address'] ?? false) || !\str_starts_with($excerpt['text'], '[addr') || (\preg_match('/\[addr(?:\s+(?:name="([^"]+)"|country="([^"]+)"|city="([^"]+)"|zip="([^"]+)"|address="([^"]+)")){0,5}\]/', $excerpt['text'], $matches) !== 1) ) { return null; } $name = $matches[1]; $country = $matches[2]; $city = $matches[3]; $zip = $matches[4]; $address = $matches[5]; return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'div', //'text' => '', 'attributes' => [ 'class' => 'addressWidget', ], 'elements' => [ [ 'name' => 'span', 'text' => $name, 'attributes' => ['class' => 'addressWidget-name'], ], [ 'name' => 'span', 'text' => $address, 'attributes' => ['class' => 'addressWidget-address'], ], [ 'name' => 'span', 'text' => $zip, 'attributes' => ['class' => 'addressWidget-zip'], ], [ 'name' => 'span', 'text' => $city, 'attributes' => ['class' => 'addressWidget-city'], ], [ 'name' => 'span', 'text' => $country, 'attributes' => ['class' => 'addressWidget-country'], ], ], ], ]; } /** * Handle contact * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineContact(array $excerpt) : ?array { if (!($this->options['contact'] ?? false) || !\str_starts_with($excerpt['text'], '[contact') || (\preg_match('/\[contact.*?([a-zA-Z]+)="(.*?)"\]/', $excerpt['text'], $matches) !== 1) ) { return null; } $src = ''; switch ($matches[1]) { case 'email': $src = 'Resources/icons/company/email.svg'; break; case 'phone': $src = 'Resources/icons/company/phone.svg'; break; case 'twitter': $src = 'Resources/icons/company/twitter.svg'; break; case 'instagram': $src = 'Resources/icons/company/instagram.svg'; break; case 'discord': $src = 'Resources/icons/company/discord.svg'; break; case 'slack': $src = 'Resources/icons/company/slack.svg'; break; case 'teams': $src = 'Resources/icons/company/teams.svg'; break; case 'facebook': $src = 'Resources/icons/company/facebook.svg'; break; case 'youtube': $src = 'Resources/icons/company/youtube.svg'; break; case 'paypal': $src = 'Resources/icons/company/paypal.svg'; break; case 'linkedin': $src = 'Resources/icons/company/linkedin.svg'; break; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'a', //'text' => '', 'attributes' => [ 'class' => 'contactWidget', 'href' => '', ], 'elements' => [ [ 'name' => 'img', 'attributes' => [ 'class' => 'contactWidget-icon', 'src' => $src, ], ], [ 'name' => 'span', 'text' => $matches[2], 'attributes' => ['class' => 'contactWidget-contact'], ], ], ], ]; } /** * Handle progress * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineProgress(array $excerpt) : ?array { if (!($this->options['progress'] ?? false) || !\str_starts_with($excerpt['text'], '[progress') || (\preg_match('/\[progress(?:\s+(?:type="([^"]+)"|percent="([^"]+)"|value="([^"]+)")){0,3}\]/', $excerpt['text'], $matches) !== 1) ) { return null; } // $type = empty($matches[1]) ? 'meter' : $matches[1]; $percent = empty($matches[2]) ? $matches[3] : $matches[2]; $value = empty($matches[3]) ? $matches[2] : $matches[3]; if ($percent === '' || $value === '' ) { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'progress', 'text' => '', 'attributes' => [ 'value' => $value, 'max' => '100', ], ], ]; } /** * Handle super script * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineSuperscript(array $excerpt) : ?array { if (\preg_match('/(?:\^(?!\^)([^\^ ]*)\^(?!\^))/', $excerpt['text'], $matches) !== 1) { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'sup', 'text' => $matches[1], 'function' => 'lineElements', ], ]; } /** * Handle sub script * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineSubscript(array $excerpt) : ?array { if (\preg_match('/(?:~(?!~)([^~ ]*)~(?!~))/', $excerpt['text'], $matches) !== 1) { return null; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'sub', 'text' => $matches[1], 'function' => 'lineElements', ], ]; } /** * Handle typographer * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineTypographer(array $excerpt) : ?array { if (\preg_match('/\+-|\(p\)|\(tm\)|\(r\)|\(c\)|\.{2,}|\!\.{3,}|\?\.{3,}/i', $excerpt['text'], $matches) !== 1) { return null; } $substitutions = [ '/\(c\)/i' => '©', '/\(r\)/i' => '®', '/\(tm\)/i' => '™', '/\(p\)/i' => '¶', '/\+-/i' => '±', '/\.{4,}|\.{2}/i' => '...', '/\!\.{3,}/i' => '!..', '/\?\.{3,}/i' => '?..', ]; return [ 'extent' => \strlen($matches[0]), 'element' => [ 'rawHtml' => \preg_replace(\array_keys($substitutions), \array_values($substitutions), $matches[0]), ], ]; } /** * Handle math * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineMath(array $excerpt) : ?array { $matchSingleDollar = $this->options['math']['single_dollar'] ?? false; if ($matchSingleDollar) { // Match single dollar - experimental if (\preg_match('/^(? \strlen($mathMatch), 'element' => [ 'text' => $mathMatch, ], ]; } /** * Handle escape sequence * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineEscapeSequence(array $excerpt) : ?array { if (!isset($excerpt['text'][1]) || !\in_array($excerpt['text'][1], $this->specialCharacters) ) { return null; } $state = $this->options['math'] ?? false; if (!$state || !\preg_match('/^(? 2, 'element' => [ 'rawHtml' => $excerpt['text'][1], ], ]; } return null; } /** * Handle block footnote * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockFootnote(array $line, ?array $_ = null) : ?array { return ($this->options['footnotes'] ?? true) && \preg_match('/^\[\^(.+?)\]:[ ]?(.*)$/', $line['text'], $matches) == 1 ? [ 'label' => $matches[1], 'text' => $matches[2], 'hidden' => true, ] : null; } /** * Handle block definition list * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockDefinitionList(array $line, ?array $block = null) : ?array { if (!($this->options['definition_lists'] ?? true) || $block === null || $block['type'] !== 'Paragraph' ) { return null; } $element = [ 'name' => 'dl', 'elements' => [], ]; $terms = \explode("\n", $block['element']['handler']['argument']); foreach ($terms as $term) { $element['elements'][] = [ 'name' => 'dt', 'handler' => [ 'function' => 'lineElements', 'argument' => $term, 'destination' => 'elements', ], ]; } $block['element'] = $element; return $this->addDdElement($line, $block); } /** * Handle block code * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockCode(array $line, ?array $block = null) : ?array { if (!($this->options['code']['blocks'] ?? true) || !($this->options['code'] ?? true) || ($block !== null && $block['type'] === 'Paragraph' && !isset($block['interrupted'])) || $line['indent'] < 4 ) { return null; } return [ 'element' => [ 'name' => 'pre', 'element' => [ 'name' => 'code', 'text' => \substr($line['body'], 4), ], ], ]; } /** * Handle block comment * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockComment(array $line, ?array $_ = null) : ?array { if (!($this->options['comments'] ?? true) || $this->markupEscaped || $this->safeMode || !\str_starts_with($line['text'], '') !== false) { $block['closed'] = true; } return $block; } /** * Handle block comment * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockHeader(array $line, ?array $_ = null) : ?array { if (!($this->options['headings'] ?? true)) { return null; } $level = \strspn($line['text'], '#'); if ($level > 6) { return null; } $text = \trim($line['text'], '#'); if ($this->strictMode && isset($text[0]) && $text[0] !== ' ') { return null; } $text = \trim($text, ' '); $block = [ 'element' => [ 'name' => 'h' . $level, 'handler' => [ 'function' => 'lineElements', 'argument' => $text, 'destination' => 'elements', ], ], ]; if (\preg_match('/[ #]*{(' . $this->regexAttribute . '+)}[ ]*$/', $block['element']['handler']['argument'], $matches, \PREG_OFFSET_CAPTURE)) { $attributeString = $matches[1][0]; $block['element']['attributes'] = $this->parseAttributeData($attributeString); $block['element']['handler']['argument'] = \substr($block['element']['handler']['argument'], 0, (int) $matches[0][1]); } // Get the text of the heading if (isset($block['element']['handler']['argument'])) { $text = $block['element']['handler']['argument']; } // Get the heading level. Levels are h1, h2, ..., h6 $level = $block['element']['name']; $headersAllowed = $this->options['headings']['allowed'] ?? ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; if (!\in_array($level, $headersAllowed)) { return null; } // Checks if auto generated anchors is allowed $autoAnchors = $this->options['headings']['auto_anchors'] ?? true; $id = $block['element']['attributes']['id'] ?? ($autoAnchors ? $this->createAnchorID($text) : null); // Set attributes to head tags $block['element']['attributes']['id'] = $id; $tocHeaders = $this->options['toc']['headings'] ?? ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; // Check if level are defined as a heading if (\in_array($level, $tocHeaders)) { // Add/stores the heading element info to the ToC list $this->setContentsList([ 'text' => $text, 'id' => $id, 'level' => $level, ]); } return $block; } /** * Handle block list * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $current Current block * * @return null|array * * @since 1.0.0 */ protected function blockList(array $line, ?array $current = null) : ?array { if (!($this->options['lists'] ?? true)) { return null; } [$name, $pattern] = $line['text'][0] <= '-' ? ['ul', '[*+-]'] : ['ol', '[0-9]{1,9}+[.\)]']; if (\preg_match('/^(' . $pattern . '([ ]++|$))(.*+)/', $line['text'], $matches) !== 1) { return null; } $contentIndent = \strlen($matches[2]); if ($contentIndent >= 5) { --$contentIndent; $matches[1] = \substr($matches[1], 0, -$contentIndent); $matches[3] = \str_repeat(' ', $contentIndent) . $matches[3]; } elseif ($contentIndent === 0) { $matches[1] .= ' '; } $markerWithoutWhitespace = \strstr($matches[1], ' ', true); if ($markerWithoutWhitespace === false) { $markerWithoutWhitespace = $matches[1]; } if ($name !== 'ul') { $markerWithoutWhitespace = \substr($markerWithoutWhitespace, -1); if ($markerWithoutWhitespace === false) { $markerWithoutWhitespace = $matches[1]; } } $block = [ 'indent' => $line['indent'], 'pattern' => $pattern, 'data' => [ 'type' => $name, 'marker' => $matches[1], 'markerType' => $markerWithoutWhitespace, ], 'element' => [ 'name' => $name, 'elements' => [], ], ]; $block['data']['markerTypeRegex'] = \preg_quote($block['data']['markerType'], '/'); if ($name === 'ol') { $tmp = \strstr($matches[1], $block['data']['markerType'], true); if ($tmp === false) { $tmp = $matches[1]; } $listStart = \ltrim($tmp, '0') ?: '0'; if ($listStart !== '0') { if (isset($current) && $current['type'] === 'Paragraph' && !isset($current['interrupted']) ) { return null; } $block['element']['attributes'] = ['start' => $listStart]; } } $block['li'] = [ 'name' => 'li', 'handler' => [ 'function' => 'li', 'argument' => empty($matches[3]) ? [] : [$matches[3]], 'destination' => 'elements', ], ]; $block['element']['elements'][] = &$block['li']; return $block; } /** * Handle block quote * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockQuote(array $line, ?array $_ = null) : ?array { if (!($this->options['qoutes'] ?? true) || \preg_match('/^>[ ]?+(.*+)/', $line['text'], $matches) !== 1 ) { return null; } return [ 'element' => [ 'name' => 'blockquote', 'handler' => [ 'function' => 'linesElements', 'argument' => (array) $matches[1], 'destination' => 'elements', ], ], ]; } /** * Handle block rule * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockRule(array $line, ?array $_ = null) : ?array { if (!($this->options['thematic_breaks'] ?? true)) { return null; } $marker = $line['text'][0]; if (\substr_count($line['text'], $marker) >= 3 && \rtrim($line['text'], " {$marker}") === '') { return [ 'element' => [ 'name' => 'hr', ], ]; } return null; } /** * Handle block header * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockSetextHeader(array $line, ?array $block = null) : ?array { if (!($this->options['headings'] ?? true) || $block === null || $block['type'] !== 'Paragraph' || isset($block['interrupted']) ) { return null; } if ($line['indent'] < 4 && \rtrim(\rtrim($line['text'], ' '), $line['text'][0]) === '') { $block['element']['name'] = $line['text'][0] === '=' ? 'h1' : 'h2'; } if (\preg_match('/[ ]*{(' . $this->regexAttribute . '+)}[ ]*$/', $block['element']['handler']['argument'], $matches, \PREG_OFFSET_CAPTURE)) { $attributeString = $matches[1][0]; $block['element']['attributes'] = $this->parseAttributeData($attributeString); $block['element']['handler']['argument'] = \substr($block['element']['handler']['argument'], 0, (int) $matches[0][1]); } // Get the text of the heading $text = null; if (isset($block['element']['handler']['argument'])) { $text = $block['element']['handler']['argument']; } // Get the heading level. Levels are h1, h2, ..., h6 $level = $block['element']['name']; $headersAllowed = $this->options['headings']['allowed'] ?? ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; if (!\in_array($level, $headersAllowed)) { return null; } // Checks if auto generated anchors is allowed $autoAnchors = $this->options['headings']['auto_anchors'] ?? true; $id = $block['element']['attributes']['id'] ?? ($autoAnchors ? $this->createAnchorID($text) : null); // Set attributes to head tags $block['element']['attributes']['id'] = $id; $headersAllowed = $this->options['headings']['allowed'] ?? ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']; // Check if level are defined as a heading if (\in_array($level, $headersAllowed)) { // Add/stores the heading element info to the ToC list $this->setContentsList([ 'text' => $text, 'id' => $id, 'level' => $level, ]); } return $block; } /** * Handle block markup * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockMarkup(array $line, ?array $_ = null) : ?array { if (!($this->options['markup'] ?? true) || $this->markupEscaped || $this->safeMode || \preg_match('/^<(\w[\w-]*)(?:[ ]*' . $this->regexHtmlAttribute . ')*[ ]*(\/)?>/', $line['text'], $matches) !== 1 ) { return null; } $element = \strtolower($matches[1]); if (\in_array($element, $this->textLevelElements)) { return null; } $block = [ 'name' => $matches[1], 'depth' => 0, 'element' => [ 'rawHtml' => $line['text'], 'autobreak' => true, ], ]; $length = \strlen($matches[0]); $remainder = \substr($line['text'], $length); if (\trim($remainder) === '') { if (isset($matches[2]) || \in_array($matches[1], $this->voidElements)) { $block['closed'] = true; $block['void'] = true; } } else { if (isset($matches[2]) || \in_array($matches[1], $this->voidElements)) { return null; } if (\preg_match('/<\/' . $matches[1] . '>[ ]*$/i', $remainder)) { $block['closed'] = true; } } return $block; } /** * Handle block reference * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockReference(array $line, ?array $_ = null) : ?array { if (!($this->options['references'] ?? true) || \strpos($line['text'], ']') === false || \preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $line['text'], $matches) !== 1 ) { return null; } $id = \strtolower($matches[1]); $this->definitionData['Reference'][$id] = [ 'url' => UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $matches[2])), 'title' => isset($matches[3]) ? $matches[3] : null, ]; return [ 'element' => [], ]; } /** * Handle block table * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockTable(array $line, ?array $block = null) : ?array { if (!($this->options['tables'] ?? true) || $block === null || $block['type'] !== 'Paragraph' || isset($block['interrupted']) || (\strpos($block['element']['handler']['argument'], '|') === false && \strpos($line['text'], '|') === false && \strpos($line['text'], ':') === false || \strpos($block['element']['handler']['argument'], "\n") !== false) || \rtrim($line['text'], ' -:|') !== '' ) { return null; } $alignments = []; $divider = $line['text']; $divider = \trim($divider); $divider = \trim($divider, '|'); $dividerCells = \explode('|', $divider); foreach ($dividerCells as $dividerCell) { $dividerCell = \trim($dividerCell); if ($dividerCell === '') { return null; } $alignment = null; if ($dividerCell[0] === ':') { $alignment = 'left'; } if (\substr($dividerCell, - 1) === ':') { $alignment = $alignment === 'left' ? 'center' : 'right'; } $alignments [] = $alignment; } $headerElements = []; $header = $block['element']['handler']['argument']; $header = \trim($header); $header = \trim($header, '|'); $headerCells = \explode('|', $header); if (\count($headerCells) !== \count($alignments)) { return null; } foreach ($headerCells as $index => $headerCell) { $headerCell = \trim($headerCell); $headerElement = [ 'name' => 'th', 'handler' => [ 'function' => 'lineElements', 'argument' => $headerCell, 'destination' => 'elements', ], ]; if (isset($alignments[$index])) { $alignment = $alignments[$index]; $headerElement['attributes'] = [ 'style' => "text-align: {$alignment};", ]; } $headerElements[] = $headerElement; } $block = [ 'alignments' => $alignments, 'identified' => true, 'element' => [ 'name' => 'table', 'elements' => [], ], ]; $block['element']['elements'][] = [ 'name' => 'thead', ]; $block['element']['elements'][] = [ 'name' => 'tbody', 'elements' => [], ]; $block['element']['elements'][0]['elements'][] = [ 'name' => 'tr', 'elements' => $headerElements, ]; return $block; } /** * Handle block abbreviation * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockAbbreviation(array $line, ?array $_ = null) : ?array { if (!($this->options['abbreviations'] ?? true)) { return null; } $allowCustomAbbr = $this->options['abbreviations']['allow_custom_abbr'] ?? true; if (isset($this->options['abbreviations']['predefine'])) { foreach ($this->options['abbreviations']['predefine'] as $abbreviations => $description) { $this->definitionData['Abbreviation'][$abbreviations] = $description; } } if (!$allowCustomAbbr || \preg_match('/^\*\[(.+?)\]:[ ]*(.+?)[ ]*$/', $line['text'], $matches) !== 1 ) { return null; } $this->definitionData['Abbreviation'][$matches[1]] = $matches[2]; return [ 'hidden' => true, ]; } /** * Handle block math * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockMath(array $line, ?array $_ = null) : ?array { $block = [ 'element' => [ 'text' => '', ], ]; if (\preg_match('/^(?options['code']['blocks'] ?? true) || !($this->options['code'] ?? true) ) { return null; } $marker = $line['text'][0]; $openerLength = \strspn($line['text'], $marker); if ($openerLength < 3) { return null; } $language = \trim(\preg_replace('/^`{3}([^\s]+)(.+)?/s', '$1', $line['text']) ?? ''); if (!($this->options['diagrams'] ?? true) || !\in_array($language, ['mermaid', 'chartjs', 'tuichart']) ) { // Is code block $element = [ 'name' => 'code', 'text' => '', ]; if ($language !== '```' && !empty($language)) { $element['attributes'] = ['class' => "language-{$language}"]; } return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'name' => 'pre', 'element' => $element, ], ]; } elseif (\strtolower($language) === 'chartjs') { // Chart.js https://www.chartjs.org/ $element = [ 'text' => '', ]; return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'element' => $element, 'name' => 'canvas', 'attributes' => [ 'class' => 'chartjs', ], ], ]; } elseif (\in_array(\strtolower($language), ['mermaid', 'tuichart'])) { // Mermaid.js https://mermaidjs.github.io // TUI.chart https://github.com/nhn/tui.chart $element = [ 'text' => '', ]; return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'element' => $element, 'name' => 'div', 'attributes' => [ 'class' => \strtolower($language), ], ], ]; } return null; } /** * Continue block spoiler * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockSpoiler(array $line, ?array $_ = null) : ?array { if (!($this->options['code']['blocks'] ?? true) || !($this->options['code'] ?? true) ) { return null; } $marker = $line['text'][0]; $openerLength = \strspn($line['text'], $marker); if ($openerLength < 3) { return null; } $summary = \trim(\preg_replace('/^\?{3}(.+)?/s', '$1', $line['text']) ?? ''); $infostring = \trim(\substr($line['text'], $openerLength), "\t "); if (\strpos($infostring, '?') !== false) { return null; } // @performance Optimize away the child element for spoilers (if reasonable) // https://github.com/Karaka-Management/phpOMS/issues/367 return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'name' => 'details', 'element' => [ 'text' => '', 'elements' => [ [ 'name' => 'summary', 'text' => $summary, ], [ 'name' => 'span', 'text' => '', ], ], ], ], ]; } /** * Complete block table * * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockTableComplete(array $block) : ?array { if (!($this->options['tables']['tablespan'] ?? true)) { return $block; } $headerElements = &$block['element']['elements'][0]['elements'][0]['elements']; $headerElementsCount = \count($headerElements); for ($index = $headerElementsCount - 1; $index >= 0; --$index) { $colspan = 1; $headerElement = &$headerElements[$index]; while ($index && $headerElements[$index - 1]['handler']['argument'] === '>') { ++$colspan; $previousHeaderElement = &$headerElements[--$index]; $previousHeaderElement['merged'] = true; if (isset($previousHeaderElement['attributes'])) { $headerElement['attributes'] = $previousHeaderElement['attributes']; } } if ($colspan > 1) { if (!isset($headerElement['attributes'])) { $headerElement['attributes'] = []; } $headerElement['attributes']['colspan'] = $colspan; } } for ($index = 0; $index < $headerElementsCount; ++$index) { if (isset($headerElements[$index]['merged'])) { unset($headerElements[$index]); } } $headerElements = \array_values($headerElements); $rows = &$block['element']['elements'][1]['elements']; foreach ($rows as $rowNo => &$row) { $elements = &$row['elements']; for ($index = \count($elements) - 1; $index >= 0; --$index) { $colspan = 1; $element = &$elements[$index]; while ($index && $elements[$index - 1]['handler']['argument'] === '>') { ++$colspan; $PreviousElement = &$elements[--$index]; $PreviousElement['merged'] = true; if (isset($PreviousElement['attributes'])) { $element['attributes'] = $PreviousElement['attributes']; } } if ($colspan > 1) { if (!isset($element['attributes'])) { $element['attributes'] = []; } $element['attributes']['colspan'] = $colspan; } } } $rowCount = \count($rows); foreach ($rows as $rowNo => &$row) { $elements = &$row['elements']; foreach ($elements as $index => &$element) { $rowspan = 1; if (isset($element['merged'])) { continue; } while ($rowNo + $rowspan < $rowCount && $index < \count($rows[$rowNo + $rowspan]['elements']) && $rows[$rowNo + $rowspan]['elements'][$index]['handler']['argument'] === '^' && ($element['attributes']['colspan'] ?? null) === ($rows[$rowNo + $rowspan]['elements'][$index]['attributes']['colspan'] ?? null) ) { $rows[$rowNo + $rowspan]['elements'][$index]['merged'] = true; ++$rowspan; } if ($rowspan > 1) { if (!isset($element['attributes'])) { $element['attributes'] = []; } $element['attributes']['rowspan'] = $rowspan; } } } foreach ($rows as $rowNo => &$row) { $elements = &$row['elements']; for ($index = \count($elements) - 1; $index >= 0; --$index) { if (isset($elements[$index]['merged'])) { unset($elements[$index]); } } $row['elements'] = \array_values($elements); } return $block; } /** * Handle block checkbox * * @param array{body:string, indent:int, text:string} $line Line data * @param null|array $_ Current block (unused parameter) * * @return null|array * * @since 1.0.0 */ protected function blockCheckbox(array $line, ?array $_ = null) : ?array { $text = \trim($line['text']); $beginLine = \substr($text, 0, 4); if ($beginLine === '[ ] ') { return [ 'handler' => 'unchecked', 'text' => \substr(\trim($text), 4), ]; } elseif ($beginLine === '[x] ') { return [ 'handler' => 'checked', 'text' => \substr(\trim($text), 4), ]; } return null; } /** * Continue checkbox. * * This function doesn't do anything! * However required as per the parsing workflow since it is automatically called. * * @param array{body:string, indent:int, text:string} $_ Line data * @param array $__ Current block * * @return null|array * * @since 1.0.0 */ protected function blockCheckboxContinue(array $_, array $__) : ?array { return null; } /** * Complete block checkbox * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockCheckboxComplete(array $block) : array { $text = $block['text']; if ($this->markupEscaped || $this->safeMode) { $text = \htmlspecialchars($text, \ENT_QUOTES, 'UTF-8'); } $html = $block['handler'] === 'unchecked' ? ' ' . $this->formatOnce($text) : ' ' . $this->formatOnce($text); $block['element'] = [ 'rawHtml' => $html, 'allowRawHtmlInSafeMode' => true, ]; return $block; } /** * Formats text without double escaping * * @param string $text Text to format * * @return string * * @since 1.0.0 */ protected function formatOnce(string $text) : string { // backup settings $markupEscaped = $this->markupEscaped; $safeMode = $this->safeMode; // disable rules to prevent double escaping. $this->markupEscaped = false; $this->safeMode = false; // format line $text = $this->elements($this->lineElements($text)); // reset old values $this->markupEscaped = $markupEscaped; $this->safeMode = $safeMode; return $text; } /** * Parse attribute data * * @param string $attribute Attribute string * * @return array * * @since 1.0.0 */ protected function parseAttributeData(string $attribute) : array { if (!($this->options['special_attributes'] ?? true)) { return []; } $data = []; $attributes = \preg_split('/[ ]+/', $attribute, - 1, \PREG_SPLIT_NO_EMPTY); $classes = []; if ($attributes === false) { return []; } foreach ($attributes as $attribute) { if ($attribute[0] === '#') { $data['id'] = \substr($attribute, 1); } else { // "." $classes[] = \substr($attribute, 1); } } if (!empty($classes)) { $data['class'] = \implode(' ', $classes); } return $data; } /** * Encodes the ToC tag to a hashed tag and replace. * * This is used to avoid parsing user defined ToC tag which includes "_" in * their tag such as "[[_toc_]]". * * @param string $text Tag text to encode * * @return string * * @since 1.0.0 */ protected function encodeToCTagToHash(string $text) : string { $salt = \bin2hex(\random_bytes(4)); $tagOrigin = $this->options['toc']['set_toc_tag'] ?? '[toc]'; if (\strpos($text, $tagOrigin) === false) { return $text; } $tagHashed = \hash('sha256', $salt . $tagOrigin); return \str_replace($tagOrigin, $tagHashed, $text); } /** * Decodes the hashed ToC tag to an original tag and replaces. * * This is used to avoid parsing user defined ToC tag which includes "_" in * their tag such as "[[_toc_]]". * * @param string $text Tag text to encode * * @return string * * @since 1.0.0 */ protected function decodeToCTagFromHash(string $text) : string { $salt = \bin2hex(\random_bytes(4)); $tagOrigin = $this->options['toc']['set_toc_tag'] ?? '[toc]'; $tagHashed = \hash('sha256', $salt . $tagOrigin); if (\strpos($text, $tagHashed) === false) { return $text; } return \str_replace($tagHashed, $tagOrigin, $text); } /** * Generates an anchor text that are link-able even if the heading is not in ASCII. * * @param string $str Header text * * @return string * * @since 1.0.0 */ protected function createAnchorID(string $str) : string { if ($this->options['toc']['urlencode'] ?? false) { $str = $this->incrementAnchorId($str); return \urlencode($str); } $charMap = [ // Latin 'ร€' => 'A', 'ร' => 'A', 'ร‚' => 'A', 'รƒ' => 'A', 'ร„' => 'A', 'ร…' => 'AA', 'ร†' => 'AE', 'ร‡' => 'C', 'รˆ' => 'E', 'ร‰' => 'E', 'รŠ' => 'E', 'ร‹' => 'E', 'รŒ' => 'I', 'ร' => 'I', 'รŽ' => 'I', 'ร' => 'I', 'ร' => 'D', 'ร‘' => 'N', 'ร’' => 'O', 'ร“' => 'O', 'ร”' => 'O', 'ร•' => 'O', 'ร–' => 'O', 'ล' => 'O', 'ร˜' => 'OE', 'ร™' => 'U', 'รš' => 'U', 'ร›' => 'U', 'รœ' => 'U', 'ลฐ' => 'U', 'ร' => 'Y', 'รž' => 'TH', 'รŸ' => 'ss', 'ร ' => 'a', 'รก' => 'a', 'รข' => 'a', 'รฃ' => 'a', 'รค' => 'a', 'รฅ' => 'aa', 'รฆ' => 'ae', 'รง' => 'c', 'รจ' => 'e', 'รฉ' => 'e', 'รช' => 'e', 'รซ' => 'e', 'รฌ' => 'i', 'รญ' => 'i', 'รฎ' => 'i', 'รฏ' => 'i', 'รฐ' => 'd', 'รฑ' => 'n', 'รฒ' => 'o', 'รณ' => 'o', 'รด' => 'o', 'รต' => 'o', 'รถ' => 'o', 'ล‘' => 'o', 'รธ' => 'oe', 'รน' => 'u', 'รบ' => 'u', 'รป' => 'u', 'รผ' => 'u', 'ลฑ' => 'u', 'รฝ' => 'y', 'รพ' => 'th', 'รฟ' => 'y', // Latin symbols 'ยฉ' => '(c)', 'ยฎ' => '(r)', 'โ„ข' => '(tm)', // Greek 'ฮ‘' => 'A', 'ฮ’' => 'B', 'ฮ“' => 'G', 'ฮ”' => 'D', 'ฮ•' => 'E', 'ฮ–' => 'Z', 'ฮ—' => 'H', 'ฮ˜' => '8', 'ฮ™' => 'I', 'ฮš' => 'K', 'ฮ›' => 'L', 'ฮœ' => 'M', 'ฮ' => 'N', 'ฮž' => '3', 'ฮŸ' => 'O', 'ฮ ' => 'P', 'ฮก' => 'R', 'ฮฃ' => 'S', 'ฮค' => 'T', 'ฮฅ' => 'Y', 'ฮฆ' => 'F', 'ฮง' => 'X', 'ฮจ' => 'PS', 'ฮฉ' => 'W', 'ฮ†' => 'A', 'ฮˆ' => 'E', 'ฮŠ' => 'I', 'ฮŒ' => 'O', 'ฮŽ' => 'Y', 'ฮ‰' => 'H', 'ฮ' => 'W', 'ฮช' => 'I', 'ฮซ' => 'Y', 'ฮฑ' => 'a', 'ฮฒ' => 'b', 'ฮณ' => 'g', 'ฮด' => 'd', 'ฮต' => 'e', 'ฮถ' => 'z', 'ฮท' => 'h', 'ฮธ' => '8', 'ฮน' => 'i', 'ฮบ' => 'k', 'ฮป' => 'l', 'ฮผ' => 'm', 'ฮฝ' => 'n', 'ฮพ' => '3', 'ฮฟ' => 'o', 'ฯ€' => 'p', 'ฯ' => 'r', 'ฯƒ' => 's', 'ฯ„' => 't', 'ฯ…' => 'y', 'ฯ†' => 'f', 'ฯ‡' => 'x', 'ฯˆ' => 'ps', 'ฯ‰' => 'w', 'ฮฌ' => 'a', 'ฮญ' => 'e', 'ฮฏ' => 'i', 'ฯŒ' => 'o', 'ฯ' => 'y', 'ฮฎ' => 'h', 'ฯŽ' => 'w', 'ฯ‚' => 's', 'ฯŠ' => 'i', 'ฮฐ' => 'y', 'ฯ‹' => 'y', 'ฮ' => 'i', // Turkish 'ลž' => 'S', 'ฤฐ' => 'I', 'ฤž' => 'G', 'ลŸ' => 's', 'ฤฑ' => 'i', 'ฤŸ' => 'g', // Russian 'ะ' => 'A', 'ะ‘' => 'B', 'ะ’' => 'V', 'ะ“' => 'G', 'ะ”' => 'D', 'ะ•' => 'E', 'ะ' => 'Yo', 'ะ–' => 'Zh', 'ะ—' => 'Z', 'ะ˜' => 'I', 'ะ™' => 'J', 'ะš' => 'K', 'ะ›' => 'L', 'ะœ' => 'M', 'ะ' => 'N', 'ะž' => 'O', 'ะŸ' => 'P', 'ะ ' => 'R', 'ะก' => 'S', 'ะข' => 'T', 'ะฃ' => 'U', 'ะค' => 'F', 'ะฅ' => 'H', 'ะฆ' => 'C', 'ะง' => 'Ch', 'ะจ' => 'Sh', 'ะฉ' => 'Sh', 'ะช' => '', 'ะซ' => 'Y', 'ะฌ' => '', 'ะญ' => 'E', 'ะฎ' => 'Yu', 'ะฏ' => 'Ya', 'ะฐ' => 'a', 'ะฑ' => 'b', 'ะฒ' => 'v', 'ะณ' => 'g', 'ะด' => 'd', 'ะต' => 'e', 'ั‘' => 'yo', 'ะถ' => 'zh', 'ะท' => 'z', 'ะธ' => 'i', 'ะน' => 'j', 'ะบ' => 'k', 'ะป' => 'l', 'ะผ' => 'm', 'ะฝ' => 'n', 'ะพ' => 'o', 'ะฟ' => 'p', 'ั€' => 'r', 'ั' => 's', 'ั‚' => 't', 'ัƒ' => 'u', 'ั„' => 'f', 'ั…' => 'h', 'ั†' => 'c', 'ั‡' => 'ch', 'ัˆ' => 'sh', 'ั‰' => 'sh', 'ัŠ' => '', 'ั‹' => 'y', 'ัŒ' => '', 'ั' => 'e', 'ัŽ' => 'yu', 'ั' => 'ya', // Ukrainian 'ะ„' => 'Ye', 'ะ†' => 'I', 'ะ‡' => 'Yi', 'า' => 'G', 'ั”' => 'ye', 'ั–' => 'i', 'ั—' => 'yi', 'า‘' => 'g', // Czech 'ฤŒ' => 'C', 'ฤŽ' => 'D', 'ฤš' => 'E', 'ล‡' => 'N', 'ล˜' => 'R', 'ล ' => 'S', 'ลค' => 'T', 'ลฎ' => 'U', 'ลฝ' => 'Z', 'ฤ' => 'c', 'ฤ' => 'd', 'ฤ›' => 'e', 'ลˆ' => 'n', 'ล™' => 'r', 'ลก' => 's', 'ลฅ' => 't', 'ลฏ' => 'u', 'ลพ' => 'z', // Polish 'ฤ„' => 'A', 'ฤ†' => 'C', 'ฤ˜' => 'e', 'ล' => 'L', 'ลƒ' => 'N', 'ลš' => 'S', 'ลน' => 'Z', 'ลป' => 'Z', 'ฤ…' => 'a', 'ฤ‡' => 'c', 'ฤ™' => 'e', 'ล‚' => 'l', 'ล„' => 'n', 'ล›' => 's', 'ลบ' => 'z', 'ลผ' => 'z', // Latvian 'ฤ€' => 'A', 'ฤ’' => 'E', 'ฤข' => 'G', 'ฤช' => 'i', 'ฤถ' => 'k', 'ฤป' => 'L', 'ล…' => 'N', 'ลช' => 'u', 'ฤ' => 'a', 'ฤ“' => 'e', 'ฤฃ' => 'g', 'ฤซ' => 'i', 'ฤท' => 'k', 'ฤผ' => 'l', 'ล†' => 'n', 'ลซ' => 'u', ]; // Transliterate characters to ASCII if ($this->options['toc']['transliterate'] ?? false) { $str = \str_replace(\array_keys($charMap), $charMap, $str); } // Replace non-alphanumeric characters with our delimiter $optionDelimiter = $this->options['toc']['delimiter'] ?? '-'; $str = \preg_replace('/[^\p{L}\p{Nd}]+/u', $optionDelimiter, $str) ?? ''; // Remove duplicate delimiters $str = \preg_replace('/(' . \preg_quote($optionDelimiter, '/') . '){2,}/', '$1', $str) ?? ''; // Truncate slug to max. characters $optionLimit = $this->options['toc']['limit'] ?? \mb_strlen($str, 'UTF-8'); $str = \mb_substr($str, 0, $optionLimit, 'UTF-8'); // Remove delimiter from ends $str = \trim($str, $optionDelimiter); $urlLowercase = $this->options['toc']['lowercase'] ?? true; $str = $urlLowercase ? \mb_strtolower($str, 'UTF-8') : $str; return $this->incrementAnchorId($str); } /** * Set/stores the heading block to ToC list in a string and array format. * * @param array $content ToC content * * @return void * * @since 1.0.0 */ protected function setContentsList(array $content) : void { $this->contentsListArray[] = $content; $text = \trim(\strip_tags($this->elements($this->lineElements($content['text'])))); $id = $content['id']; $level = (int) \trim($content['level'], 'h'); if ($this->firstHeadLevel === 0) { $this->firstHeadLevel = $level; } // Stores in markdown list format as below: // - [Header1](#Header1) // - [Header2-1](#Header2-1) // - [Header3](#Header3) // - [Header2-2](#Header2-2) // ... $this->contentsListString .= \str_repeat( ' ', $this->firstHeadLevel - 1 > $level ? 1 : $level - ($this->firstHeadLevel - 1) ) . ' - [' . $text . '](#' . $id . ")\n"; } /** * Collect and count anchors in use to prevent duplicated ids. * * Also init optional blacklist of ids. * * @param string $str Header anchor * * @return string Incremental, numeric suffix * * @since 1.0.0 */ protected function incrementAnchorId(string $str) : string { // add blacklist to list of used anchors if (!$this->isBlacklistInitialized) { $this->initBlacklist(); } do { $this->anchorDuplicates[$str] = isset($this->anchorDuplicates[$str]) ? ++$this->anchorDuplicates[$str] : 0; $newStr = $str; if (($count = $this->anchorDuplicates[$str]) === 0) { return $newStr; } $newStr .= '-' . $count; } while (isset($this->anchorDuplicates[$newStr])); $this->anchorDuplicates[$newStr] = 0; return $newStr; } /** * Add blacklisted ids to anchor list. * * @return void * * @since 1.0.0 */ protected function initBlacklist() : void { if ($this->isBlacklistInitialized) { return; } if (!empty($this->options['headings']['blacklist']) && \is_array($this->options['headings']['blacklist'])) { foreach ($this->options['headings']['blacklist'] as $v) { $this->anchorDuplicates[(string) $v] = 0; } } $this->isBlacklistInitialized = true; } /** * Parse inline elements * * @param string $text Text to parse * @param array $nonNestables Inline elements that are not allowed to be nested * * @return array * * @since 1.0.0 */ protected function lineElements(string $text, array $nonNestables = []) : array { $elements = []; if (!empty($nonNestables)) { $nonNestables = \array_combine($nonNestables, $nonNestables); } // $exc is based on the first occurrence of a marker while (($exc = \strpbrk($text, $this->inlineMarkerList)) !== false) { $marker = $exc[0]; $markerPosition = \strlen($text) - \strlen($exc); // Get the first char before the marker $beforeMarkerPosition = $markerPosition - 1; $charBeforeMarker = $beforeMarkerPosition >= 0 ? $text[$markerPosition - 1] : ''; $excerpt = ['text' => $exc, 'context' => $text, 'before' => $charBeforeMarker]; foreach ($this->inlineTypes[$marker] as $inlineType) { // check to see if the current inline type is nestable in the current context if (isset($nonNestables[$inlineType])) { continue; } $inline = $this->{"inline{$inlineType}"}($excerpt); if ($inline === null) { continue; } // makes sure that the inline belongs to "our" marker if (isset($inline['position']) && $inline['position'] > $markerPosition) { continue; } // sets a default inline position if (!isset($inline['position'])) { $inline['position'] = $markerPosition; } // cause the new element to 'inherit' our non nestables $inline['element']['nonNestables'] = isset($inline['element']['nonNestables']) ? \array_merge($inline['element']['nonNestables'], $nonNestables) : $nonNestables; // the text that comes before the inline $unmarkedText = \substr($text, 0, $inline['position']); // compile the unmarked text $inlineText = $this->inlineText($unmarkedText); $elements[] = $inlineText['element']; // compile the inline $elements[] = $this->extractElement($inline); // remove the examined text $text = \substr($text, $inline['position'] + $inline['extent']); continue 2; } // the marker does not belong to an inline $unmarkedText = \substr($text, 0, $markerPosition + 1); $inlineText = $this->inlineText($unmarkedText); $elements[] = $inlineText['element']; $text = \substr($text, $markerPosition + 1); } $inlineText = $this->inlineText($text); $elements[] = $inlineText['element']; foreach ($elements as &$element) { if (!isset($element['autobreak'])) { $element['autobreak'] = false; } } return $elements; } /** * Continue block footnote * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockFootnoteContinue(array $line, array $block) : ?array { if ($line['text'][0] === '[' && \preg_match('/^\[\^(.+?)\]:/', $line['text']) ) { return null; } if (isset($block['interrupted'])) { if ($line['indent'] >= 4) { $block['text'] .= "\n\n" . $line['text']; return $block; } } else { $block['text'] .= "\n" . $line['text']; return $block; } return null; } /** * Complete block footnote * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockFootnoteComplete(array $block) : array { $this->definitionData['Footnote'][$block['label']] = [ 'text' => $block['text'], 'count' => null, 'number' => null, ]; return $block; } /** * Continue block footnote * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockDefinitionListContinue(array $line, array $block) : ?array { if ($line['text'][0] === ':') { return $this->addDdElement($line, $block); } if (isset($block['interrupted']) && $line['indent'] === 0) { return null; } if (isset($block['interrupted'])) { $block['dd']['handler']['function'] = 'textElements'; $block['dd']['handler']['argument'] .= "\n\n"; $block['dd']['handler']['destination'] = 'elements'; unset($block['interrupted']); } $text = \substr($line['body'], \min($line['indent'], 4)); $block['dd']['handler']['argument'] .= "\n" . $text; return $block; } /** * Continue block markup * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockMarkupContinue(array $line, array $block) : ?array { if (isset($block['closed'])) { return null; } if (\preg_match('/^<' . $block['name'] . '(?:[ ]*' . $this->regexHtmlAttribute . ')*[ ]*>/i', $line['text'])) { // open ++$block['depth']; } if (\preg_match('/(.*?)<\/' . $block['name'] . '>[ ]*$/i', $line['text'], $matches)) { // close if ($block['depth'] > 0) { --$block['depth']; } else { $block['closed'] = true; } } if (isset($block['interrupted'])) { $block['element']['rawHtml'] .= "\n"; unset($block['interrupted']); } $block['element']['rawHtml'] .= "\n".$line['body']; return $block; } /** * Complete block markup * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockMarkupComplete(array $block) : array { if (!isset($block['void'])) { $block['element']['rawHtml'] = $this->processTag($block['element']['rawHtml']); } return $block; } /** * Handle footnote marker * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array{extent:int, element:array} * * @since 1.0.0 */ protected function inlineFootnoteMarker(array $excerpt) : ?array { if (\preg_match('/^\[\^(.+?)\]/', $excerpt['text'], $matches) !== 1) { return null; } $name = $matches[1]; if (!isset($this->definitionData['Footnote'][$name])) { return null; } ++$this->definitionData['Footnote'][$name]['count']; if (!isset($this->definitionData['Footnote'][$name]['number'])) { $this->definitionData['Footnote'][$name]['number'] = ++ $this->footnoteCount; // ยป & } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'sup', 'attributes' => ['id' => 'fnref' . $this->definitionData['Footnote'][$name]['count'] . ':' . $name], 'element' => [ 'name' => 'a', 'attributes' => ['href' => '#fn:' . $name, 'class' => 'footnote-ref'], 'text' => $this->definitionData['Footnote'][$name]['number'], ], ], ]; } /** * Insert/replace text with abbreviation * * @param array $element Element to insert abbreviation into * * @return array * * @since 1.0.0 */ protected function insertAbreviation(array $element) : array { if (!isset($element['text'])) { return $element; } $element['elements'] = self::pregReplaceElements( '/\b' . \preg_quote($this->currentAbbreviation, '/') . '\b/', [ [ 'name' => 'abbr', 'attributes' => [ 'title' => $this->currentMeaning, ], 'text' => $this->currentAbbreviation, ], ], $element['text'] ); unset($element['text']); return $element; } /** * Inline elements in text * * @param string $text Text to search for inlinable elements * * @return array * * @since 1.0.0 */ protected function inlineText(string $text) : array { $inline = [ 'extent' => \strlen($text), 'element' => [], ]; $inline['element']['elements'] = self::pregReplaceElements( $this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/', [ ['name' => 'br'], ['text' => "\n"], ], $text ); // Handle abbreviations if (!isset($this->definitionData['Abbreviation'])) { return $inline; } foreach ($this->definitionData['Abbreviation'] as $abbreviation => $meaning) { $this->currentAbbreviation = $abbreviation; $this->currentMeaning = $meaning; $inline['element'] = $this->elementApplyRecursiveDepthFirst( 'insertAbreviation', $inline['element'] ); } return $inline; } /** * Handle block list * * @param array $line Line data * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function addDdElement(array $line, array $block) : array { $text = \substr($line['text'], 1); $text = \trim($text); unset($block['dd']); $block['dd'] = [ 'name' => 'dd', 'handler' => [ 'function' => 'lineElements', 'argument' => $text, 'destination' => 'elements', ], ]; if (isset($block['interrupted'])) { $block['dd']['handler']['function'] = 'textElements'; unset($block['interrupted']); } $block['element']['elements'][] = &$block['dd']; return $block; } /** * Create footnotes * * @return array * * @since 1.0.0 */ protected function buildFootnoteElement() : array { $element = [ 'name' => 'div', 'attributes' => ['class' => 'footnotes'], 'elements' => [ ['name' => 'hr'], [ 'name' => 'ol', 'elements' => [], ], ], ]; \uasort($this->definitionData['Footnote'], [self::class, 'sortFootnotes']); foreach ($this->definitionData['Footnote'] as $definitionId => $definitionData) { if (!isset($definitionData['number'])) { continue; } $text = $definitionData['text']; $textElements = $this->textElements($text); $numbers = \range(1, $definitionData['count']); $backLinkElements = []; foreach ($numbers as $number) { $backLinkElements[] = ['text' => ' ']; $backLinkElements[] = [ 'name' => 'a', 'attributes' => [ 'href' => "#fnref{$number}:{$definitionId}", 'rev' => 'footnote', 'class' => 'footnote-backref', ], 'rawHtml' => '↩', 'allowRawHtmlInSafeMode' => true, 'autobreak' => false, ]; } unset($backLinkElements[0]); $n = \count($textElements) - 1; if ($textElements[$n]['name'] === 'p') { $backLinkElements = \array_merge( [ [ 'rawHtml' => ' ', 'allowRawHtmlInSafeMode' => true, ], ], $backLinkElements ); unset($textElements[$n]['name']); $textElements[$n] = [ 'name' => 'p', 'elements' => \array_merge( [$textElements[$n]], $backLinkElements ), ]; } else { $textElements[] = [ 'name' => 'p', 'elements' => $backLinkElements, ]; } $element['elements'][1]['elements'][] = [ 'name' => 'li', 'attributes' => ['id' => 'fn:' . $definitionId], 'elements' => \array_merge( $textElements ), ]; } return $element; } /** * Handle markup/html. * * Ensures that html is well formed. * * This function is called recursively * * @param string $elementMarkup Markup * * @return string * * @since 1.0.0 */ protected function processTag(string $elementMarkup) : string { // http://stackoverflow.com/q/1148928/200145 \libxml_use_internal_errors(true); $dom = new \DOMDocument(); // http://stackoverflow.com/q/11309194/200145 $elementMarkup = \mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8'); // http://stackoverflow.com/q/4879946/200145 $dom->loadHTML($elementMarkup); if ($dom->documentElement === null) { return ''; } if ($dom->doctype !== null) { $dom->removeChild($dom->doctype); } if ($dom->firstChild !== null && $dom->firstChild->firstChild?->firstChild !== null) { $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); } $elementText = ''; if ($dom->documentElement->getAttribute('markdown') === '1') { foreach ($dom->documentElement->childNodes as $node) { $elementText .= $dom->saveHTML($node); } $dom->documentElement->removeAttribute('markdown'); $elementText = "\n" . $this->text($elementText) . "\n"; } else { foreach ($dom->documentElement->childNodes as $node) { $nodeMarkup = $dom->saveHTML($node); if ($nodeMarkup === false) { $nodeMarkup = ''; } $elementText .= $node instanceof \DOMElement && !\in_array($node->nodeName, $this->textLevelElements) ? $this->processTag($nodeMarkup) : $nodeMarkup; } } // because we don't want for markup to get encoded $dom->documentElement->nodeValue = 'placeholder\x1A'; $markup = $dom->saveHTML($dom->documentElement); if ($markup === false) { return ''; } return \str_replace('placeholder\x1A', $elementText, $markup); } /** * Footnote sort function * * @param array $a First element * @param array $b Second element * * @return int * * @since 1.0.0 */ protected function sortFootnotes(array $a, array $b) : int { return $a['number'] <=> $b['number']; } /** * Parse text elements to lines and then handle lines. * * @param string $text Text to parse * * @return array * * @since 1.0.0 */ protected function textElements(string $text) : array { // make sure no definitions are set $this->definitionData = []; // standardize line breaks $text = \str_replace(["\r\n", "\r"], "\n", $text); // remove surrounding line breaks $text = \trim($text, "\n"); // split text into lines $lines = \explode("\n", $text); // iterate through lines to identify blocks return $this->linesElements($lines); } /** * Handle lines of elements * * @param string[] $lines Lines to parse * * @return array * * @since 1.0.0 */ protected function linesElements(array $lines) : array { $elements = []; $currentBlock = null; foreach ($lines as $line) { if (\rtrim($line) === '') { if (isset($currentBlock)) { $currentBlock['interrupted'] = (isset($currentBlock['interrupted']) ? $currentBlock['interrupted'] + 1 : 1 ); } continue; } while (($beforeTab = \strstr($line, "\t", true)) !== false) { $shortage = 4 - \mb_strlen($beforeTab, 'utf-8') % 4; $line = $beforeTab . \str_repeat(' ', $shortage) . \substr($line, \strlen($beforeTab) + 1); } $indent = \strspn($line, ' '); $text = $indent > 0 ? \substr($line, $indent) : $line; $line = ['body' => $line, 'indent' => $indent, 'text' => $text]; if (isset($currentBlock['continuable'])) { $methodName = 'block' . $currentBlock['type'] . 'Continue'; $block = $this->{$methodName}($line, $currentBlock); if (isset($block)) { $currentBlock = $block; continue; } elseif (\in_array($currentBlock['type'], self::COMPLETABLE)) { $methodName = 'block' . $currentBlock['type'] . 'Complete'; $currentBlock = $this->{$methodName}($currentBlock); } } $marker = $text[0]; $blockTypes = $this->unmarkedBlockTypes; if (isset($this->blockTypes[$marker])) { foreach ($this->blockTypes[$marker] as $blockType) { $blockTypes [] = $blockType; } } foreach ($blockTypes as $blockType) { $block = $this->{"block{$blockType}"}($line, $currentBlock); if (isset($block)) { $block['type'] = $blockType; if (!isset($block['identified'])) { if (isset($currentBlock)) { $elements[] = $this->extractElement($currentBlock); } $block['identified'] = true; } if (\in_array($blockType, self::CONTINUABLE)) { $block['continuable'] = true; } $currentBlock = $block; continue 2; } } if (isset($currentBlock) && $currentBlock['type'] === 'Paragraph') { $block = $this->paragraphContinue($line, $currentBlock); } if (isset($block)) { $currentBlock = $block; } else { if (isset($currentBlock)) { $elements[] = $this->extractElement($currentBlock); } $currentBlock = [ 'type' => 'Paragraph', 'element' => [ 'name' => 'p', 'handler' => [ 'function' => 'lineElements', 'argument' => $line['text'], 'destination' => 'elements', ], ], ]; $currentBlock['identified'] = true; } } if (isset($currentBlock['continuable']) && \in_array($currentBlock['type'], self::COMPLETABLE)) { $methodName = 'block' . $currentBlock['type'] . 'Complete'; $currentBlock = $this->{$methodName}($currentBlock); } if (isset($currentBlock)) { $elements[] = $this->extractElement($currentBlock); } return $elements; } /** * Extract element from block * * @param array $block Block * * @return array * * @since 1.0.0 */ protected function extractElement(array $block) : array { if (!isset($block['element'])) { if (isset($block['markup'])) { $block['element'] = ['rawHtml' => $block['markup']]; } elseif (isset($block['hidden'])) { $block['element'] = []; } } return $block['element']; } /** * Continue block code * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockCodeContinue(array $line, array $block) : ?array { if ($line['indent'] < 4) { return null; } if (isset($block['interrupted'])) { $block['element']['element']['text'] .= \str_repeat("\n", $block['interrupted']); unset($block['interrupted']); } $block['element']['element']['text'] .= "\n"; $text = \substr($line['body'], 4); $block['element']['element']['text'] .= $text; return $block; } /** * Complete block code * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockCodeComplete(array $block) : array { return $block; } /** * Continue block code * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockCommentContinue(array $line, array $block) : ?array { if (isset($block['closed'])) { return null; } $block['element']['rawHtml'] .= "\n" . $line['body']; if (\strpos($line['text'], '-->') !== false) { $block['closed'] = true; } return $block; } /** * Continue block fenced code * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockFencedCodeContinue(array $line, array $block) : ?array { if (isset($block['complete'])) { return null; } if (isset($block['interrupted'])) { $block['element']['element']['text'] .= \str_repeat("\n", $block['interrupted']); unset($block['interrupted']); } if (($len = \strspn($line['text'], $block['char'])) >= $block['openerLength'] && \rtrim(\substr($line['text'], $len), ' ') === '' ) { $block['element']['element']['text'] = \substr($block['element']['element']['text'], 1); $block['complete'] = true; return $block; } $block['element']['element']['text'] .= "\n" . $line['body']; return $block; } /** * Complete block fenced code * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockFencedCodeComplete(array $block) : array { return $block; } /** * Continue block spoiler * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockSpoilerContinue(array $line, array $block) : ?array { if (isset($block['complete'])) { return null; } if (isset($block['interrupted'])) { $block['element']['element']['text'] .= \str_repeat("\n", $block['interrupted']); unset($block['interrupted']); } if (($len = \strspn($line['text'], $block['char'])) >= $block['openerLength'] && \rtrim(\substr($line['text'], $len), ' ') === '' ) { $block['element']['element']['text'] = \substr($block['element']['element']['text'], 1); $block['complete'] = true; return $block; } $block['element']['element']['elements'][1]['text'] .= "\n" . $line['body']; return $block; } /** * Complete block spoiler * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockSpoilerComplete(array $block) : array { return $block; } /** * Continue block list * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockListContinue(array $line, array $block) : ?array { if (isset($block['interrupted']) && empty($block['li']['handler']['argument'])) { return null; } $requiredIndent = $block['indent'] + \strlen($block['data']['marker']); if ($line['indent'] < $requiredIndent && (($block['data']['type'] === 'ol' && \preg_match('/^[0-9]++' . $block['data']['markerTypeRegex'] . '(?:[ ]++(.*)|$)/', $line['text'], $matches) ) || ($block['data']['type'] === 'ul' && \preg_match('/^' . $block['data']['markerTypeRegex'] . '(?:[ ]++(.*)|$)/', $line['text'], $matches) )) ) { if (isset($block['interrupted'])) { $block['li']['handler']['argument'][] = ''; $block['loose'] = true; unset($block['interrupted']); } unset($block['li']); $text = $matches[1] ?? ''; $block['indent'] = $line['indent']; $block['li'] = [ 'name' => 'li', 'handler' => [ 'function' => 'li', 'argument' => [$text], 'destination' => 'elements', ], ]; $block['element']['elements'][] = &$block['li']; return $block; } elseif ($line['indent'] < $requiredIndent && $this->blockList($line)) { return null; } if ($line['text'][0] === '[' && $this->blockReference($line)) { return $block; } if ($line['indent'] >= $requiredIndent) { if (isset($block['interrupted'])) { $block['li']['handler']['argument'][] = ''; $block['loose'] = true; unset($block['interrupted']); } $text = \substr($line['body'], $requiredIndent); $block['li']['handler']['argument'][] = $text; return $block; } if (!isset($block['interrupted'])) { $text = \preg_replace('/^[ ]{0,' . $requiredIndent . '}+/', '', $line['body']); $block['li']['handler']['argument'][] = $text; return $block; } return null; } /** * Complete block list * * @param array $block Current block * * @return array * * @since 1.0.0 */ protected function blockListComplete(array $block) : array { if (!isset($block['loose'])) { return $block; } foreach ($block['element']['elements'] as &$li) { if (\end($li['handler']['argument']) !== '') { $li['handler']['argument'][] = ''; } } return $block; } /** * Continue block quote * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockQuoteContinue(array $line, array $block) : ?array { if (isset($block['interrupted'])) { return null; } if ($line['text'][0] === '>' && \preg_match('/^>[ ]?+(.*+)/', $line['text'], $matches)) { $block['element']['handler']['argument'][] = $matches[1]; return $block; } if (!isset($block['interrupted'])) { $block['element']['handler']['argument'][] = $line['text']; return $block; } return null; } /** * Continue block table * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function blockTableContinue(array $line, array $block) : ?array { if (isset($block['interrupted']) || (\count($block['alignments']) !== 1 && $line['text'][0] !== '|' && \strpos($line['text'], '|') === false) ) { return null; } $elements = []; $row = $line['text']; $row = \trim($row); $row = \trim($row, '|'); \preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]++`|`)++/', $row, $matches); $cells = \array_slice($matches[0], 0, \count($block['alignments'])); foreach ($cells as $index => $cell) { $cell = \trim($cell); $element = [ 'name' => 'td', 'handler' => [ 'function' => 'lineElements', 'argument' => $cell, 'destination' => 'elements', ], ]; if (isset($block['alignments'][$index])) { $element['attributes'] = [ 'style' => 'text-align: ' . $block['alignments'][$index] . ';', ]; } $elements [] = $element; } $element = [ 'name' => 'tr', 'elements' => $elements, ]; $block['element']['elements'][1]['elements'][] = $element; return $block; } /** * Continue block paragraph * * @param array{body:string, indent:int, text:string} $line Line data * @param array $block Current block * * @return null|array * * @since 1.0.0 */ protected function paragraphContinue(array $line, array $block) : ?array { if (isset($block['interrupted'])) { return null; } $block['element']['handler']['argument'] .= "\n".$line['text']; return $block; } /** * Handle link * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array * * @since 1.0.0 */ protected function inlineLinkParent(array $excerpt) : ?array { $element = [ 'name' => 'a', 'handler' => [ 'function' => 'lineElements', 'argument' => null, 'destination' => 'elements', ], 'nonNestables' => ['Url', 'Link'], 'attributes' => [ 'href' => null, 'title' => null, ], ]; $extent = 0; $remainder = $excerpt['text']; if (\preg_match('/\[((?:[^][]++|(?R))*+)\]/', $remainder, $matches)) { $element['handler']['argument'] = $matches[1]; $extent += \strlen($matches[0]); $remainder = \substr($remainder, $extent); } else { return null; } if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches)) { $element['attributes']['href'] = UriFactory::build(\str_replace('{$CSRF}', 'ERROR', $matches[1])); if (isset($matches[2])) { $element['attributes']['title'] = \substr($matches[2], 1, - 1); } $extent += \strlen($matches[0]); } else { if (\preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) { $definition = \strlen($matches[1]) !== 0 ? $matches[1] : $element['handler']['argument']; $definition = \strtolower($definition); $extent += \strlen($matches[0]); } else { $definition = \strtolower($element['handler']['argument']); } if (!isset($this->definitionData['Reference'][$definition])) { return null; } $definition = $this->definitionData['Reference'][$definition]; $element['attributes']['href'] = $definition['url']; $element['attributes']['title'] = $definition['title']; } return [ 'extent' => $extent, 'element' => $element, ]; } /** * Handle special character * * @param array{text:string, context:string, before:string} $excerpt Inline data * * @return null|array * * @since 1.0.0 */ protected function inlineSpecialCharacter(array $excerpt) : ?array { if (\substr($excerpt['text'], 1, 1) !== ' ' && \strpos($excerpt['text'], ';') !== false && \preg_match('/^&(#?+[0-9a-zA-Z]++);/', $excerpt['text'], $matches) ) { return [ 'element' => ['rawHtml' => '&' . $matches[1] . ';'], 'extent' => \strlen($matches[0]), ]; } return null; } /** * Handle "handler" * * @param array $element Element to handle * * @return array * * @since 1.0.0 */ protected function handle(array $element) : array { if (!isset($element['handler'])) { return $element; } if (!isset($element['nonNestables'])) { $element['nonNestables'] = []; } if (\is_string($element['handler'])) { $function = $element['handler']; $argument = $element['text']; unset($element['text']); $destination = 'rawHtml'; } else { $function = $element['handler']['function']; $argument = $element['handler']['argument']; $destination = $element['handler']['destination']; } $element[$destination] = $this->{$function}($argument, $element['nonNestables']); if ($destination === 'handler') { $element = $this->handle($element); } unset($element['handler']); return $element; } /** * Handle element recursively * * @param string|\Closure $closure Closure for handling element * @param array $element Element to handle * * @return array * * @since 1.0.0 */ protected function elementApplyRecursive(string|\Closure $closure, array $element) : array { $element = \is_string($closure) ? $this->{$closure}($element) : $closure($element); if (isset($element['elements'])) { foreach ($element['elements'] as &$e) { $e = $this->elementApplyRecursive($closure, $e); } } elseif (isset($element['element'])) { $element['element'] = $this->elementApplyRecursive($closure, $element['element']); } return $element; } /** * Handle element recursively * * @param string|\Closure $closure Closure for handling element * @param array $element Element to handle * * @return array * * @since 1.0.0 */ protected function elementApplyRecursiveDepthFirst(string|\Closure $closure, array $element) : array { if (isset($element['elements'])) { foreach ($element['elements'] as &$e) { $e = $this->elementApplyRecursiveDepthFirst($closure, $e); } } elseif (isset($element['element'])) { foreach ($element['element'] as &$e) { $e = $this->elementApplyRecursiveDepthFirst($closure, $e); } } return \is_string($closure) ? $this->{$closure}($element) : $closure($element); } /** * Render element * * @param array $element Element to render * * @return string * * @since 1.0.0 */ protected function element(array $element) : string { if ($this->safeMode) { $element = $this->sanitizeElement($element); } // identity map if element has no handler $element = $this->handle($element); $hasName = isset($element['name']); $markup = ''; if ($hasName) { $markup .= '<' . $element['name']; if (isset($element['attributes'])) { foreach ($element['attributes'] as $name => $value) { if ($value === null) { continue; } $markup .= ' ' . $name . '="' . \htmlspecialchars((string) $value, \ENT_QUOTES, 'UTF-8') . '"'; } } } $permitRawHtml = false; $text = null; if (isset($element['text'])) { $text = $element['text']; } elseif (isset($element['rawHtml'])) { // very strongly consider an alternative if you're writing an extension $text = $element['rawHtml']; $permitRawHtml = !$this->safeMode || ($element['allowRawHtmlInSafeMode'] ?? false); } $hasContent = isset($text) || isset($element['element']) || isset($element['elements']); if ($hasContent) { $markup .= $hasName ? '>' : ''; if (isset($element['elements'])) { $markup .= $this->elements($element['elements']); } elseif (isset($element['element'])) { $markup .= $this->element($element['element']); } elseif (!$permitRawHtml) { $markup .= \htmlspecialchars((string) $text, \ENT_NOQUOTES, 'UTF-8'); } else { $markup .= $text; } $markup .= $hasName ? '' : ''; } elseif ($hasName) { $markup .= ' />'; } return $markup; } /** * Render elements * * @param array $elements Elements to render * * @return string * * @since 1.0.0 */ protected function elements(array $elements) : string { $markup = ''; $autoBreak = true; foreach ($elements as $element) { if (empty($element)) { continue; } $autoBreakNext = (isset($element['autobreak']) ? $element['autobreak'] : isset($element['name']) ); // (autobreak === false) covers both sides of an element $autoBreak = $autoBreak ? $autoBreakNext : $autoBreak; $markup .= ($autoBreak ? "\n" : '') . $this->element($element); $autoBreak = $autoBreakNext; } return $markup . ($autoBreak ? "\n" : ''); } /** * Handle list * * @param string[] $lines Lines * * @return array * * @since 1.0.0 */ protected function li(array $lines) : array { $elements = $this->linesElements($lines); if (!\in_array('', $lines) && isset($elements[0], $elements[0]['name']) && $elements[0]['name'] === 'p' ) { unset($elements[0]['name']); } return $elements; } /** * Replace occurrences $regexp with $elements in $text. * * @param string $regexp Regex * @param array $elements Elements to replace * @param string $text Text to match against regex * * @return array * * @since 1.0.0 */ protected static function pregReplaceElements(string $regexp, array $elements, string $text) : array { $newElements = []; while (\preg_match($regexp, $text, $matches, \PREG_OFFSET_CAPTURE)) { $offset = (int) $matches[0][1]; $before = \substr($text, 0, $offset); $after = \substr($text, $offset + \strlen($matches[0][0])); $newElements[] = ['text' => $before]; foreach ($elements as $element) { $newElements[] = $element; } $text = $after; } $newElements[] = ['text' => $text]; return $newElements; } /** * Sanitize element * * @param array $element Element to sanitize * * @return array * * @since 1.0.0 */ protected function sanitizeElement(array $element) : array { static $goodAttribute = '/^[a-zA-Z0-9][a-zA-Z0-9-_]*+$/'; static $safeUrlNameToAtt = [ 'a' => 'href', 'img' => 'src', ]; if (!isset($element['name'])) { unset($element['attributes']); return $element; } if (isset($safeUrlNameToAtt[$element['name']])) { $element = $this->filterUnsafeUrlInAttribute($element, $safeUrlNameToAtt[$element['name']]); } if (!empty($element['attributes'])) { foreach ($element['attributes'] as $att => $_) { if (!\preg_match($goodAttribute, $att)) { // filter out badly parsed attribute unset($element['attributes'][$att]); } elseif (\str_starts_with($att, 'on')) { // dump onevent attribute unset($element['attributes'][$att]); } } } return $element; } /** * Sanitize url in attribute * * @param array $element Element to sanitize * @param string $attribute Attribute to sanitize * * @return array * * @since 1.0.0 */ protected function filterUnsafeUrlInAttribute(array $element, string $attribute) : array { foreach ($this->safeLinksWhitelist as $scheme) { if (\str_starts_with($element['attributes'][$attribute], $scheme)) { return $element; } } $element['attributes'][$attribute] = \str_replace(':', '%3A', $element['attributes'][$attribute]); return $element; } }