コード例 #1
0
ファイル: markdown.go プロジェクト: klauern/caddy
// Markdown configures a new Markdown middleware instance.
func Markdown(c *Controller) (middleware.Middleware, error) {
	mdconfigs, err := markdownParse(c)
	if err != nil {
		return nil, err
	}

	md := markdown.Markdown{
		Root:       c.Root,
		FileSys:    http.Dir(c.Root),
		Configs:    mdconfigs,
		IndexFiles: []string{"index.md"},
	}

	return func(next middleware.Handler) middleware.Handler {
		md.Next = next
		return md
	}, nil
}
コード例 #2
0
ファイル: markdown.go プロジェクト: ronnylt/caddy
// Markdown configures a new Markdown middleware instance.
func Markdown(c *Controller) (middleware.Middleware, error) {
	mdconfigs, err := markdownParse(c)
	if err != nil {
		return nil, err
	}

	md := markdown.Markdown{
		Root:       c.Root,
		FileSys:    http.Dir(c.Root),
		Configs:    mdconfigs,
		IndexFiles: []string{"index.md"},
	}

	// Sweep the whole path at startup to at least generate link index, maybe generate static site
	c.Startup = append(c.Startup, func() error {
		for i := range mdconfigs {
			cfg := &mdconfigs[i]

			// Generate link index and static files (if enabled)
			if err := markdown.GenerateStatic(md, cfg); err != nil {
				return err
			}

			// Watch file changes for static site generation if not in development mode.
			if !cfg.Development {
				markdown.Watch(md, cfg, markdown.DefaultInterval)
			}
		}

		return nil
	})

	return func(next middleware.Handler) middleware.Handler {
		md.Next = next
		return md
	}, nil
}
コード例 #3
0
ファイル: markdown.go プロジェクト: bigtiger/caddy
// Markdown configures a new Markdown middleware instance.
func Markdown(c *Controller) (middleware.Middleware, error) {
	mdconfigs, err := markdownParse(c)
	if err != nil {
		return nil, err
	}

	md := markdown.Markdown{
		Root:       c.Root,
		FileSys:    http.Dir(c.Root),
		Configs:    mdconfigs,
		IndexFiles: []string{"index.md"},
	}

	// For any configs that enabled static site gen, sweep the whole path at startup
	c.Startup = append(c.Startup, func() error {
		for _, cfg := range mdconfigs {
			if cfg.StaticDir == "" {
				continue
			}

			// If generated site already exists, clear it out
			_, err := os.Stat(cfg.StaticDir)
			if err == nil {
				err := os.RemoveAll(cfg.StaticDir)
				if err != nil {
					return err
				}
			}

			fp := filepath.Join(md.Root, cfg.PathScope)
			filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
				for _, ext := range cfg.Extensions {
					if !info.IsDir() && strings.HasSuffix(info.Name(), ext) {
						// Load the file
						body, err := ioutil.ReadFile(path)
						if err != nil {
							return err
						}

						// Get the relative path as if it were a HTTP request,
						// then prepend with "/" (like a real HTTP request)
						reqPath, err := filepath.Rel(md.Root, path)
						if err != nil {
							return err
						}
						reqPath = "/" + reqPath

						// Generate the static file
						_, err = md.Process(cfg, reqPath, body)
						if err != nil {
							return err
						}

						break // don't try other file extensions
					}
				}

				return nil
			})
		}

		return nil
	})

	return func(next middleware.Handler) middleware.Handler {
		md.Next = next
		return md
	}, nil
}