write.go (1794B)
1 package org 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "text/template" 8 ) 9 10 var ( 11 additionnalTemplate = `{{ if .Highlights }}* New Highlights on [{{ .Date }}] 12 {{ range $h := .Highlights -}} 13 ** [{{ $h.Date }}] Highlight [[{{ $h.URL }}][{{ $h.ID }}]]{{ if $h.Tags }} {{ orgtags $h.Tags }}{{ end }} 14 {{ $h.Text }} 15 {{ if $h.Note }}*** Note 16 {{ $h.Note }} 17 {{ end -}} 18 {{ end }}{{ end }}` 19 20 mainTemplate = `#+title: {{ .Title }} 21 #+author: {{ .Author }} 22 #+date: {{ .Date }} 23 #+identifier: {{ .Identifier }} 24 #+category: {{ .Category }} 25 {{ if .FileTags }}#+filetags: {{ orgtags .FileTags }}{{ end }} 26 #+property: READWISE_URL: {{ .ReadwiseURL }} 27 {{ if .URL }}#+property: URL: {{ .URL }}{{ end }} 28 29 {{ .Summary }} 30 31 * Highlights 32 {{ range $h := .Highlights -}} 33 ** [{{ $h.Date }}] Highlight [[{{ $h.URL }}][{{ $h.ID }}]]{{ if $h.Tags }} {{ orgtags $h.Tags }}{{ end }} 34 {{ $h.Text }} 35 {{ if $h.Note }}*** Note 36 {{ $h.Note }} 37 {{ end -}} 38 {{ end -}} 39 ` 40 ) 41 42 func orgtags(tags []string) string { 43 if len(tags) == 0 { 44 return "" 45 } 46 return fmt.Sprintf(":%s:", strings.Join(tags, ":")) 47 } 48 49 func convertDocument(d Document) ([]byte, error) { 50 var err error 51 52 funcMap := template.FuncMap{ 53 "orgtags": orgtags, 54 } 55 56 tmpl, err := template.New("org").Funcs(funcMap).Parse(mainTemplate) 57 if err != nil { 58 return []byte{}, err 59 } 60 var buff bytes.Buffer 61 if err := tmpl.Execute(&buff, d); err != nil { 62 return []byte{}, err 63 } 64 return buff.Bytes(), nil 65 } 66 67 func convertPartialDocument(d PartialDocument) ([]byte, error) { 68 var err error 69 70 funcMap := template.FuncMap{ 71 "orgtags": orgtags, 72 } 73 74 tmpl, err := template.New("org").Funcs(funcMap).Parse(additionnalTemplate) 75 if err != nil { 76 return []byte{}, err 77 } 78 var buff bytes.Buffer 79 if err := tmpl.Execute(&buff, d); err != nil { 80 return []byte{}, err 81 } 82 return buff.Bytes(), nil 83 }