<commit_before>package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"strconv"

	"github.com/num5/logger"
	"chca/utils"
)

var (
	log *logger.Log
	confile = "config.yml"
)

var chcaStr = `
============================================
*   _______________  __________________	   *
*   __  ____/___  / / /__  ____/___    |   *
*   _  /     __  /_/ / _  /     __  /| |   *
*   / /___   _  __  /  / /___   _  ___ |   *
*   \____/   /_/ /_/   \____/   /_/  |_|   *
*                                          *
*             Simple and fast              *
============================================
`


const (
	HELP = `

Usage:

chca command [args...]

	初始化博客文件夹
    	chca init

	新建 markdown 文件
    	chca new filename

	编译博客
    	chca compile/c

    打开文件监听器
    	chca watch/w

	打开文件服务器
    	chca http/web [port]

    运行chca所有服务，包括内置服务器、监听器
    	chca run [port]

	`
)

func PrintUsage() {
	fmt.Println(HELP)
}

var (
	args []string
)

func main() {

	utils.Green(chcaStr)

	defer func() {
		if err := recover(); err != nil {
			log.Fatalf("panic 错误: %s", err)
		}
	}()

	flag.Parse()
	args = flag.Args()
	if len(args) == 0 || len(args) > 3 {
		PrintUsage()
		os.Exit(1)
	}

	switch args[0] {

	default:
		PrintUsage()
		os.Exit(1)
	case "init":
		new()
	case "new":
		if len(args) == 2 {
			name := args[1]

			CrearteMark(name)
		} else {
			log.Error("缺少文件名")
		}
	case "compile", "c":
		Compile()
	case "watch", "w":
		NewWatch(Config().Paths, Config().Exts).Watcher()
		done := make(chan bool)
		<-done
	case "run":
		Compile()
		NewWatch(Config().Paths, Config().Exts).Watcher()
		var port int = 9900
		if len(args) == 2 {
			p, err := strconv.Atoi(args[1])
			if err != nil {
				panic(err)
			}

			port = p
		}
		_http(port)

	case "http", "web":
		var port int = 9900
		if len(args) == 2 {
			p, err := strconv.Atoi(args[1])
			if err != nil {
				panic(err)
			}

			port = p
		}

		_http(port)
	}
}

func new() {
	createConf()
	createDir()
}

func _http(port int) {

	log.Trac("打开内置web服务器...")

	p := strconv.Itoa(port)

	http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(Config().Html+"/assets/"))))

	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(Config().Html))))

	f := newFileHandler(Config().UploadTheme, Config().Markdown)
	f.Http()

	log.Debugf("打开内置web服务器成功，监听端口 :%d...", port)

	err := http.ListenAndServe(":"+p, nil)

	if err != nil {
		log.Errorf("ListenAndServe: %s", err)
	}
}

func init() {

	// 初始化Log
	log = logger.NewLog(1000)
	// 设置log级别
	log.SetLevel("Debug")
	// 设置输出引擎<commit_msg>add colorlog<commit_after>package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"strconv"

	"github.com/num5/logger"
	"chca/utils"
)

var (
	log *logger.Log
	confile = "config.yml"
)

var chcaStr = `
============================================
*   _______________  __________________	   *
*   __  ____/___  / / /__  ____/___    |   *
*   _  /     __  /_/ / _  /     __  /| |   *
*   / /___   _  __  /  / /___   _  ___ |   *
*   \____/   /_/ /_/   \____/   /_/  |_|   *
*                                          *
*             Simple and fast              *
============================================


`

const (
	HELP = `

Usage:

chca command [args...]

	初始化博客文件夹
    	chca init

	新建 markdown 文件
    	chca new filename

	编译博客
    	chca compile/c

    打开文件监听器
    	chca watch/w

	打开文件服务器
    	chca http/web [port]

    运行chca所有服务，包括内置服务器、监听器
    	chca run [port]

	`
)

func PrintUsage() {
	fmt.Println(HELP)
}

var (
	args []string
)

func main() {

	utils.Green(chcaStr)

	defer func() {
		if err := recover(); err != nil {
			log.Fatalf("panic 错误: %s", err)
		}
	}()

	flag.Parse()
	args = flag.Args()
	if len(args) == 0 || len(args) > 3 {
		PrintUsage()
		os.Exit(1)
	}

	switch args[0] {

	default:
		PrintUsage()
		os.Exit(1)
	case "init":
		new()
	case "new":
		if len(args) == 2 {
			name := args[1]

			CrearteMark(name)
		} else {
			log.Error("缺少文件名")
		}
	case "compile", "c":
		Compile()
	case "watch", "w":
		NewWatch(Config().Paths, Config().Exts).Watcher()
		done := make(chan bool)
		<-done
	case "run":
		Compile()
		NewWatch(Config().Paths, Config().Exts).Watcher()
		var port int = 9900
		if len(args) == 2 {
			p, err := strconv.Atoi(args[1])
			if err != nil {
				panic(err)
			}

			port = p
		}
		_http(port)

	case "http", "web":
		var port int = 9900
		if len(args) == 2 {
			p, err := strconv.Atoi(args[1])
			if err != nil {
				panic(err)
			}

			port = p
		}

		_http(port)
	}
}

func new() {
	createConf()
	createDir()
	log.Debug("初始化成功！")
}

func _http(port int) {

	log.Trac("打开内置web服务器...")

	p := strconv.Itoa(port)

	http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(Config().Html+"/assets/"))))

	http.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(Config().Html))))

	f := newFileHandler(Config().UploadTheme, Config().Markdown)
	f.Http()

	log.Debugf("打开内置web服务器成功，监听端口 :%d...", port)

	err := http.ListenAndServe(":"+p, nil)

	if err != nil {
		log.Errorf("ListenAndServe: %s", err)
	}
}

func init() {

	// 初始化Log
	log = logger.NewLog(1000)
	// 设置log级别
	log.SetLevel("Debug")
	// 设置输出引擎
{'fraction_non_alphanumeric': 0.2103213242453749, 'fraction_numerical': 0.00934761441090555, 'mean_word_length': 2.392338177014531, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2777240', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2211, 'n_words': 385}