mirror of
https://github.com/arnaucube/kunigu.git
synced 2026-02-06 19:16:44 +01:00
kunigu working ok
This commit is contained in:
18
README.md
Normal file
18
README.md
Normal file
@@ -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}}
|
||||
```
|
||||
12
demo/a.txt
Normal file
12
demo/a.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
{{kunigu demo/b.txt}}
|
||||
6
|
||||
7
|
||||
8
|
||||
{{kunigu demo/b.txt}}
|
||||
9
|
||||
10
|
||||
12
demo/aOUT.txt
Normal file
12
demo/aOUT.txt
Normal file
@@ -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
|
||||
1
demo/b.txt
Normal file
1
demo/b.txt
Normal file
@@ -0,0 +1 @@
|
||||
hi, this is the b file
|
||||
7
errors.go
Executable file
7
errors.go
Executable file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
38
files.go
Normal file
38
files.go
Normal file
@@ -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
|
||||
}
|
||||
62
main.go
Normal file
62
main.go
Normal file
@@ -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
|
||||
}
|
||||
19
savelog.go
Normal file
19
savelog.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user