12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package figtree
- import (
- "fmt"
- "path"
- "html/template"
- "strings"
- "io/ioutil"
- )
- type Fig struct {
- Id string
- Name string `json:"name"`
- Desc string `json:"desc"`
- Type string `json:"type"`
- Ext string `json:"ext"`
- Birth string `json:"birth"`
- }
- type FancyFig struct {
- Name string
- Path string
- Desc string
- Birth string
- View template.HTML
- }
- func (f Fig) Fancy() FancyFig {
- path := fmt.Sprintf("%s%s/%s.%s", FIGS, f.Type, f.Id, f.Ext)
- view := tryDispHtml(f)
- return FancyFig{ f.Name, path, f.Desc, f.Birth, view}
- }
- func tryDispHtml(fig Fig) template.HTML {
- figtype := strings.ToLower(fig.Ext)
- figpath := fig.Type + "/" + fig.Id + "." + fig.Ext
- switch figtype {
- case "png", "jpg", "jpeg", "gif":
- return template.HTML("<img src=/imagination/" + figpath + ">")
- case "txt", "org":
- contents, err := ioutil.ReadFile(path.Join(FIGS, figpath))
- if err != nil {
- panic(err)
- }
- return template.HTML(fmt.Sprintf("<blockquote><pre>%s</pre></blockquote>", contents))
- default:
- return template.HTML("<p>somethin good</p>")
- }
- }
|