-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (106 loc) · 3.38 KB
/
main.go
File metadata and controls
116 lines (106 loc) · 3.38 KB
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
package main
import (
"flag"
"fmt"
"github.com/yuhaowin/blog-maker/render"
"github.com/yuhaowin/blog-maker/server"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
)
var (
addr = ":8080"
metaPath = ".meta"
contentFolder = "content"
templatePath = "templates"
postTemplate = "post.html.tpl"
indexTemplate = "index.html.tpl"
outputFolder = flag.String("o", "public", "output folder path")
siteURL = flag.String("url", "", "site URL for RSS feed (e.g., https://example.com)")
siteTitle = flag.String("title", "My Blog", "site title for RSS feed")
siteDesc = flag.String("desc", "My Blog Description", "site description for RSS feed")
)
//----------------------------------------------------------------------------------------------------------------------
func main() {
log.SetFlags(log.Lshortfile)
if len(os.Args) == 2 {
switch os.Args[1] {
case "s":
log.Println("Starting HTTP server at http://localhost:8080/")
err := startServer()
log.Fatal(err)
case "h":
printHelp()
default:
printHelp()
}
} else {
log.Println("Starting generation blog file...")
flag.Parse()
outputPath, _ := filepath.Abs(*outputFolder)
render.RenderWithConfig(cwdPath(templatePath), cwdPath(contentFolder), filepath.Join(outputPath, metaPath), outputPath, *siteURL, *siteTitle, *siteDesc)
}
}
func startServer() error {
server := server.ViewServer{}
server.PostList = make(render.ContentList)
server.PostTemplate = render.GetTemplate(cwdPath(templatePath), postTemplate)
server.IndexTemplate = render.GetTemplate(cwdPath(templatePath), indexTemplate)
server.ContentDir = cwdPath(contentFolder)
server.PostList.UpdateRenderList(server.ContentDir)
// RSS configuration for server mode
server.SiteURL = "http://localhost:8080"
server.SiteTitle = "My Blog"
server.SiteDescription = "My Blog Description"
// Extract years from PostList
yearsMap := make(map[string]bool)
videoYearsMap := make(map[string]bool)
for k, v := range server.PostList {
if !v.IsContent() {
continue
}
paths := strings.Split(k, "/")
if len(paths) == 3 {
// /year/file
yearsMap[paths[1]] = true
} else if len(paths) == 4 && paths[1] == "videos" {
// /videos/year/file
videoYearsMap[paths[2]] = true
}
}
var years []string
for year := range yearsMap {
years = append(years, year)
}
sort.Sort(sort.Reverse(sort.StringSlice(years)))
server.Years = years
var videoYears []string
for year := range videoYearsMap {
videoYears = append(videoYears, year)
}
sort.Sort(sort.Reverse(sort.StringSlice(videoYears)))
server.VideoYears = videoYears
log.Println("RSS feed available at http://localhost:8080/feed.xml")
return http.ListenAndServe(addr, http.HandlerFunc(server.Viewing))
}
//有参数,有返回值
func cwdPath(subPath ...string) string {
// using the function
workDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
for _, p := range subPath {
workDir = filepath.Join(workDir, p)
}
return workDir
}
//无参数,无返回值
func printHelp() {
fmt.Println("./blog-maker s \n", "\trender all markdown files and then start a HTTP server to exhibit your website")
fmt.Println("./blog-maker -o path \n", "\trender all markdown files in content folder to path, default path is ./public")
fmt.Println("./blog-maker -o path -url https://example.com -title \"My Blog\" -desc \"Description\" \n", "\tgenerate RSS feed with site configuration")
}