1 ;; org-export-generic.el --- Export frameworg with custom backends
3 ;; Copyright (C) 2009 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
("* " "** " "*** " "**** " "***** ")
327 ;; minimal html exporter
330 ;; simple html output
334 :header-prefix
"<body>"
336 :title-format
"<h1>%s</h1>\n\n"
339 :date-format
"<br /><b>Date:</b> <i>%s</i><br />\n\n"
343 :body-header-section-numbers
3
345 :body-section-header-prefix
("<h1>" "<h2>" "<h3>"
346 "<h4>" "<h5>" "<h6>")
347 :body-section-header-format
"%s"
348 :body-section-header-suffix
("</h1>\n" "</h2>\n" "</h3>\n"
349 "</h4>\n" "</h5>\n" "</h6>\n")
351 :body-section-prefix
"<secprefix>\n"
352 :body-section-suffix
"</secsuffix>\n"
353 ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
354 ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
356 :body-line-export-preformated t
357 :body-line-format
"%s\n"
359 :body-text-prefix
"<p>\n"
360 :body-text-suffix
"</p>\n"
362 :body-bullet-list-prefix
(?
* ?
+ ?-
)
363 ; :body-bullet-list-suffix (?* ?+ ?-)
367 ;; internet-draft .xml for xml2rfc exporter
370 ;; this tries to use every specifier for demo purposes
374 :title-prefix
"<?xml version=\"1.0\"\?>
375 <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
376 <!ENTITY rfcs PUBLIC '' 'blah'>
377 <?rfc strict=\"yes\" ?>
379 <?rfc tocdepth=\"4\" ?>
380 <?rfc symrefs=\"yes\" ?>
381 <?rfc compact=\"yes\" ?>
382 <?rfc subcompact=\"no\" ?>
383 <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
386 :title-format
"<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
387 :title-suffix
"<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
388 <organization>Comany, Inc..</organization>
401 <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
402 <area>Operations and Management</area>
403 <workgroup>FIXME</workgroup>
409 :body-header-section-numbers nil
411 :body-section-header-format
"<section title=\"%s\">\n"
412 :body-section-suffix
"</section>\n"
414 ; if preformated text should be included (eg, : prefixed)
415 :body-line-export-preformated t
416 :body-line-fixed-prefix
"<figure>\n<artwork>\n"
417 :body-line-fixed-suffix
"\n</artwork>\n</figure>\n"
420 :body-line-format
"%s"
423 ; print above and below all body parts
424 :body-text-prefix
"<t>\n"
425 :body-text-suffix
"</t>\n"
427 :body-list-prefix
"<list style=\"symbols\">\n"
428 :body-list-suffix
"</list>\n"
429 :body-list-format
"<t>%s</t>\n"
433 "A assoc list of property lists to specify export definitions"
436 (setq org-generic-export-type
"demo")
438 (defvar org-export-generic-section-type
"")
439 (defvar org-export-generic-section-suffix
"")
442 (defun org-set-generic-type (type definition
)
443 "Adds a TYPE and DEFINITION to the existing list of defined generic
445 (aput 'org-generic-alist type definition
))
447 ;;; helper functions for org-set-generic-type
448 (defvar org-export-generic-keywords nil
)
449 (defmacro* def-org-export-generic-keyword
(keyword
452 "Define KEYWORD as a legitimate element for inclusion in
453 the body of an org-set-generic-type definition."
455 (pushnew ,keyword org-export-generic-keywords
)
456 ;; TODO: push the documentation and type information
457 ;; somewhere where it will do us some good.
460 (def-org-export-generic-keyword :body-newline-paragraph
461 :documentation
"Bound either to NIL or to a pattern to be
462 inserted in the output for every blank line in the input.
463 The intention is to handle formats where text is flowed, and
464 newlines are interpreted as significant \(e.g., as indicating
465 preformatted text\). A common non-nil value for this keyword
466 is \"\\n\". Should typically be combined with a value for
467 :body-line-format that does NOT end with a newline."
470 ;;; fontification keywords
471 (def-org-export-generic-keyword :bold-format
)
472 (def-org-export-generic-keyword :italic-format
)
473 (def-org-export-generic-keyword :underline-format
)
474 (def-org-export-generic-keyword :strikethrough-format
)
475 (def-org-export-generic-keyword :code-format
)
476 (def-org-export-generic-keyword :verbatim-format
)
481 (defun org-export-generic-remember-section (type suffix
&optional prefix
)
482 (setq org-export-generic-section-type type
)
483 (setq org-export-generic-section-suffix suffix
)
488 (defun org-export-generic-check-section (type &optional prefix suffix
)
489 "checks to see if type is already in use, or we're switching parts
490 If we're switching, then insert a potentially previously remembered
491 suffix, and insert the current prefix immediately and then save the
492 suffix a later change time."
494 (when (not (equal type org-export-generic-section-type
))
495 (if org-export-generic-section-suffix
496 (insert org-export-generic-section-suffix
))
497 (setq org-export-generic-section-type type
)
498 (setq org-export-generic-section-suffix suffix
)
503 (defun org-export-generic (arg)
504 "Export the outline as generic output.
505 If there is an active region, export only the region.
506 The prefix ARG specifies how many levels of the outline should become
507 underlined headlines. The default is 3."
509 (setq-default org-todo-line-regexp org-todo-line-regexp
)
510 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
511 (org-infile-export-plist)))
512 (region-p (org-region-active-p))
513 (rbeg (and region-p
(region-beginning)))
514 (rend (and region-p
(region-end)))
519 (and (org-at-heading-p)
520 (>= (org-end-of-subtree t t
) rend
)))))
521 (level-offset (if subtree-p
524 (+ (funcall outline-level
)
525 (if org-odd-levels-only
1 0)))
527 (opt-plist (setq org-export-opt-plist
529 (org-export-add-subtree-options opt-plist rbeg
)
533 (bogus (mapc (lambda (x)
535 (concat helpstart
"\["
537 (plist-get (cdr x
) :key-binding
))
541 (help (concat helpstart
"
543 \[ ] the current setting of the org-generic-export-type variable
551 (plist-get (cdr x
) :key-binding
)
554 (list (list ?
"default"))))
558 ;; read in the type to use
562 (save-window-excursion
563 (delete-other-windows)
564 (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
566 (org-fit-window-to-buffer (get-buffer-window
567 "*Org Export/Generic Styles Help*"))
568 (message "Select command: ")
569 (setq r1
(read-char-exclusive))))
570 (setq r2
(if (< r1
27) (+ r1
96) r1
))
571 (unless (setq ass
(cadr (assq r2 cmds
)))
572 (error "No command associated with key %c" r1
))
575 (if (equal ass
"default") org-generic-export-type ass
)
576 org-generic-alist
))))
578 (custom-times org-display-custom-times
)
579 (org-generic-current-indentation '(0 .
0))
580 (level 0) (old-level 0) line txt lastwastext
583 (case-fold-search nil
)
584 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
585 (filesuffix (or (plist-get export-plist
:file-suffix
) ".foo"))
586 (filename (concat (file-name-as-directory
587 (org-export-directory :ascii opt-plist
))
588 (file-name-sans-extension
590 (org-entry-get (region-beginning)
591 "EXPORT_FILE_NAME" t
))
592 (file-name-nondirectory bfname
)))
594 (filename (if (equal (file-truename filename
)
595 (file-truename bfname
))
596 (concat filename filesuffix
)
598 (buffer (find-file-noselect filename
))
599 (org-levels-open (make-vector org-level-max nil
))
600 (odd org-odd-levels-only
)
601 (date (plist-get opt-plist
:date
))
602 (author (plist-get opt-plist
:author
))
603 (title (or (and subtree-p
(org-export-get-title-from-subtree))
604 (plist-get opt-plist
:title
)
606 (plist-get opt-plist
:skip-before-1st-heading
))
607 (org-export-grab-title-from-buffer))
608 (file-name-sans-extension
609 (file-name-nondirectory bfname
))))
610 (email (plist-get opt-plist
:email
))
611 (language (plist-get opt-plist
:language
))
612 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
613 ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
618 (if (org-region-active-p) (region-beginning) (point-min))
619 (if (org-region-active-p) (region-end) (point-max))))
620 (lines (org-split-string
621 (org-export-preprocess-string
624 :skip-before-1st-heading
625 (plist-get opt-plist
:skip-before-1st-heading
)
626 :drawers
(plist-get export-plist
:drawers-export
)
627 :tags
(plist-get export-plist
:tags-export
)
628 :priority
(plist-get export-plist
:priority-export
)
629 :footnotes
(plist-get export-plist
:footnotes-export
)
630 :timestamps
(plist-get export-plist
:timestamps-export
)
631 :todo-keywords
(plist-get export-plist
:todo-keywords-export
)
632 :verbatim-multiline t
633 :select-tags
(plist-get export-plist
:select-tags-export
)
634 :exclude-tags
(plist-get export-plist
:exclude-tags-export
)
637 (plist-get export-plist
:archived-trees-export
)
638 :add-text
(plist-get opt-plist
:text
))
640 ;; export-generic plist variables
641 (withtags (plist-get export-plist
:tags-export
))
642 (tagsintoc (plist-get export-plist
:toc-tags-export
))
643 (tocnotagsstr (or (plist-get export-plist
:toc-tags-none-string
) ""))
644 (tocdepth (plist-get export-plist
:toc-indent-depth
))
645 (tocindentchar (plist-get export-plist
:toc-indent-char
))
646 (tocsecnums (plist-get export-plist
:toc-section-numbers
))
647 (tocsecnumform (plist-get export-plist
:toc-section-number-format
))
648 (tocformat (plist-get export-plist
:toc-format
))
649 (tocformtodo (plist-get export-plist
:toc-format-with-todo
))
650 (tocprefix (plist-get export-plist
:toc-prefix
))
651 (tocsuffix (plist-get export-plist
:toc-suffix
))
652 (bodyfixedpre (plist-get export-plist
:body-line-fixed-prefix
))
653 (bodyfixedsuf (plist-get export-plist
:body-line-fixed-suffix
))
654 (bodyfixedform (or (plist-get export-plist
:body-line-fixed-format
)
656 (listprefix (plist-get export-plist
:body-list-prefix
))
657 (listsuffix (plist-get export-plist
:body-list-suffix
))
658 (listformat (or (plist-get export-plist
:body-list-format
) "%s\n"))
660 (plist-get export-plist
:body-number-list-leave-number
))
661 (numlistprefix (plist-get export-plist
:body-number-list-prefix
))
662 (numlistsuffix (plist-get export-plist
:body-number-list-suffix
))
664 (or (plist-get export-plist
:body-number-list-format
) "%s\n"))
666 (or (plist-get export-plist
:body-list-checkbox-todo
) "\\1"))
668 (or (plist-get export-plist
:body-list-checkbox-done
) "\\1"))
670 (or (plist-get export-plist
:body-list-checkbox-half
) "\\1"))
672 (or (plist-get export-plist
:body-list-checkbox-todo-end
) ""))
674 (or (plist-get export-plist
:body-list-checkbox-done-end
) ""))
676 (or (plist-get export-plist
:body-list-checkbox-half-end
) ""))
677 (bodynewline-paragraph (plist-get export-plist
:body-newline-paragraph
))
678 (bodytextpre (plist-get export-plist
:body-text-prefix
))
679 (bodytextsuf (plist-get export-plist
:body-text-suffix
))
680 (bodylinewrap (plist-get export-plist
:body-line-wrap
))
681 (bodylineform (or (plist-get export-plist
:body-line-format
) "%s"))
682 (blockquotestart (or (plist-get export-plist
:blockquote-start
) "\n\n\t"))
683 (blockquoteend (or (plist-get export-plist
:blockquote-end
) "\n\n"))
685 ;; dynamic variables used heinously in fontification
686 ;; not referenced locally...
687 (format-boldify (plist-get export-plist
:bold-format
))
688 (format-italicize (plist-get export-plist
:italic-format
))
689 (format-underline (plist-get export-plist
:underline-format
))
690 (format-strikethrough (plist-get export-plist
:strikethrough-format
))
691 (format-code (plist-get export-plist
:code-format
))
692 (format-verbatim (plist-get export-plist
:verbatim-format
))
696 thetoc toctags have-headings first-heading-pos
697 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
699 (let ((inhibit-read-only t
))
701 (remove-text-properties (point-min) (point-max)
702 '(:org-license-to-kill t
))))
704 (setq org-min-level
(org-get-min-level lines level-offset
))
705 (setq org-last-level org-min-level
)
706 (org-init-section-numbers)
708 (find-file-noselect filename
)
710 (setq lang-words
(or (assoc language org-export-language-setup
)
711 (assoc "en" org-export-language-setup
)))
712 (switch-to-buffer-other-window buffer
)
715 ;; create local variables for all options, to make sure all called
716 ;; functions get the correct information
718 (set (make-local-variable (nth 2 x
))
719 (plist-get opt-plist
(car x
))))
720 org-export-plist-vars
)
721 (org-set-local 'org-odd-levels-only odd
)
722 (setq umax
(if arg
(prefix-numeric-value arg
)
723 org-export-headline-levels
))
729 (org-export-generic-header title export-plist
734 (if (and (or author email
)
735 (plist-get export-plist
:author-export
))
736 (insert (concat (nth 1 lang-words
) ": " (or author
"")
737 (if email
(concat " <" email
">") "")
741 ((and date
(string-match "%" date
))
742 (setq date
(format-time-string date
)))
744 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
746 (if (and date
(plist-get export-plist
:date-export
))
748 (org-export-generic-header date export-plist
753 ;; export the table of contents first
754 (if (plist-get export-plist
:toc-export
)
757 (org-export-generic-header (nth 3 lang-words
) export-plist
764 (push tocprefix thetoc
))
766 (mapc '(lambda (line)
767 (if (string-match org-todo-line-regexp line
)
768 ;; This is a headline
770 (setq have-headings t
)
771 (setq level
(- (match-end 1) (match-beginning 1)
773 level
(org-tr-level level
)
774 txt
(match-string 3 line
)
776 (or (and org-export-mark-todo-in-toc
778 (not (member (match-string 2 line
)
781 (and org-export-mark-todo-in-toc
783 (org-search-todo-below
785 (setq txt
(org-html-expand-for-generic txt
))
787 (while (string-match org-bracket-link-regexp txt
)
790 (match-string (if (match-end 2) 3 1) txt
)
793 (if (and (not tagsintoc
)
795 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
797 (setq txt
(replace-match "" t t txt
))
798 ; include tags but formated
800 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
805 (org-export-generic-header
807 export-plist
:toc-tags-prefix
808 :toc-tags-format
:toc-tags-suffix
))
810 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
812 (setq txt
(replace-match "" t t txt
)))
813 (setq toctags tocnotagsstr
)))
815 (if (string-match quote-re0 txt
)
816 (setq txt
(replace-match "" t t txt
)))
818 (if (<= level umax-toc
)
824 (* (max 0 (- level org-min-level
)) tocdepth
)
828 (format tocsecnumform
829 (org-section-number level
))
833 (if todo tocformtodo tocformat
)
839 (setq org-last-level level
))
843 (push tocsuffix thetoc
))
844 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
846 (org-init-section-numbers)
847 (org-export-generic-check-section "top")
848 (while (setq line
(pop lines
))
849 (when (and link-buffer
(string-match org-outline-regexp-bol line
))
850 (org-export-generic-push-links (nreverse link-buffer
))
851 (setq link-buffer nil
))
853 ;; Remove the quoted HTML tags.
855 (setq line
(org-html-expand-for-generic line
))
856 ;; Replace links with the description when possible
858 (while (string-match org-bracket-link-regexp line
)
859 (setq link
(match-string 1 line
)
860 desc0
(match-string 3 line
)
861 desc
(or desc0
(match-string 1 line
)))
862 (if (and (> (length link
) 8)
863 (equal (substring link
0 8) "coderef:"))
864 (setq line
(replace-match
865 (format (org-export-get-coderef-format (substring link
8) desc
)
868 org-export-code-refs
)))
870 (setq rpl
(concat "["
871 (or (match-string 3 line
) (match-string 1 line
))
873 (when (and desc0
(not (equal desc0 link
)))
874 (if org-export-generic-links-to-notes
875 (push (cons desc0 link
) link-buffer
)
876 (setq rpl
(concat rpl
" (" link
")")
877 wrap
(+ (length line
) (- (length (match-string 0 line
)))
879 (setq line
(replace-match rpl t t line
))))
881 (setq line
(org-translate-time line
)))
883 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
887 (org-export-generic-check-section "headline")
889 (setq first-heading-pos
(or first-heading-pos
(point)))
890 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
892 txt
(match-string 2 line
))
893 (org-generic-level-start level old-level txt umax export-plist lines
)
894 (setq old-level level
))
896 ((and org-export-with-tables
897 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
901 (org-export-generic-check-section "table")
905 (setq table-open t table-buffer nil
))
906 ;; Accumulate table lines
907 (setq table-buffer
(cons line table-buffer
))
908 (when (or (not lines
)
909 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
912 table-buffer
(nreverse table-buffer
))
915 (org-fix-indentation x org-generic-current-indentation
))
916 (org-format-table-generic table-buffer
)
919 ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
)
921 ;; pre-formatted text
923 (setq line
(replace-match "\\1" nil nil line
))
925 (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf
)
927 (insert (format bodyfixedform line
)))
929 ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line
)
930 ;; if the bullet list item is an asterisk, the leading space is /mandatory/
932 (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line
))
935 ;; TODO: nested lists
937 ;; first add a line break between any previous paragraph or line item and this
939 (when bodynewline-paragraph
940 (insert bodynewline-paragraph
))
942 ;; I believe this gets rid of leading whitespace.
943 (setq line
(replace-match "" nil nil line
))
945 ;; won't this insert the suffix /before/ the last line of the list?
946 ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
947 ;; unless 'org-empty-line-terminates-plain-lists' is true?
948 (org-export-generic-check-section "liststart" listprefix listsuffix
)
950 ;; deal with checkboxes
952 ((string-match "^\\(\\[ \\]\\)[ \t]*" line
)
953 (setq line
(concat (replace-match listchecktodo nil nil line
)
955 ((string-match "^\\(\\[X\\]\\)[ \t]*" line
)
956 (setq line
(concat (replace-match listcheckdone nil nil line
)
958 ((string-match "^\\(\\[/\\]\\)[ \t]*" line
)
959 (setq line
(concat (replace-match listcheckhalf nil nil line
)
963 (insert (format listformat
(org-export-generic-fontify line
))))
964 ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line
)
966 ;; numbered list item
968 ;; TODO: nested lists
970 (setq line
(replace-match (if numlistleavenum
"\\2" "") nil nil line
))
972 (org-export-generic-check-section "numliststart"
973 numlistprefix numlistsuffix
)
975 ;; deal with checkboxes
976 ;; TODO: whoops; leaving the numbers is a problem for ^ matching
978 ((string-match "\\(\\[ \\]\\)[ \t]*" line
)
979 (setq line
(concat (replace-match listchecktodo nil nil line
)
981 ((string-match "\\(\\[X\\]\\)[ \t]*" line
)
982 (setq line
(concat (replace-match listcheckdone nil nil line
)
984 ((string-match "\\(\\[/\\]\\)[ \t]*" line
)
985 (setq line
(concat (replace-match listcheckhalf nil nil line
)
989 (insert (format numlistformat
(org-export-generic-fontify line
))))
991 ((equal line
"ORG-BLOCKQUOTE-START")
992 (setq line blockquotestart
))
993 ((equal line
"ORG-BLOCKQUOTE-END")
994 (setq line blockquoteend
))
995 ((string-match "^\\s-*$" line
)
997 (if bodynewline-paragraph
998 (insert bodynewline-paragraph
)))
1003 (org-export-generic-check-section "body" bodytextpre bodytextsuf
)
1006 (org-export-generic-fontify line
))
1008 ;; XXX: properties? list?
1009 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
1010 (setq line
(replace-match "\\1\\3:" t nil line
)))
1012 (setq line
(org-fix-indentation line org-generic-current-indentation
))
1014 ;; Remove forced line breaks
1015 (if (string-match "\\\\\\\\[ \t]*$" line
)
1016 (setq line
(replace-match "" t t line
)))
1019 ;; XXX: was dependent on wrap var which was calculated by???
1020 (if (> (length line
) bodylinewrap
)
1022 (org-export-generic-wrap line bodylinewrap
))
1024 (insert (format bodylineform line
)))))
1026 ;; if we're at a level > 0; insert the closing body level stuff
1028 (while (> (- level counter
) 0)
1030 (org-export-generic-format export-plist
:body-section-suffix
0
1032 (setq counter
(1+ counter
))))
1034 (org-export-generic-check-section "bottom")
1036 (org-export-generic-push-links (nreverse link-buffer
))
1040 ;; insert the table of contents
1042 (goto-char (point-min))
1043 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
1045 (goto-char (match-beginning 0))
1047 (goto-char first-heading-pos
))
1048 (mapc 'insert thetoc
)
1049 (or (looking-at "[ \t]*\n[ \t]*\n")
1052 ;; Convert whitespace place holders
1053 (goto-char (point-min))
1055 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
1056 (setq end
(next-single-property-change beg
'org-whitespace
))
1058 (delete-region beg end
)
1059 (insert (make-string (- end beg
) ?\
))))
1063 ;; remove display and invisible chars
1065 (goto-char (point-min))
1066 (while (setq beg
(next-single-property-change (point) 'display
))
1067 (setq end
(next-single-property-change beg
'display
))
1068 (delete-region beg end
)
1071 (goto-char (point-min))
1072 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
1073 (setq end
(next-single-property-change beg
'org-cwidth
))
1074 (delete-region beg end
)
1076 (goto-char (point-min))))
1079 (defun org-export-generic-format (export-plist prop
&optional len n reverse
)
1080 "converts a property specification to a string given types of properties
1082 The EXPORT-PLIST should be defined as the lookup plist.
1083 The PROP should be the property name to search for in it.
1084 LEN is set to the length of multi-characters strings to generate (or 0)
1086 REVERSE means to reverse the list if the plist match is a list
1088 (let* ((prefixtype (plist-get export-plist prop
))
1091 ((null prefixtype
) "")
1092 ((and len
(char-or-string-p prefixtype
) (not (stringp prefixtype
)))
1093 ;; sequence of chars
1094 (concat (make-string len prefixtype
) "\n"))
1095 ((stringp prefixtype
)
1097 ((and n
(listp prefixtype
))
1099 (setq prefixtype
(reverse prefixtype
)))
1100 (setq subtype
(if (> n
(length prefixtype
))
1101 (car (last prefixtype
))
1102 (nth (1- n
) prefixtype
)))
1103 (if (stringp subtype
)
1105 (concat (make-string len subtype
) "\n")))
1109 (defun org-export-generic-header (header export-plist
1110 prefixprop formatprop postfixprop
1111 &optional n reverse
)
1112 "convert a header to an output string given formatting property names"
1113 (let* ((formatspec (plist-get export-plist formatprop
))
1114 (len (length header
)))
1116 (org-export-generic-format export-plist prefixprop len n reverse
)
1117 (format (or formatspec
"%s") header
)
1118 (org-export-generic-format export-plist postfixprop len n reverse
))
1121 (defun org-export-generic-preprocess (parameters)
1122 "Do extra work for ASCII export"
1123 ;; Put quotes around verbatim text
1124 (goto-char (point-min))
1125 (while (re-search-forward org-verbatim-re nil t
)
1126 (goto-char (match-end 2))
1127 (backward-delete-char 1) (insert "'")
1128 (goto-char (match-beginning 2))
1129 (delete-char 1) (insert "`")
1130 (goto-char (match-end 2)))
1131 ;; Remove target markers
1132 (goto-char (point-min))
1133 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
1134 (replace-match "\\1\\2")))
1136 (defun org-html-expand-for-generic (line)
1137 "Handle quoted HTML for ASCII export."
1138 (if org-export-html-expand
1139 (while (string-match "@<[^<>\n]*>" line
)
1140 ;; We just remove the tags for now.
1141 (setq line
(replace-match "" nil nil line
))))
1144 (defun org-export-generic-wrap (line where
)
1145 "Wrap LINE at or before WHERE."
1146 (let* ((ind (org-get-indentation line
))
1147 (indstr (make-string ind ?\
))
1151 (while (> len where
)
1153 (loop for i from where downto
(/ where
2) do
1154 (and (equal (aref line i
) ?\
)
1161 (if didfirst indstr
"")
1162 (substring line
0 pos
)
1165 (setq line
(substring line
(1+ pos
)))
1166 (setq len
(length line
)))
1167 (setq result
(concat result line
))
1169 (concat result indstr line
)))
1171 (defun org-export-generic-push-links (link-buffer)
1172 "Push out links in the buffer."
1174 ;; We still have links to push out.
1180 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
1181 (setq ind
(or (match-string 2)
1182 (make-string (length (match-string 3)) ?\
)))))
1183 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
1187 (defun org-generic-level-start (level old-level title umax export-plist
1189 "Insert a new level in a generic export."
1190 (let ((n (- level umax
1))
1192 (diff (- level old-level
)) (counter 0)
1193 (secnums (plist-get export-plist
:body-header-section-numbers
))
1195 (plist-get export-plist
:body-header-section-number-format
))
1197 (unless org-export-with-tags
1198 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
1199 (setq title
(replace-match "" t t title
))))
1203 ((> level old-level
)
1204 (while (< (+ old-level counter
) (1- level
))
1206 (org-export-generic-format export-plist
:body-section-prefix
0
1207 (+ old-level counter
)))
1208 (setq counter
(1+ counter
))
1211 ((< level old-level
)
1212 (while (> (- old-level counter
) (1- level
))
1214 (org-export-generic-format export-plist
:body-section-suffix
0
1215 (- old-level counter
)))
1216 (setq counter
(1+ counter
))
1219 ((= level old-level
)
1221 (org-export-generic-format export-plist
:body-section-suffix
0 level
))
1225 (org-export-generic-format export-plist
:body-section-prefix
0 level
))
1227 (if (and org-export-with-section-numbers
1229 (or (not (numberp secnums
))
1232 (concat (format (or secnumformat
"%s ")
1233 (org-section-number level
)) title
)))
1235 ;; handle tags and formatting
1237 (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title
)
1239 (if (plist-get export-plist
:body-tags-export
)
1240 (setq tagstring
(org-export-generic-header (match-string 1 title
)
1244 :body-tags-suffix
)))
1245 (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title
)
1246 (setq title
(replace-match "" t t title
)))
1247 (setq tagstring
(plist-get export-plist
:body-tags-none-string
)))
1250 (org-export-generic-header title export-plist
1251 :body-section-header-prefix
1252 :body-section-header-format
1253 :body-section-header-suffix
1258 (setq org-generic-current-indentation
'(0 .
0))))
1260 (defun org-insert-centered (s &optional underline
)
1261 "Insert the string S centered and underline it with character UNDERLINE."
1262 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
1263 (insert (make-string ind ?\
) s
"\n")
1265 (insert (make-string ind ?\
)
1266 (make-string (string-width s
) underline
)
1269 (defvar org-table-colgroup-info nil
)
1270 (defun org-format-table-generic (lines)
1271 "Format a table for ascii export."
1273 (setq lines
(org-split-string lines
"\n")))
1274 (if (not (string-match "^[ \t]*|" (car lines
)))
1275 ;; Table made by table.el - test for spanning
1278 ;; A normal org table
1279 ;; Get rid of hlines at beginning and end
1280 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1281 (setq lines
(nreverse lines
))
1282 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
1283 (setq lines
(nreverse lines
))
1284 (when org-export-table-remove-special-lines
1285 ;; Check if the table has a marking column. If yes remove the
1286 ;; column and the special lines
1287 (setq lines
(org-table-clean-before-export lines
)))
1288 ;; Get rid of the vertical lines except for grouping
1289 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
1291 (while (setq line
(pop lines
))
1292 (if (string-match org-table-hline-regexp line
)
1293 (and (string-match "|\\(.*\\)|" line
)
1294 (setq line
(replace-match " \\1" t nil line
)))
1295 (setq start
0 vl1 vl
)
1296 (while (string-match "|" line start
)
1297 (setq start
(match-end 0))
1298 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
1302 (defun org-colgroup-info-to-vline-list (info)
1305 (setq last new new
(pop info
))
1306 (if (or (memq last
'(:end
:startend
))
1307 (memq new
'(:start
:startend
)))
1310 (setq vl
(nreverse vl
))
1311 (and vl
(setcar vl nil
))
1315 ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
1316 (defvar org-export-generic-emphasis-alist
1317 '(("*" format-boldify nil
)
1318 ("/" format-italicize nil
)
1319 ("_" format-underline nil
)
1320 ("+" format-strikethrough nil
)
1322 ("~" format-verbatim t
))
1323 "Alist of org format -> formatting variables for fontification.
1324 Each element of the list is a list of three elements.
1325 The first element is the character used as a marker for fontification.
1326 The second element is a variable name, set in org-export-generic. That
1327 variable will be dereferenced to obtain a formatting string to wrap
1328 fontified text with.
1329 The third element decides whether to protect converted text from other
1332 ;;; Cargo-culted from the latex translation. I couldn't figure out how
1333 ;;; to keep the structure since the generic export operates on lines, rather
1334 ;;; than on a buffer as in the latex export, meaning that none of the
1335 ;;; search forward code could be kept. This led me to rewrite the
1336 ;;; whole thing recursively. A huge lose for efficiency (potentially),
1337 ;;; but I couldn't figure out how to make the looping work.
1338 ;;; Worse, it's /doubly/ recursive, because this function calls
1339 ;;; org-export-generic-emph-format, which can call it recursively...
1340 ;;; [2010/05/20:rpg]
1341 (defun org-export-generic-fontify (string)
1342 "Convert fontification according to generic rules."
1343 (if (string-match org-emph-re string
)
1344 ;; The match goes one char after the *string*, except at the end of a line
1345 (let ((emph (assoc (match-string 3 string
)
1346 org-export-generic-emphasis-alist
))
1347 (beg (match-beginning 0))
1348 (end (match-end 0)))
1350 (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
1351 (match-string 3 string
)))
1352 ;; now we need to determine whether we have strikethrough or
1353 ;; a list, which is a bit nasty
1354 (if (and (equal (match-string 3 string
) "+")
1356 (string-match "\\`-+\\'" (match-string 4 string
))))
1357 ;; a list --- skip this match and recurse on the point after the
1358 ;; first emph char...
1359 (concat (substring string
0 (1+ (match-beginning 3)))
1360 (org-export-generic-fontify (substring string
(match-beginning 3))))
1361 (concat (substring string
0 beg
) ;; part before the match
1362 (match-string 1 string
)
1363 (org-export-generic-emph-format (second emph
)
1364 (match-string 4 string
)
1366 (or (match-string 5 string
) "")
1367 (org-export-generic-fontify (substring string end
)))))
1370 (defun org-export-generic-emph-format (format-varname string protect
)
1371 "Return a string that results from applying the markup indicated by
1372 FORMAT-VARNAME to STRING."
1373 (let ((format (symbol-value format-varname
)))
1374 (let ((string-to-emphasize
1377 (org-export-generic-fontify string
))))
1379 (format format string-to-emphasize
)
1380 string-to-emphasize
))))
1382 (provide 'org-generic
)
1383 (provide 'org-export-generic
)
1385 ;;; org-export-generic.el ends here