Skip to content

Commit

Permalink
start with global config
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Jul 17, 2012
1 parent 6cd4e85 commit bfaf478
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
8 changes: 4 additions & 4 deletions config.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 37 additions & 11 deletions gostatic.go
Expand Up @@ -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
Expand All @@ -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()
}
15 changes: 8 additions & 7 deletions 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
Expand All @@ -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

Expand Down
17 changes: 14 additions & 3 deletions 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()

Expand Down
12 changes: 12 additions & 0 deletions 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.