From e669faa982ece1fc1bc576b504c7a61082aaf206 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Mon, 6 Nov 2017 20:12:13 +0100 Subject: [PATCH] kunigu working ok --- README.md | 18 +++++++++++++++ demo/a.txt | 12 ++++++++++ demo/aOUT.txt | 12 ++++++++++ demo/b.txt | 1 + errors.go | 7 ++++++ files.go | 38 +++++++++++++++++++++++++++++++ main.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ savelog.go | 19 ++++++++++++++++ 8 files changed, 169 insertions(+) create mode 100644 README.md create mode 100644 demo/a.txt create mode 100644 demo/aOUT.txt create mode 100644 demo/b.txt create mode 100755 errors.go create mode 100644 files.go create mode 100644 main.go create mode 100644 savelog.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5717d0 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# kunigu + +Just need to write +``` +{{kunigu file.html}} +``` +inside the file, and run: +``` +./kunigu +``` + +All the files including the {{kunigu file.html}} will be processed, and will save a copy named fileOUT.html + + +Also, can include Markdown files as HTML content: +``` +{{kunigu @file.html @--md-to-html}} +``` diff --git a/demo/a.txt b/demo/a.txt new file mode 100644 index 0000000..e5d636f --- /dev/null +++ b/demo/a.txt @@ -0,0 +1,12 @@ +1 +2 +3 +4 +5 +{{kunigu demo/b.txt}} +6 +7 +8 +{{kunigu demo/b.txt}} +9 +10 \ No newline at end of file diff --git a/demo/aOUT.txt b/demo/aOUT.txt new file mode 100644 index 0000000..84398c6 --- /dev/null +++ b/demo/aOUT.txt @@ -0,0 +1,12 @@ +1 +2 +3 +4 +5 +hi, this is the b file +6 +7 +8 +hi, this is the b file +9 +10 diff --git a/demo/b.txt b/demo/b.txt new file mode 100644 index 0000000..50e88be --- /dev/null +++ b/demo/b.txt @@ -0,0 +1 @@ +hi, this is the b file \ No newline at end of file diff --git a/errors.go b/errors.go new file mode 100755 index 0000000..d9f9f75 --- /dev/null +++ b/errors.go @@ -0,0 +1,7 @@ +package main + +func check(e error) { + if e != nil { + panic(e) + } +} diff --git a/files.go b/files.go new file mode 100644 index 0000000..b1c4b26 --- /dev/null +++ b/files.go @@ -0,0 +1,38 @@ +package main + +import ( + "io/ioutil" + "strings" + + "github.com/fatih/color" +) + +func readFile(path string) string { + dat, err := ioutil.ReadFile(path) + if err != nil { + color.Red(path) + } + check(err) + return string(dat) +} + +func writeFile(path string, newContent string) { + err := ioutil.WriteFile(path, []byte(newContent), 0644) + check(err) + + color.Green(path) + //color.Blue(newContent) +} + +func getLines(text string) []string { + lines := strings.Split(text, "\n") + return lines +} + +func concatStringsWithJumps(lines []string) string { + var r string + for _, l := range lines { + r = r + l + "\n" + } + return r +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..74e4473 --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "strings" + + blackfriday "gopkg.in/russross/blackfriday.v2" +) + +func main() { + //savelog() + + log.Println("kunigu") + + scrapDirectory(".") +} + +func scrapDirectory(path string) { + filesList, _ := ioutil.ReadDir(path) + for _, f := range filesList { + fileNameSplitted := strings.Split(f.Name(), ".") + if len(fileNameSplitted) > 1 { + if fileNameSplitted[1] == "txt" || fileNameSplitted[1] == "html" { + file := readFile(path + "/" + f.Name()) + if strings.Contains(file, "{{kunigu ") { + fmt.Println(path + "/" + f.Name()) + r := kuniguFile(file) + writeFile(path+"/"+fileNameSplitted[0]+"OUT."+fileNameSplitted[1], r) + } + } + } else { + //is a directory + //fmt.Println(path + "/" + f.Name()) + scrapDirectory(path + "/" + f.Name()) + } + } +} + +func kuniguFile(file string) string { + lines := getLines(file) + var resultL []string + for _, l := range lines { + if strings.Contains(l, "{{kunigu ") { + var htmlcontent string + includefile := strings.Split(l, " @")[1] + includefile = strings.Replace(includefile, "}}", "", -1) + if strings.Contains(l, "--md-to-html") { + mdcontent := readFile(includefile) + htmlcontent = string(blackfriday.Run([]byte(mdcontent))) + } else { + htmlcontent = readFile(includefile) + } + resultL = append(resultL, htmlcontent) + } else { + resultL = append(resultL, l) + } + } + result := concatStringsWithJumps(resultL) + return result +} diff --git a/savelog.go b/savelog.go new file mode 100644 index 0000000..d12c469 --- /dev/null +++ b/savelog.go @@ -0,0 +1,19 @@ +package main + +import ( + "io" + "log" + "os" + "time" +) + +func savelog() { + timeS := time.Now().String() + _ = os.Mkdir("logs", os.ModePerm) + logFile, err := os.OpenFile("logs/log-"+timeS+".log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) + if err != nil { + panic(err) + } + mw := io.MultiWriter(os.Stdout, logFile) + log.SetOutput(mw) +}