org_reader.el 2.4 KB

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