-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewPost.go
56 lines (42 loc) · 1.11 KB
/
newPost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"regexp"
)
var format = "---\r\ntitle: %s\r\nhidden: true\r\n---\r\n"
func check(e error) {
if e != nil {
panic(e)
}
}
var invalids = regexp.MustCompile("[?@#\\/()]|\\(.+\\)")
func makeSlug(s string) string {
s = strings.ToLower(s)
s = strings.Replace(strings.Replace(s, " ", "-", -1), "'", "", -1)
s = invalids.ReplaceAllString(s, "")
return s
}
func main() {
fmt.Print("New post. What's the title?\n")
o, _ := bufio.NewReader(os.Stdin).ReadString('\n')
title := strings.Trim(o, " \r\n")
date := time.Now().Format("2006-01-02")
slang := date + "-" + makeSlug(title)
fmt.Print("Language?\n")
l, _ := bufio.NewReader(os.Stdin).ReadString('\n')
l = strings.Trim(l, " \r\n")
// if not exist
if _, err := os.Stat(l + "/_posts/" + slang + ".md"); err == nil {
panic("Shit! File Exists!")
}
mem := []byte(fmt.Sprintf(format, title))
check(ioutil.WriteFile(fmt.Sprint(l+"/_posts/", slang, ".md"), mem, 0644))
}
// This is a small utility i made to easily create new post
// To make exe:
// go build -o newPost.exe newPost.go