1 ;; org-export-generic.el --- Export frameworg with custom backends
3 ;; Copyright (C) 2009-2011 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
91 ;; * optmization (many plist extracts should be in let vars)
92 ;; * define defcustom spec for the specifier list
93 ;; * fonts: at least monospace is not handled at all here.
95 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 (defgroup org-export-generic nil
103 "Options specific for ASCII export of Org-mode files."
104 :tag
"Org Export ASCII"
107 (defcustom org-export-generic-links-to-notes t
108 "Non-nil means convert links to notes before the next headline.
109 When nil, the link will be exported in place. If the line becomes long
110 in this way, it will be wrapped."
111 :group
'org-export-generic
115 (defvar org-generic-current-indentation nil
) ; For communication
117 (defvar org-generic-alist
120 ;; generic DEMO exporter
122 ;; (this tries to use every specifier for demo purposes)
128 :header-prefix
"<header>\n"
129 :header-suffix
"</header>\n"
138 :title-format
"<h1>%s</h1>\n"
142 :date-prefix
"<date>"
143 :date-format
"<br /><b>Date:</b> <i>%s</i><br />"
144 :date-suffix
"</date>\n\n"
147 :toc-header-prefix
"<tocname>\n"
148 :toc-header-format
"__%s__\n"
149 :toc-header-suffix
"</tocname>\n"
151 :toc-prefix
"<toc>\n"
152 :toc-suffix
"</toc>\n"
154 :toc-section-numbers t
155 :toc-section-number-format
"\#(%s) "
157 :toc-format-with-todo
"!!%s!!\n"
162 :toc-tags-prefix
" <tags>"
163 :toc-tags-format
"*%s*"
164 :toc-tags-suffix
"</tags>\n"
165 :toc-tags-none-string
"\n"
167 :body-header-section-numbers
3 ; t = all, nil = none
169 ; lists indicate different things per level
170 ; list contents or straight value can either be a
171 ; ?x char reference for printing strings that match the header len
172 ; "" string to print directly
173 :body-section-header-prefix
("<h1>" "<h2>" "<h3>"
174 "<h4>" "<h5>" "<h6>")
175 :body-section-header-format
"%s"
176 :body-section-header-suffix
("</h1>\n" "</h2>\n" "</h3>\n"
177 "</h4>\n" "</h5>\n" "</h6>\n")
181 :todo-keywords-export t
184 :body-tags-prefix
" <tags>"
185 :body-tags-suffix
"</tags>\n"
187 ; section prefixes/suffixes can be direct strings or lists as well
188 :body-section-prefix
"<secprefix>\n"
189 :body-section-suffix
"</secsuffix>\n"
190 ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
191 ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
194 ; if preformated text should be included (eg, : prefixed)
195 :body-line-export-preformated t
196 :body-line-fixed-prefix
"<pre>\n"
197 :body-line-fixed-suffix
"\n</pre>\n"
198 :body-line-fixed-format
"%s\n"
201 :body-list-prefix
"<list>\n"
202 :body-list-suffix
"</list>\n"
203 :body-list-format
"<li>%s</li>\n"
205 :body-number-list-prefix
"<ol>\n"
206 :body-number-list-suffix
"</ol>\n"
207 :body-number-list-format
"<li>%s</li>\n"
208 :body-number-list-leave-number t
210 :body-list-checkbox-todo
"<checkbox type=\"todo\">"
211 :body-list-checkbox-todo-end
"</checkbox (todo)>"
212 :body-list-checkbox-done
"<checkbox type=\"done\">"
213 :body-list-checkbox-done-end
"</checkbox (done)>"
214 :body-list-checkbox-half
"<checkbox type=\"half\">"
215 :body-list-checkbox-half-end
"</checkbox (half)>"
221 :body-line-format
"%s"
222 :body-line-wrap
60 ; wrap at 60 chars
224 ; print above and below all body parts
225 :body-text-prefix
"<p>\n"
226 :body-text-suffix
"</p>\n"
233 ;; (close to the original ascii specifier)
248 :date-format
"Date: %s\n"
251 :toc-header-prefix
""
252 :toc-header-format
"%s\n"
253 :toc-header-suffix ?
=
256 :toc-section-numbers t
257 :toc-section-number-format
"%s "
259 :toc-format-with-todo
"%s (*)\n"
263 :body-header-section-numbers
3
264 :body-section-prefix
"\n"
266 ; :body-section-header-prefix "\n"
267 ; :body-section-header-format "%s\n"
268 ; :body-section-header-suffix (?\$ ?\# ?^ ?\~ ?\= ?\-)
270 :body-section-header-prefix
("" "" "" "* " " + " " - ")
271 :body-section-header-format
"%s\n"
272 :body-section-header-suffix
(?~ ?
= ?-
"\n" "\n" "\n")
274 ; :body-section-marker-prefix ""
275 ; :body-section-marker-chars (?\$ ?\# ?^ ?\~ ?\= ?\-)
276 ; :body-section-marker-suffix "\n"
278 :body-line-export-preformated t
279 :body-line-format
"%s\n"
282 ; :body-text-prefix "<t>\n"
283 ; :body-text-suffix "</t>\n"
286 :body-bullet-list-prefix
(?
* ?
+ ?-
)
287 ; :body-bullet-list-suffix (?* ?+ ?-)
300 :title-format
"= %s =\n"
306 :body-header-section-numbers nil
307 :body-section-prefix
"\n"
309 :body-section-header-prefix
("= " "== " "=== "
310 "==== " "===== " "====== ")
311 :body-section-header-suffix
(" =\n\n" " ==\n\n" " ===\n\n"
312 " ====\n\n" " =====\n\n" " ======\n\n")
314 :body-line-export-preformated t
;; yes/no/maybe???
315 :body-line-format
"%s\n"
318 :body-line-fixed-format
" %s\n"
320 :body-list-format
"* %s\n"
321 :body-number-list-format
"# %s\n"
323 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** ")
326 ;; internet-draft .xml for xml2rfc exporter
329 ;; this tries to use every specifier for demo purposes
333 :title-prefix
"<?xml version=\"1.0\"\?>
334 <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
335 <!ENTITY rfcs PUBLIC '' 'blah'>
336 <?rfc strict=\"yes\" ?>
338 <?rfc tocdepth=\"4\" ?>
339 <?rfc symrefs=\"yes\" ?>
340 <?rfc compact=\"yes\" ?>
341 <?rfc subcompact=\"no\" ?>
342 <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
345 :title-format
"<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
346 :title-suffix
"<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
347 <organization>Comany, Inc..</organization>
360 <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
361 <area>Operations and Management</area>
362 <workgroup>FIXME</workgroup>
368 :body-header-section-numbers nil
370 :body-section-header-format
"<section title=\"%s\">\n"
371 :body-section-suffix
"</section>\n"
373 ; if preformated text should be included (eg, : prefixed)
374 :body-line-export-preformated t
375 :body-line-fixed-prefix
"<figure>\n<artwork>\n"
376 :body-line-fixed-suffix
"\n</artwork>\n</figure>\n"
379 :body-line-format
"%s"
382 ; print above and below all body parts
383 :body-text-prefix
"<t>\n"
384 :body-text-suffix
"</t>\n"
386 :body-list-prefix
"<list style=\"symbols\">\n"
387 :body-list-suffix
"</list>\n"
388 :body-list-format
"<t>%s</t>\n"
395 ;; lifted from wikipedia exporter
399 :title-format
"= %s =\n"
405 :body-header-section-numbers nil
406 :body-section-prefix
"\n"
408 :body-section-header-prefix
(" == " " === " " ==== "
409 " ===== " " ====== " " ======= ")
410 :body-section-header-suffix
(" ==\n\n" " ===\n\n" " ====\n\n"
411 " =====\n\n" " ======\n\n" " =======\n\n")
413 :body-line-export-preformated t
;; yes/no/maybe???
414 :body-line-format
"%s\n"
417 :body-line-fixed-format
" %s\n"
419 :body-list-format
" * %s\n"
420 :body-number-list-format
" # %s\n"
421 ;; :body-list-prefix "LISTSTART"
422 ;; :body-list-suffix "LISTEND"
424 ;; this is ignored! [2010/02/02:rpg]
425 :body-bullet-list-prefix
("* " "** " "*** " "**** " "***** ")
431 ;; lifted from wikipedia exporter
435 :title-format
"-= %s =-\n"
441 :body-header-section-numbers nil
442 :body-section-prefix
"\n"
444 :body-section-header-prefix
("! " "!! " "!!! " "!!!! "
445 "!!!!! " "!!!!!! " "!!!!!!! ")
446 :body-section-header-suffix
(" \n" " \n" " \n"
450 :body-line-export-preformated t
;; yes/no/maybe???
451 :body-line-format
"%s "
454 :body-line-fixed-format
" %s\n"
456 :body-list-format
"* %s\n"
457 :body-number-list-format
"# %s\n"
458 ;; :body-list-prefix "LISTSTART"
459 ;; :body-list-suffix "LISTEND"
460 :blockquote-start
"\n^\n"
461 :blockquote-end
"^\n\n"
462 :body-newline-paragraph
"\n"
463 :bold-format
"__%s__"
464 :italic-format
"''%s''"
465 :underline-format
"===%s==="
466 :strikethrough-format
"--%s--"
467 :code-format
"-+%s+-"
468 :verbatim-format
"~pp~%s~/pp~"
471 "A assoc list of property lists to specify export definitions"
474 (setq org-generic-export-type
"demo")
476 (defvar org-export-generic-section-type
"")
477 (defvar org-export-generic-section-suffix
"")
480 (defun org-set-generic-type (type definition
)
481 "Adds a TYPE and DEFINITION to the existing list of defined generic
483 (aput 'org-generic-alist type definition
))
485 ;;; helper functions for org-set-generic-type
486 (defvar org-export-generic-keywords nil
)
487 (defmacro* def-org-export-generic-keyword
(keyword
490 "Define KEYWORD as a legitimate element for inclusion in
491 the body of an org-set-generic-type definition."
493 (pushnew ,keyword org-export-generic-keywords
)
494 ;; TODO: push the documentation and type information
495 ;; somewhere where it will do us some good.
498 (def-org-export-generic-keyword :body-newline-paragraph
499 :documentation
"Bound either to NIL or to a pattern to be
500 inserted in the output for every blank line in the input.
501 The intention is to handle formats where text is flowed, and
502 newlines are interpreted as significant \(e.g., as indicating
503 preformatted text\). A common non-nil value for this keyword
504 is \"\\n\". Should typically be combined with a value for
505 :body-line-format that does NOT end with a newline."
508 ;;; fontification keywords
509 (def-org-export-generic-keyword :bold-format
)
510 (def-org-export-generic-keyword :italic-format
)
511 (def-org-export-generic-keyword :underline-format
)
512 (def-org-export-generic-keyword :strikethrough-format
)
513 (def-org-export-generic-keyword :code-format
)
514 (def-org-export-generic-keyword :verbatim-format
)
519 (defun org-export-generic-remember-section (type suffix
&optional prefix
)
520 (setq org-export-generic-section-type type
)
521 (setq org-export-generic-section-suffix suffix
)
526 (defun org-export-generic-check-section (type &optional prefix suffix
)
527 "checks to see if type is already in use, or we're switching parts
528 If we're switching, then insert a potentially previously remembered
529 suffix, and insert the current prefix immediately and then save the
530 suffix a later change time."
532 (when (not (equal type org-export-generic-section-type
))
533 (if org-export-generic-section-suffix
534 (insert org-export-generic-section-suffix
))
535 (setq org-export-generic-section-type type
)
536 (setq org-export-generic-section-suffix suffix
)
541 (defun org-export-generic (arg)
542 "Export the outline as generic output.
543 If there is an active region, export only the region.
544 The prefix ARG specifies how many levels of the outline should become
545 underlined headlines. The default is 3."
547 (setq-default org-todo-line-regexp org-todo-line-regexp
)
548 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
549 (org-infile-export-plist)))
550 (region-p (org-region-active-p))
551 (rbeg (and region-p
(region-beginning)))
552 (rend (and region-p
(region-end)))
557 (and (org-at-heading-p)
558 (>= (org-end-of-subtree t t
) rend
)))))
559 (level-offset (if subtree-p
562 (+ (funcall outline-level
)
563 (if org-odd-levels-only
1 0)))
565 (opt-plist (setq org-export-opt-plist
567 (org-export-add-subtree-options opt-plist rbeg
)
571 (bogus (mapc (lambda (x)
573 (concat helpstart
"\["
575 (plist-get (cdr x
) :key-binding
))
579 (help (concat helpstart
"
581 \[ ] the current setting of the org-generic-export-type variable
589 (plist-get (cdr x
) :key-binding
)
592 (list (list ?
"default"))))
596 ;; read in the type to use
600 (save-window-excursion
601 (delete-other-windows)
602 (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
604 (org-fit-window-to-buffer (get-buffer-window
605 "*Org Export/Generic Styles Help*"))
606 (message "Select command: ")
607 (setq r1
(read-char-exclusive))))
608 (setq r2
(if (< r1
27) (+ r1
96) r1
))
609 (unless (setq ass
(cadr (assq r2 cmds
)))
610 (error "No command associated with key %c" r1
))
613 (if (equal ass
"default") org-generic-export-type ass
)
614 org-generic-alist
))))
616 (custom-times org-display-custom-times
)
617 (org-generic-current-indentation '(0 .
0))
618 (level 0) (old-level 0) line txt lastwastext
621 (case-fold-search nil
)
622 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
623 (filesuffix (or (plist-get export-plist
:file-suffix
) ".foo"))
624 (filename (concat (file-name-as-directory
625 (org-export-directory :ascii opt-plist
))
626 (file-name-sans-extension
628 (org-entry-get (region-beginning)
629 "EXPORT_FILE_NAME" t
))
630 (file-name-nondirectory bfname
)))
632 (filename (if (equal (file-truename filename
)
633 (file-truename bfname
))
634 (concat filename filesuffix
)
636 (buffer (find-file-noselect filename
))
637 (org-levels-open (make-vector org-level-max nil
))
638 (odd org-odd-levels-only
)
639 (date (plist-get opt-plist
:date
))
640 (author (plist-get opt-plist
:author
))
641 (title (or (and subtree-p
(org-export-get-title-from-subtree))
642 (plist-get opt-plist
:title
)
644 (plist-get opt-plist
:skip-before-1st-heading
))
645 (org-export-grab-title-from-buffer))
646 (file-name-sans-extension
647 (file-name-nondirectory bfname
))))
648 (email (plist-get opt-plist
:email
))
649 (language (plist-get opt-plist
:language
))
650 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
651 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
656 (if (org-region-active-p) (region-beginning) (point-min))
657 (if (org-region-active-p) (region-end) (point-max))))
658 (org-export-current-backend 'org-export-generic
)
659 (lines (org-split-string
660 (org-export-preprocess-string
663 :skip-before-1st-heading
664 (plist-get opt-plist
:skip-before-1st-heading
)
665 :drawers
(plist-get export-plist
:drawers-export
)
666 :tags
(plist-get export-plist
:tags-export
)
667 :priority
(plist-get export-plist
:priority-export
)
668 :footnotes
(plist-get export-plist
:footnotes-export
)
669 :timestamps
(plist-get export-plist
:timestamps-export
)
670 :todo-keywords
(plist-get export-plist
:todo-keywords-export
)
671 :verbatim-multiline t
672 :select-tags
(plist-get export-plist
:select-tags-export
)
673 :exclude-tags
(plist-get export-plist
:exclude-tags-export
)
676 (plist-get export-plist
:archived-trees-export
)
677 :add-text
(plist-get opt-plist
:text
))
679 ;; export-generic plist variables
680 (withtags (plist-get export-plist
:tags-export
))
681 (tagsintoc (plist-get export-plist
:toc-tags-export
))
682 (tocnotagsstr (or (plist-get export-plist
:toc-tags-none-string
) ""))
683 (tocdepth (plist-get export-plist
:toc-indent-depth
))
684 (tocindentchar (plist-get export-plist
:toc-indent-char
))
685 (tocsecnums (plist-get export-plist
:toc-section-numbers
))
686 (tocsecnumform (plist-get export-plist
:toc-section-number-format
))
687 (tocformat (plist-get export-plist
:toc-format
))
688 (tocformtodo (plist-get export-plist
:toc-format-with-todo
))
689 (tocprefix (plist-get export-plist
:toc-prefix
))
690 (tocsuffix (plist-get export-plist
:toc-suffix
))
691 (bodyfixedpre (plist-get export-plist
:body-line-fixed-prefix
))
692 (bodyfixedsuf (plist-get export-plist
:body-line-fixed-suffix
))
693 (bodyfixedform (or (plist-get export-plist
:body-line-fixed-format
)
695 (listprefix (plist-get export-plist
:body-list-prefix
))
696 (listsuffix (plist-get export-plist
:body-list-suffix
))
697 (listformat (or (plist-get export-plist
:body-list-format
) "%s\n"))
699 (plist-get export-plist
:body-number-list-leave-number
))
700 (numlistprefix (plist-get export-plist
:body-number-list-prefix
))
701 (numlistsuffix (plist-get export-plist
:body-number-list-suffix
))
703 (or (plist-get export-plist
:body-number-list-format
) "%s\n"))
705 (or (plist-get export-plist
:body-list-checkbox-todo
) "\\1"))
707 (or (plist-get export-plist
:body-list-checkbox-done
) "\\1"))
709 (or (plist-get export-plist
:body-list-checkbox-half
) "\\1"))
711 (or (plist-get export-plist
:body-list-checkbox-todo-end
) ""))
713 (or (plist-get export-plist
:body-list-checkbox-done-end
) ""))
715 (or (plist-get export-plist
:body-list-checkbox-half-end
) ""))
716 (bodynewline-paragraph (plist-get export-plist
:body-newline-paragraph
))
717 (bodytextpre (plist-get export-plist
:body-text-prefix
))
718 (bodytextsuf (plist-get export-plist
:body-text-suffix
))
719 (bodylinewrap (plist-get export-plist
:body-line-wrap
))
720 (bodylineform (or (plist-get export-plist
:body-line-format
) "%s"))
721 (blockquotestart (or (plist-get export-plist
:blockquote-start
) "\n\n\t"))
722 (blockquoteend (or (plist-get export-plist
:blockquote-end
) "\n\n"))
724 ;; dynamic variables used heinously in fontification
725 ;; not referenced locally...
726 (format-boldify (plist-get export-plist
:bold-format
))
727 (format-italicize (plist-get export-plist
:italic-format
))
728 (format-underline (plist-get export-plist
:underline-format
))
729 (format-strikethrough (plist-get export-plist
:strikethrough-format
))
730 (format-code (plist-get export-plist
:code-format
))
731 (format-verbatim (plist-get export-plist
:verbatim-format
))
735 thetoc toctags have-headings first-heading-pos
736 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
738 (let ((inhibit-read-only t
))
740 (remove-text-properties (point-min) (point-max)
741 '(:org-license-to-kill t
))))
743 (setq org-min-level
(org-get-min-level lines level-offset
))
744 (setq org-last-level org-min-level
)
745 (org-init-section-numbers)
747 (find-file-noselect filename
)
749 (setq lang-words
(or (assoc language org-export-language-setup
)
750 (assoc "en" org-export-language-setup
)))
751 (switch-to-buffer-other-window buffer
)
754 ;; create local variables for all options, to make sure all called
755 ;; functions get the correct information
757 (set (make-local-variable (nth 2 x
))
758 (plist-get opt-plist
(car x
))))
759 org-export-plist-vars
)
760 (org-set-local 'org-odd-levels-only odd
)
761 (setq umax
(if arg
(prefix-numeric-value arg
)
762 org-export-headline-levels
))
768 (org-export-generic-header title export-plist
773 (if (and (or author email
)
774 (plist-get export-plist
:author-export
))
775 (insert (concat (nth 1 lang-words
) ": " (or author
"")
776 (if email
(concat " <" email
">") "")
780 ((and date
(string-match "%" date
))
781 (setq date
(format-time-string date
)))
783 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
785 (if (and date
(plist-get export-plist
:date-export
))
787 (org-export-generic-header date export-plist
792 ;; export the table of contents first
793 (if (plist-get export-plist
:toc-export
)
796 (org-export-generic-header (nth 3 lang-words
) export-plist
803 (push tocprefix thetoc
))
805 (mapc '(lambda (line)
806 (if (string-match org-todo-line-regexp line
)
807 ;; This is a headline
809 (setq have-headings t
)
810 (setq level
(- (match-end 1) (match-beginning 1)
812 level
(org-tr-level level
)
813 txt
(match-string 3 line
)
815 (or (and org-export-mark-todo-in-toc
817 (not (member (match-string 2 line
)
820 (and org-export-mark-todo-in-toc
822 (org-search-todo-below
824 (setq txt
(org-html-expand-for-generic txt
))
826 (while (string-match org-bracket-link-regexp txt
)
829 (match-string (if (match-end 2) 3 1) txt
)
832 (if (and (not tagsintoc
)
834 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
836 (setq txt
(replace-match "" t t txt
))
837 ; include tags but formated
839 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
844 (org-export-generic-header
846 export-plist
:toc-tags-prefix
847 :toc-tags-format
:toc-tags-suffix
))
849 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
851 (setq txt
(replace-match "" t t txt
)))
852 (setq toctags tocnotagsstr
)))
854 (if (string-match quote-re0 txt
)
855 (setq txt
(replace-match "" t t txt
)))
857 (if (<= level umax-toc
)
863 (* (max 0 (- level org-min-level
)) tocdepth
)
867 (format tocsecnumform
868 (org-section-number level
))
872 (if todo tocformtodo tocformat
)
878 (setq org-last-level level
))
882 (push tocsuffix thetoc
))
883 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
885 (org-init-section-numbers)
886 (org-export-generic-check-section "top")
887 (while (setq line
(pop lines
))
888 (when (and link-buffer
(string-match org-outline-regexp-bol line
))
889 (org-export-generic-push-links (nreverse link-buffer
))
890 (setq link-buffer nil
))
892 ;; Remove the quoted HTML tags.
894 (setq line
(org-html-expand-for-generic line
))
895 ;; Replace links with the description when possible
897 (while (string-match org-bracket-link-regexp line
)
898 (setq link
(match-string 1 line
)
899 desc0
(match-string 3 line
)
900 desc
(or desc0
(match-string 1 line
)))
901 (if (and (> (length link
) 8)
902 (equal (substring link
0 8) "coderef:"))
903 (setq line
(replace-match
904 (format (org-export-get-coderef-format (substring link
8) desc
)
907 org-export-code-refs
)))
909 (setq rpl
(concat "["
910 (or (match-string 3 line
) (match-string 1 line
))
912 (when (and desc0
(not (equal desc0 link
)))
913 (if org-export-generic-links-to-notes
914 (push (cons desc0 link
) link-buffer
)
915 (setq rpl
(concat rpl
" (" link
")")
916 wrap
(+ (length line
) (- (length (match-string 0 line
)))
918 (setq line
(replace-match rpl t t line
))))
920 (setq line
(org-translate-time line
)))
922 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
926 (org-export-generic-check-section "headline")
928 (setq first-heading-pos
(or first-heading-pos
(point)))
929 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
931 txt
(match-string 2 line
))
932 (org-generic-level-start level old-level txt umax export-plist lines
)
933 (setq old-level level
))
935 ((and org-export-with-tables
936 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
940 (org-export-generic-check-section "table")
944 (setq table-open t table-buffer nil
))
945 ;; Accumulate table lines
946 (setq table-buffer
(cons line table-buffer
))
947 (when (or (not lines
)
948 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
951 table-buffer
(nreverse table-buffer
))
954 (org-fix-indentation x org-generic-current-indentation
))
955 (org-format-table-generic table-buffer
)
958 ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
)
960 ;; pre-formatted text
962 (setq line
(replace-match "\\1" nil nil line
))
964 (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf
)
966 (insert (format bodyfixedform line
)))
968 ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line
)
969 ;; if the bullet list item is an asterisk, the leading space is /mandatory/
971 (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line
))
974 ;; TODO: nested lists
976 ;; first add a line break between any previous paragraph or line item and this
978 (when bodynewline-paragraph
979 (insert bodynewline-paragraph
))
981 ;; I believe this gets rid of leading whitespace.
982 (setq line
(replace-match "" nil nil line
))
984 ;; won't this insert the suffix /before/ the last line of the list?
985 ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
986 ;; unless 'org-empty-line-terminates-plain-lists' is true?
987 (org-export-generic-check-section "liststart" listprefix listsuffix
)
989 ;; deal with checkboxes
991 ((string-match "^\\(\\[ \\]\\)[ \t]*" line
)
992 (setq line
(concat (replace-match listchecktodo nil nil line
)
994 ((string-match "^\\(\\[X\\]\\)[ \t]*" line
)
995 (setq line
(concat (replace-match listcheckdone nil nil line
)
997 ((string-match "^\\(\\[/\\]\\)[ \t]*" line
)
998 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1002 (insert (format listformat
(org-export-generic-fontify line
))))
1003 ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line
)
1005 ;; numbered list item
1007 ;; TODO: nested lists
1009 (setq line
(replace-match (if numlistleavenum
"\\2" "") nil nil line
))
1011 (org-export-generic-check-section "numliststart"
1012 numlistprefix numlistsuffix
)
1014 ;; deal with checkboxes
1015 ;; TODO: whoops; leaving the numbers is a problem for ^ matching
1017 ((string-match "\\(\\[ \\]\\)[ \t]*" line
)
1018 (setq line
(concat (replace-match listchecktodo nil nil line
)
1020 ((string-match "\\(\\[X\\]\\)[ \t]*" line
)
1021 (setq line
(concat (replace-match listcheckdone nil nil line
)
1023 ((string-match "\\(\\[/\\]\\)[ \t]*" line
)
1024 (setq line
(concat (replace-match listcheckhalf nil nil line
)
1028 (insert (format numlistformat
(org-export-generic-fontify line
))))
1030 ((equal line
"ORG-BLOCKQUOTE-START")
1031 (setq line blockquotestart
))
1032 ((equal line
"ORG-BLOCKQUOTE-END")
1033 (setq line blockquoteend
))
1034 ((string-match "^\\s-*$" line
)
1036 (if bodynewline-paragraph
1037 (insert bodynewline-paragraph
)))
1042 (org-export-generic-check-section "body" bodytextpre bodytextsuf
)
1045 (org-export-generic-fontify line
))
1047 ;; XXX: properties? list?
1048 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
1049 (setq line
(replace-match "\\1\\3:" t nil line
)))
1051 (setq line
(org-fix-indentation line org-generic-current-indentation
))
1053 ;; Remove forced line breaks
1054 (if (string-match "\\\\\\\\[ \t]*$" line
)
1055 (setq line
(replace-match "" t t line
)))
1058 ;; XXX: was dependent on wrap var which was calculated by???
1059 (if (> (length line
) bodylinewrap
)
1061 (org-export-generic-wrap line bodylinewrap
))
1063 (insert (format bodylineform line
)))))
1065 ;; if we're at a level > 0; insert the closing body level stuff
1067 (while (> (- level counter
) 0)
1069 (org-export-generic-format export-plist
:body-section-suffix
0
1071 (setq counter
(1+ counter
))))
1073 (org-export-generic-check-section "bottom")
1075 (org-export-generic-push-links (nreverse link-buffer
))
1079 ;; insert the table of contents
1081 (goto-char (point-min))
1082 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
1084 (goto-char (match-beginning 0))
1086 (goto-char first-heading-pos
))
1087 (mapc 'insert thetoc
)
1088 (or (looking-at "[ \t]*\n[ \t]*\n")
1091 ;; Convert whitespace place holders
1092 (goto-char (point-min))
1094 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
1095 (setq end
(next-single-property-change beg
'org-whitespace
))
1097 (delete-region beg end
)
1098 (insert (make-string (- end beg
) ?\
))))
1102 ;; remove display and invisible chars
1104 (goto-char (point-min))
1105 (while (setq beg
(next-single-property-change (point) 'display
))
1106 (setq end
(next-single-property-change beg
'display
))
1107 (delete-region beg end
)
1110 (goto-char (point-min))
1111 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
1112 (setq end
(next-single-property-change beg
'org-cwidth
))
1113 (delete-region beg end
)
1115 (goto-char (point-min))))
1118 (defun org-export-generic-format (export-plist prop
&optional len n reverse
)
1119 "converts a property specification to a string given types of properties
1121 The EXPORT-PLIST should be defined as the lookup plist.
1122 The PROP should be the property name to search for in it.
1123 LEN is set to the length of multi-characters strings to generate (or 0)
1125 REVERSE means to reverse the list if the plist match is a list
1127 (let* ((prefixtype (plist-get export-plist prop
))
1130 ((null prefixtype
) "")
1131 ((and len
(char-or-string-p prefixtype
) (not (stringp prefixtype
)))
1132 ;; sequence of chars
1133 (concat (make-string len prefixtype
) "\n"))
1134 ((stringp prefixtype
)
1136 ((and n
(listp prefixtype
))
1138 (setq prefixtype
(reverse prefixtype
)))
1139 (setq subtype
(if (> n
(length prefixtype
))
1140 (car (last prefixtype
))
1141 (nth (1- n
) prefixtype
)))
1142 (if (stringp subtype
)
1144 (concat (make-string len subtype
) "\n")))
1148 (defun org-export-generic-header (header export-plist
1149 prefixprop formatprop postfixprop
1150 &optional n reverse
)
1151 "convert a header to an output string given formatting property names"
1152 (let* ((formatspec (plist-get export-plist formatprop
))
1153 (len (length header
)))
1155 (org-export-generic-format export-plist prefixprop len n reverse
)
1156 (format (or formatspec
"%s") header
)
1157 (org-export-generic-format export-plist postfixprop len n reverse
))
1160 (defun org-export-generic-preprocess (parameters)
1161 "Do extra work for ASCII export"
1162 ;; Put quotes around verbatim text
1163 (goto-char (point-min))
1164 (while (re-search-forward org-verbatim-re nil t
)
1165 (goto-char (match-end 2))
1166 (backward-delete-char 1) (insert "'")
1167 (goto-char (match-beginning 2))
1168 (delete-char 1) (insert "`")
1169 (goto-char (match-end 2)))
1170 ;; Remove target markers
1171 (goto-char (point-min))
1172 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
1173 (replace-match "\\1\\2")))
1175 (defun org-html-expand-for-generic (line)
1176 "Handle quoted HTML for ASCII export."
1177 (if org-export-html-expand
1178 (while (string-match "@<[^<>\n]*>" line
)
1179 ;; We just remove the tags for now.
1180 (setq line
(replace-match "" nil nil line
))))
1183 (defun org-export-generic-wrap (line where
)
1184 "Wrap LINE at or before WHERE."
1185 (let* ((ind (org-get-indentation line
))
1186 (indstr (make-string ind ?\
))
1190 (while (> len where
)
1192 (loop for i from where downto
(/ where
2) do
1193 (and (equal (aref line i
) ?\
)
1200 (if didfirst indstr
"")
1201 (substring line
0 pos
)
1204 (setq line
(substring line
(1+ pos
)))
1205 (setq len
(length line
)))
1206 (setq result
(concat result line
))
1208 (concat result indstr line
)))
1210 (defun org-export-generic-push-links (link-buffer)
1211 "Push out links in the buffer."
1213 ;; We still have links to push out.
1219 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
1220 (setq ind
(or (match-string 2)
1221 (make-string (length (match-string 3)) ?\
)))))
1222 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
1226 (defun org-generic-level-start (level old-level title umax export-plist
1228 "Insert a new level in a generic export."
1229 (let ((n (- level umax
1))
1231 (diff (- level old-level
)) (counter 0)
1232 (secnums (plist-get export-plist
:body-header-section-numbers
))
1234 (plist-get export-plist
:body-header-section-number-format
))
1236 (unless org-export-with-tags
1237 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
1238 (setq title
(replace-match "" t t title
))))
1242 ((> level old-level
)
1243 (while (< (+ old-level counter
) (1- level
))
1245 (org-export-generic-format export-plist
:body-section-prefix
0
1246 (+ old-level counter
)))
1247 (setq counter
(1+ counter
))
1250 ((< level old-level
)
1251 (while (> (- old-level counter
) (1- level
))
1253 (org-export-generic-format export-plist
:body-section-suffix
0
1254 (- old-level counter
)))
1255 (setq counter
(1+ counter
))
1258 ((= level old-level
)
1260 (org-export-generic-format export-plist
:body-section-suffix
0 level
))
1264 (org-export-generic-format export-plist
:body-section-prefix
0 level
))
1266 (if (and org-export-with-section-numbers
1268 (or (not (numberp secnums
))
1271 (concat (format (or secnumformat
"%s ")
1272 (org-section-number level
)) title
)))
1274 ;; handle tags and formatting
1276 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title
)
1278 (if (plist-get export-plist
:body-tags-export
)
1279 (setq tagstring
(org-export-generic-header (match-string 1 title
)
1283 :body-tags-suffix
)))
1284 (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title
)
1285 (setq title
(replace-match "" t t title
)))
1286 (setq tagstring
(plist-get export-plist
:body-tags-none-string
)))
1289 (org-export-generic-header title export-plist
1290 :body-section-header-prefix
1291 :body-section-header-format
1292 :body-section-header-suffix
1297 (setq org-generic-current-indentation
'(0 .
0))))
1299 (defun org-insert-centered (s &optional underline
)
1300 "Insert the string S centered and underline it with character UNDERLINE."
1301 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
1302 (insert (make-string ind ?\
) s
"\n")
1304 (insert (make-string ind ?\
)
1305 (make-string (string-width s
) underline
)
1308 (defvar org-table-colgroup-info nil
)
1309 (defun org-format-table-generic (lines)
1310 "Format a table for ascii export."
1312 (setq lines
(org-split-string lines
"\n")))
1313 (if (not (string-match "^[ \t]*|" (car lines
)))
1314 ;; Table made by table.el - test for spanning
1317 ;; A normal org table
1318 ;; Get rid of hlines at beginning and end
1319 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1320 (setq lines
(nreverse lines
))
1321 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1322 (setq lines
(nreverse lines
))
1323 (when org-export-table-remove-special-lines
1324 ;; Check if the table has a marking column. If yes remove the
1325 ;; column and the special lines
1326 (setq lines
(org-table-clean-before-export lines
)))
1327 ;; Get rid of the vertical lines except for grouping
1328 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
1330 (while (setq line
(pop lines
))
1331 (if (string-match org-table-hline-regexp line
)
1332 (and (string-match "|\\(.*\\)|" line
)
1333 (setq line
(replace-match " \\1" t nil line
)))
1334 (setq start
0 vl1 vl
)
1335 (while (string-match "|" line start
)
1336 (setq start
(match-end 0))
1337 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
1341 (defun org-colgroup-info-to-vline-list (info)
1344 (setq last new new
(pop info
))
1345 (if (or (memq last
'(:end
:startend
))
1346 (memq new
'(:start
:startend
)))
1349 (setq vl
(nreverse vl
))
1350 (and vl
(setcar vl nil
))
1354 ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
1355 (defvar org-export-generic-emphasis-alist
1356 '(("*" format-boldify nil
)
1357 ("/" format-italicize nil
)
1358 ("_" format-underline nil
)
1359 ("+" format-strikethrough nil
)
1361 ("~" format-verbatim t
))
1362 "Alist of org format -> formatting variables for fontification.
1363 Each element of the list is a list of three elements.
1364 The first element is the character used as a marker for fontification.
1365 The second element is a variable name, set in org-export-generic. That
1366 variable will be dereferenced to obtain a formatting string to wrap
1367 fontified text with.
1368 The third element decides whether to protect converted text from other
1371 ;;; Cargo-culted from the latex translation. I couldn't figure out how
1372 ;;; to keep the structure since the generic export operates on lines, rather
1373 ;;; than on a buffer as in the latex export, meaning that none of the
1374 ;;; search forward code could be kept. This led me to rewrite the
1375 ;;; whole thing recursively. A huge lose for efficiency (potentially),
1376 ;;; but I couldn't figure out how to make the looping work.
1377 ;;; Worse, it's /doubly/ recursive, because this function calls
1378 ;;; org-export-generic-emph-format, which can call it recursively...
1379 ;;; [2010/05/20:rpg]
1380 (defun org-export-generic-fontify (string)
1381 "Convert fontification according to generic rules."
1382 (if (string-match org-emph-re string
)
1383 ;; The match goes one char after the *string*, except at the end of a line
1384 (let ((emph (assoc (match-string 3 string
)
1385 org-export-generic-emphasis-alist
))
1386 (beg (match-beginning 0))
1387 (end (match-end 0)))
1389 (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
1390 (match-string 3 string
)))
1391 ;; now we need to determine whether we have strikethrough or
1392 ;; a list, which is a bit nasty
1393 (if (and (equal (match-string 3 string
) "+")
1395 (string-match "\\`-+\\'" (match-string 4 string
))))
1396 ;; a list --- skip this match and recurse on the point after the
1397 ;; first emph char...
1398 (concat (substring string
0 (1+ (match-beginning 3)))
1399 (org-export-generic-fontify (substring string
(match-beginning 3))))
1400 (concat (substring string
0 beg
) ;; part before the match
1401 (match-string 1 string
)
1402 (org-export-generic-emph-format (second emph
)
1403 (match-string 4 string
)
1405 (or (match-string 5 string
) "")
1406 (org-export-generic-fontify (substring string end
)))))
1409 (defun org-export-generic-emph-format (format-varname string protect
)
1410 "Return a string that results from applying the markup indicated by
1411 FORMAT-VARNAME to STRING."
1412 (let ((format (symbol-value format-varname
)))
1413 (let ((string-to-emphasize
1416 (org-export-generic-fontify string
))))
1418 (format format string-to-emphasize
)
1419 string-to-emphasize
))))
1421 (provide 'org-generic
)
1422 (provide 'org-export-generic
)
1424 ;;; org-export-generic.el ends here