finished config parser (untested)
refactored some config parser types
This commit is contained in:
parent
c6c801e248
commit
878cb3d3a8
2 changed files with 36 additions and 21 deletions
|
@ -53,22 +53,22 @@ func buildPageData(m Metadata, path string, settings *Settings) *PageData {
|
||||||
if icon, ok := m["icon"].(string); ok {
|
if icon, ok := m["icon"].(string); ok {
|
||||||
p.Icon = icon
|
p.Icon = icon
|
||||||
} else {
|
} else {
|
||||||
p.Icon = settings.Icon
|
p.Icon = settings.IconName
|
||||||
}
|
}
|
||||||
if style, ok := m["style"].(string); ok {
|
if style, ok := m["style"].(string); ok {
|
||||||
p.Stylesheet = style
|
p.Stylesheet = style
|
||||||
} else {
|
} else {
|
||||||
p.Stylesheet = settings.Stylesheet
|
p.Stylesheet = settings.StylesheetName
|
||||||
}
|
}
|
||||||
if header, ok := m["header"].(string); ok {
|
if header, ok := m["header"].(string); ok {
|
||||||
p.Header = header
|
p.Header = header
|
||||||
} else {
|
} else {
|
||||||
p.Header = settings.Header
|
p.Header = settings.HeaderName
|
||||||
}
|
}
|
||||||
if footer, ok := m["footer"].(string); ok {
|
if footer, ok := m["footer"].(string); ok {
|
||||||
p.Footer = footer
|
p.Footer = footer
|
||||||
} else {
|
} else {
|
||||||
p.Footer = settings.Footer
|
p.Footer = settings.FooterName
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ const (
|
||||||
DefStylesheetName = "style.css"
|
DefStylesheetName = "style.css"
|
||||||
DefIconName = "icon.png"
|
DefIconName = "icon.png"
|
||||||
DefTemplateName = "default.html"
|
DefTemplateName = "default.html"
|
||||||
|
ArticleTemplateName = "article.html"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed embed
|
//go:embed embed
|
||||||
|
@ -27,12 +28,13 @@ type Settings struct {
|
||||||
HeaderName string
|
HeaderName string
|
||||||
Footer template.HTML
|
Footer template.HTML
|
||||||
FooterName string
|
FooterName string
|
||||||
Stylesheet []byte
|
|
||||||
StylesheetName string
|
StylesheetName string
|
||||||
Icon []byte
|
|
||||||
IconName string
|
IconName string
|
||||||
DefaultTemplate template.HTML
|
DefaultTemplate string
|
||||||
DefaultTemplateName string
|
DefaultTemplateName string
|
||||||
|
ArticleTemplate string
|
||||||
|
Stylesheet []byte
|
||||||
|
Icon []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildSettings(f []byte) (*Settings, error) {
|
func buildSettings(f []byte) (*Settings, error) {
|
||||||
|
@ -90,7 +92,20 @@ func buildSettings(f []byte) (*Settings, error) {
|
||||||
s.Icon = icon
|
s.Icon = icon
|
||||||
s.IconName = DefIconName
|
s.IconName = DefIconName
|
||||||
}
|
}
|
||||||
if
|
if templateName, ok := c["template"].(string); ok {
|
||||||
|
temp, err := util.ReadFile(templateName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, util.ErrorPrepend("Could not read template specified in config: ", err)
|
||||||
|
}
|
||||||
|
s.DefaultTemplate = string(temp)
|
||||||
|
s.DefaultTemplateName = templateName
|
||||||
|
} else {
|
||||||
|
temp := readEmbed(DefTemplateName)
|
||||||
|
s.DefaultTemplate = string(temp)
|
||||||
|
s.DefaultTemplateName = DefTemplateName
|
||||||
|
}
|
||||||
|
artTemp := readEmbed(ArticleTemplateName)
|
||||||
|
s.ArticleTemplate = string(artTemp)
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
@ -105,21 +120,21 @@ func readEmbed(name string) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSettings(root string) *Settings {
|
func GetSettings(root string) *Settings {
|
||||||
// TODO: Read a config file to override defaults
|
|
||||||
// "Defaults" should be a default config file via embed package,
|
|
||||||
// so the settings func should need to handle one case:
|
|
||||||
// check if config file exists, if not, use embedded one
|
|
||||||
var config []byte
|
var config []byte
|
||||||
configPath := filepath.Join(root, DefConfigName)
|
configPath := filepath.Join(root, DefConfigName)
|
||||||
if !util.FileExists(configPath) {
|
if !util.FileExists(configPath) {
|
||||||
// Config file does not exist, we used embedded default
|
// Config file does not exist, we used embedded default
|
||||||
config = readEmbed(configPath)
|
config = readEmbed(configPath)
|
||||||
} else {
|
} else {
|
||||||
config, err := util.ReadFile(configPath)
|
var err error
|
||||||
|
config, err = util.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Fatal internal error: Config file exists but could not be read!")
|
log.Fatalln("Fatal internal error: Config file exists but could not be read!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s, err := buildSettings(config)
|
||||||
// return NewSettings(DefaultHeader, DefaultFooter, DefaultStylesheet, DefaultIcon, DefaultTemplate)
|
if err != nil {
|
||||||
|
log.Fatalf("Fatal error: could not parse config: %u\n", err)
|
||||||
|
}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue