aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-06-29 04:09:44 +0200
committerPřemysl Eric Janouch <p@janouch.name>2021-06-29 04:14:47 +0200
commitbe27f006859dec9eb8e3f980b378ca01f53d82ca (patch)
tree41f5455bc3847d96622faaf4f3723cde995022d2
parent61083027a3beed1996761b05625a4a87cf90e607 (diff)
downloadhaven-be27f006859dec9eb8e3f980b378ca01f53d82ca.tar.gz
haven-be27f006859dec9eb8e3f980b378ca01f53d82ca.tar.xz
haven-be27f006859dec9eb8e3f980b378ca01f53d82ca.zip
hswg: add .Attr, .AttrList, contains
Trying to figure out a sensible way of handle tags.
-rw-r--r--hswg/main.go31
1 files changed, 29 insertions, 2 deletions
diff --git a/hswg/main.go b/hswg/main.go
index 56be51b..9eaca84 100644
--- a/hswg/main.go
+++ b/hswg/main.go
@@ -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)
}