From 0b2443c720c962dbd1f5730c40e4a5d52f051a23 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Tue, 7 Feb 2023 19:11:21 +0000 Subject: [PATCH] passing tests --- go.mod | 2 +- main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 34 +++++++++++++++++++++++++++++++++ test-cases | 10 ++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 main.go create mode 100644 main_test.go create mode 100644 test-cases diff --git a/go.mod b/go.mod index 016c362..4865887 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/therealfakemoot/wikilinks-parser +module wikilinks go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..2f35d93 --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "log" + "regexp" +) + +var ( + alias = regexp.MustCompile(`\|(.*)`) + link = regexp.MustCompile(`^([a-zA-Z ]+)?[\|\#\^\\]?`) + subsection = regexp.MustCompile(`#([\w\s]+)`) + block = regexp.MustCompile(`#\^([a-zA-Z ]+)`) + wholeLink = regexp.MustCompile(`\[\[(.*)\]\]`) +) + +type Link struct { + Dest, Alias, Subsection, Block string +} + +func ExtractLink(raw string) Link { + var li Link + + rawLinkMatch := wholeLink.FindAllStringSubmatch(raw, -1) + var rawLink string + if len(rawLinkMatch) > 0 { + rawLink = rawLinkMatch[0][1] + } + + l := link.FindAllStringSubmatch(rawLink, -1) + if len(l) > 0 { + li.Dest = l[0][1] + } + + a := alias.FindAllStringSubmatch(rawLink, -1) + if len(a) > 0 { + li.Alias = a[0][1] + } + + s := subsection.FindAllStringSubmatch(rawLink, -1) + if len(s) > 0 { + li.Subsection = s[0][1] + } + + b := block.FindAllStringSubmatch(rawLink, -1) + if len(b) > 0 { + li.Block = b[0][1] + } + + return li +} + +func main() { + log.Printf("%#v\n", ExtractLink(`Embedding a link in a bigger block of text [[Regular Link#^link to block]] shouldn't cause any problems `)) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..38c3b20 --- /dev/null +++ b/main_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "testing" +) + +func Test_ExtractLink(t *testing.T) { + tcs := []struct { + name string + in string + expected Link + }{ + {"link", `[[Regular Link]]`, Link{Dest: "Regular Link"}}, + {"transclude+link", `![[Transcluded Link]]`, Link{Dest: "Transcluded Link"}}, + {"link+alias", `[[Regular Link|Alias]]`, Link{Dest: "Regular Link", Alias: "Alias"}}, + {"link+subsection", `[[Regular Link#Subsection of page]]`, Link{Dest: "Regular Link", Subsection: "Subsection of page"}}, + {"link+block", `[[Regular Link#^link to block]]`, Link{Dest: "Regular Link", Block: "link to block"}}, + {"link+subsection+alias", `[[Regular Link#Subsection of page|Alias]]`, Link{Dest: "Regular Link", Subsection: "Subsection of page", Alias: "Alias"}}, + {"link+block+alias", `[[Regular Link#^link to block|Alias]]`, Link{Dest: "Regular Link", Block: "link to block", Alias: "Alias"}}, + {"link+alias+escape", `[[Regular Link\|Alias]]`, Link{Dest: "Regular Link", Alias: "Alias"}}, + {"link+subsection+alias+escape", `[[Regular Link#Subsection of page\|Alias]]`, Link{Dest: "Regular Link", Subsection: "Subsection of page", Alias: "Alias"}}, + {"link+block+alias+escape", `[[Regular Link#^link to block\|Alias]]`, Link{Dest: "Regular Link", Block: "link to block", Alias: "Alias"}}, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + out := ExtractLink(tc.in) + if out != tc.expected { + t.Logf("got %#v\n", out) + t.Fail() + } + }) + } +} diff --git a/test-cases b/test-cases new file mode 100644 index 0000000..20b4d5f --- /dev/null +++ b/test-cases @@ -0,0 +1,10 @@ +[[Regular Link]] +![[Transcluded Link]] +[[Regular Link|Alias]] +[[Regular Link#Subsection of page]] +[[Regular Link^link to block]] +[[Regular Link#Subsection of page|Alias]] +[[Regular Link^link to block|Alias]] +[[Regular Link\|Alias]] +[[Regular Link#Subsection of page\|Alias]] +[[Regular Link^link to block\|Alias]]