From 53bb9a64678c65c1c37c9992e044c0283729208e Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Sun, 20 Jan 2019 02:37:21 +0000 Subject: [PATCH] Implement Url --- src/Components/Inlines/Url.php | 74 ++++++++++++++++++++++++++++++++++ src/Parsedown.php | 27 ------------- 2 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 src/Components/Inlines/Url.php diff --git a/src/Components/Inlines/Url.php b/src/Components/Inlines/Url.php new file mode 100644 index 0000000..bc02191 --- /dev/null +++ b/src/Components/Inlines/Url.php @@ -0,0 +1,74 @@ +url = $url; + $this->width = \strlen($url); + $this->position = $position; + } + + /** + * @param Excerpt $Excerpt + * @param State $State + * @return static|null + */ + public static function build(Excerpt $Excerpt, State $State) + { + $text = $Excerpt->text(); + + if (\strlen($text) < 2 or \substr($text, 2, 1) !== '/') { + return null; + } + + if (\strpos($Excerpt->context(), 'http') !== false + and \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt->context(), $matches, \PREG_OFFSET_CAPTURE) + ) { + return new self($matches[0][0], \intval($matches[0][1])); + } + + return null; + } + + /** + * Return an integer to declare that the inline should be treated as if it + * started from that position in the excerpt given to static::build. + * Return null to use the excerpt offset value. + * @return int|null + * */ + public function modifyStartPositionTo() + { + return $this->position; + } + + /** + * @return Element + */ + public function stateRenderable(Parsedown $_) + { + return new Element('a', ['href' => $this->url], [new Text($this->url)]); + } +} diff --git a/src/Parsedown.php b/src/Parsedown.php index b731ae3..b1d1e19 100644 --- a/src/Parsedown.php +++ b/src/Parsedown.php @@ -411,33 +411,6 @@ class Parsedown return $Elements; } - protected function inlineUrl($Excerpt) - { - if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') { - return; - } - - if (\strpos($Excerpt['context'], 'http') !== false - and \preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, \PREG_OFFSET_CAPTURE) - ) { - $url = $matches[0][0]; - - $Inline = [ - 'extent' => \strlen($matches[0][0]), - 'position' => $matches[0][1], - 'element' => [ - 'name' => 'a', - 'text' => $url, - 'attributes' => [ - 'href' => $url, - ], - ], - ]; - - return $Inline; - } - } - protected function inlineUrlTag($Excerpt) { if (\strpos($Excerpt['text'], '>') !== false and \preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) {