kunigu working ok
arnaucode arnaucode@gmail.com
Mon, 06 Nov 2017 20:12:13 +0100
8 files changed,
169 insertions(+),
0 deletions(-)
A
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}} +```
A
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
A
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
A
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 +}
A
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 +}
A
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) +}