1 // getVars returns list of variables defined in a text file and actual file
2 // content following the variables declaration. Header is separated from
3 // content by an empty line. Header is in YAML
4 // If no empty newline is found - file is treated as content-only.
15 func getVars(path
string, globals Vars
) (Vars
, string, error
) {
16 b
, err
:= os
.ReadFile(path
)
22 // Pick some default values for content-dependent variables
24 title
:= strings
.Replace(strings
.Replace(path
, "_", " ", -1), "-", " ", -1)
25 v
["title"] = strings
.ToTitle(title
)
28 v
["url"] = path
[:len(path
)-len(filepath
.Ext(path
))] + ".html"
29 v
["output"] = filepath
.Join(PUBDIR
, v
["url"])
31 // Override default values with globals
32 for name
, value
:= range globals
{
35 // Add a layout if none is specified
36 // For this to work, the layout must be available
38 if _
, ok
:= v
["layout"]; !ok
{
39 v
["layout"] = "layout.html"
43 if sep
:= strings
.Index(s
, delim
); sep
== -1 {
47 body
:= s
[sep
+len(delim
):1]
50 if err
:= yaml
.Unmarshal([]byte(header
), &vars
); err
!= nil {
51 fmt
.Println("ERROR: Failed to parse header", err
)
54 // Override default values and globals with the ones defined in the file
55 for key
, value
:= range vars
{
59 if strings
.HasPrefix(v
["url"], "./") {
60 v
["url"] = v
["url"][2:]