123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- (eval-when-compile
- (require 'cl))
- (require 'org)
- (defvar backend)
- (defvar org-exp-blocks-block-regexp
- (concat
- "^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)"
- "[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n]*[ \t]*"
- "#\\+end_\\S-+.*[\r\n]?")
- "Regular expression used to match blocks by org-exp-blocks.")
- (defun org-export-blocks-set (var value)
- "Set the value of `org-export-blocks' and install fontification."
- (set var value)
- (mapc (lambda (spec)
- (if (nth 2 spec)
- (setq org-protecting-blocks
- (delete (symbol-name (car spec))
- org-protecting-blocks))
- (add-to-list 'org-protecting-blocks
- (symbol-name (car spec)))))
- value))
- (defcustom org-export-blocks
- '(( org-export-blocks-format-comment t)
- (ditaa org-export-blocks-format-ditaa nil)
- (dot org-export-blocks-format-dot nil))
- "Use this alist to associate block types with block exporting functions.
- The type of a block is determined by the text immediately
- following the '#+BEGIN_' portion of the block header. Each block
- export function should accept three arguments."
- :group 'org-export-general
- :type '(repeat
- (list
- (symbol :tag "Block name")
- (function :tag "Block formatter")
- (boolean :tag "Fontify content as Org syntax")))
- :set 'org-export-blocks-set)
- (defun org-export-blocks-add-block (block-spec)
- "Add a new block type to `org-export-blocks'.
- BLOCK-SPEC should be a three element list the first element of
- which should indicate the name of the block, the second element
- should be the formatting function called by
- `org-export-blocks-preprocess' and the third element a flag
- indicating whether these types of blocks should be fontified in
- org-mode buffers (see `org-protecting-blocks'). For example the
- BLOCK-SPEC for ditaa blocks is as follows.
- (ditaa org-export-blocks-format-ditaa nil)"
- (unless (member block-spec org-export-blocks)
- (setq org-export-blocks (cons block-spec org-export-blocks))
- (org-export-blocks-set 'org-export-blocks org-export-blocks)))
- (defcustom org-export-interblocks
- '()
- "Use this a-list to associate block types with block exporting functions.
- The type of a block is determined by the text immediately
- following the '#+BEGIN_' portion of the block header. Each block
- export function should accept three arguments."
- :group 'org-export-general
- :type 'alist)
- (defcustom org-export-blocks-witheld
- '(hidden)
- "List of block types (see `org-export-blocks') which should not be exported."
- :group 'org-export-general
- :type 'list)
- (defcustom org-export-blocks-postblock-hook nil
- "Run after blocks have been processed with `org-export-blocks-preprocess'."
- :group 'org-export-general
- :type 'hook)
- (defun org-export-blocks-html-quote (body &optional open close)
- "Protect BODY from org html export.
- The optional OPEN and CLOSE tags will be inserted around BODY."
- (concat
- "\n#+BEGIN_HTML\n"
- (or open "")
- body (if (string-match "\n$" body) "" "\n")
- (or close "")
- "#+END_HTML\n"))
- (defun org-export-blocks-latex-quote (body &optional open close)
- "Protect BODY from org latex export.
- The optional OPEN and CLOSE tags will be inserted around BODY."
- (concat
- "\n#+BEGIN_LaTeX\n"
- (or open "")
- body (if (string-match "\n$" body) "" "\n")
- (or close "")
- "#+END_LaTeX\n"))
- (defun org-export-blocks-preprocess ()
- "Export all blocks according to the `org-export-blocks' block export alist.
- Does not export block types specified in specified in BLOCKS
- which defaults to the value of `org-export-blocks-witheld'."
- (interactive)
- (save-window-excursion
- (let ((case-fold-search t)
- (types '())
- indentation type func start body headers preserve-indent progress-marker)
- (flet ((interblock (start end)
- (mapcar (lambda (pair) (funcall (second pair) start end))
- org-export-interblocks)))
- (goto-char (point-min))
- (setq start (point))
- (while (re-search-forward org-exp-blocks-block-regexp nil t)
- (setq indentation (length (match-string 1)))
- (setq type (intern (downcase (match-string 2))))
- (setq headers (save-match-data (org-split-string (match-string 3) "[ \t]+")))
- (setq body (match-string 4))
- (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
- (unless preserve-indent
- (setq body (save-match-data (org-remove-indentation body))))
- (unless (memq type types) (setq types (cons type types)))
- (save-match-data (interblock start (match-beginning 0)))
- (when (setq func (cadr (assoc type org-export-blocks)))
- (let ((replacement (save-match-data
- (if (memq type org-export-blocks-witheld) ""
- (apply func body headers)))))
- (when replacement
- (replace-match replacement t t)
- (unless preserve-indent
- (indent-code-rigidly
- (match-beginning 0) (match-end 0) indentation)))))
- (setq start (match-end 0)))
- (interblock start (point-max))
- (run-hooks 'org-export-blocks-postblock-hook)))))
- (defvar org-ditaa-jar-path (expand-file-name
- "ditaa.jar"
- (file-name-as-directory
- (expand-file-name
- "scripts"
- (file-name-as-directory
- (expand-file-name
- "../contrib"
- (file-name-directory (or load-file-name buffer-file-name)))))))
- "Path to the ditaa jar executable.")
- (defun org-export-blocks-format-ditaa (body &rest headers)
- "DEPRECATED: use begin_src ditaa code blocks
- Pass block BODY to the ditaa utility creating an image.
- Specify the path at which the image should be saved as the first
- element of headers, any additional elements of headers will be
- passed to the ditaa utility as command line arguments."
- (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks")
- (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
- (data-file (make-temp-file "org-ditaa"))
- (hash (progn
- (set-text-properties 0 (length body) nil body)
- (sha1 (prin1-to-string (list body args)))))
- (raw-out-file (if headers (car headers)))
- (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
- (cons (match-string 1 raw-out-file)
- (match-string 2 raw-out-file))
- (cons raw-out-file "png")))
- (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
- (unless (file-exists-p org-ditaa-jar-path)
- (error (format "Could not find ditaa.jar at %s" org-ditaa-jar-path)))
- (setq body (if (string-match "^\\([^:\\|:[^ ]\\)" body)
- body
- (mapconcat (lambda (x) (substring x (if (> (length x) 1) 2 1)))
- (org-split-string body "\n")
- "\n")))
- (prog1
- (cond
- ((member backend '(html latex docbook))
- (unless (file-exists-p out-file)
- (mapc
- (lambda (file)
- (when (and (string-match (concat (regexp-quote (car out-file-parts))
- "_\\([[:alnum:]]+\\)\\."
- (regexp-quote (cdr out-file-parts)))
- file)
- (= (length (match-string 1 out-file)) 40))
- (delete-file (expand-file-name file
- (file-name-directory out-file)))))
- (directory-files (or (file-name-directory out-file)
- default-directory)))
- (with-temp-file data-file (insert body))
- (message (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file))
- (shell-command (concat "java -jar " org-ditaa-jar-path " " args " " data-file " " out-file)))
- (format "\n[[file:%s]]\n" out-file))
- (t (concat
- "\n#+BEGIN_EXAMPLE\n"
- body (if (string-match "\n$" body) "" "\n")
- "#+END_EXAMPLE\n")))
- (message "begin_ditaa blocks are DEPRECATED, use begin_src blocks"))))
- (defun org-export-blocks-format-dot (body &rest headers)
- "DEPRECATED: use \"#+begin_src dot\" code blocks
- Pass block BODY to the dot graphing utility creating an image.
- Specify the path at which the image should be saved as the first
- element of headers, any additional elements of headers will be
- passed to the dot utility as command line arguments. Don't
- forget to specify the output type for the dot command, so if you
- are exporting to a file with a name like 'image.png' you should
- include a '-Tpng' argument, and your block should look like the
- following.
- #+begin_dot models.png -Tpng
- digraph data_relationships {
- \"data_requirement\" [shape=Mrecord, label=\"{DataRequirement|description\lformat\l}\"]
- \"data_product\" [shape=Mrecord, label=\"{DataProduct|name\lversion\lpoc\lformat\l}\"]
- \"data_requirement\" -> \"data_product\"
- }
- #+end_dot"
- (message "begin_dot blocks are DEPRECATED, use begin_src blocks")
- (let* ((args (if (cdr headers) (mapconcat 'identity (cdr headers) " ")))
- (data-file (make-temp-file "org-ditaa"))
- (hash (progn
- (set-text-properties 0 (length body) nil body)
- (sha1 (prin1-to-string (list body args)))))
- (raw-out-file (if headers (car headers)))
- (out-file-parts (if (string-match "\\(.+\\)\\.\\([^\\.]+\\)$" raw-out-file)
- (cons (match-string 1 raw-out-file)
- (match-string 2 raw-out-file))
- (cons raw-out-file "png")))
- (out-file (concat (car out-file-parts) "_" hash "." (cdr out-file-parts))))
- (prog1
- (cond
- ((member backend '(html latex docbook))
- (unless (file-exists-p out-file)
- (mapc
- (lambda (file)
- (when (and (string-match (concat (regexp-quote (car out-file-parts))
- "_\\([[:alnum:]]+\\)\\."
- (regexp-quote (cdr out-file-parts)))
- file)
- (= (length (match-string 1 out-file)) 40))
- (delete-file (expand-file-name file
- (file-name-directory out-file)))))
- (directory-files (or (file-name-directory out-file)
- default-directory)))
- (with-temp-file data-file (insert body))
- (message (concat "dot " data-file " " args " -o " out-file))
- (shell-command (concat "dot " data-file " " args " -o " out-file)))
- (format "\n[[file:%s]]\n" out-file))
- (t (concat
- "\n#+BEGIN_EXAMPLE\n"
- body (if (string-match "\n$" body) "" "\n")
- "#+END_EXAMPLE\n")))
- (message "begin_dot blocks are DEPRECATED, use begin_src blocks"))))
- (defun org-export-blocks-format-comment (body &rest headers)
- "Format comment BODY by OWNER and return it formatted for export.
- Currently, this only does something for HTML export, for all
- other backends, it converts the comment into an EXAMPLE segment."
- (let ((owner (if headers (car headers)))
- (title (if (cdr headers) (mapconcat 'identity (cdr headers) " "))))
- (cond
- ((eq backend 'html)
- (concat "#+BEGIN_HTML\n"
- "<div class=\"org-comment\""
- (if owner (format " id=\"org-comment-%s\" " owner))
- ">\n"
- (if owner (concat "<b>" owner "</b> ") "")
- (if (and title (> (length title) 0)) (concat " -- " title "</br>\n") "</br>\n")
- "<p>\n"
- "#+END_HTML\n"
- body
- "\n#+BEGIN_HTML\n"
- "</p>\n"
- "</div>\n"
- "#+END_HTML\n"))
- (t
- (concat "#+BEGIN_EXAMPLE\n"
- (if title (concat "Title:" title "\n") "")
- (if owner (concat "By:" owner "\n") "")
- body
- (if (string-match "\n\\'" body) "" "\n")
- "#+END_EXAMPLE\n")))))
- (provide 'org-exp-blocks)
|