From af1e127f37aeab4e82028ffa2bffbe6871598476 Mon Sep 17 00:00:00 2001 From: "Jip J. Dekker" Date: Wed, 13 Apr 2016 22:05:20 +0200 Subject: [PATCH] Adds basic handling of Categories in the book --- compiler/book.go | 38 +++++++++++++++++++++++++++++++++----- helpers/template_checks.go | 25 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 helpers/template_checks.go diff --git a/compiler/book.go b/compiler/book.go index bdf95e0..d96e04f 100644 --- a/compiler/book.go +++ b/compiler/book.go @@ -34,7 +34,7 @@ var bookTempl = ` \usepackage[utf8]{inputenc} \usepackage{pdfpages} \usepackage[space]{grffile} -\usepackage[pdftex,pdfpagelabels,bookmarks,hyperindex,hyperfigures]{hyperref} +\usepackage{hyperref} {{if ne .Settings.Name ""}}\\title{ {{.Settings.Name}} }{{end}} {{if ne .Settings.Author ""}}\\author{ {{.Settings.Author}} }{{end}} @@ -43,14 +43,26 @@ var bookTempl = ` \begin{document} \maketitle +{{range $i, $cat := .Categories}} +\chapter{{printf "{"}}{{ . }}{{printf "}"}} \newpage - -{{range .Scores}} +{{range $.Scores}} +{{if in $cat .Categories }} \phantomsection \addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}} \includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}} {{end}} +{{end}} +{{end}} +{{ if unknown .Scores }} \chapter{Others} \newpage {{end}} +{{range .Scores}} +{{ if eq (len .Categories) 0 }} +\phantomsection +\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}} +\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}} +{{end}} +{{end}} \end{document} ` @@ -60,7 +72,10 @@ func MakeBook(path string, opts *settings.Settings) { // Everything needs to be compiled CompileDir(path, opts) // Compile the book template - var templ = template.Must(template.New("songBook").Parse(bookTempl)) + var templ = template.Must(template.New("songBook").Funcs(template.FuncMap{ + "in": helpers.InSlice, + "unknown": unknownCategories, + }).Parse(bookTempl)) texPath := filepath.Join(opts.OutputDir, "songbook.tex") log.WithFields(log.Fields{ @@ -71,9 +86,11 @@ func MakeBook(path string, opts *settings.Settings) { err = templ.Execute(f, &struct { Scores *[]settings.Score Settings *settings.Settings + Categories []string }{ Scores: &scores, Settings: opts, + Categories: scoreCategories(&scores), }) helpers.Check(err, "error executing book template") f.Close() @@ -95,7 +112,7 @@ func MakeBook(path string, opts *settings.Settings) { "error": err, }).Error("failed to clean songbook latex files") } - err = os.Remove(texPath) + // err = os.Remove(texPath) helpers.Check(err, "could not remove songbook latex template") } @@ -115,3 +132,14 @@ func scoreCategories(scores *[]settings.Score) []string { sort.Strings(categories) return categories } + +// unknownCategories returns true if the slice contains any scores with +// unknown categories +func unknownCategories(scores *[]settings.Score) bool { + for i := range *scores { + if len((*scores)[i].Categories) == 0 { + return true + } + } + return false +} diff --git a/helpers/template_checks.go b/helpers/template_checks.go new file mode 100644 index 0000000..648efbc --- /dev/null +++ b/helpers/template_checks.go @@ -0,0 +1,25 @@ +// Copyright © 2016 Jip J. Dekker +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package helpers + +// InSlice returns true if one of the elements in the slice a equals s +func InSlice(s string, a []string) bool { + for i := range a { + if a[i] == s { + return true + } + } + return false +}