change code that works with filenames

This commit is contained in:
Kirill Zhuravlev 2023-02-04 02:39:51 +01:00 committed by Avelino
parent 7d9557f198
commit a0f6a55ba1
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A
3 changed files with 86 additions and 25 deletions

View File

@ -3,8 +3,8 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath"
"strings" "strings"
"text/template" "text/template"
@ -25,15 +25,30 @@ type Object struct {
Items []Link Items []Link
} }
// Source
const readmePath = "README.md"
// Templates
const tmplCategory = "tmpl/cat-tmpl.html"
const tmplSitemap = "tmpl/sitemap-tmpl.xml"
// Output
const outDir = "out/"
const outIndexFile = "index.html"
const outSitemapFile = "sitemap.xml"
func main() { func main() {
err := GenerateHTML() outIndexAbs := filepath.Join(outDir, outIndexFile)
err := GenerateHTML(readmePath, outIndexAbs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
input, err := os.ReadFile("./tmpl/index.html")
input, err := os.ReadFile(outIndexAbs)
if err != nil { if err != nil {
panic(err) panic(err)
} }
buf := bytes.NewBuffer(input) buf := bytes.NewBuffer(input)
query, err := goquery.NewDocumentFromReader(buf) query, err := goquery.NewDocumentFromReader(buf)
if err != nil { if err != nil {
@ -55,29 +70,61 @@ func main() {
}) })
}) })
makeSiteStruct(objs) if err := makeSiteStruct(objs); err != nil {
// FIXME: remove all panics
panic(err)
}
changeLinksInIndex(string(input), query, objs) changeLinksInIndex(string(input), query, objs)
makeSitemap(objs) makeSitemap(objs)
} }
func makeSiteStruct(objs map[string]*Object) { func mkdirAll(path string) error {
_, err := os.Stat(path)
// NOTE: directory is exists
if err == nil {
return nil
}
// NOTE: unknown error
if !os.IsNotExist(err) {
return err
}
// NOTE: directory is not exists
if err := os.MkdirAll(path, 0o755); err != nil {
return err
}
return nil
}
func makeSiteStruct(objs map[string]*Object) error {
for _, obj := range objs { for _, obj := range objs {
folder := fmt.Sprintf("tmpl/%s", obj.Slug) outDir := filepath.Join(outDir, obj.Slug)
err := os.Mkdir(folder, 0755) if err := mkdirAll(outDir); err != nil {
if err != nil { return err
log.Println(err)
} }
t := template.Must(template.ParseFiles("tmpl/cat-tmpl.html")) // FIXME: embed templates
f, _ := os.Create(fmt.Sprintf("%s/index.html", folder)) // FIXME: parse templates once at start
t.Execute(f, obj) t := template.Must(template.ParseFiles(tmplCategory))
f, err := os.Create(filepath.Join(outDir, "index.html"))
if err != nil {
return err
}
if err := t.Execute(f, obj); err != nil {
return err
}
} }
return nil
} }
func makeSitemap(objs map[string]*Object) { func makeSitemap(objs map[string]*Object) {
t := template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml")) t := template.Must(template.ParseFiles(tmplSitemap))
f, _ := os.Create("tmpl/sitemap.xml") f, _ := os.Create(filepath.Join(outDir, outSitemapFile))
t.Execute(f, objs) t.Execute(f, objs)
} }
@ -129,5 +176,5 @@ func changeLinksInIndex(html string, query *goquery.Document, objs map[string]*O
} }
}) })
os.WriteFile("./tmpl/index.html", []byte(html), 0644) os.WriteFile(filepath.Join(outDir, outIndexFile), []byte(html), 0644)
} }

View File

@ -70,7 +70,7 @@ func TestSeparator(t *testing.T) {
} }
} }
func TestGenerateHTML(t *testing.T) { func TestGenerateHTML(t *testing.T) {
err := GenerateHTML() err := GenerateHTML(readmePath, outIndexFile)
if err != nil { if err != nil {
t.Errorf("html generate error '%s'", err.Error()) t.Errorf("html generate error '%s'", err.Error())
} }

View File

@ -36,16 +36,30 @@ type content struct {
} }
// GenerateHTML generate site html (index.html) from markdown file // GenerateHTML generate site html (index.html) from markdown file
func GenerateHTML() (err error) { func GenerateHTML(srcFilename, outFilename string) error {
// options // options
readmePath := "./README.md" const tplPath = "tmpl/tmpl.html"
tplPath := "tmpl/tmpl.html"
idxPath := "tmpl/index.html" input, err := ioutil.ReadFile(srcFilename)
input, _ := ioutil.ReadFile(readmePath) if err != nil {
body, _ := markdown.ConvertMarkdownToHTML(input) return err
}
body, err := markdown.ConvertMarkdownToHTML(input)
if err != nil {
return err
}
c := &content{Body: template.HTML(body)} c := &content{Body: template.HTML(body)}
t := template.Must(template.ParseFiles(tplPath)) t := template.Must(template.ParseFiles(tplPath))
f, err := os.Create(idxPath) f, err := os.Create(outFilename)
t.Execute(f, c) if err != nil {
return return err
}
if err := t.Execute(f, c); err != nil {
return err
}
return nil
} }