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