Archived
1
0

Adds a better way to manage LaTeX resources for the book template

This commit is contained in:
Jip J. Dekker 2016-04-26 10:41:59 +02:00
parent d38ef88c9c
commit 6aefbcc6e8
3 changed files with 49 additions and 15 deletions

View File

@ -33,6 +33,8 @@ func MakeBook(path string, opts *settings.Settings) {
// Sort scores // Sort scores
sort.Sort(settings.ScoresByName{scores}) sort.Sort(settings.ScoresByName{scores})
getBookResources(opts)
templ, err := parseBookTemplate(opts) templ, err := parseBookTemplate(opts)
texPath := filepath.Join(opts.OutputDir, opts.Name+".tex") texPath := filepath.Join(opts.OutputDir, opts.Name+".tex")
@ -62,18 +64,7 @@ func MakeBook(path string, opts *settings.Settings) {
}).Fatal("songbook failed to compile") }).Fatal("songbook failed to compile")
} }
cmd = exec.Command("latexmk", "-c", "-cd", texPath) cleanBookResources(texPath, opts)
out, err = cmd.CombinedOutput()
if err != nil {
log.WithFields(log.Fields{
"message": string(out),
"error": err,
}).Error("failed to clean songbook latex files")
}
if !opts.KeepBookTemplate {
err = os.Remove(texPath)
}
helpers.Check(err, "could not remove songbook latex template")
} }
// scoreCategories returns a sorted slice of all categories used // scoreCategories returns a sorted slice of all categories used
@ -103,3 +94,44 @@ func unknownCategories(scores *[]settings.Score) bool {
} }
return false return false
} }
// getBookResources copies the LaTeX resources to the compile directory
func getBookResources(opts *settings.Settings) {
for i := range opts.LatexResources {
log.WithFields(log.Fields{"path": opts.LatexResources[i]}).Debug("copying latex resource")
err := os.Link(opts.LatexResources[i], filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i])))
if err != nil {
log.WithError(err).Warning("could not link LaTeX resource")
}
}
}
// cleanBookResources removes both LaTeX generated files and copied resources
func cleanBookResources(bookpath string, opts *settings.Settings) {
var err error
log.Debug("removing LaTeX resources")
for i := range opts.LatexResources {
err = os.RemoveAll(filepath.Join(opts.OutputDir, filepath.Base(opts.LatexResources[i])))
if err != nil {
helpers.Check(err, "could not remove latex resource")
}
}
log.Debug("removing LaTeX generated files")
cmd := exec.Command("latexmk", "-c", "-cd", bookpath)
out, err := cmd.CombinedOutput()
if err != nil {
log.WithFields(log.Fields{
"message": string(out),
"error": err,
}).Error("failed to clean songbook latex files")
}
if !opts.KeepBookTemplate {
log.Debug("removing LaTeX template")
err = os.Remove(bookpath)
}
helpers.Check(err, "could not remove songbook latex template")
}

View File

@ -18,9 +18,7 @@ import log "github.com/Sirupsen/logrus"
// Crash outputs it's arguments to the log and stops the program // Crash outputs it's arguments to the log and stops the program
func Crash(err error, msg string) { func Crash(err error, msg string) {
log.WithFields(log.Fields{ log.WithError(err).Fatal(msg)
"error": err,
}).Fatal(msg)
} }
// Check calls Crash if the error is not nil // Check calls Crash if the error is not nil

View File

@ -36,6 +36,7 @@ type Settings struct {
BookTitleTempl string // Override for the partial book template creating the title page BookTitleTempl string // Override for the partial book template creating the title page
BookCategoryTempl string // Override for the partial book template creating category pages BookCategoryTempl string // Override for the partial book template creating category pages
BookScoreTempl string // Override for the partial book template placing scores 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
} }
@ -64,5 +65,8 @@ func (s *Settings) AbsolutePaths(root string) {
for i := range s.LilypondIncludes { for i := range s.LilypondIncludes {
s.LilypondIncludes[i] = helpers.AbsolutePath(s.LilypondIncludes[i], root) s.LilypondIncludes[i] = helpers.AbsolutePath(s.LilypondIncludes[i], root)
} }
for i := range s.LatexResources {
s.LatexResources[i] = helpers.AbsolutePath(s.LatexResources[i], root)
}
s.OutputDir = helpers.AbsolutePath(s.OutputDir, root) s.OutputDir = helpers.AbsolutePath(s.OutputDir, root)
} }