示例#1
0
func main() {
	argparser := flags.NewParser(&opts,
		flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
	argparser.Usage = "[OPTIONS] path/to/config\n\nBuild a site."

	args, err := argparser.Parse()
	if err != nil {
		errhandle(fmt.Errorf("cannot parse flags: %v", err))
		os.Exit(ExitCodeOther)
	}

	if opts.ShowSummary && opts.Watch {
		errhandle(fmt.Errorf("--summary and --watch do not mix together well"))
		os.Exit(ExitCodeOther)
	}

	if opts.Verbose {
		gostatic.DEBUG = true
	}

	if opts.Version {
		out("libgostatic %s\n", gostatic.VERSION)
		return
	}

	if opts.InitExample != nil {
		target, _ := os.Getwd()
		if len(*opts.InitExample) > 0 {
			target = filepath.Join(target, *opts.InitExample)
		}
		gostatic.WriteExample(target)
		return
	}

	gostatic.TemplateFuncMap["paginator"] = processors.CurrentPaginator

	procs := gostatic.ProcessorMap{
		"template":               processors.NewTemplateProcessor(),
		"inner-template":         processors.NewInnerTemplateProcessor(),
		"config":                 processors.NewConfigProcessor(),
		"markdown":               processors.NewMarkdownProcessor(),
		"ext":                    processors.NewExtProcessor(),
		"directorify":            processors.NewDirectorifyProcessor(),
		"tags":                   processors.NewTagsProcessor(),
		"paginate":               processors.NewPaginateProcessor(),
		"paginate-collect-pages": processors.NewPaginateCollectPagesProcessor(),
		"relativize":             processors.NewRelativizeProcessor(),
		"rename":                 processors.NewRenameProcessor(),
		"external":               processors.NewExternalProcessor(),
		"ignore":                 processors.NewIgnoreProcessor(),
	}

	if opts.ShowProcessors {
		procs.ProcessorSummary()
		return
	}

	if len(args) == 0 {
		argparser.WriteHelp(os.Stderr)
		os.Exit(ExitCodeInvalidFlags)
		return
	}

	config, err := gostatic.NewSiteConfig(args[0])
	if err != nil {
		errhandle(fmt.Errorf("invalid config file '%s': %v", args[0], err))
		os.Exit(ExitCodeInvalidConfig)
	}

	site := gostatic.NewSite(config, procs)

	if opts.Force {
		site.ForceRefresh = true
	}

	if opts.ShowConfig {
		x, err := json.MarshalIndent(config, "", "  ")
		errhandle(err)
		fmt.Fprintln(os.Stderr, string(x))
		return
	}

	if len(opts.DumpPage) > 0 {
		page := site.PageBySomePath(opts.DumpPage)
		if page == nil {
			out("Page '%s' not found (supply source or destination path)\n",
				opts.DumpPage)
			return
		}
		dump, err := json.MarshalIndent(page, "", "  ")
		errhandle(err)
		out("%s\n", dump)
		return
	}

	if opts.ShowSummary {
		site.Summary()
	} else {
		site.Render()
	}

	if opts.Watch {
		go gostatic.Watch(site)
		//StartWatcher(config, procs)
		out("Starting server at *:%s...\n", opts.Port)

		fs := http.FileServer(http.Dir(config.Output))
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Cache-Control", "no-store")
			fs.ServeHTTP(w, r)
		})

		err := http.ListenAndServe(":"+opts.Port, nil)
		errhandle(err)
	}
}
示例#2
0
func main() {
	argparser := flags.NewParser(&opts,
		flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
	argparser.Usage = "[OPTIONS] path/to/config\n\nBuild a site."

	args, err := argparser.Parse()

	if err != nil {
		if _, ok := err.(*flags.Error); ok {
			return
		}

		errhandle(fmt.Errorf("unknown error: %v", err))
		os.Exit(ExitCodeOther)
	}

	if opts.ShowSummary && opts.Watch {
		errhandle(fmt.Errorf("--summary and --watch do not mix together well"))
		os.Exit(ExitCodeOther)
	}

	if opts.Verbose {
		gostatic.DEBUG = true
	}

	if opts.Version {
		out("gostatic %s\n", gostatic.VERSION)
		return
	}

	if opts.InitExample != nil {
		target, _ := os.Getwd()
		if len(*opts.InitExample) > 0 {
			target = filepath.Join(target, *opts.InitExample)
		}
		gostatic.WriteExample(target)
		return
	}

	if opts.ShowProcessors {
		processors.DefaultProcessors.ProcessorSummary()
		return
	}

	if len(args) == 0 {
		argparser.WriteHelp(os.Stderr)
		os.Exit(ExitCodeInvalidFlags)
		return
	}

	config, err := gostatic.NewSiteConfig(args[0])
	if err != nil {
		errhandle(fmt.Errorf("invalid config file '%s': %v", args[0], err))
		os.Exit(ExitCodeInvalidConfig)
	}

	site := gostatic.NewSite(config, processors.DefaultProcessors)

	if opts.Force {
		site.ForceRefresh = true
	}

	if opts.ShowConfig {
		x, err := json.MarshalIndent(config, "", "  ")
		errhandle(err)
		fmt.Fprintln(os.Stderr, string(x))
		return
	}

	if len(opts.DumpPage) > 0 {
		page := site.PageBySomePath(opts.DumpPage)
		if page == nil {
			out("Page '%s' not found (supply source or destination path)\n",
				opts.DumpPage)
			return
		}
		dump, err := json.MarshalIndent(page, "", "  ")
		errhandle(err)
		out("%s\n", dump)
		return
	}

	if opts.ShowSummary {
		site.Summary()
	} else {
		site.Render()
	}

	if opts.Watch {
		go gostatic.Watch(site)
		//StartWatcher(config, procs)
		out("Starting server at *:%s...\n", opts.Port)

		fs := http.FileServer(http.Dir(config.Output))
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Cache-Control", "no-store")
			fs.ServeHTTP(w, r)
		})

		err := http.ListenAndServe(":"+opts.Port, nil)
		errhandle(err)
	}
}