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.4
  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 (interactive-p)
  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 (interactive-p)
  86. '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 (interactive-p)
  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 (interactive-p)
  98. '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 (interactive-p)
  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 (interactive-p) (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-ascii t
  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. :verbatim-multiline t
  276. :select-tags (plist-get opt-plist :select-tags)
  277. :exclude-tags (plist-get opt-plist :exclude-tags)
  278. :archived-trees
  279. (plist-get opt-plist :archived-trees)
  280. :add-text (plist-get opt-plist :text))
  281. "\n"))
  282. thetoc have-headings first-heading-pos
  283. table-open table-buffer link-buffer link type path desc desc0 rpl wrap fnc)
  284. (let ((inhibit-read-only t))
  285. (org-unmodified
  286. (remove-text-properties (point-min) (point-max)
  287. '(:org-license-to-kill t))))
  288. (setq org-min-level (org-get-min-level lines level-offset))
  289. (setq org-last-level org-min-level)
  290. (org-init-section-numbers)
  291. (setq lang-words (or (assoc language org-export-language-setup)
  292. (assoc "en" org-export-language-setup)))
  293. (set-buffer buffer)
  294. (erase-buffer)
  295. (fundamental-mode)
  296. (org-install-letbind)
  297. ;; create local variables for all options, to make sure all called
  298. ;; functions get the correct information
  299. (mapc (lambda (x)
  300. (set (make-local-variable (nth 2 x))
  301. (plist-get opt-plist (car x))))
  302. org-export-plist-vars)
  303. (org-set-local 'org-odd-levels-only odd)
  304. (setq umax (if arg (prefix-numeric-value arg)
  305. org-export-headline-levels))
  306. (setq umax-toc (if (integerp org-export-with-toc)
  307. (min org-export-with-toc umax)
  308. umax))
  309. ;; File header
  310. (unless body-only
  311. (when (and title (not (string= "" title)))
  312. (org-insert-centered title ?=)
  313. (insert "\n"))
  314. (if (and (or author email)
  315. org-export-author-info)
  316. (insert (concat (nth 1 lang-words) ": " (or author "")
  317. (if (and org-export-email-info
  318. email (string-match "\\S-" email))
  319. (concat " <" email ">") "")
  320. "\n")))
  321. (cond
  322. ((and date (string-match "%" date))
  323. (setq date (format-time-string date)))
  324. (date)
  325. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  326. (if (and date org-export-time-stamp-file)
  327. (insert (concat (nth 2 lang-words) ": " date"\n")))
  328. (unless (= (point) (point-min))
  329. (insert "\n\n")))
  330. (if (and org-export-with-toc (not body-only))
  331. (progn
  332. (push (concat (nth 3 lang-words) "\n") thetoc)
  333. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  334. "\n") thetoc)
  335. (mapc '(lambda (line)
  336. (if (string-match org-todo-line-regexp
  337. line)
  338. ;; This is a headline
  339. (progn
  340. (setq have-headings t)
  341. (setq level (- (match-end 1) (match-beginning 1)
  342. level-offset)
  343. level (org-tr-level level)
  344. txt (match-string 3 line)
  345. todo
  346. (or (and org-export-mark-todo-in-toc
  347. (match-beginning 2)
  348. (not (member (match-string 2 line)
  349. org-done-keywords)))
  350. ; TODO, not DONE
  351. (and org-export-mark-todo-in-toc
  352. (= level umax-toc)
  353. (org-search-todo-below
  354. line lines level))))
  355. (setq txt (org-html-expand-for-ascii txt))
  356. (while (string-match org-bracket-link-regexp txt)
  357. (setq txt
  358. (replace-match
  359. (match-string (if (match-end 2) 3 1) txt)
  360. t t txt)))
  361. (if (and (memq org-export-with-tags '(not-in-toc nil))
  362. (string-match
  363. (org-re "[ \t]+:[[:alnum:]_@#%:]+:[ \t]*$")
  364. txt))
  365. (setq txt (replace-match "" t t txt)))
  366. (if (string-match quote-re0 txt)
  367. (setq txt (replace-match "" t t txt)))
  368. (if org-export-with-section-numbers
  369. (setq txt (concat (org-section-number level)
  370. " " txt)))
  371. (if (<= level umax-toc)
  372. (progn
  373. (push
  374. (concat
  375. (make-string
  376. (* (max 0 (- level org-min-level)) 4) ?\ )
  377. (format (if todo "%s (*)\n" "%s\n") txt))
  378. thetoc)
  379. (setq org-last-level level))
  380. ))))
  381. lines)
  382. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  383. (org-init-section-numbers)
  384. (while (setq line (pop lines))
  385. (when (and link-buffer (string-match "^\\*+ " line))
  386. (org-export-ascii-push-links (nreverse link-buffer))
  387. (setq link-buffer nil))
  388. (setq wrap nil)
  389. ;; Remove the quoted HTML tags.
  390. (setq line (org-html-expand-for-ascii line))
  391. ;; Replace links with the description when possible
  392. (while (string-match org-bracket-link-analytic-regexp++ line)
  393. (setq path (match-string 3 line)
  394. link (concat (match-string 1 line) path)
  395. type (match-string 2 line)
  396. desc0 (match-string 5 line)
  397. desc (or desc0 link))
  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. (defun org-export-ascii-preprocess (parameters)
  504. "Do extra work for ASCII export."
  505. ;;
  506. ;; Realign tables to get rid of narrowing
  507. (when org-export-ascii-table-widen-columns
  508. (let ((org-table-do-narrow nil))
  509. (goto-char (point-min))
  510. (org-ascii-replace-entities)
  511. (goto-char (point-min))
  512. (org-table-map-tables
  513. (lambda () (org-if-unprotected (org-table-align)))
  514. 'quietly)))
  515. ;; Put quotes around verbatim text
  516. (goto-char (point-min))
  517. (while (re-search-forward org-verbatim-re nil t)
  518. (org-if-unprotected-at (match-beginning 4)
  519. (goto-char (match-end 2))
  520. (backward-delete-char 1) (insert "'")
  521. (goto-char (match-beginning 2))
  522. (delete-char 1) (insert "`")
  523. (goto-char (match-end 2))))
  524. ;; Remove target markers
  525. (goto-char (point-min))
  526. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  527. (org-if-unprotected-at (match-beginning 1)
  528. (replace-match "\\1\\2")))
  529. ;; Remove list start counters
  530. (goto-char (point-min))
  531. (while (org-search-forward-unenclosed
  532. "\\[@\\(?:start:\\)?[0-9]+\\][ \t]*" nil t)
  533. (replace-match ""))
  534. (remove-text-properties
  535. (point-min) (point-max)
  536. '(face nil font-lock-fontified nil font-lock-multiline nil line-prefix nil wrap-prefix nil)))
  537. (defun org-html-expand-for-ascii (line)
  538. "Handle quoted HTML for ASCII export."
  539. (if org-export-html-expand
  540. (while (string-match "@<[^<>\n]*>" line)
  541. ;; We just remove the tags for now.
  542. (setq line (replace-match "" nil nil line))))
  543. line)
  544. (defun org-ascii-replace-entities ()
  545. "Replace entities with the ASCII representation."
  546. (let (e)
  547. (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" nil t)
  548. (org-if-unprotected-at (match-beginning 1)
  549. (setq e (org-entity-get-representation (match-string 1)
  550. org-export-ascii-entities))
  551. (and e (replace-match e t t))))))
  552. (defun org-export-ascii-wrap (line where)
  553. "Wrap LINE at or before WHERE."
  554. (let ((ind (org-get-indentation line))
  555. pos)
  556. (catch 'found
  557. (loop for i from where downto (/ where 2) do
  558. (and (equal (aref line i) ?\ )
  559. (setq pos i)
  560. (throw 'found t))))
  561. (if pos
  562. (concat (substring line 0 pos) "\n"
  563. (make-string ind ?\ )
  564. (substring line (1+ pos)))
  565. line)))
  566. (defun org-export-ascii-push-links (link-buffer)
  567. "Push out links in the buffer."
  568. (when link-buffer
  569. ;; We still have links to push out.
  570. (insert "\n")
  571. (let ((ind ""))
  572. (save-match-data
  573. (if (save-excursion
  574. (re-search-backward
  575. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  576. (setq ind (or (match-string 2)
  577. (make-string (length (match-string 3)) ?\ )))))
  578. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  579. link-buffer))
  580. (insert "\n")))
  581. (defun org-ascii-level-start (level title umax &optional lines)
  582. "Insert a new level in ASCII export."
  583. (let (char (n (- level umax 1)) (ind 0))
  584. (if (> level umax)
  585. (progn
  586. (insert (make-string (* 2 n) ?\ )
  587. (char-to-string (nth (% n (length org-export-ascii-bullets))
  588. org-export-ascii-bullets))
  589. " " title "\n")
  590. ;; find the indentation of the next non-empty line
  591. (catch 'stop
  592. (while lines
  593. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  594. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  595. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  596. (pop lines)))
  597. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  598. (if (or (not (equal (char-before) ?\n))
  599. (not (equal (char-before (1- (point))) ?\n)))
  600. (insert "\n"))
  601. (setq char (or (nth (max (- umax level) 0)
  602. (reverse 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