diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-01 12:37:42 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-01 12:37:42 -0600 |
commit | a61bcdeb314d4e0e9f6e8915b92010895170e785 (patch) | |
tree | 29611d33a0e65f334c7238a7748244ab63ad0d5c /gopher/gophermap/mdconv | |
parent | 2ce5be68acd3c66d4d135d7eb68b9ecd1563aa1d (diff) |
refactor gophermap template handling and add markdown conversion
Diffstat (limited to 'gopher/gophermap/mdconv')
-rw-r--r-- | gopher/gophermap/mdconv/convert.go | 52 | ||||
-rw-r--r-- | gopher/gophermap/mdconv/convert_test.go | 69 |
2 files changed, 121 insertions, 0 deletions
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 }} +`)) diff --git a/gopher/gophermap/mdconv/convert_test.go b/gopher/gophermap/mdconv/convert_test.go new file mode 100644 index 0000000..2e39106 --- /dev/null +++ b/gopher/gophermap/mdconv/convert_test.go @@ -0,0 +1,69 @@ +package mdconv + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "tildegit.org/tjp/sliderule/gopher/gophermap" +) + +func TestConvert(t *testing.T) { + tests := []struct { + name string + input string + output string + }{ + { + name: "basic doc", + input: strings.ReplaceAll(` +iI am informational text localhost 70 +icontinued on this line localhost 70 +i localhost 70 +0this is my text file /file.txt localhost 70 +i localhost 70 +1here's a sub-menu /sub/ localhost 70 +. +`[1:], "\n", "\r\n"), + output: (` +` + "```" + ` +I am informational text +continued on this line + +` + "```" + ` + +[this is my text file](/file.txt) + +` + "```" + ` + +` + "```" + ` + +[here's a sub-menu](/sub/) + +`)[1:], + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + doc, err := gophermap.Parse(bytes.NewBufferString(test.input)) + if err != nil { + t.Fatal(err) + } + + buf := &bytes.Buffer{} + if err := Convert(buf, doc, nil); err != nil { + t.Fatal(err) + } + + if buf.String() != test.output { + fmt.Println(">>> expected:") + fmt.Println(test.output) + fmt.Println(">>> got:") + fmt.Println(buf.String()) + t.Fatal("html body mismatch") + } + }) + } +} |