-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorg-iv.el
106 lines (92 loc) · 4.06 KB
/
org-iv.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
;;; org-iv.el --- a tool used to view html generated by org-file immediately (lively).
;; This is free and unencumbered software released into the public domain.
;; Author: kuangdash <[email protected]>
;; Version: 1.0.1
;; Homepage: https://github.com/kuangdash/org-iv
;; Package-Requires: ((impatient-mode "1.0.0") (org "8.0") (cl-lib "0.5"))
;;; Commentary:
;; org-iv is a tool used to view html generated by org-file
;; immediately (lively). Powered by impatient-mode.
;; How to use it? see https://github.com/kuangdash/org-iv
;;; Code:
(require 'ox)
(require 'impatient-mode)
(require 'org-iv-config)
(require 'cl-lib)
;;;###autoload
(defun org-iv/immediate-view (config-name)
"Use impatient-mode to view html-file generated by org-file quickly."
(interactive
(let ((config-name
(ido-completing-read "Which config do you want to apply? "
(mapcar 'car org-iv/config-alist)
nil t nil nil nil)))
(list config-name)))
(setq org-iv--current-config-name config-name)
(with-temp-buffer
(insert-file-contents (org-iv--get-config-option :front-html-file))
(setq org-iv--front-html-string (buffer-string)))
(with-temp-buffer
(insert-file-contents (org-iv--get-config-option :back-html-file))
(setq org-iv--back-html-string (buffer-string)))
(let ((web-port (org-iv--get-config-option :web-test-port))
(web-dir (org-iv--get-config-option :web-test-root)))
(setq httpd-port web-port)
(httpd-serve-directory web-dir)
(imp-visit-buffer))
(imp-set-user-filter `(lambda (org-file)
"For use impatient-mode"
(let ((html-string (with-current-buffer org-file
(org-export-as (eval (org-iv--get-config-option :backend))
nil nil t nil))))
(insert org-iv--front-html-string
"\n"
html-string
"\n"
org-iv--back-html-string))))
(message "org-immediate-view: begin!!"))
(defun org-iv/stop-immediate-view ()
"stop org-iv/immediate-view"
(interactive)
(when (imp-buffer-enabled-p (buffer-name))
(impatient-mode)
(remove-hook 'after-save-hook 'imp--on-change t)
(when (yes-or-no-p "Stop the running http-server?: ")
(httpd-stop))))
(defun org-iv/manually-update ()
"Use org-iv/immediate-view manually (update when save the file)"
(interactive)
(when (cl-some (lambda (element) (equal element 'imp--on-change)) after-change-functions)
(remove-hook 'after-change-functions 'imp--on-change t))
(add-hook 'after-save-hook 'imp--on-change nil t))
(defun org-iv/restart-view ()
"Restart org-iv/immediate-view"
(interactive)
(when (imp-buffer-enabled-p (buffer-name))
(impatient-mode)
(httpd-stop))
(call-interactively 'org-iv/immediate-view))
(defun org-iv/org-to-html (save-file-path)
"Save the html file generated by org-iv in web-test-root"
(interactive (let* ((file-directory (org-iv--get-config-option :web-test-root))
(file-path (read-string "What is the name: " "index.html")))
(list (format "%s/%s" file-directory file-path))))
(with-temp-buffer
(insert-file-contents (org-iv--get-config-option :front-html-file))
(setq org-iv--front-html-string (buffer-string)))
(with-temp-buffer
(insert-file-contents (org-iv--get-config-option :back-html-file))
(setq org-iv--back-html-string (buffer-string)))
(let ((html-string (with-current-buffer (current-buffer)
(org-export-as (org-iv--get-config-option :backend)
nil nil t nil))))
(find-file save-file-path)
(insert org-iv--front-html-string
"\n"
html-string
"\n"
org-iv--back-html-string))
(message "Open the file in %s and then save the file in any place you like." save-file-path)
)
(provide 'org-iv)
;;; org-iv.el ends here