Browse Source

kunigu working ok

master
arnaucode 6 years ago
parent
commit
e669faa982
8 changed files with 169 additions and 0 deletions
  1. +18
    -0
      README.md
  2. +12
    -0
      demo/a.txt
  3. +12
    -0
      demo/aOUT.txt
  4. +1
    -0
      demo/b.txt
  5. +7
    -0
      errors.go
  6. +38
    -0
      files.go
  7. +62
    -0
      main.go
  8. +19
    -0
      savelog.go

+ 18
- 0
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}}
```

+ 12
- 0
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

+ 12
- 0
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

+ 1
- 0
demo/b.txt

@ -0,0 +1 @@
hi, this is the b file

+ 7
- 0
errors.go

@ -0,0 +1,7 @@
package main
func check(e error) {
if e != nil {
panic(e)
}
}

+ 38
- 0
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
}

+ 62
- 0
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
}

+ 19
- 0
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)
}

Loading…
Cancel
Save