Browse Source

Fix the export of drawers

Exporting drawers was not really supported, now it is.
Drawers that are selected for export are formatted using the function
specified in the variable `org-export-format-drawer-function'.  If
that is nil, the default `org-export-format-drawer' will publish the
drawer as a colon example, i.e. verbatim and in fixed-width.
Carsten Dominik 15 years ago
parent
commit
49690e49ca
2 changed files with 61 additions and 27 deletions
  1. 6 0
      lisp/ChangeLog
  2. 55 27
      lisp/org-exp.el

+ 6 - 0
lisp/ChangeLog

@@ -1,5 +1,11 @@
 2009-11-11  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-11-11  Carsten Dominik  <carsten.dominik@gmail.com>
 
 
+	* org-exp.el (org-export-format-drawer-function): New variable.
+	(org-export-format-drawer): New function.
+	(org-export-preprocess-string): Pass the backend as a parameter to
+	`org-export-remove-or-extract-drawers'.
+	(org-export-remove-or-extract-drawers): New parameter BACKEND.
+
 	* org-protocol.el (org-protocol-char-to-string): New defsubst.
 	* org-protocol.el (org-protocol-char-to-string): New defsubst.
 
 
 2009-11-10  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-11-10  Carsten Dominik  <carsten.dominik@gmail.com>

+ 55 - 27
lisp/org-exp.el

@@ -1310,8 +1310,8 @@ on this string to produce the exported version."
       (setq target-alist (org-export-define-heading-targets target-alist))
       (setq target-alist (org-export-define-heading-targets target-alist))
 
 
       ;; Get rid of drawers
       ;; Get rid of drawers
-      (org-export-remove-or-extract-drawers drawers
+      (org-export-remove-or-extract-drawers
-					    (plist-get parameters :drawers))
+       drawers (plist-get parameters :drawers) backend)
 
 
       ;; Get the correct stuff before the first headline
       ;; Get the correct stuff before the first headline
       (when (plist-get parameters :skip-before-1st-heading)
       (when (plist-get parameters :skip-before-1st-heading)
@@ -1535,33 +1535,61 @@ the current file."
 	 (unless desc (insert "][" link))
 	 (unless desc (insert "][" link))
 	 (add-text-properties pos (point) props))))))
 	 (add-text-properties pos (point) props))))))
 
 
-(defun org-export-remove-or-extract-drawers (all-drawers exp-drawers)
+(defun org-export-remove-or-extract-drawers (all-drawers exp-drawers backend)
-  "Remove drawers, or extract the content.
+  "Remove drawers, or extract and format the content.
 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
 ALL-DRAWERS is a list of all drawer names valid in the current buffer.
 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
 EXP-DRAWERS can be t to keep all drawer contents, or a list of drawers
-whose content to keep."
+whose content to keep.  Any drawers that are in ALL-DRAWERS but not in
-  (unless (eq t exp-drawers)
+EXP-DRAWERS will be removed.
-    (goto-char (point-min))
+BACKEND is the current export backend."
-    (let ((re (concat "^[ \t]*:\\("
+  (goto-char (point-min))
-		      (mapconcat
+  (let ((re (concat "^[ \t]*:\\("
-		       'identity
+		    (mapconcat 'identity all-drawers "\\|")
-		       (org-delete-all exp-drawers
+		    "\\):[ \t]*$"))
-				       (copy-sequence all-drawers))
+	name beg beg-content eol)
-		       "\\|")
+    (while (re-search-forward re nil t)
-		      "\\):[ \t]*$"))
+      (org-if-unprotected
-	  beg eol)
+       (setq name (match-string 1))
-      (while (re-search-forward re nil t)
+       (setq beg (match-beginning 0)
-	(org-if-unprotected
+	     beg-content (1+ (point-at-eol))
-	 (setq beg (match-beginning 0)
+	     eol (point-at-eol))
-	       eol (match-end 0))
+       (if (not (and (re-search-forward
-	 (if (re-search-forward "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]"
+		      "^\\([ \t]*:END:[ \t]*\n?\\)\\|^\\*+[ \t]" nil t)
-				nil t)
+		     (match-end 1)))
-	     (if (match-end 1)
+	   (goto-char eol)
-		 ;; terminated in this entry
+	 (goto-char (match-beginning 0))
-		 (progn
+	 (and (looking-at ".*\n?") (replace-match ""))
-		   (delete-region beg (match-end 1))
+	 (setq content (buffer-substring beg-content (point)))
-		   (goto-char beg))
+	 (delete-region beg (point))
-	       (goto-char eol))))))))
+	 (when (or (eq exp-drawers t)
+		   (member name exp-drawers))
+	   (setq content (funcall (or org-export-format-drawer-function
+				      'org-export-format-drawer)
+				  name content backend))
+	   (insert content)))))))
+
+(defvar org-export-format-drawer-function nil
+  "Function to be called to format the contents of a drawer.
+The function must accept three parameters:
+  BACKEND  one of the symbols html, docbook, latex, ascii, xoxo
+  NAME     the drawer name, like \"PROPERIES\"
+  CONTENT  the content of the drawer.
+The function should return the text to be inserted into the buffer.
+If this is nil, `org-export-format-drawer' is used as a default.")
+
+(defun org-export-format-drawer (name content backend)
+  "Format the content of a drawer as a colon example."
+  (if (string-match "[ \t]+\\'" content)
+      (setq content (substring content (match-beginning 0))))
+  (while (string-match "\\`[ \t]*\n" content)
+    (steq content (substring content (match-end 0))))
+  (setq content (org-remove-indentation content))
+  (setq content (concat ": " (mapconcat 'identity
+					(org-split-string content "\n")
+					"\n: ")
+			"\n"))
+  (setq content (concat " : " (upcase name) "\n" content))
+  (org-add-props content nil 'org-protected t))
 
 
 (defun org-export-handle-export-tags (select-tags exclude-tags)
 (defun org-export-handle-export-tags (select-tags exclude-tags)
   "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.
   "Modify the buffer, honoring SELECT-TAGS and EXCLUDE-TAGS.