<commit_before>// Copyright © 2013 <NAME> <<EMAIL>>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"./hugolib"
	"flag"
	"fmt"
	"github.com/howeyc/fsnotify"
	"net/http"
	"os"
	"path/filepath"
	"runtime/pprof"
	"sync"
)

const (
	cfgFiledefault = "config.yaml"
)

var (
	baseUrl    = flag.String("b", "", "hostname (and path) to the root eg. http://spf13.com/")
	cfgfile    = flag.String("c", cfgFiledefault, "config file (default is path/config.yaml)")
	checkMode  = flag.Bool("k", false, "analyze content and provide feedback")
	draft      = flag.Bool("d", false, "include content marked as draft")
	help       = flag.Bool("h", false, "show this help")
	path       = flag.String("p", "", "filesystem path to read files relative from")
	verbose    = flag.Bool("v", false, "verbose output")
	version    = flag.Bool("version", false, "which version of hugo")
	cpuprofile = flag.Int("cpuprofile", 0, "Number of times to create the site and profile it")
	watchMode  = flag.Bool("w", false, "watch filesystem for changes and recreate as needed")
	server     = flag.Bool("s", false, "run a (very) simple web server")
	port       = flag.String("port", "1313", "port to run web server on, default :1313")
)

func usage() {
	PrintErr("usage: hugo [flags]", "")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {

	flag.Usage = usage
	flag.Parse()

	if *help {
		usage()
	}

	config := hugolib.SetupConfig(cfgfile, path)
	config.BuildDrafts = *draft

	if *baseUrl != "" {
		config.BaseUrl = *baseUrl
	} else if *server {
        config.BaseUrl = "http://localhost:" + *port
    }

	if *version {
		fmt.Println("Hugo Static Site Generator v0.8")
	}

	if *cpuprofile != 0 {
		f, err := os.Create("/tmp/hugo-cpuprofile")

		if err != nil {
			panic(err)
		}

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()

		for i := 0; i < *cpuprofile; i++ {
			_ = buildSite(config)
		}
	}

	if *checkMode {
		site := hugolib.NewSite(config)
		site.Analyze()
		os.Exit(2)
	}

	if *watchMode {
		fmt.Println("Watching for changes. Press ctrl+c to stop")
		_ = buildSite(config)
		err := NewWatcher(config, *port, *server)

		if err != nil {
			fmt.Println(err)
		}
	}

	_ = buildSite(config)

	if *server {
		serve(*port, config)
	}

}

func serve(port string, config *hugolib.Config) {
	fmt.Println("Web Server is available at http://localhost:" + port)
	fmt.Println("Press ctrl+c to stop")
	panic(http.ListenAndServe(":"+port, http.FileServer(http.Dir(config.PublishDir))))
}

func buildSite(config *hugolib.Config) *hugolib.Site {
	site := hugolib.NewSite(config)
	site.Build()

	site.Stats()

	return site
}

func watchChange(c *hugolib.Config) {
	fmt.Println("Change detected, rebuilding site\n")
	buildSite(c)
}

func NewWatcher(c *hugolib.Config, port string, server bool) error {
	watcher, err := fsnotify.NewWatcher()
	var wg sync.WaitGroup

	if err != nil {
		return err
		fmt.Println(err)
	}

	defer watcher.Close()

	wg.Add(1)
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				var _ = ev
				watchChange(c)
				// TODO add newly created directories to the watch list
			case err := <-watcher.Error:
				if err != nil {
					fmt.Println("error:", err)<commit_msg>Adding total time to generated stats.<commit_after>// Copyright © 2013 <NAME> <<EMAIL>>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"./hugolib"
	"flag"
	"fmt"
	"github.com/howeyc/fsnotify"
	"net/http"
	"os"
	"path/filepath"
	"runtime/pprof"
	"sync"
	"time"
)

const (
	cfgFiledefault = "config.yaml"
)

var (
	baseUrl    = flag.String("b", "", "hostname (and path) to the root eg. http://spf13.com/")
	cfgfile    = flag.String("c", cfgFiledefault, "config file (default is path/config.yaml)")
	checkMode  = flag.Bool("k", false, "analyze content and provide feedback")
	draft      = flag.Bool("d", false, "include content marked as draft")
	help       = flag.Bool("h", false, "show this help")
	path       = flag.String("p", "", "filesystem path to read files relative from")
	verbose    = flag.Bool("v", false, "verbose output")
	version    = flag.Bool("version", false, "which version of hugo")
	cpuprofile = flag.Int("cpuprofile", 0, "Number of times to create the site and profile it")
	watchMode  = flag.Bool("w", false, "watch filesystem for changes and recreate as needed")
	server     = flag.Bool("s", false, "run a (very) simple web server")
	port       = flag.String("port", "1313", "port to run web server on, default :1313")
)

func usage() {
	PrintErr("usage: hugo [flags]", "")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {

	flag.Usage = usage
	flag.Parse()

	if *help {
		usage()
	}

	config := hugolib.SetupConfig(cfgfile, path)
	config.BuildDrafts = *draft

	if *baseUrl != "" {
		config.BaseUrl = *baseUrl
	} else if *server {
        config.BaseUrl = "http://localhost:" + *port
    }

	if *version {
		fmt.Println("Hugo Static Site Generator v0.8")
	}

	if *cpuprofile != 0 {
		f, err := os.Create("/tmp/hugo-cpuprofile")

		if err != nil {
			panic(err)
		}

		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()

		for i := 0; i < *cpuprofile; i++ {
			_ = buildSite(config)
		}
	}

	if *checkMode {
		site := hugolib.NewSite(config)
		site.Analyze()
		os.Exit(2)
	}

	if *watchMode {
		fmt.Println("Watching for changes. Press ctrl+c to stop")
		_ = buildSite(config)
		err := NewWatcher(config, *port, *server)

		if err != nil {
			fmt.Println(err)
		}
	}

	_ = buildSite(config)

	if *server {
		serve(*port, config)
	}

}

func serve(port string, config *hugolib.Config) {
	fmt.Println("Web Server is available at http://localhost:" + port)
	fmt.Println("Press ctrl+c to stop")
	panic(http.ListenAndServe(":"+port, http.FileServer(http.Dir(config.PublishDir))))
}

func buildSite(config *hugolib.Config) *hugolib.Site {
    startTime := time.Now()
	site := hugolib.NewSite(config)
	site.Build()
	site.Stats()
	fmt.Printf("in %v ms\n", int(1000 * time.Since(startTime).Seconds()))
	return site
}

func watchChange(c *hugolib.Config) {
	fmt.Println("Change detected, rebuilding site\n")
	buildSite(c)
}

func NewWatcher(c *hugolib.Config, port string, server bool) error {
	watcher, err := fsnotify.NewWatcher()
	var wg sync.WaitGroup

	if err != nil {
		return err
		fmt.Println(err)
	}

	defer watcher.Close()

	wg.Add(1)
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				var _ = ev
				watchChange(c)
				// TODO add newly created directories to the watch list
			case err := <-watcher.Error:
				if err != nil {
					fmt.Println("error:", err)
{'fraction_non_alphanumeric': 0.14558219631739303, 'fraction_numerical': 0.0074182010862365875, 'mean_word_length': 3.5047732696897373, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4051774', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2627, 'n_words': 768}