mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-05 19:30:12 +08:00
feat: parse template meta
This commit is contained in:
1 parent
70837bf6b1
commit
99f6e8cefd
5 files changed
+53
-2
No files matched your search
@@ -1,3 +1,5 @@
|
||||
module github.com/lancatlin/mdsh-go
|
||||
|
||||
go 1.24.5
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
@@ -0,0 +1,3 @@
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -10,7 +10,12 @@ import (
|
||||
|
||||
var tmpl = `
|
||||
---
|
||||
title: This is the title
|
||||
output: "reports/${from}.md"
|
||||
params:
|
||||
from:
|
||||
required: true
|
||||
file:
|
||||
default: "ledger.j"
|
||||
---
|
||||
Hello this is the text:
|
||||
{{ sh "hledger bal assets" }}
|
||||
@@ -50,7 +55,11 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("'%s'", frontmatter)
|
||||
templateMeta, err := parseFrontmatter(frontmatter)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("'%s'", templateMeta)
|
||||
t, err := template.New("foo").Funcs(funcMap).Parse(body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func parseFrontmatter(input string) (meta TemplateMeta, err error) {
|
||||
err = yaml.Unmarshal([]byte(input), &meta)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Failed to parse frontmatter: %w", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type TemplateMeta struct {
|
||||
Output string `yaml:"output"`
|
||||
Params map[string]ParamSpec `yaml:"params"`
|
||||
}
|
||||
|
||||
func (meta TemplateMeta) String() string {
|
||||
data, err := json.Marshal(meta)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type ParamSpec struct {
|
||||
Required bool `yaml:"required"`
|
||||
Default string `yaml:"default"`
|
||||
}
|
||||
Reference in new issue
Block a user