forked from InkProject/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
executable file
·232 lines (216 loc) · 5.1 KB
/
parse.go
File metadata and controls
executable file
·232 lines (216 loc) · 5.1 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
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
package main
import (
"html/template"
"io/ioutil"
"path/filepath"
"strings"
"time"
"github.com/InkProject/blackfriday"
"gopkg.in/yaml.v2"
)
type SiteConfig struct {
Root string
Title string
Subtitle string
Logo string
Limit int
Theme string
Comment string
Lang string
Url string
Link string
Config interface{}
}
type AuthorConfig struct {
Id string
Name string
Intro string
Avatar string
}
type BuildConfig struct {
Port string
Watch bool
Copy []string
Publish string
}
type GlobalConfig struct {
I18n map[string]string
Site SiteConfig
Authors map[string]AuthorConfig
Build BuildConfig
Develop bool
}
type ArticleConfig struct {
Title string
Date string
Update string
Author string
Tags []string
Categories []string
Topic string
Cover string
Draft bool
Preview template.HTML
Top bool
Config interface{}
}
type Article struct {
GlobalConfig
ArticleConfig
Time time.Time
MTime time.Time
Date int64
Update int64
Author AuthorConfig
Category string
Tags []string
Markdown string
Preview template.HTML
Content template.HTML
Link string
Config interface{}
}
type ThemeConfig struct {
Copy []string
Lang map[string]map[string]string
}
const (
CONFIG_SPLIT = "---"
MORE_SPLIT = "<!--more-->"
)
func Parse(markdown string) template.HTML {
// html.UnescapeString
return template.HTML(blackfriday.MarkdownCommon([]byte(markdown)))
}
func ReplaceRootFlag(content string) string {
return strings.Replace(content, "-/", globalConfig.Site.Root+"/", -1)
}
func ParseGlobalConfig(configPath string, develop bool) *GlobalConfig {
var config *GlobalConfig
// Parse Global Config
data, err := ioutil.ReadFile(configPath)
if err != nil {
return nil
}
if err = yaml.Unmarshal(data, &config); err != nil {
Fatal(err.Error())
}
if config.Site.Config == nil {
config.Site.Config = ""
}
config.Develop = develop
if develop {
config.Site.Root = ""
}
config.Site.Logo = strings.Replace(config.Site.Logo, "-/", config.Site.Root+"/", -1)
// Parse Theme Config
themeConfig := ParseThemeConfig(filepath.Join(rootPath, config.Site.Theme, "config.yml"))
for _, copyItem := range themeConfig.Copy {
config.Build.Copy = append(config.Build.Copy, filepath.Join(config.Site.Theme, copyItem))
}
config.I18n = make(map[string]string)
for item, langItem := range themeConfig.Lang {
config.I18n[item] = langItem[config.Site.Lang]
}
return config
}
func ParseThemeConfig(configPath string) *ThemeConfig {
// Read data from file
var themeConfig *ThemeConfig
data, err := ioutil.ReadFile(configPath)
if err != nil {
Fatal(err.Error())
}
// Parse config content
if err := yaml.Unmarshal(data, &themeConfig); err != nil {
Fatal(err.Error())
}
return themeConfig
}
func ParseArticleConfig(markdownPath string) (config *ArticleConfig, content string) {
var configStr string
// Read data from file
data, err := ioutil.ReadFile(markdownPath)
if err != nil {
Fatal(err.Error())
}
// Split config and markdown
contentStr := string(data)
contentStr = ReplaceRootFlag(contentStr)
markdownStr := strings.SplitN(contentStr, CONFIG_SPLIT, 2)
contentLen := len(markdownStr)
if contentLen > 0 {
configStr = markdownStr[0]
}
if contentLen > 1 {
content = markdownStr[1]
}
// Parse config content
if err := yaml.Unmarshal([]byte(configStr), &config); err != nil {
Error(err.Error())
return nil, ""
}
if config == nil {
return nil, ""
}
// Parse preview splited by MORE_SPLIT
previewAry := strings.SplitN(content, MORE_SPLIT, 2)
if len(config.Preview) <= 0 && len(previewAry) > 1 {
config.Preview = Parse(previewAry[0])
content = strings.Replace(content, MORE_SPLIT, "", 1)
}
return config, content
}
func ParseArticle(markdownPath string) *Article {
config, content := ParseArticleConfig(markdownPath)
if config == nil {
Error("Invalid format: " + markdownPath)
return nil
}
if config.Config == nil {
config.Config = ""
}
var article Article
// Parse markdown content
article.Preview = config.Preview
article.Config = config.Config
article.Markdown = content
article.Content = Parse(content)
article.Time = ParseDate(config.Date)
article.Date = article.Time.Unix()
if config.Update != "" {
article.MTime = ParseDate(config.Update)
article.Update = article.MTime.Unix()
}
article.Title = config.Title
article.Topic = config.Topic
article.Draft = config.Draft
article.Top = config.Top
if author, ok := globalConfig.Authors[config.Author]; ok {
author.Id = config.Author
author.Avatar = ReplaceRootFlag(author.Avatar)
article.Author = author
}
if len(config.Categories) > 0 {
article.Category = config.Categories[0]
} else {
article.Category = "misc"
}
tags := map[string]bool{}
article.Tags = config.Tags
for _, tag := range config.Tags {
tags[tag] = true
}
for _, cat := range config.Categories {
if _, ok := tags[cat]; !ok {
article.Tags = append(article.Tags, cat)
}
}
// Support topic and cover field
if config.Cover != "" {
article.Cover = config.Cover
} else {
article.Cover = config.Topic
}
return &article
}