1 ;; org-export-generic.el --- Export frameworg with custom backends
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Wes Hardaker <hardaker at users dot sourceforge dot net>
6 ;; Keywords: outlines, hypermedia, calendar, wp, export
7 ;; Homepage: http://orgmode.org
9 ;; Acks: Much of this code was stolen form the ascii export from Carsten
11 ;; This file is not part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; ----------------------------------------------------------------------
30 ;; IMPORTANT: IF YOU WANT TO WRITE A NEW EXPORTER FOR ORG, PLEASE
31 ;; CHECK contrib/lisp/org-export.el -- ORG-EXPORT-GENERIC.EL, WHILE
32 ;; STILL USEFUL, SHOULD NOT BE USED FOR NEW EXPORTERS.
34 ;; org-export-generic is basically a simple translation system that
35 ;; knows how to parse at least most of a .org buffer and then add
36 ;; various formatting prefixes before and after each section type. It
37 ;; does this by examining a property list stored in org-generic-alist.
38 ;; You can dynamically add propety lists of your own using the
39 ;; org-set-generic-type function:
41 ;; (org-set-generic-type
42 ;; "really-basic-text"
43 ;; '(:file-suffix ".txt"
46 ;; :title-format "=== %s ===\n"
47 ;; :body-header-section-numbers t
48 ;; :body-header-section-number-format "%s) "
49 ;; :body-section-header-prefix "\n"
50 ;; :body-section-header-suffix "\n"
51 ;; :body-line-format " %s\n"
55 ;; Note: Upper case key-bindings are reserved for your use. Lower
56 ;; case key bindings may conflict with future export-generic
59 ;; Then run org-export (ctrl-c ctrl-e) and select generic or run
60 ;; org-export-generic. You'll then be prompted with a list of export
61 ;; types to choose from which will include your new type assigned to
64 ;; ----------------------------------------------------------------------
67 ;; * handle function references
68 ;; * handle other types of multi-complex-listy-things to do
69 ;; ideas: (t ?- "%s" ?-)
70 ;; * handle indent specifiers better
72 ;; * need flag to remove indents from body text
74 ;; * handle internationalization strings better
75 ;; * date/author/etc needs improvment (internationalization too)
76 ;; * allow specifying of section ordering
77 ;; ideas: :ordering ("header" "toc" "body" "footer")
78 ;; ^ matches current hard coded ordering
79 ;; * err, actually *do* a footer
80 ;; * deal with usage of org globals
81 ;; *** should we even consider them, or let the per-section specifiers do it
82 ;; *** answer: remove; mostly removed now
83 ;; * deal with interactive support for picking a export specifier label
84 ;; * char specifiers that need extra length because of formatting
85 ;; idea: (?- 4) for 4-longer
86 ;; * centering specifier
87 ;; idea: ('center " -- %s -- ")
88 ;; * remove more of the unneeded export-to-ascii copy code
90 ;; *** supported now, but need separate format per tag
91 ;; *** allow different open/closing prefixes
94 ;; * Escape camel-case for wiki exporters.
95 ;; * Adjust to depth limits on headers --- need to roll-over from headers
96 ;; to lists, as per other exporters
97 ;; * optmization (many plist extracts should be in let vars)
98 ;; * define defcustom spec for the specifier list
99 ;; * fonts: at least monospace is not handled at all here.
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107 (eval-when-compile (require 'cl
))
109 (defgroup org-export-generic nil
110 "Options specific for ASCII export of Org-mode files."
111 :tag
"Org Export ASCII"
114 (defcustom org-export-generic-links-to-notes t
115 "Non-nil means convert links to notes before the next headline.
116 When nil, the link will be exported in place. If the line becomes long
117 in this way, it will be wrapped."
118 :group
'org-export-generic
122 (defvar org-generic-current-indentation nil
) ; For communication
124 (defvar org-generic-alist
127 ;; generic DEMO exporter
129 ;; (this tries to use every specifier for demo purposes)
135 :header-prefix
"<header>\n"
136 :header-suffix
"</header>\n"
145 :title-format
"<h1>%s</h1>\n"
149 :date-prefix
"<date>"
150 :date-format
"<br /><b>Date:</b> <i>%s</i><br />"
151 :date-suffix
"</date>\n\n"
154 :toc-header-prefix
"<tocname>\n"
155 :toc-header-format
"__%s__\n"
156 :toc-header-suffix
"</tocname>\n"
158 :toc-prefix
"<toc>\n"
159 :toc-suffix
"</toc>\n"
161 :toc-section-numbers t
162 :toc-section-number-format
"\#(%s) "
164 :toc-format-with-todo
"!!%s!!\n"
169 :toc-tags-prefix
" <tags>"
170 :toc-tags-format
"*%s*"
171 :toc-tags-suffix
"</tags>\n"
172 :toc-tags-none-string
"\n"
174 :body-header-section-numbers
3 ; t = all, nil = none
176 ; lists indicate different things per level
177 ; list contents or straight value can either be a
178 ; ?x char reference for printing strings that match the header len
179 ; "" string to print directly
180 :body-section-header-prefix
("<h1>" "<h2>" "<h3>"
181 "<h4>" "<h5>" "<h6>")
182 :body-section-header-format
"%s"
183 :body-section-header-suffix
("</h1>\n" "</h2>\n" "</h3>\n"
184 "</h4>\n" "</h5>\n" "</h6>\n")
188 :todo-keywords-export t
191 :body-tags-prefix
" <tags>"
192 :body-tags-suffix
"</tags>\n"
194 ; section prefixes/suffixes can be direct strings or lists as well
195 :body-section-prefix
"<secprefix>\n"
196 :body-section-suffix
"</secsuffix>\n"
197 ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
198 ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
201 ; if preformated text should be included (eg, : prefixed)
202 :body-line-export-preformated t
203 :body-line-fixed-prefix
"<pre>\n"
204 :body-line-fixed-suffix
"\n</pre>\n"
205 :body-line-fixed-format
"%s\n"
208 :body-list-prefix
"<list>\n"
209 :body-list-suffix
"</list>\n"
210 :body-list-format
"<li>%s</li>\n"
212 :body-number-list-prefix
"<ol>\n"
213 :body-number-list-suffix
"</ol>\n"
214 :body-number-list-format
"<li>%s</li>\n"
215 :body-number-list-leave-number t
217 :body-list-checkbox-todo
"<checkbox type=\"todo\">"
218 :body-list-checkbox-todo-end
"</checkbox (todo)>"
219 :body-list-checkbox-done
"<checkbox type=\"done\">"
220 :body-list-checkbox-done-end
"</checkbox (done)>"
221 :body-list-checkbox-half
"<checkbox type=\"half\">"
222 :body-list-checkbox-half-end
"</checkbox (half)>"
228 :body-line-format
"%s"
229 :body-line-wrap
60 ; wrap at 60 chars
231 ; print above and below all body parts
232 :body-text-prefix
"<p>\n"
233 :body-text-suffix
"</p>\n")
237 ;; (close to the original ascii specifier)
252 :date-format
"Date: %s\n"
255 :toc-header-prefix
""
256 :toc-header-format
"%s\n"
257 :toc-header-suffix ?
=
260 :toc-section-numbers t
261 :toc-section-number-format
"%s "
263 :toc-format-with-todo
"%s (*)\n"
267 :body-header-section-numbers
3
268 :body-section-prefix
"\n"
270 ; :body-section-header-prefix "\n"
271 ; :body-section-header-format "%s\n"
272 ; :body-section-header-suffix (?\$ ?\# ?^ ?\~ ?\= ?\-)
274 :body-section-header-prefix
("" "" "" "* " " + " " - ")
275 :body-section-header-format
"%s\n"
276 :body-section-header-suffix
(?~ ?
= ?-
"\n" "\n" "\n")
278 ; :body-section-marker-prefix ""
279 ; :body-section-marker-chars (?\$ ?\# ?^ ?\~ ?\= ?\-)
280 ; :body-section-marker-suffix "\n"
282 :body-line-export-preformated t
283 :body-line-format
"%s\n"
286 ; :body-text-prefix "<t>\n"
287 ; :body-text-suffix "</t>\n"
290 :body-bullet-list-prefix
(?
* ?
+ ?-
))
291 ; :body-bullet-list-suffix (?* ?+ ?-)
303 :title-format
"= %s =\n"
309 :body-header-section-numbers nil
310 :body-section-prefix
"\n"
312 :body-section-header-prefix
("= " "== " "=== "
313 "==== " "===== " "====== ")
314 :body-section-header-suffix
(" =\n\n" " ==\n\n" " ===\n\n"
315 " ====\n\n" " =====\n\n" " ======\n\n")
317 :body-line-export-preformated t
;; yes/no/maybe???
318 :body-line-format
"%s\n"
321 :body-line-fixed-format
" %s\n"
323 :body-list-format
"* %s\n"
324 :body-number-list-format
"# %s\n"
326 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** "))
337 :title-format
"= %s =\n"
343 :body-header-section-numbers nil
344 :body-section-prefix
"\n"
346 :body-section-header-prefix
("= " "== " "=== "
347 "==== " "===== " "====== ")
348 :body-section-header-suffix
(" =\n\n" " ==\n\n" " ===\n\n"
349 " ====\n\n" " =====\n\n" " ======\n\n")
351 :body-line-export-preformated t
;; yes/no/maybe???
352 :body-line-format
"%s\n"
355 :body-line-fixed-format
" %s\n"
357 :body-list-format
"* %s\n"
358 :body-number-list-format
"# %s\n"
360 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** ")
361 :body-list-checkbox-todo
"☐ "
362 :body-list-checkbox-done
"☒ "
363 :body-table-start
"{|"
365 :body-table-cell-start
"|"
366 :body-table-cell-end
"\n"
367 :body-table-last-cell-end
"|-"
368 :body-table-hline-start
"")
370 ;; internet-draft .xml for xml2rfc exporter
373 ;; this tries to use every specifier for demo purposes
377 :title-prefix
"<?xml version=\"1.0\"\?>
378 <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
379 <!ENTITY rfcs PUBLIC '' 'blah'>
380 <?rfc strict=\"yes\" ?>
382 <?rfc tocdepth=\"4\" ?>
383 <?rfc symrefs=\"yes\" ?>
384 <?rfc compact=\"yes\" ?>
385 <?rfc subcompact=\"no\" ?>
386 <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
389 :title-format
"<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
390 :title-suffix
"<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
391 <organization>Comany, Inc..</organization>
404 <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
405 <area>Operations and Management</area>
406 <workgroup>FIXME</workgroup>
412 :body-header-section-numbers nil
414 :body-section-header-format
"<section title=\"%s\">\n"
415 :body-section-suffix
"</section>\n"
417 ; if preformated text should be included (eg, : prefixed)
418 :body-line-export-preformated t
419 :body-line-fixed-prefix
"<figure>\n<artwork>\n"
420 :body-line-fixed-suffix
"\n</artwork>\n</figure>\n"
423 :body-line-format
"%s"
426 ; print above and below all body parts
427 :body-text-prefix
"<t>\n"
428 :body-text-suffix
"</t>\n"
430 :body-list-prefix
"<list style=\"symbols\">\n"
431 :body-list-suffix
"</list>\n"
432 :body-list-format
"<t>%s</t>\n")
437 ;; lifted from wikipedia exporter
441 :title-format
"= %s =\n"
447 :body-header-section-numbers nil
448 :body-section-prefix
"\n"
450 :body-section-header-prefix
(" == " " === " " ==== "
452 :body-section-header-suffix
(" ==\n\n" " ===\n\n" " ====\n\n"
453 " =====\n\n" " ======\n\n" " =======\n\n")
455 :body-line-export-preformated t
;; yes/no/maybe???
456 :body-line-format
"%s\n"
459 :body-line-fixed-format
" %s\n"
461 :body-list-format
" * %s\n"
462 :body-number-list-format
" # %s\n"
463 ;; :body-list-prefix "LISTSTART"
464 ;; :body-list-suffix "LISTEND"
466 ;; this is ignored! [2010/02/02:rpg]
467 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** "))
472 ;; lifted from wikipedia exporter
476 :title-format
"-= %s =-\n"
482 :body-header-section-numbers nil
483 :body-section-prefix
"\n"
485 :body-section-header-prefix
("! " "!! " "!!! " "!!!! "
486 "!!!!! " "!!!!!! " "!!!!!!! ")
487 :body-section-header-suffix
(" \n" " \n" " \n"
491 :body-line-export-preformated t
;; yes/no/maybe???
492 :body-line-format
"%s "
495 :body-line-fixed-format
" %s\n"
497 :body-list-format
"* %s\n"
498 :body-number-list-format
"# %s\n"
499 ;; :body-list-prefix "LISTSTART"
500 ;; :body-list-suffix "LISTEND"
501 :blockquote-start
"\n^\n"
502 :blockquote-end
"^\n\n"
503 :body-newline-paragraph
"\n"
504 :bold-format
"__%s__"
505 :italic-format
"''%s''"
506 :underline-format
"===%s==="
507 :strikethrough-format
"--%s--"
508 :code-format
"-+%s+-"
509 :verbatim-format
"~pp~%s~/pp~"))
510 "A assoc list of property lists to specify export definitions")
512 (setq org-generic-export-type
"demo")
514 (defvar org-export-generic-section-type
"")
515 (defvar org-export-generic-section-suffix
"")
517 (defun org-set-generic-type (type definition
)
518 "Adds a TYPE and DEFINITION to the existing list of defined generic
520 (aput 'org-generic-alist type definition
))
522 ;;; helper functions for org-set-generic-type
523 (defvar org-export-generic-keywords nil
)
524 (defmacro* def-org-export-generic-keyword
(keyword
527 "Define KEYWORD as a legitimate element for inclusion in
528 the body of an org-set-generic-type definition."
529 ;; TODO: push the documentation and type information
530 ;; somewhere where it will do us some good.
532 (pushnew ,keyword org-export-generic-keywords
)))
534 (def-org-export-generic-keyword :body-newline-paragraph
535 :documentation
"Bound either to NIL or to a pattern to be
536 inserted in the output for every blank line in the input.
537 The intention is to handle formats where text is flowed, and
538 newlines are interpreted as significant \(e.g., as indicating
539 preformatted text\). A common non-nil value for this keyword
540 is \"\\n\". Should typically be combined with a value for
541 :body-line-format that does NOT end with a newline."
544 ;;; fontification keywords
545 (def-org-export-generic-keyword :bold-format
)
546 (def-org-export-generic-keyword :italic-format
)
547 (def-org-export-generic-keyword :underline-format
)
548 (def-org-export-generic-keyword :strikethrough-format
)
549 (def-org-export-generic-keyword :code-format
)
550 (def-org-export-generic-keyword :verbatim-format
)
552 (defun org-export-generic-remember-section (type suffix
&optional prefix
)
553 (setq org-export-generic-section-type type
)
554 (setq org-export-generic-section-suffix suffix
)
558 (defun org-export-generic-check-section (type &optional prefix suffix
)
559 "checks to see if type is already in use, or we're switching parts
560 If we're switching, then insert a potentially previously remembered
561 suffix, and insert the current prefix immediately and then save the
562 suffix a later change time."
564 (when (not (equal type org-export-generic-section-type
))
565 (if org-export-generic-section-suffix
566 (insert org-export-generic-section-suffix
))
567 (setq org-export-generic-section-type type
)
568 (setq org-export-generic-section-suffix suffix
)
572 (defun org-export-generic (arg)
573 "Export the outline as generic output.
574 If there is an active region, export only the region.
575 The prefix ARG specifies how many levels of the outline should become
576 underlined headlines. The default is 3."
578 (setq-default org-todo-line-regexp org-todo-line-regexp
)
579 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
580 (org-infile-export-plist)))
581 (region-p (org-region-active-p))
582 (rbeg (and region-p
(region-beginning)))
583 (rend (and region-p
(region-end)))
588 (and (org-at-heading-p)
589 (>= (org-end-of-subtree t t
) rend
)))))
590 (level-offset (if subtree-p
593 (+ (funcall outline-level
)
594 (if org-odd-levels-only
1 0)))
596 (opt-plist (setq org-export-opt-plist
598 (org-export-add-subtree-options opt-plist rbeg
)
602 (bogus (mapc (lambda (x)
604 (concat helpstart
"\["
606 (plist-get (cdr x
) :key-binding
))
610 (help (concat helpstart
"
612 \[ ] the current setting of the org-generic-export-type variable
620 (plist-get (cdr x
) :key-binding
)
623 (list (list ?
"default"))))
627 ;; read in the type to use
631 (save-window-excursion
632 (delete-other-windows)
633 (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
635 (org-fit-window-to-buffer (get-buffer-window
636 "*Org Export/Generic Styles Help*"))
637 (message "Select command: ")
638 (setq r1
(read-char-exclusive))))
639 (setq r2
(if (< r1
27) (+ r1
96) r1
))
640 (unless (setq ass
(cadr (assq r2 cmds
)))
641 (error "No command associated with key %c" r1
))
644 (if (equal ass
"default") org-generic-export-type ass
)
645 org-generic-alist
))))
647 (custom-times org-display-custom-times
)
648 (org-generic-current-indentation '(0 .
0))
649 (level 0) (old-level 0) line txt lastwastext
652 (case-fold-search nil
)
653 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
654 (filesuffix (or (plist-get export-plist
:file-suffix
) ".foo"))
655 (filename (concat (file-name-as-directory
656 (org-export-directory :ascii opt-plist
))
657 (file-name-sans-extension
659 (org-entry-get (region-beginning)
660 "EXPORT_FILE_NAME" t
))
661 (file-name-nondirectory bfname
)))
663 (filename (if (equal (file-truename filename
)
664 (file-truename bfname
))
665 (concat filename filesuffix
)
667 (buffer (find-file-noselect filename
))
668 (org-levels-open (make-vector org-level-max nil
))
669 (odd org-odd-levels-only
)
670 (date (plist-get opt-plist
:date
))
671 (author (plist-get opt-plist
:author
))
672 (title (or (and subtree-p
(org-export-get-title-from-subtree))
673 (plist-get opt-plist
:title
)
675 (plist-get opt-plist
:skip-before-1st-heading
))
676 (org-export-grab-title-from-buffer))
677 (file-name-sans-extension
678 (file-name-nondirectory bfname
))))
679 (email (plist-get opt-plist
:email
))
680 (language (plist-get opt-plist
:language
))
681 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
682 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
687 (if (org-region-active-p) (region-beginning) (point-min))
688 (if (org-region-active-p) (region-end) (point-max))))
689 (org-export-current-backend 'org-export-generic
)
690 (lines (org-split-string
691 (org-export-preprocess-string
694 :skip-before-1st-heading
695 (plist-get opt-plist
:skip-before-1st-heading
)
696 :drawers
(plist-get export-plist
:drawers-export
)
697 :tags
(plist-get export-plist
:tags-export
)
698 :priority
(plist-get export-plist
:priority-export
)
699 :footnotes
(plist-get export-plist
:footnotes-export
)
700 :timestamps
(plist-get export-plist
:timestamps-export
)
701 :todo-keywords
(plist-get export-plist
:todo-keywords-export
)
702 :verbatim-multiline t
703 :select-tags
(plist-get export-plist
:select-tags-export
)
704 :exclude-tags
(plist-get export-plist
:exclude-tags-export
)
707 (plist-get export-plist
:archived-trees-export
)
708 :add-text
(plist-get opt-plist
:text
))
710 ;; export-generic plist variables
711 (withtags (plist-get export-plist
:tags-export
))
712 (tagsintoc (plist-get export-plist
:toc-tags-export
))
713 (tocnotagsstr (or (plist-get export-plist
:toc-tags-none-string
) ""))
714 (tocdepth (plist-get export-plist
:toc-indent-depth
))
715 (tocindentchar (plist-get export-plist
:toc-indent-char
))
716 (tocsecnums (plist-get export-plist
:toc-section-numbers
))
717 (tocsecnumform (plist-get export-plist
:toc-section-number-format
))
718 (tocformat (plist-get export-plist
:toc-format
))
719 (tocformtodo (plist-get export-plist
:toc-format-with-todo
))
720 (tocprefix (plist-get export-plist
:toc-prefix
))
721 (tocsuffix (plist-get export-plist
:toc-suffix
))
722 (bodyfixedpre (plist-get export-plist
:body-line-fixed-prefix
))
723 (bodyfixedsuf (plist-get export-plist
:body-line-fixed-suffix
))
724 (bodyfixedform (or (plist-get export-plist
:body-line-fixed-format
)
726 (listprefix (plist-get export-plist
:body-list-prefix
))
727 (listsuffix (plist-get export-plist
:body-list-suffix
))
728 (listformat (or (plist-get export-plist
:body-list-format
) "%s\n"))
730 (plist-get export-plist
:body-number-list-leave-number
))
731 (numlistprefix (plist-get export-plist
:body-number-list-prefix
))
732 (numlistsuffix (plist-get export-plist
:body-number-list-suffix
))
734 (or (plist-get export-plist
:body-number-list-format
) "%s\n"))
736 (or (plist-get export-plist
:body-list-checkbox-todo
) "\\1"))
738 (or (plist-get export-plist
:body-list-checkbox-done
) "\\1"))
740 (or (plist-get export-plist
:body-list-checkbox-half
) "\\1"))
742 (or (plist-get export-plist
:body-list-checkbox-todo-end
) ""))
744 (or (plist-get export-plist
:body-list-checkbox-done-end
) ""))
746 (or (plist-get export-plist
:body-list-checkbox-half-end
) ""))
748 (or (plist-get export-plist
:body-table-start
) ""))
750 (or (plist-get export-plist
:body-table-end
) ""))
752 (or (plist-get export-plist
:body-table-row-start
) ""))
754 (or (plist-get export-plist
:body-table-row-end
) ""))
756 (or (plist-get export-plist
:body-table-cell-start
) ""))
758 (or (plist-get export-plist
:body-table-cell-end
) ""))
759 (bodytablefirstcellstart
760 (or (plist-get export-plist
:body-table-first-cell-start
) ""))
761 (bodytableinteriorcellstart
762 (or (plist-get export-plist
:body-table-interior-cell-start
) ""))
763 (bodytableinteriorcellend
764 (or (plist-get export-plist
:body-table-interior-cell-end
) ""))
765 (bodytablelastcellend
766 (or (plist-get export-plist
:body-table-last-cell-end
) ""))
768 (or (plist-get export-plist
:body-table-hline-start
) " \\1"))
770 (or (plist-get export-plist
:body-table-hline-end
) ""))
774 (bodynewline-paragraph (plist-get export-plist
:body-newline-paragraph
))
775 (bodytextpre (plist-get export-plist
:body-text-prefix
))
776 (bodytextsuf (plist-get export-plist
:body-text-suffix
))
777 (bodylinewrap (plist-get export-plist
:body-line-wrap
))
778 (bodylineform (or (plist-get export-plist
:body-line-format
) "%s"))
779 (blockquotestart (or (plist-get export-plist
:blockquote-start
) "\n\n\t"))
780 (blockquoteend (or (plist-get export-plist
:blockquote-end
) "\n\n"))
782 ;; dynamic variables used heinously in fontification
783 ;; not referenced locally...
784 (format-boldify (plist-get export-plist
:bold-format
))
785 (format-italicize (plist-get export-plist
:italic-format
))
786 (format-underline (plist-get export-plist
:underline-format
))
787 (format-strikethrough (plist-get export-plist
:strikethrough-format
))
788 (format-code (plist-get export-plist
:code-format
))
789 (format-verbatim (plist-get export-plist
:verbatim-format
))
793 thetoc toctags have-headings first-heading-pos
794 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
796 (let ((inhibit-read-only t
))
798 (remove-text-properties (point-min) (point-max)
799 '(:org-license-to-kill t
))))
801 (setq org-min-level
(org-get-min-level lines level-offset
))
802 (setq org-last-level org-min-level
)
803 (org-init-section-numbers)
805 (find-file-noselect filename
)
807 (setq lang-words
(or (assoc language org-export-language-setup
)
808 (assoc "en" org-export-language-setup
)))
809 (switch-to-buffer-other-window buffer
)
812 ;; create local variables for all options, to make sure all called
813 ;; functions get the correct information
815 (set (make-local-variable (nth 2 x
))
816 (plist-get opt-plist
(car x
))))
817 org-export-plist-vars
)
818 (org-set-local 'org-odd-levels-only odd
)
819 (setq umax
(if arg
(prefix-numeric-value arg
)
820 org-export-headline-levels
))
826 (org-export-generic-header title export-plist
831 (if (and (or author email
)
832 (plist-get export-plist
:author-export
))
833 (insert (concat (nth 1 lang-words
) ": " (or author
"")
834 (if email
(concat " <" email
">") "")
838 ((and date
(string-match "%" date
))
839 (setq date
(format-time-string date
)))
841 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
843 (if (and date
(plist-get export-plist
:date-export
))
845 (org-export-generic-header date export-plist
850 ;; export the table of contents first
851 (if (plist-get export-plist
:toc-export
)
854 (org-export-generic-header (nth 3 lang-words
) export-plist
861 (push tocprefix thetoc
))
863 (mapc #'(lambda (line)
864 (if (string-match org-todo-line-regexp line
)
865 ;; This is a headline
867 (setq have-headings t
)
868 (setq level
(- (match-end 1) (match-beginning 1)
870 level
(org-tr-level level
)
871 txt
(match-string 3 line
)
873 (or (and org-export-mark-todo-in-toc
875 (not (member (match-string 2 line
)
878 (and org-export-mark-todo-in-toc
880 (org-search-todo-below
882 (setq txt
(org-html-expand-for-generic txt
))
884 (while (string-match org-bracket-link-regexp txt
)
887 (match-string (if (match-end 2) 3 1) txt
)
890 (if (and (not tagsintoc
)
892 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
894 (setq txt
(replace-match "" t t txt
))
895 ; include tags but formated
897 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
902 (org-export-generic-header
904 export-plist
:toc-tags-prefix
905 :toc-tags-format
:toc-tags-suffix
))
907 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
909 (setq txt
(replace-match "" t t txt
)))
910 (setq toctags tocnotagsstr
)))
912 (if (string-match quote-re0 txt
)
913 (setq txt
(replace-match "" t t txt
)))
915 (if (<= level umax-toc
)
921 (* (max 0 (- level org-min-level
)) tocdepth
)
925 (format tocsecnumform
926 (org-section-number level
))
930 (if todo tocformtodo tocformat
)
936 (setq org-last-level level
))))))
939 (push tocsuffix thetoc
))
940 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
942 (org-init-section-numbers)
943 (org-export-generic-check-section "top")
944 (while (setq line
(pop lines
))
945 (when (and link-buffer
(string-match org-outline-regexp-bol line
))
946 (org-export-generic-push-links (nreverse link-buffer
))
947 (setq link-buffer nil
))
949 ;; Remove the quoted HTML tags.
951 (setq line
(org-html-expand-for-generic line
))
952 ;; Replace links with the description when possible
954 (while (string-match org-bracket-link-regexp line
)
955 (setq link
(match-string 1 line
)
956 desc0
(match-string 3 line
)
957 desc
(or desc0
(match-string 1 line
)))
958 (if (and (> (length link
) 8)
959 (equal (substring link
0 8) "coderef:"))
960 (setq line
(replace-match
961 (format (org-export-get-coderef-format (substring link
8) desc
)
964 org-export-code-refs
)))
966 (setq rpl
(concat "["
967 (or (match-string 3 line
) (match-string 1 line
))
969 (when (and desc0
(not (equal desc0 link
)))
970 (if org-export-generic-links-to-notes
971 (push (cons desc0 link
) link-buffer
)
972 (setq rpl
(concat rpl
" (" link
")")
973 wrap
(+ (length line
) (- (length (match-string 0 line
)))
975 (setq line
(replace-match rpl t t line
))))
977 (setq line
(org-translate-time line
)))
979 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
983 (org-export-generic-check-section "headline")
985 (setq first-heading-pos
(or first-heading-pos
(point)))
986 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
988 txt
(match-string 2 line
))
989 (org-generic-level-start level old-level txt umax export-plist lines
)
990 (setq old-level level
))
992 ((and org-export-with-tables
993 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
997 (org-export-generic-check-section "table")
1001 (setq table-open t table-buffer nil
))
1002 ;; Accumulate table lines
1003 (setq table-buffer
(cons line table-buffer
))
1004 (when (or (not lines
)
1005 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1007 (setq table-open nil
1008 table-buffer
(nreverse table-buffer
))
1011 (org-fix-indentation x org-generic-current-indentation
))
1012 (org-format-table-generic table-buffer
)
1015 ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
)
1017 ;; pre-formatted text
1019 (setq line
(replace-match "\\1" nil nil line
))
1021 (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf
)
1023 (insert (format bodyfixedform line
)))
1025 ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line
)
1026 ;; if the bullet list item is an asterisk, the leading space is /mandatory/
1028 (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line
))
1031 ;; TODO: nested lists
1033 ;; first add a line break between any previous paragraph or line item and this
1035 (when bodynewline-paragraph
1036 (insert bodynewline-paragraph
))
1038 ;; I believe this gets rid of leading whitespace.
1039 (setq line
(replace-match "" nil nil line
))
1041 ;; won't this insert the suffix /before/ the last line of the list?
1042 ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
1043 ;; unless 'org-empty-line-terminates-plain-lists' is true?
1044 (org-export-generic-check-section "liststart" listprefix listsuffix
)
1046 ;; deal with checkboxes
1048 ((string-match "^\\(\\[ \\]\\)[ \t]*" line
)
1049 (setq line
(concat (replace-match listchecktodo nil nil line
)
1051 ((string-match "^\\(\\[X\\]\\)[ \t]*" line
)
1052 (setq line
(concat (replace-match listcheckdone nil nil line
)
1054 ((string-match "^\\(\\[/\\]\\)[ \t]*" line
)
1055 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1056 listcheckhalfend
))))
1058 (insert (format listformat
(org-export-generic-fontify line
))))
1059 ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line
)
1061 ;; numbered list item
1063 ;; TODO: nested lists
1065 (setq line
(replace-match (if numlistleavenum
"\\2" "") nil nil line
))
1067 (org-export-generic-check-section "numliststart"
1068 numlistprefix numlistsuffix
)
1070 ;; deal with checkboxes
1071 ;; TODO: whoops; leaving the numbers is a problem for ^ matching
1073 ((string-match "\\(\\[ \\]\\)[ \t]*" line
)
1074 (setq line
(concat (replace-match listchecktodo nil nil line
)
1076 ((string-match "\\(\\[X\\]\\)[ \t]*" line
)
1077 (setq line
(concat (replace-match listcheckdone nil nil line
)
1079 ((string-match "\\(\\[/\\]\\)[ \t]*" line
)
1080 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1081 listcheckhalfend
))))
1083 (insert (format numlistformat
(org-export-generic-fontify line
))))
1085 ((equal line
"ORG-BLOCKQUOTE-START")
1086 (setq line blockquotestart
))
1087 ((equal line
"ORG-BLOCKQUOTE-END")
1088 (setq line blockquoteend
))
1089 ((string-match "^\\s-*$" line
)
1091 (if bodynewline-paragraph
1092 (insert bodynewline-paragraph
)))
1097 (org-export-generic-check-section "body" bodytextpre bodytextsuf
)
1100 (org-export-generic-fontify line
))
1102 ;; XXX: properties? list?
1103 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
1104 (setq line
(replace-match "\\1\\3:" t nil line
)))
1106 (setq line
(org-fix-indentation line org-generic-current-indentation
))
1108 ;; Remove forced line breaks
1109 (if (string-match "\\\\\\\\[ \t]*$" line
)
1110 (setq line
(replace-match "" t t line
)))
1113 ;; XXX: was dependent on wrap var which was calculated by???
1114 (if (> (length line
) bodylinewrap
)
1116 (org-export-generic-wrap line bodylinewrap
))
1118 (insert (format bodylineform line
)))))
1120 ;; if we're at a level > 0; insert the closing body level stuff
1122 (while (> (- level counter
) 0)
1124 (org-export-generic-format export-plist
:body-section-suffix
0
1126 (setq counter
(1+ counter
))))
1128 (org-export-generic-check-section "bottom")
1130 (org-export-generic-push-links (nreverse link-buffer
))
1134 ;; insert the table of contents
1136 (goto-char (point-min))
1137 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
1139 (goto-char (match-beginning 0))
1141 (goto-char first-heading-pos
))
1142 (mapc 'insert thetoc
)
1143 (or (looking-at "[ \t]*\n[ \t]*\n")
1146 ;; Convert whitespace place holders
1147 (goto-char (point-min))
1149 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
1150 (setq end
(next-single-property-change beg
'org-whitespace
))
1152 (delete-region beg end
)
1153 (insert (make-string (- end beg
) ?\
))))
1157 ;; remove display and invisible chars
1159 (goto-char (point-min))
1160 (while (setq beg
(next-single-property-change (point) 'display
))
1161 (setq end
(next-single-property-change beg
'display
))
1162 (delete-region beg end
)
1165 (goto-char (point-min))
1166 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
1167 (setq end
(next-single-property-change beg
'org-cwidth
))
1168 (delete-region beg end
)
1170 (goto-char (point-min))))
1173 (defun org-export-generic-format (export-plist prop
&optional len n reverse
)
1174 "converts a property specification to a string given types of properties
1176 The EXPORT-PLIST should be defined as the lookup plist.
1177 The PROP should be the property name to search for in it.
1178 LEN is set to the length of multi-characters strings to generate (or 0)
1180 REVERSE means to reverse the list if the plist match is a list
1182 (let* ((prefixtype (plist-get export-plist prop
))
1185 ((null prefixtype
) "")
1186 ((and len
(char-or-string-p prefixtype
) (not (stringp prefixtype
)))
1187 ;; sequence of chars
1188 (concat (make-string len prefixtype
) "\n"))
1189 ((stringp prefixtype
)
1191 ((and n
(listp prefixtype
))
1193 (setq prefixtype
(reverse prefixtype
)))
1194 (setq subtype
(if (> n
(length prefixtype
))
1195 (car (last prefixtype
))
1196 (nth (1- n
) prefixtype
)))
1197 (if (stringp subtype
)
1199 (concat (make-string len subtype
) "\n")))
1202 (defun org-export-generic-header (header export-plist
1203 prefixprop formatprop postfixprop
1204 &optional n reverse
)
1205 "convert a header to an output string given formatting property names"
1206 (let* ((formatspec (plist-get export-plist formatprop
))
1207 (len (length header
)))
1209 (org-export-generic-format export-plist prefixprop len n reverse
)
1210 (format (or formatspec
"%s") header
)
1211 (org-export-generic-format export-plist postfixprop len n reverse
))))
1213 (defun org-export-generic-preprocess (parameters)
1214 "Do extra work for ASCII export"
1215 ;; Put quotes around verbatim text
1216 (goto-char (point-min))
1217 (while (re-search-forward org-verbatim-re nil t
)
1218 (goto-char (match-end 2))
1219 (delete-backward-char 1) (insert "'")
1220 (goto-char (match-beginning 2))
1221 (delete-char 1) (insert "`")
1222 (goto-char (match-end 2)))
1223 ;; Remove target markers
1224 (goto-char (point-min))
1225 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
1226 (replace-match "\\1\\2")))
1228 (defun org-html-expand-for-generic (line)
1229 "Handle quoted HTML for ASCII export."
1230 (if org-export-html-expand
1231 (while (string-match "@<[^<>\n]*>" line
)
1232 ;; We just remove the tags for now.
1233 (setq line
(replace-match "" nil nil line
))))
1236 (defun org-export-generic-wrap (line where
)
1237 "Wrap LINE at or before WHERE."
1238 (let* ((ind (org-get-indentation line
))
1239 (indstr (make-string ind ?\
))
1243 (while (> len where
)
1245 (loop for i from where downto
(/ where
2) do
1246 (and (equal (aref line i
) ?\
)
1253 (if didfirst indstr
"")
1254 (substring line
0 pos
)
1257 (setq line
(substring line
(1+ pos
)))
1258 (setq len
(length line
)))
1259 (setq result
(concat result line
))
1261 (concat result indstr line
)))
1263 (defun org-export-generic-push-links (link-buffer)
1264 "Push out links in the buffer."
1266 ;; We still have links to push out.
1272 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
1273 (setq ind
(or (match-string 2)
1274 (make-string (length (match-string 3)) ?\
)))))
1275 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
1279 (defun org-generic-level-start (level old-level title umax export-plist
1281 "Insert a new level in a generic export."
1282 (let ((n (- level umax
1))
1284 (diff (- level old-level
)) (counter 0)
1285 (secnums (plist-get export-plist
:body-header-section-numbers
))
1287 (plist-get export-plist
:body-header-section-number-format
))
1289 (unless org-export-with-tags
1290 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
1291 (setq title
(replace-match "" t t title
))))
1295 ((> level old-level
)
1296 (while (< (+ old-level counter
) (1- level
))
1298 (org-export-generic-format export-plist
:body-section-prefix
0
1299 (+ old-level counter
)))
1300 (setq counter
(1+ counter
))))
1302 ((< level old-level
)
1303 (while (> (- old-level counter
) (1- level
))
1305 (org-export-generic-format export-plist
:body-section-suffix
0
1306 (- old-level counter
)))
1307 (setq counter
(1+ counter
))))
1309 ((= level old-level
)
1311 (org-export-generic-format export-plist
:body-section-suffix
0 level
))))
1313 (org-export-generic-format export-plist
:body-section-prefix
0 level
))
1315 (if (and org-export-with-section-numbers
1317 (or (not (numberp secnums
))
1320 (concat (format (or secnumformat
"%s ")
1321 (org-section-number level
)) title
)))
1323 ;; handle tags and formatting
1325 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title
)
1327 (if (plist-get export-plist
:body-tags-export
)
1328 (setq tagstring
(org-export-generic-header (match-string 1 title
)
1332 :body-tags-suffix
)))
1333 (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title
)
1334 (setq title
(replace-match "" t t title
)))
1335 (setq tagstring
(plist-get export-plist
:body-tags-none-string
)))
1338 (org-export-generic-header title export-plist
1339 :body-section-header-prefix
1340 :body-section-header-format
1341 :body-section-header-suffix
1346 (setq org-generic-current-indentation
'(0 .
0))))
1348 (defun org-insert-centered (s &optional underline
)
1349 "Insert the string S centered and underline it with character UNDERLINE."
1350 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
1351 (insert (make-string ind ?\
) s
"\n")
1353 (insert (make-string ind ?\
)
1354 (make-string (string-width s
) underline
)
1357 (defvar org-table-colgroup-info nil
)
1358 (defun org-format-table-generic (lines)
1359 "Format a table for ascii export."
1361 (setq lines
(org-split-string lines
"\n")))
1362 (if (not (string-match "^[ \t]*|" (car lines
)))
1363 ;; Table made by table.el - test for spanning
1366 ;; A normal org table
1367 ;; Get rid of hlines at beginning and end
1368 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1369 (setq lines
(nreverse lines
))
1370 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1371 (setq lines
(nreverse lines
))
1372 (when org-export-table-remove-special-lines
1373 ;; Check if the table has a marking column. If yes remove the
1374 ;; column and the special lines
1375 (setq lines
(org-table-clean-before-export lines
)))
1376 ;; Get rid of the vertical lines except for grouping
1377 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
1378 (rtn (list bodytablestart
)) line vl1 start
)
1379 (while (setq line
(pop lines
))
1380 (setq line
(concat bodytablerowstart line
))
1381 (if (string-match org-table-hline-regexp line
)
1382 (and (string-match "|\\(.*\\)|" line
)
1383 (setq line
(replace-match (concat bodytablehlinestart bodytablehlineend
) t nil line
)))
1384 (setq start
0 vl1 vl
)
1385 (if (string-match "|\\(.*\\)|" line
)
1386 (setq line
(replace-match (concat bodytablefirstcellstart bodytablecellstart
" \\1 " bodytablecellend bodytablelastcellend
) t nil line
)))
1387 (while (string-match "|" line start
)
1388 (setq start
(+ (match-end 0) (length (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart
))))
1389 (or (pop vl1
) (setq line
(replace-match (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart
) t t line
)))))
1390 (setq line
(concat line bodytablerowend
))
1392 (setq rtn
(cons bodytableend rtn
))
1395 (defun org-colgroup-info-to-vline-list (info)
1398 (setq last new new
(pop info
))
1399 (if (or (memq last
'(:end
:startend
))
1400 (memq new
'(:start
:startend
)))
1403 (setq vl
(nreverse vl
))
1404 (and vl
(setcar vl nil
))
1408 ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
1409 (defvar org-export-generic-emphasis-alist
1410 '(("*" format-boldify nil
)
1411 ("/" format-italicize nil
)
1412 ("_" format-underline nil
)
1413 ("+" format-strikethrough nil
)
1415 ("~" format-verbatim t
))
1416 "Alist of org format -> formatting variables for fontification.
1417 Each element of the list is a list of three elements.
1418 The first element is the character used as a marker for fontification.
1419 The second element is a variable name, set in org-export-generic. That
1420 variable will be dereferenced to obtain a formatting string to wrap
1421 fontified text with.
1422 The third element decides whether to protect converted text from other
1425 ;;; Cargo-culted from the latex translation. I couldn't figure out how
1426 ;;; to keep the structure since the generic export operates on lines, rather
1427 ;;; than on a buffer as in the latex export, meaning that none of the
1428 ;;; search forward code could be kept. This led me to rewrite the
1429 ;;; whole thing recursively. A huge lose for efficiency (potentially),
1430 ;;; but I couldn't figure out how to make the looping work.
1431 ;;; Worse, it's /doubly/ recursive, because this function calls
1432 ;;; org-export-generic-emph-format, which can call it recursively...
1433 ;;; [2010/05/20:rpg]
1434 (defun org-export-generic-fontify (string)
1435 "Convert fontification according to generic rules."
1436 (if (string-match org-emph-re string
)
1437 ;; The match goes one char after the *string*, except at the end of a line
1438 (let ((emph (assoc (match-string 3 string
)
1439 org-export-generic-emphasis-alist
))
1440 (beg (match-beginning 0))
1441 (end (match-end 0)))
1443 (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
1444 (match-string 3 string
)))
1445 ;; now we need to determine whether we have strikethrough or
1446 ;; a list, which is a bit nasty
1447 (if (and (equal (match-string 3 string
) "+")
1449 (string-match "\\`-+\\'" (match-string 4 string
))))
1450 ;; a list --- skip this match and recurse on the point after the
1451 ;; first emph char...
1452 (concat (substring string
0 (1+ (match-beginning 3)))
1453 (org-export-generic-fontify (substring string
(match-beginning 3))))
1454 (concat (substring string
0 beg
) ;; part before the match
1455 (match-string 1 string
)
1456 (org-export-generic-emph-format (second emph
)
1457 (match-string 4 string
)
1459 (or (match-string 5 string
) "")
1460 (org-export-generic-fontify (substring string end
)))))
1463 (defun org-export-generic-emph-format (format-varname string protect
)
1464 "Return a string that results from applying the markup indicated by
1465 FORMAT-VARNAME to STRING."
1466 (let ((format (symbol-value format-varname
)))
1467 (let ((string-to-emphasize
1470 (org-export-generic-fontify string
))))
1472 (format format string-to-emphasize
)
1473 string-to-emphasize
))))
1475 (provide 'org-generic
)
1476 (provide 'org-export-generic
)
1478 ;;; org-export-generic.el ends here