gemacs/org/format.scm

69 lines
2.0 KiB
Scheme

(define-module (org format)
#:use-module (srfi srfi-19) ; string/date
#:use-module (ice-9 regex)
#:export (date->org
link->org
org-properties))
;; <YYYY-MM-DD Sun HH:MM>
(define (date->org date show-time active)
"Converts a date object to an Org Mode date"
(let* ((brackets (if active '("<" ">")
'( "[" "]")))
(format (string-append (car brackets) "~Y-~m-~d ~a"))
(format (if show-time
(string-append format " ~H:~M")
format)))
(date->string date (string-append format (car (cdr brackets))))))
;; (date->org (current-date) #t #t) -> <2019-01-23 Wed 15:30>
;; (date->org (current-date) #t #f) -> [2019-01-23 Wed 15:30]
;; (date->org (current-date) #f #t) -> <2019-01-23 Wed>
;; (date->org (current-date) #f #f) -> [2019-01-23 Wed]
(define (link->org lnk description replace-spaces)
"Create an Org Mode link.
`lnk' is what the link is pointing to
`description' is the visible text
`replace-spaces' replace spaces url-encodes the spaces inside the link
"
(let* ((lnk (if replace-spaces
(regexp-substitute/global #f "[ \t]+" lnk
'pre "%20" 'post)
lnk))
(output (string-concatenate `("[[" ,lnk
,(if description
(string-concatenate `("][" ,description))
"")
"]]"
))))
output))
;; (link->org "http://blog.nixo.xyz/example page" "My blog" #t)
;; -> [[http://blog.nixo.xyz/example%20page][My blog]]
;; (link->org "http://blog.nixo.xyz/example page" "My blog" #f)
;; -> [[http://blog.nixo.xyz/example page][My blog]]
;; (link->org "http://blog.nixo.xyz/example page" #f #f)
;; -> [[http://blog.nixo.xyz/example page]]
(define (make-org-pair p)
(string-concatenate `(":" ,(car p) ": " ,(cadr p))))
(define (format-properties props)
(string-join
(map (lambda (x) (make-org-pair x)) props)
"\n"))
(define (org-properties props)
(string-concatenate
`(
":PROPERTIES:\n"
,(format-properties props)
"\n:END:")))
;; (org-properties `(("NAME" "value")
;; ("PROP2" ,(number->string 2))))
;; ":PROPERTIES:
;; :NAME: value
;; :PROP2: 2
;; :END:"