From 3a7a38c21546708092f38e984a18119798d470f7 Mon Sep 17 00:00:00 2001 From: Wouter Groeneveld Date: Fri, 13 Jan 2017 12:59:45 +0100 Subject: [PATCH] fix regex --- .gitignore | 2 ++ .idea/vcs.xml | 6 ------ src/markdown_links.py | 17 +++++++++-------- test/test_markdown_links.py | 6 ++++++ 4 files changed, 17 insertions(+), 14 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 6756caf..b7ed096 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 +.idea/* + # User-specific stuff: .idea/workspace.xml .idea/tasks.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/markdown_links.py b/src/markdown_links.py index 845f975..16c3821 100644 --- a/src/markdown_links.py +++ b/src/markdown_links.py @@ -4,19 +4,20 @@ import re class MarkdownLinks: known_shortcodes = ('wp') - pattern = re.compile('\[\[.+\]\]') + pattern = re.compile('\[\[(.*?)\]\]') def convert(self, text): result = text - for link in MarkdownLinks.pattern.findall(text): + for regex_link in MarkdownLinks.pattern.findall(text): + origlink = "[[" + regex_link + "]]" convertedlink = "" - if "http" in text or "www" in text: - convertedlink = self.convert_as_external_link(text) - elif ">" in text: - convertedlink = self.convert_as_interwiki_link(text) + if "http" in regex_link or "www" in regex_link: + convertedlink = self.convert_as_external_link(origlink) + elif ">" in regex_link: + convertedlink = self.convert_as_interwiki_link(origlink) else: - convertedlink = self.convert_as_internal_link(text) - result = result.replace(link, convertedlink) + convertedlink = self.convert_as_internal_link(origlink) + result = result.replace(origlink, convertedlink) return result def parseUrl(self, text): diff --git a/test/test_markdown_links.py b/test/test_markdown_links.py index 480bc85..c03c3f0 100644 --- a/test/test_markdown_links.py +++ b/test/test_markdown_links.py @@ -34,6 +34,12 @@ class TestMarkdownLinks(TestCase): self.assertEqual(mdLink, self.converter.convert(dokuLink)) + def test_multiple_links_in_text_converted_properly(self): + mdLink = """{{< relref "bla" >}} wow this looks cool and so does {{< relref "this" >}} and such""" + dokuLink = "[[bla]] wow this looks cool and so does [[this]] and such" + + self.assertEqual(mdLink, self.converter.convert(dokuLink)) + def test_internal_links_with_some_text_in_line_converted_properly(self): mdLink = """[text]({{< relref "bla" >}}) wow this looks cool""" dokuLink = "[[bla|text]] wow this looks cool"