finstr/initer/initer.go

137 lines
3.2 KiB
Go
Raw Permalink Normal View History

2019-01-01 19:31:14 +01:00
package initer
import (
"flag"
"os"
"path/filepath"
2024-06-18 22:39:16 +02:00
goutil "git.fireandbrimst.one/aw/goutil/misc"
2019-01-01 19:31:14 +01:00
)
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}}
2020-05-25 21:44:32 +02:00
{{if .Images}}{{range $index, $img := .Images}}<a href="../{{$img.FilenameMedium}}"><img src="../{{$img.FilenameSmall}}" class="gallery" /></a>{{if $.Artsy}}<br><br>{{end}}{{end}}{{end}}
2019-01-01 19:31:14 +01:00
</div>
<br><br>
<a href="{{.Back.Path}}">{{.Back.Name}}</a>
</body>
</html>
`
const mdTemplate = `---
title: {{.Title}}
---
2020-05-25 21:44:32 +02:00
<div class="gallery">
2019-01-01 19:31:14 +01:00
{{if .Links}}{{range $index, $link := .Links}}<a href="{{$link.Path}}">{{$link.Name}}</a><br>{{if $link.Space}}<br>{{end}}{{end}}{{end}}
2020-05-25 21:44:32 +02:00
{{if .Images}}{{range $index, $img := .Images}}<a href="../{{$img.FilenameMedium}}"><img src="../{{$img.FilenameSmall}}" class="gallery" /></a>{{if $.Artsy}}<br><br>{{end}}{{end}}{{end}}
</div>
2019-01-01 19:31:14 +01:00
<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) })
2024-06-18 22:39:16 +02:00
err = goutil.OptDo(err, func() error { return goutil.WriteFile(readme(dir), "Put your .jpgs and .tags here\n") })
2019-01-01 19:31:14 +01:00
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)
}
}