Use markdown.Config as pointer everywhere

* As value mutex was copied and therefore synchronization worked wrong
* It's pretty big structure with reference types, so copying create unnecessary
  pressure on GC

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov
2015-09-10 15:12:46 -07:00
parent 55de037035
commit da7b9a6bbc
6 changed files with 20 additions and 20 deletions
+2 -2
View File
@@ -102,7 +102,7 @@ func generateStaticHTML(md Markdown, cfg *Config) error {
// Generate the static file
ctx := middleware.Context{Root: md.FileSys}
_, err = md.Process(*cfg, reqPath, body, ctx)
_, err = md.Process(cfg, reqPath, body, ctx)
if err != nil {
return err
}
@@ -115,7 +115,7 @@ func generateStaticHTML(md Markdown, cfg *Config) error {
}
// computeDirHash computes an hash on static directory of c.
func computeDirHash(md Markdown, c Config) (string, error) {
func computeDirHash(md Markdown, c *Config) (string, error) {
dir := filepath.Join(md.Root, c.PathScope)
if _, err := os.Stat(dir); err != nil {
return "", err
+3 -3
View File
@@ -27,7 +27,7 @@ type Markdown struct {
Next middleware.Handler
// The list of markdown configurations
Configs []Config
Configs []*Config
// The list of index files to try
IndexFiles []string
@@ -83,7 +83,7 @@ type Config struct {
// IsValidExt checks to see if an extension is a valid markdown extension
// for config.
func (c Config) IsValidExt(ext string) bool {
func (c *Config) IsValidExt(ext string) bool {
for _, e := range c.Extensions {
if e == ext {
return true
@@ -121,7 +121,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// if development is set, scan directory for file changes for links.
if cfg.Development {
if err := GenerateStatic(md, &cfg); err != nil {
if err := GenerateStatic(md, cfg); err != nil {
log.Println("On-demand generation error (markdown):", err)
}
}
+6 -6
View File
@@ -21,8 +21,8 @@ func TestMarkdown(t *testing.T) {
md := Markdown{
Root: "./testdata",
FileSys: http.Dir("./testdata"),
Configs: []Config{
Config{
Configs: []*Config{
&Config{
Renderer: blackfriday.HtmlRenderer(0, "", ""),
PathScope: "/blog",
Extensions: []string{".md"},
@@ -32,7 +32,7 @@ func TestMarkdown(t *testing.T) {
StaticDir: DefaultStaticDir,
StaticFiles: make(map[string]string),
},
Config{
&Config{
Renderer: blackfriday.HtmlRenderer(0, "", ""),
PathScope: "/log",
Extensions: []string{".md"},
@@ -42,7 +42,7 @@ func TestMarkdown(t *testing.T) {
StaticDir: DefaultStaticDir,
StaticFiles: make(map[string]string),
},
Config{
&Config{
Renderer: blackfriday.HtmlRenderer(0, "", ""),
PathScope: "/og",
Extensions: []string{".md"},
@@ -69,7 +69,7 @@ func TestMarkdown(t *testing.T) {
}
for i := range md.Configs {
c := &md.Configs[i]
c := md.Configs[i]
if err := GenerateStatic(md, c); err != nil {
t.Fatalf("Error: %v", err)
}
@@ -221,7 +221,7 @@ Welcome to title!
w.Wait()
f = func() {
GenerateStatic(md, &md.Configs[0])
GenerateStatic(md, md.Configs[0])
w.Done()
}
for i := 0; i < 5; i++ {
+1 -1
View File
@@ -83,7 +83,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
return false
}
hash, err := computeDirHash(md, *cfg)
hash, err := computeDirHash(md, cfg)
// same hash, return.
if err == nil && hash == cfg.linksHash {
+4 -4
View File
@@ -26,7 +26,7 @@ type MarkdownData struct {
// Process processes the contents of a page in b. It parses the metadata
// (if any) and uses the template (if found).
func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
var metadata = Metadata{Variables: make(map[string]string)}
var markdown []byte
var err error
@@ -82,7 +82,7 @@ func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middlewar
// processTemplate processes a template given a requestPath,
// template (tmpl) and metadata
func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, metadata Metadata, ctx middleware.Context) ([]byte, error) {
func (md Markdown) processTemplate(c *Config, requestPath string, tmpl []byte, metadata Metadata, ctx middleware.Context) ([]byte, error) {
// if template is not specified,
// use the default template
if tmpl == nil {
@@ -123,7 +123,7 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me
// generatePage generates a static html page from the markdown in content if c.StaticDir
// is a non-empty value, meaning that the user enabled static site generation.
func (md Markdown) generatePage(c Config, requestPath string, content []byte) error {
func (md Markdown) generatePage(c *Config, requestPath string, content []byte) error {
// Only generate the page if static site generation is enabled
if c.StaticDir != "" {
// if static directory is not existing, create it
@@ -160,7 +160,7 @@ func (md Markdown) generatePage(c Config, requestPath string, content []byte) er
}
// defaultTemplate constructs a default template.
func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte {
func defaultTemplate(c *Config, metadata Metadata, requestPath string) []byte {
var scripts, styles bytes.Buffer
for _, style := range c.Styles {
styles.WriteString(strings.Replace(cssTemplate, "{{url}}", style, 1))