diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 41 |
1 files changed, 39 insertions, 2 deletions
| @@ -94,6 +94,27 @@ func cleanHTMLTags(htmlString string) string { | |||
| 94 | return cleanString | 94 | return cleanString |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | func includeTemplateList(projectRoot string) []string { | ||
| 98 | var templateFiles []string | ||
| 99 | includesTemplatePathname := path.Join(projectRoot, "templates/includes") | ||
| 100 | err := filepath.Walk(includesTemplatePathname, func(path string, info os.FileInfo, err error) error { | ||
| 101 | if err != nil { | ||
| 102 | return err | ||
| 103 | } | ||
| 104 | if filepath.Ext(path) == ".html" { | ||
| 105 | templateFiles = append(templateFiles, path) | ||
| 106 | } | ||
| 107 | |||
| 108 | return nil | ||
| 109 | }) | ||
| 110 | |||
| 111 | if err != nil { | ||
| 112 | panic(err) | ||
| 113 | } | ||
| 114 | |||
| 115 | return templateFiles | ||
| 116 | } | ||
| 117 | |||
| 97 | func simpleServer(projectRoot string) { | 118 | func simpleServer(projectRoot string) { |
| 98 | fs := http.FileServer(http.Dir(path.Join(projectRoot, "public"))) | 119 | fs := http.FileServer(http.Dir(path.Join(projectRoot, "public"))) |
| 99 | http.Handle("/", fs) | 120 | http.Handle("/", fs) |
| @@ -109,6 +130,11 @@ func initializeProject(projectRoot string) { | |||
| 109 | return | 130 | return |
| 110 | } | 131 | } |
| 111 | 132 | ||
| 133 | if err := os.Mkdir(path.Join(projectRoot, "templates", "includes"), 0755); err != nil && !os.IsExist(err) { | ||
| 134 | log.Println("Error creating directory:", err) | ||
| 135 | return | ||
| 136 | } | ||
| 137 | |||
| 112 | if err := os.Mkdir(path.Join(projectRoot, "content"), 0755); err != nil && !os.IsExist(err) { | 138 | if err := os.Mkdir(path.Join(projectRoot, "content"), 0755); err != nil && !os.IsExist(err) { |
| 113 | log.Println("Error creating directory:", err) | 139 | log.Println("Error creating directory:", err) |
| 114 | return | 140 | return |
| @@ -253,7 +279,12 @@ func buildProject(projectRoot string) { | |||
| 253 | pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string)) | 279 | pageTemplateFilename := fmt.Sprintf("%s.html", page.Meta["type"].(string)) |
| 254 | templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename) | 280 | templatePathname := path.Join(projectRoot, "templates", pageTemplateFilename) |
| 255 | baseTemplatePathname := path.Join(projectRoot, "templates/base.html") | 281 | baseTemplatePathname := path.Join(projectRoot, "templates/base.html") |
| 256 | t, err := template.ParseFiles(baseTemplatePathname, templatePathname) | 282 | |
| 283 | templates := includeTemplateList(projectRoot) | ||
| 284 | templates = append([]string{templatePathname}, templates...) | ||
| 285 | templates = append([]string{baseTemplatePathname}, templates...) | ||
| 286 | |||
| 287 | t, err := template.ParseFiles(templates...) | ||
| 257 | if err != nil { | 288 | if err != nil { |
| 258 | panic(err) | 289 | panic(err) |
| 259 | } | 290 | } |
| @@ -293,10 +324,16 @@ func buildProject(projectRoot string) { | |||
| 293 | 324 | ||
| 294 | // Generates index page. | 325 | // Generates index page. |
| 295 | { | 326 | { |
| 327 | |||
| 296 | log.Println("Writing index...") | 328 | log.Println("Writing index...") |
| 297 | templatePathname := path.Join(projectRoot, "templates/index.html") | 329 | templatePathname := path.Join(projectRoot, "templates/index.html") |
| 298 | baseTemplatePathname := path.Join(projectRoot, "templates/base.html") | 330 | baseTemplatePathname := path.Join(projectRoot, "templates/base.html") |
| 299 | t, err := template.ParseFiles(baseTemplatePathname, templatePathname) | 331 | |
| 332 | templates := includeTemplateList(projectRoot) | ||
| 333 | templates = append([]string{templatePathname}, templates...) | ||
| 334 | templates = append([]string{baseTemplatePathname}, templates...) | ||
| 335 | |||
| 336 | t, err := template.ParseFiles(templates...) | ||
| 300 | if err != nil { | 337 | if err != nil { |
| 301 | panic(err) | 338 | panic(err) |
| 302 | } | 339 | } |
