org-ascii.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. ;;; org-ascii.el --- ASCII export for Org-mode
  2. ;; Copyright (C) 2004-2012 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 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 (eq major-mode '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
  157. nil nil ext-plist
  158. buffer body-only))
  159. (if (fboundp 'deactivate-mark) (deactivate-mark))
  160. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  161. (switch-to-buffer-other-window rtn)
  162. rtn)))
  163. ;;;###autoload
  164. (defun org-export-as-ascii (arg &optional hidden ext-plist
  165. to-buffer body-only pub-dir)
  166. "Export the outline as a pretty ASCII file.
  167. If there is an active region, export only the region.
  168. The prefix ARG specifies how many levels of the outline should become
  169. underlined headlines, default is 3. Lower levels will become bulleted
  170. lists. When HIDDEN is non-nil, don't display the ASCII buffer.
  171. EXT-PLIST is a property list with external parameters overriding
  172. org-mode's default settings, but still inferior to file-local
  173. settings. When TO-BUFFER is non-nil, create a buffer with that
  174. name and export to that buffer. If TO-BUFFER is the symbol
  175. `string', don't leave any buffer behind but just return the
  176. resulting ASCII as a string. When BODY-ONLY is set, don't produce
  177. the file header and footer. When PUB-DIR is set, use this as the
  178. publishing directory."
  179. (interactive "P")
  180. (run-hooks 'org-export-first-hook)
  181. (setq-default org-todo-line-regexp org-todo-line-regexp)
  182. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  183. ext-plist
  184. (org-infile-export-plist)))
  185. (region-p (org-region-active-p))
  186. (rbeg (and region-p (region-beginning)))
  187. (rend (and region-p (region-end)))
  188. (subtree-p
  189. (if (plist-get opt-plist :ignore-subtree-p)
  190. nil
  191. (when region-p
  192. (save-excursion
  193. (goto-char rbeg)
  194. (and (org-at-heading-p)
  195. (>= (org-end-of-subtree t t) rend))))))
  196. (level-offset (if subtree-p
  197. (save-excursion
  198. (goto-char rbeg)
  199. (+ (funcall outline-level)
  200. (if org-odd-levels-only 1 0)))
  201. 0))
  202. (opt-plist (setq org-export-opt-plist
  203. (if subtree-p
  204. (org-export-add-subtree-options opt-plist rbeg)
  205. opt-plist)))
  206. ;; The following two are dynamically scoped into other
  207. ;; routines below.
  208. (org-current-export-dir
  209. (or pub-dir (org-export-directory :html opt-plist)))
  210. (org-current-export-file buffer-file-name)
  211. (custom-times org-display-custom-times)
  212. (org-ascii-current-indentation '(0 . 0))
  213. (level 0) line txt
  214. (umax nil)
  215. (umax-toc nil)
  216. (case-fold-search nil)
  217. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  218. (filename (if to-buffer
  219. nil
  220. (concat (file-name-as-directory
  221. (or pub-dir
  222. (org-export-directory :ascii opt-plist)))
  223. (file-name-sans-extension
  224. (or (and subtree-p
  225. (org-entry-get (region-beginning)
  226. "EXPORT_FILE_NAME" t))
  227. (file-name-nondirectory bfname)))
  228. ".txt")))
  229. (filename (and filename
  230. (if (equal (file-truename filename)
  231. (file-truename bfname))
  232. (concat filename ".txt")
  233. filename)))
  234. (buffer (if to-buffer
  235. (cond
  236. ((eq to-buffer 'string)
  237. (get-buffer-create "*Org ASCII Export*"))
  238. (t (get-buffer-create to-buffer)))
  239. (find-file-noselect filename)))
  240. (org-levels-open (make-vector org-level-max nil))
  241. (odd org-odd-levels-only)
  242. (date (plist-get opt-plist :date))
  243. (author (plist-get opt-plist :author))
  244. (title (or (and subtree-p (org-export-get-title-from-subtree))
  245. (plist-get opt-plist :title)
  246. (and (not
  247. (plist-get opt-plist :skip-before-1st-heading))
  248. (org-export-grab-title-from-buffer))
  249. (and (buffer-file-name)
  250. (file-name-sans-extension
  251. (file-name-nondirectory bfname)))
  252. "UNTITLED"))
  253. (email (plist-get opt-plist :email))
  254. (language (plist-get opt-plist :language))
  255. (quote-re0 (concat "^\\(" org-quote-string "\\)\\( +\\|[ \t]*$\\)"))
  256. (todo nil)
  257. (lang-words nil)
  258. (region
  259. (buffer-substring
  260. (if (org-region-active-p) (region-beginning) (point-min))
  261. (if (org-region-active-p) (region-end) (point-max))))
  262. (org-export-footnotes-seen nil)
  263. (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
  264. (lines (org-split-string
  265. (org-export-preprocess-string
  266. region
  267. :for-backend 'ascii
  268. :skip-before-1st-heading
  269. (plist-get opt-plist :skip-before-1st-heading)
  270. :drawers (plist-get opt-plist :drawers)
  271. :tags (plist-get opt-plist :tags)
  272. :priority (plist-get opt-plist :priority)
  273. :footnotes (plist-get opt-plist :footnotes)
  274. :timestamps (plist-get opt-plist :timestamps)
  275. :todo-keywords (plist-get opt-plist :todo-keywords)
  276. :tasks (plist-get opt-plist :tasks)
  277. :verbatim-multiline t
  278. :select-tags (plist-get opt-plist :select-tags)
  279. :exclude-tags (plist-get opt-plist :exclude-tags)
  280. :archived-trees
  281. (plist-get opt-plist :archived-trees)
  282. :add-text (plist-get opt-plist :text))
  283. "\n"))
  284. thetoc have-headings first-heading-pos
  285. table-open table-buffer link-buffer link type path desc desc0 rpl wrap fnc)
  286. (let ((inhibit-read-only t))
  287. (org-unmodified
  288. (remove-text-properties (point-min) (point-max)
  289. '(:org-license-to-kill t))))
  290. (setq org-min-level (org-get-min-level lines level-offset))
  291. (setq org-last-level org-min-level)
  292. (org-init-section-numbers)
  293. (setq lang-words (or (assoc language org-export-language-setup)
  294. (assoc "en" org-export-language-setup)))
  295. (set-buffer buffer)
  296. (erase-buffer)
  297. (fundamental-mode)
  298. (org-install-letbind)
  299. ;; create local variables for all options, to make sure all called
  300. ;; functions get the correct information
  301. (mapc (lambda (x)
  302. (set (make-local-variable (nth 2 x))
  303. (plist-get opt-plist (car x))))
  304. org-export-plist-vars)
  305. (org-set-local 'org-odd-levels-only odd)
  306. (setq umax (if arg (prefix-numeric-value arg)
  307. org-export-headline-levels))
  308. (setq umax-toc (if (integerp org-export-with-toc)
  309. (min org-export-with-toc umax)
  310. umax))
  311. ;; File header
  312. (unless body-only
  313. (when (and title (not (string= "" title)))
  314. (org-insert-centered title ?=)
  315. (insert "\n"))
  316. (if (and (or author email)
  317. org-export-author-info)
  318. (insert (concat (nth 1 lang-words) ": " (or author "")
  319. (if (and org-export-email-info
  320. email (string-match "\\S-" email))
  321. (concat " <" email ">") "")
  322. "\n")))
  323. (cond
  324. ((and date (string-match "%" date))
  325. (setq date (format-time-string date)))
  326. (date)
  327. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  328. (if (and date org-export-time-stamp-file)
  329. (insert (concat (nth 2 lang-words) ": " date"\n")))
  330. (unless (= (point) (point-min))
  331. (insert "\n\n")))
  332. (if (and org-export-with-toc (not body-only))
  333. (progn
  334. (push (concat (nth 3 lang-words) "\n") thetoc)
  335. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  336. "\n") thetoc)
  337. (mapc #'(lambda (line)
  338. (if (string-match org-todo-line-regexp
  339. line)
  340. ;; This is a headline
  341. (progn
  342. (setq have-headings t)
  343. (setq level (- (match-end 1) (match-beginning 1)
  344. level-offset)
  345. level (org-tr-level level)
  346. txt (match-string 3 line)
  347. todo
  348. (or (and org-export-mark-todo-in-toc
  349. (match-beginning 2)
  350. (not (member (match-string 2 line)
  351. org-done-keywords)))
  352. ; TODO, not DONE
  353. (and org-export-mark-todo-in-toc
  354. (= level umax-toc)
  355. (org-search-todo-below
  356. line lines level))))
  357. (setq txt (org-html-expand-for-ascii txt))
  358. (while (string-match org-bracket-link-regexp txt)
  359. (setq txt
  360. (replace-match
  361. (match-string (if (match-end 2) 3 1) txt)
  362. t t txt)))
  363. (if (and (memq org-export-with-tags '(not-in-toc nil))
  364. (string-match
  365. (org-re "[ \t]+:[[:alnum:]_@#%:]+:[ \t]*$")
  366. txt))
  367. (setq txt (replace-match "" t t txt)))
  368. (if (string-match quote-re0 txt)
  369. (setq txt (replace-match "" t t txt 1)))
  370. (if org-export-with-section-numbers
  371. (setq txt (concat (org-section-number level)
  372. " " txt)))
  373. (if (<= level umax-toc)
  374. (progn
  375. (push
  376. (concat
  377. (make-string
  378. (* (max 0 (- level org-min-level)) 4) ?\ )
  379. (format (if todo "%s (*)\n" "%s\n") txt))
  380. thetoc)
  381. (setq org-last-level level))
  382. ))))
  383. lines)
  384. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  385. (org-init-section-numbers)
  386. (while (setq line (pop lines))
  387. (when (and link-buffer (string-match org-outline-regexp-bol line))
  388. (org-export-ascii-push-links (nreverse link-buffer))
  389. (setq link-buffer nil))
  390. (setq wrap nil)
  391. ;; Remove the quoted HTML tags.
  392. (setq line (org-html-expand-for-ascii line))
  393. ;; Replace links with the description when possible
  394. (while (string-match org-bracket-link-analytic-regexp++ line)
  395. (setq path (match-string 3 line)
  396. link (concat (match-string 1 line) path)
  397. type (match-string 2 line)
  398. desc0 (match-string 5 line)
  399. desc (or desc0 link))
  400. (if (and (> (length link) 8)
  401. (equal (substring link 0 8) "coderef:"))
  402. (setq line (replace-match
  403. (format (org-export-get-coderef-format (substring link 8) desc)
  404. (cdr (assoc
  405. (substring link 8)
  406. org-export-code-refs)))
  407. t t line))
  408. (setq rpl (concat "[" desc "]"))
  409. (if (functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  410. (setq rpl (or (save-match-data
  411. (funcall fnc (org-link-unescape path)
  412. desc0 'ascii))
  413. rpl))
  414. (when (and desc0 (not (equal desc0 link)))
  415. (if org-export-ascii-links-to-notes
  416. (push (cons desc0 link) link-buffer)
  417. (setq rpl (concat rpl " (" link ")")
  418. wrap (+ (length line) (- (length (match-string 0 line)))
  419. (length desc))))))
  420. (setq line (replace-match rpl t t line))))
  421. (when custom-times
  422. (setq line (org-translate-time line)))
  423. (cond
  424. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  425. ;; a Headline
  426. (setq first-heading-pos (or first-heading-pos (point)))
  427. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  428. level-offset))
  429. txt (match-string 2 line))
  430. (org-ascii-level-start level txt umax lines))
  431. ((and org-export-with-tables
  432. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  433. (if (not table-open)
  434. ;; New table starts
  435. (setq table-open t table-buffer nil))
  436. ;; Accumulate lines
  437. (setq table-buffer (cons line table-buffer))
  438. (when (or (not lines)
  439. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  440. (car lines))))
  441. (setq table-open nil
  442. table-buffer (nreverse table-buffer))
  443. (insert (mapconcat
  444. (lambda (x)
  445. (org-fix-indentation x org-ascii-current-indentation))
  446. (org-format-table-ascii table-buffer)
  447. "\n") "\n")))
  448. (t
  449. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)"
  450. line)
  451. (setq line (replace-match "\\1\\3:" t nil line)))
  452. (setq line (org-fix-indentation line org-ascii-current-indentation))
  453. ;; Remove forced line breaks
  454. (if (string-match "\\\\\\\\[ \t]*$" line)
  455. (setq line (replace-match "" t t line)))
  456. (if (and org-export-with-fixed-width
  457. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  458. (setq line (replace-match "\\1" nil nil line))
  459. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  460. (insert line "\n"))))
  461. (org-export-ascii-push-links (nreverse link-buffer))
  462. (normal-mode)
  463. ;; insert the table of contents
  464. (when thetoc
  465. (goto-char (point-min))
  466. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  467. (progn
  468. (goto-char (match-beginning 0))
  469. (replace-match ""))
  470. (goto-char first-heading-pos))
  471. (mapc 'insert thetoc)
  472. (or (looking-at "[ \t]*\n[ \t]*\n")
  473. (insert "\n\n")))
  474. ;; Convert whitespace place holders
  475. (goto-char (point-min))
  476. (let (beg end)
  477. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  478. (setq end (next-single-property-change beg 'org-whitespace))
  479. (goto-char beg)
  480. (delete-region beg end)
  481. (insert (make-string (- end beg) ?\ ))))
  482. ;; remove display and invisible chars
  483. (let (beg end)
  484. (goto-char (point-min))
  485. (while (setq beg (next-single-property-change (point) 'display))
  486. (setq end (next-single-property-change beg 'display))
  487. (delete-region beg end)
  488. (goto-char beg)
  489. (insert "=>"))
  490. (goto-char (point-min))
  491. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  492. (setq end (next-single-property-change beg 'org-cwidth))
  493. (delete-region beg end)
  494. (goto-char beg)))
  495. (run-hooks 'org-export-ascii-final-hook)
  496. (or to-buffer (save-buffer))
  497. (goto-char (point-min))
  498. (or (org-export-push-to-kill-ring "ASCII")
  499. (message "Exporting... done"))
  500. ;; Return the buffer or a string, according to how this function was called
  501. (if (eq to-buffer 'string)
  502. (prog1 (buffer-substring (point-min) (point-max))
  503. (kill-buffer (current-buffer)))
  504. (current-buffer))))
  505. (defun org-export-ascii-preprocess (parameters)
  506. "Do extra work for ASCII export."
  507. ;;
  508. ;; Realign tables to get rid of narrowing
  509. (when org-export-ascii-table-widen-columns
  510. (let ((org-table-do-narrow nil))
  511. (goto-char (point-min))
  512. (org-ascii-replace-entities)
  513. (goto-char (point-min))
  514. (org-table-map-tables
  515. (lambda () (org-if-unprotected (org-table-align)))
  516. 'quietly)))
  517. ;; Put quotes around verbatim text
  518. (goto-char (point-min))
  519. (while (re-search-forward org-verbatim-re nil t)
  520. (org-if-unprotected-at (match-beginning 4)
  521. (goto-char (match-end 2))
  522. (backward-delete-char 1) (insert "'")
  523. (goto-char (match-beginning 2))
  524. (delete-char 1) (insert "`")
  525. (goto-char (match-end 2))))
  526. ;; Remove target markers
  527. (goto-char (point-min))
  528. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  529. (org-if-unprotected-at (match-beginning 1)
  530. (replace-match "\\1\\2")))
  531. ;; Remove list start counters
  532. (goto-char (point-min))
  533. (while (org-list-search-forward
  534. "\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*" nil t)
  535. (replace-match ""))
  536. (remove-text-properties
  537. (point-min) (point-max)
  538. '(face nil font-lock-fontified nil font-lock-multiline nil line-prefix nil wrap-prefix nil)))
  539. (defun org-html-expand-for-ascii (line)
  540. "Handle quoted HTML for ASCII export."
  541. (if org-export-html-expand
  542. (while (string-match "@<[^<>\n]*>" line)
  543. ;; We just remove the tags for now.
  544. (setq line (replace-match "" nil nil line))))
  545. line)
  546. (defun org-ascii-replace-entities ()
  547. "Replace entities with the ASCII representation."
  548. (let (e)
  549. (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" nil t)
  550. (org-if-unprotected-at (match-beginning 1)
  551. (setq e (org-entity-get-representation (match-string 1)
  552. org-export-ascii-entities))
  553. (and e (replace-match e t t))))))
  554. (defun org-export-ascii-wrap (line where)
  555. "Wrap LINE at or before WHERE."
  556. (let ((ind (org-get-indentation line))
  557. pos)
  558. (catch 'found
  559. (loop for i from where downto (/ where 2) do
  560. (and (equal (aref line i) ?\ )
  561. (setq pos i)
  562. (throw 'found t))))
  563. (if pos
  564. (concat (substring line 0 pos) "\n"
  565. (make-string ind ?\ )
  566. (substring line (1+ pos)))
  567. line)))
  568. (defun org-export-ascii-push-links (link-buffer)
  569. "Push out links in the buffer."
  570. (when link-buffer
  571. ;; We still have links to push out.
  572. (insert "\n")
  573. (let ((ind ""))
  574. (save-match-data
  575. (if (save-excursion
  576. (re-search-backward
  577. (concat "^\\(\\([ \t]*\\)\\|\\("
  578. org-outline-regexp
  579. "\\)\\)[^ \t\n]") nil t))
  580. (setq ind (or (match-string 2)
  581. (make-string (length (match-string 3)) ?\ )))))
  582. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  583. link-buffer))
  584. (insert "\n")))
  585. (defun org-ascii-level-start (level title umax &optional lines)
  586. "Insert a new level in ASCII export."
  587. (let (char (n (- level umax 1)) (ind 0))
  588. (if (> level umax)
  589. (progn
  590. (insert (make-string (* 2 n) ?\ )
  591. (char-to-string (nth (% n (length org-export-ascii-bullets))
  592. org-export-ascii-bullets))
  593. " " title "\n")
  594. ;; find the indentation of the next non-empty line
  595. (catch 'stop
  596. (while lines
  597. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  598. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  599. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  600. (pop lines)))
  601. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  602. (if (or (not (equal (char-before) ?\n))
  603. (not (equal (char-before (1- (point))) ?\n)))
  604. (insert "\n"))
  605. (setq char (or (nth (1- level) org-export-ascii-underline)
  606. (car (last org-export-ascii-underline))))
  607. (unless org-export-with-tags
  608. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  609. (setq title (replace-match "" t t title))))
  610. (if org-export-with-section-numbers
  611. (setq title (concat (org-section-number level) " " title)))
  612. (insert title "\n" (make-string (string-width title) char) "\n")
  613. (setq org-ascii-current-indentation '(0 . 0)))))
  614. (defun org-insert-centered (s &optional underline)
  615. "Insert the string S centered and underline it with character UNDERLINE."
  616. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  617. (insert (make-string ind ?\ ) s "\n")
  618. (if underline
  619. (insert (make-string ind ?\ )
  620. (make-string (string-width s) underline)
  621. "\n"))))
  622. (defvar org-table-colgroup-info nil)
  623. (defun org-format-table-ascii (lines)
  624. "Format a table for ascii export."
  625. (if (stringp lines)
  626. (setq lines (org-split-string lines "\n")))
  627. (if (not (string-match "^[ \t]*|" (car lines)))
  628. ;; Table made by table.el - test for spanning
  629. lines
  630. ;; A normal org table
  631. ;; Get rid of hlines at beginning and end
  632. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  633. (setq lines (nreverse lines))
  634. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  635. (setq lines (nreverse lines))
  636. (when org-export-table-remove-special-lines
  637. ;; Check if the table has a marking column. If yes remove the
  638. ;; column and the special lines
  639. (setq lines (org-table-clean-before-export lines)))
  640. ;; Get rid of the vertical lines except for grouping
  641. (if org-export-ascii-table-keep-all-vertical-lines
  642. lines
  643. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  644. rtn line vl1 start)
  645. (while (setq line (pop lines))
  646. (if (string-match org-table-hline-regexp line)
  647. (and (string-match "|\\(.*\\)|" line)
  648. (setq line (replace-match " \\1" t nil line)))
  649. (setq start 0 vl1 vl)
  650. (while (string-match "|" line start)
  651. (setq start (match-end 0))
  652. (or (pop vl1) (setq line (replace-match " " t t line)))))
  653. (push line rtn))
  654. (nreverse rtn)))))
  655. (defun org-colgroup-info-to-vline-list (info)
  656. (let (vl new last)
  657. (while info
  658. (setq last new new (pop info))
  659. (if (or (memq last '(:end :startend))
  660. (memq new '(:start :startend)))
  661. (push t vl)
  662. (push nil vl)))
  663. (setq vl (nreverse vl))
  664. (and vl (setcar vl nil))
  665. vl))
  666. (provide 'org-ascii)
  667. ;;; org-ascii.el ends here