diff options
Diffstat (limited to 'hswg')
-rw-r--r-- | hswg/main.go | 31 |
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) } |