fixed stylesheet embedding
This commit is contained in:
parent
c65ebfc809
commit
065c344c03
5 changed files with 192 additions and 14 deletions
|
@ -60,7 +60,7 @@ func buildPageData(m Metadata, path string, settings *Settings) *PageData {
|
|||
if style, ok := m["style"].(string); ok {
|
||||
p.Stylesheet = style
|
||||
} else {
|
||||
p.Stylesheet = settings.StylesheetName
|
||||
p.Stylesheet = settings.StylePath
|
||||
}
|
||||
if header, ok := m["header"].(string); ok {
|
||||
p.HeaderName = header
|
||||
|
|
|
@ -11,13 +11,14 @@ import (
|
|||
)
|
||||
|
||||
var defaultNames = map[string]string{
|
||||
"config": "config.yml",
|
||||
"header": "header.md",
|
||||
"footer": "footer.md",
|
||||
"style": "style.css",
|
||||
"icon": "favicon.png",
|
||||
"article": "article.html",
|
||||
"template": "default.html",
|
||||
"config": "config.yml",
|
||||
"header": "header.md",
|
||||
"footer": "footer.md",
|
||||
"style": "style.css",
|
||||
"stylePath": filepath.Join("style", "style.css"),
|
||||
"icon": "favicon.png",
|
||||
"article": "article.html",
|
||||
"template": "default.html",
|
||||
}
|
||||
|
||||
//go:embed embed/article.html
|
||||
|
@ -40,9 +41,12 @@ type Settings struct {
|
|||
DefaultTemplateName string
|
||||
ArticleTemplate string
|
||||
Stylesheet []byte
|
||||
StylePath string
|
||||
Icon []byte
|
||||
}
|
||||
|
||||
var isDefaultStyle bool
|
||||
|
||||
// processSetting checks the user's configuration for
|
||||
// each option. If set, reads the specified file. If not,
|
||||
// default option is used.
|
||||
|
@ -55,12 +59,13 @@ func processSetting(c map[string]interface{}, s string) (string, []byte, error)
|
|||
return name, val, nil
|
||||
} else {
|
||||
val := readEmbed(defaultNames[s])
|
||||
isDefaultStyle = true
|
||||
return defaultNames[s], val, nil
|
||||
}
|
||||
}
|
||||
|
||||
// buildSettings constructs the Settings struct.
|
||||
func buildSettings(f []byte) (*Settings, error) {
|
||||
func buildSettings(f []byte, outRoot string) (*Settings, error) {
|
||||
s := &Settings{}
|
||||
var c map[string]interface{}
|
||||
// Parse YAML
|
||||
|
@ -79,12 +84,27 @@ func buildSettings(f []byte) (*Settings, error) {
|
|||
}
|
||||
s.FooterName = n
|
||||
s.Footer = template.HTML(MdToHTML(v))
|
||||
isDefaultStyle = false
|
||||
n, v, err = processSetting(c, "style")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.StylesheetName = n
|
||||
s.Stylesheet = MdToHTML(v)
|
||||
s.Stylesheet = v
|
||||
|
||||
if isDefaultStyle {
|
||||
stylePath := filepath.Join(outRoot, defaultNames["stylePath"])
|
||||
// We convert the stylesheet path to its website root dir format and store it
|
||||
s.StylePath = "/" + util.StripTopDir(stylePath)
|
||||
err := util.CreateParents(stylePath)
|
||||
if err != nil {
|
||||
return nil, util.ErrorPrepend("Could not create default stylesheet directory: ", err)
|
||||
}
|
||||
err = util.WriteFile(s.Stylesheet, stylePath)
|
||||
if err != nil {
|
||||
return nil, util.ErrorPrepend("Could not create default stylesheet: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
n, v, err = processSetting(c, "icon")
|
||||
if err != nil {
|
||||
|
@ -114,7 +134,7 @@ func readEmbed(name string) []byte {
|
|||
return f
|
||||
}
|
||||
|
||||
func GetSettings(root string) *Settings {
|
||||
func GetSettings(root string, outRoot string) *Settings {
|
||||
var config []byte
|
||||
configPath := filepath.Join(root, defaultNames["config"])
|
||||
if !util.FileExists(configPath) {
|
||||
|
@ -122,12 +142,12 @@ func GetSettings(root string) *Settings {
|
|||
config = readEmbed(defaultNames["config"])
|
||||
} else {
|
||||
var err error
|
||||
config, err = util.ReadFile(configPath)
|
||||
config, err = util.ReadFile(filepath.Join(root, configPath))
|
||||
if err != nil {
|
||||
log.Fatalln("Fatal internal error: Config file exists but could not be read!")
|
||||
}
|
||||
}
|
||||
s, err := buildSettings(config)
|
||||
s, err := buildSettings(config, outRoot)
|
||||
if err != nil {
|
||||
log.Fatalf("Fatal error: could not parse config: %u\n", err)
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ func FileExists(path string) bool {
|
|||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
// CreateParents creates the parent directories required for a given path
|
||||
func CreateParents(path string) error {
|
||||
dir := filepath.Dir(path)
|
||||
// Check if the parent directory already exists
|
||||
|
@ -59,3 +60,12 @@ func CreateParents(path string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func StripTopDir(path string) string {
|
||||
cleanedPath := filepath.Clean(path)
|
||||
components := strings.Split(cleanedPath, string(filepath.Separator))
|
||||
if len(components) <= 1 {
|
||||
return path
|
||||
}
|
||||
return filepath.Join(components[1:]...)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue