org-ascii.el 25 KB

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