templates.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Package templates provides procedures and wrapper types for working with
  2. // Go's standard library template packages in a more straightforward way,
  3. // as well as enabling templates to include metadata, which the standard
  4. // library packages do not support.
  5. //
  6. // For details on the formats and processing of the metadata headers, see the
  7. // documentation for the metadata package.
  8. package templates
  9. import (
  10. "crypto/md5"
  11. "fmt"
  12. "io"
  13. htemplate "html/template"
  14. ttemplate "text/template"
  15. "cdop.pt/go/free/platepipe/documents"
  16. "cdop.pt/go/free/platepipe/documents/files"
  17. )
  18. // FromFile loads a text or HTML template from a file whose path is passed as
  19. // the argument.
  20. //
  21. // If the file's extension indicates that the file contains HTML, the
  22. // template will be parsed by html/template. Otherwise, it will be parsed by
  23. // text/template.
  24. func FromFile(file string) (*Template, map[string]any, error) {
  25. if files.HasKnownHTMLExt(file) {
  26. return HTMLTemplateFromFile(file)
  27. }
  28. return TextTemplateFromFile(file)
  29. }
  30. // HTMLTemplateFromFile loads an html/template and its metadata (if any) from
  31. // the given file.
  32. func HTMLTemplateFromFile(file string) (*Template, map[string]any, error) {
  33. buf, data, err := documents.FromTextFile(file)
  34. if err != nil {
  35. return nil, nil, err
  36. }
  37. t, err := newHTMLTemplate(buf)
  38. if err != nil {
  39. return nil, nil, err
  40. }
  41. return t, data, nil
  42. }
  43. // HTMLTemplateFromStream loads an html/template and its metadata (if any) from
  44. // the given io.Reader.
  45. func HTMLTemplateFromStream(r io.Reader) (*Template, map[string]any, error) {
  46. buf, data, err := documents.FromTextStream(r)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. t, err := newHTMLTemplate(buf)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. return t, data, nil
  55. }
  56. // TextTemplateFromFile loads an html/template and its metadata (if any) from
  57. // the given file.
  58. func TextTemplateFromFile(file string) (*Template, map[string]any, error) {
  59. buf, data, err := documents.FromTextFile(file)
  60. if err != nil {
  61. return nil, nil, err
  62. }
  63. t, err := newTextTemplate(buf)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. return t, data, nil
  68. }
  69. // TextTemplateFromStream loads an html/template and its metadata (if any) from
  70. // the given io.Reader.
  71. func TextTemplateFromStream(r io.Reader) (*Template, map[string]any, error) {
  72. buf, data, err := documents.FromTextStream(r)
  73. if err != nil {
  74. return nil, nil, err
  75. }
  76. t, err := newTextTemplate(buf)
  77. if err != nil {
  78. return nil, nil, err
  79. }
  80. return t, data, nil
  81. }
  82. func newHTMLTemplate(buf []byte) (*Template, error) {
  83. t, err := htemplate.New(hash(buf)).Parse(string(buf))
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &Template{t}, nil
  88. }
  89. func newTextTemplate(buf []byte) (*Template, error) {
  90. t, err := ttemplate.New(hash(buf)).Parse(string(buf))
  91. if err != nil {
  92. return nil, err
  93. }
  94. return &Template{t}, nil
  95. }
  96. func hash(buf []byte) string {
  97. return fmt.Sprintf("%x", (md5.New().Sum(buf))[0:4])
  98. }
  99. // Template is a wrapper type around the template types provided by both
  100. // text/template and html/template.
  101. type Template struct {
  102. stdTemplate interface {
  103. Execute(io.Writer, any) error
  104. }
  105. }
  106. // Apply renders the template, with the provided data, to an io.Writer.
  107. func (t *Template) Apply(w io.Writer, data map[string]any) error {
  108. return t.stdTemplate.Execute(w, data)
  109. }