From 766b4317cc2f5ffc9fb54485801df8fc4e1c3921 Mon Sep 17 00:00:00 2001 From: Kazuyuki Hayashi Date: Thu, 23 Jan 2014 17:13:37 +0900 Subject: [PATCH] Fixes #27 [URL Auto-linking] URL inside --- .../Extension/Gfm/UrlAutoLinkExtension.php | 17 ++++++++++++++++- test/Ciconia/Resources/gfm/link-in-code-span.md | 4 +++- .../Ciconia/Resources/gfm/link-in-code-span.out | 4 +++- test/Ciconia/Resources/gfm/link-in-codeblock.md | 3 +++ .../Ciconia/Resources/gfm/link-in-codeblock.out | 2 ++ 5 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/Ciconia/Resources/gfm/link-in-codeblock.md create mode 100644 test/Ciconia/Resources/gfm/link-in-codeblock.out diff --git a/src/Ciconia/Extension/Gfm/UrlAutoLinkExtension.php b/src/Ciconia/Extension/Gfm/UrlAutoLinkExtension.php index 35bc58e..0cbe546 100644 --- a/src/Ciconia/Extension/Gfm/UrlAutoLinkExtension.php +++ b/src/Ciconia/Extension/Gfm/UrlAutoLinkExtension.php @@ -29,7 +29,22 @@ public function register(Markdown $markdown) */ public function processStandardUrl(Text $text) { - $text->replace('{(?|\[)((?:https?|ftp)://[^\'">\s]+)(?!>|<|\"|\])}', '<\1>'); + $hashes = array(); + + // escape + $text->replace('{.*?}m', function (Text $w) use (&$hashes) { + $md5 = md5($w); + $hashes[$md5] = $w; + + return "{gfm-extraction-$md5}"; + }); + + $text->replace('{(?\s]+)(?!>|\"|\])}', '<\1>'); + + /** @noinspection PhpUnusedParameterInspection */ + $text->replace('/\{gfm-extraction-([0-9a-f]{32})\}/m', function (Text $w, Text $md5) use (&$hashes) { + return $hashes[(string)$md5]; + }); } /** diff --git a/test/Ciconia/Resources/gfm/link-in-code-span.md b/test/Ciconia/Resources/gfm/link-in-code-span.md index 39f016b..25f772b 100644 --- a/test/Ciconia/Resources/gfm/link-in-code-span.md +++ b/test/Ciconia/Resources/gfm/link-in-code-span.md @@ -1 +1,3 @@ -Look at: `http://someurl.com` and text. \ No newline at end of file +Look at: `http://someurl.com` and text. + +Look at: `url: http://someurl.com` and text. \ No newline at end of file diff --git a/test/Ciconia/Resources/gfm/link-in-code-span.out b/test/Ciconia/Resources/gfm/link-in-code-span.out index b65a19f..091784e 100644 --- a/test/Ciconia/Resources/gfm/link-in-code-span.out +++ b/test/Ciconia/Resources/gfm/link-in-code-span.out @@ -1 +1,3 @@ -

Look at: http://someurl.com and text.

\ No newline at end of file +

Look at: http://someurl.com and text.

+ +

Look at: url: http://someurl.com and text.

\ No newline at end of file diff --git a/test/Ciconia/Resources/gfm/link-in-codeblock.md b/test/Ciconia/Resources/gfm/link-in-codeblock.md new file mode 100644 index 0000000..139e92d --- /dev/null +++ b/test/Ciconia/Resources/gfm/link-in-codeblock.md @@ -0,0 +1,3 @@ +``` php +$validator->validate('http://www.example.com/'); +``` \ No newline at end of file diff --git a/test/Ciconia/Resources/gfm/link-in-codeblock.out b/test/Ciconia/Resources/gfm/link-in-codeblock.out new file mode 100644 index 0000000..c784e37 --- /dev/null +++ b/test/Ciconia/Resources/gfm/link-in-codeblock.out @@ -0,0 +1,2 @@ +
$validator->validate('http://www.example.com/');
+
\ No newline at end of file