forked from anarcher/goblog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
177 lines (157 loc) · 4.91 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
// Copyright 2013 Joshua Marsh. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package main
import (
"fmt"
flag "github.com/ogier/pflag"
"github.com/pyanfield/goblog/archives"
"github.com/pyanfield/goblog/blogs"
"github.com/pyanfield/goblog/fs"
"github.com/pyanfield/goblog/rss"
"github.com/pyanfield/goblog/tags"
"github.com/pyanfield/goblog/templates"
"go/build"
"log"
"os"
"path"
)
var (
LOG = log.New(os.Stderr, "---", log.Ldate|log.Ltime|log.Lshortfile)
ERROR = LOG
)
const (
// 本框架路径
GOBLOG_IMPORT_PATH = "github.com/pyanfield/goblog"
//默认情况下会在 GOPATH下面的 goblog_workspace下面去创建文件夹来存放我们所有的文件
GOBLOG_CONTENT = "goblog_workspace"
)
func main() {
// Parse the flags.
flag.Parse()
setupDirectories()
// First load the templates.
// 返回的是 tmplts 是map[string]*template.Template,一个以模版文件名字为key值的Template的map
tmplts, err := templates.LoadTemplates(TemplateDir)
if err != nil {
fmt.Println("loading templates:", err)
os.Exit(1)
}
// Now, move the static files over.
// 复制 static 文件夹下所有的子文件夹和子文件到 public 文件夹下
err = fs.CopyFilesRecursively(OutputDir, StaticDir)
if err != nil {
fmt.Println("making output dir:", err)
os.Exit(1)
}
// Get a list of files from the BlogDir.
// 得到Blog文件夹下的所有md文件列表,如果在Blog下有子文件夹,那么这个文件夹的名字作为前缀,以"-"为连接符,形成新的文件名
entries, err := blogs.GetBlogFiles(BlogDir)
if err != nil {
fmt.Println("getting blog file list:", err)
os.Exit(1)
}
// Iteratively Parse each blog for it's useful data and generate a
// page for each blog.
// 遍历 []*BlogEntry{}
for _, blog := range entries {
// 解析 md 文件的内容,并且获取一些描述信息,比如 title, author ,date等等
contents, err := blog.Parse()
if err != nil {
fmt.Println("parsing blog", blog, ":", err)
os.Exit(1)
}
err = tmplts.MakeBlogEntry(OutputDir, blog, contents)
if err != nil {
fmt.Println("generating blog html", blog, ":", err)
os.Exit(1)
}
}
// Generate the about page.
err = tmplts.MakeAbout(OutputDir)
if err != nil {
fmt.Println("generating about.html:", err)
os.Exit(1)
}
// Get a sorted list of tags.
tagentries := tags.ParseBlogs(entries)
t := tagentries.Slice()
// Generate the tags page.
err = tmplts.MakeTags(OutputDir, t)
if err != nil {
fmt.Println("generating tags.html:", err)
os.Exit(1)
}
// Get a sort list of archives.
dateentries := archives.ParseBlogs(entries)
a := dateentries.Slice()
// Generate the archive page.
err = tmplts.MakeArchive(OutputDir, a)
if err != nil {
fmt.Println("generating archive.html:", err)
os.Exit(1)
}
// Generate the index page.
mostRecent := archives.GetMostRecent(a, MaxIndexEntries)
err = tmplts.MakeIndex(OutputDir, mostRecent)
if err != nil {
fmt.Println("generating index.html:", err)
os.Exit(1)
}
// Generate the RSS feed.
err = rss.MakeRss(archives.GetMostRecent(a, 10), URL, TemplateDir, OutputDir)
if err != nil {
fmt.Println("generating feed.rss:", err)
fmt.Println("no rss will be available")
}
}
// SetupDirectories is a helper function that prepends the working
// directory to the other directories.
func setupDirectories() {
// 检查是否通过命令输入了 WorkingDir ,如果没有,那就按照 GOBLOG_CONTENT建立博客的文件结构
if WorkingDir == "./" {
src := findSrcPath()
if err := fs.MakeDirIfNotExists(GOBLOG_CONTENT); err != nil {
ERROR.Fatalln("make diretory blog content:", err)
}
WorkingDir = path.Join(src, GOBLOG_CONTENT)
}
OutputDir = path.Join(WorkingDir, OutputDir)
// Next, let's clear out the OutputDir if requested.
if EmptyOutputDir {
// 删除当前路径及其路径下的所有子文件。如果这个路径不存在将返回nil
err := os.RemoveAll(OutputDir)
if err != nil {
fmt.Println("cleaning output dir:", err)
os.Exit(1)
}
}
if err := fs.MakeDirIfNotExists(OutputDir); err != nil {
ERROR.Fatalln(err)
}
TemplateDir = path.Join(WorkingDir, TemplateDir)
if err := fs.MakeDirIfNotExists(TemplateDir); err != nil {
ERROR.Fatalln(err)
}
StaticDir = path.Join(WorkingDir, StaticDir)
if err := fs.MakeDirIfNotExists(StaticDir); err != nil {
ERROR.Fatalln(err)
}
BlogDir = path.Join(WorkingDir, BlogDir)
if err := fs.MakeDirIfNotExists(BlogDir); err != nil {
ERROR.Fatalln(err)
}
}
//查找本项目的源地址
func findSrcPath() string {
// 检查是否定义了 GOPATH 环境变量
if gopath := os.Getenv("GOPATH"); gopath == "" {
ERROR.Fatalln("Please set GOPATH first")
}
goblogPkg, err := build.Import(GOBLOG_IMPORT_PATH, "", build.FindOnly)
// LOG.Println("appPkg >> ", goblogPkg.SrcRoot)
if err != nil {
ERROR.Fatalln("Failed to import", "", "with error:", err)
}
return goblogPkg.SrcRoot
}