-
Notifications
You must be signed in to change notification settings - Fork 17
/
org-export-pdf.el
98 lines (86 loc) · 3.42 KB
/
org-export-pdf.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
(require 'cli (concat (file-name-directory load-file-name) "org-export-cli.el"))
(cli-eval-file cli-config-file)
(cli-package-setup cli-package-dir cli-packages)
(require 'ox)
(require 'ox-latex)
;; (byte-compile-file (concat (file-name-directory load-file-name) "cli.el"))
(setq options-alist
`(("--infile" "path to input .org file (required)")
("--outfile" "path to output .pdf file (use base name of infile by default)"
nil)
("--evaluate" "evaluate source code blocks" nil)
("--config" "an elisp expression defining additional configuration" nil)
("--config-file" "a file path containing elisp
expressions defining additional configuration" nil)))
(setq docstring "
Note that code block evaluation is disabled by default; use
'--evaluate' to set a default value of ':eval yes' for all code
blocks. If you would like to evaluate by default without requiring
this option, include '#+PROPERTY: header-args :eval yes' in the file
header. Individual blocks can be selectively evaluated using ':eval
yes' in the block header.
")
(condition-case err
(setq args (cli-parse-args options-alist docstring))
(quit (kill-emacs 0))
(error (progn (message (nth 1 err)) (kill-emacs 1))))
(defun getopt (name) (gethash name args))
;; ess configuration
(add-hook 'ess-mode-hook
(lambda ()
(setq ess-ask-for-ess-directory nil)))
;; org-mode and export configuration
(add-hook 'org-mode-hook
(lambda ()
;; (font-lock-mode)
;; (setq org-src-fontify-natively t)
;; (setq htmlize-output-type 'inline-css)
(setq org-confirm-babel-evaluate nil)
(setq org-export-allow-BIND 1)
;; (setq org-export-preserve-breaks t)
;; (setq org-export-with-sub-superscripts nil)
;; (setq org-export-with-section-numbers nil)
(setq org-babel-sh-command "bash")
(setq org-babel-default-header-args
(list `(:session . "none")
`(:eval . ,(if (getopt "evaluate") "yes" "no"))
`(:results . "output replace")
`(:exports . "both")
`(:cache . "no")
`(:noweb . "no")
`(:hlines . "no")
`(:tangle . "no")
`(:padnewline . "yes")
))
;; explicitly set the PATH in sh code blocks; note that
;; `list`, the backtick, and the comma are required to
;; dereference cli-sh-src-prologue as a variable; see
;; http://stackoverflow.com/questions/24188100
(setq org-babel-default-header-args:sh
(list `(:prologue . ,cli-sh-src-prologue)))
(cli-org-babel-load-languages (getopt "add-langs"))
)) ;; end org-mode-hook
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;; compile and export ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; evaluate extra configuration if provided
(cli-eval-file cli-config-file)
(cli-eval-expr (getopt "config"))
(cli-eval-file (getopt "config-file"))
;; export using a temporary buffer to avoid modifying input file; working
;; directory contains the input file.
(let* ((infile (expand-file-name (getopt "infile")))
(outfile (cli-get-output-file (getopt "outfile") infile ".pdf"))
(texfile (concat (file-name-sans-extension outfile) ".tex"))
(async nil)
(subtreep nil)
(visible-only nil)
(body-only nil)
(ext-plist nil))
(with-temp-buffer
(insert-file-contents-literally infile)
(cd (file-name-directory infile))
(org-mode)
(org-export-to-file 'latex texfile
async subtreep visible-only body-only ext-plist #'org-latex-compile)
))