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/htmlconv/convert.go | 135 ++--------------------------------- 1 file changed, 4 insertions(+), 131 deletions(-) (limited to 'gopher/gophermap/htmlconv/convert.go') diff --git a/gopher/gophermap/htmlconv/convert.go b/gopher/gophermap/htmlconv/convert.go index a669601..8fa2f52 100644 --- a/gopher/gophermap/htmlconv/convert.go +++ b/gopher/gophermap/htmlconv/convert.go @@ -1,13 +1,11 @@ package htmlconv import ( - "bytes" "html/template" "io" - "strings" "tildegit.org/tjp/sliderule/gopher" - "tildegit.org/tjp/sliderule/internal/types" + "tildegit.org/tjp/sliderule/gopher/gophermap/internal" ) // Convert writes html to a writer from the provided gophermap document. @@ -32,13 +30,13 @@ func Convert(wr io.Writer, doc gopher.MapDocument, overrides *template.Template) return err } - tmpl, err = addOverrides(tmpl, overrides) + tmpl, err = internal.AddHTMLOverrides(tmpl, overrides) if err != nil { return err } - for _, item := range renderItems(doc) { - if err := tmpl.ExecuteTemplate(wr, item.template, item.object); err != nil { + for _, item := range internal.RenderItems(doc) { + if err := tmpl.ExecuteTemplate(wr, item.Template, item.Object); err != nil { return err } } @@ -46,131 +44,6 @@ func Convert(wr io.Writer, doc gopher.MapDocument, overrides *template.Template) return nil } -type renderItem struct { - template string - object any -} - -type renderRef struct { - Type types.Status - Display string - Selector template.URL - Hostname string - Port string -} - -func refItem(item gopher.MapItem) renderRef { - return renderRef{ - Type: item.Type, - Display: item.Display, - Selector: template.URL(item.Selector), - Hostname: item.Hostname, - Port: item.Port, - } -} - -func renderItems(doc gopher.MapDocument) []renderItem { - out := make([]renderItem, 0, len(doc)) - out = append(out, renderItem{ - template: "header", - object: doc, - }) - inMsg := false - msg := &bytes.Buffer{} - var currentHost, currentPort string - - for _, mapItem := range doc { - switch mapItem.Type { - case gopher.InfoMessageType: - if inMsg { - _, _ = msg.WriteString("\n") - } else { - msg.Reset() - } - _, _ = msg.WriteString(mapItem.Display) - inMsg = true - - if currentHost == "" { - currentHost = mapItem.Hostname - } - if currentPort == "" { - currentPort = mapItem.Port - } - case gopher.GifFileType, gopher.ImageFileType, gopher.BitmapType, gopher.PngImageFileType: - if inMsg { - out = append(out, renderItem{ - template: "message", - object: msg.String(), - }) - inMsg = false - } - out = append(out, renderItem{ - template: "image", - object: refItem(mapItem), - }) - default: - if inMsg { - out = append(out, renderItem{ - template: "message", - object: msg.String(), - }) - inMsg = false - } - out = append(out, renderItem{ - template: "link", - object: refItem(mapItem), - }) - } - } - - if inMsg { - out = append(out, renderItem{ - template: "message", - object: msg.String(), - }) - } - - simplifyLinks(out, currentHost, currentPort) - - return append(out, renderItem{ - template: "footer", - object: doc, - }) -} - -func simplifyLinks(items []renderItem, currentHost, currentPort string) { - for i, item := range items { - if item.template != "link" && item.template != "image" { - continue - } - - m := item.object.(renderRef) - if m.Hostname == currentHost && m.Port == currentPort { - m.Hostname = "" - m.Port = "" - m.Selector = template.URL(strings.TrimPrefix(string(m.Selector), "URL:")) - items[i].object = m - } - } -} - -func addOverrides(base *template.Template, overrides *template.Template) (*template.Template, error) { - if overrides == nil { - return base, nil - } - - tmpl := base - var err error - for _, override := range overrides.Templates() { - tmpl, err = tmpl.AddParseTree(override.Name(), override.Tree) - if err != nil { - return nil, err - } - } - - return tmpl, nil -} - var baseTmpl = template.Must(template.New("htmlconv").Parse(` {{ define "header" -}} -- cgit v1.2.3