|
@@ -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()
|
|
|
}
|