hswg: add .Attr, .AttrList, contains

Trying to figure out a sensible way of handle tags.
This commit is contained in:
Přemysl Eric Janouch 2021-06-29 04:09:44 +02:00
parent 61083027a3
commit be27f00685
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 29 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import (
"path/filepath"
"regexp"
"sort"
"strings"
"syscall"
"time"
@ -40,6 +41,21 @@ type Metadata struct {
// be linked anywhere else.
func (m *Metadata) IsDraft() bool { return m.Attributes.Has("draft") }
// Attr is a shortcut for retrieving document attributes by name.
func (m *Metadata) Attr(name string) string {
return m.Attributes.GetAsStringWithDefault(name, "")
}
// AttrList is similar to Attr, but splits the result at commas,
// and trims whitespace around array elements.
func (m *Metadata) AttrList(name string) []string {
res := strings.Split(m.Attr(name), ",")
for i := range res {
res[i] = strings.TrimSpace(res[i])
}
return res
}
// Render converts an io.Reader with an AsciiDoc document to HTML. So long as
// the file could be read at all, it will always return a non-empty document.
func Render(r io.Reader, config configuration.Configuration) (
@ -398,6 +414,17 @@ func watchDirectory(dirname string) (<-chan *watchEvent, error) {
return ch, nil
}
var funcs = template.FuncMap{
"contains": func(needle string, haystack []string) bool {
for _, el := range haystack {
if el == needle {
return true
}
}
return false
},
}
func singleFile() {
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
if err != nil {
@ -426,7 +453,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
tmplEntry, err := template.New("entry").Parse(string(header))
tmplEntry, err := template.New("entry").Funcs(funcs).Parse(string(header))
if err != nil {
log.Fatalln(err)
}
@ -436,7 +463,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
tmplIndex, err := template.New("index").Parse(string(index))
tmplIndex, err := template.New("index").Funcs(funcs).Parse(string(index))
if err != nil {
log.Fatalln(err)
}