close #87
This commit is contained in:
		
							parent
							
								
									e3fdadc2d4
								
							
						
					
					
						commit
						3e9cb873b3
					
				| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
document.addEventListener('DOMContentLoaded', event => {
 | 
					document.addEventListener('DOMContentLoaded', event => {
 | 
				
			||||||
    let link = document.querySelector('header > div:first-child a:first-child').getAttribute('href') + "/settings/"
 | 
					    let link = document.querySelector('.only-side > p a:first-child').getAttribute('href') + "/settings/"
 | 
				
			||||||
    document.getElementById('logout').insertAdjacentHTML('beforebegin', `<a href="${link}">
 | 
					    document.getElementById('logout').insertAdjacentHTML('beforebegin', `<a href="${link}">
 | 
				
			||||||
      <div class="action">
 | 
					      <div class="action">
 | 
				
			||||||
       <i class="material-icons">settings</i>
 | 
					       <i class="material-icons">settings</i>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										241
									
								
								setup.go
								
								
								
								
							
							
						
						
									
										241
									
								
								setup.go
								
								
								
								
							| 
						 | 
					@ -5,7 +5,6 @@ import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/exec"
 | 
						"os/exec"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
| 
						 | 
					@ -22,7 +21,10 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AssetsURL is the base url for the assets
 | 
					// AssetsURL is the base url for the assets
 | 
				
			||||||
const AssetsURL = "/_hugointernal"
 | 
					const (
 | 
				
			||||||
 | 
						AssetsURL    = "/_hugointernal"
 | 
				
			||||||
 | 
						HugoNotFound = "It seems that you don't have 'hugo' on your PATH."
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	caddy.RegisterPlugin("hugo", caddy.Plugin{
 | 
						caddy.RegisterPlugin("hugo", caddy.Plugin{
 | 
				
			||||||
| 
						 | 
					@ -35,8 +37,140 @@ func init() {
 | 
				
			||||||
// middleware thing.
 | 
					// middleware thing.
 | 
				
			||||||
func setup(c *caddy.Controller) error {
 | 
					func setup(c *caddy.Controller) error {
 | 
				
			||||||
	cnf := httpserver.GetConfig(c)
 | 
						cnf := httpserver.GetConfig(c)
 | 
				
			||||||
	conf, _ := parse(c, cnf.Root)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						conf, fm, err := parse(c, cnf.Root)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Generates the Hugo website for the first time the plugin is activated.
 | 
				
			||||||
 | 
						go RunHugo(conf, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mid := func(next httpserver.Handler) httpserver.Handler {
 | 
				
			||||||
 | 
							fm.Next = next
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return &Hugo{
 | 
				
			||||||
 | 
								Next:        next,
 | 
				
			||||||
 | 
								Config:      conf,
 | 
				
			||||||
 | 
								FileManager: fm,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cnf.AddMiddleware(mid)
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Config is a configuration for managing a particular hugo website.
 | 
				
			||||||
 | 
					type Config struct {
 | 
				
			||||||
 | 
						Public  string   // Public content path
 | 
				
			||||||
 | 
						Root    string   // Hugo files path
 | 
				
			||||||
 | 
						Hugo    string   // Hugo executable location
 | 
				
			||||||
 | 
						Styles  string   // Admin styles path
 | 
				
			||||||
 | 
						Args    []string // Hugo arguments
 | 
				
			||||||
 | 
						BaseURL string   // BaseURL of admin interface
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Parse parses the configuration set by the user so it can be
 | 
				
			||||||
 | 
					// used by the middleware
 | 
				
			||||||
 | 
					func parse(c *caddy.Controller, root string) (*Config, *filemanager.FileManager, error) {
 | 
				
			||||||
 | 
						var (
 | 
				
			||||||
 | 
							cfg    *Config
 | 
				
			||||||
 | 
							fm     *filemanager.FileManager
 | 
				
			||||||
 | 
							err    error
 | 
				
			||||||
 | 
							tokens string
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cfg = new(Config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if cfg.Hugo, err = exec.LookPath("hugo"); err != nil {
 | 
				
			||||||
 | 
							fmt.Println(HugoNotFound)
 | 
				
			||||||
 | 
							return cfg, fm, errors.New(HugoNotFound)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for c.Next() {
 | 
				
			||||||
 | 
							cfg.Public = strings.Replace(root, "./", "", -1)
 | 
				
			||||||
 | 
							cfg.BaseURL = "/admin"
 | 
				
			||||||
 | 
							cfg.Root = "./"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							args := c.RemainingArgs()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if len(args) >= 1 {
 | 
				
			||||||
 | 
								cfg.Root = args[0]
 | 
				
			||||||
 | 
								cfg.Root = strings.TrimSuffix(cfg.Root, "/")
 | 
				
			||||||
 | 
								cfg.Root += "/"
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if len(args) >= 2 {
 | 
				
			||||||
 | 
								cfg.BaseURL = args[1]
 | 
				
			||||||
 | 
								cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
 | 
				
			||||||
 | 
								cfg.BaseURL = "/" + cfg.BaseURL
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for c.NextBlock() {
 | 
				
			||||||
 | 
								switch c.Val() {
 | 
				
			||||||
 | 
								case "flag":
 | 
				
			||||||
 | 
									if !c.NextArg() {
 | 
				
			||||||
 | 
										return cfg, &filemanager.FileManager{}, c.ArgErr()
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									values := strings.Split(c.Val(), " ")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if len(values) == 0 {
 | 
				
			||||||
 | 
										return cfg, fm, errors.New("Not enough arguments for 'flag' option.")
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									value := "true"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if len(values) > 1 {
 | 
				
			||||||
 | 
										value = values[1]
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									cfg.Args = append(cfg.Args, "--"+values[0]+"="+value)
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									if c.Val() == "show" {
 | 
				
			||||||
 | 
										fmt.Println("Option 'show' is not allowed here.")
 | 
				
			||||||
 | 
										c.NextArg()
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									line := "\n\t" + c.Val()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if c.NextArg() {
 | 
				
			||||||
 | 
										line += " " + c.Val()
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									tokens += line
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						tokens = "filemanager " + cfg.BaseURL + " {\n\tshow " + cfg.Root + tokens
 | 
				
			||||||
 | 
						tokens += "\n}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fmConfig, err := config.Parse(caddy.NewTestController("http", tokens))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return cfg, fm, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fm = &filemanager.FileManager{Configs: fmConfig}
 | 
				
			||||||
 | 
						fm.Configs[0].HugoEnabled = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						format := getFrontMatter(cfg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, user := range fm.Configs[0].Users {
 | 
				
			||||||
 | 
							user.FrontMatter = format
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return cfg, fm, err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return cfg, fm, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getFrontMatter(conf *Config) string {
 | 
				
			||||||
	format := "toml"
 | 
						format := "toml"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Checks if there is an Hugo website in the path that is provided.
 | 
						// Checks if there is an Hugo website in the path that is provided.
 | 
				
			||||||
| 
						 | 
					@ -94,104 +228,5 @@ func setup(c *caddy.Controller) error {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Generates the Hugo website for the first time the plugin is activated.
 | 
						return format
 | 
				
			||||||
	go RunHugo(conf, true)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	mid := func(next httpserver.Handler) httpserver.Handler {
 | 
					 | 
				
			||||||
		return &Hugo{
 | 
					 | 
				
			||||||
			Next:   next,
 | 
					 | 
				
			||||||
			Config: conf,
 | 
					 | 
				
			||||||
			FileManager: &filemanager.FileManager{
 | 
					 | 
				
			||||||
				Next: next,
 | 
					 | 
				
			||||||
				Configs: []config.Config{
 | 
					 | 
				
			||||||
					config.Config{
 | 
					 | 
				
			||||||
						HugoEnabled: true,
 | 
					 | 
				
			||||||
						User: &config.User{
 | 
					 | 
				
			||||||
							PathScope:   conf.Root,
 | 
					 | 
				
			||||||
							FrontMatter: format,
 | 
					 | 
				
			||||||
							Root:        http.Dir(conf.Root),
 | 
					 | 
				
			||||||
							StyleSheet:  conf.Styles,
 | 
					 | 
				
			||||||
						},
 | 
					 | 
				
			||||||
						BaseURL:     conf.BaseURL,
 | 
					 | 
				
			||||||
						AbsoluteURL: strings.Replace(cnf.Addr.Path+"/"+conf.BaseURL, "//", "/", -1),
 | 
					 | 
				
			||||||
						AddrPath:    strings.TrimSuffix(cnf.Addr.Path, "/"),
 | 
					 | 
				
			||||||
					},
 | 
					 | 
				
			||||||
				},
 | 
					 | 
				
			||||||
			},
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	cnf.AddMiddleware(mid)
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Config is a configuration for managing a particular hugo website.
 | 
					 | 
				
			||||||
type Config struct {
 | 
					 | 
				
			||||||
	Public      string   // Public content path
 | 
					 | 
				
			||||||
	Root        string   // Hugo files path
 | 
					 | 
				
			||||||
	Hugo        string   // Hugo executable location
 | 
					 | 
				
			||||||
	Styles      string   // Admin styles path
 | 
					 | 
				
			||||||
	Args        []string // Hugo arguments
 | 
					 | 
				
			||||||
	BaseURL     string   // BaseURL of admin interface
 | 
					 | 
				
			||||||
	FileManager *filemanager.FileManager
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Parse parses the configuration set by the user so it can be
 | 
					 | 
				
			||||||
// used by the middleware
 | 
					 | 
				
			||||||
func parse(c *caddy.Controller, root string) (*Config, error) {
 | 
					 | 
				
			||||||
	conf := &Config{
 | 
					 | 
				
			||||||
		Public:  strings.Replace(root, "./", "", -1),
 | 
					 | 
				
			||||||
		BaseURL: "/admin",
 | 
					 | 
				
			||||||
		Root:    "./",
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if hugo, err := exec.LookPath("hugo"); err == nil {
 | 
					 | 
				
			||||||
		conf.Hugo = hugo
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		fmt.Println("It seems that you don't have 'hugo' on your PATH.")
 | 
					 | 
				
			||||||
		return conf, errors.New("It seems that you don't have 'hugo' on your PATH.")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for c.Next() {
 | 
					 | 
				
			||||||
		args := c.RemainingArgs()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		switch len(args) {
 | 
					 | 
				
			||||||
		case 1:
 | 
					 | 
				
			||||||
			conf.Root = args[0]
 | 
					 | 
				
			||||||
			conf.Root = strings.TrimSuffix(conf.Root, "/")
 | 
					 | 
				
			||||||
			conf.Root += "/"
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		for c.NextBlock() {
 | 
					 | 
				
			||||||
			switch c.Val() {
 | 
					 | 
				
			||||||
			case "styles":
 | 
					 | 
				
			||||||
				if !c.NextArg() {
 | 
					 | 
				
			||||||
					return conf, c.ArgErr()
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				tplBytes, err := ioutil.ReadFile(c.Val())
 | 
					 | 
				
			||||||
				if err != nil {
 | 
					 | 
				
			||||||
					return conf, err
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				conf.Styles = string(tplBytes)
 | 
					 | 
				
			||||||
			case "admin":
 | 
					 | 
				
			||||||
				if !c.NextArg() {
 | 
					 | 
				
			||||||
					return conf, c.ArgErr()
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				conf.BaseURL = c.Val()
 | 
					 | 
				
			||||||
				conf.BaseURL = strings.TrimPrefix(conf.BaseURL, "/")
 | 
					 | 
				
			||||||
				conf.BaseURL = "/" + conf.BaseURL
 | 
					 | 
				
			||||||
			default:
 | 
					 | 
				
			||||||
				key := "--" + c.Val()
 | 
					 | 
				
			||||||
				value := "true"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				if c.NextArg() {
 | 
					 | 
				
			||||||
					value = c.Val()
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				conf.Args = append(conf.Args, key+"="+value)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return conf, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue