org-export-generic.el 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. ;; org-export-generic.el --- Export frameworg with custom backends
  2. ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
  3. ;; Author: Wes Hardaker <hardaker at users dot sourceforge dot net>
  4. ;; Keywords: outlines, hypermedia, calendar, wp, export
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 6.25trans
  7. ;; Acks: Much of this code was stolen form the ascii export from Carsten
  8. ;;
  9. ;; This file is not 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. ;;
  24. ;; OVERVIEW
  25. ;;
  26. ;; IMPORTANT: IF YOU WANT TO WRITE A NEW EXPORTER FOR ORG, PLEASE
  27. ;; CHECK contrib/lisp/org-export.el -- ORG-EXPORT-GENERIC.EL, WHILE
  28. ;; STILL USEFUL, SHOULD NOT BE USED FOR NEW EXPORTERS.
  29. ;;
  30. ;; org-export-generic is basically a simple translation system that
  31. ;; knows how to parse at least most of a .org buffer and then add
  32. ;; various formatting prefixes before and after each section type. It
  33. ;; does this by examining a property list stored in org-generic-alist.
  34. ;; You can dynamically add propety lists of your own using the
  35. ;; org-set-generic-type function:
  36. ;;
  37. ;; (org-set-generic-type
  38. ;; "really-basic-text"
  39. ;; '(:file-suffix ".txt"
  40. ;; :key-binding ?R
  41. ;;
  42. ;; :title-format "=== %s ===\n"
  43. ;; :body-header-section-numbers t
  44. ;; :body-header-section-number-format "%s) "
  45. ;; :body-section-header-prefix "\n"
  46. ;; :body-section-header-suffix "\n"
  47. ;; :body-line-format " %s\n"
  48. ;; :body-line-wrap 75
  49. ;; ))
  50. ;;
  51. ;; Note: Upper case key-bindings are reserved for your use. Lower
  52. ;; case key bindings may conflict with future export-generic
  53. ;; publications.
  54. ;;
  55. ;; Then run org-export (ctrl-c ctrl-e) and select generic or run
  56. ;; org-export-generic. You'll then be prompted with a list of export
  57. ;; types to choose from which will include your new type assigned to
  58. ;; the key "r".
  59. ;;
  60. ;; ----------------------------------------------------------------------
  61. ;;
  62. ;; TODO (non-ordered)
  63. ;; * handle function references
  64. ;; * handle other types of multi-complex-listy-things to do
  65. ;; ideas: (t ?- "%s" ?-)
  66. ;; * handle indent specifiers better
  67. ;; ideas: (4 ?\ "%s")
  68. ;; * need flag to remove indents from body text
  69. ;; * handle links
  70. ;; * handle internationalization strings better
  71. ;; * date/author/etc needs improvment (internationalization too)
  72. ;; * allow specifying of section ordering
  73. ;; ideas: :ordering ("header" "toc" "body" "footer")
  74. ;; ^ matches current hard coded ordering
  75. ;; * err, actually *do* a footer
  76. ;; * deal with usage of org globals
  77. ;; *** should we even consider them, or let the per-section specifiers do it
  78. ;; *** answer: remove; mostly removed now
  79. ;; * deal with interactive support for picking a export specifier label
  80. ;; * char specifiers that need extra length because of formatting
  81. ;; idea: (?- 4) for 4-longer
  82. ;; * centering specifier
  83. ;; idea: ('center " -- %s -- ")
  84. ;; * remove more of the unneeded export-to-ascii copy code
  85. ;; * tags
  86. ;; *** supported now, but need separate format per tag
  87. ;; *** allow different open/closing prefixes
  88. ;; * properties
  89. ;; * drawers
  90. ;; * Escape camel-case for wiki exporters.
  91. ;; * Adjust to depth limits on headers --- need to roll-over from headers
  92. ;; to lists, as per other exporters
  93. ;; * optmization (many plist extracts should be in let vars)
  94. ;; * define defcustom spec for the specifier list
  95. ;; * fonts: at least monospace is not handled at all here.
  96. ;;
  97. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  98. ;;
  99. ;;; Commentary:
  100. (require 'org-exp)
  101. (require 'assoc)
  102. (eval-when-compile (require 'cl))
  103. (defgroup org-export-generic nil
  104. "Options specific for ASCII export of Org-mode files."
  105. :tag "Org Export ASCII"
  106. :group 'org-export)
  107. (defcustom org-export-generic-links-to-notes t
  108. "Non-nil means convert links to notes before the next headline.
  109. When nil, the link will be exported in place. If the line becomes long
  110. in this way, it will be wrapped."
  111. :group 'org-export-generic
  112. :type 'boolean)
  113. (defvar org-generic-current-indentation nil) ; For communication
  114. (defvar org-generic-alist
  115. '(
  116. ;;
  117. ;; generic DEMO exporter
  118. ;;
  119. ;; (this tries to use every specifier for demo purposes)
  120. ;;
  121. ("demo"
  122. :file-suffix ".txt"
  123. :key-binding ?d
  124. :header-prefix "<header>\n"
  125. :header-suffix "</header>\n"
  126. :author-export t
  127. :tags-export t
  128. :drawers-export t
  129. :title-prefix ?=
  130. :title-format "<h1>%s</h1>\n"
  131. :title-suffix ?=
  132. :date-export t
  133. :date-prefix "<date>"
  134. :date-format "<br /><b>Date:</b> <i>%s</i><br />"
  135. :date-suffix "</date>\n\n"
  136. :toc-export t
  137. :toc-header-prefix "<tocname>\n"
  138. :toc-header-format "__%s__\n"
  139. :toc-header-suffix "</tocname>\n"
  140. :toc-prefix "<toc>\n"
  141. :toc-suffix "</toc>\n"
  142. :toc-section-numbers t
  143. :toc-section-number-format "\#(%s) "
  144. :toc-format "--%s--"
  145. :toc-format-with-todo "!!%s!!\n"
  146. :toc-indent-char ?\
  147. :toc-indent-depth 4
  148. :toc-tags-export t
  149. :toc-tags-prefix " <tags>"
  150. :toc-tags-format "*%s*"
  151. :toc-tags-suffix "</tags>\n"
  152. :toc-tags-none-string "\n"
  153. :body-header-section-numbers 3 ; t = all, nil = none
  154. ; lists indicate different things per level
  155. ; list contents or straight value can either be a
  156. ; ?x char reference for printing strings that match the header len
  157. ; "" string to print directly
  158. :body-section-header-prefix ("<h1>" "<h2>" "<h3>"
  159. "<h4>" "<h5>" "<h6>")
  160. :body-section-header-format "%s"
  161. :body-section-header-suffix ("</h1>\n" "</h2>\n" "</h3>\n"
  162. "</h4>\n" "</h5>\n" "</h6>\n")
  163. :timestamps-export t
  164. :priorities-export t
  165. :todo-keywords-export t
  166. :body-tags-export t
  167. :body-tags-prefix " <tags>"
  168. :body-tags-suffix "</tags>\n"
  169. ; section prefixes/suffixes can be direct strings or lists as well
  170. :body-section-prefix "<secprefix>\n"
  171. :body-section-suffix "</secsuffix>\n"
  172. ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
  173. ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
  174. ; if preformated text should be included (eg, : prefixed)
  175. :body-line-export-preformated t
  176. :body-line-fixed-prefix "<pre>\n"
  177. :body-line-fixed-suffix "\n</pre>\n"
  178. :body-line-fixed-format "%s\n"
  179. :body-list-prefix "<list>\n"
  180. :body-list-suffix "</list>\n"
  181. :body-list-format "<li>%s</li>\n"
  182. :body-number-list-prefix "<ol>\n"
  183. :body-number-list-suffix "</ol>\n"
  184. :body-number-list-format "<li>%s</li>\n"
  185. :body-number-list-leave-number t
  186. :body-list-checkbox-todo "<checkbox type=\"todo\">"
  187. :body-list-checkbox-todo-end "</checkbox (todo)>"
  188. :body-list-checkbox-done "<checkbox type=\"done\">"
  189. :body-list-checkbox-done-end "</checkbox (done)>"
  190. :body-list-checkbox-half "<checkbox type=\"half\">"
  191. :body-list-checkbox-half-end "</checkbox (half)>"
  192. ; other body lines
  193. :body-line-format "%s"
  194. :body-line-wrap 60 ; wrap at 60 chars
  195. ; print above and below all body parts
  196. :body-text-prefix "<p>\n"
  197. :body-text-suffix "</p>\n")
  198. ;;
  199. ;; ascii exporter
  200. ;;
  201. ;; (close to the original ascii specifier)
  202. ;;
  203. ("ascii"
  204. :file-suffix ".txt"
  205. :key-binding ?a
  206. :header-prefix ""
  207. :header-suffix ""
  208. :title-prefix ?=
  209. :title-format "%s\n"
  210. :title-suffix ?=
  211. :date-export t
  212. :date-prefix ""
  213. :date-format "Date: %s\n"
  214. :date-suffix ""
  215. :toc-header-prefix ""
  216. :toc-header-format "%s\n"
  217. :toc-header-suffix ?=
  218. :toc-export t
  219. :toc-section-numbers t
  220. :toc-section-number-format "%s "
  221. :toc-format "%s\n"
  222. :toc-format-with-todo "%s (*)\n"
  223. :toc-indent-char ?\
  224. :toc-indent-depth 4
  225. :body-header-section-numbers 3
  226. :body-section-prefix "\n"
  227. ; :body-section-header-prefix "\n"
  228. ; :body-section-header-format "%s\n"
  229. ; :body-section-header-suffix (?\$ ?\# ?^ ?\~ ?\= ?\-)
  230. :body-section-header-prefix ("" "" "" "* " " + " " - ")
  231. :body-section-header-format "%s\n"
  232. :body-section-header-suffix (?~ ?= ?- "\n" "\n" "\n")
  233. ; :body-section-marker-prefix ""
  234. ; :body-section-marker-chars (?\$ ?\# ?^ ?\~ ?\= ?\-)
  235. ; :body-section-marker-suffix "\n"
  236. :body-line-export-preformated t
  237. :body-line-format "%s\n"
  238. :body-line-wrap 75
  239. ; :body-text-prefix "<t>\n"
  240. ; :body-text-suffix "</t>\n"
  241. :body-bullet-list-prefix (?* ?+ ?-))
  242. ; :body-bullet-list-suffix (?* ?+ ?-)
  243. ;;
  244. ;; wikipedia
  245. ;;
  246. ("wikipedia"
  247. :file-suffix ".txt"
  248. :key-binding ?w
  249. :header-prefix ""
  250. :header-suffix ""
  251. :title-format "= %s =\n"
  252. :date-export nil
  253. :toc-export nil
  254. :body-header-section-numbers nil
  255. :body-section-prefix "\n"
  256. :body-section-header-prefix ("= " "== " "=== "
  257. "==== " "===== " "====== ")
  258. :body-section-header-suffix (" =\n\n" " ==\n\n" " ===\n\n"
  259. " ====\n\n" " =====\n\n" " ======\n\n")
  260. :body-line-export-preformated t ;; yes/no/maybe???
  261. :body-line-format "%s\n"
  262. :body-line-wrap 75
  263. :body-line-fixed-format " %s\n"
  264. :body-list-format "* %s\n"
  265. :body-number-list-format "# %s\n"
  266. :body-bullet-list-prefix ("* " "** " "*** " "**** " "***** "))
  267. ;;
  268. ;; mediawiki
  269. ;;
  270. ("mediawiki"
  271. :file-suffix ".txt"
  272. :key-binding ?m
  273. :header-prefix ""
  274. :header-suffix ""
  275. :title-format "= %s =\n"
  276. :date-export nil
  277. :toc-export nil
  278. :body-header-section-numbers nil
  279. :body-section-prefix "\n"
  280. :body-section-header-prefix ("= " "== " "=== "
  281. "==== " "===== " "====== ")
  282. :body-section-header-suffix (" =\n\n" " ==\n\n" " ===\n\n"
  283. " ====\n\n" " =====\n\n" " ======\n\n")
  284. :body-line-export-preformated t ;; yes/no/maybe???
  285. :body-line-format "%s\n"
  286. :body-line-wrap 75
  287. :body-line-fixed-format " %s\n"
  288. :body-list-format "* %s\n"
  289. :body-number-list-format "# %s\n"
  290. :body-bullet-list-prefix ("* " "** " "*** " "**** " "***** ")
  291. :body-list-checkbox-todo "&#9744; "
  292. :body-list-checkbox-done "&#9746; "
  293. :body-table-start "{|"
  294. :body-table-end "|}"
  295. :body-table-cell-start "|"
  296. :body-table-cell-end "\n"
  297. :body-table-last-cell-end "|-"
  298. :body-table-hline-start "")
  299. ;;
  300. ;; internet-draft .xml for xml2rfc exporter
  301. ;;
  302. ("ietfid"
  303. ;; this tries to use every specifier for demo purposes
  304. :file-suffix ".xml"
  305. :key-binding ?i
  306. :title-prefix "<?xml version=\"1.0\"\?>
  307. <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
  308. <!ENTITY rfcs PUBLIC '' 'blah'>
  309. <?rfc strict=\"yes\" ?>
  310. <?rfc toc=\"yes\" ?>
  311. <?rfc tocdepth=\"4\" ?>
  312. <?rfc symrefs=\"yes\" ?>
  313. <?rfc compact=\"yes\" ?>
  314. <?rfc subcompact=\"no\" ?>
  315. <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
  316. <front>
  317. "
  318. :title-format "<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
  319. :title-suffix "<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
  320. <organization>Comany, Inc..</organization>
  321. <address>
  322. <postal>
  323. <street></street>
  324. <city></city>
  325. <region></region>
  326. <code></code>
  327. <country></country>
  328. </postal>
  329. <phone></phone>
  330. <email></email>
  331. </address>
  332. </author>
  333. <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
  334. <area>Operations and Management</area>
  335. <workgroup>FIXME</workgroup>
  336. <abstract>\n"
  337. :date-export nil
  338. :toc-export nil
  339. :body-header-section-numbers nil
  340. :body-section-header-format "<section title=\"%s\">\n"
  341. :body-section-suffix "</section>\n"
  342. ; if preformated text should be included (eg, : prefixed)
  343. :body-line-export-preformated t
  344. :body-line-fixed-prefix "<figure>\n<artwork>\n"
  345. :body-line-fixed-suffix "\n</artwork>\n</figure>\n"
  346. ; other body lines
  347. :body-line-format "%s"
  348. :body-line-wrap 75
  349. ; print above and below all body parts
  350. :body-text-prefix "<t>\n"
  351. :body-text-suffix "</t>\n"
  352. :body-list-prefix "<list style=\"symbols\">\n"
  353. :body-list-suffix "</list>\n"
  354. :body-list-format "<t>%s</t>\n")
  355. ("trac-wiki"
  356. :file-suffix ".txt"
  357. :key-binding ?T
  358. ;; lifted from wikipedia exporter
  359. :header-prefix ""
  360. :header-suffix ""
  361. :title-format "= %s =\n"
  362. :date-export nil
  363. :toc-export nil
  364. :body-header-section-numbers nil
  365. :body-section-prefix "\n"
  366. :body-section-header-prefix (" == " " === " " ==== "
  367. " ===== " )
  368. :body-section-header-suffix (" ==\n\n" " ===\n\n" " ====\n\n"
  369. " =====\n\n" " ======\n\n" " =======\n\n")
  370. :body-line-export-preformated t ;; yes/no/maybe???
  371. :body-line-format "%s\n"
  372. :body-line-wrap 75
  373. :body-line-fixed-format " %s\n"
  374. :body-list-format " * %s\n"
  375. :body-number-list-format " # %s\n"
  376. ;; :body-list-prefix "LISTSTART"
  377. ;; :body-list-suffix "LISTEND"
  378. ;; this is ignored! [2010/02/02:rpg]
  379. :body-bullet-list-prefix ("* " "** " "*** " "**** " "***** "))
  380. ("tikiwiki"
  381. :file-suffix ".txt"
  382. :key-binding ?U
  383. ;; lifted from wikipedia exporter
  384. :header-prefix ""
  385. :header-suffix ""
  386. :title-format "-= %s =-\n"
  387. :date-export nil
  388. :toc-export nil
  389. :body-header-section-numbers nil
  390. :body-section-prefix "\n"
  391. :body-section-header-prefix ("! " "!! " "!!! " "!!!! "
  392. "!!!!! " "!!!!!! " "!!!!!!! ")
  393. :body-section-header-suffix (" \n" " \n" " \n"
  394. " \n" " \n" " \n")
  395. :body-line-export-preformated t ;; yes/no/maybe???
  396. :body-line-format "%s "
  397. :body-line-wrap nil
  398. :body-line-fixed-format " %s\n"
  399. :body-list-format "* %s\n"
  400. :body-number-list-format "# %s\n"
  401. ;; :body-list-prefix "LISTSTART"
  402. ;; :body-list-suffix "LISTEND"
  403. :blockquote-start "\n^\n"
  404. :blockquote-end "^\n\n"
  405. :body-newline-paragraph "\n"
  406. :bold-format "__%s__"
  407. :italic-format "''%s''"
  408. :underline-format "===%s==="
  409. :strikethrough-format "--%s--"
  410. :code-format "-+%s+-"
  411. :verbatim-format "~pp~%s~/pp~"))
  412. "A assoc list of property lists to specify export definitions")
  413. (setq org-generic-export-type "demo")
  414. (defvar org-export-generic-section-type "")
  415. (defvar org-export-generic-section-suffix "")
  416. (defun org-set-generic-type (type definition)
  417. "Adds a TYPE and DEFINITION to the existing list of defined generic
  418. export definitions."
  419. (aput 'org-generic-alist type definition))
  420. ;;; helper functions for org-set-generic-type
  421. (defvar org-export-generic-keywords nil)
  422. (defmacro* def-org-export-generic-keyword (keyword
  423. &key documentation
  424. type)
  425. "Define KEYWORD as a legitimate element for inclusion in
  426. the body of an org-set-generic-type definition."
  427. ;; TODO: push the documentation and type information
  428. ;; somewhere where it will do us some good.
  429. `(progn
  430. (pushnew ,keyword org-export-generic-keywords)))
  431. (def-org-export-generic-keyword :body-newline-paragraph
  432. :documentation "Bound either to NIL or to a pattern to be
  433. inserted in the output for every blank line in the input.
  434. The intention is to handle formats where text is flowed, and
  435. newlines are interpreted as significant \(e.g., as indicating
  436. preformatted text\). A common non-nil value for this keyword
  437. is \"\\n\". Should typically be combined with a value for
  438. :body-line-format that does NOT end with a newline."
  439. :type string)
  440. ;;; fontification keywords
  441. (def-org-export-generic-keyword :bold-format)
  442. (def-org-export-generic-keyword :italic-format)
  443. (def-org-export-generic-keyword :underline-format)
  444. (def-org-export-generic-keyword :strikethrough-format)
  445. (def-org-export-generic-keyword :code-format)
  446. (def-org-export-generic-keyword :verbatim-format)
  447. (defun org-export-generic-remember-section (type suffix &optional prefix)
  448. (setq org-export-generic-section-type type)
  449. (setq org-export-generic-section-suffix suffix)
  450. (if prefix
  451. (insert prefix)))
  452. (defun org-export-generic-check-section (type &optional prefix suffix)
  453. "checks to see if type is already in use, or we're switching parts
  454. If we're switching, then insert a potentially previously remembered
  455. suffix, and insert the current prefix immediately and then save the
  456. suffix a later change time."
  457. (when (not (equal type org-export-generic-section-type))
  458. (if org-export-generic-section-suffix
  459. (insert org-export-generic-section-suffix))
  460. (setq org-export-generic-section-type type)
  461. (setq org-export-generic-section-suffix suffix)
  462. (if prefix
  463. (insert prefix))))
  464. (defun org-export-generic (arg)
  465. "Export the outline as generic output.
  466. If there is an active region, export only the region.
  467. The prefix ARG specifies how many levels of the outline should become
  468. underlined headlines. The default is 3."
  469. (interactive "P")
  470. (setq-default org-todo-line-regexp org-todo-line-regexp)
  471. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  472. (org-infile-export-plist)))
  473. (region-p (org-region-active-p))
  474. (rbeg (and region-p (region-beginning)))
  475. (rend (and region-p (region-end)))
  476. (subtree-p
  477. (when region-p
  478. (save-excursion
  479. (goto-char rbeg)
  480. (and (org-at-heading-p)
  481. (>= (org-end-of-subtree t t) rend)))))
  482. (level-offset (if subtree-p
  483. (save-excursion
  484. (goto-char rbeg)
  485. (+ (funcall outline-level)
  486. (if org-odd-levels-only 1 0)))
  487. 0))
  488. (opt-plist (setq org-export-opt-plist
  489. (if subtree-p
  490. (org-export-add-subtree-options opt-plist rbeg)
  491. opt-plist)))
  492. helpstart
  493. (bogus (mapc (lambda (x)
  494. (setq helpstart
  495. (concat helpstart "\["
  496. (char-to-string
  497. (plist-get (cdr x) :key-binding))
  498. "] " (car x) "\n")))
  499. org-generic-alist))
  500. (help (concat helpstart "
  501. \[ ] the current setting of the org-generic-export-type variable
  502. "))
  503. (cmds
  504. (append
  505. (mapcar (lambda (x)
  506. (list
  507. (plist-get (cdr x) :key-binding)
  508. (car x)))
  509. org-generic-alist)
  510. (list (list ? "default"))))
  511. r1 r2 ass
  512. ;; read in the type to use
  513. (export-plist
  514. (progn
  515. (save-excursion
  516. (save-window-excursion
  517. (delete-other-windows)
  518. (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
  519. (princ help))
  520. (org-fit-window-to-buffer (get-buffer-window
  521. "*Org Export/Generic Styles Help*"))
  522. (message "Select command: ")
  523. (setq r1 (read-char-exclusive))))
  524. (setq r2 (if (< r1 27) (+ r1 96) r1))
  525. (unless (setq ass (cadr (assq r2 cmds)))
  526. (error "No command associated with key %c" r1))
  527. (cdr (assoc
  528. (if (equal ass "default") org-generic-export-type ass)
  529. org-generic-alist))))
  530. (custom-times org-display-custom-times)
  531. (org-generic-current-indentation '(0 . 0))
  532. (level 0) (old-level 0) line txt lastwastext
  533. (umax nil)
  534. (umax-toc nil)
  535. (case-fold-search nil)
  536. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  537. (filesuffix (or (plist-get export-plist :file-suffix) ".foo"))
  538. (filename (concat (file-name-as-directory
  539. (org-export-directory :ascii opt-plist))
  540. (file-name-sans-extension
  541. (or (and subtree-p
  542. (org-entry-get (region-beginning)
  543. "EXPORT_FILE_NAME" t))
  544. (file-name-nondirectory bfname)))
  545. filesuffix))
  546. (filename (if (equal (file-truename filename)
  547. (file-truename bfname))
  548. (concat filename filesuffix)
  549. filename))
  550. (buffer (find-file-noselect filename))
  551. (org-levels-open (make-vector org-level-max nil))
  552. (odd org-odd-levels-only)
  553. (date (plist-get opt-plist :date))
  554. (author (plist-get opt-plist :author))
  555. (title (or (and subtree-p (org-export-get-title-from-subtree))
  556. (plist-get opt-plist :title)
  557. (and (not
  558. (plist-get opt-plist :skip-before-1st-heading))
  559. (org-export-grab-title-from-buffer))
  560. (file-name-sans-extension
  561. (file-name-nondirectory bfname))))
  562. (email (plist-get opt-plist :email))
  563. (language (plist-get opt-plist :language))
  564. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  565. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  566. (todo nil)
  567. (lang-words nil)
  568. (region
  569. (buffer-substring
  570. (if (org-region-active-p) (region-beginning) (point-min))
  571. (if (org-region-active-p) (region-end) (point-max))))
  572. (org-export-current-backend 'org-export-generic)
  573. (lines (org-split-string
  574. (org-export-preprocess-string
  575. region
  576. :for-backend 'ascii
  577. :skip-before-1st-heading
  578. (plist-get opt-plist :skip-before-1st-heading)
  579. :drawers (plist-get export-plist :drawers-export)
  580. :tags (plist-get export-plist :tags-export)
  581. :priority (plist-get export-plist :priority-export)
  582. :footnotes (plist-get export-plist :footnotes-export)
  583. :timestamps (plist-get export-plist :timestamps-export)
  584. :todo-keywords (plist-get export-plist :todo-keywords-export)
  585. :verbatim-multiline t
  586. :select-tags (plist-get export-plist :select-tags-export)
  587. :exclude-tags (plist-get export-plist :exclude-tags-export)
  588. :emph-multiline t
  589. :archived-trees
  590. (plist-get export-plist :archived-trees-export)
  591. :add-text (plist-get opt-plist :text))
  592. "\n"))
  593. ;; export-generic plist variables
  594. (withtags (plist-get export-plist :tags-export))
  595. (tagsintoc (plist-get export-plist :toc-tags-export))
  596. (tocnotagsstr (or (plist-get export-plist :toc-tags-none-string) ""))
  597. (tocdepth (plist-get export-plist :toc-indent-depth))
  598. (tocindentchar (plist-get export-plist :toc-indent-char))
  599. (tocsecnums (plist-get export-plist :toc-section-numbers))
  600. (tocsecnumform (plist-get export-plist :toc-section-number-format))
  601. (tocformat (plist-get export-plist :toc-format))
  602. (tocformtodo (plist-get export-plist :toc-format-with-todo))
  603. (tocprefix (plist-get export-plist :toc-prefix))
  604. (tocsuffix (plist-get export-plist :toc-suffix))
  605. (bodyfixedpre (plist-get export-plist :body-line-fixed-prefix))
  606. (bodyfixedsuf (plist-get export-plist :body-line-fixed-suffix))
  607. (bodyfixedform (or (plist-get export-plist :body-line-fixed-format)
  608. "%s"))
  609. (listprefix (plist-get export-plist :body-list-prefix))
  610. (listsuffix (plist-get export-plist :body-list-suffix))
  611. (listformat (or (plist-get export-plist :body-list-format) "%s\n"))
  612. (numlistleavenum
  613. (plist-get export-plist :body-number-list-leave-number))
  614. (numlistprefix (plist-get export-plist :body-number-list-prefix))
  615. (numlistsuffix (plist-get export-plist :body-number-list-suffix))
  616. (numlistformat
  617. (or (plist-get export-plist :body-number-list-format) "%s\n"))
  618. (listchecktodo
  619. (or (plist-get export-plist :body-list-checkbox-todo) "\\1"))
  620. (listcheckdone
  621. (or (plist-get export-plist :body-list-checkbox-done) "\\1"))
  622. (listcheckhalf
  623. (or (plist-get export-plist :body-list-checkbox-half) "\\1"))
  624. (listchecktodoend
  625. (or (plist-get export-plist :body-list-checkbox-todo-end) ""))
  626. (listcheckdoneend
  627. (or (plist-get export-plist :body-list-checkbox-done-end) ""))
  628. (listcheckhalfend
  629. (or (plist-get export-plist :body-list-checkbox-half-end) ""))
  630. (bodytablestart
  631. (or (plist-get export-plist :body-table-start) ""))
  632. (bodytableend
  633. (or (plist-get export-plist :body-table-end) ""))
  634. (bodytablerowstart
  635. (or (plist-get export-plist :body-table-row-start) ""))
  636. (bodytablerowend
  637. (or (plist-get export-plist :body-table-row-end) ""))
  638. (bodytablecellstart
  639. (or (plist-get export-plist :body-table-cell-start) ""))
  640. (bodytablecellend
  641. (or (plist-get export-plist :body-table-cell-end) ""))
  642. (bodytablefirstcellstart
  643. (or (plist-get export-plist :body-table-first-cell-start) ""))
  644. (bodytableinteriorcellstart
  645. (or (plist-get export-plist :body-table-interior-cell-start) ""))
  646. (bodytableinteriorcellend
  647. (or (plist-get export-plist :body-table-interior-cell-end) ""))
  648. (bodytablelastcellend
  649. (or (plist-get export-plist :body-table-last-cell-end) ""))
  650. (bodytablehlinestart
  651. (or (plist-get export-plist :body-table-hline-start) " \\1"))
  652. (bodytablehlineend
  653. (or (plist-get export-plist :body-table-hline-end) ""))
  654. (bodynewline-paragraph (plist-get export-plist :body-newline-paragraph))
  655. (bodytextpre (plist-get export-plist :body-text-prefix))
  656. (bodytextsuf (plist-get export-plist :body-text-suffix))
  657. (bodylinewrap (plist-get export-plist :body-line-wrap))
  658. (bodylineform (or (plist-get export-plist :body-line-format) "%s"))
  659. (blockquotestart (or (plist-get export-plist :blockquote-start) "\n\n\t"))
  660. (blockquoteend (or (plist-get export-plist :blockquote-end) "\n\n"))
  661. ;; dynamic variables used heinously in fontification
  662. ;; not referenced locally...
  663. (format-boldify (plist-get export-plist :bold-format))
  664. (format-italicize (plist-get export-plist :italic-format))
  665. (format-underline (plist-get export-plist :underline-format))
  666. (format-strikethrough (plist-get export-plist :strikethrough-format))
  667. (format-code (plist-get export-plist :code-format))
  668. (format-verbatim (plist-get export-plist :verbatim-format))
  669. thetoc toctags have-headings first-heading-pos
  670. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  671. (let ((inhibit-read-only t))
  672. (org-unmodified
  673. (remove-text-properties (point-min) (point-max)
  674. '(:org-license-to-kill t))))
  675. (setq org-min-level (org-get-min-level lines level-offset))
  676. (setq org-last-level org-min-level)
  677. (org-init-section-numbers)
  678. (find-file-noselect filename)
  679. (setq lang-words (or (assoc language org-export-language-setup)
  680. (assoc "en" org-export-language-setup)))
  681. (switch-to-buffer-other-window buffer)
  682. (erase-buffer)
  683. (fundamental-mode)
  684. ;; create local variables for all options, to make sure all called
  685. ;; functions get the correct information
  686. (mapc (lambda (x)
  687. (set (make-local-variable (nth 2 x))
  688. (plist-get opt-plist (car x))))
  689. org-export-plist-vars)
  690. (org-set-local 'org-odd-levels-only odd)
  691. (setq umax (if arg (prefix-numeric-value arg)
  692. org-export-headline-levels))
  693. (setq umax-toc umax)
  694. ;; File header
  695. (if title
  696. (insert
  697. (org-export-generic-header title export-plist
  698. :title-prefix
  699. :title-format
  700. :title-suffix)))
  701. (if (and (or author email)
  702. (plist-get export-plist :author-export))
  703. (insert (concat (nth 1 lang-words) ": " (or author "")
  704. (if email (concat " <" email ">") "")
  705. "\n")))
  706. (cond
  707. ((and date (string-match "%" date))
  708. (setq date (format-time-string date)))
  709. (date)
  710. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  711. (if (and date (plist-get export-plist :date-export))
  712. (insert
  713. (org-export-generic-header date export-plist
  714. :date-prefix
  715. :date-format
  716. :date-suffix)))
  717. ;; export the table of contents first
  718. (if (plist-get export-plist :toc-export)
  719. (progn
  720. (push
  721. (org-export-generic-header (nth 3 lang-words) export-plist
  722. :toc-header-prefix
  723. :toc-header-format
  724. :toc-header-suffix)
  725. thetoc)
  726. (if tocprefix
  727. (push tocprefix thetoc))
  728. (mapc #'(lambda (line)
  729. (if (string-match org-todo-line-regexp line)
  730. ;; This is a headline
  731. (progn
  732. (setq have-headings t)
  733. (setq level (- (match-end 1) (match-beginning 1)
  734. level-offset)
  735. level (org-tr-level level)
  736. txt (match-string 3 line)
  737. todo
  738. (or (and org-export-mark-todo-in-toc
  739. (match-beginning 2)
  740. (not (member (match-string 2 line)
  741. org-done-keywords)))
  742. ; TODO, not DONE
  743. (and org-export-mark-todo-in-toc
  744. (= level umax-toc)
  745. (org-search-todo-below
  746. line lines level))))
  747. (setq txt (org-html-expand-for-generic txt))
  748. (while (string-match org-bracket-link-regexp txt)
  749. (setq txt
  750. (replace-match
  751. (match-string (if (match-end 2) 3 1) txt)
  752. t t txt)))
  753. (if (and (not tagsintoc)
  754. (string-match
  755. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  756. txt))
  757. (setq txt (replace-match "" t t txt))
  758. ; include tags but formated
  759. (if (string-match
  760. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
  761. txt)
  762. (progn
  763. (setq
  764. toctags
  765. (org-export-generic-header
  766. (match-string 1 txt)
  767. export-plist :toc-tags-prefix
  768. :toc-tags-format :toc-tags-suffix))
  769. (string-match
  770. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  771. txt)
  772. (setq txt (replace-match "" t t txt)))
  773. (setq toctags tocnotagsstr)))
  774. (if (string-match quote-re0 txt)
  775. (setq txt (replace-match "" t t txt)))
  776. (if (<= level umax-toc)
  777. (progn
  778. (push
  779. (concat
  780. (make-string
  781. (* (max 0 (- level org-min-level)) tocdepth)
  782. tocindentchar)
  783. (if tocsecnums
  784. (format tocsecnumform
  785. (org-section-number level))
  786. "")
  787. (format
  788. (if todo tocformtodo tocformat)
  789. txt)
  790. toctags)
  791. thetoc)
  792. (setq org-last-level level))))))
  793. lines)
  794. (if tocsuffix
  795. (push tocsuffix thetoc))
  796. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  797. (org-init-section-numbers)
  798. (org-export-generic-check-section "top")
  799. (while (setq line (pop lines))
  800. (when (and link-buffer (string-match org-outline-regexp-bol line))
  801. (org-export-generic-push-links (nreverse link-buffer))
  802. (setq link-buffer nil))
  803. (setq wrap nil)
  804. ;; Remove the quoted HTML tags.
  805. ;; XXX
  806. (setq line (org-html-expand-for-generic line))
  807. ;; Replace links with the description when possible
  808. ;; XXX
  809. (while (string-match org-bracket-link-regexp line)
  810. (setq link (match-string 1 line)
  811. desc0 (match-string 3 line)
  812. desc (or desc0 (match-string 1 line)))
  813. (if (and (> (length link) 8)
  814. (equal (substring link 0 8) "coderef:"))
  815. (setq line (replace-match
  816. (format (org-export-get-coderef-format (substring link 8) desc)
  817. (cdr (assoc
  818. (substring link 8)
  819. org-export-code-refs)))
  820. t t line))
  821. (setq rpl (concat "["
  822. (or (match-string 3 line) (match-string 1 line))
  823. "]"))
  824. (when (and desc0 (not (equal desc0 link)))
  825. (if org-export-generic-links-to-notes
  826. (push (cons desc0 link) link-buffer)
  827. (setq rpl (concat rpl " (" link ")")
  828. wrap (+ (length line) (- (length (match-string 0 line)))
  829. (length desc)))))
  830. (setq line (replace-match rpl t t line))))
  831. (when custom-times
  832. (setq line (org-translate-time line)))
  833. (cond
  834. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  835. ;;
  836. ;; a Headline
  837. ;;
  838. (org-export-generic-check-section "headline")
  839. (setq first-heading-pos (or first-heading-pos (point)))
  840. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  841. level-offset))
  842. txt (match-string 2 line))
  843. (org-generic-level-start level old-level txt umax export-plist lines)
  844. (setq old-level level))
  845. ((and org-export-with-tables
  846. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  847. ;;
  848. ;; a Table
  849. ;;
  850. (org-export-generic-check-section "table")
  851. (if (not table-open)
  852. ;; New table starts
  853. (setq table-open t table-buffer nil))
  854. ;; Accumulate table lines
  855. (setq table-buffer (cons line table-buffer))
  856. (when (or (not lines)
  857. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  858. (car lines))))
  859. (setq table-open nil
  860. table-buffer (nreverse table-buffer))
  861. (insert (mapconcat
  862. (lambda (x)
  863. (org-fix-indentation x org-generic-current-indentation))
  864. (org-format-table-generic table-buffer)
  865. "\n") "\n")))
  866. ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line)
  867. ;;
  868. ;; pre-formatted text
  869. ;;
  870. (setq line (replace-match "\\1" nil nil line))
  871. (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf)
  872. (insert (format bodyfixedform line)))
  873. ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line)
  874. ;; if the bullet list item is an asterisk, the leading space is /mandatory/
  875. ;; [2010/02/02:rpg]
  876. (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line))
  877. ;;
  878. ;; plain list item
  879. ;; TODO: nested lists
  880. ;;
  881. ;; first add a line break between any previous paragraph or line item and this
  882. ;; one
  883. (when bodynewline-paragraph
  884. (insert bodynewline-paragraph))
  885. ;; I believe this gets rid of leading whitespace.
  886. (setq line (replace-match "" nil nil line))
  887. ;; won't this insert the suffix /before/ the last line of the list?
  888. ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
  889. ;; unless 'org-empty-line-terminates-plain-lists' is true?
  890. (org-export-generic-check-section "liststart" listprefix listsuffix)
  891. ;; deal with checkboxes
  892. (cond
  893. ((string-match "^\\(\\[ \\]\\)[ \t]*" line)
  894. (setq line (concat (replace-match listchecktodo nil nil line)
  895. listchecktodoend)))
  896. ((string-match "^\\(\\[X\\]\\)[ \t]*" line)
  897. (setq line (concat (replace-match listcheckdone nil nil line)
  898. listcheckdoneend)))
  899. ((string-match "^\\(\\[/\\]\\)[ \t]*" line)
  900. (setq line (concat (replace-match listcheckhalf nil nil line)
  901. listcheckhalfend))))
  902. (insert (format listformat (org-export-generic-fontify line))))
  903. ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line)
  904. ;;
  905. ;; numbered list item
  906. ;;
  907. ;; TODO: nested lists
  908. ;;
  909. (setq line (replace-match (if numlistleavenum "\\2" "") nil nil line))
  910. (org-export-generic-check-section "numliststart"
  911. numlistprefix numlistsuffix)
  912. ;; deal with checkboxes
  913. ;; TODO: whoops; leaving the numbers is a problem for ^ matching
  914. (cond
  915. ((string-match "\\(\\[ \\]\\)[ \t]*" line)
  916. (setq line (concat (replace-match listchecktodo nil nil line)
  917. listchecktodoend)))
  918. ((string-match "\\(\\[X\\]\\)[ \t]*" line)
  919. (setq line (concat (replace-match listcheckdone nil nil line)
  920. listcheckdoneend)))
  921. ((string-match "\\(\\[/\\]\\)[ \t]*" line)
  922. (setq line (concat (replace-match listcheckhalf nil nil line)
  923. listcheckhalfend))))
  924. (insert (format numlistformat (org-export-generic-fontify line))))
  925. ((equal line "ORG-BLOCKQUOTE-START")
  926. (setq line blockquotestart))
  927. ((equal line "ORG-BLOCKQUOTE-END")
  928. (setq line blockquoteend))
  929. ((string-match "^\\s-*$" line)
  930. ;; blank line
  931. (if bodynewline-paragraph
  932. (insert bodynewline-paragraph)))
  933. (t
  934. ;;
  935. ;; body
  936. ;;
  937. (org-export-generic-check-section "body" bodytextpre bodytextsuf)
  938. (setq line
  939. (org-export-generic-fontify line))
  940. ;; XXX: properties? list?
  941. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  942. (setq line (replace-match "\\1\\3:" t nil line)))
  943. (setq line (org-fix-indentation line org-generic-current-indentation))
  944. ;; Remove forced line breaks
  945. (if (string-match "\\\\\\\\[ \t]*$" line)
  946. (setq line (replace-match "" t t line)))
  947. (if bodylinewrap
  948. ;; XXX: was dependent on wrap var which was calculated by???
  949. (if (> (length line) bodylinewrap)
  950. (setq line
  951. (org-export-generic-wrap line bodylinewrap))
  952. (setq line line)))
  953. (insert (format bodylineform line)))))
  954. ;; if we're at a level > 0; insert the closing body level stuff
  955. (let ((counter 0))
  956. (while (> (- level counter) 0)
  957. (insert
  958. (org-export-generic-format export-plist :body-section-suffix 0
  959. (- level counter)))
  960. (setq counter (1+ counter))))
  961. (org-export-generic-check-section "bottom")
  962. (org-export-generic-push-links (nreverse link-buffer))
  963. (normal-mode)
  964. ;; insert the table of contents
  965. (when thetoc
  966. (goto-char (point-min))
  967. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  968. (progn
  969. (goto-char (match-beginning 0))
  970. (replace-match ""))
  971. (goto-char first-heading-pos))
  972. (mapc 'insert thetoc)
  973. (or (looking-at "[ \t]*\n[ \t]*\n")
  974. (insert "\n\n")))
  975. ;; Convert whitespace place holders
  976. (goto-char (point-min))
  977. (let (beg end)
  978. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  979. (setq end (next-single-property-change beg 'org-whitespace))
  980. (goto-char beg)
  981. (delete-region beg end)
  982. (insert (make-string (- end beg) ?\ ))))
  983. (save-buffer)
  984. ;; remove display and invisible chars
  985. (let (beg end)
  986. (goto-char (point-min))
  987. (while (setq beg (next-single-property-change (point) 'display))
  988. (setq end (next-single-property-change beg 'display))
  989. (delete-region beg end)
  990. (goto-char beg)
  991. (insert "=>"))
  992. (goto-char (point-min))
  993. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  994. (setq end (next-single-property-change beg 'org-cwidth))
  995. (delete-region beg end)
  996. (goto-char beg)))
  997. (goto-char (point-min))))
  998. (defun org-export-generic-format (export-plist prop &optional len n reverse)
  999. "converts a property specification to a string given types of properties
  1000. The EXPORT-PLIST should be defined as the lookup plist.
  1001. The PROP should be the property name to search for in it.
  1002. LEN is set to the length of multi-characters strings to generate (or 0)
  1003. N is the tree depth
  1004. REVERSE means to reverse the list if the plist match is a list
  1005. "
  1006. (let* ((prefixtype (plist-get export-plist prop))
  1007. subtype)
  1008. (cond
  1009. ((null prefixtype) "")
  1010. ((and len (char-or-string-p prefixtype) (not (stringp prefixtype)))
  1011. ;; sequence of chars
  1012. (concat (make-string len prefixtype) "\n"))
  1013. ((stringp prefixtype)
  1014. prefixtype)
  1015. ((and n (listp prefixtype))
  1016. (if reverse
  1017. (setq prefixtype (reverse prefixtype)))
  1018. (setq subtype (if (> n (length prefixtype))
  1019. (car (last prefixtype))
  1020. (nth (1- n) prefixtype)))
  1021. (if (stringp subtype)
  1022. subtype
  1023. (concat (make-string len subtype) "\n")))
  1024. (t ""))))
  1025. (defun org-export-generic-header (header export-plist
  1026. prefixprop formatprop postfixprop
  1027. &optional n reverse)
  1028. "convert a header to an output string given formatting property names"
  1029. (let* ((formatspec (plist-get export-plist formatprop))
  1030. (len (length header)))
  1031. (concat
  1032. (org-export-generic-format export-plist prefixprop len n reverse)
  1033. (format (or formatspec "%s") header)
  1034. (org-export-generic-format export-plist postfixprop len n reverse))))
  1035. (defun org-export-generic-preprocess (parameters)
  1036. "Do extra work for ASCII export"
  1037. ;; Put quotes around verbatim text
  1038. (goto-char (point-min))
  1039. (while (re-search-forward org-verbatim-re nil t)
  1040. (goto-char (match-end 2))
  1041. (delete-backward-char 1) (insert "'")
  1042. (goto-char (match-beginning 2))
  1043. (delete-char 1) (insert "`")
  1044. (goto-char (match-end 2)))
  1045. ;; Remove target markers
  1046. (goto-char (point-min))
  1047. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  1048. (replace-match "\\1\\2")))
  1049. (defun org-html-expand-for-generic (line)
  1050. "Handle quoted HTML for ASCII export."
  1051. (if org-export-html-expand
  1052. (while (string-match "@<[^<>\n]*>" line)
  1053. ;; We just remove the tags for now.
  1054. (setq line (replace-match "" nil nil line))))
  1055. line)
  1056. (defun org-export-generic-wrap (line where)
  1057. "Wrap LINE at or before WHERE."
  1058. (let* ((ind (org-get-indentation line))
  1059. (indstr (make-string ind ?\ ))
  1060. (len (length line))
  1061. (result "")
  1062. pos didfirst)
  1063. (while (> len where)
  1064. (catch 'found
  1065. (loop for i from where downto (/ where 2) do
  1066. (and (equal (aref line i) ?\ )
  1067. (setq pos i)
  1068. (throw 'found t))))
  1069. (if pos
  1070. (progn
  1071. (setq result
  1072. (concat result
  1073. (if didfirst indstr "")
  1074. (substring line 0 pos)
  1075. "\n"))
  1076. (setq didfirst t)
  1077. (setq line (substring line (1+ pos)))
  1078. (setq len (length line)))
  1079. (setq result (concat result line))
  1080. (setq len 0)))
  1081. (concat result indstr line)))
  1082. (defun org-export-generic-push-links (link-buffer)
  1083. "Push out links in the buffer."
  1084. (when link-buffer
  1085. ;; We still have links to push out.
  1086. (insert "\n")
  1087. (let ((ind ""))
  1088. (save-match-data
  1089. (if (save-excursion
  1090. (re-search-backward
  1091. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  1092. (setq ind (or (match-string 2)
  1093. (make-string (length (match-string 3)) ?\ )))))
  1094. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  1095. link-buffer))
  1096. (insert "\n")))
  1097. (defun org-generic-level-start (level old-level title umax export-plist
  1098. &optional lines)
  1099. "Insert a new level in a generic export."
  1100. (let ((n (- level umax 1))
  1101. (ind 0)
  1102. (diff (- level old-level)) (counter 0)
  1103. (secnums (plist-get export-plist :body-header-section-numbers))
  1104. (secnumformat
  1105. (plist-get export-plist :body-header-section-number-format))
  1106. char tagstring)
  1107. (unless org-export-with-tags
  1108. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1109. (setq title (replace-match "" t t title))))
  1110. (cond
  1111. ;; going deeper
  1112. ((> level old-level)
  1113. (while (< (+ old-level counter) (1- level))
  1114. (insert
  1115. (org-export-generic-format export-plist :body-section-prefix 0
  1116. (+ old-level counter)))
  1117. (setq counter (1+ counter))))
  1118. ;; going up
  1119. ((< level old-level)
  1120. (while (> (- old-level counter) (1- level))
  1121. (insert
  1122. (org-export-generic-format export-plist :body-section-suffix 0
  1123. (- old-level counter)))
  1124. (setq counter (1+ counter))))
  1125. ;; same level
  1126. ((= level old-level)
  1127. (insert
  1128. (org-export-generic-format export-plist :body-section-suffix 0 level))))
  1129. (insert
  1130. (org-export-generic-format export-plist :body-section-prefix 0 level))
  1131. (if (and org-export-with-section-numbers
  1132. secnums
  1133. (or (not (numberp secnums))
  1134. (< level secnums)))
  1135. (setq title
  1136. (concat (format (or secnumformat "%s ")
  1137. (org-section-number level)) title)))
  1138. ;; handle tags and formatting
  1139. (if (string-match
  1140. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title)
  1141. (progn
  1142. (if (plist-get export-plist :body-tags-export)
  1143. (setq tagstring (org-export-generic-header (match-string 1 title)
  1144. export-plist
  1145. :body-tags-prefix
  1146. :body-tags-format
  1147. :body-tags-suffix)))
  1148. (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title)
  1149. (setq title (replace-match "" t t title)))
  1150. (setq tagstring (plist-get export-plist :body-tags-none-string)))
  1151. (insert
  1152. (org-export-generic-header title export-plist
  1153. :body-section-header-prefix
  1154. :body-section-header-format
  1155. :body-section-header-suffix
  1156. level))
  1157. (if tagstring
  1158. (insert tagstring))
  1159. (setq org-generic-current-indentation '(0 . 0))))
  1160. (defun org-insert-centered (s &optional underline)
  1161. "Insert the string S centered and underline it with character UNDERLINE."
  1162. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  1163. (insert (make-string ind ?\ ) s "\n")
  1164. (if underline
  1165. (insert (make-string ind ?\ )
  1166. (make-string (string-width s) underline)
  1167. "\n"))))
  1168. (defvar org-table-colgroup-info nil)
  1169. (defun org-format-table-generic (lines)
  1170. "Format a table for ascii export."
  1171. (if (stringp lines)
  1172. (setq lines (org-split-string lines "\n")))
  1173. (if (not (string-match "^[ \t]*|" (car lines)))
  1174. ;; Table made by table.el - test for spanning
  1175. lines
  1176. ;; A normal org table
  1177. ;; Get rid of hlines at beginning and end
  1178. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1179. (setq lines (nreverse lines))
  1180. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1181. (setq lines (nreverse lines))
  1182. (when org-export-table-remove-special-lines
  1183. ;; Check if the table has a marking column. If yes remove the
  1184. ;; column and the special lines
  1185. (setq lines (org-table-clean-before-export lines)))
  1186. ;; Get rid of the vertical lines except for grouping
  1187. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  1188. (rtn (list bodytablestart)) line vl1 start)
  1189. (while (setq line (pop lines))
  1190. (setq line (concat bodytablerowstart line))
  1191. (if (string-match org-table-hline-regexp line)
  1192. (and (string-match "|\\(.*\\)|" line)
  1193. (setq line (replace-match (concat bodytablehlinestart bodytablehlineend) t nil line)))
  1194. (setq start 0 vl1 vl)
  1195. (if (string-match "|\\(.*\\)|" line)
  1196. (setq line (replace-match (concat bodytablefirstcellstart bodytablecellstart " \\1 " bodytablecellend bodytablelastcellend) t nil line)))
  1197. (while (string-match "|" line start)
  1198. (setq start (+ (match-end 0) (length (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart))))
  1199. (or (pop vl1) (setq line (replace-match (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart) t t line)))))
  1200. (setq line (concat line bodytablerowend))
  1201. (push line rtn))
  1202. (setq rtn (cons bodytableend rtn))
  1203. (nreverse rtn))))
  1204. (defun org-colgroup-info-to-vline-list (info)
  1205. (let (vl new last)
  1206. (while info
  1207. (setq last new new (pop info))
  1208. (if (or (memq last '(:end :startend))
  1209. (memq new '(:start :startend)))
  1210. (push t vl)
  1211. (push nil vl)))
  1212. (setq vl (nreverse vl))
  1213. (and vl (setcar vl nil))
  1214. vl))
  1215. ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
  1216. (defvar org-export-generic-emphasis-alist
  1217. '(("*" format-boldify nil)
  1218. ("/" format-italicize nil)
  1219. ("_" format-underline nil)
  1220. ("+" format-strikethrough nil)
  1221. ("=" format-code t)
  1222. ("~" format-verbatim t))
  1223. "Alist of org format -> formatting variables for fontification.
  1224. Each element of the list is a list of three elements.
  1225. The first element is the character used as a marker for fontification.
  1226. The second element is a variable name, set in org-export-generic. That
  1227. variable will be dereferenced to obtain a formatting string to wrap
  1228. fontified text with.
  1229. The third element decides whether to protect converted text from other
  1230. conversions.")
  1231. ;;; Cargo-culted from the latex translation. I couldn't figure out how
  1232. ;;; to keep the structure since the generic export operates on lines, rather
  1233. ;;; than on a buffer as in the latex export, meaning that none of the
  1234. ;;; search forward code could be kept. This led me to rewrite the
  1235. ;;; whole thing recursively. A huge lose for efficiency (potentially),
  1236. ;;; but I couldn't figure out how to make the looping work.
  1237. ;;; Worse, it's /doubly/ recursive, because this function calls
  1238. ;;; org-export-generic-emph-format, which can call it recursively...
  1239. ;;; [2010/05/20:rpg]
  1240. (defun org-export-generic-fontify (string)
  1241. "Convert fontification according to generic rules."
  1242. (if (string-match org-emph-re string)
  1243. ;; The match goes one char after the *string*, except at the end of a line
  1244. (let ((emph (assoc (match-string 3 string)
  1245. org-export-generic-emphasis-alist))
  1246. (beg (match-beginning 0))
  1247. (end (match-end 0)))
  1248. (unless emph
  1249. (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1250. (match-string 3 string)))
  1251. ;; now we need to determine whether we have strikethrough or
  1252. ;; a list, which is a bit nasty
  1253. (if (and (equal (match-string 3 string) "+")
  1254. (save-match-data
  1255. (string-match "\\`-+\\'" (match-string 4 string))))
  1256. ;; a list --- skip this match and recurse on the point after the
  1257. ;; first emph char...
  1258. (concat (substring string 0 (1+ (match-beginning 3)))
  1259. (org-export-generic-fontify (substring string (match-beginning 3))))
  1260. (concat (substring string 0 beg) ;; part before the match
  1261. (match-string 1 string)
  1262. (org-export-generic-emph-format (second emph)
  1263. (match-string 4 string)
  1264. (third emph))
  1265. (or (match-string 5 string) "")
  1266. (org-export-generic-fontify (substring string end)))))
  1267. string))
  1268. (defun org-export-generic-emph-format (format-varname string protect)
  1269. "Return a string that results from applying the markup indicated by
  1270. FORMAT-VARNAME to STRING."
  1271. (let ((format (symbol-value format-varname)))
  1272. (let ((string-to-emphasize
  1273. (if protect
  1274. string
  1275. (org-export-generic-fontify string))))
  1276. (if format
  1277. (format format string-to-emphasize)
  1278. string-to-emphasize))))
  1279. (provide 'org-generic)
  1280. (provide 'org-export-generic)
  1281. ;;; org-export-generic.el ends here