Ejemplo n.º 1
0
func magicImage(path string, width, height uint, caption, class string) html.HTML {
	f, err := os.Open(env.RelPath("static", path))
	if err != nil {
		panic(err)
	}
	defer f.Close()

	ext := strings.ToLower(filepath.Ext(path))
	resizedPath := fmt.Sprintf("%s%dx%d%s", path[:len(path)-len(ext)], width, height, ext)

	finfo, err := os.Stat(env.RelPath("static", resizedPath))

	if err != nil || finfo.Size() == 0 {
		fmt.Printf("Resizing image '%s'...", path)
		img, _, err := image.Decode(f)
		if err != nil {
			panic(err)
		}
		resized := resize.Resize(width, height, img, resize.Bicubic)

		resizedFile, err := os.Create(env.RelPath("static", resizedPath))
		if err != nil {
			panic(err)
		}
		defer resizedFile.Close()

		switch ext {
		case ".jpg", ".jpeg":
			jpeg.Encode(resizedFile, resized, nil)
		case ".png":
			png.Encode(resizedFile, resized)
		}
		fmt.Println(" complete.")
	}
	return html.HTML(fmt.Sprintf(`
<div class="%s">
<figure>
	<a href="/static/%s">
		<img src="/static/%s" />
	</a>
	<figcaption>%s</figcaption>
	</figure>
</div>
`, class, path, resizedPath, caption))
}
Ejemplo n.º 2
0
func templatePath(components ...string) string {
	components[len(components)-1] += ".html"
	return env.RelPath(append([]string{TemplatePath}, components...)...)
}
Ejemplo n.º 3
0
func NewPageController(paths ...string) gadget.Controller {
	pageset := NewPageSet(env.RelPath(paths...))
	return &PageController{pages: pageset, url: paths[len(paths)-1]}
}