-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathoctohug.go
236 lines (217 loc) · 7.7 KB
/
octohug.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
//
// octohug
//
// copies octopress posts to hugo posts
// converts the header
// converts categories and tags to hugo format in header
// if run in the octopress directory, replaces include_file with the contents
//
// http://codebrane.com/blog/2015/09/10/migrating-from-octopress-to-hugo/
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/igorsobreira/titlecase"
)
var octopressPostsDirectory string
var hugoPostDirectory string
func readFile(path string) (string, error) {
file, fileError := os.Open(path)
if fileError != nil {
}
defer file.Close()
var buffer []byte
fileReader := bufio.NewReaderSize(file, 10*1024)
line, isPrefix, lineError := fileReader.ReadLine()
for lineError == nil && !isPrefix {
buffer = append(buffer, line...)
buffer = append(buffer, byte('\n'))
line, isPrefix, lineError = fileReader.ReadLine()
}
if isPrefix {
fmt.Fprintln(os.Stderr, "buffer size too small")
return "", nil
}
return string(buffer), nil
}
func visit(path string, fileInfo os.FileInfo, err error) error {
if fileInfo.IsDir() {
return nil
}
// Get the base filename of the post
octopressFilename := filepath.Base(path)
// Need to strip off the initial date and final .markdown from the post filename
regex := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}-(.*).m(arkdown|d)`)
matches := regex.FindStringSubmatch(octopressFilename)
// Ignore non-matching filenames (i.e. do no dereference nil)
if matches == nil {
return nil
}
octopressFilenameWithoutExtension := matches[1]
hugoFilename := hugoPostDirectory + "/" + octopressFilenameWithoutExtension + ".md"
fmt.Printf("%s\n%s\n", path, hugoFilename)
// Open the octopress file
octopressFile, octopressFileError := os.Open(path)
// Nothing to do if we can open the source file
if octopressFileError != nil {
fmt.Fprintf(os.Stderr, "Error opening octopress file %s, ignoring\n", path)
return nil
}
defer octopressFile.Close()
// Create the hugo file
hugoFile, hugoFileError := os.Create(hugoFilename)
if hugoFileError != nil {
fmt.Fprintf(os.Stderr, "could not create hugo file: %v\n", hugoFileError)
return nil
}
defer hugoFile.Close()
hugoFileWriter := bufio.NewWriter(hugoFile)
// octopressDateRegex := regexp.MustCompile(`^date:`)
octopressCategoryOrTagNameRegex := regexp.MustCompile(`^- (.*)`)
// Read the octopress file line by line
headerTagSeen := false
inCategories := false
firstCategoryAdded := false
inTags := false
firstTagAdded := false
octopressFileReader := bufio.NewReaderSize(octopressFile, 10*1024)
octopressLine, isPrefix, lineError := octopressFileReader.ReadLine()
for lineError == nil && !isPrefix {
octopressLineAsString := string(octopressLine)
if octopressLineAsString == "---" {
headerTagSeen = !headerTagSeen
if inCategories || inTags {
hugoFileWriter.WriteString("]\n")
inCategories = false
inTags = false
}
octopressLineAsString = string("+++")
}
if strings.Contains(octopressLineAsString, "categories:") {
inCategories = true
hugoFileWriter.WriteString("Categories = [")
} else if strings.Contains(octopressLineAsString, "tags:") {
if inCategories {
inCategories = false
hugoFileWriter.WriteString("]\n")
}
inTags = true
hugoFileWriter.WriteString("Tags = [")
} else if strings.Contains(octopressLineAsString, "keywords: ") {
inCategories = false
if inTags {
hugoFileWriter.WriteString("]\n")
inTags = false
}
hugoFileWriter.WriteString("keywords = [")
parts := strings.Split(octopressLineAsString, ": ")
keywords := strings.Split(strings.Replace(parts[1], "\"", "", -1), ",")
firstKeyword := true
for _, keyword := range keywords {
if !firstKeyword {
hugoFileWriter.WriteString(",")
}
hugoFileWriter.WriteString("\"" + keyword + "\"")
firstKeyword = false
}
hugoFileWriter.WriteString("]\n")
} else if inCategories && !inTags {
matches = octopressCategoryOrTagNameRegex.FindStringSubmatch(octopressLineAsString)
if len(matches) > 1 {
if firstCategoryAdded {
hugoFileWriter.WriteString(", ")
}
hugoFileWriter.WriteString("\"" + matches[1] + "\"")
firstCategoryAdded = true
}
} else if octopressLineAsString == "tags:" {
inTags = true
hugoFileWriter.WriteString("Tags = [")
} else if inTags && !inCategories {
matches = octopressCategoryOrTagNameRegex.FindStringSubmatch(octopressLineAsString)
if len(matches) > 1 {
if firstTagAdded {
hugoFileWriter.WriteString(", ")
}
hugoFileWriter.WriteString("\"" + matches[1] + "\"")
firstTagAdded = true
} else if matches != nil {
tag := strings.Replace(matches[1], "'", "", -1)
tag = strings.Replace(tag, "\"", "", -1)
hugoFileWriter.WriteString("\"" + tag + "\"")
firstTagAdded = true
}
} else if strings.Contains(octopressLineAsString, "date: ") {
parts := strings.Split(octopressLineAsString, " ")
hugoFileWriter.WriteString("date = \"" + parts[1] + "\"\n")
octoSlugDate := strings.Replace(parts[1], "-", "/", -1)
octoFriendlySlug := octoSlugDate + "/" + octopressFilenameWithoutExtension
hugoFileWriter.WriteString("slug = \"" + octoFriendlySlug + "\"\n")
} else if strings.Contains(octopressLineAsString, "title: ") {
// to keep the urls the same as octopress, the title
// needs to be the filename
parts := strings.Split(octopressFilenameWithoutExtension, "-")
hugoFileWriter.WriteString("title = \"")
firstPart := true
title := ""
for _, part := range parts {
if !firstPart {
title += " "
}
title += part
firstPart = false
}
title += "\"\n"
title = titlecase.Title(title)
hugoFileWriter.WriteString(title)
} else if strings.Contains(octopressLineAsString, "description: ") {
parts := strings.Split(octopressLineAsString, ": ")
hugoFileWriter.WriteString("description = " + parts[1] + "\n")
} else if strings.Contains(octopressLineAsString, "layout: ") {
} else if strings.Contains(octopressLineAsString, "author: ") {
} else if strings.Contains(octopressLineAsString, "comments: ") {
} else if strings.Contains(octopressLineAsString, "slug: ") {
} else if strings.Contains(octopressLineAsString, "wordpress_id: ") {
} else if strings.Contains(octopressLineAsString, "published: ") {
hugoFileWriter.WriteString("published = false\n")
} else if strings.Contains(octopressLineAsString, "include_code") {
parts := strings.Split(octopressLineAsString, " ")
// can be:
// {% include_code [RedViewController.m] lang:objectivec slidernav/RedViewController.m %}
// or
// {% include_code [RedViewController.m] slidernav/RedViewController.m %}
codeFilePath := "source/downloads/code/" + parts[len(parts)-2]
codeFileContent, _ := readFile(codeFilePath)
codeFileContent = strings.Replace(codeFileContent, "<", "<", -1)
codeFileContent = strings.Replace(codeFileContent, ">", ">", -1)
hugoFileWriter.WriteString("<pre><code>\n" + codeFileContent + "</code></pre>\n")
} else {
hugoFileWriter.WriteString(octopressLineAsString + "\n")
} // if octopressLineAsString == "categories:"
hugoFileWriter.Flush()
octopressLine, isPrefix, lineError = octopressFileReader.ReadLine()
}
if isPrefix {
fmt.Fprintln(os.Stderr, "buffer size too small")
}
return nil
}
func init() {
flag.StringVar(&octopressPostsDirectory, "octo", "source/_posts", "path to octopress posts directory")
flag.StringVar(&hugoPostDirectory, "hugo", "content/post", "path to hugo post directory")
}
func main() {
flag.Parse()
// Check that we can trust octopressPostsDirectory
if _, err := os.Stat(octopressPostsDirectory); err != nil {
fmt.Fprintf(os.Stderr, "Error reading directory: %v\n", err)
os.Exit(-1)
}
os.MkdirAll(hugoPostDirectory, 0777)
filepath.Walk(octopressPostsDirectory, visit)
}