Skip to content

Instantly share code, notes, and snippets.

@sulami
Last active March 22, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sulami/536d430efb5b41f239838113d7d0f3b5 to your computer and use it in GitHub Desktop.
Save sulami/536d430efb5b41f239838113d7d0f3b5 to your computer and use it in GitHub Desktop.
Convert my markdown blog sources to org-mode
#!/usr/bin/env racket
#lang racket
(require threading)
(define (complement f)
(λ args
(not (apply f args))))
(define is-file?
(complement directory-exists?))
(define (insert-at lst pos x)
(define-values (before after) (split-at lst pos))
(append before (cons x after)))
(define (remove-file-timestamp file-path)
(let* ((parts (string-split file-path "/")))
(~> parts
(list-update (- (length parts) 1)
(λ (s) (substring s 11)))
(string-join "/"))))
(define (md->org-header lines)
(map (λ (l)
(define-values (k v) (apply values (string-split l ":")))
(string-append "#+" (string-upcase k) ":" v))
lines))
(define (maybe-shift-headers lines)
(map (λ (line)
(if (string-prefix? line "*")
(substring line 1)
line))
lines))
(define (system-in-out-string in cmd)
(with-input-from-string in
(λ ()
(with-output-to-string
(λ ()
(system cmd))))))
(define (pandoc-string content)
(system-in-out-string content
"pandoc -f markdown -t org"))
(define (convert-file file-name)
(printf "converting ~a\n" file-name)
(let* ((file-date (~> file-name
(string-split "/")
(last)
(substring 0 10)))
(new-file-name (~> file-name
(string-split ".")
(first)
(string-append ".org")
(remove-file-timestamp)))
(old-file-content (file->string file-name))
(header (~> old-file-content
(string-split "\n")
(rest)
(takef (λ (l) (not (equal? "---" l))))
(append (list (string-append "date: " file-date)))
(string-join "\n")))
(org-header (~> header
(string-split "\n")
(md->org-header)
(string-join "\n")))
(org-content (~> old-file-content
(pandoc-string)
(string-split "\n")
(maybe-shift-headers)
(string-join "\n")))
(new-file-content (string-append org-header
"\n\n"
org-content)))
(display-to-file header
(string-append new-file-name ".metadata")
#:exists 'truncate)
(display-to-file new-file-content
new-file-name
#:exists 'truncate)))
(~>> "content/posts"
(in-directory)
(sequence->list)
(filter is-file?)
(map path->string)
(filter (λ (fp) (string-suffix? fp ".md")))
(map convert-file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment