-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgokl.go
375 lines (342 loc) · 10.1 KB
/
gokl.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
"time"
git "gopkg.in/src-d/go-git.v4"
)
const datelayout = "2006-01-02"
const outputdate = "Monday, 2. January 2006"
type logentry struct {
begin time.Time
end time.Time
topic string
appendix string
media []string
body string
}
type outputentry struct {
Begin string
End string
Topic string
Appendix string
Media []string
Body string
}
type outputpage struct {
Entries []outputentry
MediaLinks []string
Links []string
Month string
Year string
}
type ByBegin []logentry
func (a ByBegin) Len() int { return len(a) }
func (a ByBegin) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByBegin) Less(i, j int) bool { return a[i].begin.Before(a[j].begin) }
func getRepo(repodir string, repourl string) error {
var repo *git.Repository
var err error
repo, err = git.PlainClone(repodir, false, &git.CloneOptions{
URL: repourl,
Progress: os.Stdout,
})
if err != nil {
if err != git.ErrRepositoryAlreadyExists {
return errors.New("Error while cloning repo:" + err.Error())
}
repo, err = git.PlainOpen(repodir)
if err != nil {
return errors.New("Error while opening repo:" + err.Error())
}
}
tree, err := repo.Worktree()
if err != nil {
return errors.New("Error while creating worktree:" + err.Error())
}
err = tree.Reset(&git.ResetOptions{
Mode: git.HardReset,
})
if err != nil {
return errors.New("Error while resetting repo:" + err.Error())
}
err = tree.Pull(&git.PullOptions{
RemoteName: "origin",
Force: true,
})
if err != nil {
if err != git.NoErrAlreadyUpToDate {
return errors.New("Error while pulling changes:" + err.Error())
}
}
return nil
}
func parseFile(fp string, mediapath string, repodir string) (logentry, error) {
var result logentry
b, err := ioutil.ReadFile(fp)
if err != nil {
return result, errors.New("Error opening file:" + err.Error())
}
content := string(b)
fmt.Println(content)
split := strings.Split(content, "\n\n")
if len(split) != 2 {
return result, errors.New("Invalid entry format")
}
header := split[0]
body := split[1]
split = strings.Split(header, "\n")
for _, s := range split {
if !strings.HasPrefix(s, "#") {
split := strings.SplitN(s, ": ", 2)
switch split[0] {
case "BEGIN":
t, err := time.Parse(datelayout, split[1])
if err != nil {
return result, errors.New("Error while parsing date:" + err.Error())
}
result.begin = t
case "END":
if split[1] != "None" {
t, err := time.Parse(datelayout, split[1])
if err != nil {
return result, errors.New("Error while parsing date:" + err.Error())
}
result.end = t
}
case "TOPIC":
result.topic = split[1]
case "APPENDIX":
result.appendix = split[1]
}
}
}
if _, err := os.Stat(mediapath); !os.IsNotExist(err) {
fis, err := ioutil.ReadDir(mediapath)
if err != nil {
return result, errors.New("Error reading media path:" + err.Error())
}
for _, fi := range fis {
media := strings.TrimPrefix(filepath.Join(mediapath, fi.Name()), repodir)
result.media = append(result.media, media)
}
}
fmt.Println(result.media)
result.body = body
return result, nil
}
func generateLogEntries(repodir string) ([]logentry, error) {
var result []logentry
fis, err := ioutil.ReadDir(repodir)
if err != nil {
return result, err
}
for _, fi := range fis {
if (fi.Name() != "media") && (fi.Name() != "dokuwiki") && (fi.Name() != ".git") {
if fi.IsDir() {
yeardir := repodir + string(filepath.Separator) + fi.Name()
fis2, err := ioutil.ReadDir(yeardir)
if err != nil {
return result, err
}
for _, fi2 := range fis2 {
if fi2.IsDir() {
monthdir := yeardir + string(filepath.Separator) + fi2.Name()
fis3, err := ioutil.ReadDir(monthdir)
if err != nil {
return result, err
}
for _, fi3 := range fis3 {
if !fi3.IsDir() {
path := monthdir + string(filepath.Separator) + fi3.Name()
mediadir := repodir + string(filepath.Separator) + "media" + string(filepath.Separator)
mediadir = mediadir + fi.Name() + string(filepath.Separator) + fi2.Name()
ext := filepath.Ext(fi3.Name())
filename := strings.TrimSuffix(fi3.Name(), ext)
split := strings.Split(filename, "-")
if len(split) > 1 {
mediadir = mediadir + string(filepath.Separator) + split[0] + string(filepath.Separator)
mediadir = mediadir + split[1] + string(filepath.Separator)
}
le, err := parseFile(path, mediadir, repodir)
if err != nil {
return nil, err
}
result = append(result, le)
}
}
}
}
}
}
}
return result, nil
}
func convertlink(in string, count int) (string, string) {
var inlink, listlink string
status := 0
scount := strconv.Itoa(count)
enclosed := strings.Replace(in, "]]", "", -1)
if strings.Contains(in, "[[:") {
status = 2
enclosed = strings.Replace(enclosed, "[[:", "", -1)
} else {
enclosed = strings.Replace(enclosed, "[[", "", -1)
if strings.Contains(enclosed, "http://") || strings.Contains(enclosed, "https://") {
status = 3
} else {
status = 2
}
}
split := strings.Split(enclosed, "|")
link := split[0]
name := ""
if len(split) > 1 {
name = split[1]
} else {
name = link
}
switch status {
case 2:
inlink = "[" + name + "][LINK:" + scount + "]"
listlink = "[h|" + "[LINK " + scount + "]: " + name + "|" + "URL:http://www.binary-kitchen.de/wiki/doku.php?id=" + link + "|gopher.binary-kitchen.de|70]"
case 3:
inlink = "[" + name + "][LINK:" + scount + "]"
listlink = "[h|" + "[LINK " + scount + "]: " + name + "|" + "URL:" + link + "|gopher.binary-kitchen.de|70]"
}
return inlink, listlink
}
func formatEntry(entry logentry, imageurl string, linkcount, mediacount int) (outputentry, []string, []string, int, int) {
var outentry outputentry
var links []string
var media []string
outentry.Begin = entry.begin.Format(outputdate)
if entry.end.After(entry.begin) {
outentry.End = "bis " + entry.end.Format(outputdate)
}
outentry.Topic = entry.topic
outentry.Appendix = entry.appendix
outentry.Body = entry.body
for strings.Contains(outentry.Body, "[[") {
sep := strings.SplitN(outentry.Body, "[[", 2)
if len(sep) > 1 {
before := sep[0]
remaining := sep[1]
sep := strings.SplitAfterN(remaining, "]]", 2)
if len(sep) > 1 {
middle := sep[0]
after := sep[1]
inlink, listlink := convertlink(middle, linkcount)
links = append(links, listlink)
outentry.Body = before + inlink + after
linkcount = linkcount + 1
}
}
}
for _, m := range entry.media {
imagename := "[BILD " + strconv.Itoa(mediacount) + "]"
outentry.Media = append(outentry.Media, imagename)
imagelink := "[h|" + imagename + "|URL:" + imageurl + m + "|gopher.binary-kitchen.de|70]"
media = append(media, imagelink)
mediacount = mediacount + 1
}
return outentry, links, media, linkcount, mediacount
}
func writeMonth(gopherdir string, year string, month string, templatepath string, currentpage outputpage) error {
currentpage.Month = month
currentpage.Year = year
monthpath := gopherdir + string(filepath.Separator) + year + string(filepath.Separator) + month
err := os.MkdirAll(monthpath, 0755)
if err != nil {
return errors.New("Error creating month directory: " + err.Error())
}
t, err := template.ParseFiles(templatepath)
if err != nil {
return errors.New("Error parsing tempalte: " + err.Error())
}
f, err := os.Create(monthpath + string(filepath.Separator) + "index.gph")
if err != nil {
return errors.New("Error creating gopherfile: " + err.Error())
}
defer f.Close()
err = t.Execute(f, ¤tpage)
if err != nil {
return errors.New("Error executing template: " + err.Error())
}
f.Close()
return nil
}
func generateGopherDir(entries []logentry, gopherdir string, imageurl string, templatepath string) error {
month := ""
year := ""
mediacount := 1
linkcount := 1
var currentpage outputpage
for index, e := range entries {
newmonth := e.begin.Format("01-January")
if month != newmonth {
year = e.begin.Format("2006")
if newmonth == "01-January" {
year = entries[index-1].begin.Format("2006")
}
err := writeMonth(gopherdir, year, month, templatepath, currentpage)
if err != nil {
return errors.New("Error writing month: " + err.Error())
}
currentpage = outputpage{}
mediacount = 1
linkcount = 1
month = newmonth
}
outentry, links, media, newlinkcount, newmediacount := formatEntry(e, imageurl, linkcount, mediacount)
currentpage.Entries = append(currentpage.Entries, outentry)
currentpage.Links = append(currentpage.Links, links...)
currentpage.MediaLinks = append(currentpage.MediaLinks, media...)
linkcount = newlinkcount
mediacount = newmediacount
}
err := writeMonth(gopherdir, year, month, templatepath, currentpage)
if err != nil {
return errors.New("Error writing month: " + err.Error())
}
return nil
}
func main() {
var repodir string
var gopherdir string
var repourl string
var imageurl string
var templatepath string
flag.StringVar(&repodir, "r", "./", "Directory for checking out the repository")
flag.StringVar(&gopherdir, "g", "/var/gopher/Kuechenlog", "Directory for the generated gopher content")
flag.StringVar(&repourl, "u", "https://github.com/Binary-Kitchen/kitchenlog.git", "URL of the log repository")
flag.StringVar(&imageurl, "i", "https://raw.githubusercontent.com/Binary-Kitchen/kitchenlog/master", "The URL for the raw image files")
flag.StringVar(&templatepath, "t", "./month-template.txt", "Path to the template for the gopher pages")
flag.Parse()
log.Println("Getting Repository")
err := getRepo(repodir, repourl)
if err != nil {
log.Fatal("Error getting Repo:", err)
}
log.Println("Parsing Log Entries")
les, err := generateLogEntries(repodir)
if err != nil {
log.Fatal("Error parsing Entries:", err)
}
sort.Sort(ByBegin(les))
log.Println("Wirting in gopher dir")
err = generateGopherDir(les, gopherdir, imageurl, templatepath)
if err != nil {
log.Fatal("Error creating gopher files:", err)
}
log.Println("Done")
}