初稿: 2018-08-09 Thu 09:28
最終更新日: 2018-12-14 Fri 20:44
ホーム | 文書トップ | 目次

org-export-as
org-exportにおけるorg記法のパース

  1: (defun org-export-as
  2:     (backend &optional subtreep visible-only body-only ext-plist)
  3:   "Transcode current Org buffer into BACKEND code.
  4: 
  5: BACKEND is either an export back-end, as returned by, e.g.,
  6: `org-export-create-backend', or a symbol referring to
  7: a registered back-end.
  8: 
  9: If narrowing is active in the current buffer, only transcode its
 10: narrowed part.
 11: 
 12: If a region is active, transcode that region.
 13: 
 14: When optional argument SUBTREEP is non-nil, transcode the
 15: sub-tree at point, extracting information from the headline
 16: properties first.
 17: 
 18: When optional argument VISIBLE-ONLY is non-nil, don't export
 19: contents of hidden elements.
 20: 
 21: When optional argument BODY-ONLY is non-nil, only return body
 22: code, without surrounding template.
 23: 
 24: Optional argument EXT-PLIST, when provided, is a property list
 25: with external parameters overriding Org default settings, but
 26: still inferior to file-local settings.
 27: 
 28: Return code as a string."
 29:   (when (symbolp backend) (setq backend (org-export-get-backend backend)))
 30:   (org-export-barf-if-invalid-backend backend)
 31:   (save-excursion
 32:     (save-restriction
 33:       ;; Narrow buffer to an appropriate region or subtree for
 34:       ;; parsing.  If parsing subtree, be sure to remove main
 35:       ;; headline, planning data and property drawer.
 36:       (cond ((org-region-active-p)
 37:              (narrow-to-region (region-beginning) (region-end)))
 38:             (subtreep
 39:              (org-narrow-to-subtree)
 40:              (goto-char (point-min))
 41:              (org-end-of-meta-data)
 42:              (narrow-to-region (point) (point-max))))
 43:       ;; Initialize communication channel with original buffer
 44:       ;; attributes, unavailable in its copy.
 45:       (let* ((org-export-current-backend (org-export-backend-name backend))
 46:              (info (org-combine-plists
 47:                     (org-export--get-export-attributes
 48:                      backend subtreep visible-only body-only)
 49:                     (org-export--get-buffer-attributes)))
 50:              (parsed-keywords
 51:               (delq nil
 52:                     (mapcar (lambda (o) (and (eq (nth 4 o) 'parse) (nth 1 o)))
 53:                             (append (org-export-get-all-options backend)
 54:                                     org-export-options-alist))))
 55:              tree)
 56:         ;; Update communication channel and get parse tree.  Buffer
 57:         ;; isn't parsed directly.  Instead, all buffer modifications
 58:         ;; and consequent parsing are undertaken in a temporary copy.
 59:         (org-export-with-buffer-copy
 60:          ;; Run first hook with current back-end's name as argument.
 61:          (run-hook-with-args 'org-export-before-processing-hook
 62:                              (org-export-backend-name backend))
 63:          ;; Include files, delete comments and expand macros.
 64:          (org-export-expand-include-keyword)
 65:          (org-export--delete-comment-trees)
 66:          (org-macro-initialize-templates)
 67:          (org-macro-replace-all
 68:           (append org-macro-templates org-export-global-macros)
 69:           nil parsed-keywords)
 70:          ;; Refresh buffer properties and radio targets after
 71:          ;; potentially invasive previous changes.  Likewise, do it
 72:          ;; again after executing Babel code.
 73:          (org-set-regexps-and-options)
 74:          (org-update-radio-target-regexp)
 75:          (when org-export-use-babel
 76:            (org-babel-exp-process-buffer)
 77:            (org-set-regexps-and-options)
 78:            (org-update-radio-target-regexp))
 79:          ;; Run last hook with current back-end's name as argument.
 80:          ;; Update buffer properties and radio targets one last time
 81:          ;; before parsing.
 82:          (goto-char (point-min))
 83:          (save-excursion
 84:            (run-hook-with-args 'org-export-before-parsing-hook
 85:                                (org-export-backend-name backend)))
 86:          (org-set-regexps-and-options)
 87:          (org-update-radio-target-regexp)
 88:          ;; Update communication channel with environment.
 89:          (setq info
 90:                (org-combine-plists
 91:                 info (org-export-get-environment backend subtreep ext-plist)))
 92:          ;; De-activate uninterpreted data from parsed keywords.
 93:          (dolist (entry (append (org-export-get-all-options backend)
 94:                                 org-export-options-alist))
 95:            (pcase entry
 96:              (`(,p ,_ ,_ ,_ parse)
 97:               (let ((value (plist-get info p)))
 98:                 (plist-put info
 99:                            p
100:                            (org-export--remove-uninterpreted-data value info))))
101:              (_ nil)))
102:          ;; Install user's and developer's filters.
103:          (setq info (org-export-install-filters info))
104:          ;; Call options filters and update export options.  We do not
105:          ;; use `org-export-filter-apply-functions' here since the
106:          ;; arity of such filters is different.
107:          (let ((backend-name (org-export-backend-name backend)))
108:            (dolist (filter (plist-get info :filter-options))
109:              (let ((result (funcall filter info backend-name)))
110:                (when result (setq info result)))))
111:          ;; Expand export-specific set of macros: {{{author}}},
112:          ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}.  It must
113:          ;; be done once regular macros have been expanded, since
114:          ;; parsed keywords may contain one of them.
115:          (org-macro-replace-all
116:           (list
117:            (cons "author" (org-element-interpret-data (plist-get info :author)))
118:            (cons "date"
119:                  (let* ((date (plist-get info :date))
120:                         (value (or (org-element-interpret-data date) "")))
121:                    (if (and (consp date)
122:                             (not (cdr date))
123:                             (eq (org-element-type (car date)) 'timestamp))
124:                        (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
125:                                (format "(org-timestamp-format '%S \"$1\")"
126:                                        (org-element-copy (car date)))
127:                                value)
128:                      value)))
129:            (cons "email" (org-element-interpret-data (plist-get info :email)))
130:            (cons "title" (org-element-interpret-data (plist-get info :title)))
131:            (cons "results" "$1"))
132:           'finalize
133:           parsed-keywords)
134:          ;; Parse buffer.
135:          (setq tree (org-element-parse-buffer nil visible-only))
136:          ;; Prune tree from non-exported elements and transform
137:          ;; uninterpreted elements or objects in both parse tree and
138:          ;; communication channel.
139:          (org-export--prune-tree tree info)
140:          (org-export--remove-uninterpreted-data tree info)
141:          ;; Call parse tree filters.
142:          (setq tree
143:                (org-export-filter-apply-functions
144:                 (plist-get info :filter-parse-tree) tree info))
145:          ;; Now tree is complete, compute its properties and add them
146:          ;; to communication channel.
147:          (setq info (org-export--collect-tree-properties tree info))
148:          ;; Eventually transcode TREE.  Wrap the resulting string into
149:          ;; a template.
150:          (let* ((body (org-element-normalize-string
151:                        (or (org-export-data tree info) "")))
152:                 (inner-template (cdr (assq 'inner-template
153:                                            (plist-get info :translate-alist))))
154:                 (full-body (org-export-filter-apply-functions
155:                             (plist-get info :filter-body)
156:                             (if (not (functionp inner-template)) body
157:                               (funcall inner-template body info))
158:                             info))
159:                 (template (cdr (assq 'template
160:                                      (plist-get info :translate-alist)))))
161:            ;; Remove all text properties since they cannot be
162:            ;; retrieved from an external process.  Finally call
163:            ;; final-output filter and return result.
164:            (org-no-properties
165:             (org-export-filter-apply-functions
166:              (plist-get info :filter-final-output)
167:              (if (or (not (functionp template)) body-only) full-body
168:                (funcall template full-body info))
169:              info))))))))
sf

日付: 2018-08-09 Thu 09:28

著者: conao

Created: 2018-12-14 Fri 21:04

Validate