forked from InkProject/ink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
executable file
·436 lines (412 loc) · 8.95 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/codegangsta/cli"
"github.com/facebookgo/symwalk"
"gopkg.in/yaml.v2"
"html/template"
"time"
)
const (
VERSION = "RELEASE 2018-07-27"
DEFAULT_ROOT = "blog"
DATE_FORMAT_STRING = "2006-01-02 15:04:05"
INDENT = " " // 2 spaces
POST_TEMPLATE = `title: {{.Title}}
date: {{.DateString}}
author: {{.Author}}
{{- if .Cover}}
cover: {{.Cover}}
{{- end}}
draft: {{.Draft}}
top: {{.Top}}
{{- if .Preview}}
preview: {{.Preview}}
{{- end}}
{{- if .Tags}}
{{.Tags}}
{{- end}}
type: {{.Type}}
hide: {{.Hide}}
---
`
)
var globalConfig *GlobalConfig
var rootPath string
func main() {
app := cli.NewApp()
app.Name = "ink"
app.Usage = "An elegant static blog generator"
app.Author = "https://github.com/imeoer"
app.Email = "[email protected]"
app.Version = VERSION
app.Commands = []cli.Command{
{
Name: "build",
Usage: "Generate blog to public folder",
Action: func(c *cli.Context) error {
ParseGlobalConfigByCli(c, false)
Build()
return nil
},
},
{
Name: "preview",
Usage: "Run in server mode to preview blog",
Action: func(c *cli.Context) error {
ParseGlobalConfigByCli(c, true)
Build()
Watch()
Serve()
return nil
},
},
{
Name: "publish",
Usage: "Generate blog to public folder and publish",
Action: func(c *cli.Context) error {
ParseGlobalConfigByCli(c, false)
Build()
Publish()
return nil
},
},
{
Name: "serve",
Usage: "Run in server mode to serve blog",
Action: func(c *cli.Context) error {
ParseGlobalConfigByCli(c, true)
Build()
Serve()
return nil
},
},
{
Name: "convert",
Usage: "Convert Jekyll/Hexo post format to Ink format (Beta)",
Action: func(c *cli.Context) error {
Convert(c)
return nil
},
},
{
Name: "new",
Usage: "Creates a new article",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "hide",
Usage: "Hides the article",
},
cli.BoolFlag{
Name: "top",
Usage: "Places the article at the top",
},
cli.BoolFlag{
Name: "post",
Usage: "The article is a post",
},
cli.BoolFlag{
Name: "page",
Usage: "The article is a page",
},
cli.BoolFlag{
Name: "draft",
Usage: "The article is a draft",
},
cli.StringFlag{
Name: "title",
Usage: "Article title",
},
cli.StringFlag{
Name: "author",
Usage: "Article author",
},
cli.StringFlag{
Name: "cover",
Usage: "Article cover path",
},
cli.StringFlag{
Name: "date",
Usage: "The date and time on which the article was created (2006-01-02 15:04:05)",
},
cli.StringFlag{
Name: "file",
Usage: "The path of where the article will be stored",
},
cli.StringSliceFlag{
Name: "tag",
Usage: "Adds a tag to the article",
},
},
Action: func(c *cli.Context) error {
New(c)
return nil
},
},
}
app.Run(os.Args)
os.Exit(exitCode)
}
func ParseGlobalConfigByCli(c *cli.Context, develop bool) {
if len(c.Args()) > 0 {
rootPath = c.Args()[0]
} else {
rootPath = "."
}
ParseGlobalConfigWrap(rootPath, develop)
if globalConfig == nil {
ParseGlobalConfigWrap(DEFAULT_ROOT, develop)
if globalConfig == nil {
Fatal("Parse config.yml failed, please specify a valid path")
}
}
}
func ParseGlobalConfigWrap(root string, develop bool) {
rootPath = root
globalConfig = ParseGlobalConfig(filepath.Join(rootPath, "config.yml"), develop)
if globalConfig == nil {
return
}
}
func New(c *cli.Context) {
// If source folder does not exist, create
if _, err := os.Stat("source/"); os.IsNotExist(err) {
os.Mkdir("source", os.ModePerm)
}
var author, blogTitle, fileName string
var tags []string
// Default values
draft := "false"
top := "false"
postType := "post"
hide := "false"
date := time.Now()
// Empty string values
preview := ""
cover := ""
// Parse args
args := c.Args()
if len(args) > 0 {
blogTitle = args[0]
}
if blogTitle == "" {
if c.String("title") != "" {
blogTitle = c.String("title")
} else {
Fatal("Please specify the name of the blog post")
}
}
fileName = blogTitle + ".md"
if c.String("file") != "" {
fileName = c.String("file")
}
if len(args) > 1 {
author = args[1]
}
if author == "" {
author = c.String("author")
}
if c.Bool("post") && c.Bool("page") {
Fatal("The post and page arguments are mutually exclusive and cannot appear together")
}
if c.Bool("post") {
postType = "post"
}
if c.Bool("page") {
postType = "page"
}
if c.Bool("hide") {
hide = "true"
}
if c.Bool("draft") {
draft = "true"
}
if c.Bool("top") {
top = "true"
}
if c.String("preview") != "" {
preview = c.String("preview")
}
if c.String("cover") != "" {
cover = c.String("cover")
}
var filePath = "source/" + fileName
file, err := os.Create(filePath)
if err != nil {
Fatal(err)
}
postTemplate, err := template.New("post").Parse(POST_TEMPLATE)
if err != nil {
Fatal(err)
}
if c.StringSlice("tag") != nil {
tags = c.StringSlice("tag")
}
var tagString string
if len(tags) > 0 {
tagString = "tags:"
for _, tag := range tags {
tagString += "\n" + INDENT + "- " + tag
}
}
var dateString string
if c.String("date") != "" {
dateString = c.String("date")
_, err = time.Parse(DATE_FORMAT_STRING, dateString)
if err != nil {
Fatal("Illegal date string")
}
} else {
dateString = date.Format(DATE_FORMAT_STRING)
}
data := map[string]string{
"Title": blogTitle,
"DateString": dateString,
"Author": author,
"Draft": draft,
"Top": top,
"Type": postType,
"Hide": hide,
"Preview": preview,
"Cover": cover,
"Tags": tagString,
}
fileWriter := bufio.NewWriter(file)
err = postTemplate.Execute(fileWriter, data)
if err != nil {
Fatal(err)
}
err = fileWriter.Flush()
if err != nil {
Fatal(err)
}
}
func Publish() {
command := globalConfig.Build.Publish
// Prepare exec command
var shell, flag string
if runtime.GOOS == "windows" {
shell = "cmd"
flag = "/C"
} else {
shell = "/bin/sh"
flag = "-c"
}
cmd := exec.Command(shell, flag, command)
cmd.Dir = filepath.Join(rootPath, globalConfig.Build.Output)
// Start print stdout and stderr of process
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
out := bufio.NewScanner(stdout)
err := bufio.NewScanner(stderr)
// Print stdout
go func() {
for out.Scan() {
Log(out.Text())
}
}()
// Print stdin
go func() {
for err.Scan() {
Log(err.Text())
}
}()
// Exec command
cmd.Run()
}
func Convert(c *cli.Context) {
// Parse arguments
var sourcePath, rootPath string
args := c.Args()
if len(args) > 0 {
sourcePath = args[0]
} else {
Fatal("Please specify the posts source path")
}
if len(args) > 1 {
rootPath = args[1]
} else {
rootPath = "."
}
// Check if path exist
if !Exists(sourcePath) || !Exists(rootPath) {
Fatal("Please specify valid path")
}
// Parse Jekyll/Hexo post file
count := 0
symwalk.Walk(sourcePath, func(path string, f os.FileInfo, err error) error {
fileExt := strings.ToLower(filepath.Ext(path))
if fileExt == ".md" || fileExt == ".html" {
// Read data from file
data, err := ioutil.ReadFile(path)
fileName := filepath.Base(path)
Log("Converting " + fileName)
if err != nil {
Fatal(err.Error())
}
// Split config and markdown
var configStr, contentStr string
content := strings.TrimSpace(string(data))
parseAry := strings.SplitN(content, "---", 3)
parseLen := len(parseAry)
if parseLen == 3 { // Jekyll
configStr = parseAry[1]
contentStr = parseAry[2]
} else if parseLen == 2 { // Hexo
configStr = parseAry[0]
contentStr = parseAry[1]
}
// Parse config
var article ArticleConfig
if err = yaml.Unmarshal([]byte(configStr), &article); err != nil {
Fatal(err.Error())
}
tags := make(map[string]bool)
for _, t := range article.Tags {
tags[t] = true
}
for _, c := range article.Categories {
if _, ok := tags[c]; !ok {
article.Tags = append(article.Tags, c)
}
}
if article.Author == "" {
article.Author = "me"
}
// Convert date
dateAry := strings.SplitN(article.Date, ".", 2)
if len(dateAry) == 2 {
article.Date = dateAry[0]
}
if len(article.Date) == 10 {
article.Date = article.Date + " 00:00:00"
}
if len(article.Date) == 0 {
article.Date = "1970-01-01 00:00:00"
}
article.Update = ""
// Generate Config
var inkConfig []byte
if inkConfig, err = yaml.Marshal(article); err != nil {
Fatal(err.Error())
}
inkConfigStr := string(inkConfig)
markdownStr := inkConfigStr + "\n\n---\n\n" + contentStr + "\n"
targetName := "source/" + fileName
if fileExt != ".md" {
targetName = targetName + ".md"
}
ioutil.WriteFile(filepath.Join(rootPath, targetName), []byte(markdownStr), 0644)
count++
}
return nil
})
fmt.Printf("\nConvert finish, total %v articles\n", count)
}