arnaucube
arnaucube/kuniguPublic Self-hosted repository
kunigu/main.go
main.goRaw
 1package main
 2
 3import (
 4	"fmt"
 5	"io/ioutil"
 6	"log"
 7	"strings"
 8
 9	blackfriday "gopkg.in/russross/blackfriday.v2"
10)
11
12func main() {
13	//savelog()
14
15	log.Println("kunigu")
16
17	scrapDirectory(".")
18}
19
20func scrapDirectory(path string) {
21	filesList, _ := ioutil.ReadDir(path)
22	for _, f := range filesList {
23		fileNameSplitted := strings.Split(f.Name(), ".")
24		if len(fileNameSplitted) > 1 {
25			if fileNameSplitted[1] == "txt" || fileNameSplitted[1] == "html" {
26				file := readFile(path + "/" + f.Name())
27				if strings.Contains(file, "{{kunigu ") {
28					fmt.Println(path + "/" + f.Name())
29					r := kuniguFile(file)
30					writeFile(path+"/"+fileNameSplitted[0]+"OUT."+fileNameSplitted[1], r)
31				}
32			}
33		} else {
34			//is a directory
35			//fmt.Println(path + "/" + f.Name())
36			scrapDirectory(path + "/" + f.Name())
37		}
38	}
39}
40
41func kuniguFile(file string) string {
42	lines := getLines(file)
43	var resultL []string
44	for _, l := range lines {
45		if strings.Contains(l, "{{kunigu ") {
46			var htmlcontent string
47			includefile := strings.Split(l, " @")[1]
48			includefile = strings.Replace(includefile, "}}", "", -1)
49			if strings.Contains(l, "--md-to-html") {
50				mdcontent := readFile(includefile)
51				htmlcontent = string(blackfriday.Run([]byte(mdcontent)))
52			} else {
53				htmlcontent = readFile(includefile)
54			}
55			resultL = append(resultL, htmlcontent)
56		} else {
57			resultL = append(resultL, l)
58		}
59	}
60	result := concatStringsWithJumps(resultL)
61	return result
62}