org-ascii.el 25 KB

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