org-ascii.el 21 KB

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