options = $params; $this->options['toc'] = $this->options['toc'] ?? false; // Marks $state = $this->options['mark'] ?? true; if ($state !== false) { $this->InlineTypes['='][] = 'mark'; $this->inlineMarkerList .= '='; } // Keystrokes $state = $this->options['keystrokes'] ?? true; if ($state !== false) { $this->InlineTypes['['][] = 'Keystrokes'; $this->inlineMarkerList .= '['; } // Inline Math $state = $this->options['math'] ?? false; if ($state !== false) { $this->InlineTypes['\\'][] = 'Math'; $this->inlineMarkerList .= '\\'; $this->InlineTypes['$'][] = 'Math'; $this->inlineMarkerList .= '$'; } // Superscript $state = $this->options['sup'] ?? false; if ($state !== false) { $this->InlineTypes['^'][] = 'Superscript'; $this->inlineMarkerList .= '^'; } // Subscript $state = $this->options['sub'] ?? false; if ($state !== false) { $this->InlineTypes['~'][] = 'Subscript'; } // Emojis $state = $this->options['emojis'] ?? true; if ($state !== false) { $this->InlineTypes[':'][] = 'Emojis'; $this->inlineMarkerList .= ':'; } // Typographer $state = $this->options['typographer'] ?? false; if ($state !== false) { $this->InlineTypes['('][] = 'Typographer'; $this->inlineMarkerList .= '('; $this->InlineTypes['.'][] = 'Typographer'; $this->inlineMarkerList .= '.'; $this->InlineTypes['+'][] = 'Typographer'; $this->inlineMarkerList .= '+'; $this->InlineTypes['!'][] = 'Typographer'; $this->inlineMarkerList .= '!'; $this->InlineTypes['?'][] = 'Typographer'; $this->inlineMarkerList .= '?'; } // Smartypants $state = $this->options['smarty'] ?? false; if ($state !== false) { $this->InlineTypes['<'][] = 'Smartypants'; $this->inlineMarkerList .= '<'; $this->InlineTypes['>'][] = 'Smartypants'; $this->inlineMarkerList .= '>'; $this->InlineTypes['-'][] = 'Smartypants'; $this->inlineMarkerList .= '-'; $this->InlineTypes['.'][] = 'Smartypants'; $this->inlineMarkerList .= '.'; $this->InlineTypes["'"][] = 'Smartypants'; $this->inlineMarkerList .= "'"; $this->InlineTypes['"'][] = 'Smartypants'; $this->inlineMarkerList .= '"'; $this->InlineTypes['`'][] = 'Smartypants'; $this->inlineMarkerList .= '`'; } /* * Blocks * ------------------------------------------------------------------------ */ // Block Math $state = $this->options['math'] ?? false; if ($state !== false) { $this->BlockTypes['\\'][] = 'Math'; $this->BlockTypes['$'][] = 'Math'; } // Task $state = $this->options['lists']['tasks'] ?? true; if ($state !== false) { $this->BlockTypes['['][] = 'Checkbox'; } } public function textParent($text) { $Elements = $this->textElements($text); # convert to markup $markup = $this->elements($Elements); # trim line breaks $markup = \trim($markup, "\n"); # merge consecutive dl elements $markup = \preg_replace('/<\/dl>\s+
\s+/', '', $markup); # add footnotes if (isset($this->DefinitionData['Footnote'])) { $Element = $this->buildFootnoteElement(); $markup .= "\n" . $this->element($Element); } return $markup; } /** * Parses the given markdown string to an HTML string but it leaves the ToC * tag as is. It's an alias of the parent method "\DynamicParent::text()". */ public function body($text) : string { $text = $this->encodeTagToHash($text); // Escapes ToC tag temporary $html = $this->textParent($text); // Parses the markdown text return $this->decodeTagFromHash($html); // Unescape the ToC tag } /** * Parses markdown string to HTML and also the "[toc]" tag as well. * It overrides the parent method: \Parsedown::text(). */ public function text($text) { // 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; } $tagOrigin = $this->getTagToC(); if (\strpos($text, $tagOrigin) === false) { return $html; } $tocData = $this->contentsList(); $tocId = $this->getIdAttributeToC(); $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 */ public function contentsList($typeReturn = 'html') { if (\strtolower($typeReturn) === 'html') { $result = ''; if (!empty($this->contentsListString)) { // Parses the ToC list in markdown to HTML $result = $this->body($this->contentsListString); } return $result; } if (\strtolower($typeReturn) === 'json') { return \json_encode($this->contentsListArray); } // Forces to return ToC as "html" \error_log( 'Unknown return type given while parsing ToC.' .' At: '.__FUNCTION__.'() ' .' in Line:'.__LINE__.' (Using default type)' ); return $this->contentsList('html'); } /** * ------------------------------------------------------------------------ * Inline * ------------------------------------------------------------------------. */ // inlineCode protected function inlineCode($Excerpt) { $codeSnippets = $this->options['code']['inline'] ?? true; $codeMain = $this->options['code'] ?? true; if ($codeSnippets !== true || $codeMain !== true) { return; } $marker = $Excerpt['text'][0]; if (\preg_match('/^(['.$marker.']++)[ ]*+(.+?)[ ]*+(? \strlen($matches[0]), 'element' => [ 'name' => 'code', 'text' => $text, ], ]; } } protected function inlineEmailTag($Excerpt) { $mainState = $this->options['links'] ?? true; $state = $this->options['links']['email_links'] ?? true; if (!$mainState || !$state) { return; } $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) ){ $url = $matches[1]; if (!isset($matches[2])) { $url = "mailto:{$url}"; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'a', 'text' => $matches[1], 'attributes' => [ 'href' => $url, ], ], ]; } } protected function inlineEmphasis($Excerpt) { $state = $this->options['emphasis'] ?? true; if (!$state) { return; } if (!isset($Excerpt['text'][1])) { return; } $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; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => $emphasis, 'handler' => [ 'function' => 'lineElements', 'argument' => $matches[1], 'destination' => 'elements', ], ], ]; } protected function inlineImage($Excerpt) { $state = $this->options['images'] ?? true; if (!$state) { return; } if (!isset($Excerpt['text'][1]) || $Excerpt['text'][1] !== '[') { return; } $Excerpt['text']= \substr($Excerpt['text'], 1); $Link = $this->inlineLink($Excerpt); if ($Link === null) { return; } $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; } protected function inlineLink($Excerpt) { $state = $this->options['links'] ?? true; if (!$state) { return; } $Link = $this->inlineLinkParent($Excerpt); $remainder = $Link !== null ? \substr($Excerpt['text'], $Link['extent']) : ''; if (\preg_match('/^[ ]*{('.$this->regexAttribute.'+)}/', $remainder, $matches)) { $Link['element']['attributes'] += $this->parseAttributeData($matches[1]); $Link['extent'] += \strlen($matches[0]); } return $Link; } protected function inlineMarkup($Excerpt) { $state = $this->options['markup'] ?? true; if (!$state) { return; } if ($this->markupEscaped || $this->safeMode || \strpos($Excerpt['text'], '>') === false) { return; } if ($Excerpt['text'][1] === '/' && \preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) { return [ 'element' => ['rawHtml' => $matches[0]], 'extent' => \strlen($matches[0]), ]; } if ($Excerpt['text'][1] === '!' && \preg_match('/^/s', $Excerpt['text'], $matches)) { return [ 'element' => ['rawHtml' => $matches[0]], 'extent' => \strlen($matches[0]), ]; } if ($Excerpt['text'][1] !== ' ' && \preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) { return [ 'element' => ['rawHtml' => $matches[0]], 'extent' => \strlen($matches[0]), ]; } } protected function inlineStrikethrough($Excerpt) { $state = $this->options['strikethroughs'] ?? true; if (!$state) { return; } if (!isset($Excerpt['text'][1])) { return; } if ($Excerpt['text'][1] === '~' && \preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'del', 'handler' => [ 'function' => 'lineElements', 'argument' => $matches[1], 'destination' => 'elements', ], ], ]; } } protected function inlineUrl($Excerpt) { $state = $this->options['links'] ?? true; if (!$state) { return; } if ($this->urlsLinked !== true || !isset($Excerpt['text'][2]) || $Excerpt['text'][2] !== '/') { return; } if (\strpos($Excerpt['context'], 'http') !== false && \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, \PREG_OFFSET_CAPTURE) ) { $url = $matches[0][0]; return [ 'extent' => \strlen($matches[0][0]), 'position' => $matches[0][1], 'element' => [ 'name' => 'a', 'text' => $url, 'attributes' => [ 'href' => $url, ], ], ]; } } protected function inlineUrlTag($Excerpt) { $state = $this->options['links'] ?? true; if (!$state) { return; } if (\strpos($Excerpt['text'], '>') !== false && \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) { $url = $matches[1]; return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'a', 'text' => $url, 'attributes' => [ 'href' => $url, ], ], ]; } } protected function inlineEmojis($excerpt) { $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:' => 'โฌœ', ]; if (\preg_match('/^(:)([^: ]*?)(:)/', $excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \str_replace(\array_keys($emojiMap), $emojiMap, $matches[0]), ], ]; } } // Inline Marks protected function inlineMark($excerpt) { if (\preg_match('/^(==)([^=]*?)(==)/', $excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'mark', 'text' => $matches[2], ], ]; } } // Inline Keystrokes protected function inlineKeystrokes($excerpt) { if (\preg_match('/^(? \strlen($matches[0]), 'element' => [ 'name' => 'kbd', 'text' => $matches[1], ], ]; } } // Inline Superscript protected function inlineSuperscript($excerpt) { if (\preg_match('/(?:\^(?!\^)([^\^ ]*)\^(?!\^))/', $excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'sup', 'text' => $matches[1], 'function' => 'lineElements', ], ]; } } // Inline Subscript protected function inlineSubscript($excerpt) { if (\preg_match('/(?:~(?!~)([^~ ]*)~(?!~))/', $excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'name' => 'sub', 'text' => $matches[1], 'function' => 'lineElements', ], ]; } } // Inline typographer protected function inlineTypographer($excerpt) { $substitutions = [ '/\(c\)/i' => '©', '/\(r\)/i' => '®', '/\(tm\)/i' => '™', '/\(p\)/i' => '¶', '/\+-/i' => '±', '/\.{4,}|\.{2}/i' => '...', '/\!\.{3,}/i' => '!..', '/\?\.{3,}/i' => '?..', ]; if (\preg_match('/\+-|\(p\)|\(tm\)|\(r\)|\(c\)|\.{2,}|\!\.{3,}|\?\.{3,}/i', $excerpt['text'], $matches)) { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'rawHtml' => \preg_replace(\array_keys($substitutions), \array_values($substitutions), $matches[0]), ], ]; } } // Inline Smartypants protected function inlineSmartypants($excerpt) { // Substitutions $backtickDoublequoteOpen = $this->options['smarty']['substitutions']['left-double-quote'] ?? '“'; $backtickDoublequoteClose = $this->options['smarty']['substitutions']['right-double-quote'] ?? '”'; $smartDoublequoteOpen = $this->options['smarty']['substitutions']['left-double-quote'] ?? '“'; $smartDoublequoteClose = $this->options['smarty']['substitutions']['right-double-quote'] ?? '”'; $smartSinglequoteOpen = $this->options['smarty']['substitutions']['left-single-quote'] ?? '‘'; $smartSinglequoteClose = $this->options['smarty']['substitutions']['right-single-quote'] ?? '’'; $leftAngleQuote = $this->options['smarty']['substitutions']['left-angle-quote'] ?? '«'; $rightAngleQuote = $this->options['smarty']['substitutions']['right-angle-quote'] ?? '»'; if (\preg_match('/(``)(?!\s)([^"\'`]{1,})(\'\')|(\")(?!\s)([^\"]{1,})(\")|(\')(?!\s)([^\']{1,})(\')|(<{2})(?!\s)([^<>]{1,})(>{2})|(\.{3})|(-{3})|(-{2})/i', $excerpt['text'], $matches)) { $matches = \array_values(\array_filter($matches)); // Smart backticks $smartBackticks = $this->options['smarty']['smart_backticks'] ?? false; if ($smartBackticks && $matches[1] === '``') { $length = \strlen(\trim($excerpt['before'])); if ($length > 0) { return; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \html_entity_decode($backtickDoublequoteOpen).$matches[2].\html_entity_decode($backtickDoublequoteClose), ], ]; } // Smart quotes $smartQuotes = $this->options['smarty']['smart_quotes'] ?? true; if ($smartQuotes) { if ($matches[1] === "'") { $length = \strlen(\trim($excerpt['before'])); if ($length > 0) { return; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \html_entity_decode($smartSinglequoteOpen).$matches[2].\html_entity_decode($smartSinglequoteClose), ], ]; } if ($matches[1] === '"') { $length = \strlen(\trim($excerpt['before'])); if ($length > 0) { return; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \html_entity_decode($smartDoublequoteOpen).$matches[2].\html_entity_decode($smartDoublequoteClose), ], ]; } } // Smart angled quotes $smartAngledQuotes = $this->options['smarty']['smart_angled_quotes'] ?? true; if ($smartAngledQuotes && $matches[1] === '<<') { $length = \strlen(\trim($excerpt['before'])); if ($length > 0) { return; } return [ 'extent' => \strlen($matches[0]), 'element' => [ 'text' => \html_entity_decode($leftAngleQuote).$matches[2].\html_entity_decode($rightAngleQuote), ], ]; } // Smart dashes $smartDashes = $this->options['smarty']['smart_dashes'] ?? true; if ($smartDashes) { if ($matches[1] === '---') { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'rawHtml' => $this->options['smarty']['substitutions']['mdash'] ?? '—', ], ]; } if ($matches[1] === '--') { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'rawHtml' => $this->options['smarty']['substitutions']['ndash'] ?? '–', ], ]; } } // Smart ellipses $smartEllipses = $this->options['smarty']['smart_ellipses'] ?? true; if ($smartEllipses && $matches[1] === '...') { return [ 'extent' => \strlen($matches[0]), 'element' => [ 'rawHtml' => $this->options['smarty']['substitutions']['ellipses'] ?? '…', ], ]; } } } // Inline Math protected function inlineMath($excerpt) { $matchSingleDollar = $this->options['math']['single_dollar'] ?? false; // Inline Matches if ($matchSingleDollar) { // Match single dollar - experimental if (\preg_match('/^(? \strlen($mathMatch), 'element' => [ 'text' => $mathMatch, ], ]; } } protected function inlineEscapeSequence($excerpt) { $element = [ 'element' => [ 'rawHtml' => $excerpt['text'][1], ], 'extent' => 2, ]; $state = $this->options['math'] ?? false; if ($state) { if (isset($excerpt['text'][1]) && \in_array($excerpt['text'][1], $this->specialCharacters) && !\preg_match('/^(?specialCharacters)) { return $element; } } /** * ------------------------------------------------------------------------ * Blocks. * ------------------------------------------------------------------------ */ protected function blockFootnote($line, array $_ = null) { $state = $this->options['footnotes'] ?? true; if ($state) { return $this->blockFootnoteBase($line); } } protected function blockDefinitionList($line, $block) { $state = $this->options['definition_lists'] ?? true; if ($state) { return $this->blockDefinitionListBase($line, $block); } } protected function blockCode($line, $block = null) { $codeBlock = $this->options['code']['blocks'] ?? true; $codeMain = $this->options['code'] ?? true; if ($codeBlock === true && $codeMain === true) { return $this->blockCodeBase($line, $block); } } protected function blockComment($line, array $_ = null) { $state = $this->options['comments'] ?? true; if ($state) { return $this->blockCommentBase($line); } } protected function blockHeader($line, array $_ = null) { $state = $this->options['headings'] ?? true; if (!$state) { return; } $block = $this->blockHeaderBase($line); if (!empty($block)) { // 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; } // Checks if auto generated anchors is allowed $autoAnchors = $this->options['headings']['auto_anchors'] ?? true; if ($autoAnchors) { // Get the anchor of the heading to link from the ToC list $id = $block['element']['attributes']['id'] ?? $this->createAnchorID($text); } else { // Get the anchor of the heading to link from the ToC list $id = $block['element']['attributes']['id'] ?? 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; } } protected function blockList($line, array $CurrentBlock = null) { $state = $this->options['lists'] ?? true; if ($state) { return $this->blockListBase($line, $CurrentBlock); } } protected function blockQuote($line, array $_ = null) { $state = $this->options['qoutes'] ?? true; if ($state) { return $this->blockQuoteBase($line); } } protected function blockRule($line, array $_ = null) { $state = $this->options['thematic_breaks'] ?? true; if ($state) { return $this->blockRuleBase($line); } } protected function blockSetextHeader($line, $block = null) { $state = $this->options['headings'] ?? true; if (!$state) { return; } $block = $this->blockSetextHeaderBase($line, $block); if (!empty($block)) { // 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; } // Checks if auto generated anchors is allowed $autoAnchors = $this->options['headings']['auto_anchors'] ?? true; if ($autoAnchors) { // Get the anchor of the heading to link from the ToC list $id = $block['element']['attributes']['id'] ?? $this->createAnchorID($text); } else { // Get the anchor of the heading to link from the ToC list $id = $block['element']['attributes']['id'] ?? 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; } } protected function blockMarkup($line, array $_ = null) { $state = $this->options['markup'] ?? true; if ($state) { return $this->blockMarkupBase($line); } } protected function blockReference($line, array $_ = null) { $state = $this->options['references'] ?? true; if ($state) { return $this->blockReferenceBase($line); } } protected function blockTable($line, $block = null) { $state = $this->options['tables'] ?? true; if ($state) { return $this->blockTableBase($line, $block); } } protected function blockAbbreviation($line, array $_ = null) { $allowCustomAbbr = $this->options['abbreviations']['allow_custom_abbr'] ?? true; $state = $this->options['abbreviations'] ?? true; if ($state) { if (isset($this->options['abbreviations']['predefine'])) { foreach ($this->options['abbreviations']['predefine'] as $abbreviations => $description) { $this->DefinitionData['Abbreviation'][$abbreviations] = $description; } } if ($allowCustomAbbr == true) { return $this->blockAbbreviationBase($line); } return; } } // Block Math protected function blockMath($line, array $_ = null) { $block = [ 'element' => [ 'text' => '', ], ]; if (\preg_match('/^(?options['code']['blocks'] ?? true; $codeMain = $this->options['code'] ?? true; if ($codeBlock === false || $codeMain === false) { return; } $block = $this->blockFencedCodeBase($line); $marker = $line['text'][0]; $openerLength = \strspn($line['text'], $marker); $language = \trim( \preg_replace('/^`{3}([^\s]+)(.+)?/s', '$1', $line['text']) ); $state = $this->options['diagrams'] ?? true; if ($state) { // Mermaid.js https://mermaidjs.github.io if (\strtolower($language) == 'mermaid') { $element = [ 'text' => '', ]; return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'element' => $element, 'name' => 'div', 'attributes' => [ 'class' => 'mermaid', ], ], ]; } // Chart.js https://www.chartjs.org/ if (\strtolower($language) == 'chart') { $element = [ 'text' => '', ]; return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'element' => $element, 'name' => 'canvas', 'attributes' => [ 'class' => 'chartjs', ], ], ]; } } return $block; } // Parsedown Tablespan from @KENNYSOFT protected function blockTableComplete(array $block) { $state = $this->options['tables']['tablespan'] ?? false; if ($state === false) { return $block; } if (!isset($block)) { return null; } $HeaderElements = &$block['element']['elements'][0]['elements'][0]['elements']; for ($index = \count($HeaderElements) - 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 = \count($HeaderElements) - 1; $index >= 0; --$index) { if (isset($HeaderElements[$index]['merged'])) { \array_splice($HeaderElements, $index, 1); } } $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; } } } foreach ($rows as $rowNo => &$row) { $elements = &$row['elements']; foreach ($elements as $index => &$element) { $rowspan = 1; if (isset($element['merged'])) { continue; } while ($rowNo + $rowspan < \count($rows) && $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'])) { \array_splice($elements, $index, 1); } } } return $block; } /* * Checkbox * ------------------------------------------------------------------------- */ protected function blockCheckbox($line) { $text = \trim($line['text']); $beginLine = \substr($text, 0, 4); if ($beginLine === '[ ] ') { return [ 'handler' => 'checkboxUnchecked', 'text' => \substr(\trim($text), 4), ]; } if ($beginLine === '[x] ') { return [ 'handler' => 'checkboxChecked', 'text' => \substr(\trim($text), 4), ]; } } protected function blockCheckboxContinue(array $block) : void { // This is here because Parsedown require it. } protected function blockCheckboxComplete(array $block) { $block['element'] = [ 'rawHtml' => $this->{$block['handler']}($block['text']), 'allowRawHtmlInSafeMode' => true, ]; return $block; } protected function checkboxUnchecked($text) : string { if ($this->markupEscaped || $this->safeMode) { $text = self::escape($text); } return ' '.$this->format($text); } protected function checkboxChecked($text) : string { if ($this->markupEscaped || $this->safeMode) { $text = self::escape($text); } return ' '.$this->format($text); } /** * ------------------------------------------------------------------------ * Helpers. * ------------------------------------------------------------------------. */ /** * Formats the checkbox label without double escaping. */ protected function format($text) { // backup settings $markupEscaped = $this->markupEscaped; $safeMode = $this->safeMode; // disable rules to prevent double escaping. $this->setMarkupEscaped(false); $this->setSafeMode(false); // format line $text = $this->line($text); // reset old values $this->setMarkupEscaped($markupEscaped); $this->setSafeMode($safeMode); return $text; } protected function parseAttributeData($attributeString) { $state = $this->options['special_attributes'] ?? true; if ($state) { return $this->parseAttributeDataBase($attributeString); } return []; } /** * 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_]]". Unless it will be parsed as: * "

[[TOC]]

" */ protected function encodeTagToHash($text) { $salt = $this->getSalt(); $tagOrigin = $this->getTagToC(); 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_]]". Unless it will be parsed as: * "

[[TOC]]

" */ protected function decodeTagFromHash($text) { $salt = $this->getSalt(); $tagOrigin = $this->getTagToC(); $tagHashed = \hash('sha256', $salt.$tagOrigin); if (\strpos($text, $tagHashed) === false) { return $text; } return \str_replace($tagHashed, $tagOrigin, $text); } /** * Unique string to use as a salt value. */ protected function getSalt() { static $salt; if (isset($salt)) { return $salt; } $salt = \hash('md5', (string) \time()); return $salt; } /** * Gets the markdown tag for ToC. */ protected function getTagToC() { return $this->options['toc']['set_toc_tag'] ?? '[toc]'; } /** * Gets the ID attribute of the ToC for HTML tags. */ protected function getIdAttributeToC() { if (!empty($this->idToc)) { return $this->idToc; } return self::ID_ATTRIBUTE_DEFAULT; } /** * Generates an anchor text that are link-able even if the heading is not in * ASCII. */ protected function createAnchorID($str) : string { $optionUrlEncode = $this->options['toc']['urlencode'] ?? false; if ($optionUrlEncode) { // Check AnchorID is unique $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 $optionTransliterate = $this->options['toc']['transliterate'] ?? false; if ($optionTransliterate) { $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); } /** * Get only the text from a markdown string. * It parses to HTML once then trims the tags to get the text. */ protected function fetchText($text) : string { return \trim(\strip_tags($this->line($text))); } /** * Set/stores the heading block to ToC list in a string and array format. */ protected function setContentsList(array $Content) : void { // Stores as an array $this->setContentsListAsArray($Content); // Stores as string in markdown list format. $this->setContentsListAsString($Content); } /** * Sets/stores the heading block info as an array. */ protected function setContentsListAsArray(array $Content) : void { $this->contentsListArray[] = $Content; } /** * Sets/stores the heading block info as a list in markdown format. */ protected function setContentsListAsString(array $Content) : void { $text = $this->fetchText($Content['text']); $id = $Content['id']; $level = (int) \trim($Content['level'], 'h'); $link = "[{$text}](#{$id})"; if ($this->firstHeadLevel === 0) { $this->firstHeadLevel = $level; } $cutIndent = $this->firstHeadLevel - 1; $level = $cutIndent > $level ? 1 : $level - $cutIndent; $indent = \str_repeat(' ', $level); // Stores in markdown list format as below: // - [Header1](#Header1) // - [Header2-1](#Header2-1) // - [Header3](#Header3) // - [Header2-2](#Header2-2) // ... $this->contentsListString .= "{$indent}- {$link}".\PHP_EOL; } /** * Collect and count anchors in use to prevent duplicated ids. Return string * with incremental, numeric suffix. Also init optional blacklist of ids. */ protected function incrementAnchorId($str) { // add blacklist to list of used anchors if (!$this->isBlacklistInitialized) { $this->initBlacklist(); } $this->anchorDuplicates[$str] = isset($this->anchorDuplicates[$str]) ? ++$this->anchorDuplicates[$str] : 0; $newStr = $str; if ($count = $this->anchorDuplicates[$str]) { $newStr .= "-{$count}"; // increment until conversion doesn't produce new duplicates anymore if (isset($this->anchorDuplicates[$newStr])) { $newStr = $this->incrementAnchorId($str); } else { $this->anchorDuplicates[$newStr] = 0; } } return $newStr; } /** * Add blacklisted ids to anchor list. */ 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) { if (\is_string($v)) { $this->anchorDuplicates[$v] = 0; } } } $this->isBlacklistInitialized = true; } protected function lineElements($text, $nonNestables = []) { $Elements = []; $nonNestables = ( empty($nonNestables) ? [] : \array_combine($nonNestables, $nonNestables) ); // $excerpt is based on the first occurrence of a marker while ($excerpt = \strpbrk($text, $this->inlineMarkerList)) { $marker = $excerpt[0]; $markerPosition = \strlen($text) - \strlen($excerpt); // Get the first char before the marker $beforeMarkerPosition = $markerPosition - 1; $charBeforeMarker = $beforeMarkerPosition >= 0 ? $text[$markerPosition - 1] : ''; $Excerpt = ['text' => $excerpt, '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 (!isset($Inline)) { 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; } private function pregReplaceAssoc(array $replace, $subject) { return \preg_replace(\array_keys($replace), \array_values($replace), $subject); } # # Blocks # # # Abbreviation protected function blockAbbreviationBase($Line) { if (\preg_match('/^\*\[(.+?)\]:[ ]*(.+?)[ ]*$/', $Line['text'], $matches)) { $this->DefinitionData['Abbreviation'][$matches[1]] = $matches[2]; return [ 'hidden' => true, ]; } } # # Footnote protected function blockFootnoteBase($Line) { if (\preg_match('/^\[\^(.+?)\]:[ ]?(.*)$/', $Line['text'], $matches)) { return [ 'label' => $matches[1], 'text' => $matches[2], 'hidden' => true, ]; } } protected function blockFootnoteContinue($Line, $Block) { if ($Line['text'][0] === '[' && \preg_match('/^\[\^(.+?)\]:/', $Line['text'])) { return; } if (isset($Block['interrupted'])) { if ($Line['indent'] >= 4) { $Block['text'] .= "\n\n" . $Line['text']; return $Block; } } else { $Block['text'] .= "\n" . $Line['text']; return $Block; } } protected function blockFootnoteComplete($Block) { $this->DefinitionData['Footnote'][$Block['label']] = [ 'text' => $Block['text'], 'count' => null, 'number' => null, ]; return $Block; } # # Definition List protected function blockDefinitionListBase($Line, $Block) { if (!isset($Block) || $Block['type'] !== 'Paragraph') { return; } $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); } protected function blockDefinitionListContinue($Line, array $Block) { if ($Line['text'][0] === ':') { return $this->addDdElement($Line, $Block); } else { if (isset($Block['interrupted']) && $Line['indent'] === 0) { return; } 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; } } # # Header protected function blockHeaderBase($Line) { $Block = $this->blockHeaderParent($Line); if ($Block !== null && \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, $matches[0][1]); } return $Block; } # # Markup protected function blockMarkupBase($Line) { if ($this->markupEscaped || $this->safeMode) { return; } if (\preg_match('/^<(\w[\w-]*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches)) { $element = \strtolower($matches[1]); if (\in_array($element, $this->textLevelElements)) { return; } $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; } if (\preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder)) { $Block['closed'] = true; } } return $Block; } } protected function blockMarkupContinue($Line, array $Block) { if (isset($Block['closed'])) { return; } 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; } protected function blockMarkupComplete($Block) { if (!isset($Block['void'])) { $Block['element']['rawHtml'] = $this->processTag($Block['element']['rawHtml']); } return $Block; } # # Setext protected function blockSetextHeaderBase($Line, array $Block = null) { $Block = $this->blockSetextHeaderParent($Line, $Block); if ($Block !== null && \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, $matches[0][1]); } return $Block; } # # Inline Elements # # # Footnote Marker protected function inlineFootnoteMarker($Excerpt) { if (\preg_match('/^\[\^(.+?)\]/', $Excerpt['text'], $matches)) { $name = $matches[1]; if (!isset($this->DefinitionData['Footnote'][$name])) { return; } ++$this->DefinitionData['Footnote'][$name]['count']; if (!isset($this->DefinitionData['Footnote'][$name]['number'])) { $this->DefinitionData['Footnote'][$name]['number'] = ++ $this->footnoteCount; # ยป & } $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'], ], ]; return [ 'extent' => \strlen($matches[0]), 'element' => $Element, ]; } } private $footnoteCount = 0; # # ~ # private $currentAbreviation; private $currentMeaning; protected function insertAbreviation(array $Element) { if (isset($Element['text'])) { $Element['elements'] = self::pregReplaceElements( '/\b'.\preg_quote($this->currentAbreviation, '/').'\b/', [ [ 'name' => 'abbr', 'attributes' => [ 'title' => $this->currentMeaning, ], 'text' => $this->currentAbreviation, ], ], $Element['text'] ); unset($Element['text']); } return $Element; } protected function inlineText($text) { $Inline = $this->inlineTextParent($text); if (isset($this->DefinitionData['Abbreviation'])) { foreach ($this->DefinitionData['Abbreviation'] as $abbreviation => $meaning) { $this->currentAbreviation = $abbreviation; $this->currentMeaning = $meaning; $Inline['element'] = $this->elementApplyRecursiveDepthFirst( [$this, 'insertAbreviation'], $Inline['element'] ); } } return $Inline; } # # Util Methods # protected function addDdElement(array $Line, array $Block) { $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; } protected function buildFootnoteElement() { $Element = [ 'name' => 'div', 'attributes' => ['class' => 'footnotes'], 'elements' => [ ['name' => 'hr'], [ 'name' => 'ol', 'elements' => [], ], ], ]; \uasort($this->DefinitionData['Footnote'], 'self::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; } # ~ protected function parseAttributeDataBase($attributeString) { $Data = []; $attributes = \preg_split('/[ ]+/', $attributeString, - 1, \PREG_SPLIT_NO_EMPTY); foreach ($attributes as $attribute) { if ($attribute[0] === '#') { $Data['id'] = \substr($attribute, 1); } else # "." { $classes []= \substr($attribute, 1); } } if (isset($classes)) { $Data['class'] = \implode(' ', $classes); } return $Data; } # ~ protected function processTag($elementMarkup) # recursive { # http://stackoverflow.com/q/1148928/200145 \libxml_use_internal_errors(true); $DOMDocument = new \DOMDocument(); # http://stackoverflow.com/q/11309194/200145 $elementMarkup = \mb_convert_encoding($elementMarkup, 'HTML-ENTITIES', 'UTF-8'); # http://stackoverflow.com/q/4879946/200145 $DOMDocument->loadHTML($elementMarkup); $DOMDocument->removeChild($DOMDocument->doctype); $DOMDocument->replaceChild($DOMDocument->firstChild->firstChild->firstChild, $DOMDocument->firstChild); $elementText = ''; if ($DOMDocument->documentElement->getAttribute('markdown') === '1') { foreach ($DOMDocument->documentElement->childNodes as $Node) { $elementText .= $DOMDocument->saveHTML($Node); } $DOMDocument->documentElement->removeAttribute('markdown'); $elementText = "\n".$this->text($elementText)."\n"; } else { foreach ($DOMDocument->documentElement->childNodes as $Node) { $nodeMarkup = $DOMDocument->saveHTML($Node); if ($Node instanceof \DOMElement && ! \in_array($Node->nodeName, $this->textLevelElements)) { $elementText .= $this->processTag($nodeMarkup); } else { $elementText .= $nodeMarkup; } } } # because we don't want for markup to get encoded $DOMDocument->documentElement->nodeValue = 'placeholder\x1A'; $markup = $DOMDocument->saveHTML($DOMDocument->documentElement); return \str_replace('placeholder\x1A', $elementText, $markup); } # ~ protected function sortFootnotes($A, $B) # callback { return $A['number'] - $B['number']; } # # Fields # protected $regexAttribute = '(?:[#.][-\w]+[ ]*)'; protected function textElements($text) { # 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); } # # Setters # public function setBreaksEnabled($breaksEnabled) { $this->breaksEnabled = $breaksEnabled; return $this; } protected $breaksEnabled; public function setMarkupEscaped($markupEscaped) { $this->markupEscaped = $markupEscaped; return $this; } protected $markupEscaped; public function setUrlsLinked($urlsLinked) { $this->urlsLinked = $urlsLinked; return $this; } protected $urlsLinked = true; public function setSafeMode($safeMode) { $this->safeMode = (bool) $safeMode; return $this; } protected $safeMode; public function setStrictMode($strictMode) { $this->strictMode = (bool) $strictMode; return $this; } protected $strictMode; protected $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:', ]; # # Lines # protected $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'], '~' => ['FencedCode'], ]; # ~ protected $unmarkedBlockTypes = [ 'Code', ]; # # Blocks # protected function lines(array $lines) { return $this->elements($this->linesElements($lines)); } protected function linesElements(array $lines) { $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 ($this->isBlockCompletable($CurrentBlock['type'])) { $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 ($this->isBlockContinuable($blockType)) { $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 = $this->paragraph($Line); $CurrentBlock['identified'] = true; } } # ~ if (isset($CurrentBlock['continuable']) && $this->isBlockCompletable($CurrentBlock['type'])) { $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; $CurrentBlock = $this->{$methodName}($CurrentBlock); } # ~ if (isset($CurrentBlock)) { $Elements[] = $this->extractElement($CurrentBlock); } # ~ return $Elements; } protected function extractElement(array $Component) { if (!isset($Component['element'])) { if (isset($Component['markup'])) { $Component['element'] = ['rawHtml' => $Component['markup']]; } elseif (isset($Component['hidden'])) { $Component['element'] = []; } } return $Component['element']; } protected function isBlockContinuable($Type) : bool { return \method_exists($this, 'block' . $Type . 'Continue'); } protected function isBlockCompletable($Type) : bool { return \method_exists($this, 'block' . $Type . 'Complete'); } # # Code protected function blockCodeBase($Line, $Block = null) { if (isset($Block) && $Block['type'] === 'Paragraph' && !isset($Block['interrupted'])) { return; } if ($Line['indent'] >= 4) { $text = \substr($Line['body'], 4); return [ 'element' => [ 'name' => 'pre', 'element' => [ 'name' => 'code', 'text' => $text, ], ], ]; } } protected function blockCodeContinue($Line, $Block) { if ($Line['indent'] >= 4) { 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; } } protected function blockCodeComplete($Block) { return $Block; } # # Comment protected function blockCommentBase($Line) { if ($this->markupEscaped || $this->safeMode) { return; } if (\strpos($Line['text'], '') !== false) { $Block['closed'] = true; } return $Block; } } protected function blockCommentContinue($Line, array $Block) { if (isset($Block['closed'])) { return; } $Block['element']['rawHtml'] .= "\n" . $Line['body']; if (\strpos($Line['text'], '-->') !== false) { $Block['closed'] = true; } return $Block; } # # Fenced Code protected function blockFencedCodeBase($Line) { $marker = $Line['text'][0]; $openerLength = \strspn($Line['text'], $marker); if ($openerLength < 3) { return; } $infostring = \trim(\substr($Line['text'], $openerLength), "\t "); if (\strpos($infostring, '`') !== false) { return; } $Element = [ 'name' => 'code', 'text' => '', ]; if ($infostring !== '') { /** * https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#classes * Every HTML element may have a class attribute specified. * The attribute, if specified, must have a value that is a set * of space-separated tokens representing the various classes * that the element belongs to. * [...] * The space characters, for the purposes of this specification, * are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), * U+000A LINE FEED (LF), U+000C FORM FEED (FF), and * U+000D CARRIAGE RETURN (CR). */ $language = \substr($infostring, 0, \strcspn($infostring, " \t\n\f\r")); $Element['attributes'] = ['class' => "language-{$language}"]; } return [ 'char' => $marker, 'openerLength' => $openerLength, 'element' => [ 'name' => 'pre', 'element' => $Element, ], ]; } protected function blockFencedCodeContinue($Line, $Block) { if (isset($Block['complete'])) { return; } 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; } protected function blockFencedCodeComplete($Block) { return $Block; } # # Header protected function blockHeaderParent($Line) { $level = \strspn($Line['text'], '#'); if ($level > 6) { return; } $text = \trim($Line['text'], '#'); if ($this->strictMode && isset($text[0]) && $text[0] !== ' ') { return; } $text = \trim($text, ' '); return [ 'element' => [ 'name' => 'h' . $level, 'handler' => [ 'function' => 'lineElements', 'argument' => $text, 'destination' => 'elements', ], ], ]; } # # List protected function blockListBase($Line, array $CurrentBlock = null) { list($name, $pattern) = $Line['text'][0] <= '-' ? ['ul', '[*+-]'] : ['ol', '[0-9]{1,9}+[.\)]']; if (\preg_match('/^('.$pattern.'([ ]++|$))(.*+)/', $Line['text'], $matches)) { $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); $Block = [ 'indent' => $Line['indent'], 'pattern' => $pattern, 'data' => [ 'type' => $name, 'marker' => $matches[1], 'markerType' => ($name === 'ul' ? $markerWithoutWhitespace : \substr($markerWithoutWhitespace, -1)), ], 'element' => [ 'name' => $name, 'elements' => [], ], ]; $Block['data']['markerTypeRegex'] = \preg_quote($Block['data']['markerType'], '/'); if ($name === 'ol') { $listStart = \ltrim(\strstr($matches[1], $Block['data']['markerType'], true), '0') ?: '0'; if ($listStart !== '1') { if ( isset($CurrentBlock) && $CurrentBlock['type'] === 'Paragraph' && !isset($CurrentBlock['interrupted']) ) { return; } $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; } } protected function blockListContinue($Line, array $Block) { 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 = isset($matches[1]) ? $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; } } protected function blockListComplete(array $Block) { if (isset($Block['loose'])) { foreach ($Block['element']['elements'] as &$li) { if (\end($li['handler']['argument']) !== '') { $li['handler']['argument'] []= ''; } } } return $Block; } # # Quote protected function blockQuoteBase($Line) { if (\preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) { return [ 'element' => [ 'name' => 'blockquote', 'handler' => [ 'function' => 'linesElements', 'argument' => (array) $matches[1], 'destination' => 'elements', ], ], ]; } } protected function blockQuoteContinue($Line, array $Block) { if (isset($Block['interrupted'])) { return; } 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; } } # # Rule protected function blockRuleBase($Line) { $marker = $Line['text'][0]; if (\substr_count($Line['text'], $marker) >= 3 && \rtrim($Line['text'], " {$marker}") === '') { return [ 'element' => [ 'name' => 'hr', ], ]; } } # # Setext protected function blockSetextHeaderParent($Line, array $Block = null) { if (!isset($Block) || $Block['type'] !== 'Paragraph' || isset($Block['interrupted'])) { return; } if ($Line['indent'] < 4 && \rtrim(\rtrim($Line['text'], ' '), $Line['text'][0]) === '') { $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; return $Block; } } # # Reference protected function blockReferenceBase($Line) { if (\strpos($Line['text'], ']') !== false && \preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $Line['text'], $matches) ) { $id = \strtolower($matches[1]); $Data = [ 'url' => UriFactory::build($matches[2]), 'title' => isset($matches[3]) ? $matches[3] : null, ]; $this->DefinitionData['Reference'][$id] = $Data; return [ 'element' => [], ]; } } # # Table protected function blockTableBase($Line, array $Block = null) { if (!isset($Block) || $Block['type'] !== 'Paragraph' || isset($Block['interrupted'])) { return; } if ( \strpos($Block['element']['handler']['argument'], '|') === false && \strpos($Line['text'], '|') === false && \strpos($Line['text'], ':') === false || \strpos($Block['element']['handler']['argument'], "\n") !== false ) { return; } if (\rtrim($Line['text'], ' -:|') !== '') { return; } $alignments = []; $divider = $Line['text']; $divider = \trim($divider); $divider = \trim($divider, '|'); $dividerCells = \explode('|', $divider); foreach ($dividerCells as $dividerCell) { $dividerCell = \trim($dividerCell); if ($dividerCell === '') { return; } $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; } 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; } protected function blockTableContinue($Line, array $Block) { if (isset($Block['interrupted'])) { return; } if (\count($Block['alignments']) === 1 || $Line['text'][0] === '|' || \strpos($Line['text'], '|')) { $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; } } # # ~ # protected function paragraph($Line) { return [ 'type' => 'Paragraph', 'element' => [ 'name' => 'p', 'handler' => [ 'function' => 'lineElements', 'argument' => $Line['text'], 'destination' => 'elements', ], ], ]; } protected function paragraphContinue($Line, array $Block) { if (isset($Block['interrupted'])) { return; } $Block['element']['handler']['argument'] .= "\n".$Line['text']; return $Block; } # # Inline Elements # protected $InlineTypes = [ '!' => ['Image'], '&' => ['SpecialCharacter'], '*' => ['Emphasis'], ':' => ['Url'], '<' => ['UrlTag', 'EmailTag', 'Markup'], '[' => ['FootnoteMarker', 'Link'], '_' => ['Emphasis'], '`' => ['Code'], '~' => ['Strikethrough'], '\\' => ['EscapeSequence'], ]; # ~ protected $inlineMarkerList = '!*_&[:<`~\\'; # # ~ # public function line($text, $nonNestables = []) { return $this->elements($this->lineElements($text, $nonNestables)); } /* protected function lineElements($text, $nonNestables = array()) { # standardize line breaks $text = str_replace(array("\r\n", "\r"), "\n", $text); $Elements = array(); $nonNestables = (empty($nonNestables) ? array() : array_combine($nonNestables, $nonNestables) ); # $excerpt is based on the first occurrence of a marker while ($excerpt = strpbrk($text, $this->inlineMarkerList)) { $marker = $excerpt[0]; $markerPosition = strlen($text) - strlen($excerpt); $Excerpt = array('text' => $excerpt, 'context' => $text); 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 ( !isset($Inline)) { continue; } # makes sure that the inline belongs to "our" marker if (isset($Inline['position']) and $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; } */ # # ~ # protected function inlineTextParent($text) { $Inline = [ 'extent' => \strlen($text), 'element' => [], ]; $Inline['element']['elements'] = self::pregReplaceElements( $this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/', [ ['name' => 'br'], ['text' => "\n"], ], $text ); return $Inline; } protected function inlineLinkParent($Excerpt) { $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; } if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches)) { $Element['attributes']['href'] = $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; } $Definition = $this->DefinitionData['Reference'][$definition]; $Element['attributes']['href'] = $Definition['url']; $Element['attributes']['title'] = $Definition['title']; } return [ 'extent' => $extent, 'element' => $Element, ]; } protected function inlineSpecialCharacter($Excerpt) { 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]), ]; } } # ~ protected function unmarkedText($text) { $Inline = $this->inlineText($text); return $this->element($Inline['element']); } # # Handlers # protected function handle(array $Element) { if (isset($Element['handler'])) { 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; } protected function handleElementRecursive(array $Element) { return $this->elementApplyRecursive([$this, 'handle'], $Element); } protected function handleElementsRecursive(array $Elements) { return $this->elementsApplyRecursive([$this, 'handle'], $Elements); } protected function elementApplyRecursive($closure, array $Element) { $Element = $closure($Element); if (isset($Element['elements'])) { $Element['elements'] = $this->elementsApplyRecursive($closure, $Element['elements']); } elseif (isset($Element['element'])) { $Element['element'] = $this->elementApplyRecursive($closure, $Element['element']); } return $Element; } protected function elementApplyRecursiveDepthFirst($closure, array $Element) { if (isset($Element['elements'])) { $Element['elements'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['elements']); } elseif (isset($Element['element'])) { $Element['element'] = $this->elementsApplyRecursiveDepthFirst($closure, $Element['element']); } return $closure($Element); } protected function elementsApplyRecursive($closure, array $Elements) { foreach ($Elements as &$Element) { $Element = $this->elementApplyRecursive($closure, $Element); } return $Elements; } protected function elementsApplyRecursiveDepthFirst($closure, array $Elements) { foreach ($Elements as &$Element) { $Element = $this->elementApplyRecursiveDepthFirst($closure, $Element); } return $Elements; } protected function element(array $Element) { if ($this->safeMode) { $Element = $this->sanitiseElement($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}=\"".self::escape($value).'"'; } } } $permitRawHtml = false; if (isset($Element['text'])) { $text = $Element['text']; } // very strongly consider an alternative if you're writing an // extension elseif (isset($Element['rawHtml'])) { $text = $Element['rawHtml']; $allowRawHtmlInSafeMode = isset($Element['allowRawHtmlInSafeMode']) && $Element['allowRawHtmlInSafeMode']; $permitRawHtml = !$this->safeMode || $allowRawHtmlInSafeMode; } $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 .= self::escape((string) $text, true); } else { $markup .= $text; } $markup .= $hasName ? '' : ''; } elseif ($hasName) { $markup .= ' />'; } return $markup; } 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" : ''); } # ~ protected function li($lines) { $Elements = $this->linesElements($lines); if (! \in_array('', $lines) && isset($Elements[0], $Elements[0]['name']) && $Elements[0]['name'] === 'p' ) { unset($Elements[0]['name']); } return $Elements; } # # AST Convenience # /** * Replace occurrences $regexp with $Elements in $text. Return an array of * elements representing the replacement. */ protected static function pregReplaceElements($regexp, $Elements, $text) { $newElements = []; while (\preg_match($regexp, $text, $matches, \PREG_OFFSET_CAPTURE)) { $offset = $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; } # # Deprecated Methods # public static function parse($text) { $parsedown = new self(); return $parsedown->text($text); } protected function sanitiseElement(array $Element) { 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 => $val) { # filter out badly parsed attribute if (! \preg_match($goodAttribute, $att)) { unset($Element['attributes'][$att]); } # dump onevent attribute elseif (self::striAtStart($att, 'on')) { unset($Element['attributes'][$att]); } } } return $Element; } protected function filterUnsafeUrlInAttribute(array $Element, $attribute) { foreach ($this->safeLinksWhitelist as $scheme) { if (self::striAtStart($Element['attributes'][$attribute], $scheme)) { return $Element; } } $Element['attributes'][$attribute] = \str_replace(':', '%3A', $Element['attributes'][$attribute]); return $Element; } # # Static Methods # protected static function escape(string $text, bool $allowQuotes = false) : string { return \htmlspecialchars($text, $allowQuotes ? \ENT_NOQUOTES : \ENT_QUOTES, 'UTF-8'); } protected static function striAtStart($string, $needle) { $len = \strlen($needle); if ($len > \strlen($string)) { return false; } else { return \strtolower(\substr($string, 0, $len)) === \strtolower($needle); } } public static function instance($name = 'default') { if (isset(self::$instances[$name])) { return self::$instances[$name]; } $instance = new static(); self::$instances[$name] = $instance; return $instance; } private static $instances = []; # # Fields # protected $DefinitionData; public const ID_ATTRIBUTE_DEFAULT = 'toc'; protected $tagToc = '[toc]'; protected $contentsListArray = []; protected $contentsListString = ''; protected $firstHeadLevel = 0; protected $isBlacklistInitialized = false; protected $anchorDuplicates = []; # # Read-Only protected $specialCharacters = [ '\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', '?', '"', "'", '<', ]; protected $StrongRegex = [ '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', ]; protected $UnderlineRegex = [ '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us', ]; protected $EmRegex = [ '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', ]; protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*+(?:\s*+=\s*+(?:[^"\'=<>`\s]+|"[^"]*+"|\'[^\']*+\'))?+'; protected $voidElements = [ 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', ]; protected $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', ]; }