org-export-generic.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. ;;;###autoload
  417. (defun org-set-generic-type (type definition)
  418. "Adds a TYPE and DEFINITION to the existing list of defined generic
  419. export definitions."
  420. (aput 'org-generic-alist type definition))
  421. ;;; helper functions for org-set-generic-type
  422. (defvar org-export-generic-keywords nil)
  423. (defmacro* def-org-export-generic-keyword (keyword
  424. &key documentation
  425. type)
  426. "Define KEYWORD as a legitimate element for inclusion in
  427. the body of an org-set-generic-type definition."
  428. ;; TODO: push the documentation and type information
  429. ;; somewhere where it will do us some good.
  430. `(progn
  431. (pushnew ,keyword org-export-generic-keywords)))
  432. (def-org-export-generic-keyword :body-newline-paragraph
  433. :documentation "Bound either to NIL or to a pattern to be
  434. inserted in the output for every blank line in the input.
  435. The intention is to handle formats where text is flowed, and
  436. newlines are interpreted as significant \(e.g., as indicating
  437. preformatted text\). A common non-nil value for this keyword
  438. is \"\\n\". Should typically be combined with a value for
  439. :body-line-format that does NOT end with a newline."
  440. :type string)
  441. ;;; fontification keywords
  442. (def-org-export-generic-keyword :bold-format)
  443. (def-org-export-generic-keyword :italic-format)
  444. (def-org-export-generic-keyword :underline-format)
  445. (def-org-export-generic-keyword :strikethrough-format)
  446. (def-org-export-generic-keyword :code-format)
  447. (def-org-export-generic-keyword :verbatim-format)
  448. (defun org-export-generic-remember-section (type suffix &optional prefix)
  449. (setq org-export-generic-section-type type)
  450. (setq org-export-generic-section-suffix suffix)
  451. (if prefix
  452. (insert prefix)))
  453. (defun org-export-generic-check-section (type &optional prefix suffix)
  454. "checks to see if type is already in use, or we're switching parts
  455. If we're switching, then insert a potentially previously remembered
  456. suffix, and insert the current prefix immediately and then save the
  457. suffix a later change time."
  458. (when (not (equal type org-export-generic-section-type))
  459. (if org-export-generic-section-suffix
  460. (insert org-export-generic-section-suffix))
  461. (setq org-export-generic-section-type type)
  462. (setq org-export-generic-section-suffix suffix)
  463. (if prefix
  464. (insert prefix))))
  465. ;;;###autoload
  466. (defun org-export-generic (arg)
  467. "Export the outline as generic output.
  468. If there is an active region, export only the region.
  469. The prefix ARG specifies how many levels of the outline should become
  470. underlined headlines. The default is 3."
  471. (interactive "P")
  472. (setq-default org-todo-line-regexp org-todo-line-regexp)
  473. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  474. (org-infile-export-plist)))
  475. (region-p (org-region-active-p))
  476. (rbeg (and region-p (region-beginning)))
  477. (rend (and region-p (region-end)))
  478. (subtree-p
  479. (when region-p
  480. (save-excursion
  481. (goto-char rbeg)
  482. (and (org-at-heading-p)
  483. (>= (org-end-of-subtree t t) rend)))))
  484. (level-offset (if subtree-p
  485. (save-excursion
  486. (goto-char rbeg)
  487. (+ (funcall outline-level)
  488. (if org-odd-levels-only 1 0)))
  489. 0))
  490. (opt-plist (setq org-export-opt-plist
  491. (if subtree-p
  492. (org-export-add-subtree-options opt-plist rbeg)
  493. opt-plist)))
  494. helpstart
  495. (bogus (mapc (lambda (x)
  496. (setq helpstart
  497. (concat helpstart "\["
  498. (char-to-string
  499. (plist-get (cdr x) :key-binding))
  500. "] " (car x) "\n")))
  501. org-generic-alist))
  502. (help (concat helpstart "
  503. \[ ] the current setting of the org-generic-export-type variable
  504. "))
  505. (cmds
  506. (append
  507. (mapcar (lambda (x)
  508. (list
  509. (plist-get (cdr x) :key-binding)
  510. (car x)))
  511. org-generic-alist)
  512. (list (list ? "default"))))
  513. r1 r2 ass
  514. ;; read in the type to use
  515. (export-plist
  516. (progn
  517. (save-excursion
  518. (save-window-excursion
  519. (delete-other-windows)
  520. (with-output-to-temp-buffer "*Org Export/Generic Styles Help*"
  521. (princ help))
  522. (org-fit-window-to-buffer (get-buffer-window
  523. "*Org Export/Generic Styles Help*"))
  524. (message "Select command: ")
  525. (setq r1 (read-char-exclusive))))
  526. (setq r2 (if (< r1 27) (+ r1 96) r1))
  527. (unless (setq ass (cadr (assq r2 cmds)))
  528. (error "No command associated with key %c" r1))
  529. (cdr (assoc
  530. (if (equal ass "default") org-generic-export-type ass)
  531. org-generic-alist))))
  532. (custom-times org-display-custom-times)
  533. (org-generic-current-indentation '(0 . 0))
  534. (level 0) (old-level 0) line txt lastwastext
  535. (umax nil)
  536. (umax-toc nil)
  537. (case-fold-search nil)
  538. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  539. (filesuffix (or (plist-get export-plist :file-suffix) ".foo"))
  540. (filename (concat (file-name-as-directory
  541. (org-export-directory :ascii opt-plist))
  542. (file-name-sans-extension
  543. (or (and subtree-p
  544. (org-entry-get (region-beginning)
  545. "EXPORT_FILE_NAME" t))
  546. (file-name-nondirectory bfname)))
  547. filesuffix))
  548. (filename (if (equal (file-truename filename)
  549. (file-truename bfname))
  550. (concat filename filesuffix)
  551. filename))
  552. (buffer (find-file-noselect filename))
  553. (org-levels-open (make-vector org-level-max nil))
  554. (odd org-odd-levels-only)
  555. (date (plist-get opt-plist :date))
  556. (author (plist-get opt-plist :author))
  557. (title (or (and subtree-p (org-export-get-title-from-subtree))
  558. (plist-get opt-plist :title)
  559. (and (not
  560. (plist-get opt-plist :skip-before-1st-heading))
  561. (org-export-grab-title-from-buffer))
  562. (file-name-sans-extension
  563. (file-name-nondirectory bfname))))
  564. (email (plist-get opt-plist :email))
  565. (language (plist-get opt-plist :language))
  566. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  567. ; (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  568. (todo nil)
  569. (lang-words nil)
  570. (region
  571. (buffer-substring
  572. (if (org-region-active-p) (region-beginning) (point-min))
  573. (if (org-region-active-p) (region-end) (point-max))))
  574. (org-export-current-backend 'org-export-generic)
  575. (lines (org-split-string
  576. (org-export-preprocess-string
  577. region
  578. :for-backend 'ascii
  579. :skip-before-1st-heading
  580. (plist-get opt-plist :skip-before-1st-heading)
  581. :drawers (plist-get export-plist :drawers-export)
  582. :tags (plist-get export-plist :tags-export)
  583. :priority (plist-get export-plist :priority-export)
  584. :footnotes (plist-get export-plist :footnotes-export)
  585. :timestamps (plist-get export-plist :timestamps-export)
  586. :todo-keywords (plist-get export-plist :todo-keywords-export)
  587. :verbatim-multiline t
  588. :select-tags (plist-get export-plist :select-tags-export)
  589. :exclude-tags (plist-get export-plist :exclude-tags-export)
  590. :emph-multiline t
  591. :archived-trees
  592. (plist-get export-plist :archived-trees-export)
  593. :add-text (plist-get opt-plist :text))
  594. "\n"))
  595. ;; export-generic plist variables
  596. (withtags (plist-get export-plist :tags-export))
  597. (tagsintoc (plist-get export-plist :toc-tags-export))
  598. (tocnotagsstr (or (plist-get export-plist :toc-tags-none-string) ""))
  599. (tocdepth (plist-get export-plist :toc-indent-depth))
  600. (tocindentchar (plist-get export-plist :toc-indent-char))
  601. (tocsecnums (plist-get export-plist :toc-section-numbers))
  602. (tocsecnumform (plist-get export-plist :toc-section-number-format))
  603. (tocformat (plist-get export-plist :toc-format))
  604. (tocformtodo (plist-get export-plist :toc-format-with-todo))
  605. (tocprefix (plist-get export-plist :toc-prefix))
  606. (tocsuffix (plist-get export-plist :toc-suffix))
  607. (bodyfixedpre (plist-get export-plist :body-line-fixed-prefix))
  608. (bodyfixedsuf (plist-get export-plist :body-line-fixed-suffix))
  609. (bodyfixedform (or (plist-get export-plist :body-line-fixed-format)
  610. "%s"))
  611. (listprefix (plist-get export-plist :body-list-prefix))
  612. (listsuffix (plist-get export-plist :body-list-suffix))
  613. (listformat (or (plist-get export-plist :body-list-format) "%s\n"))
  614. (numlistleavenum
  615. (plist-get export-plist :body-number-list-leave-number))
  616. (numlistprefix (plist-get export-plist :body-number-list-prefix))
  617. (numlistsuffix (plist-get export-plist :body-number-list-suffix))
  618. (numlistformat
  619. (or (plist-get export-plist :body-number-list-format) "%s\n"))
  620. (listchecktodo
  621. (or (plist-get export-plist :body-list-checkbox-todo) "\\1"))
  622. (listcheckdone
  623. (or (plist-get export-plist :body-list-checkbox-done) "\\1"))
  624. (listcheckhalf
  625. (or (plist-get export-plist :body-list-checkbox-half) "\\1"))
  626. (listchecktodoend
  627. (or (plist-get export-plist :body-list-checkbox-todo-end) ""))
  628. (listcheckdoneend
  629. (or (plist-get export-plist :body-list-checkbox-done-end) ""))
  630. (listcheckhalfend
  631. (or (plist-get export-plist :body-list-checkbox-half-end) ""))
  632. (bodytablestart
  633. (or (plist-get export-plist :body-table-start) ""))
  634. (bodytableend
  635. (or (plist-get export-plist :body-table-end) ""))
  636. (bodytablerowstart
  637. (or (plist-get export-plist :body-table-row-start) ""))
  638. (bodytablerowend
  639. (or (plist-get export-plist :body-table-row-end) ""))
  640. (bodytablecellstart
  641. (or (plist-get export-plist :body-table-cell-start) ""))
  642. (bodytablecellend
  643. (or (plist-get export-plist :body-table-cell-end) ""))
  644. (bodytablefirstcellstart
  645. (or (plist-get export-plist :body-table-first-cell-start) ""))
  646. (bodytableinteriorcellstart
  647. (or (plist-get export-plist :body-table-interior-cell-start) ""))
  648. (bodytableinteriorcellend
  649. (or (plist-get export-plist :body-table-interior-cell-end) ""))
  650. (bodytablelastcellend
  651. (or (plist-get export-plist :body-table-last-cell-end) ""))
  652. (bodytablehlinestart
  653. (or (plist-get export-plist :body-table-hline-start) " \\1"))
  654. (bodytablehlineend
  655. (or (plist-get export-plist :body-table-hline-end) ""))
  656. (bodynewline-paragraph (plist-get export-plist :body-newline-paragraph))
  657. (bodytextpre (plist-get export-plist :body-text-prefix))
  658. (bodytextsuf (plist-get export-plist :body-text-suffix))
  659. (bodylinewrap (plist-get export-plist :body-line-wrap))
  660. (bodylineform (or (plist-get export-plist :body-line-format) "%s"))
  661. (blockquotestart (or (plist-get export-plist :blockquote-start) "\n\n\t"))
  662. (blockquoteend (or (plist-get export-plist :blockquote-end) "\n\n"))
  663. ;; dynamic variables used heinously in fontification
  664. ;; not referenced locally...
  665. (format-boldify (plist-get export-plist :bold-format))
  666. (format-italicize (plist-get export-plist :italic-format))
  667. (format-underline (plist-get export-plist :underline-format))
  668. (format-strikethrough (plist-get export-plist :strikethrough-format))
  669. (format-code (plist-get export-plist :code-format))
  670. (format-verbatim (plist-get export-plist :verbatim-format))
  671. thetoc toctags have-headings first-heading-pos
  672. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  673. (let ((inhibit-read-only t))
  674. (org-unmodified
  675. (remove-text-properties (point-min) (point-max)
  676. '(:org-license-to-kill t))))
  677. (setq org-min-level (org-get-min-level lines level-offset))
  678. (setq org-last-level org-min-level)
  679. (org-init-section-numbers)
  680. (find-file-noselect filename)
  681. (setq lang-words (or (assoc language org-export-language-setup)
  682. (assoc "en" org-export-language-setup)))
  683. (switch-to-buffer-other-window buffer)
  684. (erase-buffer)
  685. (fundamental-mode)
  686. ;; create local variables for all options, to make sure all called
  687. ;; functions get the correct information
  688. (mapc (lambda (x)
  689. (set (make-local-variable (nth 2 x))
  690. (plist-get opt-plist (car x))))
  691. org-export-plist-vars)
  692. (org-set-local 'org-odd-levels-only odd)
  693. (setq umax (if arg (prefix-numeric-value arg)
  694. org-export-headline-levels))
  695. (setq umax-toc umax)
  696. ;; File header
  697. (if title
  698. (insert
  699. (org-export-generic-header title export-plist
  700. :title-prefix
  701. :title-format
  702. :title-suffix)))
  703. (if (and (or author email)
  704. (plist-get export-plist :author-export))
  705. (insert (concat (nth 1 lang-words) ": " (or author "")
  706. (if email (concat " <" email ">") "")
  707. "\n")))
  708. (cond
  709. ((and date (string-match "%" date))
  710. (setq date (format-time-string date)))
  711. (date)
  712. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  713. (if (and date (plist-get export-plist :date-export))
  714. (insert
  715. (org-export-generic-header date export-plist
  716. :date-prefix
  717. :date-format
  718. :date-suffix)))
  719. ;; export the table of contents first
  720. (if (plist-get export-plist :toc-export)
  721. (progn
  722. (push
  723. (org-export-generic-header (nth 3 lang-words) export-plist
  724. :toc-header-prefix
  725. :toc-header-format
  726. :toc-header-suffix)
  727. thetoc)
  728. (if tocprefix
  729. (push tocprefix thetoc))
  730. (mapc #'(lambda (line)
  731. (if (string-match org-todo-line-regexp line)
  732. ;; This is a headline
  733. (progn
  734. (setq have-headings t)
  735. (setq level (- (match-end 1) (match-beginning 1)
  736. level-offset)
  737. level (org-tr-level level)
  738. txt (match-string 3 line)
  739. todo
  740. (or (and org-export-mark-todo-in-toc
  741. (match-beginning 2)
  742. (not (member (match-string 2 line)
  743. org-done-keywords)))
  744. ; TODO, not DONE
  745. (and org-export-mark-todo-in-toc
  746. (= level umax-toc)
  747. (org-search-todo-below
  748. line lines level))))
  749. (setq txt (org-html-expand-for-generic txt))
  750. (while (string-match org-bracket-link-regexp txt)
  751. (setq txt
  752. (replace-match
  753. (match-string (if (match-end 2) 3 1) txt)
  754. t t txt)))
  755. (if (and (not tagsintoc)
  756. (string-match
  757. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  758. txt))
  759. (setq txt (replace-match "" t t txt))
  760. ; include tags but formated
  761. (if (string-match
  762. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
  763. txt)
  764. (progn
  765. (setq
  766. toctags
  767. (org-export-generic-header
  768. (match-string 1 txt)
  769. export-plist :toc-tags-prefix
  770. :toc-tags-format :toc-tags-suffix))
  771. (string-match
  772. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  773. txt)
  774. (setq txt (replace-match "" t t txt)))
  775. (setq toctags tocnotagsstr)))
  776. (if (string-match quote-re0 txt)
  777. (setq txt (replace-match "" t t txt)))
  778. (if (<= level umax-toc)
  779. (progn
  780. (push
  781. (concat
  782. (make-string
  783. (* (max 0 (- level org-min-level)) tocdepth)
  784. tocindentchar)
  785. (if tocsecnums
  786. (format tocsecnumform
  787. (org-section-number level))
  788. "")
  789. (format
  790. (if todo tocformtodo tocformat)
  791. txt)
  792. toctags)
  793. thetoc)
  794. (setq org-last-level level))))))
  795. lines)
  796. (if tocsuffix
  797. (push tocsuffix thetoc))
  798. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  799. (org-init-section-numbers)
  800. (org-export-generic-check-section "top")
  801. (while (setq line (pop lines))
  802. (when (and link-buffer (string-match org-outline-regexp-bol line))
  803. (org-export-generic-push-links (nreverse link-buffer))
  804. (setq link-buffer nil))
  805. (setq wrap nil)
  806. ;; Remove the quoted HTML tags.
  807. ;; XXX
  808. (setq line (org-html-expand-for-generic line))
  809. ;; Replace links with the description when possible
  810. ;; XXX
  811. (while (string-match org-bracket-link-regexp line)
  812. (setq link (match-string 1 line)
  813. desc0 (match-string 3 line)
  814. desc (or desc0 (match-string 1 line)))
  815. (if (and (> (length link) 8)
  816. (equal (substring link 0 8) "coderef:"))
  817. (setq line (replace-match
  818. (format (org-export-get-coderef-format (substring link 8) desc)
  819. (cdr (assoc
  820. (substring link 8)
  821. org-export-code-refs)))
  822. t t line))
  823. (setq rpl (concat "["
  824. (or (match-string 3 line) (match-string 1 line))
  825. "]"))
  826. (when (and desc0 (not (equal desc0 link)))
  827. (if org-export-generic-links-to-notes
  828. (push (cons desc0 link) link-buffer)
  829. (setq rpl (concat rpl " (" link ")")
  830. wrap (+ (length line) (- (length (match-string 0 line)))
  831. (length desc)))))
  832. (setq line (replace-match rpl t t line))))
  833. (when custom-times
  834. (setq line (org-translate-time line)))
  835. (cond
  836. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  837. ;;
  838. ;; a Headline
  839. ;;
  840. (org-export-generic-check-section "headline")
  841. (setq first-heading-pos (or first-heading-pos (point)))
  842. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  843. level-offset))
  844. txt (match-string 2 line))
  845. (org-generic-level-start level old-level txt umax export-plist lines)
  846. (setq old-level level))
  847. ((and org-export-with-tables
  848. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  849. ;;
  850. ;; a Table
  851. ;;
  852. (org-export-generic-check-section "table")
  853. (if (not table-open)
  854. ;; New table starts
  855. (setq table-open t table-buffer nil))
  856. ;; Accumulate table lines
  857. (setq table-buffer (cons line table-buffer))
  858. (when (or (not lines)
  859. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  860. (car lines))))
  861. (setq table-open nil
  862. table-buffer (nreverse table-buffer))
  863. (insert (mapconcat
  864. (lambda (x)
  865. (org-fix-indentation x org-generic-current-indentation))
  866. (org-format-table-generic table-buffer)
  867. "\n") "\n")))
  868. ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line)
  869. ;;
  870. ;; pre-formatted text
  871. ;;
  872. (setq line (replace-match "\\1" nil nil line))
  873. (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf)
  874. (insert (format bodyfixedform line)))
  875. ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line)
  876. ;; if the bullet list item is an asterisk, the leading space is /mandatory/
  877. ;; [2010/02/02:rpg]
  878. (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line))
  879. ;;
  880. ;; plain list item
  881. ;; TODO: nested lists
  882. ;;
  883. ;; first add a line break between any previous paragraph or line item and this
  884. ;; one
  885. (when bodynewline-paragraph
  886. (insert bodynewline-paragraph))
  887. ;; I believe this gets rid of leading whitespace.
  888. (setq line (replace-match "" nil nil line))
  889. ;; won't this insert the suffix /before/ the last line of the list?
  890. ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
  891. ;; unless 'org-empty-line-terminates-plain-lists' is true?
  892. (org-export-generic-check-section "liststart" listprefix listsuffix)
  893. ;; deal with checkboxes
  894. (cond
  895. ((string-match "^\\(\\[ \\]\\)[ \t]*" line)
  896. (setq line (concat (replace-match listchecktodo nil nil line)
  897. listchecktodoend)))
  898. ((string-match "^\\(\\[X\\]\\)[ \t]*" line)
  899. (setq line (concat (replace-match listcheckdone nil nil line)
  900. listcheckdoneend)))
  901. ((string-match "^\\(\\[/\\]\\)[ \t]*" line)
  902. (setq line (concat (replace-match listcheckhalf nil nil line)
  903. listcheckhalfend))))
  904. (insert (format listformat (org-export-generic-fontify line))))
  905. ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line)
  906. ;;
  907. ;; numbered list item
  908. ;;
  909. ;; TODO: nested lists
  910. ;;
  911. (setq line (replace-match (if numlistleavenum "\\2" "") nil nil line))
  912. (org-export-generic-check-section "numliststart"
  913. numlistprefix numlistsuffix)
  914. ;; deal with checkboxes
  915. ;; TODO: whoops; leaving the numbers is a problem for ^ matching
  916. (cond
  917. ((string-match "\\(\\[ \\]\\)[ \t]*" line)
  918. (setq line (concat (replace-match listchecktodo nil nil line)
  919. listchecktodoend)))
  920. ((string-match "\\(\\[X\\]\\)[ \t]*" line)
  921. (setq line (concat (replace-match listcheckdone nil nil line)
  922. listcheckdoneend)))
  923. ((string-match "\\(\\[/\\]\\)[ \t]*" line)
  924. (setq line (concat (replace-match listcheckhalf nil nil line)
  925. listcheckhalfend))))
  926. (insert (format numlistformat (org-export-generic-fontify line))))
  927. ((equal line "ORG-BLOCKQUOTE-START")
  928. (setq line blockquotestart))
  929. ((equal line "ORG-BLOCKQUOTE-END")
  930. (setq line blockquoteend))
  931. ((string-match "^\\s-*$" line)
  932. ;; blank line
  933. (if bodynewline-paragraph
  934. (insert bodynewline-paragraph)))
  935. (t
  936. ;;
  937. ;; body
  938. ;;
  939. (org-export-generic-check-section "body" bodytextpre bodytextsuf)
  940. (setq line
  941. (org-export-generic-fontify line))
  942. ;; XXX: properties? list?
  943. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  944. (setq line (replace-match "\\1\\3:" t nil line)))
  945. (setq line (org-fix-indentation line org-generic-current-indentation))
  946. ;; Remove forced line breaks
  947. (if (string-match "\\\\\\\\[ \t]*$" line)
  948. (setq line (replace-match "" t t line)))
  949. (if bodylinewrap
  950. ;; XXX: was dependent on wrap var which was calculated by???
  951. (if (> (length line) bodylinewrap)
  952. (setq line
  953. (org-export-generic-wrap line bodylinewrap))
  954. (setq line line)))
  955. (insert (format bodylineform line)))))
  956. ;; if we're at a level > 0; insert the closing body level stuff
  957. (let ((counter 0))
  958. (while (> (- level counter) 0)
  959. (insert
  960. (org-export-generic-format export-plist :body-section-suffix 0
  961. (- level counter)))
  962. (setq counter (1+ counter))))
  963. (org-export-generic-check-section "bottom")
  964. (org-export-generic-push-links (nreverse link-buffer))
  965. (normal-mode)
  966. ;; insert the table of contents
  967. (when thetoc
  968. (goto-char (point-min))
  969. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  970. (progn
  971. (goto-char (match-beginning 0))
  972. (replace-match ""))
  973. (goto-char first-heading-pos))
  974. (mapc 'insert thetoc)
  975. (or (looking-at "[ \t]*\n[ \t]*\n")
  976. (insert "\n\n")))
  977. ;; Convert whitespace place holders
  978. (goto-char (point-min))
  979. (let (beg end)
  980. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  981. (setq end (next-single-property-change beg 'org-whitespace))
  982. (goto-char beg)
  983. (delete-region beg end)
  984. (insert (make-string (- end beg) ?\ ))))
  985. (save-buffer)
  986. ;; remove display and invisible chars
  987. (let (beg end)
  988. (goto-char (point-min))
  989. (while (setq beg (next-single-property-change (point) 'display))
  990. (setq end (next-single-property-change beg 'display))
  991. (delete-region beg end)
  992. (goto-char beg)
  993. (insert "=>"))
  994. (goto-char (point-min))
  995. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  996. (setq end (next-single-property-change beg 'org-cwidth))
  997. (delete-region beg end)
  998. (goto-char beg)))
  999. (goto-char (point-min))))
  1000. (defun org-export-generic-format (export-plist prop &optional len n reverse)
  1001. "converts a property specification to a string given types of properties
  1002. The EXPORT-PLIST should be defined as the lookup plist.
  1003. The PROP should be the property name to search for in it.
  1004. LEN is set to the length of multi-characters strings to generate (or 0)
  1005. N is the tree depth
  1006. REVERSE means to reverse the list if the plist match is a list
  1007. "
  1008. (let* ((prefixtype (plist-get export-plist prop))
  1009. subtype)
  1010. (cond
  1011. ((null prefixtype) "")
  1012. ((and len (char-or-string-p prefixtype) (not (stringp prefixtype)))
  1013. ;; sequence of chars
  1014. (concat (make-string len prefixtype) "\n"))
  1015. ((stringp prefixtype)
  1016. prefixtype)
  1017. ((and n (listp prefixtype))
  1018. (if reverse
  1019. (setq prefixtype (reverse prefixtype)))
  1020. (setq subtype (if (> n (length prefixtype))
  1021. (car (last prefixtype))
  1022. (nth (1- n) prefixtype)))
  1023. (if (stringp subtype)
  1024. subtype
  1025. (concat (make-string len subtype) "\n")))
  1026. (t ""))))
  1027. (defun org-export-generic-header (header export-plist
  1028. prefixprop formatprop postfixprop
  1029. &optional n reverse)
  1030. "convert a header to an output string given formatting property names"
  1031. (let* ((formatspec (plist-get export-plist formatprop))
  1032. (len (length header)))
  1033. (concat
  1034. (org-export-generic-format export-plist prefixprop len n reverse)
  1035. (format (or formatspec "%s") header)
  1036. (org-export-generic-format export-plist postfixprop len n reverse))))
  1037. (defun org-export-generic-preprocess (parameters)
  1038. "Do extra work for ASCII export"
  1039. ;; Put quotes around verbatim text
  1040. (goto-char (point-min))
  1041. (while (re-search-forward org-verbatim-re nil t)
  1042. (goto-char (match-end 2))
  1043. (delete-backward-char 1) (insert "'")
  1044. (goto-char (match-beginning 2))
  1045. (delete-char 1) (insert "`")
  1046. (goto-char (match-end 2)))
  1047. ;; Remove target markers
  1048. (goto-char (point-min))
  1049. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  1050. (replace-match "\\1\\2")))
  1051. (defun org-html-expand-for-generic (line)
  1052. "Handle quoted HTML for ASCII export."
  1053. (if org-export-html-expand
  1054. (while (string-match "@<[^<>\n]*>" line)
  1055. ;; We just remove the tags for now.
  1056. (setq line (replace-match "" nil nil line))))
  1057. line)
  1058. (defun org-export-generic-wrap (line where)
  1059. "Wrap LINE at or before WHERE."
  1060. (let* ((ind (org-get-indentation line))
  1061. (indstr (make-string ind ?\ ))
  1062. (len (length line))
  1063. (result "")
  1064. pos didfirst)
  1065. (while (> len where)
  1066. (catch 'found
  1067. (loop for i from where downto (/ where 2) do
  1068. (and (equal (aref line i) ?\ )
  1069. (setq pos i)
  1070. (throw 'found t))))
  1071. (if pos
  1072. (progn
  1073. (setq result
  1074. (concat result
  1075. (if didfirst indstr "")
  1076. (substring line 0 pos)
  1077. "\n"))
  1078. (setq didfirst t)
  1079. (setq line (substring line (1+ pos)))
  1080. (setq len (length line)))
  1081. (setq result (concat result line))
  1082. (setq len 0)))
  1083. (concat result indstr line)))
  1084. (defun org-export-generic-push-links (link-buffer)
  1085. "Push out links in the buffer."
  1086. (when link-buffer
  1087. ;; We still have links to push out.
  1088. (insert "\n")
  1089. (let ((ind ""))
  1090. (save-match-data
  1091. (if (save-excursion
  1092. (re-search-backward
  1093. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  1094. (setq ind (or (match-string 2)
  1095. (make-string (length (match-string 3)) ?\ )))))
  1096. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  1097. link-buffer))
  1098. (insert "\n")))
  1099. (defun org-generic-level-start (level old-level title umax export-plist
  1100. &optional lines)
  1101. "Insert a new level in a generic export."
  1102. (let ((n (- level umax 1))
  1103. (ind 0)
  1104. (diff (- level old-level)) (counter 0)
  1105. (secnums (plist-get export-plist :body-header-section-numbers))
  1106. (secnumformat
  1107. (plist-get export-plist :body-header-section-number-format))
  1108. char tagstring)
  1109. (unless org-export-with-tags
  1110. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1111. (setq title (replace-match "" t t title))))
  1112. (cond
  1113. ;; going deeper
  1114. ((> level old-level)
  1115. (while (< (+ old-level counter) (1- level))
  1116. (insert
  1117. (org-export-generic-format export-plist :body-section-prefix 0
  1118. (+ old-level counter)))
  1119. (setq counter (1+ counter))))
  1120. ;; going up
  1121. ((< level old-level)
  1122. (while (> (- old-level counter) (1- level))
  1123. (insert
  1124. (org-export-generic-format export-plist :body-section-suffix 0
  1125. (- old-level counter)))
  1126. (setq counter (1+ counter))))
  1127. ;; same level
  1128. ((= level old-level)
  1129. (insert
  1130. (org-export-generic-format export-plist :body-section-suffix 0 level))))
  1131. (insert
  1132. (org-export-generic-format export-plist :body-section-prefix 0 level))
  1133. (if (and org-export-with-section-numbers
  1134. secnums
  1135. (or (not (numberp secnums))
  1136. (< level secnums)))
  1137. (setq title
  1138. (concat (format (or secnumformat "%s ")
  1139. (org-section-number level)) title)))
  1140. ;; handle tags and formatting
  1141. (if (string-match
  1142. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title)
  1143. (progn
  1144. (if (plist-get export-plist :body-tags-export)
  1145. (setq tagstring (org-export-generic-header (match-string 1 title)
  1146. export-plist
  1147. :body-tags-prefix
  1148. :body-tags-format
  1149. :body-tags-suffix)))
  1150. (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title)
  1151. (setq title (replace-match "" t t title)))
  1152. (setq tagstring (plist-get export-plist :body-tags-none-string)))
  1153. (insert
  1154. (org-export-generic-header title export-plist
  1155. :body-section-header-prefix
  1156. :body-section-header-format
  1157. :body-section-header-suffix
  1158. level))
  1159. (if tagstring
  1160. (insert tagstring))
  1161. (setq org-generic-current-indentation '(0 . 0))))
  1162. (defun org-insert-centered (s &optional underline)
  1163. "Insert the string S centered and underline it with character UNDERLINE."
  1164. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  1165. (insert (make-string ind ?\ ) s "\n")
  1166. (if underline
  1167. (insert (make-string ind ?\ )
  1168. (make-string (string-width s) underline)
  1169. "\n"))))
  1170. (defvar org-table-colgroup-info nil)
  1171. (defun org-format-table-generic (lines)
  1172. "Format a table for ascii export."
  1173. (if (stringp lines)
  1174. (setq lines (org-split-string lines "\n")))
  1175. (if (not (string-match "^[ \t]*|" (car lines)))
  1176. ;; Table made by table.el - test for spanning
  1177. lines
  1178. ;; A normal org table
  1179. ;; Get rid of hlines at beginning and end
  1180. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1181. (setq lines (nreverse lines))
  1182. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1183. (setq lines (nreverse lines))
  1184. (when org-export-table-remove-special-lines
  1185. ;; Check if the table has a marking column. If yes remove the
  1186. ;; column and the special lines
  1187. (setq lines (org-table-clean-before-export lines)))
  1188. ;; Get rid of the vertical lines except for grouping
  1189. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  1190. (rtn (list bodytablestart)) line vl1 start)
  1191. (while (setq line (pop lines))
  1192. (setq line (concat bodytablerowstart line))
  1193. (if (string-match org-table-hline-regexp line)
  1194. (and (string-match "|\\(.*\\)|" line)
  1195. (setq line (replace-match (concat bodytablehlinestart bodytablehlineend) t nil line)))
  1196. (setq start 0 vl1 vl)
  1197. (if (string-match "|\\(.*\\)|" line)
  1198. (setq line (replace-match (concat bodytablefirstcellstart bodytablecellstart " \\1 " bodytablecellend bodytablelastcellend) t nil line)))
  1199. (while (string-match "|" line start)
  1200. (setq start (+ (match-end 0) (length (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart))))
  1201. (or (pop vl1) (setq line (replace-match (concat bodytablecellend bodytableinteriorcellend bodytableinteriorcellstart bodytablecellstart) t t line)))))
  1202. (setq line (concat line bodytablerowend))
  1203. (push line rtn))
  1204. (setq rtn (cons bodytableend rtn))
  1205. (nreverse rtn))))
  1206. (defun org-colgroup-info-to-vline-list (info)
  1207. (let (vl new last)
  1208. (while info
  1209. (setq last new new (pop info))
  1210. (if (or (memq last '(:end :startend))
  1211. (memq new '(:start :startend)))
  1212. (push t vl)
  1213. (push nil vl)))
  1214. (setq vl (nreverse vl))
  1215. (and vl (setcar vl nil))
  1216. vl))
  1217. ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
  1218. (defvar org-export-generic-emphasis-alist
  1219. '(("*" format-boldify nil)
  1220. ("/" format-italicize nil)
  1221. ("_" format-underline nil)
  1222. ("+" format-strikethrough nil)
  1223. ("=" format-code t)
  1224. ("~" format-verbatim t))
  1225. "Alist of org format -> formatting variables for fontification.
  1226. Each element of the list is a list of three elements.
  1227. The first element is the character used as a marker for fontification.
  1228. The second element is a variable name, set in org-export-generic. That
  1229. variable will be dereferenced to obtain a formatting string to wrap
  1230. fontified text with.
  1231. The third element decides whether to protect converted text from other
  1232. conversions.")
  1233. ;;; Cargo-culted from the latex translation. I couldn't figure out how
  1234. ;;; to keep the structure since the generic export operates on lines, rather
  1235. ;;; than on a buffer as in the latex export, meaning that none of the
  1236. ;;; search forward code could be kept. This led me to rewrite the
  1237. ;;; whole thing recursively. A huge lose for efficiency (potentially),
  1238. ;;; but I couldn't figure out how to make the looping work.
  1239. ;;; Worse, it's /doubly/ recursive, because this function calls
  1240. ;;; org-export-generic-emph-format, which can call it recursively...
  1241. ;;; [2010/05/20:rpg]
  1242. (defun org-export-generic-fontify (string)
  1243. "Convert fontification according to generic rules."
  1244. (if (string-match org-emph-re string)
  1245. ;; The match goes one char after the *string*, except at the end of a line
  1246. (let ((emph (assoc (match-string 3 string)
  1247. org-export-generic-emphasis-alist))
  1248. (beg (match-beginning 0))
  1249. (end (match-end 0)))
  1250. (unless emph
  1251. (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1252. (match-string 3 string)))
  1253. ;; now we need to determine whether we have strikethrough or
  1254. ;; a list, which is a bit nasty
  1255. (if (and (equal (match-string 3 string) "+")
  1256. (save-match-data
  1257. (string-match "\\`-+\\'" (match-string 4 string))))
  1258. ;; a list --- skip this match and recurse on the point after the
  1259. ;; first emph char...
  1260. (concat (substring string 0 (1+ (match-beginning 3)))
  1261. (org-export-generic-fontify (substring string (match-beginning 3))))
  1262. (concat (substring string 0 beg) ;; part before the match
  1263. (match-string 1 string)
  1264. (org-export-generic-emph-format (second emph)
  1265. (match-string 4 string)
  1266. (third emph))
  1267. (or (match-string 5 string) "")
  1268. (org-export-generic-fontify (substring string end)))))
  1269. string))
  1270. (defun org-export-generic-emph-format (format-varname string protect)
  1271. "Return a string that results from applying the markup indicated by
  1272. FORMAT-VARNAME to STRING."
  1273. (let ((format (symbol-value format-varname)))
  1274. (let ((string-to-emphasize
  1275. (if protect
  1276. string
  1277. (org-export-generic-fontify string))))
  1278. (if format
  1279. (format format string-to-emphasize)
  1280. string-to-emphasize))))
  1281. (provide 'org-generic)
  1282. (provide 'org-export-generic)
  1283. ;;; org-export-generic.el ends here