Browse Source

fix receiver and parameter naming

fix glog and gonf imports
cdelorme 7 năm trước cách đây
mục cha
commit
2f44042a97
3 tập tin đã thay đổi với 63 bổ sung63 xóa
  1. 3 3
      cmd/staticmd/main.go
  2. 57 57
      generator.go
  3. 3 3
      generator_test.go

+ 3 - 3
cmd/staticmd/main.go

@@ -4,7 +4,7 @@ import (
 	"os"
 	"path/filepath"
 
-	"github.com/cdelorme/go-log"
+	"github.com/cdelorme/glog"
 	"github.com/cdelorme/gonf"
 	"github.com/cdelorme/staticmd"
 )
@@ -20,12 +20,12 @@ func configure() generator {
 	cwd, _ := getwd()
 
 	smd := &staticmd.Generator{
-		Logger: &log.Logger{},
+		L:      &glog.Logger{},
 		Input:  cwd,
 		Output: filepath.Join(cwd, "public/"),
 	}
 
-	g := &gonf.Gonf{Description: "command line tool for generating deliverable static content", Configuration: smd}
+	g := &gonf.Config{Description: "command line tool for generating deliverable static content", Target: smd}
 	g.Add("template", "path to the template file", "STATICMD_TEMPLATE", "--template", "-t:")
 	g.Add("input", "path to the markdown files", "STATICMD_INPUT", "--input", "-i:")
 	g.Add("output", "path to place generated content", "STATICMD_OUTPUT", "--output", "-o:")

+ 57 - 57
generator.go

@@ -18,7 +18,7 @@ var mkdirall = os.MkdirAll
 var parseFiles = template.ParseFiles
 var walk = filepath.Walk
 
-type ht interface {
+type executor interface {
 	Execute(io.Writer, interface{}) error
 }
 
@@ -29,56 +29,56 @@ type Generator struct {
 	Relative     bool   `json:"relative,omitempty"`
 	TemplateFile string `json:"template,omitempty"`
 
-	Logger logger
+	L logger
 
 	version  string
 	pages    []string
-	template ht
+	template executor
 }
 
-func (self *Generator) ior(path string) string {
-	return strings.TrimSuffix(strings.Replace(path, self.Input, self.Output, 1), filepath.Ext(path)) + ".html"
+func (g *Generator) ior(path string) string {
+	return strings.TrimSuffix(strings.Replace(path, g.Input, g.Output, 1), filepath.Ext(path)) + ".html"
 }
 
-func (self *Generator) depth(path string) string {
-	if self.Relative {
-		if rel, err := filepath.Rel(filepath.Dir(path), self.Output); err == nil {
+func (g *Generator) depth(path string) string {
+	if g.Relative {
+		if rel, err := filepath.Rel(filepath.Dir(path), g.Output); err == nil {
 			return rel + string(os.PathSeparator)
 		}
 	}
 	return ""
 }
 
-func (self *Generator) walk(path string, file os.FileInfo, err error) error {
+func (g *Generator) walk(path string, file os.FileInfo, err error) error {
 	if file != nil && file.Mode().IsRegular() && file.Size() > 0 && isMarkdown(path) {
-		self.pages = append(self.pages, path)
+		g.pages = append(g.pages, path)
 	}
 	return err
 }
 
-func (self *Generator) multi() error {
+func (g *Generator) multi() error {
 	navi := make(map[string][]navigation)
 	var err error
 
-	for i, _ := range self.pages {
-		out := self.ior(self.pages[i])
-		dir := filepath.Dir(self.ior(out))
+	for i, _ := range g.pages {
+		out := g.ior(g.pages[i])
+		dir := filepath.Dir(g.ior(out))
 		nav := navigation{}
 
-		if filepath.Dir(out) != self.Output && strings.ToLower(basename(out)) == "index" {
+		if filepath.Dir(out) != g.Output && strings.ToLower(basename(out)) == "index" {
 			nav.Title = basename(dir)
-			if self.Relative {
+			if g.Relative {
 				nav.Link = filepath.Join(strings.TrimPrefix(dir, filepath.Dir(dir)+string(os.PathSeparator)), filepath.Base(out))
 			} else {
-				nav.Link = strings.TrimPrefix(dir, self.Output) + string(os.PathSeparator)
+				nav.Link = strings.TrimPrefix(dir, g.Output) + string(os.PathSeparator)
 			}
 			dir = filepath.Dir(dir)
 		} else {
 			nav.Title = basename(out)
-			if self.Relative {
+			if g.Relative {
 				nav.Link = strings.TrimPrefix(out, filepath.Dir(out)+string(os.PathSeparator))
 			} else {
-				nav.Link = strings.TrimPrefix(out, self.Output)
+				nav.Link = strings.TrimPrefix(out, g.Output)
 			}
 		}
 
@@ -86,7 +86,7 @@ func (self *Generator) multi() error {
 			navi[dir] = make([]navigation, 0)
 			if ok, _ := exists(dir); !ok {
 				if err = mkdirall(dir, 0770); err != nil {
-					self.Logger.Error("failed to create path: %s, %s", dir, err)
+					g.L.Error("failed to create path: %s, %s", dir, err)
 				}
 			}
 		}
@@ -94,28 +94,28 @@ func (self *Generator) multi() error {
 		navi[dir] = append(navi[dir], nav)
 	}
 
-	for _, p := range self.pages {
+	for _, p := range g.pages {
 		var markdown []byte
 		if markdown, err = readfile(p); err != nil {
-			self.Logger.Error("failed to read file: %s, %s", p, err)
+			g.L.Error("failed to read file: %s, %s", p, err)
 			return err
 		}
 
-		out := self.ior(p)
+		out := g.ior(p)
 		dir := filepath.Dir(out)
 		page := page{
 			Name:    basename(p),
-			Version: self.version,
-			Nav:     navi[self.Output],
-			Depth:   self.depth(out),
+			Version: g.version,
+			Nav:     navi[g.Output],
+			Depth:   g.depth(out),
 		}
 
-		if dir != self.Output && strings.ToLower(basename(p)) == "index" {
+		if dir != g.Output && strings.ToLower(basename(p)) == "index" {
 			toc := "\n## Table of Contents:\n\n"
 			for i, _ := range navi[dir] {
 				toc = toc + "- [" + navi[dir][i].Title + "](" + navi[dir][i].Link + ")\n"
 			}
-			self.Logger.Debug("table of contents for %s, %s", out, toc)
+			g.L.Debug("table of contents for %s, %s", out, toc)
 			markdown = append([]byte(toc), markdown...)
 		}
 
@@ -123,7 +123,7 @@ func (self *Generator) multi() error {
 
 		var f *os.File
 		if f, err = create(out); err != nil {
-			self.Logger.Error("%s\n", err)
+			g.L.Error("%s\n", err)
 			return err
 		}
 		defer f.Close()
@@ -131,22 +131,22 @@ func (self *Generator) multi() error {
 		fb := bufio.NewWriter(f)
 		defer fb.Flush()
 
-		if err = self.template.Execute(fb, page); err != nil {
-			self.Logger.Error("%s\n", err)
+		if err = g.template.Execute(fb, page); err != nil {
+			g.L.Error("%s\n", err)
 		}
 	}
 
 	return err
 }
 
-func (self *Generator) single() error {
+func (g *Generator) single() error {
 	content := make([]byte, 0)
 	toc := "\n"
 	previous_depth := 0
 	var err error
 
-	for _, p := range self.pages {
-		shorthand := strings.TrimPrefix(p, self.Input+string(os.PathSeparator))
+	for _, p := range g.pages {
+		shorthand := strings.TrimPrefix(p, g.Input+string(os.PathSeparator))
 		depth := strings.Count(shorthand, string(os.PathSeparator))
 		if depth > previous_depth {
 			toc = toc + strings.Repeat("\t", depth-1) + "- " + basename(filepath.Dir(p)) + "\n"
@@ -156,7 +156,7 @@ func (self *Generator) single() error {
 
 		var markdown []byte
 		if markdown, err = readfile(p); err != nil {
-			self.Logger.Error("failed to read file: %s (%s)", p, err)
+			g.L.Error("failed to read file: %s (%s)", p, err)
 			continue
 		}
 
@@ -168,22 +168,22 @@ func (self *Generator) single() error {
 
 	content = append([]byte(toc), content...)
 
-	if ok, _ := exists(self.Output); !ok {
-		if err = mkdirall(self.Output, 0770); err != nil {
-			self.Logger.Error("failed to create path: %s (%s)", self.Output, err)
+	if ok, _ := exists(g.Output); !ok {
+		if err = mkdirall(g.Output, 0770); err != nil {
+			g.L.Error("failed to create path: %s (%s)", g.Output, err)
 			return err
 		}
 	}
 
 	page := page{
-		Version: self.version,
+		Version: g.version,
 		Content: template.HTML(blackfriday.MarkdownCommon(content)),
 	}
-	out := filepath.Join(self.Output, "index.html")
+	out := filepath.Join(g.Output, "index.html")
 
 	var f *os.File
 	if f, err = create(out); err != nil {
-		self.Logger.Error("%s\n", err)
+		g.L.Error("%s\n", err)
 		return err
 	}
 	defer f.Close()
@@ -191,34 +191,34 @@ func (self *Generator) single() error {
 	fb := bufio.NewWriter(f)
 	defer fb.Flush()
 
-	if err = self.template.Execute(fb, page); err != nil {
-		self.Logger.Error("%s\n", err)
+	if err = g.template.Execute(fb, page); err != nil {
+		g.L.Error("%s\n", err)
 	}
 
 	return err
 }
 
-func (self *Generator) Generate() error {
+func (g *Generator) Generate() error {
 	var err error
-	if self.template, err = parseFiles(self.TemplateFile); err != nil {
-		self.Logger.Error("%s\n", err)
+	if g.template, err = parseFiles(g.TemplateFile); err != nil {
+		g.L.Error("%s\n", err)
 		return err
 	}
 
-	self.version = version(self.Input)
-	self.Input, _ = filepath.Abs(self.Input)
-	self.Output, _ = filepath.Abs(self.Output)
-	self.Input = filepath.Clean(self.Input)
-	self.Output = filepath.Clean(self.Output)
+	g.version = version(g.Input)
+	g.Input, _ = filepath.Abs(g.Input)
+	g.Output, _ = filepath.Abs(g.Output)
+	g.Input = filepath.Clean(g.Input)
+	g.Output = filepath.Clean(g.Output)
 
-	if err := walk(self.Input, self.walk); err != nil {
-		self.Logger.Error("%s\n", err)
+	if err := walk(g.Input, g.walk); err != nil {
+		g.L.Error("%s\n", err)
 		return err
 	}
-	self.Logger.Debug("generator state: %+v", self)
+	g.L.Debug("generator state: %+v", g)
 
-	if self.Book {
-		return self.single()
+	if g.Book {
+		return g.single()
 	}
-	return self.multi()
+	return g.multi()
 }

+ 3 - 3
generator_test.go

@@ -87,7 +87,7 @@ func TestWalk(t *testing.T) {
 }
 
 func TestMulti(t *testing.T) {
-	g := Generator{Logger: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
+	g := Generator{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
 
 	// set expected defaults
 	notExist = false
@@ -136,7 +136,7 @@ func TestMulti(t *testing.T) {
 }
 
 func TestSingle(t *testing.T) {
-	g := Generator{Logger: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
+	g := Generator{L: &mockLogger{}, template: &mockTemplate{}, pages: []string{"fuck.md", "deeper/than/index.md", "deeper/than/data.md"}}
 
 	// reset expected defaults
 	statError = nil
@@ -176,7 +176,7 @@ func TestSingle(t *testing.T) {
 }
 
 func TestGenerate(t *testing.T) {
-	g := Generator{Logger: &mockLogger{}}
+	g := Generator{L: &mockLogger{}}
 
 	// set template for stand-alone execution
 	parseTemplate = template.New("test")