Browse files

start with global config

  • Loading branch information...
1 parent 6cd4e85 commit bfaf478fb29c63f5abd2a40d1826ac30dd016eb8 @piranha committed Jul 17, 2012
Showing with 75 additions and 25 deletions.
  1. +4 −4 config.go
  2. +37 −11 gostatic.go
  3. +8 −7 page.go
  4. +14 −3 site.go
  5. +12 −0 test/config.json
View
8 config.go
@@ -9,13 +9,13 @@ import (
"reflect"
)
-type Config struct {
+type PageConfig struct {
Title string `default:"unknown title"`
Type string `default:"page"`
Other map[string]string
}
-func (cfg *Config) ParseLine (line string, elemptr *reflect.Value) {
+func (cfg *PageConfig) ParseLine (line string, elemptr *reflect.Value) {
var s reflect.Value
if elemptr != nil {
s = *elemptr
@@ -45,8 +45,8 @@ func (cfg *Config) ParseLine (line string, elemptr *reflect.Value) {
f.SetString(strings.TrimSpace(value))
}
-func ParseConfig (source string) *Config {
- cfg := &Config{}
+func ParseConfig (source string) *PageConfig {
+ cfg := &PageConfig{}
s := reflect.ValueOf(cfg).Elem()
// Set default values
View
48 gostatic.go
@@ -5,24 +5,51 @@ package main
import (
"fmt"
+ "path/filepath"
+ // "text/template"
+ "io/ioutil"
+ "encoding/json"
goopt "github.com/droundy/goopt"
- "text/template"
)
var Version = "0.1"
-var Summary = `gostatic -t template [-t template] sitedir
+var Summary = `gostatic path/to/config.json
-Build a site
+Build a site.
`
-var templates = goopt.Strings([]string{"-t", "--template"},
- "template", "path to template")
-var output = goopt.String([]string{"-o", "--output"},
- "directory", "output directory")
var showVersion = goopt.Flag([]string{"-v", "--version"}, []string{},
"show version and exit", "")
+type GlobalConfig struct {
+ Templates []string
+ Source string
+ Output string
+ Rules map[string]([]string)
+}
+
+func RetrieveGlobalConfig(path string) *GlobalConfig {
+ conftext, err := ioutil.ReadFile(path)
+ errhandle(err)
+
+ var config GlobalConfig
+ err = json.Unmarshal(conftext, &config)
+ errhandle(err)
+
+ basepath, _ := filepath.Split(path)
+ config.Source = filepath.Join(basepath, config.Source)
+ config.Output = filepath.Join(basepath, config.Output)
+
+ templates := make([]string, len(config.Templates))
+ for i, template := range config.Templates {
+ templates[i] = filepath.Join(basepath, template)
+ }
+ config.Templates = templates
+
+ return &config
+}
+
func main() {
goopt.Version = Version
goopt.Summary = Summary
@@ -34,14 +61,13 @@ func main() {
return
}
- if len(*templates) == 0 || len(goopt.Args) == 0 {
+ if len(goopt.Args) == 0 {
println(goopt.Usage())
return
}
- t, err := template.ParseFiles(*templates...)
- errhandle(err)
+ config := RetrieveGlobalConfig(goopt.Args[0])
- site := NewSite(t, goopt.Args[0])
+ site := NewSite(config)
site.Summary()
}
View
15 page.go
@@ -1,21 +1,22 @@
package main
import (
- "os"
- "time"
"bytes"
"io/ioutil"
- "text/template"
+ "os"
"path/filepath"
+ "sort"
"strings"
+ "text/template"
+ "time"
blackfriday "github.com/russross/blackfriday"
- "sort"
)
type Page struct {
+ PageConfig
Site *Site
- Config
Content string
+ Current string
Path string
ModTime time.Time
RenderTime time.Time
@@ -36,15 +37,15 @@ func NewPage(site *Site, path string) *Page {
head, content := SplitHead(text)
return &Page{
+ PageConfig: *ParseConfig(head),
Site: site,
- Config: *ParseConfig(head),
Content: content,
Path: relpath,
ModTime: stat.ModTime(),
}
}
-// Destination() returns relative path to a file in future published repository
+// Destination() returns relative path to a file in future published site
func (page *Page) Destination() string {
path := page.Path
View
17 site.go
@@ -1,20 +1,31 @@
package main
import (
- "os"
"fmt"
+ "os"
"path/filepath"
"text/template"
)
type Site struct {
Path string
+ Output string
Template *template.Template
+ Rules map[string]([]string)
Pages PageSlice
}
-func NewSite(t *template.Template, dir string) *Site {
- site := &Site{dir, t, make(PageSlice, 0)}
+func NewSite(config *GlobalConfig) *Site {
+ template, err := template.ParseFiles(config.Templates...)
+ errhandle(err)
+
+ site := &Site{
+ Path: config.Source,
+ Output: config.Output,
+ Template: template,
+ Rules: config.Rules,
+ Pages: make(PageSlice, 0),
+ }
site.Collect()
View
12 test/config.json
@@ -0,0 +1,12 @@
+{
+ "templates": ["template.tmpl"],
+ "source": "site",
+ "output": "out",
+ "rules": {
+ "template.tmpl": [":ignore"],
+ "config.json": [":ignore"],
+ "*.md": [":template", ":markdown", ":rename *.html"],
+ "*.html": [":template"],
+ "*.less": ["lessc", ":rename *.css"]
+ }
+}

0 comments on commit bfaf478

Please sign in to comment.