132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
package initer
|
|
|
|
import (
|
|
"flag"
|
|
"git.gutmet.org/goutil.git"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
const htmlTemplate = `<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>{{.Title}}</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<style>
|
|
body {
|
|
font-family: Open Sans,Clear Sans,Verdana,Helvetica,Arial;
|
|
font-size : 12pt;
|
|
max-width: 850px;
|
|
margin-left: auto;
|
|
margin-right: auto;
|
|
background-color : black;
|
|
color: #E8E8E8;
|
|
text-align:justify;
|
|
}
|
|
|
|
a {
|
|
color : #0D0;
|
|
}
|
|
a:hover {
|
|
color : red;
|
|
}
|
|
img {
|
|
max-width: 100%;
|
|
}
|
|
div.gallery {
|
|
text-align: left;
|
|
}
|
|
h1 {
|
|
text-align: left;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>{{.Title}}</h1>
|
|
<div class="gallery">
|
|
|
|
{{if .Links}}{{range $index, $link := .Links}}<a href="{{$link.Path}}">{{$link.Name}}</a><br>{{if $link.Space}}<br>{{end}}{{end}}{{end}}
|
|
|
|
{{if .Images}}{{range $index, $img := .Images}}<a href="../{{$img.FilenameMedium}}"><img src="../{{$img.FilenameSmall}}" /></a>{{if $.Artsy}}<br><br>{{end}}{{end}}{{end}}
|
|
|
|
</div>
|
|
<br><br>
|
|
<a href="{{.Back.Path}}">{{.Back.Name}}</a>
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
const mdTemplate = `---
|
|
title: {{.Title}}
|
|
---
|
|
{{if .Links}}{{range $index, $link := .Links}}<a href="{{$link.Path}}">{{$link.Name}}</a><br>{{if $link.Space}}<br>{{end}}{{end}}{{end}}
|
|
|
|
{{if .Images}}{{range $index, $img := .Images}}<a href="../{{$img.FilenameMedium}}"><img src="../{{$img.FilenameSmall}}" /></a>{{if $.Artsy}}<br><br>{{end}}{{end}}{{end}}
|
|
<br><br>
|
|
<a href="{{.Back.Path}}">{{.Back.Name}}</a>
|
|
`
|
|
|
|
type IniterFlags struct {
|
|
Dir string
|
|
Markdown bool
|
|
Artsy bool
|
|
}
|
|
|
|
func InitializedOrDie(dir string) {
|
|
paths := []string{imgFolder(dir), InitFlag(dir), TemplateFile(dir)}
|
|
for _, p := range paths {
|
|
if !goutil.PathExists(p) {
|
|
panic("Not a finstr folder? Did not find " + p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func imgFolder(dir string) string {
|
|
return filepath.Join(dir, "img")
|
|
}
|
|
|
|
func InitFlag(dir string) string {
|
|
return filepath.Join(dir, ".finstr")
|
|
}
|
|
|
|
func readme(dir string) string {
|
|
return filepath.Join(imgFolder(dir), "Readme")
|
|
}
|
|
|
|
func TemplateFile(dir string) string {
|
|
return filepath.Join(dir, "template")
|
|
}
|
|
|
|
func Init(fl IniterFlags) error {
|
|
dir := fl.Dir
|
|
err := os.MkdirAll(imgFolder(dir), 0755)
|
|
var style string
|
|
if fl.Artsy {
|
|
style = "artsy"
|
|
} else {
|
|
style = "square"
|
|
}
|
|
var template string
|
|
if fl.Markdown {
|
|
template = mdTemplate
|
|
} else {
|
|
template = htmlTemplate
|
|
}
|
|
err = goutil.OptDo(err, func() error { return goutil.WriteFile(InitFlag(dir), style) })
|
|
err = goutil.OptDo(err, func() error { return goutil.WriteFile(readme(dir), "Put your .jpgs and .tags here") })
|
|
err = goutil.OptDo(err, func() error { return goutil.WriteFile(TemplateFile(dir), template) })
|
|
return err
|
|
}
|
|
|
|
func Command() (goutil.CommandFlagsInit, goutil.CommandFunc) {
|
|
f := IniterFlags{}
|
|
flagsInit := func(s *flag.FlagSet) {
|
|
s.BoolVar(&f.Markdown, "markdown", false, "init with Markdown template instead of HTML")
|
|
s.StringVar(&f.Dir, "dir", "", "initialize 'dir' instead of current working directory")
|
|
s.BoolVar(&f.Artsy, "artsy", false, "mark gallery to generate thumbnails with original ratio instead of square")
|
|
}
|
|
return flagsInit, func([]string) error {
|
|
return Init(f)
|
|
}
|
|
}
|