-
Notifications
You must be signed in to change notification settings - Fork 12
/
everorg.go
479 lines (419 loc) · 11 KB
/
everorg.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// everorg.go
// EverOrg
//
// Created by Mario Martelli on 24.02.17.
// Copyright © 2017 Mario Martelli. All rights reserved.
//
// This file is part of EverOrg.
//
// Everorg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EverOrg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EverOrg. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"crypto/md5"
b64 "encoding/base64"
"encoding/hex"
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"mime"
"os"
"path/filepath"
"strings"
"golang.org/x/net/html"
)
// Globals for filehandling
var (
readFile = ""
attFolderExt = "-attachments"
attachmentPath = ""
isMerged, rgrdDiv bool
)
// Get Attributes for html tag
func getAttr(attribute string, token html.Token) string {
for _, attr := range token.Attr {
if attr.Key == attribute {
return attr.Val
}
}
return ""
}
func mimeFiles(token html.Token) (string, string) {
mimeType := getAttr("type", token)
ext, err := mime.ExtensionsByType(mimeType)
if err == nil {
var fileExt string
if len(ext) > 0 {
fileExt = ext[0]
} else {
fileExt = ".unknwn"
}
return filepath.Base(attachmentPath), getAttr("hash", token) + fileExt
}
return "", ""
}
// Org mode representation of Node
func (nodes Nodes) orgFormat() string {
var value strings.Builder
header := 0
table := 0
list := 0
listValue := []int{}
for _, node := range nodes {
switch node.Token.Type {
case html.SelfClosingTagToken:
switch node.Token.Data {
case "en-media":
base, file := mimeFiles(node.Token)
value.WriteString("[[./" + base + "/")
value.WriteString(file + "]]")
case "en-todo":
switch getAttr("checked", node.Token) {
case "true":
value.WriteString("\n- [X] ")
case "false":
value.WriteString("\n- [ ] ")
}
}
case html.StartTagToken:
switch node.Token.Data {
case "a":
// We do not want links in the header
if header == 0 {
value.WriteString("[[" + getAttr("href", node.Token) + "][")
}
case "p":
value.WriteString("\n")
case "u":
value.WriteString("_")
case "i":
value.WriteString("/")
case "b", "strong", "em":
value.WriteString("*")
case "del":
value.WriteString("+")
case "h1":
value.WriteString("\n** ")
header++
case "h2":
value.WriteString("\n*** ")
header++
case "h3":
value.WriteString("\n**** ")
header++
case "h4":
value.WriteString("\n***** ")
header++
case "h5":
value.WriteString("\n****** ")
header++
case "h6":
value.WriteString("\n******* ")
header++
// These tags are ignored
case "span", "tr", "tbody", "abbr", "th", "thead", "ins", "img":
break
case "sup", "sub", "small", "br", "dl", "dd", "dt", "font", "colgroup", "cite":
break
case "address", "s", "map", "area", "center", "q":
break
// only the end tag is regarded
case "div":
break
case "hr":
value.WriteString("\n------\n")
case "en-media":
base, file := mimeFiles(node.Token)
value.WriteString("[[./" + base + "/")
value.WriteString(file + "]]")
case "table":
table++
case "td":
value.WriteString("|")
case "ol":
list++
listValue = append(listValue, 1)
case "ul":
list++
listValue = append(listValue, 0)
case "li":
value.WriteString("\n")
for i := 0; i <= list; i++ {
value.WriteString(" ")
}
if list > 0 {
switch listValue[list-1] {
case 0:
value.WriteString("- ")
default:
value.WriteString(fmt.Sprintf("%d.", listValue[list-1]))
listValue[list-1] = listValue[list-1] + 1
}
}
case "code":
value.WriteString("~")
case "pre":
value.WriteString("\n#+BEGIN_SRC\n")
case "blockquote":
value.WriteString("\n#+BEGIN_QUOTE\n")
default:
fmt.Println("skip token: " + node.Token.Data)
}
case html.EndTagToken:
switch node.Token.Data {
case "u":
value.WriteString("_")
case "i":
value.WriteString("/")
case "b", "strong", "em":
value.WriteString("*")
case "del":
value.WriteString("+")
case "a":
if header == 0 {
value.WriteString("]]")
}
case "h1", "h2", "h3", "h4", "h5", "h6":
header--
case "table":
table--
case "tr":
value.WriteString("|\n")
case "ol", "ul":
list--
listValue = listValue[:len(listValue)-1]
case "code":
value.WriteString("~")
case "pre":
value.WriteString("\n#+END_SRC\n")
case "blockquote":
value.WriteString("\n#+END_QUOTE\n")
case "div":
if rgrdDiv && table == 0 {
value.WriteString("\n")
}
}
}
value.WriteString(node.Text)
}
return value.String()
}
func parseHTML(r io.Reader) Nodes {
var nodes Nodes
d := html.NewTokenizer(r)
for {
// token type
tokenType := d.Next()
if tokenType == html.ErrorToken {
return nodes
}
token := d.Token()
tokenValue := token.String()
switch tokenType {
case html.StartTagToken:
switch token.Data {
case "en-note":
break
default:
nodes = append(nodes, Node{token, ""})
}
case html.TextToken:
nodes = append(nodes, Node{html.Token{}, html.UnescapeString(tokenValue)})
case html.EndTagToken:
nodes = append(nodes, Node{token, ""})
case html.SelfClosingTagToken:
if token.String() == "<br/>" || token.String() == "<hr/>" {
nodes = append(nodes, Node{html.Token{}, "\n"})
} else {
nodes = append(nodes, Node{token, ""})
}
}
}
}
func main() {
wordPtr := flag.String("input", "enex File", "relative path to enex file")
flag.BoolVar(&isMerged, "merge", false, "whether to merge notes to single file")
flag.BoolVar(&rgrdDiv, "regardDiv", false, "closing <div> will become newline")
flag.Parse()
if wordPtr == nil || *wordPtr == "" {
panic("input file is missing")
}
fmt.Println("input:", *wordPtr)
// Open the file given at commandline
readFile = *wordPtr
xmlFile, err := os.Open(readFile)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer func() { _ = xmlFile.Close() }()
currentDir, fileName := filepath.Split(readFile)
ext := filepath.Ext(readFile)
baseName := strings.TrimSuffix(fileName, ext)
newWrittenDir := filepath.Join(currentDir, baseName)
if _, err = os.Stat(newWrittenDir); os.IsNotExist(err) {
_ = os.Mkdir(newWrittenDir, 0711)
}
// Create Attachments Directory if not existent
currentFilePathName := filepath.Join(newWrittenDir, baseName)
attachmentPath = currentFilePathName + attFolderExt
if _, err = os.Stat(attachmentPath); os.IsNotExist(err) {
_ = os.Mkdir(attachmentPath, 0711)
}
b, _ := ioutil.ReadAll(xmlFile)
var q Query
_ = xml.Unmarshal(b, &q)
var f *os.File
// Parse the contained xml
if isMerged {
f, err = os.Create(currentFilePathName + ".org")
fmt.Println("output: " + currentFilePathName + ".org")
if err != nil {
fmt.Println("err", err)
return
}
defer func() { _ = f.Close() }()
}
attachmentsCount := 0
notesCount := 0
for _, note := range q.Notes {
cdata := []byte(note.Content)
reader := bytes.NewReader(cdata)
nodes := parseHTML(reader)
if isMerged {
_, _ = f.WriteString(note.orgProperties())
_, _ = f.WriteString(nodes.orgFormat())
_ = f.Sync()
} else {
noteFileName := sanitize(note.Title) + "-" + note.Updated + ".org"
newFile, err := os.Create(filepath.Join(newWrittenDir, noteFileName))
if err != nil {
continue
}
_, _ = newFile.WriteString(note.orgProperties())
_, _ = newFile.WriteString(nodes.orgFormat())
_ = newFile.Close()
}
notesCount++
for _, attachment := range note.Resources {
if attachment.Data.Encoding == "base64" {
err = createAttachment(attachment, attachmentPath)
if err == nil {
attachmentsCount++
}
}
}
}
if attachmentsCount == 0 {
// remove attachment directory
_ = os.Remove(attachmentPath)
return
}
fmt.Printf("\nThere are %d notes and %d attachments created", notesCount, attachmentsCount)
}
func (note Note) orgProperties() string {
var result strings.Builder
attr := note.Attributes
if isMerged {
result.WriteString("\n* " + note.Title)
if len(note.Tags) > 0 {
result.WriteString(" ")
for _, tag := range note.Tags {
result.WriteString(":" + tag)
}
result.WriteString(":")
}
result.WriteString("\n")
result.WriteString(":PROPERTIES:\n")
if attr.Author != "" {
result.WriteString(":AUTHOR: " + attr.Author + "\n")
}
if note.Created != "" {
result.WriteString(":EVNT_CREATED: " + note.Created + "\n")
result.WriteString(":EVNT_UPDATED: " + note.Updated + "\n")
}
if attr.Latitude > 0 {
result.WriteString(fmt.Sprintf(":GEO_LAT: %f\n", attr.Latitude))
result.WriteString(fmt.Sprintf(":GEO_LON: %f\n", attr.Longitude))
}
if attr.Source != "" {
result.WriteString(":EVNT_SOURCE: " + attr.Source + "\n")
}
if attr.SourceUrl != "" {
result.WriteString(":EVNT_SOURCEURL: " + attr.SourceUrl + "\n")
}
result.WriteString(":END:\n")
} else {
result.WriteString("#+TITLE: " + note.Title + "\n")
result.WriteString("#+STARTUP: showall" + "\n")
if attr.Author != "" {
result.WriteString("#+AUTHOR: " + attr.Author + "\n")
}
if len(note.Tags) > 0 {
result.WriteString("#+TAGS: ")
result.WriteString(strings.Join(note.Tags, " ") + "\n")
}
if note.Created != "" {
result.WriteString("#+DATE: " + note.Created + "\n")
}
if attr.Latitude > 0 {
result.WriteString(fmt.Sprintf("#+LAT: %f\n", attr.Latitude))
result.WriteString(fmt.Sprintf("#+LON: %f\n", attr.Longitude))
}
if attr.Source != "" {
result.WriteString("#+SOURCE: " + attr.Source + "\n")
}
if attr.SourceUrl != "" {
result.WriteString("#+DESCRIPTION: " + attr.SourceUrl + "\n")
}
}
return result.String()
}
func sanitize(title string) string {
title = strings.TrimSpace(strings.ToLower(title))
title = strings.Replace(title, "-", "", -1)
title = strings.Replace(title, "'", "", -1)
title = strings.Replace(title, "(", "", -1)
title = strings.Replace(title, ")", "", -1)
title = strings.Replace(title, ",", "", -1)
title = strings.Replace(title, ":", "", -1)
title = strings.Replace(title, "|", "", -1)
title = strings.Replace(title, "?", "", -1)
title = strings.Replace(title, " ", "-", -1)
return title
}
func createAttachment(attachment Resource, attachmentPath string) error {
h := md5.New()
sDec, _ := b64.StdEncoding.DecodeString(attachment.Data.Content)
_, _ = h.Write(sDec)
filename := hex.EncodeToString(h.Sum(nil))
ext, err := mime.ExtensionsByType(attachment.Mime)
if err != nil {
return err
}
var fileExt string
if len(ext) > 0 {
fileExt = ext[0]
} else {
fileExt = ".unknwn"
}
err = ioutil.WriteFile(attachmentPath+"/"+filename+fileExt, sDec, 0644)
if err != nil {
return err
}
return nil
}