org-export-generic.el 50 KB

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