dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ox-org.el (13344B)


      1 ;;; ox-org.el --- Org Back-End for Org Export Engine -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2013-2023 Free Software Foundation, Inc.
      4 
      5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
      6 ;; Maintainer: Nicolas Goaziou <mail@nicolasgoaziou.fr>
      7 ;; Keywords: org, wp
      8 
      9 ;; This file is part of GNU Emacs.
     10 
     11 ;; GNU Emacs is free software: you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; GNU Emacs is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;;; Code:
     27 
     28 (require 'org-macs)
     29 (org-assert-version)
     30 
     31 (require 'ox)
     32 (declare-function htmlize-buffer "ext:htmlize" (&optional buffer))
     33 (defvar htmlize-output-type)
     34 
     35 (defgroup org-export-org nil
     36   "Options for exporting Org mode files to Org."
     37   :tag "Org Export Org"
     38   :group 'org-export
     39   :version "24.4"
     40   :package-version '(Org . "8.0"))
     41 
     42 (defcustom org-org-htmlized-css-url nil
     43   "URL pointing to the CSS defining colors for htmlized Emacs buffers.
     44 Normally when creating an htmlized version of an Org buffer,
     45 htmlize will create the CSS to define the font colors.  However,
     46 this does not work when converting in batch mode, and it also can
     47 look bad if different people with different fontification setup
     48 work on the same website.  When this variable is non-nil,
     49 creating an htmlized version of an Org buffer using
     50 `org-org-export-as-org' will include a link to this URL if the
     51 setting of `org-html-htmlize-output-type' is `css'."
     52   :group 'org-export-org
     53   :type '(choice
     54 	  (const :tag "Don't include external stylesheet link" nil)
     55 	  (string :tag "URL or local href")))
     56 
     57 (org-export-define-backend 'org
     58   '((babel-call . org-org-identity)
     59     (bold . org-org-identity)
     60     (center-block . org-org-identity)
     61     (clock . org-org-identity)
     62     (code . org-org-identity)
     63     (diary-sexp . org-org-identity)
     64     (drawer . org-org-identity)
     65     (dynamic-block . org-org-identity)
     66     (entity . org-org-identity)
     67     (example-block . org-org-identity)
     68     (export-block . org-org-export-block)
     69     (fixed-width . org-org-identity)
     70     (footnote-definition . ignore)
     71     (footnote-reference . org-org-identity)
     72     (headline . org-org-headline)
     73     (horizontal-rule . org-org-identity)
     74     (inline-babel-call . org-org-identity)
     75     (inline-src-block . org-org-identity)
     76     (inlinetask . org-org-identity)
     77     (italic . org-org-identity)
     78     (item . org-org-identity)
     79     (keyword . org-org-keyword)
     80     (latex-environment . org-org-identity)
     81     (latex-fragment . org-org-identity)
     82     (line-break . org-org-identity)
     83     (link . org-org-link)
     84     (node-property . org-org-identity)
     85     (template . org-org-template)
     86     (paragraph . org-org-identity)
     87     (plain-list . org-org-identity)
     88     (planning . org-org-identity)
     89     (property-drawer . org-org-identity)
     90     (quote-block . org-org-identity)
     91     (radio-target . org-org-identity)
     92     (section . org-org-section)
     93     (special-block . org-org-identity)
     94     (src-block . org-org-identity)
     95     (statistics-cookie . org-org-identity)
     96     (strike-through . org-org-identity)
     97     (subscript . org-org-identity)
     98     (superscript . org-org-identity)
     99     (table . org-org-identity)
    100     (table-cell . org-org-identity)
    101     (table-row . org-org-identity)
    102     (target . org-org-identity)
    103     (timestamp . org-org-timestamp)
    104     (underline . org-org-identity)
    105     (verbatim . org-org-identity)
    106     (verse-block . org-org-identity))
    107   :menu-entry
    108   '(?O "Export to Org"
    109        ((?O "As Org buffer" org-org-export-as-org)
    110 	(?o "As Org file" org-org-export-to-org)
    111 	(?v "As Org file and open"
    112 	    (lambda (a s v b)
    113 	      (if a (org-org-export-to-org t s v b)
    114 		(org-open-file (org-org-export-to-org nil s v b)))))))
    115   :filters-alist '((:filter-parse-tree . org-org--add-missing-sections)))
    116 
    117 (defun org-org--add-missing-sections (tree _backend _info)
    118   "Ensure each headline has an associated section.
    119 
    120 TREE is the parse tree being exported.
    121 
    122 Footnotes relative to the headline are inserted in the section,
    123 using `org-org-section'.  However, this function is not called if
    124 the headline doesn't contain any section in the first place, so
    125 we make sure it is always called."
    126   (org-element-map tree 'headline
    127     (lambda (h)
    128       (let ((first-child (car (org-element-contents h)))
    129 	    (new-section (org-element-create 'section)))
    130 	(pcase (org-element-type first-child)
    131 	  (`section nil)
    132 	  (`nil (org-element-adopt-elements h new-section))
    133 	  (_ (org-element-insert-before new-section first-child))))))
    134   tree)
    135 
    136 (defun org-org-export-block (export-block _contents _info)
    137   "Transcode a EXPORT-BLOCK element from Org to LaTeX.
    138 CONTENTS and INFO are ignored."
    139   (and (equal (org-element-property :type export-block) "ORG")
    140        (org-element-property :value export-block)))
    141 
    142 (defun org-org-identity (blob contents _info)
    143   "Transcode BLOB element or object back into Org syntax.
    144 CONTENTS is its contents, as a string or nil.  INFO is ignored."
    145   (let ((case-fold-search t))
    146     (replace-regexp-in-string
    147      "^[ \t]*#\\+attr_[-_a-z0-9]+:\\(?: .*\\)?\n" ""
    148      (org-export-expand blob contents t))))
    149 
    150 (defun org-org-headline (headline contents info)
    151   "Transcode HEADLINE element back into Org syntax.
    152 CONTENTS is its contents, as a string or nil.  INFO is ignored."
    153   (unless (org-element-property :footnote-section-p headline)
    154     (unless (plist-get info :with-todo-keywords)
    155       (org-element-put-property headline :todo-keyword nil))
    156     (unless (plist-get info :with-tags)
    157       (org-element-put-property headline :tags nil))
    158     (unless (plist-get info :with-priority)
    159       (org-element-put-property headline :priority nil))
    160     (org-element-put-property headline :level
    161 			      (org-export-get-relative-level headline info))
    162     (org-element-headline-interpreter headline contents)))
    163 
    164 (defun org-org-keyword (keyword _contents _info)
    165   "Transcode KEYWORD element back into Org syntax.
    166 CONTENTS is nil.  INFO is ignored."
    167   (let ((key (org-element-property :key keyword)))
    168     (unless (member key
    169 		    '("AUTHOR" "CREATOR" "DATE" "EMAIL" "OPTIONS" "TITLE"))
    170       (org-element-keyword-interpreter keyword nil))))
    171 
    172 (defun org-org-link (link contents info)
    173   "Transcode LINK object back into Org syntax.
    174 CONTENTS is the description of the link, as a string, or nil.
    175 INFO is a plist containing current export state."
    176   (or (org-export-custom-protocol-maybe link contents 'org info)
    177       (org-element-link-interpreter link contents)))
    178 
    179 (defun org-org-template (contents info)
    180   "Return Org document template with document keywords.
    181 CONTENTS is the transcoded contents string.  INFO is a plist used
    182 as a communication channel."
    183   (concat
    184    (and (plist-get info :time-stamp-file)
    185 	(format-time-string "# Created %Y-%m-%d %a %H:%M\n"))
    186    (org-element-normalize-string
    187     (mapconcat #'identity
    188 	       (org-element-map (plist-get info :parse-tree) 'keyword
    189 		 (lambda (k)
    190 		   (and (string-equal (org-element-property :key k) "OPTIONS")
    191 			(concat "#+options: "
    192 				(org-element-property :value k)))))
    193 	       "\n"))
    194    (and (plist-get info :with-title)
    195 	(format "#+title: %s\n" (org-export-data (plist-get info :title) info)))
    196    (and (plist-get info :with-date)
    197 	(let ((date (org-export-data (org-export-get-date info) info)))
    198 	  (and (org-string-nw-p date)
    199 	       (format "#+date: %s\n" date))))
    200    (and (plist-get info :with-author)
    201 	(let ((author (org-export-data (plist-get info :author) info)))
    202 	  (and (org-string-nw-p author)
    203 	       (format "#+author: %s\n" author))))
    204    (and (plist-get info :with-email)
    205 	(let ((email (org-export-data (plist-get info :email) info)))
    206 	  (and (org-string-nw-p email)
    207 	       (format "#+email: %s\n" email))))
    208    (and (plist-get info :with-creator)
    209 	(org-string-nw-p (plist-get info :creator))
    210 	(format "#+creator: %s\n" (plist-get info :creator)))
    211    contents))
    212 
    213 (defun org-org-timestamp (timestamp _contents _info)
    214   "Transcode a TIMESTAMP object to custom format or back into Org syntax."
    215   (org-timestamp-translate timestamp))
    216 
    217 (defun org-org-section (section contents info)
    218   "Transcode SECTION element back into Org syntax.
    219 CONTENTS is the contents of the section.  INFO is a plist used as
    220 a communication channel."
    221   (concat
    222    (org-element-normalize-string contents)
    223    ;; Insert footnote definitions appearing for the first time in this
    224    ;; section, or in the relative headline title.  Indeed, some of
    225    ;; them may not be available to narrowing so we make sure all of
    226    ;; them are included in the result.
    227    (let ((footnotes
    228 	  (org-element-map
    229 	      (list (org-export-get-parent-headline section) section)
    230 	      'footnote-reference
    231 	    (lambda (fn)
    232 	      (and (eq (org-element-property :type fn) 'standard)
    233 		   (org-export-footnote-first-reference-p fn info)
    234 		   (org-element-normalize-string
    235 		    (format "[fn:%s] %s"
    236 			    (org-element-property :label fn)
    237 			    (org-export-data
    238 			     (org-export-get-footnote-definition fn info)
    239 			     info)))))
    240 	    info nil 'headline t)))
    241      (and footnotes (concat "\n" (mapconcat #'identity footnotes "\n"))))))
    242 
    243 ;;;###autoload
    244 (defun org-org-export-as-org
    245     (&optional async subtreep visible-only body-only ext-plist)
    246   "Export current buffer to an Org buffer.
    247 
    248 If narrowing is active in the current buffer, only export its
    249 narrowed part.
    250 
    251 If a region is active, export that region.
    252 
    253 A non-nil optional argument ASYNC means the process should happen
    254 asynchronously.  The resulting buffer should be accessible
    255 through the `org-export-stack' interface.
    256 
    257 When optional argument SUBTREEP is non-nil, export the sub-tree
    258 at point, extracting information from the headline properties
    259 first.
    260 
    261 When optional argument VISIBLE-ONLY is non-nil, don't export
    262 contents of hidden elements.
    263 
    264 When optional argument BODY-ONLY is non-nil, strip document
    265 keywords from output.
    266 
    267 EXT-PLIST, when provided, is a property list with external
    268 parameters overriding Org default settings, but still inferior to
    269 file-local settings.
    270 
    271 Export is done in a buffer named \"*Org ORG Export*\", which will
    272 be displayed when `org-export-show-temporary-export-buffer' is
    273 non-nil."
    274   (interactive)
    275   (org-export-to-buffer 'org "*Org ORG Export*"
    276     async subtreep visible-only body-only ext-plist (lambda () (org-mode))))
    277 
    278 ;;;###autoload
    279 (defun org-org-export-to-org
    280     (&optional async subtreep visible-only body-only ext-plist)
    281   "Export current buffer to an Org file.
    282 
    283 If narrowing is active in the current buffer, only export its
    284 narrowed part.
    285 
    286 If a region is active, export that region.
    287 
    288 A non-nil optional argument ASYNC means the process should happen
    289 asynchronously.  The resulting file should be accessible through
    290 the `org-export-stack' interface.
    291 
    292 When optional argument SUBTREEP is non-nil, export the sub-tree
    293 at point, extracting information from the headline properties
    294 first.
    295 
    296 When optional argument VISIBLE-ONLY is non-nil, don't export
    297 contents of hidden elements.
    298 
    299 When optional argument BODY-ONLY is non-nil, strip document
    300 keywords from output.
    301 
    302 EXT-PLIST, when provided, is a property list with external
    303 parameters overriding Org default settings, but still inferior to
    304 file-local settings.
    305 
    306 Return output file name."
    307   (interactive)
    308   (let ((outfile (org-export-output-file-name ".org" subtreep)))
    309     (org-export-to-file 'org outfile
    310       async subtreep visible-only body-only ext-plist)))
    311 
    312 ;;;###autoload
    313 (defun org-org-publish-to-org (plist filename pub-dir)
    314   "Publish an Org file to Org.
    315 
    316 FILENAME is the filename of the Org file to be published.  PLIST
    317 is the property list for the given project.  PUB-DIR is the
    318 publishing directory.
    319 
    320 Return output file name."
    321   (org-publish-org-to 'org filename ".org" plist pub-dir)
    322   (when (plist-get plist :htmlized-source)
    323     (or (require 'htmlize nil t)
    324 	(error "Please install htmlize from https://github.com/hniksic/emacs-htmlize"))
    325     (require 'ox-html)
    326     (let* ((org-inhibit-startup t)
    327 	   (htmlize-output-type 'css)
    328 	   (html-ext (concat "." (or (plist-get plist :html-extension)
    329 				     org-html-extension "html")))
    330 	   (visitingp (find-buffer-visiting filename))
    331 	   (work-buffer (or visitingp (find-file-noselect filename)))
    332 	   newbuf)
    333       (with-current-buffer work-buffer
    334         (font-lock-ensure)
    335         (org-fold-show-all)
    336         (setq newbuf (htmlize-buffer)))
    337       (with-current-buffer newbuf
    338 	(when org-org-htmlized-css-url
    339 	  (goto-char (point-min))
    340 	  (and (re-search-forward
    341 		"<style type=\"text/css\">[^\000]*?\n[ \t]*</style>.*" nil t)
    342 	       (replace-match
    343 		(format
    344 		 "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\">"
    345 		 org-org-htmlized-css-url)
    346                 t t)))
    347 	(write-file (concat pub-dir (file-name-nondirectory filename) html-ext)))
    348       (kill-buffer newbuf)
    349       (unless visitingp (kill-buffer work-buffer)))
    350     ;; FIXME: Why?  Which buffer is this supposed to apply to?
    351     (set-buffer-modified-p nil)))
    352 
    353 
    354 (provide 'ox-org)
    355 
    356 ;; Local variables:
    357 ;; generated-autoload-file: "org-loaddefs.el"
    358 ;; End:
    359 
    360 ;;; ox-org.el ends here