mirror of
https://github.com/avelino/awesome-go.git
synced 2025-01-31 04:48:53 +00:00
change code that works with filenames
This commit is contained in:
parent
7d9557f198
commit
a0f6a55ba1
@ -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 {
|
||||||
for _, obj := range objs {
|
_, err := os.Stat(path)
|
||||||
folder := fmt.Sprintf("tmpl/%s", obj.Slug)
|
// NOTE: directory is exists
|
||||||
err := os.Mkdir(folder, 0755)
|
if err == nil {
|
||||||
if err != nil {
|
return nil
|
||||||
log.Println(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t := template.Must(template.ParseFiles("tmpl/cat-tmpl.html"))
|
// NOTE: unknown error
|
||||||
f, _ := os.Create(fmt.Sprintf("%s/index.html", folder))
|
if !os.IsNotExist(err) {
|
||||||
t.Execute(f, obj)
|
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 {
|
||||||
|
outDir := filepath.Join(outDir, obj.Slug)
|
||||||
|
if err := mkdirAll(outDir); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: embed templates
|
||||||
|
// FIXME: parse templates once at start
|
||||||
|
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)
|
||||||
}
|
}
|
@ -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())
|
||||||
}
|
}
|
32
scripts.go
32
scripts.go
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user