From a61bcdeb314d4e0e9f6e8915b92010895170e785 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Fri, 1 Sep 2023 12:37:42 -0600 Subject: refactor gophermap template handling and add markdown conversion --- gopher/gophermap/mdconv/convert.go | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 gopher/gophermap/mdconv/convert.go (limited to 'gopher/gophermap/mdconv/convert.go') diff --git a/gopher/gophermap/mdconv/convert.go b/gopher/gophermap/mdconv/convert.go new file mode 100644 index 0000000..5d4da08 --- /dev/null +++ b/gopher/gophermap/mdconv/convert.go @@ -0,0 +1,52 @@ +package mdconv + +import ( + "io" + "text/template" + + "tildegit.org/tjp/sliderule/gopher" + "tildegit.org/tjp/sliderule/gopher/gophermap/internal" +) + +func Convert(wr io.Writer, doc gopher.MapDocument, overrides *template.Template) error { + tmpl, err := baseTmpl.Clone() + if err != nil { + return err + } + + tmpl, err = internal.AddOverrides(tmpl, overrides) + if err != nil { + return err + } + + for _, item := range internal.RenderItems(doc) { + if err := tmpl.ExecuteTemplate(wr, item.Template, item.Object); err != nil { + return err + } + } + + return nil +} + +var baseTmpl = template.Must(template.New("mdconv").Parse(` +{{ define "header" }}{{ end }} + +{{ define "message" -}} +` + "```" + ` +{{.}} +` + "```" + ` + +{{ end }} + +{{ define "link" -}} +[{{.Display}}]({{ if .Hostname | and .Port }}gopher://{{.Hostname}}:{{.Port}}{{ end }}{{.Selector}}) + +{{ end }} + +{{ define "image" -}} +![{{.Display}}]({{ if .Hostname | and .Port }}gopher://{{.Hostname}}:{{.Port}}{{ end }}{{.Selector}}) + +{{ end }} + +{{ define "footer" }}{{ end }} +`)) -- cgit v1.2.3