You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
1.7 KiB
Go
109 lines
1.7 KiB
Go
package wikilink
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
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 {
|
|
log.Println("lexIdent")
|
|
for {
|
|
r := l.next()
|
|
s := l.input[l.pos:]
|
|
if r == '\\' { // i think this will handle escape characters?
|
|
break
|
|
}
|
|
switch {
|
|
case isBlockRef(s):
|
|
l.emit(ItemIdent)
|
|
return lexBlockRef
|
|
case isAlias(s):
|
|
l.emit(ItemIdent)
|
|
return lexAlias
|
|
case isCloseLink(s):
|
|
l.emit(ItemIdent)
|
|
return lexCloseLink
|
|
case isHeading(s):
|
|
l.emit(ItemIdent)
|
|
return lexHeading
|
|
|
|
}
|
|
}
|
|
return l.errorf("malformed link")
|
|
}
|
|
|
|
func lexHeading(l *Lexer) stateFn {
|
|
log.Println("lexHeading")
|
|
l.pos += len(Heading)
|
|
l.emit(ItemHeading)
|
|
|
|
return lexIdent
|
|
}
|
|
|
|
func lexBlockRef(l *Lexer) stateFn {
|
|
log.Println("lexBlockRef")
|
|
l.pos += len(BlockRef)
|
|
l.emit(ItemBlockRef)
|
|
|
|
return lexIdent
|
|
}
|
|
|
|
func lexAlias(l *Lexer) stateFn {
|
|
log.Println("lexAlias")
|
|
l.pos += len(Alias)
|
|
l.emit(ItemAlias)
|
|
|
|
return lexIdent
|
|
}
|
|
|
|
func lexText(l *Lexer) stateFn {
|
|
log.Println("lexText")
|
|
for {
|
|
if isOpenLink(l.input[l.pos:]) {
|
|
return lexOpenLink
|
|
}
|
|
r := l.next()
|
|
switch {
|
|
case r == EOF || r == '\n':
|
|
l.emit(ItemText)
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func lexOpenLink(l *Lexer) stateFn {
|
|
log.Println("lexOpenLink")
|
|
l.pos += len(OpenLink)
|
|
l.emit(ItemOpenLink)
|
|
|
|
return lexIdent
|
|
}
|
|
|
|
func lexCloseLink(l *Lexer) stateFn {
|
|
log.Println("lexCloseLink")
|
|
l.pos += len(CloseLink)
|
|
l.emit(ItemCloseLink)
|
|
|
|
return lexText
|
|
}
|