fig.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package figtree
  2. import (
  3. "fmt"
  4. "path"
  5. "html/template"
  6. "strings"
  7. "io/ioutil"
  8. )
  9. type Fig struct {
  10. Id string
  11. Name string `json:"name"`
  12. Desc string `json:"desc"`
  13. Type string `json:"type"`
  14. Ext string `json:"ext"`
  15. Birth string `json:"birth"`
  16. }
  17. type FancyFig struct {
  18. Name string
  19. Path string
  20. Desc string
  21. Birth string
  22. View template.HTML
  23. }
  24. func (f Fig) Fancy() FancyFig {
  25. path := fmt.Sprintf("%s%s/%s.%s", FIGS, f.Type, f.Id, f.Ext)
  26. view := tryDispHtml(f)
  27. return FancyFig{ f.Name, path, f.Desc, f.Birth, view}
  28. }
  29. func tryDispHtml(fig Fig) template.HTML {
  30. figtype := strings.ToLower(fig.Ext)
  31. figpath := fig.Type + "/" + fig.Id + "." + fig.Ext
  32. switch figtype {
  33. case "png", "jpg", "jpeg", "gif":
  34. return template.HTML("<img src=/imagination/" + figpath + ">")
  35. case "txt", "org":
  36. contents, err := ioutil.ReadFile(path.Join(FIGS, figpath))
  37. if err != nil {
  38. panic(err)
  39. }
  40. return template.HTML(fmt.Sprintf("<blockquote><pre>%s</pre></blockquote>", contents))
  41. default:
  42. return template.HTML("<p>somethin good</p>")
  43. }
  44. }