From dc6888bf540ea2826548605358e7019463951c65 Mon Sep 17 00:00:00 2001 From: Basil Philipp Date: Sun, 12 Jul 2020 22:09:44 +0200 Subject: [PATCH] Correctly deal with multiple links on one line (#30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐞 Fix greedy matching of [[]] links. * 🐞 Fix greedy matching of []() links. --- pyproject.toml | 2 +- .../03272020061037-electrodermal-activity.md | 3 +++ .../202002251025_This_is_the_first_test_zettel.md | 4 +--- .../03272020061037-electrodermal-activity.txt | 2 ++ .../202002251025_This_is_the_first_test_zettel.txt | 4 +--- tests/test_vizel.py | 4 ++-- vizel/cli.py | 8 +++++--- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 29bc3be..58a0920 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vizel" -version = "0.2.0" +version = "0.2.1" description = "Vizualise a Zettelkasten" authors = ["Basil Philipp "] license = "GPL-3.0-only" diff --git a/tests/data/zettelkasten_md/03272020061037-electrodermal-activity.md b/tests/data/zettelkasten_md/03272020061037-electrodermal-activity.md index f09955d..9dc10a1 100644 --- a/tests/data/zettelkasten_md/03272020061037-electrodermal-activity.md +++ b/tests/data/zettelkasten_md/03272020061037-electrodermal-activity.md @@ -31,4 +31,7 @@ nocite: | --- --- +Here we have to links on one line to test the regex. [A first link](202002251025_This_is_the_first_test_zettel.md) and a [second line](202003211727_This_is_the_second_test_zettel.md). + + ### **References:** \ No newline at end of file diff --git a/tests/data/zettelkasten_md/202002251025_This_is_the_first_test_zettel.md b/tests/data/zettelkasten_md/202002251025_This_is_the_first_test_zettel.md index 775b9e8..01e36da 100644 --- a/tests/data/zettelkasten_md/202002251025_This_is_the_first_test_zettel.md +++ b/tests/data/zettelkasten_md/202002251025_This_is_the_first_test_zettel.md @@ -1,9 +1,7 @@ # 202002251025 This is the first test Zettel tags: #artificialintelligence #zettelkasten -This Zettel references this other Zettel here[[202002241029]]. - -It points to the second Zettel [[202003211727]]. +This Zettel references this other Zettel here[[202002241029]]. It points to the second Zettel [[202003211727]]. ## It also has subtitles, fancy diff --git a/tests/data/zettelkasten_txt/03272020061037-electrodermal-activity.txt b/tests/data/zettelkasten_txt/03272020061037-electrodermal-activity.txt index 15c5113..0cb922b 100644 --- a/tests/data/zettelkasten_txt/03272020061037-electrodermal-activity.txt +++ b/tests/data/zettelkasten_txt/03272020061037-electrodermal-activity.txt @@ -31,4 +31,6 @@ nocite: | --- --- +Here we have to links on one line to test the regex. [A first link](202002251025_This_is_the_first_test_zettel.txt) and a [second line](202003211727_This_is_the_second_test_zettel.txt). + ### **References:** \ No newline at end of file diff --git a/tests/data/zettelkasten_txt/202002251025_This_is_the_first_test_zettel.txt b/tests/data/zettelkasten_txt/202002251025_This_is_the_first_test_zettel.txt index 775b9e8..01e36da 100644 --- a/tests/data/zettelkasten_txt/202002251025_This_is_the_first_test_zettel.txt +++ b/tests/data/zettelkasten_txt/202002251025_This_is_the_first_test_zettel.txt @@ -1,9 +1,7 @@ # 202002251025 This is the first test Zettel tags: #artificialintelligence #zettelkasten -This Zettel references this other Zettel here[[202002241029]]. - -It points to the second Zettel [[202003211727]]. +This Zettel references this other Zettel here[[202002241029]]. It points to the second Zettel [[202003211727]]. ## It also has subtitles, fancy diff --git a/tests/test_vizel.py b/tests/test_vizel.py index 1d9e364..687945a 100644 --- a/tests/test_vizel.py +++ b/tests/test_vizel.py @@ -47,9 +47,9 @@ def test_stats(zettelkasten_directory, stderr_expected): assert result.exit_code == 0 stdout_output = ( '7 Zettel\n' - '4 references between Zettel\n' + '6 references between Zettel\n' '2 Zettel with no references\n' - '4 connected components\n' + '3 connected components\n' ) assert result.stdout == stdout_output diff --git a/vizel/cli.py b/vizel/cli.py index 7b40baa..c72217d 100644 --- a/vizel/cli.py +++ b/vizel/cli.py @@ -68,11 +68,13 @@ def _load_references(zettel_path, zettel_directory_path): zettel_filenames = sorted( [os.path.basename(f) for f in glob.glob(os.path.join(zettel_directory_path, '*[.md|.txt]'))]) - # Extract references for the [[]] link format - references += _extract_valid_references('\[\[(.+)\]\]', zettel_path, zettel_filenames) + # Extract references for the [[ID]] link format + # Look for [[, and then match anything that isn't ]]. End with ]]. + references += _extract_valid_references('\[\[([^\]\]]+)\]\]', zettel_path, zettel_filenames) # Extract references for the markdown link format - references += _extract_valid_references('\[.*\]\((.+)\)', zettel_path, zettel_filenames) + # Look for [, and then match anything that isn't ]. Then look for ( and match anything that isn't ). End with ). + references += _extract_valid_references('\[[^\]]+\]\(([^\)]+)\)', zettel_path, zettel_filenames) return references