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