diff --git a/lexer.go b/lexer.go index 112aedb..31399bf 100644 --- a/lexer.go +++ b/lexer.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "strings" + // "unicode" "unicode/utf8" ) @@ -136,11 +137,17 @@ func lexFragment(l *Lexer) stateFn { func lexAlias(l *Lexer) stateFn { // l.accept log.Println("entering lexAlias") + if strings.HasPrefix(l.input[l.pos:], CloseLink) { + return lexCloseLink + } + l.emit(ItemAlias) + return lexInsideLink } func lexInsideLink(l *Lexer) stateFn { log.Println("entering lexInsideLink") + for { if strings.HasPrefix(l.input[l.pos:], CloseLink) { return lexCloseLink @@ -154,12 +161,15 @@ func lexInsideLink(l *Lexer) stateFn { return l.errorf("unclosed link") case r == '#': l.emit(ItemText) + return lexFragment case r == '|': l.emit(ItemText) + return lexAlias case l.peek() == ']': l.emit(ItemText) + return lexCloseLink } } diff --git a/lexer_test.go b/lexer_test.go index 1e50d3d..3968850 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -11,11 +11,26 @@ func Test_Lexer(t *testing.T) { tcs := []struct { name string in string - expected []wikilink.ItemType + expected []wikilink.Item }{ - {name: "wikilink", in: "[[wikilink]]", expected: []wikilink.ItemType{wikilink.ItemOpenLink, wikilink.ItemText, wikilink.ItemCloseLink}}, + { + name: "wikilink", in: "[[wikilink]]", expected: []wikilink.Item{ + {Typ: wikilink.ItemOpenLink, Val: "[["}, + {Typ: wikilink.ItemText, Val: "wikilink"}, + {Typ: wikilink.ItemCloseLink, Val: "]]"}, + }, + }, + { + name: "wikilink|display name", in: "[[wikilink|display name]]", expected: []wikilink.Item{ + {Typ: wikilink.ItemOpenLink, Val: "[["}, + {Typ: wikilink.ItemText, Val: "wikilink"}, + {Typ: wikilink.ItemAlias, Val: "|"}, + {Typ: wikilink.ItemText, Val: "display name"}, + {Typ: wikilink.ItemCloseLink, Val: "]]"}, + }, + }, + // {name: "", in: "", expected: []wikilink.ItemType{wikilink.ItemOpenLink, wikilink.ItemText, wikilink.ItemAlias, wikilink.ItemText, wikilink.ItemCloseLink}}, /* - {name: "wikilink|display name", in: "[[wikilink|display name]]", expected: []wikilink.ItemType{}}, {name: "wikilink|display name|second pipe", in: "[[wikilink|display name|second pipe]]", expected: []wikilink.ItemType{}}, {name: "wikilink with numeric alias|420|second pipe", in: "[[wikilink|420|second pipe]]", expected: []wikilink.ItemType{}}, {name: "wikilink with spaces in filename", in: "[[wikilink spaces]]", expected: []wikilink.ItemType{}}, @@ -39,8 +54,13 @@ func Test_Lexer(t *testing.T) { l := wikilink.Lex("testLexer", tc.in) for _, e := range tc.expected { n := l.NextItem() - if e != n.Typ { - t.Logf("expected %s, received %s\n with raw value %q", e, n, n.Val) + if e.Typ != n.Typ { + t.Logf("expected Type %s, received %s\n with raw value %q", e.String(), n, n.Val) + t.Fail() + } + + if e.Val != n.Val { + t.Logf("expected Value %s, received %s\n with raw value %q", e.String(), n, n.Val) t.Fail() } }