org_reader.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (require 'json)
  2. (require 'org)
  3. (defun org->pelican (filename backend)
  4. (progn
  5. (save-excursion
  6. ; open org file
  7. (find-file filename)
  8. ; pre-process some metadata
  9. (let (; extract org export properties
  10. (org-export-env (org-export-get-environment))
  11. ; convert MODIFIED prop to string
  12. (modifiedstr (cdr (assoc-string "MODIFIED" org-file-properties t)))
  13. ; prepare date property
  14. (dateobj (car (plist-get (org-export-get-environment) ':date)))
  15. )
  16. ; check if #+TITLE: is given and give sensible error message if not
  17. (if (symbolp (car (plist-get org-export-env :title)))
  18. (error "Each page/article must have a #+TITLE: property"))
  19. ; construct the JSON object
  20. (princ (json-encode
  21. (list
  22. ; org export environment
  23. :title (substring-no-properties
  24. (car (plist-get org-export-env :title)))
  25. ; if #+DATE is not given, dateobj is nil
  26. ; if #+DATE is a %Y-%m-%d string, dateobj is a string,
  27. ; and otherwise we assume #+DATE is a org timestamp
  28. :date (if (symbolp dateobj)
  29. ""
  30. (if (stringp dateobj)
  31. (org-read-date nil nil dateobj nil)
  32. (org-timestamp-format dateobj "%Y-%m-%d")))
  33. :author (substring-no-properties
  34. (car (plist-get org-export-env ':author)))
  35. :language (plist-get org-export-env ':language)
  36. ; org file properties
  37. :category (cdr (assoc-string "CATEGORY" org-file-properties t))
  38. ; custom org file properties, defined as #+PROPERTY: NAME ARG
  39. :save_as (cdr (assoc-string "SAVE_AS" org-file-properties t))
  40. :tags (cdr (assoc-string "TAGS" org-file-properties t))
  41. :summary (cdr (assoc-string "SUMMARY" org-file-properties t))
  42. :slug (cdr (assoc-string "SLUG" org-file-properties t))
  43. :modified (if (stringp modifiedstr)
  44. (org-read-date nil nil modifiedstr nil)
  45. "")
  46. :post (org-export-as backend nil nil t)
  47. )
  48. )
  49. )
  50. )
  51. )
  52. )
  53. )