adoc/element/link.go

73 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-03-15 15:16:15 +01:00
package element
import (
2024-09-15 12:07:22 +02:00
"go-mod.ewintr.nl/adoc/token"
2022-03-15 15:16:15 +01:00
)
type Link struct {
URL string
Title string
}
func (l Link) Text() string { return l.Title }
func (l Link) Append(_ []Element) Element { return l }
func NewLinkFromTokens(tr ReadUnreader) (ParseResult, bool) {
tok, ok := tr.Read(1)
if !ok {
return ParseResult{}, false
}
tr.Unread(1)
if tok[0].Type != token.TYPE_WORD {
return ParseResult{}, false
}
url, title := []token.Token{}, []token.Token{}
lb := false
count := 0
for {
ntoks, ok := tr.Read(1)
if !ok {
tr.Unread(count)
return ParseResult{}, false
}
count++
tok := ntoks[0]
if tok.Equal(token.TOKEN_EOF, token.TOKEN_EOS, token.TOKEN_NEWLINE, token.TOKEN_DOUBLE_NEWLINE) {
tr.Unread(count)
return ParseResult{}, false
}
if !lb && tok.Type == token.TYPE_WHITESPACE {
tr.Unread(count)
return ParseResult{}, false
}
if tok.Equal(token.Token{Type: token.TYPE_BRACKETOPEN, Literal: "["}) {
lb = true
continue
}
if lb && tok.Equal(token.Token{Type: token.TYPE_BRACKETCLOSE, Literal: "]"}) {
break
}
if lb {
title = append(title, tok)
continue
}
url = append(url, tok)
}
if len(url) == 0 || !lb {
tr.Unread(count)
return ParseResult{}, false
}
link := Link{
URL: token.Literals(url),
Title: token.Literals(title),
}
return ParseResult{
Element: link,
Inner: []token.Token{},
}, true
}