|
|
|
package wikilink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func isOpenLink(s string) bool {
|
|
|
|
return strings.HasPrefix(s, OpenLink)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isCloseLink(s string) bool {
|
|
|
|
return strings.HasPrefix(s, CloseLink)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAlias(s string) bool {
|
|
|
|
return strings.HasPrefix(s, Alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHeading(s string) bool {
|
|
|
|
return strings.HasPrefix(s, Heading)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isBlockRef(s string) bool {
|
|
|
|
return strings.HasPrefix(s, BlockRef)
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexIdent(l *Lexer) stateFn {
|
|
|
|
for {
|
|
|
|
L := l.L.Named("lexIdent")
|
|
|
|
s := l.input[l.GetPos():]
|
|
|
|
L.Debug("stepping through lexIdent")
|
|
|
|
if s[0] == '\\' { // i think this will handle escape characters?
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case isCloseLink(s):
|
|
|
|
L.Debug("found CloseLink")
|
|
|
|
l.emit(LexIdent)
|
|
|
|
return lexCloseLink
|
|
|
|
case isBlockRef(s):
|
|
|
|
L.Debug("found BlockRef")
|
|
|
|
l.emit(LexIdent)
|
|
|
|
return lexBlockRef
|
|
|
|
case isAlias(s):
|
|
|
|
L.Debug("found Alias")
|
|
|
|
l.emit(LexIdent)
|
|
|
|
return lexAlias
|
|
|
|
case isHeading(s):
|
|
|
|
L.Debug("found Heading")
|
|
|
|
l.emit(LexIdent)
|
|
|
|
return lexHeading
|
|
|
|
}
|
|
|
|
r := l.next()
|
|
|
|
L = l.L.With(
|
|
|
|
zap.String("rune", string(r)),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
return l.errorf("malformed link")
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexHeading(l *Lexer) stateFn {
|
|
|
|
l.SetPos(l.GetPos() + len(Heading))
|
|
|
|
l.emit(LexHeading)
|
|
|
|
|
|
|
|
return lexIdent
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexBlockRef(l *Lexer) stateFn {
|
|
|
|
l.SetPos(l.GetPos() + len(BlockRef))
|
|
|
|
l.emit(LexBlockRef)
|
|
|
|
|
|
|
|
return lexIdent
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexAlias(l *Lexer) stateFn {
|
|
|
|
l.SetPos(l.GetPos() + len(Alias))
|
|
|
|
l.emit(LexAlias)
|
|
|
|
|
|
|
|
return lexIdent
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexText(l *Lexer) stateFn {
|
|
|
|
L := l.L.Named("lexText")
|
|
|
|
for {
|
|
|
|
if isOpenLink(l.input[l.GetPos():]) {
|
|
|
|
L.Debug("found openLink")
|
|
|
|
return lexOpenLink
|
|
|
|
}
|
|
|
|
r := l.next()
|
|
|
|
switch {
|
|
|
|
case r == EOF || r == '\n':
|
|
|
|
l.emit(LexText)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexOpenLink(l *Lexer) stateFn {
|
|
|
|
l.SetPos(l.GetPos() + len(OpenLink))
|
|
|
|
l.emit(LexOpenLink)
|
|
|
|
|
|
|
|
return lexIdent
|
|
|
|
}
|
|
|
|
|
|
|
|
func lexCloseLink(l *Lexer) stateFn {
|
|
|
|
l.SetPos(l.GetPos() + len(CloseLink))
|
|
|
|
l.emit(LexCloseLink)
|
|
|
|
|
|
|
|
return lexText
|
|
|
|
}
|