mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-26 08:12:43 -04:00 
			
		
		
		
	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:
		
							parent
							
								
									55de037035
								
							
						
					
					
						commit
						da7b9a6bbc
					
				| @ -28,7 +28,7 @@ func Markdown(c *Controller) (middleware.Middleware, error) { | |||||||
| 	// Sweep the whole path at startup to at least generate link index, maybe generate static site | 	// Sweep the whole path at startup to at least generate link index, maybe generate static site | ||||||
| 	c.Startup = append(c.Startup, func() error { | 	c.Startup = append(c.Startup, func() error { | ||||||
| 		for i := range mdconfigs { | 		for i := range mdconfigs { | ||||||
| 			cfg := &mdconfigs[i] | 			cfg := mdconfigs[i] | ||||||
| 
 | 
 | ||||||
| 			// Generate link index and static files (if enabled) | 			// Generate link index and static files (if enabled) | ||||||
| 			if err := markdown.GenerateStatic(md, cfg); err != nil { | 			if err := markdown.GenerateStatic(md, cfg); err != nil { | ||||||
| @ -50,11 +50,11 @@ func Markdown(c *Controller) (middleware.Middleware, error) { | |||||||
| 	}, nil | 	}, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func markdownParse(c *Controller) ([]markdown.Config, error) { | func markdownParse(c *Controller) ([]*markdown.Config, error) { | ||||||
| 	var mdconfigs []markdown.Config | 	var mdconfigs []*markdown.Config | ||||||
| 
 | 
 | ||||||
| 	for c.Next() { | 	for c.Next() { | ||||||
| 		md := markdown.Config{ | 		md := &markdown.Config{ | ||||||
| 			Renderer:    blackfriday.HtmlRenderer(0, "", ""), | 			Renderer:    blackfriday.HtmlRenderer(0, "", ""), | ||||||
| 			Templates:   make(map[string]string), | 			Templates:   make(map[string]string), | ||||||
| 			StaticFiles: make(map[string]string), | 			StaticFiles: make(map[string]string), | ||||||
|  | |||||||
| @ -102,7 +102,7 @@ func generateStaticHTML(md Markdown, cfg *Config) error { | |||||||
| 
 | 
 | ||||||
| 				// Generate the static file | 				// Generate the static file | ||||||
| 				ctx := middleware.Context{Root: md.FileSys} | 				ctx := middleware.Context{Root: md.FileSys} | ||||||
| 				_, err = md.Process(*cfg, reqPath, body, ctx) | 				_, err = md.Process(cfg, reqPath, body, ctx) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					return err | 					return err | ||||||
| 				} | 				} | ||||||
| @ -115,7 +115,7 @@ func generateStaticHTML(md Markdown, cfg *Config) error { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // computeDirHash computes an hash on static directory of c. | // 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) | 	dir := filepath.Join(md.Root, c.PathScope) | ||||||
| 	if _, err := os.Stat(dir); err != nil { | 	if _, err := os.Stat(dir); err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
|  | |||||||
| @ -27,7 +27,7 @@ type Markdown struct { | |||||||
| 	Next middleware.Handler | 	Next middleware.Handler | ||||||
| 
 | 
 | ||||||
| 	// The list of markdown configurations | 	// The list of markdown configurations | ||||||
| 	Configs []Config | 	Configs []*Config | ||||||
| 
 | 
 | ||||||
| 	// The list of index files to try | 	// The list of index files to try | ||||||
| 	IndexFiles []string | 	IndexFiles []string | ||||||
| @ -83,7 +83,7 @@ type Config struct { | |||||||
| 
 | 
 | ||||||
| // IsValidExt checks to see if an extension is a valid markdown extension | // IsValidExt checks to see if an extension is a valid markdown extension | ||||||
| // for config. | // for config. | ||||||
| func (c Config) IsValidExt(ext string) bool { | func (c *Config) IsValidExt(ext string) bool { | ||||||
| 	for _, e := range c.Extensions { | 	for _, e := range c.Extensions { | ||||||
| 		if e == ext { | 		if e == ext { | ||||||
| 			return true | 			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 development is set, scan directory for file changes for links. | ||||||
| 				if cfg.Development { | 				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) | 						log.Println("On-demand generation error (markdown):", err) | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  | |||||||
| @ -21,8 +21,8 @@ func TestMarkdown(t *testing.T) { | |||||||
| 	md := Markdown{ | 	md := Markdown{ | ||||||
| 		Root:    "./testdata", | 		Root:    "./testdata", | ||||||
| 		FileSys: http.Dir("./testdata"), | 		FileSys: http.Dir("./testdata"), | ||||||
| 		Configs: []Config{ | 		Configs: []*Config{ | ||||||
| 			Config{ | 			&Config{ | ||||||
| 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | ||||||
| 				PathScope:   "/blog", | 				PathScope:   "/blog", | ||||||
| 				Extensions:  []string{".md"}, | 				Extensions:  []string{".md"}, | ||||||
| @ -32,7 +32,7 @@ func TestMarkdown(t *testing.T) { | |||||||
| 				StaticDir:   DefaultStaticDir, | 				StaticDir:   DefaultStaticDir, | ||||||
| 				StaticFiles: make(map[string]string), | 				StaticFiles: make(map[string]string), | ||||||
| 			}, | 			}, | ||||||
| 			Config{ | 			&Config{ | ||||||
| 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | ||||||
| 				PathScope:   "/log", | 				PathScope:   "/log", | ||||||
| 				Extensions:  []string{".md"}, | 				Extensions:  []string{".md"}, | ||||||
| @ -42,7 +42,7 @@ func TestMarkdown(t *testing.T) { | |||||||
| 				StaticDir:   DefaultStaticDir, | 				StaticDir:   DefaultStaticDir, | ||||||
| 				StaticFiles: make(map[string]string), | 				StaticFiles: make(map[string]string), | ||||||
| 			}, | 			}, | ||||||
| 			Config{ | 			&Config{ | ||||||
| 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | 				Renderer:    blackfriday.HtmlRenderer(0, "", ""), | ||||||
| 				PathScope:   "/og", | 				PathScope:   "/og", | ||||||
| 				Extensions:  []string{".md"}, | 				Extensions:  []string{".md"}, | ||||||
| @ -69,7 +69,7 @@ func TestMarkdown(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for i := range md.Configs { | 	for i := range md.Configs { | ||||||
| 		c := &md.Configs[i] | 		c := md.Configs[i] | ||||||
| 		if err := GenerateStatic(md, c); err != nil { | 		if err := GenerateStatic(md, c); err != nil { | ||||||
| 			t.Fatalf("Error: %v", err) | 			t.Fatalf("Error: %v", err) | ||||||
| 		} | 		} | ||||||
| @ -221,7 +221,7 @@ Welcome to title! | |||||||
| 	w.Wait() | 	w.Wait() | ||||||
| 
 | 
 | ||||||
| 	f = func() { | 	f = func() { | ||||||
| 		GenerateStatic(md, &md.Configs[0]) | 		GenerateStatic(md, md.Configs[0]) | ||||||
| 		w.Done() | 		w.Done() | ||||||
| 	} | 	} | ||||||
| 	for i := 0; i < 5; i++ { | 	for i := 0; i < 5; i++ { | ||||||
|  | |||||||
| @ -83,7 +83,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool { | |||||||
| 		return false | 		return false | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	hash, err := computeDirHash(md, *cfg) | 	hash, err := computeDirHash(md, cfg) | ||||||
| 
 | 
 | ||||||
| 	// same hash, return. | 	// same hash, return. | ||||||
| 	if err == nil && hash == cfg.linksHash { | 	if err == nil && hash == cfg.linksHash { | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ type MarkdownData struct { | |||||||
| 
 | 
 | ||||||
| // Process processes the contents of a page in b. It parses the metadata | // Process processes the contents of a page in b. It parses the metadata | ||||||
| // (if any) and uses the template (if found). | // (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 metadata = Metadata{Variables: make(map[string]string)} | ||||||
| 	var markdown []byte | 	var markdown []byte | ||||||
| 	var err error | 	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, | // processTemplate processes a template given a requestPath, | ||||||
| // template (tmpl) and metadata | // 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, | 	// if template is not specified, | ||||||
| 	// use the default template | 	// use the default template | ||||||
| 	if tmpl == nil { | 	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 | // 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. | // 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 | 	// Only generate the page if static site generation is enabled | ||||||
| 	if c.StaticDir != "" { | 	if c.StaticDir != "" { | ||||||
| 		// if static directory is not existing, create it | 		// 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. | // 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 | 	var scripts, styles bytes.Buffer | ||||||
| 	for _, style := range c.Styles { | 	for _, style := range c.Styles { | ||||||
| 		styles.WriteString(strings.Replace(cssTemplate, "{{url}}", style, 1)) | 		styles.WriteString(strings.Replace(cssTemplate, "{{url}}", style, 1)) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user