Commit 2ab46659 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Markdown: Modify development mode to generate links on page requests.

parent 851026d3
...@@ -36,10 +36,8 @@ func Markdown(c *Controller) (middleware.Middleware, error) { ...@@ -36,10 +36,8 @@ func Markdown(c *Controller) (middleware.Middleware, error) {
if err := markdown.GenerateLinks(md, cfg); err != nil { if err := markdown.GenerateLinks(md, cfg); err != nil {
return err return err
} }
// Watch file changes for links generation. // Watch file changes for links generation if not in development mode.
if cfg.Development { if !cfg.Development {
markdown.Watch(md, cfg, 0)
} else {
markdown.Watch(md, cfg, markdown.DefaultInterval) markdown.Watch(md, cfg, markdown.DefaultInterval)
} }
......
...@@ -4,6 +4,7 @@ package markdown ...@@ -4,6 +4,7 @@ package markdown
import ( import (
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
...@@ -119,6 +120,13 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error ...@@ -119,6 +120,13 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
return http.StatusNotFound, nil return http.StatusNotFound, nil
} }
// if development is set, scan directory for file changes for links.
if m.Development {
if err := GenerateLinks(md, m); err != nil {
log.Println(err)
}
}
// if static site is generated, attempt to use it // if static site is generated, attempt to use it
if filepath, ok := m.StaticFiles[fpath]; ok { if filepath, ok := m.StaticFiles[fpath]; ok {
if fs1, err := os.Stat(filepath); err == nil { if fs1, err := os.Stat(filepath); err == nil {
......
...@@ -199,7 +199,7 @@ func getTrue() bool { ...@@ -199,7 +199,7 @@ func getTrue() bool {
} }
} }
// attempt to trigger race condition // attempt to trigger race conditions
var w sync.WaitGroup var w sync.WaitGroup
f := func() { f := func() {
req, err := http.NewRequest("GET", "/log/test.md", nil) req, err := http.NewRequest("GET", "/log/test.md", nil)
...@@ -217,6 +217,16 @@ func getTrue() bool { ...@@ -217,6 +217,16 @@ func getTrue() bool {
} }
w.Wait() w.Wait()
f = func() {
GenerateLinks(md, &md.Configs[0])
w.Done()
}
for i := 0; i < 5; i++ {
w.Add(1)
go f()
}
w.Wait()
if err = os.RemoveAll(DefaultStaticDir); err != nil { if err = os.RemoveAll(DefaultStaticDir); err != nil {
t.Errorf("Error while removing the generated static files: %v", err) t.Errorf("Error while removing the generated static files: %v", err)
} }
......
...@@ -79,6 +79,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) { ...@@ -79,6 +79,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
if _, err := os.Stat(fp); os.IsNotExist(err) { if _, err := os.Stat(fp); os.IsNotExist(err) {
l.Lock() l.Lock()
l.lastErr = err l.lastErr = err
l.generating = false
l.Unlock() l.Unlock()
return return
} }
...@@ -87,6 +88,9 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) { ...@@ -87,6 +88,9 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
// same hash, return. // same hash, return.
if err == nil && hash == cfg.linksHash { if err == nil && hash == cfg.linksHash {
l.Lock()
l.generating = false
l.Unlock()
return return
} else if err != nil { } else if err != nil {
log.Println("Error:", err) log.Println("Error:", err)
......
...@@ -20,39 +20,19 @@ func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct ...@@ -20,39 +20,19 @@ func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct
func TickerFunc(interval time.Duration, f func()) chan struct{} { func TickerFunc(interval time.Duration, f func()) chan struct{} {
stopChan := make(chan struct{}) stopChan := make(chan struct{})
if interval > 0 { ticker := time.NewTicker(interval)
ticker := time.NewTicker(interval) go func() {
go func() { loop:
loop: for {
for { select {
select { case <-ticker.C:
case <-ticker.C: f()
f() case <-stopChan:
case <-stopChan: ticker.Stop()
ticker.Stop() break loop
break loop
}
} }
}() }
} else { }()
go func() {
loop:
for {
m := make(chan struct{})
go func() {
f()
m <- struct{}{}
}()
select {
case <-m:
continue loop
case <-stopChan:
break loop
}
time.Sleep(DevInterval)
}
}()
}
return stopChan return stopChan
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment