Changeset 20 in code


Ignore:
Timestamp:
Aug 29, 2015, 5:54:55 PM (10 years ago)
Author:
zaitsev.serge
Message:

added global variables, added default date for markdown

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/zs.go

    r19 r20  
    3939
    4040// Parses markdown content. Returns parsed header variables and content
    41 func md(path string) (Vars, string, error) {
     41func md(path string, globals Vars) (Vars, string, error) {
    4242        b, err := ioutil.ReadFile(path)
    4343        if err != nil {
     
    5252                "layout": "index.html",
    5353        }
     54        if info, err := os.Stat(path); err == nil {
     55                v["date"] = info.ModTime().Format("02-01-2006")
     56        }
     57        for name, value := range globals {
     58                v[name] = value
     59        }
    5460        if strings.Index(s, "\n\n") == -1 {
    55                 return Vars{}, s, nil
     61                return v, s, nil
    5662        }
    5763        header, body := split2(s, "\n\n")
     
    140146// Renders markdown with the given layout into html expanding all the macros
    141147func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error {
    142         v, body, err := md(path)
     148        v, body, err := md(path, vars)
    143149        if err != nil {
    144150                return err
     
    244250func createFuncs() template.FuncMap {
    245251        // Builtin functions
    246         funcs := template.FuncMap{}
     252        funcs := template.FuncMap{
     253                "exec": func(s ...string) string {
     254                        // Run external command with arguments
     255                        return ""
     256                },
     257                "zs": func(args ...string) string {
     258                        // Run zs with arguments
     259                        return ""
     260                },
     261        }
    247262        // Plugin functions
    248263        files, _ := ioutil.ReadDir(ZSDIR)
     
    258273}
    259274
     275func globals() Vars {
     276        vars := Vars{}
     277        for _, e := range os.Environ() {
     278                pair := strings.Split(e, "=")
     279                if strings.HasPrefix(pair[0], "ZS_") {
     280                        vars[strings.ToLower(pair[0][3:])] = pair[1]
     281                }
     282        }
     283        return vars
     284}
     285
    260286func buildAll(once bool) {
    261287        lastModified := time.Unix(0, 0)
    262288        modified := false
    263         // Convert env variables into zs global variables
    264         globals := Vars{}
    265         for _, e := range os.Environ() {
    266                 pair := strings.Split(e, "=")
    267                 if strings.HasPrefix(pair[0], "ZS_") {
    268                         globals[strings.ToLower(pair[0][3:])] = pair[1]
    269                 }
    270         }
     289
     290        vars := globals()
    271291        for {
    272292                os.Mkdir(PUBDIR, 0755)
     
    291311                                if ext == ".md" || ext == ".mkd" {
    292312                                        log.Println("md: ", path)
    293                                         return buildMarkdown(path, funcs, globals)
     313                                        return buildMarkdown(path, funcs, vars)
    294314                                } else if ext == ".html" || ext == ".xml" {
    295315                                        log.Println("html: ", path)
    296                                         return buildPlain(path, funcs, globals)
     316                                        return buildPlain(path, funcs, vars)
    297317                                } else if ext == ".amber" {
    298318                                        log.Println("html: ", path)
    299                                         return buildAmber(path, funcs, globals)
     319                                        return buildAmber(path, funcs, vars)
    300320                                } else if ext == ".gcss" {
    301321                                        log.Println("css: ", path)
     
    342362                        return
    343363                }
    344                 if vars, _, err := md(args[0]); err == nil {
     364                if vars, _, err := md(args[0], globals()); err == nil {
    345365                        if len(args) > 1 {
    346366                                for _, a := range args[1:] {
  • trunk/zs_test.go

    r19 r20  
    4747        bayan: [:|||:]
    4848
    49 this: is a content`))
     49this: is a content`), Vars{})
    5050        if v["title"] != "Hello, world!" {
    5151                t.Error()
     
    6565
    6666        // Test empty md
    67         v, body, _ = md(tmpfile("foo.md", ""))
    68         if len(v) != 0 || len(body) != 0 {
     67        v, body, _ = md(tmpfile("foo.md", ""), Vars{})
     68        if v["url"] != "foo.html" || len(body) != 0 {
    6969                t.Error(v, body)
    7070        }
    7171
    7272        // Test empty header
    73         v, body, _ = md(tmpfile("foo.md", "Hello"))
    74         if len(v) != 0 || body != "Hello" {
     73        v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
     74        if v["url"] != "foo.html" || body != "Hello" {
    7575                t.Error(v, body)
    7676        }
Note: See TracChangeset for help on using the changeset viewer.