-
Notifications
You must be signed in to change notification settings - Fork 4
/
gravizool.go
76 lines (63 loc) · 2.02 KB
/
gravizool.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
const gravizoolVersion = "1.0.5"
const gravizoBegin string = ".gravizo.com/svg?"
const gravizoEnd string = "enduml"
var gravizoEncode = strings.NewReplacer(";", "%3B", " ", "%20", "\n", "%0A", "@", "%40",
"(", "%28", ")", "%29", "*", "%2A", "\\", "%5C", "<", "%3C", ">", "%3E", "\"", "%22")
var gravizoDecode = strings.NewReplacer("%3B", ";", "%20", " ", "%0A", "\n", "%40", "@",
"%28", "(", "%29", ")", "%2A", "*", "%5C", "\\", "%3C", "<", "%3E", ">", "%22", "\"")
var gravizoFixEncode = strings.NewReplacer("\n", ";\n")
var gravizoFixDecode = strings.NewReplacer(";", "")
func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func convert(filename string, replacer *strings.Replacer, backup bool) {
buffer, err := ioutil.ReadFile(filename)
check(err)
if backup {
err = ioutil.WriteFile(fmt.Sprint(filename+".bak"), buffer, 0644)
check(err)
}
text := string(buffer)
for _, slice := range strings.Split(text, gravizoBegin) {
if strings.Contains(slice, gravizoEnd) {
subSlice := strings.Split(slice, gravizoEnd)
if len(subSlice) > 0 {
gravizoText := subSlice[0]
convertedText := replacer.Replace(gravizoText)
text = strings.Replace(text, gravizoText, convertedText, -1)
}
}
}
err = ioutil.WriteFile(filename, []byte(text), 0644)
check(err)
}
func main() {
encode := flag.String("e", "", "Encode the GitHub Markdown file")
decode := flag.String("d", "", "Decode the GitHub Markdown file")
fix := flag.String("f", "", "Fix the GitHub Markdown file")
backup := flag.Bool("b", true, "Backup the GitHub Markdown file")
version := flag.Bool("v", false, "Print the program version")
flag.Parse()
if len(*encode) > 0 {
convert(*encode, gravizoEncode, *backup)
} else if len(*decode) > 0 {
convert(*decode, gravizoDecode, *backup)
} else if len(*fix) > 0 {
convert(*fix, gravizoFixDecode, *backup)
convert(*fix, gravizoFixEncode, *backup)
} else if *version {
fmt.Println(gravizoolVersion)
os.Exit(0)
}
}