-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
548 lines (468 loc) · 12.3 KB
/
sync.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
// "log"
"mime"
"os"
"os/user"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/adrg/frontmatter"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/spf13/cobra"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
gast "github.com/yuin/goldmark/ast"
log "github.com/sirupsen/logrus"
parser2 "github.com/gohugoio/hugo/parser"
)
type SyncPair struct {
Source string `json:"source"`
Target string `json:"target"`
}
type SyncData struct {
Label string `json:"label"`
Pairs []SyncPair `json:"pairs"`
}
type SyncInfo struct {
Data []SyncData `json:"data"`
HomeDir string `json:"-"`
}
type FrontMatter struct {
Draft bool `yaml:"draft"`
Published bool `yaml:"published"`
Date string `yaml:"date"`
Tags []string `yaml:"tags"`
}
type CalloutAttrs struct {
Type string `json:"type"`
Collapsed bool `json:"collapsed"`
Title string `json:"title,omitempty"`
}
const syncFileName = "sync.json"
func Execute() {
err := rootCmd.Execute()
if err != nil {
log.Error(err)
os.Exit(1)
}
}
func main() {
// rootCmd.Flags().StringP("sync-pairs", "p", "", "JSON transfer pair metadata")
// rootCmd.MarkFlagRequired("sync-pairs")
Execute()
}
var rootCmd = &cobra.Command{
Use: "obsidian2hugo",
Short: "Finds obsidian pages marked as `published: true` and copies the files into the hugo directory",
Long: `This tool was created to be able to export blog posts created inside obsidian for the usage inside a hugo blog.`,
Run: func(cmd *cobra.Command, args []string) {
var err error
// var limit string
var usr *user.User
var syncPath string
var syncMeta SyncInfo
if usr, err = user.Current(); err != nil {
log.Fatalf("unable to get current user")
}
// if syncPath, err = cmd.Flags().GetString("sync-pairs"); err != nil {
// log.Fatal("`sync-pairs` flag missing")
// }
// if syncFile, err = os.Open(syncPath); err != nil {
// log.Fatal("unable to open sync file for path data")
// }
// defer syncFile.Close()
for _, dir := range cmd.Flags().Args() {
if syncPath, err = filepath.Abs(dir); err != nil {
log.Fatal(err)
}
if _, err = os.Stat(syncPath); err != nil {
log.Fatal(err)
}
log.Infof("path=%s", syncPath)
var syncPath = filepath.Join(syncPath, syncFileName)
if syncMeta, err = ExtractSyncData(syncPath); err != nil {
log.Fatal(err)
}
syncMeta.HomeDir = usr.HomeDir
if err = ProcessSyncInfo(syncMeta); err != nil {
log.Fatal(err)
}
}
// log.Fatalf("%v", cmd.Flags().Args())
},
}
func ExtractSyncData(path string) (SyncInfo, error) {
var err error
var info SyncInfo
var syncFile *os.File
var syncBytes []byte
if syncFile, err = os.Open(path); err != nil {
return info, fmt.Errorf("unable to open sync file: %s", path)
}
defer syncFile.Close()
if syncBytes, err = io.ReadAll(syncFile); err != nil {
log.Fatal("unable to read contents of sync file")
}
if err = json.Unmarshal([]byte(syncBytes), &info); err != nil {
log.Fatal("`sync-pairs` flag contains invalid data")
}
if len(info.Data) == 0 {
log.Fatal("no sync data found")
}
return info, err
}
func ProcessSyncInfo(info SyncInfo) error {
var err error
for _, data := range info.Data {
for _, pair := range data.Pairs {
pair.Source = strings.ReplaceAll(pair.Source, "~", info.HomeDir)
pair.Target = strings.ReplaceAll(pair.Target, "~", info.HomeDir)
if pair.Source, err = filepath.Abs(pair.Source); err != nil {
log.Error(err)
log.Fatalf("unable to determine absolute path: %s", pair.Source)
}
log.Infof("pair.Source = %s", pair.Source)
if pair.Target, err = filepath.Abs(pair.Target); err != nil {
log.Error(err)
log.Fatalf("unable to determine absolute path: %s", pair.Target)
}
log.Infof("pair.Target = %s", pair.Target)
if err = ProcessSyncPair(pair); err != nil {
log.Fatal(err)
}
}
}
return err
}
func ProcessSyncPair(pair SyncPair) error {
var md = ".md"
return filepath.Walk(pair.Source, func(path string, info os.FileInfo, errWalk error) error {
if errWalk != nil {
return errWalk
}
if info.IsDir() {
return nil
}
var rel string
if rel, errWalk = filepath.Rel(pair.Source, path); errWalk != nil {
return errWalk
}
var dest = filepath.Join(pair.Target, rel)
var ext = filepath.Ext(rel)
var mtype = mime.TypeByExtension(ext)
if ext == md {
var fio *os.File
if fio, errWalk = os.Open(path); errWalk != nil {
log.Fatal(errWalk)
}
defer fio.Close()
var buf = bufio.NewReader(fio)
var matter = FrontMatter{}
frontmatter.Parse(buf, &matter)
if !matter.Draft {
return ProcessMarkdown(path, dest)
}
return nil
} else if len(mtype) >= 6 && mtype[:6] == "image/" {
log.Debugf("mime = %s\t%s", mtype, rel)
CopyFile(path, dest)
return nil
}
return fmt.Errorf("unsupported file type [%s]: %s", mtype, path)
})
}
func ProcessMarkdown(path string, dest string) error {
/*
https://help.obsidian.md/Editing+and+formatting/Callouts
https://gohugo.io/content-management/shortcodes/
https://discourse.gohugo.io/t/using-wide-with-markdown-images-and-hugo-processing/39879/9
https://jpdroege.com/blog/hugo-shortcodes-partials/
https://gohugo.io/content-management/shortcodes/
*/
var err error
var fio *os.File
var fdst *os.File
var res pageparser.ContentFrontMatter
if fio, err = os.Open(path); err != nil {
log.Fatal(err)
}
defer fio.Close()
if res, err = pageparser.ParseFrontMatterAndContent(fio); err != nil {
log.Error(err)
}
var content = string(res.Content)
var reCallouts = regexp.MustCompile(`(?s)\>\s*?\[!\w{2,10}\]\-?\+?.*?\n(?:\>.*?\n)+`)
var cleaned = reCallouts.ReplaceAllStringFunc(content, func(dirty string) string {
if len(dirty) == 0 {
return ""
}
var callout strings.Builder
for i, line := range strings.Split(dirty, "\n") {
line = strings.TrimPrefix(line, "> ")
if i == 0 {
var open bool
var parts = strings.SplitN(strings.TrimPrefix(line, "[!"), "]", 2)
var cType = parts[0]
var title = parts[1]
if title[:2] == "+ " {
open = true
title = title[2:]
} else if title[:2] == "- " {
title = title[2:]
}
callout.WriteString(`{{% callout type="`)
callout.WriteString(cType)
callout.WriteString(`" `)
if title != "" {
callout.WriteString(`title="`)
callout.WriteString(title)
callout.WriteString(`" `)
}
if open {
callout.WriteString(`open=true `)
} else {
// callout.WriteString(`open=false `)
}
callout.WriteString("%}}\n")
continue
}
callout.WriteString(line)
callout.WriteString("\n")
}
callout.WriteString("{{% /callout %}}\n")
log.Debugf("callout=%s", callout.String())
return callout.String()
})
var reLinks = regexp.MustCompile(`!?\[.*?\]\(.*?\)`)
cleaned = reLinks.ReplaceAllStringFunc(cleaned, func(dirty string) string {
if len(dirty) == 0 {
return ""
}
var iAlt = strings.Index(dirty, "[") + 1
var iAltEnd = iAlt + strings.Index(dirty[iAlt:], "]")
if iAltEnd < iAlt {
log.Fatalf("unknown issue: '%s'", dirty)
}
var iParams int
if strings.Index(dirty[iAlt:iAltEnd], "|") < 0 {
iParams = iAltEnd
} else {
iParams = iAlt + strings.Index(dirty[iAlt:iAltEnd], "|") + 1
}
var iAltTextEnd = iAltEnd
if iAltEnd != iParams {
iAltTextEnd = iParams - 1
}
var iLink = iAltEnd + strings.Index(dirty[iAltEnd:], "(") + 1
if iLink < 0 {
log.Fatalf("unknown issue: '%s'", dirty)
}
var iLinkEnd = iLink + strings.Index(dirty[iLink:], ")")
if len(dirty[iLinkEnd+1:]) != 0 {
log.Warnf(dirty[iLinkEnd+1:])
log.Fatalf("extra link data = '%s'", dirty)
}
var img strings.Builder
img.WriteString("{{% figure ")
if len(dirty[iAlt:iAltTextEnd]) != 0 {
img.WriteString("alt=\"")
img.WriteString(dirty[iAlt:iAltTextEnd])
img.WriteString("\" ")
// img.WriteString("title=\"")
// img.WriteString(dirty[iAlt:iAltTextEnd])
// img.WriteString("\" ")
}
if len(dirty[iLink:iLinkEnd]) != 0 {
img.WriteString("src=\"")
img.WriteString(dirty[iLink:iLinkEnd])
img.WriteString("\" ")
}
if len(dirty[iParams:iAltEnd]) != 0 {
var extra = dirty[iParams:iAltEnd]
var x = strings.Index(extra, "x")
if x < 0 {
img.WriteString("width=\"")
img.WriteString(extra)
img.WriteString("\" ")
} else {
img.WriteString("width=\"")
img.WriteString(extra[:x])
img.WriteString("\" ")
if x+1 <= (len(extra)-1) && extra[x+1:] != "" {
img.WriteString(", height=")
img.WriteString(extra[x+1:])
img.WriteString(" ")
}
}
}
img.WriteString("%}}")
log.Debugf("result=%s", img.String())
return img.String()
})
// save the processed content
if fdst, err = os.OpenFile(dest, os.O_RDWR, 0755); err != nil {
log.Fatal(err)
}
defer fdst.Close()
var writeBuf bytes.Buffer
if len(res.FrontMatter) != 0 {
err := parser2.InterfaceToFrontMatter(res.FrontMatter, "yaml", &writeBuf)
if err != nil {
log.Error(err)
}
}
writeBuf.WriteString(cleaned)
fdst.Truncate(0)
fdst.Seek(0,0)
fdst.Write(writeBuf.Bytes())
return err
}
func CopyFile(src string, dst string) error {
var err error
var srcStat os.FileInfo
var source *os.File
var destination *os.File
if srcStat, err = os.Stat(src); err != nil {
return err
}
if !srcStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
if err = os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return err
}
if source, err = os.Open(src); err != nil {
return err
}
defer source.Close()
if destination, err = os.Create(dst); err != nil {
return err
}
defer destination.Close()
log.Debugf("copying %s", dst)
log.Debugf("src = %s", src)
log.Debugf("dst = %s", dst)
if _, err = io.Copy(destination, source); err != nil {
return err
}
if srcStat, err = os.Stat(src); err != nil {
return err
}
if err = os.Chmod(dst, srcStat.Mode()); err != nil {
return err
}
return nil
}
type Ext struct {
Title string
Description string
DescriptionTag string
}
func (e *Ext) Extend(md goldmark.Markdown) {
md.Parser().AddOptions(
parser.WithASTTransformers(util.Prioritized(e, 999)),
)
}
func (e* Ext) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
tldrFound := false
//node.Dump(reader.Source(), 0)
for c := node.FirstChild(); c != nil; c = c.NextSibling() {
if c.Kind() == gast.KindHeading {
h := c.(*gast.Heading)
if h.Level == 1 {
e.Title = string(c.Text(reader.Source()))
} else if h.Level == 2 {
t := c.FirstChild().(*gast.Text)
h2Text := string(t.Text(reader.Source()))
if h2Text == e.DescriptionTag {
tldrFound = true
}
}
}
// Extract description
if tldrFound && c.Kind() == gast.KindParagraph {
log.Debug(c.FirstChild().Kind().String())
e.Description = e.dumpStr(c, reader.Source(), "")
return
}
}
}
func (e *Ext) dumpStr(c gast.Node, source []byte, str string) string {
res := str
for l := c.FirstChild(); l != nil; l = l.NextSibling() {
if l.Kind() == gast.KindText {
desc := string(l.Text(source))
log.Debug(desc)
res = res + desc
} else {
if l.HasChildren() {
res = e.dumpStr(l, source, res)
}
}
}
return res
}
// Dir copies a whole directory recursively
func CopyDir(src string, dst string) error {
log.Printf("Copy %s to %s\n\n", src, dst)
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo
if srcinfo, err = os.Stat(src); err != nil {
return err
}
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}
if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = CopyDir(srcfp, dstfp); err != nil {
fmt.Println(err)
}
} else {
if err = CopyFile(srcfp, dstfp); err != nil {
fmt.Println(err)
}
}
}
return nil
}
func WalkMatch(root, pattern string) ([]string, error) {
var matches []string
var errWalk = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if true {
log.Fatalf("%+v", info)
}
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
return err
} else if matched {
matches = append(matches, path)
}
return nil
})
if errWalk != nil {
return nil, errWalk
}
return matches, nil
}