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) {
if err := markdown.GenerateLinks(md, cfg); err != nil {
return err
}
// Watch file changes for links generation.
if cfg.Development {
markdown.Watch(md, cfg, 0)
} else {
// Watch file changes for links generation if not in development mode.
if !cfg.Development {
markdown.Watch(md, cfg, markdown.DefaultInterval)
}
......
......@@ -4,6 +4,7 @@ package markdown
import (
"io/ioutil"
"log"
"net/http"
"os"
"strings"
......@@ -119,6 +120,13 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
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 filepath, ok := m.StaticFiles[fpath]; ok {
if fs1, err := os.Stat(filepath); err == nil {
......
......@@ -199,7 +199,7 @@ func getTrue() bool {
}
}
// attempt to trigger race condition
// attempt to trigger race conditions
var w sync.WaitGroup
f := func() {
req, err := http.NewRequest("GET", "/log/test.md", nil)
......@@ -217,6 +217,16 @@ func getTrue() bool {
}
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 {
t.Errorf("Error while removing the generated static files: %v", err)
}
......
......@@ -79,6 +79,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
if _, err := os.Stat(fp); os.IsNotExist(err) {
l.Lock()
l.lastErr = err
l.generating = false
l.Unlock()
return
}
......@@ -87,6 +88,9 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
// same hash, return.
if err == nil && hash == cfg.linksHash {
l.Lock()
l.generating = false
l.Unlock()
return
} else if err != nil {
log.Println("Error:", err)
......
......@@ -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{} {
stopChan := make(chan struct{})
if interval > 0 {
ticker := time.NewTicker(interval)
go func() {
loop:
for {
select {
case <-ticker.C:
f()
case <-stopChan:
ticker.Stop()
break loop
}
ticker := time.NewTicker(interval)
go func() {
loop:
for {
select {
case <-ticker.C:
f()
case <-stopChan:
ticker.Stop()
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
}
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