1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 21:34:45 +01:00
This commit is contained in:
Oliver Steele 2017-07-09 09:37:23 -04:00
parent 3db6096dba
commit bec42f24d7
8 changed files with 68 additions and 52 deletions

View File

@ -92,7 +92,7 @@ func renderCommand(site *site.Site) error {
logger.path("Render:", filepath.Join(site.SourceDir(), p.SourcePath()))
logger.label("URL:", p.Permalink())
logger.label("Content:", "")
return site.WriteDocument(p, os.Stdout)
return site.WriteDocument(os.Stdout, p)
}
// If path starts with /, it's a URL path. Else it's a file path relative

View File

@ -13,8 +13,8 @@ func (c *Config) IsMarkdown(name string) bool {
return c.markdownExtensions()[strings.TrimLeft(ext, ".")]
}
// IsSassPath returns a boolean indicating whether the file is a Sass (".sass" or ".scss") file.
func (c *Config) IsSassPath(name string) bool {
// IsSASSPath returns a boolean indicating whether the file is a Sass (".sass" or ".scss") file.
func (c *Config) IsSASSPath(name string) bool {
return strings.HasSuffix(name, ".sass") || strings.HasSuffix(name, ".scss")
}
@ -39,7 +39,7 @@ func (c *Config) OutputExt(pathname string) string {
switch {
case c.IsMarkdown(pathname):
return ".html"
case c.IsSassPath(pathname):
case c.IsSASSPath(pathname):
return ".css"
default:
return filepath.Ext(pathname)

View File

@ -83,11 +83,17 @@ outer:
case html.TextToken:
if body {
s := (string(z.Text()))
buf.WriteString((fn(s)))
_, err := buf.WriteString((fn(s)))
if err != nil {
panic(err)
}
continue outer
}
}
buf.Write(z.Raw())
_, err := buf.Write(z.Raw())
if err != nil {
panic(err)
}
}
return buf.Bytes()
}

View File

@ -60,7 +60,7 @@ func (p *Pipeline) OutputExt(pathname string) string {
// Render returns nil iff it wrote to the writer
func (p *Pipeline) Render(w io.Writer, b []byte, filename string, e map[string]interface{}) ([]byte, error) {
if p.config.IsSassPath(filename) {
if p.config.IsSASSPath(filename) {
return nil, p.WriteSass(w, b)
}
b, err := p.renderTemplate(b, e, filename)

View File

@ -31,7 +31,7 @@ func (s *Server) Run(open bool, logger func(label, value string)) error {
if err := s.StartLiveReloader(); err != nil {
return err
}
if err := s.watchFiles(); err != nil {
if err := s.watchAndReload(); err != nil {
return err
}
http.HandleFunc("/", s.handler)
@ -76,7 +76,7 @@ func (s *Server) handler(rw http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(mimeType, "text/html;") {
w = NewLiveReloadInjector(w)
}
err := site.WriteDocument(p, w)
err := site.WriteDocument(w, p)
if err != nil {
fmt.Printf("Error rendering %s: %s", urlpath, err)
}

View File

@ -8,18 +8,37 @@ import (
"github.com/osteele/gojekyll/helpers"
)
func (s *Server) watchFiles() error {
func (s *Server) watchAndReload() error {
site := s.Site
return s.watchFiles(func(filenames []string) {
// Resolve to URLS *before* reloading the site, in case the latter
// remaps permalinks.
urls := map[string]bool{}
for _, relpath := range filenames {
url, ok := site.FilenameURLPath(relpath)
if ok {
urls[url] = true
}
}
s.reloadSite()
for url := range urls {
s.lr.Reload(url)
}
})
}
// calls fn repeatedly in a goroutine
func (s *Server) watchFiles(fn func([]string)) error {
var (
site = s.Site
sourceDir = site.SourceDir()
events = make(chan string)
debounced = debounce(time.Second, events)
)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go func() {
for {
select {
@ -30,30 +49,20 @@ func (s *Server) watchFiles() error {
}
}
}()
go func() {
for {
filenames := <-debounced
// Resolve to URLS *before* reloading the site, in case the latter
// remaps permalinks.
urls := map[string]bool{}
relpaths := make([]string, 0, len(filenames))
for _, filename := range filenames {
relpath := helpers.MustRel(site.SourceDir(), filename)
url, found := site.FilenameURLPath(relpath)
if !found {
// TODO don't warn re config and excluded files
log.Println("error:", filename, "does not match a site URL")
relpath := helpers.MustRel(sourceDir, filename)
if relpath == "_config.yml" || !site.Exclude(relpath) {
relpaths = append(relpaths, relpath)
}
urls[url] = true
}
s.reloadSite()
for url := range urls {
s.lr.Reload(url)
}
fn(relpaths)
}
}()
return watcher.Add(site.SourceDir())
return watcher.Add(sourceDir)
}
// debounce relays values from input to output, merging successive values within interval

View File

@ -25,11 +25,23 @@ func (s *Site) prepareRendering() error {
return nil
}
// WriteDocument writes the document to w.
func (s *Site) WriteDocument(p pages.Document, w io.Writer) error {
// WriteDocument writes the document to a writer.
func (s *Site) WriteDocument(w io.Writer, p pages.Document) error {
if err := s.prepareRendering(); err != nil {
return err
}
buf := new(bytes.Buffer)
if err := p.Write(buf, s); err != nil {
return err
}
c := buf.Bytes()
err := s.runHooks(func(p plugins.Plugin) error {
c = p.PostRender(c)
return nil
})
if err != nil {
return err
}
return p.Write(w, s)
}
@ -40,7 +52,7 @@ func (s *Site) WritePages(options BuildOptions) (count int, err error) {
for _, p := range s.OutputPages() {
count++
go func(p pages.Document) {
errs <- s.WritePage(p, options)
errs <- s.SavePage(p, options)
}(p)
}
for i := 0; i < count; i++ {
@ -53,9 +65,9 @@ func (s *Site) WritePages(options BuildOptions) (count int, err error) {
return count, err
}
// WriteDocument writes a document to the destination directory.
// SavePage writes a document to the destination directory.
// It attends to options.dry_run.
func (s *Site) WritePage(p pages.Document, options BuildOptions) error {
func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
from := filepath.Join(s.SourceDir(), filepath.ToSlash(p.SourcePath()))
to := filepath.Join(s.DestDir(), p.Permalink())
if !p.Static() && filepath.Ext(to) == "" {
@ -78,25 +90,13 @@ func (s *Site) WritePage(p pages.Document, options BuildOptions) error {
case p.Static():
return helpers.CopyFileContents(to, from, 0644)
default:
buf := new(bytes.Buffer)
if err := p.Write(buf, s); err != nil {
return err
}
c := buf.Bytes()
err := s.runHooks(func(p plugins.Plugin) error {
c = p.PostRender(c)
return nil
})
if err != nil {
return err
}
return helpers.VisitCreatedFile(to, func(w io.Writer) error {
_, err := w.Write(c)
return err
})
return s.SaveDocumentToFile(p, to)
}
}
// // WritePage writes a page to the destination directory.
// func (s *Site) WritePage(p pages.Page) error {
// }
// SaveDocumentToFile writes a page to filename.
func (s *Site) SaveDocumentToFile(d pages.Document, filename string) error {
return helpers.VisitCreatedFile(filename, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
}

1
version_test.go Normal file
View File

@ -0,0 +1 @@
package gojekyll