-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
288 lines (256 loc) · 7.54 KB
/
parser.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/writeas/web-core/i18n"
)
var wd string = ""
func ParseContentDirectory(p string, s bool) ([]PostToMigrate, error) {
var numberOfFiles int = 0
// Get the current working directory.
rwd, err := os.Getwd()
wd = rwd
// Find the config file
matches, err := filepath.Glob("config.*")
if err != nil {
log.Fatal(err)
}
fileComponents := strings.Split(matches[0], ".")
format := fileComponents[len(fileComponents)-1]
configFilePath := filepath.Join(rwd, matches[0])
languageCode, err := ScanConfigForLanguage(configFilePath, format)
if err != nil {
log.Fatal(err)
}
fmt.Println("Setting language:", languageCode)
baseURL, err := ScanConfigForBaseUrl(configFilePath, format)
if err != nil {
log.Fatal(err)
}
fmt.Println("Found baseURL:", baseURL)
// Change directory to the path passed in.
srcPath := filepath.Join(rwd, "content", p)
os.Chdir(srcPath)
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
var posts []PostToMigrate
filepath.Walk(wd, func(path string, i os.FileInfo, err error) error {
if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") && strings.HasSuffix(i.Name(), ".md") {
fileName := i.Name()
fmt.Println("> Parsing", fileName)
post, err := parsePost(fileName, languageCode, baseURL, s)
if err != nil {
log.Fatal(err)
}
posts = append(posts, post)
fmt.Println("> Finished parsing", fileName)
numberOfFiles += 1
}
return nil
})
fmt.Printf("Parsed %d files\n\n", numberOfFiles)
// Change the working directory back to /content
os.Chdir(rwd)
rwd, err = os.Getwd()
return posts, nil
}
func parsePost(f string, l string, b string, s bool) (PostToMigrate, error) {
file, err := os.Open(f)
if err != nil {
return PostToMigrate{}, err
}
defer func() {
if err = file.Close(); err != nil {
log.Fatal(err)
}
}()
pf, err := pageparser.ParseFrontMatterAndContent(file)
hashtags := []string{}
var post PostToMigrate
var created time.Time
var updated time.Time
if pf.FrontMatterFormat == metadecoders.JSON ||
pf.FrontMatterFormat == metadecoders.YAML ||
pf.FrontMatterFormat == metadecoders.TOML {
for k, v := range pf.FrontMatter {
if k == "date" {
c, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
return PostToMigrate{}, err
}
created = c.UTC()
}
if k == "lastMod" {
u, err := time.Parse(time.RFC3339, v.(string))
if err != nil {
return PostToMigrate{}, err
}
updated = u.UTC()
}
if k == "tags" {
// Enumerate the tags, translate them to hashtags,
// and append them to hastags array
tags, ok := v.([]interface{})
if !ok {
continue
}
for _, ti := range tags {
if t, ok := ti.(string); ok {
hashtags = append(hashtags, ConvertToHashtag(t))
}
}
} else if k == "categories" {
// Enumerate the categories, translate them to hashtags,
// and append them to hashtag array
categories, ok := v.([]interface{})
if !ok {
continue
}
for _, ci := range categories {
if c, ok := ci.(string); ok {
hashtags = append(hashtags, ConvertToHashtag(c))
}
}
}
}
content := string(pf.Content[:]) + "\n\n" + strings.Join(hashtags, " ")
content = scanContentForShortcodes(content)
if s {
content = scanContentForLocalImages(content, b)
}
var slug string
if pf.FrontMatter["slug"] != nil {
slug = pf.FrontMatter["slug"].(string)
} else {
slug = ""
}
rtl := i18n.LangIsRTL(l)
post = PostToMigrate{
body: content,
title: pf.FrontMatter["title"].(string),
slug: slug,
lang: &l,
rtl: &rtl,
created: &created,
updated: &updated,
}
}
return post, nil
}
func scanContentForShortcodes(c string) string {
var shortcode = regexp.MustCompile(`{{< (?P<shortcodeType>\S+)\s(?P<shortcodeID>.*) >}}`)
matches := shortcode.FindAllStringSubmatch(c, -1)
for _, match := range matches {
fmt.Println("> ==============================================================================")
shortcodeType := match[1]
shortcodeValue := match[2]
fmt.Println(" > Shortcode found:", match[0])
fmt.Println(" > Shortcode type: ", shortcodeType)
fmt.Println(" > Shortcode ID: ", shortcodeValue)
var urlString string = ""
switch shortcodeType {
case "gist":
// Split username from gist ID and strip any filename that may have been passed in
values := strings.Split(shortcodeValue, " ")
urlString = "https://gist.github.com/" + values[0] + "/" + values[1]
case "instagram":
// Strip any `hidecaption` option that may have been passed in
values := strings.Split(shortcodeValue, " ")
urlString = "https://www.instagram.com/p/" + values[0]
case "tweet":
urlString = "https://twitter.com/twitter/status/" + shortcodeValue
case "vimeo":
urlString = "https://player.vimeo.com/video/" + shortcodeValue
case "youtube":
// Strip anything other than the video ID that may have been passed in
values := strings.Split(shortcodeValue, " ")
fmt.Println(" > VALUES:", values[0])
if string(values[0][0:4]) == "id=\"" {
subvalues := strings.Split(values[0], "\"")
urlString = "https://www.youtube.com/watch?v=" + subvalues[1]
} else {
urlString = "https://www.youtube.com/watch?v=" + shortcodeValue
}
default:
urlString = match[0]
fmt.Println(" > Unsupported shortcode — please see README")
}
c = strings.Replace(c, match[0], urlString, -1)
fmt.Println(" > Migrated URL: ", urlString)
fmt.Println("> ==============================================================================")
}
return c
}
func scanContentForLocalImages(c string, b string) string {
// Search for Markdown image links with optional alt text
var reMarkdown = regexp.MustCompile(`!\[.*\]\((?P<url>.+)\)`)
mdMatches := reMarkdown.FindAllStringSubmatch(c, -1)
for _, mdMatch := range mdMatches {
img := mdMatch[1]
if ImageIsLocal(img, b) {
// Strip the base URL if the post uses an absolute URL.
if strings.HasPrefix(img, b) {
img = strings.Replace(img, b, "", 1)
}
imgURL := uploadOrLogError(img)
c = strings.Replace(c, img, imgURL, -1)
} else {
fmt.Println(" > 🖼 (⛔️) Skipping upload of remote image: ", img)
}
}
// Search for HTML image links with optional alt text
var reHtml = regexp.MustCompile(`<img.*src="(?P<url>\S+)".*>`)
htmlMatches := reHtml.FindAllStringSubmatch(c, -1)
for _, htmlMatch := range htmlMatches {
img := htmlMatch[1]
if ImageIsLocal(img, b) {
// Strip the base URL if the post uses an absolute URL.
if strings.HasPrefix(img, b) {
img = strings.Replace(img, b, "", 1)
}
imgURL := uploadOrLogError(img)
c = strings.Replace(c, img, imgURL, -1)
} else {
fmt.Println(" > 🖼 (⛔️) Skipping upload of remote image: ", img)
}
}
return c
}
func uploadOrLogError(i string) string {
ip := filepath.Join(wd, "static", i)
fmt.Println(" > 🖼 (⏳) Uploading image to Snap.as:", ip)
retries := 3
var upErr string
for retries > 0 {
imgURL, err := UploadImage(ip)
if err != nil {
upErr = err.Error()
retries--
fmt.Println(" > 🖼 (⚠️) Upload failed. Retrying...")
} else {
fmt.Println(" > 🖼 (✅) Upload complete, at:", imgURL)
return imgURL
}
}
fmt.Println(" > 🖼 (⚠️) Upload failed. Logging error and skipping.")
LogUploadError(i, upErr)
return i
}
type PostToMigrate struct {
body string
title string
slug string
lang *string
rtl *bool
created *time.Time
updated *time.Time
}