unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 98e4682b4b52f52300ac0afd55bfd16b38d78dc0 13344 bytes (raw)
name: lisp/ox-org.el 	 # note: path name is non-authoritative(*)

  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
 
;;; ox-org.el --- Org Back-End for Org Export Engine -*- lexical-binding: t; -*-

;; Copyright (C) 2013-2023 Free Software Foundation, Inc.

;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
;; Maintainer: Nicolas Goaziou <mail@nicolasgoaziou.fr>
;; Keywords: org, wp

;; This file is part of GNU Emacs.

;; GNU Emacs 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 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'org-load)
(org-assert-version)

(require 'ox)
(declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
(defvar htmlize-output-type)

(defgroup org-export-org nil
  "Options for exporting Org mode files to Org."
  :tag "Org Export Org"
  :group 'org-export
  :version "24.4"
  :package-version '(Org . "8.0"))

(defcustom org-org-htmlized-css-url nil
  "URL pointing to the CSS defining colors for htmlized Emacs buffers.
Normally when creating an htmlized version of an Org buffer,
htmlize will create the CSS to define the font colors.  However,
this does not work when converting in batch mode, and it also can
look bad if different people with different fontification setup
work on the same website.  When this variable is non-nil,
creating an htmlized version of an Org buffer using
`org-org-export-as-org' will include a link to this URL if the
setting of `org-html-htmlize-output-type' is `css'."
  :group 'org-export-org
  :type '(choice
	  (const :tag "Don't include external stylesheet link" nil)
	  (string :tag "URL or local href")))

(org-export-define-backend 'org
  '((babel-call . org-org-identity)
    (bold . org-org-identity)
    (center-block . org-org-identity)
    (clock . org-org-identity)
    (code . org-org-identity)
    (diary-sexp . org-org-identity)
    (drawer . org-org-identity)
    (dynamic-block . org-org-identity)
    (entity . org-org-identity)
    (example-block . org-org-identity)
    (export-block . org-org-export-block)
    (fixed-width . org-org-identity)
    (footnote-definition . ignore)
    (footnote-reference . org-org-identity)
    (headline . org-org-headline)
    (horizontal-rule . org-org-identity)
    (inline-babel-call . org-org-identity)
    (inline-src-block . org-org-identity)
    (inlinetask . org-org-identity)
    (italic . org-org-identity)
    (item . org-org-identity)
    (keyword . org-org-keyword)
    (latex-environment . org-org-identity)
    (latex-fragment . org-org-identity)
    (line-break . org-org-identity)
    (link . org-org-link)
    (node-property . org-org-identity)
    (template . org-org-template)
    (paragraph . org-org-identity)
    (plain-list . org-org-identity)
    (planning . org-org-identity)
    (property-drawer . org-org-identity)
    (quote-block . org-org-identity)
    (radio-target . org-org-identity)
    (section . org-org-section)
    (special-block . org-org-identity)
    (src-block . org-org-identity)
    (statistics-cookie . org-org-identity)
    (strike-through . org-org-identity)
    (subscript . org-org-identity)
    (superscript . org-org-identity)
    (table . org-org-identity)
    (table-cell . org-org-identity)
    (table-row . org-org-identity)
    (target . org-org-identity)
    (timestamp . org-org-timestamp)
    (underline . org-org-identity)
    (verbatim . org-org-identity)
    (verse-block . org-org-identity))
  :menu-entry
  '(?O "Export to Org"
       ((?O "As Org buffer" org-org-export-as-org)
	(?o "As Org file" org-org-export-to-org)
	(?v "As Org file and open"
	    (lambda (a s v b)
	      (if a (org-org-export-to-org t s v b)
		(org-open-file (org-org-export-to-org nil s v b)))))))
  :filters-alist '((:filter-parse-tree . org-org--add-missing-sections)))

(defun org-org--add-missing-sections (tree _backend _info)
  "Ensure each headline has an associated section.

TREE is the parse tree being exported.

Footnotes relative to the headline are inserted in the section,
using `org-org-section'.  However, this function is not called if
the headline doesn't contain any section in the first place, so
we make sure it is always called."
  (org-element-map tree 'headline
    (lambda (h)
      (let ((first-child (car (org-element-contents h)))
	    (new-section (org-element-create 'section)))
	(pcase (org-element-type first-child)
	  (`section nil)
	  (`nil (org-element-adopt-elements h new-section))
	  (_ (org-element-insert-before new-section first-child))))))
  tree)

(defun org-org-export-block (export-block _contents _info)
  "Transcode a EXPORT-BLOCK element from Org to LaTeX.
CONTENTS and INFO are ignored."
  (and (equal (org-element-property :type export-block) "ORG")
       (org-element-property :value export-block)))

(defun org-org-identity (blob contents _info)
  "Transcode BLOB element or object back into Org syntax.
CONTENTS is its contents, as a string or nil.  INFO is ignored."
  (let ((case-fold-search t))
    (replace-regexp-in-string
     "^[ \t]*#\\+attr_[-_a-z0-9]+:\\(?: .*\\)?\n" ""
     (org-export-expand blob contents t))))

(defun org-org-headline (headline contents info)
  "Transcode HEADLINE element back into Org syntax.
CONTENTS is its contents, as a string or nil.  INFO is ignored."
  (unless (org-element-property :footnote-section-p headline)
    (unless (plist-get info :with-todo-keywords)
      (org-element-put-property headline :todo-keyword nil))
    (unless (plist-get info :with-tags)
      (org-element-put-property headline :tags nil))
    (unless (plist-get info :with-priority)
      (org-element-put-property headline :priority nil))
    (org-element-put-property headline :level
			      (org-export-get-relative-level headline info))
    (org-element-headline-interpreter headline contents)))

(defun org-org-keyword (keyword _contents _info)
  "Transcode KEYWORD element back into Org syntax.
CONTENTS is nil.  INFO is ignored."
  (let ((key (org-element-property :key keyword)))
    (unless (member key
		    '("AUTHOR" "CREATOR" "DATE" "EMAIL" "OPTIONS" "TITLE"))
      (org-element-keyword-interpreter keyword nil))))

(defun org-org-link (link contents info)
  "Transcode LINK object back into Org syntax.
CONTENTS is the description of the link, as a string, or nil.
INFO is a plist containing current export state."
  (or (org-export-custom-protocol-maybe link contents 'org info)
      (org-element-link-interpreter link contents)))

(defun org-org-template (contents info)
  "Return Org document template with document keywords.
CONTENTS is the transcoded contents string.  INFO is a plist used
as a communication channel."
  (concat
   (and (plist-get info :time-stamp-file)
	(format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
   (org-element-normalize-string
    (mapconcat #'identity
	       (org-element-map (plist-get info :parse-tree) 'keyword
		 (lambda (k)
		   (and (string-equal (org-element-property :key k) "OPTIONS")
			(concat "#+options: "
				(org-element-property :value k)))))
	       "\n"))
   (and (plist-get info :with-title)
	(format "#+title: %s\n" (org-export-data (plist-get info :title) info)))
   (and (plist-get info :with-date)
	(let ((date (org-export-data (org-export-get-date info) info)))
	  (and (org-string-nw-p date)
	       (format "#+date: %s\n" date))))
   (and (plist-get info :with-author)
	(let ((author (org-export-data (plist-get info :author) info)))
	  (and (org-string-nw-p author)
	       (format "#+author: %s\n" author))))
   (and (plist-get info :with-email)
	(let ((email (org-export-data (plist-get info :email) info)))
	  (and (org-string-nw-p email)
	       (format "#+email: %s\n" email))))
   (and (plist-get info :with-creator)
	(org-string-nw-p (plist-get info :creator))
	(format "#+creator: %s\n" (plist-get info :creator)))
   contents))

(defun org-org-timestamp (timestamp _contents _info)
  "Transcode a TIMESTAMP object to custom format or back into Org syntax."
  (org-timestamp-translate timestamp))

(defun org-org-section (section contents info)
  "Transcode SECTION element back into Org syntax.
CONTENTS is the contents of the section.  INFO is a plist used as
a communication channel."
  (concat
   (org-element-normalize-string contents)
   ;; Insert footnote definitions appearing for the first time in this
   ;; section, or in the relative headline title.  Indeed, some of
   ;; them may not be available to narrowing so we make sure all of
   ;; them are included in the result.
   (let ((footnotes
	  (org-element-map
	      (list (org-export-get-parent-headline section) section)
	      'footnote-reference
	    (lambda (fn)
	      (and (eq (org-element-property :type fn) 'standard)
		   (org-export-footnote-first-reference-p fn info)
		   (org-element-normalize-string
		    (format "[fn:%s] %s"
			    (org-element-property :label fn)
			    (org-export-data
			     (org-export-get-footnote-definition fn info)
			     info)))))
	    info nil 'headline t)))
     (and footnotes (concat "\n" (mapconcat #'identity footnotes "\n"))))))

;;;###autoload
(defun org-org-export-as-org
    (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to an Org buffer.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting buffer should be accessible
through the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, strip document
keywords from output.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Export is done in a buffer named \"*Org ORG Export*\", which will
be displayed when `org-export-show-temporary-export-buffer' is
non-nil."
  (interactive)
  (org-export-to-buffer 'org "*Org ORG Export*"
    async subtreep visible-only body-only ext-plist (lambda () (org-mode))))

;;;###autoload
(defun org-org-export-to-org
    (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer to an Org file.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, strip document
keywords from output.

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Return output file name."
  (interactive)
  (let ((outfile (org-export-output-file-name ".org" subtreep)))
    (org-export-to-file 'org outfile
      async subtreep visible-only body-only ext-plist)))

;;;###autoload
(defun org-org-publish-to-org (plist filename pub-dir)
  "Publish an Org file to Org.

FILENAME is the filename of the Org file to be published.  PLIST
is the property list for the given project.  PUB-DIR is the
publishing directory.

Return output file name."
  (org-publish-org-to 'org filename ".org" plist pub-dir)
  (when (plist-get plist :htmlized-source)
    (or (require 'htmlize nil t)
	(error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
    (require 'ox-html)
    (let* ((org-inhibit-startup t)
	   (htmlize-output-type 'css)
	   (html-ext (concat "." (or (plist-get plist :html-extension)
				     org-html-extension "html")))
	   (visitingp (find-buffer-visiting filename))
	   (work-buffer (or visitingp (find-file-noselect filename)))
	   newbuf)
      (with-current-buffer work-buffer
        (font-lock-ensure)
        (org-fold-show-all)
        (setq newbuf (htmlize-buffer)))
      (with-current-buffer newbuf
	(when org-org-htmlized-css-url
	  (goto-char (point-min))
	  (and (re-search-forward
		"<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
	       (replace-match
		(format
		 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
		 org-org-htmlized-css-url)
                t t)))
	(write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
      (kill-buffer newbuf)
      (unless visitingp (kill-buffer work-buffer)))
    ;; FIXME: Why?  Which buffer is this supposed to apply to?
    (set-buffer-modified-p nil)))


(provide 'ox-org)

;; Local variables:
;; generated-autoload-file: "org-loaddefs.el"
;; End:

;;; ox-org.el ends here

debug log:

solving 98e4682b4 ...
found 98e4682b4 in https://yhetil.org/emacs-bugs/871qi2mlnc.fsf@localhost/
found ed72cf4f2 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 ed72cf4f2ba4952fd77a10105f3475c63bc9e6fd	lisp/ox-org.el

applying [1/1] https://yhetil.org/emacs-bugs/871qi2mlnc.fsf@localhost/
diff --git a/lisp/ox-org.el b/lisp/ox-org.el
index ed72cf4f2..98e4682b4 100644

Checking patch lisp/ox-org.el...
Applied patch lisp/ox-org.el cleanly.

index at:
100644 98e4682b4b52f52300ac0afd55bfd16b38d78dc0	lisp/ox-org.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).