-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (80 loc) · 2.82 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"flag"
"io/ioutil"
"os"
"github.com/russross/blackfriday"
)
//go:generate go-bindata -prefix "static/" -pkg main -o bindata.go static/...
const FILE_NOT_GIVEN = "You need to give a file name to convert to static site."
type DefaultValues struct {
Input string
Path string
Title string
Author string
Description string
}
type Placeholders struct {
Content string
Title string
Author string
Description string
}
type Config struct {
Placeholder Placeholders
Default DefaultValues
Help DefaultValues
}
func main() {
// Read and parse the config file.
config := readConfig()
// Define the available flags.
inputFile := flag.String("input", config.Default.Input, config.Help.Input)
resultDirectoryName := flag.String("path", config.Default.Path, config.Help.Path)
projectTitle := flag.String("title", config.Default.Title, config.Help.Title)
projectAuthor := flag.String("author", config.Default.Author, config.Help.Author)
projectDescription := flag.String("description", config.Default.Description,
config.Help.Description)
flag.Parse()
// Check if input file exists, and stop if not.
if !fileExists(*inputFile) {
printError("The file '" + *inputFile + "' does not exist.")
return
}
// Read the template HTML file.
template, err := Asset("template.html")
check(err)
// Read the markdown file.
dat, err := ioutil.ReadFile(*inputFile)
check(err)
// Convert markdown to HTML and insert into the template.
html := blackfriday.MarkdownCommon(dat)
result := bytes.Replace(template, []byte(config.Placeholder.Content), html, -1)
// Replace the placeholders with given values.
result = bytes.Replace(result, []byte(config.Placeholder.Title), []byte(*projectTitle), -1)
result = bytes.Replace(result, []byte(config.Placeholder.Author), []byte(*projectAuthor), -1)
result = bytes.Replace(result, []byte(config.Placeholder.Description),
[]byte(*projectDescription), -1)
// Remove if an existing folder exists.
if dirExists(*resultDirectoryName) {
os.RemoveAll(*resultDirectoryName)
}
generateOutputFiles(*resultDirectoryName, result)
printSuccess("Documentation has been created successfully.\n" +
"Generated source files can be found in '" + *resultDirectoryName + "' folder.")
}
// generateOutputFiles copies the required asset files to the specified location.
func generateOutputFiles(directoryPath string, data []byte) {
// Create the result path and write HTML output to that path.
os.Mkdir(directoryPath, 0755)
os.Mkdir(directoryPath+"/css/", 0755)
os.Mkdir(directoryPath+"/js/", 0755)
err := ioutil.WriteFile(directoryPath+"/index.html", data, 0755)
check(err)
// Copy the asset files to the location.
fileNames := [...]string{"css/docs.css"}
for _, filePath := range fileNames {
copyFile(directoryPath, filePath)
}
}