org-ascii.el 25 KB

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