org-ascii.el 24 KB

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