org-ascii.el 24 KB

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