47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package kaku
|
|
|
|
import "strings"
|
|
|
|
// Searches for key value pairs for specific markup like (audio: ) or (link: )
|
|
func parseKeyedTag(text string, keys []string) map[string]string {
|
|
buffers := map[string]*strings.Builder{}
|
|
var active *strings.Builder
|
|
|
|
// Loop over each character of the text
|
|
for i := 0; i < len(text); {
|
|
matched := ""
|
|
|
|
// At each char look ahead to see if it's actually one of the keys
|
|
for _, key := range keys {
|
|
if strings.HasPrefix(text[i:], key+":") {
|
|
matched = key
|
|
break
|
|
}
|
|
}
|
|
|
|
// If a key is found, build a string builder
|
|
// Point active to this string builder
|
|
// Skip past the key
|
|
if matched != "" {
|
|
if buffers[matched] == nil {
|
|
buffers[matched] = &strings.Builder{}
|
|
}
|
|
active = buffers[matched]
|
|
i += len(matched) + 1
|
|
continue
|
|
}
|
|
|
|
// If active refers to string builder
|
|
// Then put the text inside
|
|
if active != nil {
|
|
active.WriteByte(text[i])
|
|
}
|
|
i++
|
|
}
|
|
|
|
result := make(map[string]string, len(buffers))
|
|
for key, buf := range buffers {
|
|
result[key] = strings.TrimSpace(buf.String())
|
|
}
|
|
return result
|
|
}
|