org-ascii.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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: 6.26d
  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. (defgroup org-export-ascii nil
  26. "Options specific for ASCII export of Org-mode files."
  27. :tag "Org Export ASCII"
  28. :group 'org-export)
  29. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  30. "Characters for underlining headings in ASCII export.
  31. In the given sequence, these characters will be used for level 1, 2, ..."
  32. :group 'org-export-ascii
  33. :type '(repeat character))
  34. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  35. "Bullet characters for headlines converted to lists in ASCII export.
  36. The first character is used for the first lest level generated in this
  37. way, and so on. If there are more levels than characters given here,
  38. the list will be repeated.
  39. Note that plain lists will keep the same bullets as the have in the
  40. Org-mode file."
  41. :group 'org-export-ascii
  42. :type '(repeat character))
  43. (defcustom org-export-ascii-links-to-notes t
  44. "Non-nil means, convert links to notes before the next headline.
  45. When nil, the link will be exported in place. If the line becomes long
  46. in this way, it will be wrapped."
  47. :group 'org-export-ascii
  48. :type 'boolean)
  49. ;;; ASCII export
  50. (defvar org-ascii-current-indentation nil) ; For communication
  51. ;;;###autoload
  52. (defun org-export-as-ascii (arg)
  53. "Export the outline as a pretty ASCII file.
  54. If there is an active region, export only the region.
  55. The prefix ARG specifies how many levels of the outline should become
  56. underlined headlines. The default is 3."
  57. (interactive "P")
  58. (setq-default org-todo-line-regexp org-todo-line-regexp)
  59. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  60. (org-infile-export-plist)))
  61. (region-p (org-region-active-p))
  62. (rbeg (and region-p (region-beginning)))
  63. (rend (and region-p (region-end)))
  64. (subtree-p
  65. (when region-p
  66. (save-excursion
  67. (goto-char rbeg)
  68. (and (org-at-heading-p)
  69. (>= (org-end-of-subtree t t) rend)))))
  70. (level-offset (if subtree-p
  71. (save-excursion
  72. (goto-char rbeg)
  73. (+ (funcall outline-level)
  74. (if org-odd-levels-only 1 0)))
  75. 0))
  76. (opt-plist (setq org-export-opt-plist
  77. (if subtree-p
  78. (org-export-add-subtree-options opt-plist rbeg)
  79. opt-plist)))
  80. (custom-times org-display-custom-times)
  81. (org-ascii-current-indentation '(0 . 0))
  82. (level 0) line txt
  83. (umax nil)
  84. (umax-toc nil)
  85. (case-fold-search nil)
  86. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  87. (filename (concat (file-name-as-directory
  88. (org-export-directory :ascii opt-plist))
  89. (file-name-sans-extension
  90. (or (and subtree-p
  91. (org-entry-get (region-beginning)
  92. "EXPORT_FILE_NAME" t))
  93. (file-name-nondirectory bfname)))
  94. ".txt"))
  95. (filename (if (equal (file-truename filename)
  96. (file-truename bfname))
  97. (concat filename ".txt")
  98. filename))
  99. (buffer (find-file-noselect filename))
  100. (org-levels-open (make-vector org-level-max nil))
  101. (odd org-odd-levels-only)
  102. (date (plist-get opt-plist :date))
  103. (author (plist-get opt-plist :author))
  104. (title (or (and subtree-p (org-export-get-title-from-subtree))
  105. (plist-get opt-plist :title)
  106. (and (not
  107. (plist-get opt-plist :skip-before-1st-heading))
  108. (org-export-grab-title-from-buffer))
  109. (file-name-sans-extension
  110. (file-name-nondirectory bfname))))
  111. (email (plist-get opt-plist :email))
  112. (language (plist-get opt-plist :language))
  113. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  114. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  115. (todo nil)
  116. (lang-words nil)
  117. (region
  118. (buffer-substring
  119. (if (org-region-active-p) (region-beginning) (point-min))
  120. (if (org-region-active-p) (region-end) (point-max))))
  121. (lines (org-split-string
  122. (org-export-preprocess-string
  123. region
  124. :for-ascii t
  125. :skip-before-1st-heading
  126. (plist-get opt-plist :skip-before-1st-heading)
  127. :drawers (plist-get opt-plist :drawers)
  128. :tags (plist-get opt-plist :tags)
  129. :priority (plist-get opt-plist :priority)
  130. :footnotes (plist-get opt-plist :footnotes)
  131. :timestamps (plist-get opt-plist :timestamps)
  132. :todo-keywords (plist-get opt-plist :todo-keywords)
  133. :verbatim-multiline t
  134. :select-tags (plist-get opt-plist :select-tags)
  135. :exclude-tags (plist-get opt-plist :exclude-tags)
  136. :archived-trees
  137. (plist-get opt-plist :archived-trees)
  138. :add-text (plist-get opt-plist :text))
  139. "\n"))
  140. thetoc have-headings first-heading-pos
  141. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  142. (let ((inhibit-read-only t))
  143. (org-unmodified
  144. (remove-text-properties (point-min) (point-max)
  145. '(:org-license-to-kill t))))
  146. (setq org-min-level (org-get-min-level lines level-offset))
  147. (setq org-last-level org-min-level)
  148. (org-init-section-numbers)
  149. (find-file-noselect filename)
  150. (setq lang-words (or (assoc language org-export-language-setup)
  151. (assoc "en" org-export-language-setup)))
  152. (switch-to-buffer-other-window buffer)
  153. (erase-buffer)
  154. (fundamental-mode)
  155. ;; create local variables for all options, to make sure all called
  156. ;; functions get the correct information
  157. (mapc (lambda (x)
  158. (set (make-local-variable (nth 2 x))
  159. (plist-get opt-plist (car x))))
  160. org-export-plist-vars)
  161. (org-set-local 'org-odd-levels-only odd)
  162. (setq umax (if arg (prefix-numeric-value arg)
  163. org-export-headline-levels))
  164. (setq umax-toc (if (integerp org-export-with-toc)
  165. (min org-export-with-toc umax)
  166. umax))
  167. ;; File header
  168. (if title (org-insert-centered title ?=))
  169. (insert "\n")
  170. (if (and (or author email)
  171. org-export-author-info)
  172. (insert (concat (nth 1 lang-words) ": " (or author "")
  173. (if email (concat " <" email ">") "")
  174. "\n")))
  175. (cond
  176. ((and date (string-match "%" date))
  177. (setq date (format-time-string date)))
  178. (date)
  179. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  180. (if (and date org-export-time-stamp-file)
  181. (insert (concat (nth 2 lang-words) ": " date"\n")))
  182. (insert "\n\n")
  183. (if org-export-with-toc
  184. (progn
  185. (push (concat (nth 3 lang-words) "\n") thetoc)
  186. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  187. "\n") thetoc)
  188. (mapc '(lambda (line)
  189. (if (string-match org-todo-line-regexp
  190. line)
  191. ;; This is a headline
  192. (progn
  193. (setq have-headings t)
  194. (setq level (- (match-end 1) (match-beginning 1)
  195. level-offset)
  196. level (org-tr-level level)
  197. txt (match-string 3 line)
  198. todo
  199. (or (and org-export-mark-todo-in-toc
  200. (match-beginning 2)
  201. (not (member (match-string 2 line)
  202. org-done-keywords)))
  203. ; TODO, not DONE
  204. (and org-export-mark-todo-in-toc
  205. (= level umax-toc)
  206. (org-search-todo-below
  207. line lines level))))
  208. (setq txt (org-html-expand-for-ascii txt))
  209. (while (string-match org-bracket-link-regexp txt)
  210. (setq txt
  211. (replace-match
  212. (match-string (if (match-end 2) 3 1) txt)
  213. t t txt)))
  214. (if (and (memq org-export-with-tags '(not-in-toc nil))
  215. (string-match
  216. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  217. txt))
  218. (setq txt (replace-match "" t t txt)))
  219. (if (string-match quote-re0 txt)
  220. (setq txt (replace-match "" t t txt)))
  221. (if org-export-with-section-numbers
  222. (setq txt (concat (org-section-number level)
  223. " " txt)))
  224. (if (<= level umax-toc)
  225. (progn
  226. (push
  227. (concat
  228. (make-string
  229. (* (max 0 (- level org-min-level)) 4) ?\ )
  230. (format (if todo "%s (*)\n" "%s\n") txt))
  231. thetoc)
  232. (setq org-last-level level))
  233. ))))
  234. lines)
  235. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  236. (org-init-section-numbers)
  237. (while (setq line (pop lines))
  238. (when (and link-buffer (string-match "^\\*+ " line))
  239. (org-export-ascii-push-links (nreverse link-buffer))
  240. (setq link-buffer nil))
  241. (setq wrap nil)
  242. ;; Remove the quoted HTML tags.
  243. (setq line (org-html-expand-for-ascii line))
  244. ;; Replace links with the description when possible
  245. (while (string-match org-bracket-link-regexp line)
  246. (setq link (match-string 1 line)
  247. desc0 (match-string 3 line)
  248. desc (or desc0 (match-string 1 line)))
  249. (if (and (> (length link) 8)
  250. (equal (substring link 0 8) "coderef:"))
  251. (setq line (replace-match
  252. (format (org-export-get-coderef-format (substring link 8) desc)
  253. (cdr (assoc
  254. (substring link 8)
  255. org-export-code-refs)))
  256. t t line))
  257. (setq rpl (concat "["
  258. (or (match-string 3 line) (match-string 1 line))
  259. "]"))
  260. (when (and desc0 (not (equal desc0 link)))
  261. (if org-export-ascii-links-to-notes
  262. (push (cons desc0 link) link-buffer)
  263. (setq rpl (concat rpl " (" link ")")
  264. wrap (+ (length line) (- (length (match-string 0)))
  265. (length desc)))))
  266. (setq line (replace-match rpl t t line))))
  267. (when custom-times
  268. (setq line (org-translate-time line)))
  269. (cond
  270. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  271. ;; a Headline
  272. (setq first-heading-pos (or first-heading-pos (point)))
  273. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  274. level-offset))
  275. txt (match-string 2 line))
  276. (org-ascii-level-start level txt umax lines))
  277. ((and org-export-with-tables
  278. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  279. (if (not table-open)
  280. ;; New table starts
  281. (setq table-open t table-buffer nil))
  282. ;; Accumulate lines
  283. (setq table-buffer (cons line table-buffer))
  284. (when (or (not lines)
  285. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  286. (car lines))))
  287. (setq table-open nil
  288. table-buffer (nreverse table-buffer))
  289. (insert (mapconcat
  290. (lambda (x)
  291. (org-fix-indentation x org-ascii-current-indentation))
  292. (org-format-table-ascii table-buffer)
  293. "\n") "\n")))
  294. (t
  295. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  296. (setq line (replace-match "\\1\\3:" t nil line)))
  297. (setq line (org-fix-indentation line org-ascii-current-indentation))
  298. ;; Remove forced line breaks
  299. (if (string-match "\\\\\\\\[ \t]*$" line)
  300. (setq line (replace-match "" t t line)))
  301. (if (and org-export-with-fixed-width
  302. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  303. (setq line (replace-match "\\1" nil nil line))
  304. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  305. (insert line "\n"))))
  306. (org-export-ascii-push-links (nreverse link-buffer))
  307. (normal-mode)
  308. ;; insert the table of contents
  309. (when thetoc
  310. (goto-char (point-min))
  311. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  312. (progn
  313. (goto-char (match-beginning 0))
  314. (replace-match ""))
  315. (goto-char first-heading-pos))
  316. (mapc 'insert thetoc)
  317. (or (looking-at "[ \t]*\n[ \t]*\n")
  318. (insert "\n\n")))
  319. ;; Convert whitespace place holders
  320. (goto-char (point-min))
  321. (let (beg end)
  322. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  323. (setq end (next-single-property-change beg 'org-whitespace))
  324. (goto-char beg)
  325. (delete-region beg end)
  326. (insert (make-string (- end beg) ?\ ))))
  327. (save-buffer)
  328. ;; remove display and invisible chars
  329. (let (beg end)
  330. (goto-char (point-min))
  331. (while (setq beg (next-single-property-change (point) 'display))
  332. (setq end (next-single-property-change beg 'display))
  333. (delete-region beg end)
  334. (goto-char beg)
  335. (insert "=>"))
  336. (goto-char (point-min))
  337. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  338. (setq end (next-single-property-change beg 'org-cwidth))
  339. (delete-region beg end)
  340. (goto-char beg)))
  341. (goto-char (point-min))))
  342. (defun org-export-ascii-preprocess (parameters)
  343. "Do extra work for ASCII export"
  344. ;; Put quotes around verbatim text
  345. (goto-char (point-min))
  346. (while (re-search-forward org-verbatim-re nil t)
  347. (goto-char (match-end 2))
  348. (backward-delete-char 1) (insert "'")
  349. (goto-char (match-beginning 2))
  350. (delete-char 1) (insert "`")
  351. (goto-char (match-end 2)))
  352. ;; Remove target markers
  353. (goto-char (point-min))
  354. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  355. (replace-match "\\1\\2")))
  356. (defun org-html-expand-for-ascii (line)
  357. "Handle quoted HTML for ASCII export."
  358. (if org-export-html-expand
  359. (while (string-match "@<[^<>\n]*>" line)
  360. ;; We just remove the tags for now.
  361. (setq line (replace-match "" nil nil line))))
  362. line)
  363. (defun org-export-ascii-wrap (line where)
  364. "Wrap LINE at or before WHERE."
  365. (let ((ind (org-get-indentation line))
  366. pos)
  367. (catch 'found
  368. (loop for i from where downto (/ where 2) do
  369. (and (equal (aref line i) ?\ )
  370. (setq pos i)
  371. (throw 'found t))))
  372. (if pos
  373. (concat (substring line 0 pos) "\n"
  374. (make-string ind ?\ )
  375. (substring line (1+ pos)))
  376. line)))
  377. (defun org-export-ascii-push-links (link-buffer)
  378. "Push out links in the buffer."
  379. (when link-buffer
  380. ;; We still have links to push out.
  381. (insert "\n")
  382. (let ((ind ""))
  383. (save-match-data
  384. (if (save-excursion
  385. (re-search-backward
  386. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  387. (setq ind (or (match-string 2)
  388. (make-string (length (match-string 3)) ?\ )))))
  389. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  390. link-buffer))
  391. (insert "\n")))
  392. (defun org-ascii-level-start (level title umax &optional lines)
  393. "Insert a new level in ASCII export."
  394. (let (char (n (- level umax 1)) (ind 0))
  395. (if (> level umax)
  396. (progn
  397. (insert (make-string (* 2 n) ?\ )
  398. (char-to-string (nth (% n (length org-export-ascii-bullets))
  399. org-export-ascii-bullets))
  400. " " title "\n")
  401. ;; find the indentation of the next non-empty line
  402. (catch 'stop
  403. (while lines
  404. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  405. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  406. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  407. (pop lines)))
  408. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  409. (if (or (not (equal (char-before) ?\n))
  410. (not (equal (char-before (1- (point))) ?\n)))
  411. (insert "\n"))
  412. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  413. (unless org-export-with-tags
  414. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  415. (setq title (replace-match "" t t title))))
  416. (if org-export-with-section-numbers
  417. (setq title (concat (org-section-number level) " " title)))
  418. (insert title "\n" (make-string (string-width title) char) "\n")
  419. (setq org-ascii-current-indentation '(0 . 0)))))
  420. (defun org-insert-centered (s &optional underline)
  421. "Insert the string S centered and underline it with character UNDERLINE."
  422. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  423. (insert (make-string ind ?\ ) s "\n")
  424. (if underline
  425. (insert (make-string ind ?\ )
  426. (make-string (string-width s) underline)
  427. "\n"))))
  428. (defvar org-table-colgroup-info nil)
  429. (defun org-format-table-ascii (lines)
  430. "Format a table for ascii export."
  431. (if (stringp lines)
  432. (setq lines (org-split-string lines "\n")))
  433. (if (not (string-match "^[ \t]*|" (car lines)))
  434. ;; Table made by table.el - test for spanning
  435. lines
  436. ;; A normal org table
  437. ;; Get rid of hlines at beginning and end
  438. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  439. (setq lines (nreverse lines))
  440. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  441. (setq lines (nreverse lines))
  442. (when org-export-table-remove-special-lines
  443. ;; Check if the table has a marking column. If yes remove the
  444. ;; column and the special lines
  445. (setq lines (org-table-clean-before-export lines)))
  446. ;; Get rid of the vertical lines except for grouping
  447. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  448. rtn line vl1 start)
  449. (while (setq line (pop lines))
  450. (if (string-match org-table-hline-regexp line)
  451. (and (string-match "|\\(.*\\)|" line)
  452. (setq line (replace-match " \\1" t nil line)))
  453. (setq start 0 vl1 vl)
  454. (while (string-match "|" line start)
  455. (setq start (match-end 0))
  456. (or (pop vl1) (setq line (replace-match " " t t line)))))
  457. (push line rtn))
  458. (nreverse rtn))))
  459. (defun org-colgroup-info-to-vline-list (info)
  460. (let (vl new last)
  461. (while info
  462. (setq last new new (pop info))
  463. (if (or (memq last '(:end :startend))
  464. (memq new '(:start :startend)))
  465. (push t vl)
  466. (push nil vl)))
  467. (setq vl (nreverse vl))
  468. (and vl (setcar vl nil))
  469. vl))
  470. (provide 'org-ascii)
  471. ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
  472. ;;; org-ascii.el ends here