org-ascii.el 21 KB

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