// +build ignore package main import ( "bufio" "bytes" "fmt" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" "html/template" "io/ioutil" "os" "os/exec" "strings" "time" ) const wikiUrl = "https://github.com/statping/statping.wiki" var vue = ` ` var temp *template.Template type Category struct { String string Pages []*Page } type Page struct { String string Data string } type Render struct { Categories []*Category Pages []*Page Home *Page Footer *Page CreatedAt time.Time } func main() { fmt.Println("RUNNING: ./source/generate_help.go") fmt.Println("\n\nGenerating Help.vue from Statping's Wiki") fmt.Println("Cloning ", wikiUrl) cmd := exec.Command("git", "clone", wikiUrl) cmd.Start() cmd.Wait() fmt.Println("Generating Help view from Wiki") d, _ := ioutil.ReadFile("statping.wiki/_Sidebar.md") var cats []*Category var pages []*Page scanner := bufio.NewScanner(strings.NewReader(string(d))) var thisCategory *Category for scanner.Scan() { txt := scanner.Text() if txt == "" { continue } if txt[0:1] == "#" { newCate := &Category{ String: txt[2:len(txt)], } if txt[2:len(txt)] == "Contact" || txt[2:len(txt)] == "Badges" { continue } thisCategory = newCate cats = append(cats, newCate) } if txt[0:2] == "[[" { file := "statping.wiki/" + txt[2:len(txt)-2] + ".md" file = strings.ReplaceAll(file, " ", "-") page := &Page{ String: txt[2 : len(txt)-2], Data: open(file), } pages = append(pages, page) thisCategory.Pages = append(thisCategory.Pages, page) } } home := &Page{ String: "Home", Data: open("statping.wiki/Home.md"), } footer := &Page{ String: "Footer", Data: open("statping.wiki/_Footer.md"), } w := bytes.NewBufferString("") temp = template.New("wiki") temp.Funcs(template.FuncMap{ "html": func(val string) template.HTML { return template.HTML(val) }, "fake": func(val string) template.HTML { return template.HTML(`{{` + val + `}}`) }, }) temp, _ = temp.Parse(vue) temp.ExecuteTemplate(w, "wiki", Render{Categories: cats, Pages: pages, Home: home, Footer: footer, CreatedAt: time.Now().UTC()}) fmt.Println("Saving wiki page to: ./frontend/src/pages/Home.vue") ioutil.WriteFile("../frontend/src/pages/Help.vue", w.Bytes(), os.FileMode(0755)) fmt.Println("Deleting statping wiki repo") os.RemoveAll("statping.wiki") } func open(filename string) string { htmlFlags := html.CommonFlags | html.HrefTargetBlank opts := html.RendererOptions{Flags: htmlFlags} renderer := html.NewRenderer(opts) d, _ := ioutil.ReadFile(filename) output := markdown.ToHTML(d, nil, renderer) return string(output) }