forked from ardumont/org2jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org2jekyll.el
594 lines (522 loc) · 23.1 KB
/
org2jekyll.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
;;; org2jekyll.el --- Minor mode to publish org-mode post to jekyll without specific yaml
;; Copyright (C) 2014-2017 Antoine R. Dumont <eniotna.t AT gmail.com>
;; Author: Antoine R. Dumont <eniotna.t AT gmail.com>
;; Maintainer: Antoine R. Dumont <eniotna.t AT gmail.com>
;; Version: 0.1.9
;; Package-Requires: ((dash-functional "2.11.0") (s "1.9.0") (deferred "0.3.1") (kv "0.0.19"))
;; Keywords: org-mode jekyll blog publish
;; URL: https://github.com/ardumont/org2jekyll
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Functions to ease publishing jekyll posts from org-mode file
;; Providing you have a working `'jekyll`' and `'org-publish`'
;; This will permit you to simply export an org-mode file with the right jekyll
;; format to the right folder
;;
;; M-x org2jekyll-create-draft create a draft with the necessary metadata
;;
;; M-x org2jekyll-publish publish the current post (or page) to the jekyll folder
;;
;; M-x org2jekyll-publish-pages to publish all pages (layout 'default')
;;
;; M-x org2jekyll-publish-posts to publish all post pages (layout 'post')
;;
;; M-x org2jekyll-mode to activate org2jekyll's minor mode
;;
;; You can customize using M-x customize-group RET org2jekyll RET
;;
;; More information on https://github.com/ardumont/org2jekyll
;;; Code:
(require 'org)
(require (if (version< emacs-version "24.4") 'org-publish 'ox-publish))
(require 'dash-functional)
(require 's)
(require 'deferred)
(require 'ido)
(require 'kv)
(defgroup org2jekyll nil "Publish org-mode posts to jekyll"
:tag "org2jekyll"
:version "0.0.3"
:group 'org)
(defcustom org2jekyll-blog-author nil
"Blog entry author."
:type 'string
:require 'org2jekyll
:group 'org2jekyll)
(defcustom org2jekyll-source-directory nil
"Path to the source directory."
:type 'string
:require 'org2jekyll
:group 'org2jekyll)
(defcustom org2jekyll-jekyll-directory nil
"Path to Jekyll blog."
:type 'string
:require 'org2jekyll
:group 'org2jekyll)
(defcustom org2jekyll-jekyll-drafts-dir nil
"Relative path to drafts directory."
:type 'string
:require 'org2jekyll
:group 'org2jekyll)
(defcustom org2jekyll-jekyll-posts-dir nil
"Relative path to posts directory."
:type 'string
:require 'org2jekyll
:group 'org2jekyll)
(defvar org2jekyll-jekyll-post-ext ".org"
"File extension of Jekyll posts.")
(defvar org2jekyll-jekyll-org-post-template nil
"Default template for org2jekyll draft posts.
The `'%s`' will be replaced respectively by name, author, generated date, title,
description and categories.")
(setq org2jekyll-jekyll-org-post-template
"#+STARTUP: showall
#+STARTUP: hidestars
#+OPTIONS: H:2 num:nil tags:nil toc:nil timestamps:t
#+LAYOUT: %s
#+AUTHOR: %s
#+DATE: %s
#+TITLE: %s
#+DESCRIPTION: %s
#+TAGS: %s
#+CATEGORIES: %s
\n")
(defun org2jekyll--optional-folder (folder-source &optional folder-name)
"Compute the folder name from a FOLDER-SOURCE and an optional FOLDER-NAME."
(format "%s/%s" folder-source (if folder-name folder-name "")))
(defun org2jekyll-input-directory (&optional folder-name)
"Compute the input folder from the FOLDER-NAME."
(org2jekyll--optional-folder org2jekyll-source-directory folder-name))
(defun org2jekyll-output-directory (&optional folder-name)
"Compute the output folder from the optional FOLDER-NAME."
(org2jekyll--optional-folder org2jekyll-jekyll-directory folder-name))
(defun org2jekyll--make-slug (s)
"Turn a string S into a slug."
(->> s
(replace-regexp-in-string "[\]\[(){}!#$~^\\]" "")
downcase
(replace-regexp-in-string " " "-")))
(defun org2jekyll--yaml-escape (s)
"Escape a string S for YAML."
(if (or (string-match ":" s)
(string-match "\"" s))
(concat "\"" (replace-regexp-in-string "\"" "\\\\\"" s) "\"")
s))
(defun org2jekyll-now ()
"Generate a formatted now date."
(format-time-string "%Y-%m-%d %a %H:%M"))
(defun org2jekyll-default-headers-template (blog-layout
blog-author
post-date
post-title
post-description
post-tags
post-categories)
"Compute default headers.
BLOG-LAYOUT is the layout of the post.
BLOG-AUTHOR is the author.
POST-DATE is the date of the post.
POST-TITLE is the title.
POST-DESCRIPTION is the description.
POST-TAGS is the tags
POST-CATEGORIES is the categories."
(format org2jekyll-jekyll-org-post-template
blog-layout
blog-author
post-date
(org2jekyll--yaml-escape post-title)
post-description
post-tags
post-categories))
(defun org2jekyll--draft-filename (draft-dir title)
"Compute the draft's filename from the DRAFT-DIR and TITLE."
(concat draft-dir (org2jekyll--make-slug title) org2jekyll-jekyll-post-ext))
(defun org2jekyll--read-title ()
"Read the title."
(read-string "Title: "))
(defun org2jekyll--read-description ()
"Read the description."
(read-string "Description: "))
(defun org2jekyll--read-tags ()
"Read the tags."
(read-string "Tags (csv): "))
(defun org2jekyll--read-categories ()
"Read the categories."
(read-string "Categories (csv): "))
(defun org2jekyll--input-read (prompt collection)
"Input PROMPT with possibilities limited to COLLECTION."
(ido-completing-read prompt
collection
nil
'require-match))
(defun org2jekyll--init-buffer-metadata ()
"Initialize a DRAFT-FILE or current buffer routine.
:: () -> [(Symbol, String)]"
(list :author org2jekyll-blog-author
:date (org2jekyll-now)
:layout (org2jekyll--input-read "Layout: " '("post" "default"))
:title (org2jekyll--read-title)
:description (org2jekyll--read-description)
:tags (org2jekyll--read-tags)
:categories (org2jekyll--read-categories)))
;;;###autoload
(defun org2jekyll-init-current-buffer ()
"Given an existing buffer, add the needed metadata to make it a post or page."
(interactive)
(let* ((metadata (org2jekyll--init-buffer-metadata))
(author (plist-get metadata :author))
(date (plist-get metadata :date))
(layout (plist-get metadata :layout))
(title (plist-get metadata :title))
(description (plist-get metadata :description))
(tags (plist-get metadata :tags))
(categories (plist-get metadata :categories)))
(save-excursion
(with-current-buffer (buffer-name)
(goto-char (point-min))
(insert (org2jekyll-default-headers-template layout
author
date
title
description
tags
categories))))))
;;;###autoload
(defun org2jekyll-create-draft ()
"Create a new Jekyll blog post with TITLE.
The `'%s`' will be replaced respectively by the blog entry name, the author, the
generated date, the title, the description, the tags and the categories."
(interactive)
(let* ((metadata (org2jekyll--init-buffer-metadata))
(author (plist-get metadata :author))
(date (plist-get metadata :date))
(layout (plist-get metadata :layout))
(title (plist-get metadata :title))
(description (plist-get metadata :description))
(tags (plist-get metadata :tags))
(categories (plist-get metadata :categories))
(draft-file (org2jekyll--draft-filename
(org2jekyll-input-directory org2jekyll-jekyll-drafts-dir)
title)))
(unless (file-exists-p draft-file)
(with-temp-file draft-file
(insert (org2jekyll-default-headers-template layout
author
date
title
description
tags
categories))
(insert "* ")))
(find-file draft-file)))
(defun org2jekyll--list-dir (dir)
"List the content of DIR."
(find-file dir))
;;;###autoload
(defun org2jekyll-list-posts ()
"Lists the posts folder."
(interactive)
(org2jekyll--list-dir
(org2jekyll-output-directory org2jekyll-jekyll-posts-dir)))
;;;###autoload
(defun org2jekyll-list-drafts ()
"List the drafts folder."
(interactive)
(org2jekyll--list-dir
(org2jekyll-input-directory org2jekyll-jekyll-drafts-dir)))
(defun org2jekyll-get-options-from-buffer ()
"Return special lines at the beginning of current buffer."
(let ((special-line-regex "^#\\+\\(.+\\):[ \t]+\\(.+\\)$")
(get-current-line (lambda ()
(buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
(options-plist))
(save-excursion
(goto-char (point-min))
(catch 'break
(while (string-match special-line-regex (funcall get-current-line))
(setq options-plist (plist-put options-plist
(->> (funcall get-current-line)
(match-string 1)
downcase
(concat ":")
intern)
(match-string 2 (funcall get-current-line))))
(unless (= 0 (forward-line))
(throw 'break nil))))
options-plist)))
(defun org2jekyll-get-options-from-file (orgfile)
"Return special lines at the beginning of ORGFILE."
(with-temp-buffer
(when (file-exists-p orgfile)
(insert-file-contents orgfile)
(org2jekyll-get-options-from-buffer))))
(defun org2jekyll-article-p (org-file)
"Determine if the current ORG-FILE's layout.
Depends on the metadata header #+LAYOUT."
(plist-get (org2jekyll-get-options-from-file org-file) :layout))
(defun org2jekyll--org-to-jekyll-metadata (org-metadata)
"Given an ORG-METADATA map, translate Org keywords to Jekyll keywords."
(let ((org2jekyll-map-keys '(("description" . "excerpt"))))
(--map (-if-let (jekyll-car (assoc-default (car it) org2jekyll-map-keys))
(cons jekyll-car (cdr it))
it)
org-metadata)))
(defun org2jekyll--convert-timestamp-to-yyyy-dd-mm (timestamp)
"Convert org TIMESTAMP to ."
(format-time-string "%Y-%m-%d"
(apply 'encode-time (org-parse-time-string timestamp))))
(defun org2jekyll--old-org-version-p ()
"Determine if we are in old version or not."
;; HACK: support both pre-9.0 and 9.0+ Org export block syntax. Instead of
;; checking for the version, check for org-element-block-name-alist
;; availability to detect pre-release 9.0 Git snapshots with the new syntax,
;; see http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=54318ad
(boundp 'org-element-block-name-alist))
(defun org2jekyll--to-yaml-header (org-metadata)
"Given a list of ORG-METADATA, compute the yaml header string."
(-let (((begin end) (if (org2jekyll--old-org-version-p)
'("#+BEGIN_HTML" "#+END_HTML\n")
'("#+BEGIN_EXPORT HTML" "#+END_EXPORT\n"))))
(--> org-metadata
org2jekyll--org-to-jekyll-metadata
(--map (format "%s: %s" (car it) (cdr it)) it)
(cons "---" it)
(cons begin it)
(-snoc it "---")
(-snoc it end)
(s-join "\n" it))))
(defun org2jekyll--csv-to-yaml (str-csv)
"Transform a STR-CSV entries into a yaml entries."
(->> str-csv
(concat ",")
(s-replace ", " ",")
(s-replace "," "\n- ")
s-trim
(concat "\n")))
(defun org2jekyll--compute-ready-jekyll-file-name (date org-file)
"Given a DATE and an ORG-FILE, compute a ready jekyll file name.
If the current path contains the `'org2jekyll-jekyll-drafts-dir`', removes it."
(let ((temp-org-jekyll-filename (format "%s-%s" date
(file-name-nondirectory org-file)))
(temp-org-jekyll-directory (file-name-directory org-file)))
(->> temp-org-jekyll-filename
(format "%s%s" temp-org-jekyll-directory)
(replace-regexp-in-string (format "%s" org2jekyll-jekyll-drafts-dir) "")
(replace-regexp-in-string "//" "/"))))
(defun org2jekyll--copy-org-file-to-jekyll-org-file (date org-file yaml-headers)
"Given DATE, ORG-FILE and YAML-HEADERS, copy content as org-jekyll ready file.
This returns the new filename path."
(let ((jekyll-filename (org2jekyll--compute-ready-jekyll-file-name date
org-file)))
(with-temp-file jekyll-filename ;; write file updated with jekyll specifics
(insert-file-contents org-file)
(goto-char (point-min))
(insert (org2jekyll--to-yaml-header yaml-headers)))
jekyll-filename))
(defun org2jekyll-assoc-default (key org-data default-value)
"Given KEY, ORG-DATA and DEFAULT-VALUE, return the value associated with key.
Return DEFAULT-VALUE if not found."
(-if-let (data (assoc-default key org-data nil default-value))
data
default-value))
(defconst org2jekyll-required-org-header-alist '((:title . 'required)
(:date)
(:categories . 'required)
(:tags)
(:description . 'required)
(:author)
(:layout . 'required)))
(defun org2jekyll-check-metadata (org-metadata)
"Check that all required headers in ORG-METADATA are provided.
Return error messages for any required headers that are missing,
and nil if no problems are found."
(let ((required-options (funcall (-compose (lambda (l) (mapcar #'car l))
(lambda (l) (-filter #'cdr l)))
org2jekyll-required-org-header-alist)))
(-when-let (error-messages
(->> required-options
(--map (unless (plist-member org-metadata it)
(format (concat "- The %s is required, please add "
"'#+%s' at the top of your org buffer.")
(substring (symbol-name it) 1 nil)
(upcase (substring (symbol-name it) 1 nil)))))
(s-join "\n")
s-trim))
(if (string= "" error-messages) nil error-messages))))
(defun org2jekyll-remove-org-only-options (yaml-alist)
"Filter out org options with no Jekyll meaning from YAML-ALIST."
(let* ((required-options (--map (substring (symbol-name (car it)) 1 nil)
org2jekyll-required-org-header-alist))
(org-options (--map (downcase (substring it 0 -1))
org-options-keywords))
(org-only-options (--filter (not (member it required-options))
org-options))
(jekyll-options (--filter (not (member (car it) org-only-options))
yaml-alist)))
jekyll-options))
(defun org2jekyll-read-metadata (org-file)
"Given an ORG-FILE, return its org metadata.
If unrequired values are missing, they are replaced with dummy
ones. Otherwise, display the error messages about the missing
required values."
(let* ((buffer-metadata (org2jekyll-get-options-from-file org-file))
(org-defaults `(:date ,(org2jekyll-now)
:tags "dummy-tags-should-be-replaced"
:author ""))
(merged-metadata (kvplist-merge org-defaults buffer-metadata))
(categories (org2jekyll--csv-to-yaml (plist-get merged-metadata :categories)))
(tags (org2jekyll--csv-to-yaml (plist-get merged-metadata :tags)))
(date (org2jekyll--convert-timestamp-to-yyyy-dd-mm (plist-get merged-metadata :date)))
(yaml-metadata (-> merged-metadata
(plist-put :categories categories)
(plist-put :tags tags)
(plist-put :date date)))
(yaml-alist (--map (cons (symbol-name (car it))
(cdr it))
(kvplist->alist yaml-metadata))))
(-if-let (error-messages (org2jekyll-check-metadata buffer-metadata))
(format "This org-mode file is missing required header(s):
%s
Publication skipped" error-messages)
(org2jekyll-remove-org-only-options yaml-alist))))
(defun org2jekyll-read-metadata-and-execute (action-fn org-file)
"Execute ACTION-FN function after checking metadata from the ORG-FILE."
(let ((filename-non-dir (file-name-nondirectory org-file)))
(if (org2jekyll-article-p org-file)
(let ((org-metadata (org2jekyll-read-metadata org-file)))
(if (stringp org-metadata)
(org2jekyll-message org-metadata)
(let ((page-or-post (if (org2jekyll-post-p
(assoc-default "layout" org-metadata))
"Post"
"Page")))
(funcall action-fn org-metadata org-file)
(format "%s '%s' published!" page-or-post filename-non-dir))))
(format "'%s' is not an article, publication skipped!" filename-non-dir))))
(defun org2jekyll-message (&rest args)
"Log formatted ARGS."
(apply 'message (format "org2jekyll - %s" (car args)) (cdr args)))
(defun org2jekyll--publish-post-org-file-with-metadata (org-metadata org-file)
"Publish as post with ORG-METADATA the ORG-FILE."
(let ((blog-project (assoc-default "layout" org-metadata))
(jekyll-filename (org2jekyll--copy-org-file-to-jekyll-org-file
(assoc-default "date" org-metadata)
org-file org-metadata)))
(org-publish-file jekyll-filename
(assoc blog-project org-publish-project-alist))
(delete-file jekyll-filename)))
(defun org2jekyll-publish-post (org-file)
"Publish ORG-FILE as a post."
(org2jekyll-read-metadata-and-execute
'org2jekyll--publish-post-org-file-with-metadata
org-file))
(defun org2jekyll--publish-page-org-file-with-metadata (org-metadata org-file)
"Publish as page with ORG-METADATA the ORG-FILE."
(let* ((blog-project (assoc-default "layout" org-metadata))
(ext (file-name-extension org-file))
(temp-file (format "%sorg2jekyll" (s-chop-suffix ext org-file))))
(copy-file org-file temp-file t t t)
(with-temp-file temp-file
(insert-file-contents temp-file)
(goto-char (point-min))
(insert (org2jekyll--to-yaml-header org-metadata))
(write-file temp-file)
(org-publish-file temp-file
(assoc blog-project org-publish-project-alist)))
(delete-file temp-file)))
(defun org2jekyll-publish-page (org-file)
"Publish ORG-FILE as a page."
(org2jekyll-read-metadata-and-execute
'org2jekyll--publish-page-org-file-with-metadata
org-file))
(defun org2jekyll-post-p (layout)
"Determine if the LAYOUT corresponds to a post."
(string= "post" layout))
(defun org2jekyll-page-p (layout)
"Determine if the LAYOUT corresponds to a page."
(string= "default" layout))
(defun org2jekyll-publish-web-project ()
"Publish the 'web' project."
(org2jekyll-message "Publish `'web`' project (images, css, js, etc...).")
(org-publish-project "web"))
;;;###autoload
(defun org2jekyll-publish ()
"Publish the current org file as post or page depending on the chosen layout.
Layout `'post`' is a jekyll post.
Layout `'default`' is a page."
(interactive)
(lexical-let ((org-file (buffer-file-name (current-buffer))))
(deferred:$
(deferred:next (lambda ()
(-> (plist-get (org2jekyll-get-options-from-buffer) :layout)
org2jekyll-post-p
(if 'org2jekyll-publish-post
'org2jekyll-publish-page))))
(deferred:nextc it (lambda (publish-fn) (funcall publish-fn org-file)))
(deferred:nextc it (lambda (final-message)
(org2jekyll-publish-web-project)
final-message))
(deferred:nextc it (lambda (final-message)
(org2jekyll-message final-message))))))
(defvar org2jekyll-mode-map nil "Default Bindings map for org2jekyll mode.")
(setq org2jekyll-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c . n") 'org2jekyll-create-draft)
(define-key map (kbd "C-c . p") 'org2jekyll-publish)
(define-key map (kbd "C-c . P") 'org2jekyll-publish-posts)
(define-key map (kbd "C-c . l") 'org2jekyll-list-posts)
(define-key map (kbd "C-c . d") 'org2jekyll-list-drafts)
map))
;;;###autoload
(defun org2jekyll-publish-posts ()
"Publish all the posts."
(interactive)
(deferred:$
(deferred:next
(lambda () (->> (assoc "post" org-publish-project-alist)
org-publish-get-base-files
(--filter (org2jekyll-post-p (org2jekyll-article-p it))))))
(deferred:nextc it
(lambda (posts)
(mapc #'org2jekyll-publish-post posts)))))
;;;###autoload
(defun org2jekyll-publish-pages ()
"Publish all the pages."
(interactive)
(deferred:$
(deferred:next
(lambda () (->> (assoc "default" org-publish-project-alist)
org-publish-get-base-files
(--filter (org2jekyll-page-p (org2jekyll-article-p it))))))
(deferred:nextc it
(lambda (pages)
(mapc #'org2jekyll-publish-page pages)))))
;;;###autoload
(define-minor-mode org2jekyll-mode
"Functionality for publishing the current org-mode post to jekyll.
With no argument, the mode is toggled on/off.
Non-nil argument turns mode on.
Nil argument turns mode off.
Commands:
\\{org2jekyll-mode-map}"
:init-value nil
:lighter " o2j"
:group 'org2jekyll
:keymap org2jekyll-mode-map)
(provide 'org2jekyll)
;;; org2jekyll.el ends here