the most working-ish current version
This commit is contained in:
parent
aad872d077
commit
f7f0140bc5
192
main.go
192
main.go
|
@ -24,11 +24,6 @@ var htmlOutputDir string = filepath.Join(root, "public_html")
|
|||
var inputDir string = filepath.Join(root, "content")
|
||||
var tagFile string = filepath.Join(root, "tags.json")
|
||||
|
||||
const (
|
||||
GEMINI = iota
|
||||
HTML
|
||||
)
|
||||
|
||||
type filePathInfo struct {
|
||||
Path string
|
||||
New bool // True if the file is new
|
||||
|
@ -39,6 +34,16 @@ type pageInfo struct {
|
|||
Title string
|
||||
}
|
||||
|
||||
type templateTag struct {
|
||||
Name string
|
||||
Pages []pageInfo
|
||||
} // Only used for tagpages
|
||||
|
||||
const (
|
||||
GEMINI = iota
|
||||
HTML
|
||||
)
|
||||
|
||||
// RemoveIndex is a reslicing function.
|
||||
func RemoveIndex(s []pageInfo, index int) []pageInfo {
|
||||
return append(s[:index], s[index+1:]...)
|
||||
|
@ -113,7 +118,21 @@ func processLink(o gemtext.GemtextObject, mode int) gemtext.GemtextObject {
|
|||
return o
|
||||
}
|
||||
|
||||
func processLinkString(s string, mode int) (string, error) {
|
||||
o, err := filepath.Rel(inputDir, s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if mode == GEMINI {
|
||||
o = filepath.Join(geminiPrefix, o)
|
||||
} else if mode == HTML {
|
||||
o = filepath.Join(htmlPrefix, strings.Replace(o, ".gmi", ".html", -1))
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// processPage finds a page, gets the tags, and writes it out to HTML and gemini output directories.
|
||||
// TODO BUG: somehow tagData ends up empty.
|
||||
func processPage(f filePathInfo) error {
|
||||
|
||||
// Delete any existing tag references to this file.
|
||||
|
@ -175,7 +194,6 @@ func processPage(f filePathInfo) error {
|
|||
return err
|
||||
}
|
||||
output = strings.Replace(filepath.Join(htmlOutputDir, rel), ".gmi", ".html", -1)
|
||||
fmt.Println("writing", output)
|
||||
err = ioutil.WriteFile(output, []byte(htmlWrap(&htmlRender)), 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -221,53 +239,143 @@ func walkInputDir(path string, info os.FileInfo, err error) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeTagPage writes a tag page.
|
||||
func writeTagPage(tag string, pages *[]pageInfo, t *t.Template, basePath string, linkType int) error {
|
||||
var outPath string
|
||||
if linkType == HTML {
|
||||
outPath = filepath.Join(basePath, tag+".html")
|
||||
} else {
|
||||
outPath = filepath.Join(basePath, tag+".gmi")
|
||||
}
|
||||
f, err := os.Create(filepath.Join(outPath))
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
np := make([]pageInfo, len(*pages))
|
||||
for i, p := range *pages {
|
||||
np[i].Path, err = processLinkString(p.Path, linkType)
|
||||
np[i].Title = p.Title
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = t.Execute(f, templateTag{Name: tag, Pages: np})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.Sync()
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
loadingJson := true
|
||||
if _, err := os.Stat(tagFile); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
loadingJson = false
|
||||
fmt.Println("Not loading tags file, it doesn't exist yet")
|
||||
} else { panic(err) }
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
tagFileData, err := ioutil.ReadFile(tagFile)
|
||||
if loadingJson {
|
||||
err = json.Unmarshal([]byte(tagFileData), &tagData)
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
htmlTagPageTemplateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "tag.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
htmlTagPageTemplate, err := t.New("htmlTagPage").Parse(string(htmlTagPageTemplateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
geminiTagPageTemplateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "tag.gmi"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
geminiTagPageTemplate, err := t.New("geminiTagPage").Parse(string(geminiTagPageTemplateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = filepath.Walk(inputDir, walkInputDir) // walks the tree, creates the files slice and updates tagData
|
||||
if err != nil { panic(err) }
|
||||
for _, file := range files {
|
||||
err := processPage(file) // Process all the updated files.
|
||||
if err != nil { panic(err) }
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
err := processPage(file) // Process all the updated files.
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Write out index page
|
||||
templateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "index.gmi"))
|
||||
if err != nil { panic(err) }
|
||||
indexTemplate, err := t.New("gemtextIndex").Parse(string(templateFile))
|
||||
if err != nil { panic(err) }
|
||||
fmt.Println(filepath.Join(geminiOutputDir, "index.gmi"))
|
||||
f, err := os.Create(filepath.Join(geminiOutputDir, "index.gmi"))
|
||||
defer f.Close()
|
||||
if err != nil { panic(err) }
|
||||
err = indexTemplate.Execute(f, tagData)
|
||||
if err != nil { panic(err) }
|
||||
f.Sync()
|
||||
|
||||
// Write out HTML index page
|
||||
templateFile, err = ioutil.ReadFile(filepath.Join(templateDir, "index.html"))
|
||||
if err != nil { panic(err) }
|
||||
indexTemplate, err = t.New("htmlIndex").Parse(string(templateFile))
|
||||
if err != nil { panic(err) }
|
||||
fmt.Println(filepath.Join(htmlOutputDir, "index.html"))
|
||||
hf, err := os.Create(filepath.Join(htmlOutputDir, "index.html"))
|
||||
defer hf.Close()
|
||||
if err != nil { panic(err) }
|
||||
err = indexTemplate.Execute(hf, tagData)
|
||||
if err != nil { panic(err) }
|
||||
hf.Sync()
|
||||
// TODO tag pages
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(htmlOutputDir, "_tags"), 0664); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
if err := os.Mkdir(filepath.Join(geminiOutputDir, "_tags"), 0664); err != nil {
|
||||
if !os.IsExist(err) {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
// Write out index page
|
||||
templateFile, err := ioutil.ReadFile(filepath.Join(templateDir, "index.gmi"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
indexTemplate, err := t.New("gemtextIndex").Parse(string(templateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
f, err := os.Create(filepath.Join(geminiOutputDir, "index.gmi"))
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = indexTemplate.Execute(f, tagData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
f.Sync()
|
||||
|
||||
json, err := json.Marshal(tagData)
|
||||
err = ioutil.WriteFile(tagFile, json, 0644)
|
||||
if err != nil { panic(err) }
|
||||
}
|
||||
// Write out HTML index page
|
||||
templateFile, err = ioutil.ReadFile(filepath.Join(templateDir, "index.html"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
indexTemplate, err = t.New("htmlIndex").Parse(string(templateFile))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hf, err := os.Create(filepath.Join(htmlOutputDir, "index.html"))
|
||||
defer hf.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = indexTemplate.Execute(hf, tagData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
hf.Sync()
|
||||
// Write tag pages
|
||||
for tag, pages := range tagData {
|
||||
err = writeTagPage(tag, &pages, htmlTagPageTemplate, filepath.Join(htmlOutputDir, "/_tags/"), HTML)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = writeTagPage(tag, &pages, geminiTagPageTemplate, filepath.Join(geminiOutputDir, "/_tags/"), GEMINI)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
json, err := json.Marshal(tagData)
|
||||
err = ioutil.WriteFile(tagFile, json, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue