Archived
1
0
This repository has been archived on 2025-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
ponder/settings/settings.go
2016-09-25 15:31:17 +02:00

78 lines
2.8 KiB
Go

// 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 settings
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"github.com/jjdekker/ponder/helpers"
)
// Settings provides a structure to interact with the settings
// of a Ponder library
type Settings struct {
Name string // Name of the Ponder library
Author string // Author of the Ponder library
IgnoreDirs []string // Directories to be ignored on search
LilypondIncludes []string // Directories to be included when running the lilypond compiler
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
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
EnablePointAndClick bool // Enable Lilypond's Point and Click functionality
}
// FromFile reads a settings file in json format and returns the Settings struct
func FromFile(path string) (map[string]Settings, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
s := make(map[string]Settings)
err = json.Unmarshal(data, &s)
if err != nil {
return nil, err
}
for i := range s {
set := s[i]
set.AbsolutePaths(filepath.Dir(path))
s[i] = set
}
return s, nil
}
// AbsolutePaths makes all paths in settings absolute using the given
// absolute root
func (s *Settings) AbsolutePaths(root string) {
for i := range s.IgnoreDirs {
s.IgnoreDirs[i] = helpers.AbsolutePath(s.IgnoreDirs[i], root)
}
for i := range s.LilypondIncludes {
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.BookTemplateDir = helpers.AbsolutePath(s.BookTemplateDir, root)
}