package htmlconv import ( "html/template" "io" "tildegit.org/tjp/sliderule/gopher" "tildegit.org/tjp/sliderule/gopher/gophermap/internal" ) // Convert writes html to a writer from the provided gophermap document. // // Templates can be provided to override the output for different line types. // The supported templates are: // - "header' is called before any lines and is passed the full document. // - "footer" is called after all lines and is passed the full document. // - "message" is called for every group of consecutive info-message lines. It is // passed a string of all the display components of the included lines, joined by // newline characters. // - "image" is called for all lines of any of the following types: GifFileType, // ImageFileType, BitmapType, PngImageFileType. It is passed the gopher.MapItem. // - "link" is called for all lines of any other type not yet mentioned, and is passed // the gopher.MapItem. // // There are already default implementations of each of these templates, so the "overrides" // argument can be nil or include just a subset of the supported templates. func Convert(wr io.Writer, doc gopher.MapDocument, overrides *template.Template) error { tmpl, err := baseTmpl.Clone() if err != nil { return err } tmpl, err = internal.AddHTMLOverrides(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("htmlconv").Parse(` {{ define "header" -}} {{- end }} {{ define "message" -}}
{{.}}
{{- end }} {{ define "link" -}}

{{.Display}}

{{- end }} {{ define "image" -}}

{{.Display}}

{{- end }} {{ define "footer" -}} {{ end }} `))