home

My NixOS systems configurations.
Log | Files | Refs | LICENSE

commit 1c6eb8a5c7478989c36400450971a72a02a4d0e0
parent 9eb67e685b5d04401b6dbbcf9b273ab298fd7cdd
Author: Vincent Demeester <vincent@sbr.pm>
Date:   Mon, 17 Jun 2024 16:50:49 +0200

go-org-readwise: implement updates on highlights

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Diffstat:
Mtools/go-org-readwise/internal/org/sync.go | 22++++++++++++++++++----
Mtools/go-org-readwise/internal/org/write.go | 25++++++++++++++++++++++++-
2 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/tools/go-org-readwise/internal/org/sync.go b/tools/go-org-readwise/internal/org/sync.go @@ -8,6 +8,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "github.com/vdemeester/home/tools/go-org-readwise/internal/readwise" ) @@ -48,7 +49,14 @@ func Sync(ctx context.Context, target string, results []readwise.Result) error { fmt.Println("file", filename) if _, err := os.Stat(filename); err == nil { // Append to the file - return errors.New("Not implemented") + p := createPartialOrgDocument(result) + content, err := convertPartialDocument(p) + if err != nil { + return err + } + if err := os.WriteFile(filename, content, 0o644); err != nil { + return err + } } else if errors.Is(err, os.ErrNotExist) { // Create the file d := createNewOrgDocument(result) @@ -76,7 +84,7 @@ func createNewOrgDocument(r readwise.Result) Document { filetags[i] = sluggify(t.Name) } } - d := Document{ + return Document{ Title: r.Title, Author: r.Author, ReadwiseURL: r.ReadwiseURL, @@ -89,7 +97,13 @@ func createNewOrgDocument(r readwise.Result) Document { Summary: r.Summary, Highlights: transformHighlights(r.Highlights), } - return d +} + +func createPartialOrgDocument(r readwise.Result) PartialDocument { + return PartialDocument{ + Date: time.Now().Format(orgDateFormat), + Highlights: transformHighlights(r.Highlights), + } } func transformHighlights(highlights []readwise.Highlight) []Highlight { @@ -134,7 +148,7 @@ func denoteFilename(result readwise.Result) string { // to use them to establish sequential relations between files // (e.g. 1, 1a, 1b, 1b1, 1b2, …). // We use signature to mark files synced from readwise. - signature = "==" + result.Category + signature = "==" + result.Category + "@readwise" // The TITLE field is the title of the note, as provided by // the user. It automatically gets downcased by default and is diff --git a/tools/go-org-readwise/internal/org/write.go b/tools/go-org-readwise/internal/org/write.go @@ -9,7 +9,12 @@ import ( var ( additionnalTemplate = `* New Highlights on {{ .Date }} -{{ range .Highlights }} +{{ range $h := .Highlights -}} +** [{{ $h.Date }}] Highlight [[{{ $h.URL }}][{{ $h.ID }}]]{{ if $h.Tags }} {{ orgtags $h.Tags }}{{ end }} +{{ $h.Text }} +{{ if $h.Note }}*** Note +{{ $h.Note }} +{{ end -}} {{ end }}` mainTemplate = `#+title: {{ .Title }} @@ -58,3 +63,21 @@ func convertDocument(d Document) ([]byte, error) { } return buff.Bytes(), nil } + +func convertPartialDocument(d PartialDocument) ([]byte, error) { + var err error + + funcMap := template.FuncMap{ + "orgtags": orgtags, + } + + tmpl, err := template.New("org").Funcs(funcMap).Parse(additionnalTemplate) + if err != nil { + return []byte{}, err + } + var buff bytes.Buffer + if err := tmpl.Execute(&buff, d); err != nil { + return []byte{}, err + } + return buff.Bytes(), nil +}