Put files in subdirectories, bootstrap css

This commit is contained in:
Johannes Rothe 2022-09-12 21:38:04 +02:00
parent 83d55a2fe5
commit d291d281a2

33
main.go
View File

@ -38,15 +38,22 @@ func (n *Node) writeIndex(w io.Writer) {
<ul> <ul>
{{ range $index, $element := .Children}} {{ range $index, $element := .Children}}
<li> <li>
<a href="{{$element.Data.Id}}.html">{{ $element.Data.Title }}</a> <a href="pages/{{$element.Data.Id}}.html">{{ $element.Data.Title }}</a>
{{ template "tree" $element }} {{ template "tree" $element }}
</li> </li>
{{ end }} {{ end }}
</ul> </ul>
{{ end }} {{ end }}
{{ end }} {{ end }}
<html> <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
</head>
<body>
{{ template "tree" . }} {{ template "tree" . }}
</body>
</html> </html>
`) `)
if err != nil { if err != nil {
@ -84,13 +91,28 @@ func (n *Node) addChild(child *Node) {
} }
func writeHTML(n *Node) { func writeHTML(n *Node) {
os.Chdir("pages")
f, err := os.Create(n.Data.Id + ".html") f, err := os.Create(n.Data.Id + ".html")
if err != nil { if err != nil {
log.Fatalf("Error creating file for ID %v", n.Data.Id) log.Fatalf("Error creating file for ID %v", n.Data.Id)
} }
defer f.Close() defer f.Close()
log.Printf("Writing file %v", f.Name()) log.Printf("Writing file %v containing page: %v", f.Name(), n.Data.Title)
f.Write([]byte(n.Data.Body)) html := `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
</head>
<body>
`
html += n.Data.Body
html += "</body></html>"
_, err = f.Write([]byte(html))
if err != nil {
log.Fatalf("Error writing file for ID %v", n.Data.Id)
}
} }
type spaceResult struct { type spaceResult struct {
@ -310,6 +332,9 @@ func main() {
root := createPageTree(&result) root := createPageTree(&result)
fname := "index.html" fname := "index.html"
os.Mkdir("content", 0744)
os.Mkdir("content/pages", 0744)
os.Chdir("content")
f, err := os.Create(fname) f, err := os.Create(fname)
if err != nil { if err != nil {
log.Fatalf("Failed to open %v", fname) log.Fatalf("Failed to open %v", fname)