Skip to content

Commit a072e6d

Browse files
author
Maciej Lechowski
committed
Allow to include additional files into generated html
1 parent bdb0175 commit a072e6d

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

project.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ func getValueFromSource(source string, pattern *regexp.Regexp) string {
9494
return ""
9595
}
9696

97+
func getArticles(files []os.FileInfo, postsDir string, entryType int) []sourceFile {
98+
var articles []sourceFile
99+
100+
for _, postFile := range files {
101+
name := postFile.Name()
102+
ext := name[len(name)-3:]
103+
if ext == ".md" {
104+
data := parseSourceFile(filepath.Join(postsDir, name), entryType)
105+
articles = append(articles, data)
106+
}
107+
}
108+
109+
return articles
110+
}
111+
97112
func parseSourceFile(path string, entryType int) sourceFile {
98113
f, _ := ioutil.ReadFile(path)
99114
source := string(f)
@@ -206,6 +221,21 @@ func generateArticleHtml(project string, theme string, templateFile string, data
206221
output = strings.Replace(output, "{{#include "+templateName+"}}", templateContent, -1)
207222
}
208223

224+
extraPath := regexp.MustCompile("{{#include (.+)}}")
225+
226+
matches := extraPath.FindAllStringSubmatch(output, -1)
227+
228+
for _, match := range matches {
229+
path := filepath.Join(project, "source", match[1])
230+
f, err := ioutil.ReadFile(path)
231+
232+
if err != nil {
233+
continue
234+
}
235+
236+
output = strings.Replace(output, match[0], string(f), -1)
237+
}
238+
209239
for _, article := range articles {
210240
output = strings.Replace(output, "{{@"+article.slug+"}}", createLink(article), -1)
211241
}
@@ -361,17 +391,10 @@ func buildProject(project string, conf *config) {
361391
templates[templateFile.Name()] = string(f)
362392
}
363393

364-
var articles []sourceFile
394+
postsArticles := getArticles(postsFiles, postsDir, POST)
395+
pagesArticles := getArticles(pagesFiles, pagesDir, PAGE)
365396

366-
for _, postFile := range postsFiles {
367-
data := parseSourceFile(filepath.Join(postsDir, postFile.Name()), POST)
368-
articles = append(articles, data)
369-
}
370-
371-
for _, pageFile := range pagesFiles {
372-
data := parseSourceFile(filepath.Join(pagesDir, pageFile.Name()), PAGE)
373-
articles = append(articles, data)
374-
}
397+
articles := append(postsArticles, pagesArticles...)
375398

376399
indexData := parseSourceFile(filepath.Join(project, "source", "index.md"), INDEX)
377400
articles = append(articles, indexData)

0 commit comments

Comments
 (0)