2015-08-05 06:35:09 +08:00
|
|
|
package markdown
|
|
|
|
|
2015-08-05 16:55:04 +08:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
2015-08-05 06:35:09 +08:00
|
|
|
|
2015-10-10 06:35:34 +08:00
|
|
|
// DefaultInterval is the default interval at which the markdown watcher
|
|
|
|
// checks for changes.
|
2015-08-05 08:00:53 +08:00
|
|
|
const DefaultInterval = time.Second * 60
|
2015-08-05 06:35:09 +08:00
|
|
|
|
|
|
|
// Watch monitors the configured markdown directory for changes. It calls GenerateLinks
|
|
|
|
// when there are changes.
|
|
|
|
func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct{}) {
|
|
|
|
return TickerFunc(interval, func() {
|
2015-08-05 16:55:04 +08:00
|
|
|
if err := GenerateStatic(md, c); err != nil {
|
2015-11-06 08:01:08 +08:00
|
|
|
log.Printf("[ERROR] markdown: Re-generating static site: %v", err)
|
2015-08-05 16:55:04 +08:00
|
|
|
}
|
2015-08-05 06:35:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-05 16:55:04 +08:00
|
|
|
// TickerFunc runs f at interval. A message to the returned channel will stop the
|
|
|
|
// executing goroutine.
|
2015-08-05 06:35:09 +08:00
|
|
|
func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
|
|
|
stopChan := make(chan struct{})
|
|
|
|
|
2015-08-05 07:41:04 +08:00
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
go func() {
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
f()
|
|
|
|
case <-stopChan:
|
|
|
|
ticker.Stop()
|
|
|
|
break loop
|
2015-08-05 06:35:09 +08:00
|
|
|
}
|
2015-08-05 07:41:04 +08:00
|
|
|
}
|
|
|
|
}()
|
2015-08-05 06:35:09 +08:00
|
|
|
|
|
|
|
return stopChan
|
|
|
|
}
|