From 416fa70b61bd351d1bd22c6b98bb1c30af481d09 Mon Sep 17 00:00:00 2001 From: Christof Laenzlinger <6319634+laenzlinger@users.noreply.github.com> Date: Mon, 1 Apr 2024 12:33:42 +0200 Subject: [PATCH] 3 generate suisa list (#14) * Add `generate suisa` command * Render the Suisa List * Configure margin in setlist template * docs: improve README --- Makefile | 1 + README.md | 17 ++--- cmd/generate_setlist.go | 2 + cmd/generate_suisa.go | 87 ++++++++++++++++++++++++++ cmd/root.go | 2 +- internal/html/template/generate.go | 1 + internal/html/template/setlist.html | 4 +- internal/repertoire/helper.go | 20 ++++++ internal/repertoire/repertoire.go | 17 ++++- internal/repertoire/repertoire_test.go | 3 +- internal/repertoire/song.go | 14 +---- test/Repertoire/Band/Repertoire.md | 10 +-- 12 files changed, 147 insertions(+), 31 deletions(-) create mode 100644 cmd/generate_suisa.go create mode 100644 internal/repertoire/helper.go diff --git a/Makefile b/Makefile index 9b5589e..2b5e6de 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ integration-test: clean docker-build $(RUN) generate sheet --band-name Band --all $(RUN) generate sheet --band-name Band "Grand Ole Opry" $(RUN) generate list --band-name Band "Grand Ole Opry" + $(RUN) generate suisa --band-name Band "Grand Ole Opry" ls -lart test/Repertoire/out lint: ## lint source code diff --git a/README.md b/README.md index 2ea3444..b3cb858 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,16 @@ Repertoire.md file. See [example]{test/Repertoire/Band/Repertoire.md). The Table must have a header row. The only mandatory column is the `Title` column which is used to refer to the song titles for both `generte sheet` and `generate list`. -Optional columns used by default generate output. - -| Column | Type | Used by command | -|-------------|-----------|-----------------| -| Title | Mandatory | list, cheat | -| Year | Optional | list | -| Description | Optional | list | +Optional columns used (by default) generate output. + +| Column | Type | Used by command | +|-------------|-----------|--------------------| +| Title | Mandatory | list, cheat, suisa | +| Year | Optional | list, suisa | +| Description | Optional | list | +| Arranger | Optional | suisa | +| Composer | Optional | suisa | +| Duration | Optional | suisa | The output columns can be selected by the `--include-columns` flag, but the order or the columns is defined by the input Repertoire.md diff --git a/cmd/generate_setlist.go b/cmd/generate_setlist.go index 1566666..cbd3f56 100644 --- a/cmd/generate_setlist.go +++ b/cmd/generate_setlist.go @@ -70,10 +70,12 @@ func generateSetlist(gigName string) error { content := rep.Filter(gig). IncludeColumns(include...). + NoHeader(). Render() data := tmpl.Data{ Title: gig.Name, + Margin: "0cm", Content: template.HTML(content), //nolint: gosec // not a web application } diff --git a/cmd/generate_suisa.go b/cmd/generate_suisa.go new file mode 100644 index 0000000..5ad78ba --- /dev/null +++ b/cmd/generate_suisa.go @@ -0,0 +1,87 @@ +/* +Copyright © 2024 Christof Laenzlinger + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +package cmd + +import ( + "fmt" + "html/template" + "path/filepath" + + "github.com/laenzlinger/setlist/internal/config" + "github.com/laenzlinger/setlist/internal/gig" + convert "github.com/laenzlinger/setlist/internal/html/pdf" + tmpl "github.com/laenzlinger/setlist/internal/html/template" + "github.com/laenzlinger/setlist/internal/repertoire" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +//nolint:gochecknoglobals // cobra is designed like this +var suisaCmd = &cobra.Command{ + Use: "suisa", + Short: "Generate a Suisa list", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Long: `Generates a Suisa list for a gig. +`, + Run: func(_ *cobra.Command, args []string) { + err := generateSuisalist(args[0]) + cobra.CheckErr(err) + }, +} + +//nolint:gochecknoinits // cobra is desigend like this +func init() { + generateCmd.AddCommand(suisaCmd) + + suisaCmd.Flags().StringSliceP("include-columns", "i", []string{"Title", "Year", "Composer", "Arranger", "Duration"}, + "defines the repertoire columns to include in the output") + + err := viper.BindPFlag("suisa.include-columns", suisaCmd.Flags().Lookup("include-columns")) + cobra.CheckErr(err) +} + +func generateSuisalist(gigName string) error { + include := viper.GetStringSlice("suisa.include-columns") + band := config.NewBand() + + rep, err := repertoire.New(band) + if err != nil { + return err + } + + gig, err := gig.New(band, gigName) + if err != nil { + return err + } + + content := rep.Filter(gig). + IncludeColumns(include...). + Render() + + data := tmpl.Data{ + Title: gig.Name, + Margin: "1cm", + Content: template.HTML(content), //nolint: gosec // not a web application + } + + filename, err := tmpl.CreateSetlist(&data) + if err != nil { + return err + } + + return convert.HTMLToPDF(filename, filepath.Join(config.Target(), fmt.Sprintf("Suisa List %s.pdf", gig.Name))) +} diff --git a/cmd/root.go b/cmd/root.go index 43b5132..be50250 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,7 +60,7 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $PWD/.setlist.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is .setlist.yaml)") rootCmd.PersistentFlags().StringP("band-name", "b", "", "the name of the band") err := viper.BindPFlag("band.name", rootCmd.PersistentFlags().Lookup("band-name")) diff --git a/internal/html/template/generate.go b/internal/html/template/generate.go index 26bd369..87286ee 100644 --- a/internal/html/template/generate.go +++ b/internal/html/template/generate.go @@ -29,6 +29,7 @@ func init() { type Data struct { Title string + Margin string Content template.HTML } diff --git a/internal/html/template/setlist.html b/internal/html/template/setlist.html index 552159e..cd98e21 100644 --- a/internal/html/template/setlist.html +++ b/internal/html/template/setlist.html @@ -5,8 +5,8 @@