67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package wikitext_parser
|
|
|
|
import "strings"
|
|
|
|
type Link struct {
|
|
URL string
|
|
IsExternal bool
|
|
Name []interface{}
|
|
}
|
|
|
|
func ParseLink(text string, index int, depth int, startCharacter byte) (i int, link *Link) {
|
|
|
|
var c byte
|
|
lastToken := index
|
|
|
|
addValue := func() int {
|
|
if lastToken < len(text) && i-lastToken > 0 {
|
|
t := strings.TrimSpace(text[lastToken:i])
|
|
if len(t) > 0 {
|
|
if link == nil {
|
|
link = &Link{URL: t, IsExternal: startCharacter == '['}
|
|
} else {
|
|
link.Name = append(link.Name, t)
|
|
}
|
|
}
|
|
|
|
return len(t)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
for i = index; i < len(text); i++ {
|
|
c = text[i]
|
|
|
|
if c == ' ' || c == '\t' && link == nil {
|
|
addValue()
|
|
lastToken = i + 1
|
|
} else if startCharacter == '{' && c == '}' {
|
|
addValue()
|
|
i += 1
|
|
break
|
|
} else if startCharacter == '[' && c == ']' { //end of link
|
|
addValue()
|
|
i += 1
|
|
break
|
|
//template or light might have parameters
|
|
} else if (c == '{' && i < len(text)-1 && text[i+1] == '{') || (c == '[' && i < len(text)-1 && text[i+1] == '[') {
|
|
addValue()
|
|
var tpl *Template
|
|
var scanIndex int
|
|
scanIndex, tpl = ParseTemplate(text, i+2, depth+1, c)
|
|
if tpl != nil {
|
|
if link == nil {
|
|
link = &Link{}
|
|
}
|
|
|
|
link.Name = append(link.Name, tpl)
|
|
}
|
|
lastToken = scanIndex
|
|
i = scanIndex - 1
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|