Use templates in files instead of strings in JSON
This commit is contained in:
parent
5bd2912d13
commit
554aa82ecb
@ -15,6 +15,7 @@
|
|||||||
package compiler
|
package compiler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
@ -30,10 +31,14 @@ func parseBookTemplate(opts *settings.Settings) (t *template.Template, err error
|
|||||||
"unknown": unknownCategories,
|
"unknown": unknownCategories,
|
||||||
})
|
})
|
||||||
|
|
||||||
parsePartialTemplate(t.New("Packages"), opts.BookPackagesTempl, packagesTempl)
|
parsePartialTemplate(t.New("packages.tex.tmpl"),
|
||||||
parsePartialTemplate(t.New("Title"), opts.BookTitleTempl, titleTempl)
|
filepath.Join(opts.BookTemplateDir, "packages.tex.tmpl"), packagesTempl)
|
||||||
parsePartialTemplate(t.New("Category"), opts.BookCategoryTempl, categoryTempl)
|
parsePartialTemplate(t.New("title.tex.tmpl"),
|
||||||
parsePartialTemplate(t.New("Score"), opts.BookScoreTempl, scoreTempl)
|
filepath.Join(opts.BookTemplateDir, "title.tex.tmpl"), titleTempl)
|
||||||
|
parsePartialTemplate(t.New("category.tex.tmpl"),
|
||||||
|
filepath.Join(opts.BookTemplateDir, "category.tex.tmpl"), categoryTempl)
|
||||||
|
parsePartialTemplate(t.New("score.tex.tmpl"),
|
||||||
|
filepath.Join(opts.BookTemplateDir, "score.tex.tmpl"), scoreTempl)
|
||||||
|
|
||||||
_, err = t.Parse(bookTempl)
|
_, err = t.Parse(bookTempl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -46,10 +51,10 @@ func parseBookTemplate(opts *settings.Settings) (t *template.Template, err error
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePartialTemplate(t *template.Template, source, fallback string) {
|
func parsePartialTemplate(t *template.Template, sourceFile, fallback string) {
|
||||||
var err error
|
var err error
|
||||||
if source != "" {
|
if helpers.Exists(sourceFile) {
|
||||||
_, err = t.Parse(source)
|
_, err = t.ParseFiles(sourceFile)
|
||||||
} else {
|
} else {
|
||||||
_, err = t.Parse(fallback)
|
_, err = t.Parse(fallback)
|
||||||
}
|
}
|
||||||
@ -61,23 +66,23 @@ func parsePartialTemplate(t *template.Template, source, fallback string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const bookTempl = `{{ template "Packages" . }}
|
const bookTempl = `{{ template "packages.tex.tmpl" . }}
|
||||||
|
|
||||||
{{if ne .Settings.Name ""}}\title{ {{.Settings.Name}} }{{end}}
|
{{if ne .Settings.Name ""}}\title{ {{.Settings.Name}} }{{end}}
|
||||||
{{if ne .Settings.Author ""}}\author{ {{.Settings.Author}} }{{end}}
|
{{if ne .Settings.Author ""}}\author{ {{.Settings.Author}} }{{end}}
|
||||||
\date{\today}
|
\date{\today}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
{{ template "Title" . }}
|
{{ template "title.tex.tmpl" . }}
|
||||||
|
|
||||||
{{range $i, $cat := .Categories}}
|
{{range $i, $cat := .Categories}}
|
||||||
{{ template "Category" . }}
|
{{ template "category.tex.tmpl" . }}
|
||||||
{{range $.Scores}}{{if in $cat .Categories }}{{template "Score" . }}{{end}}{{end}}
|
{{range $.Scores}}{{if in $cat .Categories }}{{template "score.tex.tmpl" . }}{{end}}{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if not .Settings.HideUncategorized }}{{ if unknown .Scores }}
|
{{if not .Settings.HideUncategorized }}{{ if unknown .Scores }}
|
||||||
{{ if ne .Settings.UncategorizedChapter "" }}{{$title := .Settings.UncategorizedChapter}}{{else}}{{$title := "Others"}}{{ template "Category" $title }}{{end}}
|
{{ if ne .Settings.UncategorizedChapter "" }}{{$title := .Settings.UncategorizedChapter}}{{else}}{{$title := "Others"}}{{ template "category.tex.tmpl" $title }}{{end}}
|
||||||
{{range .Scores}}{{ if eq (len .Categories) 0 }}{{template "Score" . }}{{end}}{{end}}
|
{{range .Scores}}{{ if eq (len .Categories) 0 }}{{template "score.tex.tmpl" . }}{{end}}{{end}}
|
||||||
{{end}}{{end}}
|
{{end}}{{end}}
|
||||||
\end{document}
|
\end{document}
|
||||||
`
|
`
|
||||||
|
@ -41,7 +41,8 @@ func compilePath(root string, opts *settings.Settings,
|
|||||||
// Skip directories that are ignored
|
// Skip directories that are ignored
|
||||||
relPath, err := filepath.Rel(root, path)
|
relPath, err := filepath.Rel(root, path)
|
||||||
helpers.Check(err, "Unable to create relative Path")
|
helpers.Check(err, "Unable to create relative Path")
|
||||||
for _, dir := range append(append(opts.IgnoreDirs, opts.LilypondIncludes...), opts.OutputDir) {
|
for _, dir := range append(append(append(opts.IgnoreDirs,
|
||||||
|
opts.LilypondIncludes...), opts.OutputDir), opts.BookTemplateDir) {
|
||||||
if relPath == dir || (filepath.IsAbs(dir) && path == dir) {
|
if relPath == dir || (filepath.IsAbs(dir) && path == dir) {
|
||||||
log.WithFields(log.Fields{"path": path}).Debug("ignoring directory")
|
log.WithFields(log.Fields{"path": path}).Debug("ignoring directory")
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
|
@ -32,11 +32,8 @@ type Settings struct {
|
|||||||
OutputDir string // Directory in which all complete file are stored
|
OutputDir string // Directory in which all complete file are stored
|
||||||
HideUncategorized bool // Hide scores without a category from the book
|
HideUncategorized bool // Hide scores without a category from the book
|
||||||
UncategorizedChapter string // Name of the chapter with uncategorized scores
|
UncategorizedChapter string // Name of the chapter with uncategorized scores
|
||||||
BookPackagesTempl string // Override for the partial book template declaring packages
|
BookTemplateDir string // Directory in which partial book templates are placed
|
||||||
BookTitleTempl string // Override for the partial book template creating the title page
|
LatexResources []string // Files (or directories) to be copied to compile the book template
|
||||||
BookCategoryTempl string // Override for the partial book template creating category pages
|
|
||||||
BookScoreTempl string // Override for the partial book template placing scores
|
|
||||||
LatexResources []string // Files to be copied to compile the book template
|
|
||||||
KeepBookTemplate bool // Leave the LaTeX source for the book in the output directory
|
KeepBookTemplate bool // Leave the LaTeX source for the book in the output directory
|
||||||
FlatOutputDir bool // Keep all output file in a flat output directory
|
FlatOutputDir bool // Keep all output file in a flat output directory
|
||||||
DefaultCategories []string // Categories included in the book by default
|
DefaultCategories []string // Categories included in the book by default
|
||||||
@ -76,4 +73,5 @@ func (s *Settings) AbsolutePaths(root string) {
|
|||||||
s.LatexResources[i] = helpers.AbsolutePath(s.LatexResources[i], root)
|
s.LatexResources[i] = helpers.AbsolutePath(s.LatexResources[i], root)
|
||||||
}
|
}
|
||||||
s.OutputDir = helpers.AbsolutePath(s.OutputDir, root)
|
s.OutputDir = helpers.AbsolutePath(s.OutputDir, root)
|
||||||
|
s.BookTemplateDir = helpers.AbsolutePath(s.BookTemplateDir, root)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user