[48] | 1 | package main
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bytes"
|
---|
| 5 | "fmt"
|
---|
| 6 | "io"
|
---|
| 7 | "io/ioutil"
|
---|
| 8 | "log"
|
---|
| 9 | "os"
|
---|
| 10 | "os/exec"
|
---|
| 11 | "path/filepath"
|
---|
| 12 | "strings"
|
---|
| 13 | "text/template"
|
---|
| 14 | "time"
|
---|
| 15 |
|
---|
| 16 | "github.com/russross/blackfriday/v2"
|
---|
| 17 | "gopkg.in/yaml.v2"
|
---|
| 18 | )
|
---|
| 19 |
|
---|
| 20 | const (
|
---|
| 21 | ZSDIR = ".zs"
|
---|
| 22 | PUBDIR = ".pub"
|
---|
| 23 | )
|
---|
| 24 |
|
---|
| 25 | type Vars map[string]string
|
---|
| 26 |
|
---|
| 27 | // renameExt renames extension (if any) from oldext to newext
|
---|
| 28 | // If oldext is an empty string - extension is extracted automatically.
|
---|
| 29 | // If path has no extension - new extension is appended
|
---|
| 30 | func renameExt(path, oldext, newext string) string {
|
---|
| 31 | if oldext == "" {
|
---|
| 32 | oldext = filepath.Ext(path)
|
---|
| 33 | }
|
---|
| 34 | if oldext == "" || strings.HasSuffix(path, oldext) {
|
---|
| 35 | return strings.TrimSuffix(path, oldext) + newext
|
---|
| 36 | } else {
|
---|
| 37 | return path
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | // globals returns list of global OS environment variables that start
|
---|
| 42 | // with ZS_ prefix as Vars, so the values can be used inside templates
|
---|
| 43 | func globals() Vars {
|
---|
| 44 | vars := Vars{}
|
---|
| 45 | for _, e := range os.Environ() {
|
---|
| 46 | pair := strings.Split(e, "=")
|
---|
| 47 | if strings.HasPrefix(pair[0], "ZS_") {
|
---|
| 48 | vars[strings.ToLower(pair[0][3:])] = pair[1]
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | return vars
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | // run executes a command or a script. Vars define the command environment,
|
---|
| 55 | // each zs var is converted into OS environemnt variable with ZS_ prefix
|
---|
| 56 | // prepended. Additional variable $ZS contains path to the zs binary. Command
|
---|
| 57 | // stderr is printed to zs stderr, command output is returned as a string.
|
---|
| 58 | func run(vars Vars, cmd string, args ...string) (string, error) {
|
---|
| 59 | // First check if partial exists (.html)
|
---|
| 60 | if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".html")); err == nil {
|
---|
| 61 | return string(b), nil
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | var errbuf, outbuf bytes.Buffer
|
---|
| 65 | c := exec.Command(cmd, args...)
|
---|
| 66 | env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
|
---|
| 67 | env = append(env, os.Environ()...)
|
---|
| 68 | for k, v := range vars {
|
---|
| 69 | env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
|
---|
| 70 | }
|
---|
| 71 | c.Env = env
|
---|
| 72 | c.Stdout = &outbuf
|
---|
| 73 | c.Stderr = &errbuf
|
---|
| 74 |
|
---|
| 75 | err := c.Run()
|
---|
| 76 |
|
---|
| 77 | if errbuf.Len() > 0 {
|
---|
| 78 | log.Println("ERROR:", errbuf.String())
|
---|
| 79 | }
|
---|
| 80 | if err != nil {
|
---|
| 81 | return "", err
|
---|
| 82 | }
|
---|
| 83 | return string(outbuf.Bytes()), nil
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | // getVars returns list of variables defined in a text file and actual file
|
---|
| 87 | // content following the variables declaration. Header is separated from
|
---|
| 88 | // content by an empty line. Header can be either YAML or JSON.
|
---|
| 89 | // If no empty newline is found - file is treated as content-only.
|
---|
| 90 | func getVars(path string, globals Vars) (Vars, string, error) {
|
---|
| 91 | b, err := ioutil.ReadFile(path)
|
---|
| 92 | if err != nil {
|
---|
| 93 | return nil, "", err
|
---|
| 94 | }
|
---|
| 95 | s := string(b)
|
---|
| 96 |
|
---|
| 97 | // Pick some default values for content-dependent variables
|
---|
| 98 | v := Vars{}
|
---|
| 99 | title := strings.Replace(strings.Replace(path, "_", " ", -1), "-", " ", -1)
|
---|
| 100 | v["title"] = strings.ToTitle(title)
|
---|
| 101 | v["description"] = ""
|
---|
| 102 | v["file"] = path
|
---|
| 103 | v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
|
---|
| 104 | v["output"] = filepath.Join(PUBDIR, v["url"])
|
---|
| 105 |
|
---|
| 106 | // Override default values with globals
|
---|
| 107 | for name, value := range globals {
|
---|
| 108 | v[name] = value
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | // Add layout if none is specified
|
---|
| 112 | if _, ok := v["layout"]; !ok {
|
---|
| 113 | v["layout"] = "layout.html"
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | delim := "\n---\n"
|
---|
| 117 | if sep := strings.Index(s, delim); sep == -1 {
|
---|
| 118 | return v, s, nil
|
---|
| 119 | } else {
|
---|
| 120 | header := s[:sep]
|
---|
| 121 | body := s[sep+len(delim):]
|
---|
| 122 |
|
---|
| 123 | vars := Vars{}
|
---|
| 124 | if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
|
---|
| 125 | fmt.Println("ERROR: failed to parse header", err)
|
---|
| 126 | return nil, "", err
|
---|
| 127 | } else {
|
---|
| 128 | // Override default values + globals with the ones defines in the file
|
---|
| 129 | for key, value := range vars {
|
---|
| 130 | v[key] = value
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | if strings.HasPrefix(v["url"], "./") {
|
---|
| 134 | v["url"] = v["url"][2:]
|
---|
| 135 | }
|
---|
| 136 | return v, body, nil
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | // Render expanding zs plugins and variables
|
---|
| 141 | func render(s string, vars Vars) (string, error) {
|
---|
| 142 | delim_open := "{{"
|
---|
| 143 | delim_close := "}}"
|
---|
| 144 |
|
---|
| 145 | out := &bytes.Buffer{}
|
---|
| 146 | for {
|
---|
| 147 | if from := strings.Index(s, delim_open); from == -1 {
|
---|
| 148 | out.WriteString(s)
|
---|
| 149 | return out.String(), nil
|
---|
| 150 | } else {
|
---|
| 151 | if to := strings.Index(s, delim_close); to == -1 {
|
---|
| 152 | return "", fmt.Errorf("Close delim not found")
|
---|
| 153 | } else {
|
---|
| 154 | out.WriteString(s[:from])
|
---|
| 155 | cmd := s[from+len(delim_open) : to]
|
---|
| 156 | s = s[to+len(delim_close):]
|
---|
| 157 | m := strings.Fields(cmd)
|
---|
| 158 | if len(m) == 1 {
|
---|
| 159 | if v, ok := vars[m[0]]; ok {
|
---|
| 160 | out.WriteString(v)
|
---|
| 161 | continue
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | if res, err := run(vars, m[0], m[1:]...); err == nil {
|
---|
| 165 | out.WriteString(res)
|
---|
| 166 | } else {
|
---|
| 167 | fmt.Println(err)
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | return s, nil
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // Renders markdown with the given layout into html expanding all the macros
|
---|
| 176 | func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
---|
| 177 | v, body, err := getVars(path, vars)
|
---|
| 178 | if err != nil {
|
---|
| 179 | return err
|
---|
| 180 | }
|
---|
| 181 | content, err := render(body, v)
|
---|
| 182 | if err != nil {
|
---|
| 183 | return err
|
---|
| 184 | }
|
---|
[52] | 185 | v["content"] = string(blackfriday.Run(
|
---|
| 186 | []byte(content),
|
---|
| 187 | blackfriday.WithExtensions(blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs),
|
---|
| 188 | ))
|
---|
[48] | 189 | if w == nil {
|
---|
| 190 | out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html")))
|
---|
| 191 | if err != nil {
|
---|
| 192 | return err
|
---|
| 193 | }
|
---|
| 194 | defer out.Close()
|
---|
| 195 | w = out
|
---|
| 196 | }
|
---|
| 197 | return buildHTML(filepath.Join(ZSDIR, v["layout"]), w, v)
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | // Renders text file expanding all variable macros inside it
|
---|
| 201 | func buildHTML(path string, w io.Writer, vars Vars) error {
|
---|
| 202 | v, body, err := getVars(path, vars)
|
---|
| 203 | if err != nil {
|
---|
| 204 | return err
|
---|
| 205 | }
|
---|
| 206 | if body, err = render(body, v); err != nil {
|
---|
| 207 | return err
|
---|
| 208 | }
|
---|
| 209 | tmpl, err := template.New("").Delims("<%", "%>").Parse(body)
|
---|
| 210 | if err != nil {
|
---|
| 211 | return err
|
---|
| 212 | }
|
---|
| 213 | if w == nil {
|
---|
| 214 | f, err := os.Create(filepath.Join(PUBDIR, path))
|
---|
| 215 | if err != nil {
|
---|
| 216 | return err
|
---|
| 217 | }
|
---|
| 218 | defer f.Close()
|
---|
| 219 | w = f
|
---|
| 220 | }
|
---|
| 221 | return tmpl.Execute(w, vars)
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | // Copies file as is from path to writer
|
---|
| 225 | func buildRaw(path string, w io.Writer) error {
|
---|
| 226 | in, err := os.Open(path)
|
---|
| 227 | if err != nil {
|
---|
| 228 | return err
|
---|
| 229 | }
|
---|
| 230 | defer in.Close()
|
---|
| 231 | if w == nil {
|
---|
| 232 | if out, err := os.Create(filepath.Join(PUBDIR, path)); err != nil {
|
---|
| 233 | return err
|
---|
| 234 | } else {
|
---|
| 235 | defer out.Close()
|
---|
| 236 | w = out
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | _, err = io.Copy(w, in)
|
---|
| 240 | return err
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | func build(path string, w io.Writer, vars Vars) error {
|
---|
| 244 | ext := filepath.Ext(path)
|
---|
| 245 | if ext == ".md" || ext == ".mkd" {
|
---|
| 246 | return buildMarkdown(path, w, vars)
|
---|
| 247 | } else if ext == ".html" || ext == ".xml" {
|
---|
| 248 | return buildHTML(path, w, vars)
|
---|
| 249 | } else {
|
---|
| 250 | return buildRaw(path, w)
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | func buildAll(watch bool) {
|
---|
| 255 | lastModified := time.Unix(0, 0)
|
---|
| 256 | modified := false
|
---|
| 257 |
|
---|
| 258 | vars := globals()
|
---|
| 259 | for {
|
---|
| 260 | os.Mkdir(PUBDIR, 0755)
|
---|
| 261 | filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
---|
| 262 | // ignore hidden files and directories
|
---|
| 263 | if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") {
|
---|
| 264 | return nil
|
---|
| 265 | }
|
---|
| 266 | // inform user about fs walk errors, but continue iteration
|
---|
| 267 | if err != nil {
|
---|
| 268 | fmt.Println("error:", err)
|
---|
| 269 | return nil
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | if info.IsDir() {
|
---|
| 273 | os.Mkdir(filepath.Join(PUBDIR, path), 0755)
|
---|
| 274 | return nil
|
---|
| 275 | } else if info.ModTime().After(lastModified) {
|
---|
| 276 | if !modified {
|
---|
| 277 | // First file in this build cycle is about to be modified
|
---|
| 278 | run(vars, "prehook")
|
---|
| 279 | modified = true
|
---|
| 280 | }
|
---|
| 281 | log.Println("build:", path)
|
---|
| 282 | return build(path, nil, vars)
|
---|
| 283 | }
|
---|
| 284 | return nil
|
---|
| 285 | })
|
---|
| 286 | if modified {
|
---|
| 287 | // At least one file in this build cycle has been modified
|
---|
| 288 | run(vars, "posthook")
|
---|
| 289 | modified = false
|
---|
| 290 | }
|
---|
| 291 | if !watch {
|
---|
| 292 | break
|
---|
| 293 | }
|
---|
| 294 | lastModified = time.Now()
|
---|
| 295 | time.Sleep(1 * time.Second)
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | func init() {
|
---|
| 300 | // prepend .zs to $PATH, so plugins will be found before OS commands
|
---|
| 301 | p := os.Getenv("PATH")
|
---|
| 302 | p = ZSDIR + ":" + p
|
---|
| 303 | os.Setenv("PATH", p)
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | func main() {
|
---|
| 307 | if len(os.Args) == 1 {
|
---|
| 308 | fmt.Println(os.Args[0], "<command> [args]")
|
---|
| 309 | return
|
---|
| 310 | }
|
---|
| 311 | cmd := os.Args[1]
|
---|
| 312 | args := os.Args[2:]
|
---|
| 313 | switch cmd {
|
---|
| 314 | case "build":
|
---|
| 315 | if len(args) == 0 {
|
---|
| 316 | buildAll(false)
|
---|
| 317 | } else if len(args) == 1 {
|
---|
| 318 | if err := build(args[0], os.Stdout, globals()); err != nil {
|
---|
| 319 | fmt.Println("ERROR: " + err.Error())
|
---|
| 320 | }
|
---|
| 321 | } else {
|
---|
| 322 | fmt.Println("ERROR: too many arguments")
|
---|
| 323 | }
|
---|
| 324 | case "watch":
|
---|
| 325 | buildAll(true)
|
---|
| 326 | case "var":
|
---|
| 327 | if len(args) == 0 {
|
---|
| 328 | fmt.Println("var: filename expected")
|
---|
| 329 | } else {
|
---|
| 330 | s := ""
|
---|
| 331 | if vars, _, err := getVars(args[0], Vars{}); err != nil {
|
---|
| 332 | fmt.Println("var: " + err.Error())
|
---|
| 333 | } else {
|
---|
| 334 | if len(args) > 1 {
|
---|
| 335 | for _, a := range args[1:] {
|
---|
| 336 | s = s + vars[a] + "\n"
|
---|
| 337 | }
|
---|
| 338 | } else {
|
---|
| 339 | for k, v := range vars {
|
---|
| 340 | s = s + k + ":" + v + "\n"
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | fmt.Println(strings.TrimSpace(s))
|
---|
| 345 | }
|
---|
| 346 | default:
|
---|
| 347 | if s, err := run(globals(), cmd, args...); err != nil {
|
---|
| 348 | fmt.Println(err)
|
---|
| 349 | } else {
|
---|
| 350 | fmt.Println(s)
|
---|
| 351 | }
|
---|
| 352 | }
|
---|
| 353 | }
|
---|