Archived
1
0

Splits book template into a seperate file

This commit is contained in:
Jip J. Dekker 2016-04-14 21:22:59 +02:00
parent 2cb52ef198
commit cf8aa0aabd
2 changed files with 77 additions and 43 deletions

View File

@ -21,48 +21,10 @@ import (
"sort"
log "github.com/Sirupsen/logrus"
"github.com/alecthomas/template"
"github.com/jjdekker/ponder/helpers"
"github.com/jjdekker/ponder/settings"
)
// TODO: Add git version
// TODO: Support multiple authors
// TODO: Support categories
var bookTempl = `
\documentclass[11pt,fleqn]{book}
\usepackage[utf8]{inputenc}
\usepackage{pdfpages}
\usepackage[space]{grffile}
\usepackage{hyperref}
{{if ne .Settings.Name ""}}\\title{ {{.Settings.Name}} }{{end}}
{{if ne .Settings.Author ""}}\\author{ {{.Settings.Author}} }{{end}}
\date{\today}
\begin{document}
\maketitle
{{range $i, $cat := .Categories}}
\chapter{{printf "{"}}{{ . }}{{printf "}"}}
\newpage
{{range $.Scores}}{{if in $cat .Categories }}
\phantomsection
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}}
{{end}}{{end}}{{end}}
{{if not .Settings.HideUncategorized }}{{ if unknown .Scores }}
\chapter{{printf "{"}}{{ if ne .Settings.UncategorizedChapter "" }}{{.Settings.UncategorizedChapter}}{{else}}Others{{end}}{{printf "}"}} \newpage {{end}}
{{range .Scores}}
{{ if eq (len .Categories) 0 }}
\phantomsection
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}}
{{end}}{{end}}{{end}}
\end{document}
`
// MakeBook will combine all scores into a single songbook
// generated using LaTeX.
func MakeBook(path string, opts *settings.Settings) {
@ -70,11 +32,8 @@ func MakeBook(path string, opts *settings.Settings) {
CompileDir(path, opts)
// Sort scores
sort.Sort(settings.ScoresByName{scores})
// Compile the book template
var templ = template.Must(template.New("songBook").Funcs(template.FuncMap{
"in": helpers.InSlice,
"unknown": unknownCategories,
}).Parse(bookTempl))
templ, err := parseBookTemplate(opts)
texPath := filepath.Join(opts.OutputDir, "songbook.tex")
log.WithFields(log.Fields{

75
compiler/book_template.go Normal file
View File

@ -0,0 +1,75 @@
// Copyright © 2016 Jip J. Dekker <jip@dekker.li>
//
// 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 compiler
import (
"text/template"
log "github.com/Sirupsen/logrus"
"github.com/jjdekker/ponder/helpers"
"github.com/jjdekker/ponder/settings"
)
func parseBookTemplate(opts *settings.Settings) (t *template.Template, err error) {
t = template.New("Songbook")
t.Funcs(template.FuncMap{
"in": helpers.InSlice,
"unknown": unknownCategories,
})
t, err = t.Parse(bookTempl)
if err != nil {
log.WithFields(log.Fields{
"template": t,
"source": bookTempl,
"error": err,
}).Fatal("songbook template failed to compile")
}
return
}
const bookTempl = `
\documentclass[11pt,fleqn]{book}
\usepackage[utf8]{inputenc}
\usepackage{pdfpages}
\usepackage[space]{grffile}
\usepackage{hyperref}
{{if ne .Settings.Name ""}}\\title{ {{.Settings.Name}} }{{end}}
{{if ne .Settings.Author ""}}\\author{ {{.Settings.Author}} }{{end}}
\date{\today}
\begin{document}
\maketitle
{{range $i, $cat := .Categories}}
\chapter{{printf "{"}}{{ . }}{{printf "}"}}
\newpage
{{range $.Scores}}{{if in $cat .Categories }}
\phantomsection
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}}
{{end}}{{end}}{{end}}
{{if not .Settings.HideUncategorized }}{{ if unknown .Scores }}
\chapter{{printf "{"}}{{ if ne .Settings.UncategorizedChapter "" }}{{.Settings.UncategorizedChapter}}{{else}}Others{{end}}{{printf "}"}} \newpage {{end}}
{{range .Scores}}
{{ if eq (len .Categories) 0 }}
\phantomsection
\addcontentsline{toc}{section}{{printf "{"}}{{ .Name }}{{printf "}"}}
\includepdf[pages=-]{{printf "{"}}{{.OutputPath}}{{printf "}"}}
{{end}}{{end}}{{end}}
\end{document}
`