org-ascii.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. ;;; org-ascii.el --- ASCII export for Org-mode
  2. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;;; Code:
  23. (require 'org-exp)
  24. (eval-when-compile
  25. (require 'cl))
  26. (defgroup org-export-ascii nil
  27. "Options specific for ASCII export of Org-mode files."
  28. :tag "Org Export ASCII"
  29. :group 'org-export)
  30. (defcustom org-export-ascii-underline '(?\= ?\- ?\~ ?\^ ?\. ?\# ?\$)
  31. "Characters for underlining headings in ASCII export.
  32. In the given sequence, these characters will be used for level 1, 2, ..."
  33. :group 'org-export-ascii
  34. :type '(repeat character))
  35. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  36. "Bullet characters for headlines converted to lists in ASCII export.
  37. The first character is used for the first lest level generated in this
  38. way, and so on. If there are more levels than characters given here,
  39. the list will be repeated.
  40. Note that plain lists will keep the same bullets as the have in the
  41. Org-mode file."
  42. :group 'org-export-ascii
  43. :type '(repeat character))
  44. (defcustom org-export-ascii-links-to-notes t
  45. "Non-nil means convert links to notes before the next headline.
  46. When nil, the link will be exported in place. If the line becomes long
  47. in this way, it will be wrapped."
  48. :group 'org-export-ascii
  49. :type 'boolean)
  50. (defcustom org-export-ascii-table-keep-all-vertical-lines nil
  51. "Non-nil means keep all vertical lines in ASCII tables.
  52. When nil, vertical lines will be removed except for those needed
  53. for column grouping."
  54. :group 'org-export-ascii
  55. :type 'boolean)
  56. (defcustom org-export-ascii-table-widen-columns t
  57. "Non-nil means widen narrowed columns for export.
  58. When nil, narrowed columns will look in ASCII export just like in org-mode,
  59. i.e. with \"=>\" as ellipsis."
  60. :group 'org-export-ascii
  61. :type 'boolean)
  62. (defvar org-export-ascii-entities 'ascii
  63. "The ascii representation to be used during ascii export.
  64. Possible values are:
  65. ascii Only use plain ASCII characters
  66. latin1 Include Latin-1 character
  67. utf8 Use all UTF-8 characters")
  68. ;;; Hooks
  69. (defvar org-export-ascii-final-hook nil
  70. "Hook run at the end of ASCII export, in the new buffer.")
  71. ;;; ASCII export
  72. (defvar org-ascii-current-indentation nil) ; For communication
  73. ;;;###autoload
  74. (defun org-export-as-latin1 (&rest args)
  75. "Like `org-export-as-ascii', use latin1 encoding for special symbols."
  76. (interactive)
  77. (org-export-as-encoding 'org-export-as-ascii (org-called-interactively-p 'any)
  78. 'latin1 args))
  79. ;;;###autoload
  80. (defun org-export-as-latin1-to-buffer (&rest args)
  81. "Like `org-export-as-ascii-to-buffer', use latin1 encoding for symbols."
  82. (interactive)
  83. (org-export-as-encoding 'org-export-as-ascii-to-buffer
  84. (org-called-interactively-p 'any) 'latin1 args))
  85. ;;;###autoload
  86. (defun org-export-as-utf8 (&rest args)
  87. "Like `org-export-as-ascii', use encoding for special symbols."
  88. (interactive)
  89. (org-export-as-encoding 'org-export-as-ascii
  90. (org-called-interactively-p 'any)
  91. 'utf8 args))
  92. ;;;###autoload
  93. (defun org-export-as-utf8-to-buffer (&rest args)
  94. "Like `org-export-as-ascii-to-buffer', use utf8 encoding for symbols."
  95. (interactive)
  96. (org-export-as-encoding 'org-export-as-ascii-to-buffer
  97. (org-called-interactively-p 'any) 'utf8 args))
  98. (defun org-export-as-encoding (command interactivep encoding &rest args)
  99. (let ((org-export-ascii-entities encoding))
  100. (if interactivep
  101. (call-interactively command)
  102. (apply command args))))
  103. ;;;###autoload
  104. (defun org-export-as-ascii-to-buffer (arg)
  105. "Call `org-export-as-ascii` with output to a temporary buffer.
  106. No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
  107. (interactive "P")
  108. (org-export-as-ascii arg nil "*Org ASCII Export*")
  109. (when org-export-show-temporary-export-buffer
  110. (switch-to-buffer-other-window "*Org ASCII Export*")))
  111. ;;;###autoload
  112. (defun org-replace-region-by-ascii (beg end)
  113. "Assume the current region has org-mode syntax, and convert it to plain ASCII.
  114. This can be used in any buffer. For example, you could write an
  115. itemized list in org-mode syntax in a Mail buffer and then use this
  116. command to convert it."
  117. (interactive "r")
  118. (let (reg ascii buf pop-up-frames)
  119. (save-window-excursion
  120. (if (derived-mode-p 'org-mode)
  121. (setq ascii (org-export-region-as-ascii
  122. beg end t 'string))
  123. (setq reg (buffer-substring beg end)
  124. buf (get-buffer-create "*Org tmp*"))
  125. (with-current-buffer buf
  126. (erase-buffer)
  127. (insert reg)
  128. (org-mode)
  129. (setq ascii (org-export-region-as-ascii
  130. (point-min) (point-max) t 'string)))
  131. (kill-buffer buf)))
  132. (delete-region beg end)
  133. (insert ascii)))
  134. ;;;###autoload
  135. (defun org-export-region-as-ascii (beg end &optional body-only buffer)
  136. "Convert region from BEG to END in org-mode buffer to plain ASCII.
  137. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  138. contents, and only produce the region of converted text, useful for
  139. cut-and-paste operations.
  140. If BUFFER is a buffer or a string, use/create that buffer as a target
  141. of the converted ASCII. If BUFFER is the symbol `string', return the
  142. produced ASCII as a string and leave not buffer behind. For example,
  143. a Lisp program could call this function in the following way:
  144. (setq ascii (org-export-region-as-ascii beg end t 'string))
  145. When called interactively, the output buffer is selected, and shown
  146. in a window. A non-interactive call will only return the buffer."
  147. (interactive "r\nP")
  148. (when (org-called-interactively-p 'any)
  149. (setq buffer "*Org ASCII Export*"))
  150. (let ((transient-mark-mode t) (zmacs-regions t)
  151. ext-plist rtn)
  152. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  153. (goto-char end)
  154. (set-mark (point)) ;; to activate the region
  155. (goto-char beg)
  156. (setq rtn (org-export-as-ascii nil ext-plist buffer body-only))
  157. (if (fboundp 'deactivate-mark) (deactivate-mark))
  158. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  159. (switch-to-buffer-other-window rtn)
  160. rtn)))
  161. ;;;###autoload
  162. (defun org-export-as-ascii (arg &optional ext-plist to-buffer body-only pub-dir)
  163. "Export the outline as a pretty ASCII file.
  164. If there is an active region, export only the region.
  165. The prefix ARG specifies how many levels of the outline should become
  166. underlined headlines, default is 3. Lower levels will become bulleted
  167. lists. EXT-PLIST is a property list with external parameters overriding
  168. org-mode's default settings, but still inferior to file-local
  169. settings. When TO-BUFFER is non-nil, create a buffer with that
  170. name and export to that buffer. If TO-BUFFER is the symbol
  171. `string', don't leave any buffer behind but just return the
  172. resulting ASCII as a string. When BODY-ONLY is set, don't produce
  173. the file header and footer. When PUB-DIR is set, use this as the
  174. publishing directory."
  175. (interactive "P")
  176. (run-hooks 'org-export-first-hook)
  177. (setq-default org-todo-line-regexp org-todo-line-regexp)
  178. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  179. ext-plist
  180. (org-infile-export-plist)))
  181. (region-p (org-region-active-p))
  182. (rbeg (and region-p (region-beginning)))
  183. (rend (and region-p (region-end)))
  184. (subtree-p
  185. (if (plist-get opt-plist :ignore-subtree-p)
  186. nil
  187. (when region-p
  188. (save-excursion
  189. (goto-char rbeg)
  190. (and (org-at-heading-p)
  191. (>= (org-end-of-subtree t t) rend))))))
  192. (level-offset (if subtree-p
  193. (save-excursion
  194. (goto-char rbeg)
  195. (+ (funcall outline-level)
  196. (if org-odd-levels-only 1 0)))
  197. 0))
  198. (opt-plist (setq org-export-opt-plist
  199. (if subtree-p
  200. (org-export-add-subtree-options opt-plist rbeg)
  201. opt-plist)))
  202. ;; The following two are dynamically scoped into other
  203. ;; routines below.
  204. (org-current-export-dir
  205. (or pub-dir (org-export-directory :html opt-plist)))
  206. (org-current-export-file buffer-file-name)
  207. (custom-times org-display-custom-times)
  208. (org-ascii-current-indentation '(0 . 0))
  209. (level 0) line txt
  210. (umax nil)
  211. (umax-toc nil)
  212. (case-fold-search nil)
  213. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  214. (filename (if to-buffer
  215. nil
  216. (concat (file-name-as-directory
  217. (or pub-dir
  218. (org-export-directory :ascii opt-plist)))
  219. (file-name-sans-extension
  220. (or (and subtree-p
  221. (org-entry-get (region-beginning)
  222. "EXPORT_FILE_NAME" t))
  223. (file-name-nondirectory bfname)))
  224. ".txt")))
  225. (filename (and filename
  226. (if (equal (file-truename filename)
  227. (file-truename bfname))
  228. (concat filename ".txt")
  229. filename)))
  230. (buffer (if to-buffer
  231. (cond
  232. ((eq to-buffer 'string)
  233. (get-buffer-create "*Org ASCII Export*"))
  234. (t (get-buffer-create to-buffer)))
  235. (find-file-noselect filename)))
  236. (org-levels-open (make-vector org-level-max nil))
  237. (odd org-odd-levels-only)
  238. (date (plist-get opt-plist :date))
  239. (author (plist-get opt-plist :author))
  240. (title (or (and subtree-p (org-export-get-title-from-subtree))
  241. (plist-get opt-plist :title)
  242. (and (not
  243. (plist-get opt-plist :skip-before-1st-heading))
  244. (org-export-grab-title-from-buffer))
  245. (and (buffer-file-name)
  246. (file-name-sans-extension
  247. (file-name-nondirectory bfname)))
  248. "UNTITLED"))
  249. (email (plist-get opt-plist :email))
  250. (language (plist-get opt-plist :language))
  251. (quote-re0 (concat "^\\(" org-quote-string "\\)\\( +\\|[ \t]*$\\)"))
  252. (todo nil)
  253. (lang-words nil)
  254. (region
  255. (buffer-substring
  256. (if (org-region-active-p) (region-beginning) (point-min))
  257. (if (org-region-active-p) (region-end) (point-max))))
  258. (org-export-footnotes-seen nil)
  259. (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
  260. (lines (org-split-string
  261. (org-export-preprocess-string
  262. region
  263. :for-backend 'ascii
  264. :skip-before-1st-heading
  265. (plist-get opt-plist :skip-before-1st-heading)
  266. :drawers (plist-get opt-plist :drawers)
  267. :tags (plist-get opt-plist :tags)
  268. :priority (plist-get opt-plist :priority)
  269. :footnotes (plist-get opt-plist :footnotes)
  270. :timestamps (plist-get opt-plist :timestamps)
  271. :todo-keywords (plist-get opt-plist :todo-keywords)
  272. :tasks (plist-get opt-plist :tasks)
  273. :verbatim-multiline t
  274. :select-tags (plist-get opt-plist :select-tags)
  275. :exclude-tags (plist-get opt-plist :exclude-tags)
  276. :archived-trees
  277. (plist-get opt-plist :archived-trees)
  278. :add-text (plist-get opt-plist :text))
  279. "\n"))
  280. thetoc have-headings first-heading-pos
  281. table-open table-buffer link-buffer link type path desc desc0 rpl wrap fnc)
  282. (let ((inhibit-read-only t))
  283. (org-unmodified
  284. (remove-text-properties (point-min) (point-max)
  285. '(:org-license-to-kill t))))
  286. (setq org-min-level (org-get-min-level lines level-offset))
  287. (setq org-last-level org-min-level)
  288. (org-init-section-numbers)
  289. (setq lang-words (or (assoc language org-export-language-setup)
  290. (assoc "en" org-export-language-setup)))
  291. (set-buffer buffer)
  292. (erase-buffer)
  293. (fundamental-mode)
  294. (org-install-letbind)
  295. ;; create local variables for all options, to make sure all called
  296. ;; functions get the correct information
  297. (mapc (lambda (x)
  298. (set (make-local-variable (nth 2 x))
  299. (plist-get opt-plist (car x))))
  300. org-export-plist-vars)
  301. (org-set-local 'org-odd-levels-only odd)
  302. (setq umax (if arg (prefix-numeric-value arg)
  303. org-export-headline-levels))
  304. (setq umax-toc (if (integerp org-export-with-toc)
  305. (min org-export-with-toc umax)
  306. umax))
  307. ;; File header
  308. (unless body-only
  309. (when (and title (not (string= "" title)))
  310. (org-insert-centered title ?=)
  311. (insert "\n"))
  312. (if (and (or author email)
  313. org-export-author-info)
  314. (insert (concat (nth 1 lang-words) ": " (or author "")
  315. (if (and org-export-email-info
  316. email (string-match "\\S-" email))
  317. (concat " <" email ">") "")
  318. "\n")))
  319. (cond
  320. ((and date (string-match "%" date))
  321. (setq date (format-time-string date)))
  322. (date)
  323. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  324. (if (and date org-export-time-stamp-file)
  325. (insert (concat (nth 2 lang-words) ": " date"\n")))
  326. (unless (= (point) (point-min))
  327. (insert "\n\n")))
  328. (if (and org-export-with-toc (not body-only))
  329. (progn
  330. (push (concat (nth 3 lang-words) "\n") thetoc)
  331. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  332. "\n") thetoc)
  333. (mapc #'(lambda (line)
  334. (if (string-match org-todo-line-regexp
  335. line)
  336. ;; This is a headline
  337. (progn
  338. (setq have-headings t)
  339. (setq level (- (match-end 1) (match-beginning 1)
  340. level-offset)
  341. level (org-tr-level level)
  342. txt (match-string 3 line)
  343. todo
  344. (or (and org-export-mark-todo-in-toc
  345. (match-beginning 2)
  346. (not (member (match-string 2 line)
  347. org-done-keywords)))
  348. ; TODO, not DONE
  349. (and org-export-mark-todo-in-toc
  350. (= level umax-toc)
  351. (org-search-todo-below
  352. line lines level))))
  353. (setq txt (org-html-expand-for-ascii txt))
  354. (while (string-match org-bracket-link-regexp txt)
  355. (setq txt
  356. (replace-match
  357. (match-string (if (match-end 2) 3 1) txt)
  358. t t txt)))
  359. (if (and (memq org-export-with-tags '(not-in-toc nil))
  360. (string-match
  361. (org-re "[ \t]+:[[:alnum:]_@#%:]+:[ \t]*$")
  362. txt))
  363. (setq txt (replace-match "" t t txt)))
  364. (if (string-match quote-re0 txt)
  365. (setq txt (replace-match "" t t txt 1)))
  366. (if org-export-with-section-numbers
  367. (setq txt (concat (org-section-number level)
  368. " " txt)))
  369. (if (<= level umax-toc)
  370. (progn
  371. (push
  372. (concat
  373. (make-string
  374. (* (max 0 (- level org-min-level)) 4) ?\ )
  375. (format (if todo "%s (*)\n" "%s\n") txt))
  376. thetoc)
  377. (setq org-last-level level))
  378. ))))
  379. lines)
  380. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  381. (org-init-section-numbers)
  382. (while (setq line (pop lines))
  383. (when (and link-buffer (string-match org-outline-regexp-bol line))
  384. (org-export-ascii-push-links (nreverse link-buffer))
  385. (setq link-buffer nil))
  386. (setq wrap nil)
  387. ;; Remove the quoted HTML tags.
  388. (setq line (org-html-expand-for-ascii line))
  389. ;; Replace links with the description when possible
  390. (while (string-match org-bracket-link-analytic-regexp++ line)
  391. (setq path (match-string 3 line)
  392. link (concat (match-string 1 line) path)
  393. type (match-string 2 line)
  394. desc0 (match-string 5 line)
  395. desc0 (replace-regexp-in-string "\\\\_" "_" desc0)
  396. desc (or desc0 link)
  397. desc (replace-regexp-in-string "\\\\_" "_" desc))
  398. (if (and (> (length link) 8)
  399. (equal (substring link 0 8) "coderef:"))
  400. (setq line (replace-match
  401. (format (org-export-get-coderef-format (substring link 8) desc)
  402. (cdr (assoc
  403. (substring link 8)
  404. org-export-code-refs)))
  405. t t line))
  406. (setq rpl (concat "[" desc "]"))
  407. (if (functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  408. (setq rpl (or (save-match-data
  409. (funcall fnc (org-link-unescape path)
  410. desc0 'ascii))
  411. rpl))
  412. (when (and desc0 (not (equal desc0 link)))
  413. (if org-export-ascii-links-to-notes
  414. (push (cons desc0 link) link-buffer)
  415. (setq rpl (concat rpl " (" link ")")
  416. wrap (+ (length line) (- (length (match-string 0 line)))
  417. (length desc))))))
  418. (setq line (replace-match rpl t t line))))
  419. (when custom-times
  420. (setq line (org-translate-time line)))
  421. (cond
  422. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  423. ;; a Headline
  424. (setq first-heading-pos (or first-heading-pos (point)))
  425. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  426. level-offset))
  427. txt (match-string 2 line))
  428. (org-ascii-level-start level txt umax lines))
  429. ((and org-export-with-tables
  430. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  431. (if (not table-open)
  432. ;; New table starts
  433. (setq table-open t table-buffer nil))
  434. ;; Accumulate lines
  435. (setq table-buffer (cons line table-buffer))
  436. (when (or (not lines)
  437. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  438. (car lines))))
  439. (setq table-open nil
  440. table-buffer (nreverse table-buffer))
  441. (insert (mapconcat
  442. (lambda (x)
  443. (org-fix-indentation x org-ascii-current-indentation))
  444. (org-format-table-ascii table-buffer)
  445. "\n") "\n")))
  446. (t
  447. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)"
  448. line)
  449. (setq line (replace-match "\\1\\3:" t nil line)))
  450. (setq line (org-fix-indentation line org-ascii-current-indentation))
  451. ;; Remove forced line breaks
  452. (if (string-match "\\\\\\\\[ \t]*$" line)
  453. (setq line (replace-match "" t t line)))
  454. (if (and org-export-with-fixed-width
  455. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  456. (setq line (replace-match "\\1" nil nil line))
  457. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  458. (insert line "\n"))))
  459. (org-export-ascii-push-links (nreverse link-buffer))
  460. (normal-mode)
  461. ;; insert the table of contents
  462. (when thetoc
  463. (goto-char (point-min))
  464. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  465. (progn
  466. (goto-char (match-beginning 0))
  467. (replace-match ""))
  468. (goto-char first-heading-pos))
  469. (mapc 'insert thetoc)
  470. (or (looking-at "[ \t]*\n[ \t]*\n")
  471. (insert "\n\n")))
  472. ;; Convert whitespace place holders
  473. (goto-char (point-min))
  474. (let (beg end)
  475. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  476. (setq end (next-single-property-change beg 'org-whitespace))
  477. (goto-char beg)
  478. (delete-region beg end)
  479. (insert (make-string (- end beg) ?\ ))))
  480. ;; remove display and invisible chars
  481. (let (beg end)
  482. (goto-char (point-min))
  483. (while (setq beg (next-single-property-change (point) 'display))
  484. (setq end (next-single-property-change beg 'display))
  485. (delete-region beg end)
  486. (goto-char beg)
  487. (insert "=>"))
  488. (goto-char (point-min))
  489. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  490. (setq end (next-single-property-change beg 'org-cwidth))
  491. (delete-region beg end)
  492. (goto-char beg)))
  493. (run-hooks 'org-export-ascii-final-hook)
  494. (or to-buffer (save-buffer))
  495. (goto-char (point-min))
  496. (or (org-export-push-to-kill-ring "ASCII")
  497. (message "Exporting... done"))
  498. ;; Return the buffer or a string, according to how this function was called
  499. (if (eq to-buffer 'string)
  500. (prog1 (buffer-substring (point-min) (point-max))
  501. (kill-buffer (current-buffer)))
  502. (current-buffer))))
  503. ;;;###autoload
  504. (defun org-export-ascii-preprocess (parameters)
  505. "Do extra work for ASCII export."
  506. ;;
  507. ;; Realign tables to get rid of narrowing
  508. (when org-export-ascii-table-widen-columns
  509. (let ((org-table-do-narrow nil))
  510. (goto-char (point-min))
  511. (org-ascii-replace-entities)
  512. (goto-char (point-min))
  513. (org-table-map-tables
  514. (lambda () (org-if-unprotected (org-table-align)))
  515. 'quietly)))
  516. ;; Put quotes around verbatim text
  517. (goto-char (point-min))
  518. (while (re-search-forward org-verbatim-re nil t)
  519. (org-if-unprotected-at (match-beginning 4)
  520. (goto-char (match-end 2))
  521. (backward-delete-char 1) (insert "'")
  522. (goto-char (match-beginning 2))
  523. (delete-char 1) (insert "`")
  524. (goto-char (match-end 2))))
  525. ;; Remove target markers
  526. (goto-char (point-min))
  527. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  528. (org-if-unprotected-at (match-beginning 1)
  529. (replace-match "\\1\\2")))
  530. ;; Remove list start counters
  531. (goto-char (point-min))
  532. (while (org-list-search-forward
  533. "\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*" nil t)
  534. (replace-match ""))
  535. (remove-text-properties
  536. (point-min) (point-max)
  537. '(face nil font-lock-fontified nil font-lock-multiline nil line-prefix nil wrap-prefix nil)))
  538. (defun org-html-expand-for-ascii (line)
  539. "Handle quoted HTML for ASCII export."
  540. (if org-export-html-expand
  541. (while (string-match "@<[^<>\n]*>" line)
  542. ;; We just remove the tags for now.
  543. (setq line (replace-match "" nil nil line))))
  544. line)
  545. (defun org-ascii-replace-entities ()
  546. "Replace entities with the ASCII representation."
  547. (let (e)
  548. (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" nil t)
  549. (org-if-unprotected-at (match-beginning 1)
  550. (setq e (org-entity-get-representation (match-string 1)
  551. org-export-ascii-entities))
  552. (and e (replace-match e t t))))))
  553. (defun org-export-ascii-wrap (line where)
  554. "Wrap LINE at or before WHERE."
  555. (let ((ind (org-get-indentation line))
  556. pos)
  557. (catch 'found
  558. (loop for i from where downto (/ where 2) do
  559. (and (equal (aref line i) ?\ )
  560. (setq pos i)
  561. (throw 'found t))))
  562. (if pos
  563. (concat (substring line 0 pos) "\n"
  564. (make-string ind ?\ )
  565. (substring line (1+ pos)))
  566. line)))
  567. (defun org-export-ascii-push-links (link-buffer)
  568. "Push out links in the buffer."
  569. (when link-buffer
  570. ;; We still have links to push out.
  571. (insert "\n")
  572. (let ((ind ""))
  573. (save-match-data
  574. (if (save-excursion
  575. (re-search-backward
  576. (concat "^\\(\\([ \t]*\\)\\|\\("
  577. org-outline-regexp
  578. "\\)\\)[^ \t\n]") nil t))
  579. (setq ind (or (match-string 2)
  580. (make-string (length (match-string 3)) ?\ )))))
  581. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  582. link-buffer))
  583. (insert "\n")))
  584. (defun org-ascii-level-start (level title umax &optional lines)
  585. "Insert a new level in ASCII export."
  586. (let (char (n (- level umax 1)) (ind 0))
  587. (if (> level umax)
  588. (progn
  589. (insert (make-string (* 2 n) ?\ )
  590. (char-to-string (nth (% n (length org-export-ascii-bullets))
  591. org-export-ascii-bullets))
  592. " " title "\n")
  593. ;; find the indentation of the next non-empty line
  594. (catch 'stop
  595. (while lines
  596. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  597. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  598. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  599. (pop lines)))
  600. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  601. (if (or (not (equal (char-before) ?\n))
  602. (not (equal (char-before (1- (point))) ?\n)))
  603. (insert "\n"))
  604. (setq char (or (nth (1- level) org-export-ascii-underline)
  605. (car (last org-export-ascii-underline))))
  606. (unless org-export-with-tags
  607. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  608. (setq title (replace-match "" t t title))))
  609. (if org-export-with-section-numbers
  610. (setq title (concat (org-section-number level) " " title)))
  611. (insert title "\n" (make-string (string-width title) char) "\n")
  612. (setq org-ascii-current-indentation '(0 . 0)))))
  613. (defun org-insert-centered (s &optional underline)
  614. "Insert the string S centered and underline it with character UNDERLINE."
  615. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  616. (insert (make-string ind ?\ ) s "\n")
  617. (if underline
  618. (insert (make-string ind ?\ )
  619. (make-string (string-width s) underline)
  620. "\n"))))
  621. (defvar org-table-colgroup-info nil)
  622. (defun org-format-table-ascii (lines)
  623. "Format a table for ascii export."
  624. (if (stringp lines)
  625. (setq lines (org-split-string lines "\n")))
  626. (if (not (string-match "^[ \t]*|" (car lines)))
  627. ;; Table made by table.el - test for spanning
  628. lines
  629. ;; A normal org table
  630. ;; Get rid of hlines at beginning and end
  631. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  632. (setq lines (nreverse lines))
  633. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  634. (setq lines (nreverse lines))
  635. (when org-export-table-remove-special-lines
  636. ;; Check if the table has a marking column. If yes remove the
  637. ;; column and the special lines
  638. (setq lines (org-table-clean-before-export lines)))
  639. ;; Get rid of the vertical lines except for grouping
  640. (if org-export-ascii-table-keep-all-vertical-lines
  641. lines
  642. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  643. rtn line vl1 start)
  644. (while (setq line (pop lines))
  645. (if (string-match org-table-hline-regexp line)
  646. (and (string-match "|\\(.*\\)|" line)
  647. (setq line (replace-match " \\1" t nil line)))
  648. (setq start 0 vl1 vl)
  649. (while (string-match "|" line start)
  650. (setq start (match-end 0))
  651. (or (pop vl1) (setq line (replace-match " " t t line)))))
  652. (push line rtn))
  653. (nreverse rtn)))))
  654. (defun org-colgroup-info-to-vline-list (info)
  655. (let (vl new last)
  656. (while info
  657. (setq last new new (pop info))
  658. (if (or (memq last '(:end :startend))
  659. (memq new '(:start :startend)))
  660. (push t vl)
  661. (push nil vl)))
  662. (setq vl (nreverse vl))
  663. (and vl (setcar vl nil))
  664. vl))
  665. (provide 'org-ascii)
  666. ;; Local variables:
  667. ;; generated-autoload-file: "org-loaddefs.el"
  668. ;; End:
  669. ;;; org-ascii.el ends here