1 ;; org-export-generic.el --- Export frameworg with custom backends
3 ;; Copyright (C) 2009-2012 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 yet 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 ;; org-export-generic is basically a simple translation system that
31 ;; knows how to parse at least most of a .org buffer and then add
32 ;; various formatting prefixes before and after each section type. It
33 ;; does this by examining a property list stored in org-generic-alist.
34 ;; You can dynamically add propety lists of your own using the
35 ;; org-set-generic-type function:
37 ;; (org-set-generic-type
38 ;; "really-basic-text"
39 ;; '(:file-suffix ".txt"
42 ;; :title-format "=== %s ===\n"
43 ;; :body-header-section-numbers t
44 ;; :body-header-section-number-format "%s) "
45 ;; :body-section-header-prefix "\n"
46 ;; :body-section-header-suffix "\n"
47 ;; :body-line-format " %s\n"
51 ;; Note: Upper case key-bindings are reserved for your use. Lower
52 ;; case key bindings may conflict with future export-generic
55 ;; Then run org-export (ctrl-c ctrl-e) and select generic or run
56 ;; org-export-generic. You'll then be prompted with a list of export
57 ;; types to choose from which will include your new type assigned to
60 ;; ----------------------------------------------------------------------
63 ;; * handle function references
64 ;; * handle other types of multi-complex-listy-things to do
65 ;; ideas: (t ?- "%s" ?-)
66 ;; * handle indent specifiers better
68 ;; * need flag to remove indents from body text
70 ;; * handle internationalization strings better
71 ;; * date/author/etc needs improvment (internationalization too)
72 ;; * allow specifying of section ordering
73 ;; ideas: :ordering ("header" "toc" "body" "footer")
74 ;; ^ matches current hard coded ordering
75 ;; * err, actually *do* a footer
76 ;; * deal with usage of org globals
77 ;; *** should we even consider them, or let the per-section specifiers do it
78 ;; *** answer: remove; mostly removed now
79 ;; * deal with interactive support for picking a export specifier label
80 ;; * char specifiers that need extra length because of formatting
81 ;; idea: (?- 4) for 4-longer
82 ;; * centering specifier
83 ;; idea: ('center " -- %s -- ")
84 ;; * remove more of the unneeded export-to-ascii copy code
86 ;; *** supported now, but need separate format per tag
87 ;; *** allow different open/closing prefixes
90 ;; * Escape camel-case for wiki exporters.
91 ;; * Adjust to depth limits on headers --- need to roll-over from headers
92 ;; to lists, as per other exporters
93 ;; * optmization (many plist extracts should be in let vars)
94 ;; * define defcustom spec for the specifier list
95 ;; * fonts: at least monospace is not handled at all here.
97 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104 (defgroup org-export-generic nil
105 "Options specific for ASCII export of Org-mode files."
106 :tag
"Org Export ASCII"
109 (defcustom org-export-generic-links-to-notes t
110 "Non-nil means convert links to notes before the next headline.
111 When nil, the link will be exported in place. If the line becomes long
112 in this way, it will be wrapped."
113 :group
'org-export-generic
117 (defvar org-generic-current-indentation nil
) ; For communication
119 (defvar org-generic-alist
122 ;; generic DEMO exporter
124 ;; (this tries to use every specifier for demo purposes)
130 :header-prefix
"<header>\n"
131 :header-suffix
"</header>\n"
140 :title-format
"<h1>%s</h1>\n"
144 :date-prefix
"<date>"
145 :date-format
"<br /><b>Date:</b> <i>%s</i><br />"
146 :date-suffix
"</date>\n\n"
149 :toc-header-prefix
"<tocname>\n"
150 :toc-header-format
"__%s__\n"
151 :toc-header-suffix
"</tocname>\n"
153 :toc-prefix
"<toc>\n"
154 :toc-suffix
"</toc>\n"
156 :toc-section-numbers t
157 :toc-section-number-format
"\#(%s) "
159 :toc-format-with-todo
"!!%s!!\n"
164 :toc-tags-prefix
" <tags>"
165 :toc-tags-format
"*%s*"
166 :toc-tags-suffix
"</tags>\n"
167 :toc-tags-none-string
"\n"
169 :body-header-section-numbers
3 ; t = all, nil = none
171 ; lists indicate different things per level
172 ; list contents or straight value can either be a
173 ; ?x char reference for printing strings that match the header len
174 ; "" string to print directly
175 :body-section-header-prefix
("<h1>" "<h2>" "<h3>"
176 "<h4>" "<h5>" "<h6>")
177 :body-section-header-format
"%s"
178 :body-section-header-suffix
("</h1>\n" "</h2>\n" "</h3>\n"
179 "</h4>\n" "</h5>\n" "</h6>\n")
183 :todo-keywords-export t
186 :body-tags-prefix
" <tags>"
187 :body-tags-suffix
"</tags>\n"
189 ; section prefixes/suffixes can be direct strings or lists as well
190 :body-section-prefix
"<secprefix>\n"
191 :body-section-suffix
"</secsuffix>\n"
192 ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
193 ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
196 ; if preformated text should be included (eg, : prefixed)
197 :body-line-export-preformated t
198 :body-line-fixed-prefix
"<pre>\n"
199 :body-line-fixed-suffix
"\n</pre>\n"
200 :body-line-fixed-format
"%s\n"
203 :body-list-prefix
"<list>\n"
204 :body-list-suffix
"</list>\n"
205 :body-list-format
"<li>%s</li>\n"
207 :body-number-list-prefix
"<ol>\n"
208 :body-number-list-suffix
"</ol>\n"
209 :body-number-list-format
"<li>%s</li>\n"
210 :body-number-list-leave-number t
212 :body-list-checkbox-todo
"<checkbox type=\"todo\">"
213 :body-list-checkbox-todo-end
"</checkbox (todo)>"
214 :body-list-checkbox-done
"<checkbox type=\"done\">"
215 :body-list-checkbox-done-end
"</checkbox (done)>"
216 :body-list-checkbox-half
"<checkbox type=\"half\">"
217 :body-list-checkbox-half-end
"</checkbox (half)>"
223 :body-line-format
"%s"
224 :body-line-wrap
60 ; wrap at 60 chars
226 ; print above and below all body parts
227 :body-text-prefix
"<p>\n"
228 :body-text-suffix
"</p>\n"
235 ;; (close to the original ascii specifier)
250 :date-format
"Date: %s\n"
253 :toc-header-prefix
""
254 :toc-header-format
"%s\n"
255 :toc-header-suffix ?
=
258 :toc-section-numbers t
259 :toc-section-number-format
"%s "
261 :toc-format-with-todo
"%s (*)\n"
265 :body-header-section-numbers
3
266 :body-section-prefix
"\n"
268 ; :body-section-header-prefix "\n"
269 ; :body-section-header-format "%s\n"
270 ; :body-section-header-suffix (?\$ ?\# ?^ ?\~ ?\= ?\-)
272 :body-section-header-prefix
("" "" "" "* " " + " " - ")
273 :body-section-header-format
"%s\n"
274 :body-section-header-suffix
(?~ ?
= ?-
"\n" "\n" "\n")
276 ; :body-section-marker-prefix ""
277 ; :body-section-marker-chars (?\$ ?\# ?^ ?\~ ?\= ?\-)
278 ; :body-section-marker-suffix "\n"
280 :body-line-export-preformated t
281 :body-line-format
"%s\n"
284 ; :body-text-prefix "<t>\n"
285 ; :body-text-suffix "</t>\n"
288 :body-bullet-list-prefix
(?
* ?
+ ?-
)
289 ; :body-bullet-list-suffix (?* ?+ ?-)
302 :title-format
"= %s =\n"
308 :body-header-section-numbers nil
309 :body-section-prefix
"\n"
311 :body-section-header-prefix
("= " "== " "=== "
312 "==== " "===== " "====== ")
313 :body-section-header-suffix
(" =\n\n" " ==\n\n" " ===\n\n"
314 " ====\n\n" " =====\n\n" " ======\n\n")
316 :body-line-export-preformated t
;; yes/no/maybe???
317 :body-line-format
"%s\n"
320 :body-line-fixed-format
" %s\n"
322 :body-list-format
"* %s\n"
323 :body-number-list-format
"# %s\n"
325 :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
""
373 ;; internet-draft .xml for xml2rfc exporter
376 ;; this tries to use every specifier for demo purposes
380 :title-prefix
"<?xml version=\"1.0\"\?>
381 <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
382 <!ENTITY rfcs PUBLIC '' 'blah'>
383 <?rfc strict=\"yes\" ?>
385 <?rfc tocdepth=\"4\" ?>
386 <?rfc symrefs=\"yes\" ?>
387 <?rfc compact=\"yes\" ?>
388 <?rfc subcompact=\"no\" ?>
389 <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
392 :title-format
"<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
393 :title-suffix
"<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
394 <organization>Comany, Inc..</organization>
407 <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
408 <area>Operations and Management</area>
409 <workgroup>FIXME</workgroup>
415 :body-header-section-numbers nil
417 :body-section-header-format
"<section title=\"%s\">\n"
418 :body-section-suffix
"</section>\n"
420 ; if preformated text should be included (eg, : prefixed)
421 :body-line-export-preformated t
422 :body-line-fixed-prefix
"<figure>\n<artwork>\n"
423 :body-line-fixed-suffix
"\n</artwork>\n</figure>\n"
426 :body-line-format
"%s"
429 ; print above and below all body parts
430 :body-text-prefix
"<t>\n"
431 :body-text-suffix
"</t>\n"
433 :body-list-prefix
"<list style=\"symbols\">\n"
434 :body-list-suffix
"</list>\n"
435 :body-list-format
"<t>%s</t>\n"
442 ;; lifted from wikipedia exporter
446 :title-format
"= %s =\n"
452 :body-header-section-numbers nil
453 :body-section-prefix
"\n"
455 :body-section-header-prefix
(" == " " === " " ==== "
457 :body-section-header-suffix
(" ==\n\n" " ===\n\n" " ====\n\n"
458 " =====\n\n" " ======\n\n" " =======\n\n")
460 :body-line-export-preformated t
;; yes/no/maybe???
461 :body-line-format
"%s\n"
464 :body-line-fixed-format
" %s\n"
466 :body-list-format
" * %s\n"
467 :body-number-list-format
" # %s\n"
468 ;; :body-list-prefix "LISTSTART"
469 ;; :body-list-suffix "LISTEND"
471 ;; this is ignored! [2010/02/02:rpg]
472 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** ")
478 ;; lifted from wikipedia exporter
482 :title-format
"-= %s =-\n"
488 :body-header-section-numbers nil
489 :body-section-prefix
"\n"
491 :body-section-header-prefix
("! " "!! " "!!! " "!!!! "
492 "!!!!! " "!!!!!! " "!!!!!!! ")
493 :body-section-header-suffix
(" \n" " \n" " \n"
497 :body-line-export-preformated t
;; yes/no/maybe???
498 :body-line-format
"%s "
501 :body-line-fixed-format
" %s\n"
503 :body-list-format
"* %s\n"
504 :body-number-list-format
"# %s\n"
505 ;; :body-list-prefix "LISTSTART"
506 ;; :body-list-suffix "LISTEND"
507 :blockquote-start
"\n^\n"
508 :blockquote-end
"^\n\n"
509 :body-newline-paragraph
"\n"
510 :bold-format
"__%s__"
511 :italic-format
"''%s''"
512 :underline-format
"===%s==="
513 :strikethrough-format
"--%s--"
514 :code-format
"-+%s+-"
515 :verbatim-format
"~pp~%s~/pp~"
518 "A assoc list of property lists to specify export definitions"
521 (setq org-generic-export-type
"demo")
523 (defvar org-export-generic-section-type
"")
524 (defvar org-export-generic-section-suffix
"")
527 (defun org-set-generic-type (type definition
)
528 "Adds a TYPE and DEFINITION to the existing list of defined generic
530 (aput 'org-generic-alist type definition
))
532 ;;; helper functions for org-set-generic-type
533 (defvar org-export-generic-keywords nil
)
534 (defmacro* def-org-export-generic-keyword
(keyword
537 "Define KEYWORD as a legitimate element for inclusion in
538 the body of an org-set-generic-type definition."
540 (pushnew ,keyword org-export-generic-keywords
)
541 ;; TODO: push the documentation and type information
542 ;; somewhere where it will do us some good.
545 (def-org-export-generic-keyword :body-newline-paragraph
546 :documentation
"Bound either to NIL or to a pattern to be
547 inserted in the output for every blank line in the input.
548 The intention is to handle formats where text is flowed, and
549 newlines are interpreted as significant \(e.g., as indicating
550 preformatted text\). A common non-nil value for this keyword
551 is \"\\n\". Should typically be combined with a value for
552 :body-line-format that does NOT end with a newline."
555 ;;; fontification keywords
556 (def-org-export-generic-keyword :bold-format
)
557 (def-org-export-generic-keyword :italic-format
)
558 (def-org-export-generic-keyword :underline-format
)
559 (def-org-export-generic-keyword :strikethrough-format
)
560 (def-org-export-generic-keyword :code-format
)
561 (def-org-export-generic-keyword :verbatim-format
)
566 (defun org-export-generic-remember-section (type suffix
&optional prefix
)
567 (setq org-export-generic-section-type type
)
568 (setq org-export-generic-section-suffix suffix
)
573 (defun org-export-generic-check-section (type &optional prefix suffix
)
574 "checks to see if type is already in use, or we're switching parts
575 If we're switching, then insert a potentially previously remembered
576 suffix, and insert the current prefix immediately and then save the
577 suffix a later change time."
579 (when (not (equal type org-export-generic-section-type
))
580 (if org-export-generic-section-suffix
581 (insert org-export-generic-section-suffix
))
582 (setq org-export-generic-section-type type
)
583 (setq org-export-generic-section-suffix suffix
)
588 (defun org-export-generic (arg)
589 "Export the outline as generic output.
590 If there is an active region, export only the region.
591 The prefix ARG specifies how many levels of the outline should become
592 underlined headlines. The default is 3."
594 (setq-default org-todo-line-regexp org-todo-line-regexp
)
595 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
596 (org-infile-export-plist)))
597 (region-p (org-region-active-p))
598 (rbeg (and region-p
(region-beginning)))
599 (rend (and region-p
(region-end)))
604 (and (org-at-heading-p)
605 (>= (org-end-of-subtree t t
) rend
)))))
606 (level-offset (if subtree-p
609 (+ (funcall outline-level
)
610 (if org-odd-levels-only
1 0)))
612 (opt-plist (setq org-export-opt-plist
614 (org-export-add-subtree-options opt-plist rbeg
)
618 (bogus (mapc (lambda (x)
620 (concat helpstart
"\["
622 (plist-get (cdr x
) :key-binding
))
626 (help (concat helpstart
"
628 \[ ] the current setting of the org-generic-export-type variable
636 (plist-get (cdr x
) :key-binding
)
639 (list (list ?
"default"))))
643 ;; read in the type to use
647 (save-window-excursion
648 (delete-other-windows)
649 (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
651 (org-fit-window-to-buffer (get-buffer-window
652 "*Org Export/Generic Styles Help*"))
653 (message "Select command: ")
654 (setq r1
(read-char-exclusive))))
655 (setq r2
(if (< r1
27) (+ r1
96) r1
))
656 (unless (setq ass
(cadr (assq r2 cmds
)))
657 (error "No command associated with key %c" r1
))
660 (if (equal ass
"default") org-generic-export-type ass
)
661 org-generic-alist
))))
663 (custom-times org-display-custom-times
)
664 (org-generic-current-indentation '(0 .
0))
665 (level 0) (old-level 0) line txt lastwastext
668 (case-fold-search nil
)
669 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
670 (filesuffix (or (plist-get export-plist
:file-suffix
) ".foo"))
671 (filename (concat (file-name-as-directory
672 (org-export-directory :ascii opt-plist
))
673 (file-name-sans-extension
675 (org-entry-get (region-beginning)
676 "EXPORT_FILE_NAME" t
))
677 (file-name-nondirectory bfname
)))
679 (filename (if (equal (file-truename filename
)
680 (file-truename bfname
))
681 (concat filename filesuffix
)
683 (buffer (find-file-noselect filename
))
684 (org-levels-open (make-vector org-level-max nil
))
685 (odd org-odd-levels-only
)
686 (date (plist-get opt-plist
:date
))
687 (author (plist-get opt-plist
:author
))
688 (title (or (and subtree-p
(org-export-get-title-from-subtree))
689 (plist-get opt-plist
:title
)
691 (plist-get opt-plist
:skip-before-1st-heading
))
692 (org-export-grab-title-from-buffer))
693 (file-name-sans-extension
694 (file-name-nondirectory bfname
))))
695 (email (plist-get opt-plist
:email
))
696 (language (plist-get opt-plist
:language
))
697 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
698 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
703 (if (org-region-active-p) (region-beginning) (point-min))
704 (if (org-region-active-p) (region-end) (point-max))))
705 (org-export-current-backend 'org-export-generic
)
706 (lines (org-split-string
707 (org-export-preprocess-string
710 :skip-before-1st-heading
711 (plist-get opt-plist
:skip-before-1st-heading
)
712 :drawers
(plist-get export-plist
:drawers-export
)
713 :tags
(plist-get export-plist
:tags-export
)
714 :priority
(plist-get export-plist
:priority-export
)
715 :footnotes
(plist-get export-plist
:footnotes-export
)
716 :timestamps
(plist-get export-plist
:timestamps-export
)
717 :todo-keywords
(plist-get export-plist
:todo-keywords-export
)
718 :verbatim-multiline t
719 :select-tags
(plist-get export-plist
:select-tags-export
)
720 :exclude-tags
(plist-get export-plist
:exclude-tags-export
)
723 (plist-get export-plist
:archived-trees-export
)
724 :add-text
(plist-get opt-plist
:text
))
726 ;; export-generic plist variables
727 (withtags (plist-get export-plist
:tags-export
))
728 (tagsintoc (plist-get export-plist
:toc-tags-export
))
729 (tocnotagsstr (or (plist-get export-plist
:toc-tags-none-string
) ""))
730 (tocdepth (plist-get export-plist
:toc-indent-depth
))
731 (tocindentchar (plist-get export-plist
:toc-indent-char
))
732 (tocsecnums (plist-get export-plist
:toc-section-numbers
))
733 (tocsecnumform (plist-get export-plist
:toc-section-number-format
))
734 (tocformat (plist-get export-plist
:toc-format
))
735 (tocformtodo (plist-get export-plist
:toc-format-with-todo
))
736 (tocprefix (plist-get export-plist
:toc-prefix
))
737 (tocsuffix (plist-get export-plist
:toc-suffix
))
738 (bodyfixedpre (plist-get export-plist
:body-line-fixed-prefix
))
739 (bodyfixedsuf (plist-get export-plist
:body-line-fixed-suffix
))
740 (bodyfixedform (or (plist-get export-plist
:body-line-fixed-format
)
742 (listprefix (plist-get export-plist
:body-list-prefix
))
743 (listsuffix (plist-get export-plist
:body-list-suffix
))
744 (listformat (or (plist-get export-plist
:body-list-format
) "%s\n"))
746 (plist-get export-plist
:body-number-list-leave-number
))
747 (numlistprefix (plist-get export-plist
:body-number-list-prefix
))
748 (numlistsuffix (plist-get export-plist
:body-number-list-suffix
))
750 (or (plist-get export-plist
:body-number-list-format
) "%s\n"))
752 (or (plist-get export-plist
:body-list-checkbox-todo
) "\\1"))
754 (or (plist-get export-plist
:body-list-checkbox-done
) "\\1"))
756 (or (plist-get export-plist
:body-list-checkbox-half
) "\\1"))
758 (or (plist-get export-plist
:body-list-checkbox-todo-end
) ""))
760 (or (plist-get export-plist
:body-list-checkbox-done-end
) ""))
762 (or (plist-get export-plist
:body-list-checkbox-half-end
) ""))
764 (or (plist-get export-plist
:body-table-start
) ""))
766 (or (plist-get export-plist
:body-table-end
) ""))
768 (or (plist-get export-plist
:body-table-row-start
) ""))
770 (or (plist-get export-plist
:body-table-row-end
) ""))
772 (or (plist-get export-plist
:body-table-cell-start
) ""))
774 (or (plist-get export-plist
:body-table-cell-end
) ""))
775 (bodytablefirstcellstart
776 (or (plist-get export-plist
:body-table-first-cell-start
) ""))
777 (bodytableinteriorcellstart
778 (or (plist-get export-plist
:body-table-interior-cell-start
) ""))
779 (bodytableinteriorcellend
780 (or (plist-get export-plist
:body-table-interior-cell-end
) ""))
781 (bodytablelastcellend
782 (or (plist-get export-plist
:body-table-last-cell-end
) ""))
784 (or (plist-get export-plist
:body-table-hline-start
) " \\1"))
786 (or (plist-get export-plist
:body-table-hline-end
) ""))
790 (bodynewline-paragraph (plist-get export-plist
:body-newline-paragraph
))
791 (bodytextpre (plist-get export-plist
:body-text-prefix
))
792 (bodytextsuf (plist-get export-plist
:body-text-suffix
))
793 (bodylinewrap (plist-get export-plist
:body-line-wrap
))
794 (bodylineform (or (plist-get export-plist
:body-line-format
) "%s"))
795 (blockquotestart (or (plist-get export-plist
:blockquote-start
) "\n\n\t"))
796 (blockquoteend (or (plist-get export-plist
:blockquote-end
) "\n\n"))
798 ;; dynamic variables used heinously in fontification
799 ;; not referenced locally...
800 (format-boldify (plist-get export-plist
:bold-format
))
801 (format-italicize (plist-get export-plist
:italic-format
))
802 (format-underline (plist-get export-plist
:underline-format
))
803 (format-strikethrough (plist-get export-plist
:strikethrough-format
))
804 (format-code (plist-get export-plist
:code-format
))
805 (format-verbatim (plist-get export-plist
:verbatim-format
))
809 thetoc toctags have-headings first-heading-pos
810 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
812 (let ((inhibit-read-only t
))
814 (remove-text-properties (point-min) (point-max)
815 '(:org-license-to-kill t
))))
817 (setq org-min-level
(org-get-min-level lines level-offset
))
818 (setq org-last-level org-min-level
)
819 (org-init-section-numbers)
821 (find-file-noselect filename
)
823 (setq lang-words
(or (assoc language org-export-language-setup
)
824 (assoc "en" org-export-language-setup
)))
825 (switch-to-buffer-other-window buffer
)
828 ;; create local variables for all options, to make sure all called
829 ;; functions get the correct information
831 (set (make-local-variable (nth 2 x
))
832 (plist-get opt-plist
(car x
))))
833 org-export-plist-vars
)
834 (org-set-local 'org-odd-levels-only odd
)
835 (setq umax
(if arg
(prefix-numeric-value arg
)
836 org-export-headline-levels
))
842 (org-export-generic-header title export-plist
847 (if (and (or author email
)
848 (plist-get export-plist
:author-export
))
849 (insert (concat (nth 1 lang-words
) ": " (or author
"")
850 (if email
(concat " <" email
">") "")
854 ((and date
(string-match "%" date
))
855 (setq date
(format-time-string date
)))
857 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
859 (if (and date
(plist-get export-plist
:date-export
))
861 (org-export-generic-header date export-plist
866 ;; export the table of contents first
867 (if (plist-get export-plist
:toc-export
)
870 (org-export-generic-header (nth 3 lang-words
) export-plist
877 (push tocprefix thetoc
))
879 (mapc '(lambda (line)
880 (if (string-match org-todo-line-regexp line
)
881 ;; This is a headline
883 (setq have-headings t
)
884 (setq level
(- (match-end 1) (match-beginning 1)
886 level
(org-tr-level level
)
887 txt
(match-string 3 line
)
889 (or (and org-export-mark-todo-in-toc
891 (not (member (match-string 2 line
)
894 (and org-export-mark-todo-in-toc
896 (org-search-todo-below
898 (setq txt
(org-html-expand-for-generic txt
))
900 (while (string-match org-bracket-link-regexp txt
)
903 (match-string (if (match-end 2) 3 1) txt
)
906 (if (and (not tagsintoc
)
908 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
910 (setq txt
(replace-match "" t t txt
))
911 ; include tags but formated
913 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
918 (org-export-generic-header
920 export-plist
:toc-tags-prefix
921 :toc-tags-format
:toc-tags-suffix
))
923 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
925 (setq txt
(replace-match "" t t txt
)))
926 (setq toctags tocnotagsstr
)))
928 (if (string-match quote-re0 txt
)
929 (setq txt
(replace-match "" t t txt
)))
931 (if (<= level umax-toc
)
937 (* (max 0 (- level org-min-level
)) tocdepth
)
941 (format tocsecnumform
942 (org-section-number level
))
946 (if todo tocformtodo tocformat
)
952 (setq org-last-level level
))
956 (push tocsuffix thetoc
))
957 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
959 (org-init-section-numbers)
960 (org-export-generic-check-section "top")
961 (while (setq line
(pop lines
))
962 (when (and link-buffer
(string-match org-outline-regexp-bol line
))
963 (org-export-generic-push-links (nreverse link-buffer
))
964 (setq link-buffer nil
))
966 ;; Remove the quoted HTML tags.
968 (setq line
(org-html-expand-for-generic line
))
969 ;; Replace links with the description when possible
971 (while (string-match org-bracket-link-regexp line
)
972 (setq link
(match-string 1 line
)
973 desc0
(match-string 3 line
)
974 desc
(or desc0
(match-string 1 line
)))
975 (if (and (> (length link
) 8)
976 (equal (substring link
0 8) "coderef:"))
977 (setq line
(replace-match
978 (format (org-export-get-coderef-format (substring link
8) desc
)
981 org-export-code-refs
)))
983 (setq rpl
(concat "["
984 (or (match-string 3 line
) (match-string 1 line
))
986 (when (and desc0
(not (equal desc0 link
)))
987 (if org-export-generic-links-to-notes
988 (push (cons desc0 link
) link-buffer
)
989 (setq rpl
(concat rpl
" (" link
")")
990 wrap
(+ (length line
) (- (length (match-string 0 line
)))
992 (setq line
(replace-match rpl t t line
))))
994 (setq line
(org-translate-time line
)))
996 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
1000 (org-export-generic-check-section "headline")
1002 (setq first-heading-pos
(or first-heading-pos
(point)))
1003 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
1005 txt
(match-string 2 line
))
1006 (org-generic-level-start level old-level txt umax export-plist lines
)
1007 (setq old-level level
))
1009 ((and org-export-with-tables
1010 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
1014 (org-export-generic-check-section "table")
1016 (if (not table-open
)
1018 (setq table-open t table-buffer nil
))
1019 ;; Accumulate table lines
1020 (setq table-buffer
(cons line table-buffer
))
1021 (when (or (not lines
)
1022 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
1024 (setq table-open nil
1025 table-buffer
(nreverse table-buffer
))
1028 (org-fix-indentation x org-generic-current-indentation
))
1029 (org-format-table-generic table-buffer
)
1032 ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
)
1034 ;; pre-formatted text
1036 (setq line
(replace-match "\\1" nil nil line
))
1038 (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf
)
1040 (insert (format bodyfixedform line
)))
1042 ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line
)
1043 ;; if the bullet list item is an asterisk, the leading space is /mandatory/
1045 (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line
))
1048 ;; TODO: nested lists
1050 ;; first add a line break between any previous paragraph or line item and this
1052 (when bodynewline-paragraph
1053 (insert bodynewline-paragraph
))
1055 ;; I believe this gets rid of leading whitespace.
1056 (setq line
(replace-match "" nil nil line
))
1058 ;; won't this insert the suffix /before/ the last line of the list?
1059 ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
1060 ;; unless 'org-empty-line-terminates-plain-lists' is true?
1061 (org-export-generic-check-section "liststart" listprefix listsuffix
)
1063 ;; deal with checkboxes
1065 ((string-match "^\\(\\[ \\]\\)[ \t]*" line
)
1066 (setq line
(concat (replace-match listchecktodo nil nil line
)
1068 ((string-match "^\\(\\[X\\]\\)[ \t]*" line
)
1069 (setq line
(concat (replace-match listcheckdone nil nil line
)
1071 ((string-match "^\\(\\[/\\]\\)[ \t]*" line
)
1072 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1076 (insert (format listformat
(org-export-generic-fontify line
))))
1077 ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line
)
1079 ;; numbered list item
1081 ;; TODO: nested lists
1083 (setq line
(replace-match (if numlistleavenum
"\\2" "") nil nil line
))
1085 (org-export-generic-check-section "numliststart"
1086 numlistprefix numlistsuffix
)
1088 ;; deal with checkboxes
1089 ;; TODO: whoops; leaving the numbers is a problem for ^ matching
1091 ((string-match "\\(\\[ \\]\\)[ \t]*" line
)
1092 (setq line
(concat (replace-match listchecktodo nil nil line
)
1094 ((string-match "\\(\\[X\\]\\)[ \t]*" line
)
1095 (setq line
(concat (replace-match listcheckdone nil nil line
)
1097 ((string-match "\\(\\[/\\]\\)[ \t]*" line
)
1098 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1102 (insert (format numlistformat
(org-export-generic-fontify line
))))
1104 ((equal line
"ORG-BLOCKQUOTE-START")
1105 (setq line blockquotestart
))
1106 ((equal line
"ORG-BLOCKQUOTE-END")
1107 (setq line blockquoteend
))
1108 ((string-match "^\\s-*$" line
)
1110 (if bodynewline-paragraph
1111 (insert bodynewline-paragraph
)))
1116 (org-export-generic-check-section "body" bodytextpre bodytextsuf
)
1119 (org-export-generic-fontify line
))
1121 ;; XXX: properties? list?
1122 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
1123 (setq line
(replace-match "\\1\\3:" t nil line
)))
1125 (setq line
(org-fix-indentation line org-generic-current-indentation
))
1127 ;; Remove forced line breaks
1128 (if (string-match "\\\\\\\\[ \t]*$" line
)
1129 (setq line
(replace-match "" t t line
)))
1132 ;; XXX: was dependent on wrap var which was calculated by???
1133 (if (> (length line
) bodylinewrap
)
1135 (org-export-generic-wrap line bodylinewrap
))
1137 (insert (format bodylineform line
)))))
1139 ;; if we're at a level > 0; insert the closing body level stuff
1141 (while (> (- level counter
) 0)
1143 (org-export-generic-format export-plist
:body-section-suffix
0
1145 (setq counter
(1+ counter
))))
1147 (org-export-generic-check-section "bottom")
1149 (org-export-generic-push-links (nreverse link-buffer
))
1153 ;; insert the table of contents
1155 (goto-char (point-min))
1156 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
1158 (goto-char (match-beginning 0))
1160 (goto-char first-heading-pos
))
1161 (mapc 'insert thetoc
)
1162 (or (looking-at "[ \t]*\n[ \t]*\n")
1165 ;; Convert whitespace place holders
1166 (goto-char (point-min))
1168 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
1169 (setq end
(next-single-property-change beg
'org-whitespace
))
1171 (delete-region beg end
)
1172 (insert (make-string (- end beg
) ?\
))))
1176 ;; remove display and invisible chars
1178 (goto-char (point-min))
1179 (while (setq beg
(next-single-property-change (point) 'display
))
1180 (setq end
(next-single-property-change beg
'display
))
1181 (delete-region beg end
)
1184 (goto-char (point-min))
1185 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
1186 (setq end
(next-single-property-change beg
'org-cwidth
))
1187 (delete-region beg end
)
1189 (goto-char (point-min))))
1192 (defun org-export-generic-format (export-plist prop
&optional len n reverse
)
1193 "converts a property specification to a string given types of properties
1195 The EXPORT-PLIST should be defined as the lookup plist.
1196 The PROP should be the property name to search for in it.
1197 LEN is set to the length of multi-characters strings to generate (or 0)
1199 REVERSE means to reverse the list if the plist match is a list
1201 (let* ((prefixtype (plist-get export-plist prop
))
1204 ((null prefixtype
) "")
1205 ((and len
(char-or-string-p prefixtype
) (not (stringp prefixtype
)))
1206 ;; sequence of chars
1207 (concat (make-string len prefixtype
) "\n"))
1208 ((stringp prefixtype
)
1210 ((and n
(listp prefixtype
))
1212 (setq prefixtype
(reverse prefixtype
)))
1213 (setq subtype
(if (> n
(length prefixtype
))
1214 (car (last prefixtype
))
1215 (nth (1- n
) prefixtype
)))
1216 (if (stringp subtype
)
1218 (concat (make-string len subtype
) "\n")))
1222 (defun org-export-generic-header (header export-plist
1223 prefixprop formatprop postfixprop
1224 &optional n reverse
)
1225 "convert a header to an output string given formatting property names"
1226 (let* ((formatspec (plist-get export-plist formatprop
))
1227 (len (length header
)))
1229 (org-export-generic-format export-plist prefixprop len n reverse
)
1230 (format (or formatspec
"%s") header
)
1231 (org-export-generic-format export-plist postfixprop len n reverse
))
1234 (defun org-export-generic-preprocess (parameters)
1235 "Do extra work for ASCII export"
1236 ;; Put quotes around verbatim text
1237 (goto-char (point-min))
1238 (while (re-search-forward org-verbatim-re nil t
)
1239 (goto-char (match-end 2))
1240 (backward-delete-char 1) (insert "'")
1241 (goto-char (match-beginning 2))
1242 (delete-char 1) (insert "`")
1243 (goto-char (match-end 2)))
1244 ;; Remove target markers
1245 (goto-char (point-min))
1246 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
1247 (replace-match "\\1\\2")))
1249 (defun org-html-expand-for-generic (line)
1250 "Handle quoted HTML for ASCII export."
1251 (if org-export-html-expand
1252 (while (string-match "@<[^<>\n]*>" line
)
1253 ;; We just remove the tags for now.
1254 (setq line
(replace-match "" nil nil line
))))
1257 (defun org-export-generic-wrap (line where
)
1258 "Wrap LINE at or before WHERE."
1259 (let* ((ind (org-get-indentation line
))
1260 (indstr (make-string ind ?\
))
1264 (while (> len where
)
1266 (loop for i from where downto
(/ where
2) do
1267 (and (equal (aref line i
) ?\
)
1274 (if didfirst indstr
"")
1275 (substring line
0 pos
)
1278 (setq line
(substring line
(1+ pos
)))
1279 (setq len
(length line
)))
1280 (setq result
(concat result line
))
1282 (concat result indstr line
)))
1284 (defun org-export-generic-push-links (link-buffer)
1285 "Push out links in the buffer."
1287 ;; We still have links to push out.
1293 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
1294 (setq ind
(or (match-string 2)
1295 (make-string (length (match-string 3)) ?\
)))))
1296 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
1300 (defun org-generic-level-start (level old-level title umax export-plist
1302 "Insert a new level in a generic export."
1303 (let ((n (- level umax
1))
1305 (diff (- level old-level
)) (counter 0)
1306 (secnums (plist-get export-plist
:body-header-section-numbers
))
1308 (plist-get export-plist
:body-header-section-number-format
))
1310 (unless org-export-with-tags
1311 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
1312 (setq title
(replace-match "" t t title
))))
1316 ((> level old-level
)
1317 (while (< (+ old-level counter
) (1- level
))
1319 (org-export-generic-format export-plist
:body-section-prefix
0
1320 (+ old-level counter
)))
1321 (setq counter
(1+ counter
))
1324 ((< level old-level
)
1325 (while (> (- old-level counter
) (1- level
))
1327 (org-export-generic-format export-plist
:body-section-suffix
0
1328 (- old-level counter
)))
1329 (setq counter
(1+ counter
))
1332 ((= level old-level
)
1334 (org-export-generic-format export-plist
:body-section-suffix
0 level
))
1338 (org-export-generic-format export-plist
:body-section-prefix
0 level
))
1340 (if (and org-export-with-section-numbers
1342 (or (not (numberp secnums
))
1345 (concat (format (or secnumformat
"%s ")
1346 (org-section-number level
)) title
)))
1348 ;; handle tags and formatting
1350 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title
)
1352 (if (plist-get export-plist
:body-tags-export
)
1353 (setq tagstring
(org-export-generic-header (match-string 1 title
)
1357 :body-tags-suffix
)))
1358 (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title
)
1359 (setq title
(replace-match "" t t title
)))
1360 (setq tagstring
(plist-get export-plist
:body-tags-none-string
)))
1363 (org-export-generic-header title export-plist
1364 :body-section-header-prefix
1365 :body-section-header-format
1366 :body-section-header-suffix
1371 (setq org-generic-current-indentation
'(0 .
0))))
1373 (defun org-insert-centered (s &optional underline
)
1374 "Insert the string S centered and underline it with character UNDERLINE."
1375 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
1376 (insert (make-string ind ?\
) s
"\n")
1378 (insert (make-string ind ?\
)
1379 (make-string (string-width s
) underline
)
1382 (defvar org-table-colgroup-info nil
)
1383 (defun org-format-table-generic (lines)
1384 "Format a table for ascii export."
1386 (setq lines
(org-split-string lines
"\n")))
1387 (if (not (string-match "^[ \t]*|" (car lines
)))
1388 ;; Table made by table.el - test for spanning
1391 ;; A normal org table
1392 ;; Get rid of hlines at beginning and end
1393 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1394 (setq lines
(nreverse lines
))
1395 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1396 (setq lines
(nreverse lines
))
1397 (when org-export-table-remove-special-lines
1398 ;; Check if the table has a marking column. If yes remove the
1399 ;; column and the special lines
1400 (setq lines
(org-table-clean-before-export lines
)))
1401 ;; Get rid of the vertical lines except for grouping
1402 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
1403 (rtn (list bodytablestart
)) line vl1 start
)
1404 (while (setq line
(pop lines
))
1405 (setq line
(concat bodytablerowstart line
))
1406 (if (string-match org-table-hline-regexp line
)
1407 (and (string-match "|\\(.*\\)|" line
)
1408 (setq line
(replace-match (concat bodytablehlinestart bodytablehlineend
) t nil line
)))
1409 (setq start
0 vl1 vl
)
1410 (if (string-match "|\\(.*\\)|" line
)
1411 (setq line
(replace-match (concat bodytablefirstcellstart bodytablecellstart
" \\1 " bodytablecellend bodytablelastcellend
) t nil line
)))
1412 (while (string-match "|" line start
)
1413 (setq start
(+ (match-end 0) (length (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart
))))
1414 (or (pop vl1
) (setq line
(replace-match (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart
) t t line
)))))
1415 (setq line
(concat line bodytablerowend
))
1417 (setq rtn
(cons bodytableend rtn
))
1420 (defun org-colgroup-info-to-vline-list (info)
1423 (setq last new new
(pop info
))
1424 (if (or (memq last
'(:end
:startend
))
1425 (memq new
'(:start
:startend
)))
1428 (setq vl
(nreverse vl
))
1429 (and vl
(setcar vl nil
))
1433 ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
1434 (defvar org-export-generic-emphasis-alist
1435 '(("*" format-boldify nil
)
1436 ("/" format-italicize nil
)
1437 ("_" format-underline nil
)
1438 ("+" format-strikethrough nil
)
1440 ("~" format-verbatim t
))
1441 "Alist of org format -> formatting variables for fontification.
1442 Each element of the list is a list of three elements.
1443 The first element is the character used as a marker for fontification.
1444 The second element is a variable name, set in org-export-generic. That
1445 variable will be dereferenced to obtain a formatting string to wrap
1446 fontified text with.
1447 The third element decides whether to protect converted text from other
1450 ;;; Cargo-culted from the latex translation. I couldn't figure out how
1451 ;;; to keep the structure since the generic export operates on lines, rather
1452 ;;; than on a buffer as in the latex export, meaning that none of the
1453 ;;; search forward code could be kept. This led me to rewrite the
1454 ;;; whole thing recursively. A huge lose for efficiency (potentially),
1455 ;;; but I couldn't figure out how to make the looping work.
1456 ;;; Worse, it's /doubly/ recursive, because this function calls
1457 ;;; org-export-generic-emph-format, which can call it recursively...
1458 ;;; [2010/05/20:rpg]
1459 (defun org-export-generic-fontify (string)
1460 "Convert fontification according to generic rules."
1461 (if (string-match org-emph-re string
)
1462 ;; The match goes one char after the *string*, except at the end of a line
1463 (let ((emph (assoc (match-string 3 string
)
1464 org-export-generic-emphasis-alist
))
1465 (beg (match-beginning 0))
1466 (end (match-end 0)))
1468 (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
1469 (match-string 3 string
)))
1470 ;; now we need to determine whether we have strikethrough or
1471 ;; a list, which is a bit nasty
1472 (if (and (equal (match-string 3 string
) "+")
1474 (string-match "\\`-+\\'" (match-string 4 string
))))
1475 ;; a list --- skip this match and recurse on the point after the
1476 ;; first emph char...
1477 (concat (substring string
0 (1+ (match-beginning 3)))
1478 (org-export-generic-fontify (substring string
(match-beginning 3))))
1479 (concat (substring string
0 beg
) ;; part before the match
1480 (match-string 1 string
)
1481 (org-export-generic-emph-format (second emph
)
1482 (match-string 4 string
)
1484 (or (match-string 5 string
) "")
1485 (org-export-generic-fontify (substring string end
)))))
1488 (defun org-export-generic-emph-format (format-varname string protect
)
1489 "Return a string that results from applying the markup indicated by
1490 FORMAT-VARNAME to STRING."
1491 (let ((format (symbol-value format-varname
)))
1492 (let ((string-to-emphasize
1495 (org-export-generic-fontify string
))))
1497 (format format string-to-emphasize
)
1498 string-to-emphasize
))))
1500 (provide 'org-generic
)
1501 (provide 'org-export-generic
)
1503 ;;; org-export-generic.el ends here