mark2web/config/tree.go

172 lines
4.5 KiB
Go
Raw Normal View History

2019-02-28 10:43:30 +01:00
package config
import (
2019-03-11 15:29:05 +01:00
"fmt"
2019-02-28 10:43:30 +01:00
"reflect"
"github.com/imdario/mergo"
)
2019-03-11 15:29:05 +01:00
// MapString is a map[string]interface{} which always unmarsahls yaml to map[string]interface{}
type MapString map[string]interface{}
// UnmarshalYAML handles all maps as map[string]interface{} for later JSON
// see https://github.com/elastic/beats/blob/6435194af9f42cbf778ca0a1a92276caf41a0da8/libbeat/common/mapstr.go
func (ms *MapString) UnmarshalYAML(unmarshal func(interface{}) error) error {
var result map[interface{}]interface{}
err := unmarshal(&result)
if err != nil {
return err
}
*ms = cleanUpInterfaceMap(result)
return nil
}
func cleanUpInterfaceArray(in []interface{}) []interface{} {
result := make([]interface{}, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
}
return result
}
func cleanUpInterfaceMap(in map[interface{}]interface{}) MapString {
result := make(MapString)
for k, v := range in {
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
}
return result
}
func cleanUpMapValue(v interface{}) interface{} {
switch v := v.(type) {
case []interface{}:
return cleanUpInterfaceArray(v)
case map[interface{}]interface{}:
return cleanUpInterfaceMap(v)
2019-03-11 16:38:44 +01:00
case string, bool, int, int8, int16, int32, int64, float32, float64:
2019-03-11 15:29:05 +01:00
return v
default:
return fmt.Sprintf("%v", v)
}
}
2019-03-10 13:26:26 +01:00
// CollectionConfig describes a collection
type CollectionConfig struct {
2019-03-10 16:02:53 +01:00
Name *string `yaml:"Name"`
URL *string `yaml:"URL"`
2019-03-10 16:02:53 +01:00
NavTemplate *struct {
EntriesAttribute string `yaml:"EntriesAttribute"`
GoTo string `yaml:"GoTo"`
Navname string `yaml:"Navname"`
Body string `yaml:"Body"`
DataKey string `yaml:"DataKey"`
Hidden bool `yaml:"Hidden"`
Template string `yaml:"Template"`
Recursive bool `yaml:"Recursive"`
2019-03-10 16:02:53 +01:00
} `yaml:"NavTemplate"`
2019-03-10 13:26:26 +01:00
}
2019-02-28 10:43:30 +01:00
// ThisPathConfig is struct for This in paths yaml
type ThisPathConfig struct {
2019-03-10 13:26:26 +01:00
Navname *string `yaml:"Navname"`
GoTo *string `yaml:"GoTo"`
Collections []*CollectionConfig `yaml:"Collections"`
2019-03-11 15:29:05 +01:00
Data MapString `yaml:"Data"`
2019-02-28 10:43:30 +01:00
}
// IndexConfig describes index input and output file
type IndexConfig struct {
2019-02-28 15:13:59 +01:00
InputFile *string `yaml:"InputFile"`
InputString *string `yaml:"InputString"`
OutputFile *string `yaml:"OutputFile"`
2019-02-28 10:43:30 +01:00
}
// MetaData describes meta data for current site/tree node
type MetaData struct {
Title *string `yaml:"Title"`
Description *string `yaml:"Description"`
Keywords *string `yaml:"Keywords"`
}
// DirnameConfig describes how to handle directory names
type DirnameConfig struct {
Strip *string `yaml:"Strip"`
IgnoreForNav *string `yaml:"IgnoreForNav"`
}
// FilenameConfig describes how to handle filenames
type FilenameConfig struct {
Strip *string `yaml:"Strip"`
Ignore *string `yaml:"Ignore"`
OutputExtension *string `yaml:"OutputExtension"`
}
// MarkdownConfig describes markdown handling
type MarkdownConfig struct {
ChromaRenderer *bool `yaml:"ChromaRenderer"`
ChromaStyle *string `yaml:"ChromaStyle"`
}
2019-03-04 16:05:29 +01:00
// ImagingConfig defines parameter for imaging processing
type ImagingConfig struct {
Width int `yaml:"Width"`
Height int `yaml:"Height"`
Process string `yaml:"Process"`
Anchor string `yaml:"Anchor"`
Quality int `yaml:"Quality"`
Filename string `yaml:"-"`
Format string `yaml:"-"`
}
2019-02-28 10:43:30 +01:00
// PathConfig of subdir
type PathConfig struct {
This ThisPathConfig `yaml:"This"`
Template *string `yaml:"Template"`
Index *IndexConfig `yaml:"Index"`
Meta *MetaData `yaml:"Meta"`
Path *DirnameConfig `yaml:"Path"`
Filename *FilenameConfig `yaml:"Filename"`
Markdown *MarkdownConfig `yaml:"Markdown"`
2019-03-04 16:05:29 +01:00
Imaging *ImagingConfig `yaml:"Imaging"`
2019-02-28 10:43:30 +01:00
2019-03-11 15:29:05 +01:00
Data MapString `yaml:"Data"`
2019-02-28 10:43:30 +01:00
}
// PathConfigTree is complete config tree of content dir
type PathConfigTree struct {
InputPath string
OutputPath string
2019-02-28 14:14:31 +01:00
Hidden bool // for collections which are not part of the navigation
2019-02-28 10:43:30 +01:00
2019-03-11 15:29:05 +01:00
ColMap MapString
2019-03-10 13:26:26 +01:00
2019-02-28 10:43:30 +01:00
InputFiles []string
OtherFiles []string
Config *PathConfig
Sub []*PathConfigTree
}
type ptrTransformer struct{}
func (t ptrTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ.Kind() == reflect.Ptr {
return func(dst, src reflect.Value) error {
if dst.CanSet() {
if dst.IsNil() {
dst.Set(src)
}
}
return nil
}
}
return nil
}
// Merge merges 2 objects or maps
func Merge(dst, src interface{}) error {
return mergo.Merge(dst, src, mergo.WithTransformers(ptrTransformer{}))
}