org-export-generic.el 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. ;; org-export-generic.el --- Export frameworg with custom backends
  2. ;; Copyright (C) 2009-2011 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. ;; * oh my
  87. ;; * optmization (many plist extracts should be in let vars)
  88. ;; * define defcustom spec for the specifier list
  89. ;; * fonts: at least monospace is not handled at all here.
  90. ;;
  91. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  92. ;;
  93. ;;; Commentary:
  94. (require 'org-exp)
  95. (require 'assoc)
  96. (defgroup org-export-generic nil
  97. "Options specific for ASCII export of Org-mode files."
  98. :tag "Org Export ASCII"
  99. :group 'org-export)
  100. (defcustom org-export-generic-links-to-notes t
  101. "Non-nil means convert links to notes before the next headline.
  102. When nil, the link will be exported in place. If the line becomes long
  103. in this way, it will be wrapped."
  104. :group 'org-export-generic
  105. :type 'boolean)
  106. (defvar org-generic-current-indentation nil) ; For communication
  107. (defvar org-generic-alist
  108. '(
  109. ;;
  110. ;; generic DEMO exporter
  111. ;;
  112. ;; (this tries to use every specifier for demo purposes)
  113. ;;
  114. ("demo"
  115. :file-suffix ".txt"
  116. :key-binding ?d
  117. :header-prefix "<header>\n"
  118. :header-suffix "</header>\n"
  119. :author-export t
  120. :tags-export t
  121. :drawers-export t
  122. :title-prefix ?=
  123. :title-format "<h1>%s</h1>\n"
  124. :title-suffix ?=
  125. :date-export t
  126. :date-prefix "<date>"
  127. :date-format "<br /><b>Date:</b> <i>%s</i><br />"
  128. :date-suffix "</date>\n\n"
  129. :toc-export t
  130. :toc-header-prefix "<tocname>\n"
  131. :toc-header-format "__%s__\n"
  132. :toc-header-suffix "</tocname>\n"
  133. :toc-prefix "<toc>\n"
  134. :toc-suffix "</toc>\n"
  135. :toc-section-numbers t
  136. :toc-section-number-format "\#(%s) "
  137. :toc-format "--%s--"
  138. :toc-format-with-todo "!!%s!!\n"
  139. :toc-indent-char ?\
  140. :toc-indent-depth 4
  141. :toc-tags-export t
  142. :toc-tags-prefix " <tags>"
  143. :toc-tags-format "*%s*"
  144. :toc-tags-suffix "</tags>\n"
  145. :toc-tags-none-string "\n"
  146. :body-header-section-numbers 3 ; t = all, nil = none
  147. ; lists indicate different things per level
  148. ; list contents or straight value can either be a
  149. ; ?x char reference for printing strings that match the header len
  150. ; "" string to print directly
  151. :body-section-header-prefix ("<h1>" "<h2>" "<h3>"
  152. "<h4>" "<h5>" "<h6>")
  153. :body-section-header-format "%s"
  154. :body-section-header-suffix ("</h1>\n" "</h2>\n" "</h3>\n"
  155. "</h4>\n" "</h5>\n" "</h6>\n")
  156. :timestamps-export t
  157. :priorities-export t
  158. :todo-keywords-export t
  159. :body-tags-export t
  160. :body-tags-prefix " <tags>"
  161. :body-tags-suffix "</tags>\n"
  162. ; section prefixes/suffixes can be direct strings or lists as well
  163. :body-section-prefix "<secprefix>\n"
  164. :body-section-suffix "</secsuffix>\n"
  165. ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
  166. ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
  167. ; if preformated text should be included (eg, : prefixed)
  168. :body-line-export-preformated t
  169. :body-line-fixed-prefix "<pre>\n"
  170. :body-line-fixed-suffix "\n</pre>\n"
  171. :body-line-fixed-format "%s\n"
  172. :body-list-prefix "<list>\n"
  173. :body-list-suffix "</list>\n"
  174. :body-list-format "<li>%s</li>\n"
  175. :body-number-list-prefix "<ol>\n"
  176. :body-number-list-suffix "</ol>\n"
  177. :body-number-list-format "<li>%s</li>\n"
  178. :body-number-list-leave-number t
  179. :body-list-checkbox-todo "<checkbox type=\"todo\">"
  180. :body-list-checkbox-todo-end "</checkbox (todo)>"
  181. :body-list-checkbox-done "<checkbox type=\"done\">"
  182. :body-list-checkbox-done-end "</checkbox (done)>"
  183. :body-list-checkbox-half "<checkbox type=\"half\">"
  184. :body-list-checkbox-half-end "</checkbox (half)>"
  185. ; other body lines
  186. :body-line-format "%s"
  187. :body-line-wrap 60 ; wrap at 60 chars
  188. ; print above and below all body parts
  189. :body-text-prefix "<p>\n"
  190. :body-text-suffix "</p>\n"
  191. )
  192. ;;
  193. ;; ascii exporter
  194. ;;
  195. ;; (close to the original ascii specifier)
  196. ;;
  197. ("ascii"
  198. :file-suffix ".txt"
  199. :key-binding ?a
  200. :header-prefix ""
  201. :header-suffix ""
  202. :title-prefix ?=
  203. :title-format "%s\n"
  204. :title-suffix ?=
  205. :date-export t
  206. :date-prefix ""
  207. :date-format "Date: %s\n"
  208. :date-suffix ""
  209. :toc-header-prefix ""
  210. :toc-header-format "%s\n"
  211. :toc-header-suffix ?=
  212. :toc-export t
  213. :toc-section-numbers t
  214. :toc-section-number-format "%s "
  215. :toc-format "%s\n"
  216. :toc-format-with-todo "%s (*)\n"
  217. :toc-indent-char ?\
  218. :toc-indent-depth 4
  219. :body-header-section-numbers 3
  220. :body-section-prefix "\n"
  221. ; :body-section-header-prefix "\n"
  222. ; :body-section-header-format "%s\n"
  223. ; :body-section-header-suffix (?\$ ?\# ?^ ?\~ ?\= ?\-)
  224. :body-section-header-prefix ("" "" "" "* " " + " " - ")
  225. :body-section-header-format "%s\n"
  226. :body-section-header-suffix (?~ ?= ?- "\n" "\n" "\n")
  227. ; :body-section-marker-prefix ""
  228. ; :body-section-marker-chars (?\$ ?\# ?^ ?\~ ?\= ?\-)
  229. ; :body-section-marker-suffix "\n"
  230. :body-line-export-preformated t
  231. :body-line-format "%s\n"
  232. :body-line-wrap 75
  233. ; :body-text-prefix "<t>\n"
  234. ; :body-text-suffix "</t>\n"
  235. :body-bullet-list-prefix (?* ?+ ?-)
  236. ; :body-bullet-list-suffix (?* ?+ ?-)
  237. )
  238. ;;
  239. ;; wikipedia
  240. ;;
  241. ("wikipedia"
  242. :file-suffix ".txt"
  243. :key-binding ?w
  244. :header-prefix ""
  245. :header-suffix ""
  246. :title-format "= %s =\n"
  247. :date-export nil
  248. :toc-export nil
  249. :body-header-section-numbers nil
  250. :body-section-prefix "\n"
  251. :body-section-header-prefix ("= " "== " "=== "
  252. "==== " "===== " "====== ")
  253. :body-section-header-suffix (" =\n\n" " ==\n\n" " ===\n\n"
  254. " ====\n\n" " =====\n\n" " ======\n\n")
  255. :body-line-export-preformated t ;; yes/no/maybe???
  256. :body-line-format "%s\n"
  257. :body-line-wrap 75
  258. :body-line-fixed-format " %s\n"
  259. :body-list-format "* %s\n"
  260. :body-number-list-format "# %s\n"
  261. :body-bullet-list-prefix ("* " "** " "*** " "**** " "***** ")
  262. )
  263. ;;
  264. ;; minimal html exporter
  265. ;;
  266. ("html"
  267. ;; simple html output
  268. :file-suffix ".html"
  269. :key-binding ?h
  270. :header-prefix "<body>"
  271. :title-format "<h1>%s</h1>\n\n"
  272. :date-export t
  273. :date-format "<br /><b>Date:</b> <i>%s</i><br />\n\n"
  274. :toc-export nil
  275. :body-header-section-numbers 3
  276. :body-section-header-prefix ("<h1>" "<h2>" "<h3>"
  277. "<h4>" "<h5>" "<h6>")
  278. :body-section-header-format "%s"
  279. :body-section-header-suffix ("</h1>\n" "</h2>\n" "</h3>\n"
  280. "</h4>\n" "</h5>\n" "</h6>\n")
  281. :body-section-prefix "<secprefix>\n"
  282. :body-section-suffix "</secsuffix>\n"
  283. ; :body-section-prefix ("<sec1>\n" "<sec2>\n" "<sec3>\n")
  284. ; :body-section-suffix ("</sec1>\n" "</sec2>\n" "</sec3>\n")
  285. :body-line-export-preformated t
  286. :body-line-format "%s\n"
  287. :body-text-prefix "<p>\n"
  288. :body-text-suffix "</p>\n"
  289. :body-bullet-list-prefix (?* ?+ ?-)
  290. ; :body-bullet-list-suffix (?* ?+ ?-)
  291. )
  292. ;;
  293. ;; internet-draft .xml for xml2rfc exporter
  294. ;;
  295. ("ietfid"
  296. ;; this tries to use every specifier for demo purposes
  297. :file-suffix ".xml"
  298. :key-binding ?i
  299. :title-prefix "<?xml version=\"1.0\"\?>
  300. <!DOCTYPE rfc SYSTEM \"rfc2629.dtd\" [
  301. <!ENTITY rfcs PUBLIC '' 'blah'>
  302. <?rfc strict=\"yes\" ?>
  303. <?rfc toc=\"yes\" ?>
  304. <?rfc tocdepth=\"4\" ?>
  305. <?rfc symrefs=\"yes\" ?>
  306. <?rfc compact=\"yes\" ?>
  307. <?rfc subcompact=\"no\" ?>
  308. <rfc category=\"std\" ipr=\"pre5378Trust200902\" docName=\"FILLME.txt\">
  309. <front>
  310. "
  311. :title-format "<title abbrev=\"ABBREV HERE\">\n%s\n</title>\n"
  312. :title-suffix "<author initials=\"A.A\" surname=\"LASTNAME\" fullname=\"FULL NAME\">
  313. <organization>Comany, Inc..</organization>
  314. <address>
  315. <postal>
  316. <street></street>
  317. <city></city>
  318. <region></region>
  319. <code></code>
  320. <country></country>
  321. </postal>
  322. <phone></phone>
  323. <email></email>
  324. </address>
  325. </author>
  326. <date month=\"FILLMONTH\" year=\"FILLYEAR\"/>
  327. <area>Operations and Management</area>
  328. <workgroup>FIXME</workgroup>
  329. <abstract>\n"
  330. :date-export nil
  331. :toc-export nil
  332. :body-header-section-numbers nil
  333. :body-section-header-format "<section title=\"%s\">\n"
  334. :body-section-suffix "</section>\n"
  335. ; if preformated text should be included (eg, : prefixed)
  336. :body-line-export-preformated t
  337. :body-line-fixed-prefix "<figure>\n<artwork>\n"
  338. :body-line-fixed-suffix "\n</artwork>\n</figure>\n"
  339. ; other body lines
  340. :body-line-format "%s"
  341. :body-line-wrap 75
  342. ; print above and below all body parts
  343. :body-text-prefix "<t>\n"
  344. :body-text-suffix "</t>\n"
  345. :body-list-prefix "<list style=\"symbols\">\n"
  346. :body-list-suffix "</list>\n"
  347. :body-list-format "<t>%s</t>\n"
  348. )
  349. ("trac-wiki"
  350. :file-suffix ".txt"
  351. :key-binding ?T
  352. ;; lifted from wikipedia exporter
  353. :header-prefix ""
  354. :header-suffix ""
  355. :title-format "= %s =\n"
  356. :date-export nil
  357. :toc-export nil
  358. :body-header-section-numbers nil
  359. :body-section-prefix "\n"
  360. :body-section-header-prefix ("== " "=== " "==== "
  361. "===== " "====== " "======= ")
  362. :body-section-header-suffix (" ==\n\n" " ===\n\n" " ====\n\n"
  363. " =====\n\n" " ======\n\n" " =======\n\n")
  364. :body-line-export-preformated t ;; yes/no/maybe???
  365. :body-line-format "%s\n"
  366. :body-line-wrap 75
  367. :body-line-fixed-format " %s\n"
  368. :body-list-format " * %s\n"
  369. :body-number-list-format " # %s\n"
  370. ;; :body-list-prefix "LISTSTART"
  371. ;; :body-list-suffix "LISTEND"
  372. ;; this is ignored! [2010/02/02:rpg]
  373. :body-bullet-list-prefix ("* " "** " "*** " "**** " "***** ")
  374. )
  375. ("tikiwiki"
  376. :file-suffix ".txt"
  377. :key-binding ?U
  378. ;; lifted from wikipedia exporter
  379. :header-prefix ""
  380. :header-suffix ""
  381. :title-format "-= %s =-\n"
  382. :date-export nil
  383. :toc-export nil
  384. :body-header-section-numbers nil
  385. :body-section-prefix "\n"
  386. :body-section-header-prefix ("! " "!! " "!!! " "!!!! "
  387. "!!!!! " "!!!!!! " "!!!!!!! ")
  388. :body-section-header-suffix (" \n" " \n" " \n"
  389. " \n" " \n" " \n")
  390. :body-line-export-preformated t ;; yes/no/maybe???
  391. :body-line-format "%s "
  392. :body-line-wrap nil
  393. :body-line-fixed-format " %s\n"
  394. :body-list-format "* %s\n"
  395. :body-number-list-format "# %s\n"
  396. ;; :body-list-prefix "LISTSTART"
  397. ;; :body-list-suffix "LISTEND"
  398. :blockquote-start "\n^\n"
  399. :blockquote-end "^\n\n"
  400. :body-newline-paragraph "\n"
  401. :bold-format "__%s__"
  402. :italic-format "''%s''"
  403. :underline-format "===%s==="
  404. :strikethrough-format "--%s--"
  405. :code-format "-+%s+-"
  406. :verbatim-format "~pp~%s~/pp~"
  407. )
  408. )
  409. "A assoc list of property lists to specify export definitions"
  410. )
  411. (setq org-generic-export-type "demo")
  412. (defvar org-export-generic-section-type "")
  413. (defvar org-export-generic-section-suffix "")
  414. ;;;###autoload
  415. (defun org-set-generic-type (type definition)
  416. "Adds a TYPE and DEFINITION to the existing list of defined generic
  417. export definitions."
  418. (aput 'org-generic-alist type definition))
  419. ;;; helper functions for org-set-generic-type
  420. (defvar org-export-generic-keywords nil)
  421. (defmacro* def-org-export-generic-keyword (keyword
  422. &key documentation
  423. type)
  424. "Define KEYWORD as a legitimate element for inclusion in
  425. the body of an org-set-generic-type definition."
  426. `(progn
  427. (pushnew ,keyword org-export-generic-keywords)
  428. ;; TODO: push the documentation and type information
  429. ;; somewhere where it will do us some good.
  430. ))
  431. (def-org-export-generic-keyword :body-newline-paragraph
  432. :documentation "Bound either to NIL or to a pattern to be
  433. inserted in the output for every blank line in the input.
  434. The intention is to handle formats where text is flowed, and
  435. newlines are interpreted as significant \(e.g., as indicating
  436. preformatted text\). A common non-nil value for this keyword
  437. is \"\\n\". Should typically be combined with a value for
  438. :body-line-format that does NOT end with a newline."
  439. :type string)
  440. ;;; fontification keywords
  441. (def-org-export-generic-keyword :bold-format)
  442. (def-org-export-generic-keyword :italic-format)
  443. (def-org-export-generic-keyword :underline-format)
  444. (def-org-export-generic-keyword :strikethrough-format)
  445. (def-org-export-generic-keyword :code-format)
  446. (def-org-export-generic-keyword :verbatim-format)
  447. (defun org-export-generic-remember-section (type suffix &optional prefix)
  448. (setq org-export-generic-section-type type)
  449. (setq org-export-generic-section-suffix suffix)
  450. (if prefix
  451. (insert prefix))
  452. )
  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. (bodynewline-paragraph (plist-get export-plist :body-newline-paragraph))
  633. (bodytextpre (plist-get export-plist :body-text-prefix))
  634. (bodytextsuf (plist-get export-plist :body-text-suffix))
  635. (bodylinewrap (plist-get export-plist :body-line-wrap))
  636. (bodylineform (or (plist-get export-plist :body-line-format) "%s"))
  637. (blockquotestart (or (plist-get export-plist :blockquote-start) "\n\n\t"))
  638. (blockquoteend (or (plist-get export-plist :blockquote-end) "\n\n"))
  639. ;; dynamic variables used heinously in fontification
  640. ;; not referenced locally...
  641. (format-boldify (plist-get export-plist :bold-format))
  642. (format-italicize (plist-get export-plist :italic-format))
  643. (format-underline (plist-get export-plist :underline-format))
  644. (format-strikethrough (plist-get export-plist :strikethrough-format))
  645. (format-code (plist-get export-plist :code-format))
  646. (format-verbatim (plist-get export-plist :verbatim-format))
  647. thetoc toctags have-headings first-heading-pos
  648. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  649. (let ((inhibit-read-only t))
  650. (org-unmodified
  651. (remove-text-properties (point-min) (point-max)
  652. '(:org-license-to-kill t))))
  653. (setq org-min-level (org-get-min-level lines level-offset))
  654. (setq org-last-level org-min-level)
  655. (org-init-section-numbers)
  656. (find-file-noselect filename)
  657. (setq lang-words (or (assoc language org-export-language-setup)
  658. (assoc "en" org-export-language-setup)))
  659. (switch-to-buffer-other-window buffer)
  660. (erase-buffer)
  661. (fundamental-mode)
  662. ;; create local variables for all options, to make sure all called
  663. ;; functions get the correct information
  664. (mapc (lambda (x)
  665. (set (make-local-variable (nth 2 x))
  666. (plist-get opt-plist (car x))))
  667. org-export-plist-vars)
  668. (org-set-local 'org-odd-levels-only odd)
  669. (setq umax (if arg (prefix-numeric-value arg)
  670. org-export-headline-levels))
  671. (setq umax-toc umax)
  672. ;; File header
  673. (if title
  674. (insert
  675. (org-export-generic-header title export-plist
  676. :title-prefix
  677. :title-format
  678. :title-suffix)))
  679. (if (and (or author email)
  680. (plist-get export-plist :author-export))
  681. (insert (concat (nth 1 lang-words) ": " (or author "")
  682. (if email (concat " <" email ">") "")
  683. "\n")))
  684. (cond
  685. ((and date (string-match "%" date))
  686. (setq date (format-time-string date)))
  687. (date)
  688. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  689. (if (and date (plist-get export-plist :date-export))
  690. (insert
  691. (org-export-generic-header date export-plist
  692. :date-prefix
  693. :date-format
  694. :date-suffix)))
  695. ;; export the table of contents first
  696. (if (plist-get export-plist :toc-export)
  697. (progn
  698. (push
  699. (org-export-generic-header (nth 3 lang-words) export-plist
  700. :toc-header-prefix
  701. :toc-header-format
  702. :toc-header-suffix)
  703. thetoc)
  704. (if tocprefix
  705. (push tocprefix thetoc))
  706. (mapc '(lambda (line)
  707. (if (string-match org-todo-line-regexp line)
  708. ;; This is a headline
  709. (progn
  710. (setq have-headings t)
  711. (setq level (- (match-end 1) (match-beginning 1)
  712. level-offset)
  713. level (org-tr-level level)
  714. txt (match-string 3 line)
  715. todo
  716. (or (and org-export-mark-todo-in-toc
  717. (match-beginning 2)
  718. (not (member (match-string 2 line)
  719. org-done-keywords)))
  720. ; TODO, not DONE
  721. (and org-export-mark-todo-in-toc
  722. (= level umax-toc)
  723. (org-search-todo-below
  724. line lines level))))
  725. (setq txt (org-html-expand-for-generic txt))
  726. (while (string-match org-bracket-link-regexp txt)
  727. (setq txt
  728. (replace-match
  729. (match-string (if (match-end 2) 3 1) txt)
  730. t t txt)))
  731. (if (and (not tagsintoc)
  732. (string-match
  733. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  734. txt))
  735. (setq txt (replace-match "" t t txt))
  736. ; include tags but formated
  737. (if (string-match
  738. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$")
  739. txt)
  740. (progn
  741. (setq
  742. toctags
  743. (org-export-generic-header
  744. (match-string 1 txt)
  745. export-plist :toc-tags-prefix
  746. :toc-tags-format :toc-tags-suffix))
  747. (string-match
  748. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  749. txt)
  750. (setq txt (replace-match "" t t txt)))
  751. (setq toctags tocnotagsstr)))
  752. (if (string-match quote-re0 txt)
  753. (setq txt (replace-match "" t t txt)))
  754. (if (<= level umax-toc)
  755. (progn
  756. (push
  757. (concat
  758. (make-string
  759. (* (max 0 (- level org-min-level)) tocdepth)
  760. tocindentchar)
  761. (if tocsecnums
  762. (format tocsecnumform
  763. (org-section-number level))
  764. "")
  765. (format
  766. (if todo tocformtodo tocformat)
  767. txt)
  768. toctags)
  769. thetoc)
  770. (setq org-last-level level))
  771. ))))
  772. lines)
  773. (if tocsuffix
  774. (push tocsuffix thetoc))
  775. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  776. (org-init-section-numbers)
  777. (org-export-generic-check-section "top")
  778. (while (setq line (pop lines))
  779. (when (and link-buffer (string-match org-outline-regexp-bol line))
  780. (org-export-generic-push-links (nreverse link-buffer))
  781. (setq link-buffer nil))
  782. (setq wrap nil)
  783. ;; Remove the quoted HTML tags.
  784. ;; XXX
  785. (setq line (org-html-expand-for-generic line))
  786. ;; Replace links with the description when possible
  787. ;; XXX
  788. (while (string-match org-bracket-link-regexp line)
  789. (setq link (match-string 1 line)
  790. desc0 (match-string 3 line)
  791. desc (or desc0 (match-string 1 line)))
  792. (if (and (> (length link) 8)
  793. (equal (substring link 0 8) "coderef:"))
  794. (setq line (replace-match
  795. (format (org-export-get-coderef-format (substring link 8) desc)
  796. (cdr (assoc
  797. (substring link 8)
  798. org-export-code-refs)))
  799. t t line))
  800. (setq rpl (concat "["
  801. (or (match-string 3 line) (match-string 1 line))
  802. "]"))
  803. (when (and desc0 (not (equal desc0 link)))
  804. (if org-export-generic-links-to-notes
  805. (push (cons desc0 link) link-buffer)
  806. (setq rpl (concat rpl " (" link ")")
  807. wrap (+ (length line) (- (length (match-string 0 line)))
  808. (length desc)))))
  809. (setq line (replace-match rpl t t line))))
  810. (when custom-times
  811. (setq line (org-translate-time line)))
  812. (cond
  813. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  814. ;;
  815. ;; a Headline
  816. ;;
  817. (org-export-generic-check-section "headline")
  818. (setq first-heading-pos (or first-heading-pos (point)))
  819. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  820. level-offset))
  821. txt (match-string 2 line))
  822. (org-generic-level-start level old-level txt umax export-plist lines)
  823. (setq old-level level))
  824. ((and org-export-with-tables
  825. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  826. ;;
  827. ;; a Table
  828. ;;
  829. (org-export-generic-check-section "table")
  830. (if (not table-open)
  831. ;; New table starts
  832. (setq table-open t table-buffer nil))
  833. ;; Accumulate table lines
  834. (setq table-buffer (cons line table-buffer))
  835. (when (or (not lines)
  836. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  837. (car lines))))
  838. (setq table-open nil
  839. table-buffer (nreverse table-buffer))
  840. (insert (mapconcat
  841. (lambda (x)
  842. (org-fix-indentation x org-generic-current-indentation))
  843. (org-format-table-generic table-buffer)
  844. "\n") "\n")))
  845. ((string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line)
  846. ;;
  847. ;; pre-formatted text
  848. ;;
  849. (setq line (replace-match "\\1" nil nil line))
  850. (org-export-generic-check-section "preformat" bodyfixedpre bodyfixedsuf)
  851. (insert (format bodyfixedform line)))
  852. ((or (string-match "^\\([ \t]*\\)\\([\-\+][ \t]*\\)" line)
  853. ;; if the bullet list item is an asterisk, the leading space is /mandatory/
  854. ;; [2010/02/02:rpg]
  855. (string-match "^\\([ \t]+\\)\\(\\*[ \t]*\\)" line))
  856. ;;
  857. ;; plain list item
  858. ;; TODO: nested lists
  859. ;;
  860. ;; first add a line break between any previous paragraph or line item and this
  861. ;; one
  862. (when bodynewline-paragraph
  863. (insert bodynewline-paragraph))
  864. ;; I believe this gets rid of leading whitespace.
  865. (setq line (replace-match "" nil nil line))
  866. ;; won't this insert the suffix /before/ the last line of the list?
  867. ;; also isn't it spoofed by bulleted lists that have a line skip between the list items
  868. ;; unless 'org-empty-line-terminates-plain-lists' is true?
  869. (org-export-generic-check-section "liststart" listprefix listsuffix)
  870. ;; deal with checkboxes
  871. (cond
  872. ((string-match "^\\(\\[ \\]\\)[ \t]*" line)
  873. (setq line (concat (replace-match listchecktodo nil nil line)
  874. listchecktodoend)))
  875. ((string-match "^\\(\\[X\\]\\)[ \t]*" line)
  876. (setq line (concat (replace-match listcheckdone nil nil line)
  877. listcheckdoneend)))
  878. ((string-match "^\\(\\[/\\]\\)[ \t]*" line)
  879. (setq line (concat (replace-match listcheckhalf nil nil line)
  880. listcheckhalfend)))
  881. )
  882. (insert (format listformat (org-export-generic-fontify line))))
  883. ((string-match "^\\([ \t]+\\)\\([0-9]+\\.[ \t]*\\)" line)
  884. ;;
  885. ;; numbered list item
  886. ;;
  887. ;; TODO: nested lists
  888. ;;
  889. (setq line (replace-match (if numlistleavenum "\\2" "") nil nil line))
  890. (org-export-generic-check-section "numliststart"
  891. numlistprefix numlistsuffix)
  892. ;; deal with checkboxes
  893. ;; TODO: whoops; leaving the numbers is a problem for ^ matching
  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. )
  905. (insert (format numlistformat (org-export-generic-fontify line))))
  906. ((equal line "ORG-BLOCKQUOTE-START")
  907. (setq line blockquotestart))
  908. ((equal line "ORG-BLOCKQUOTE-END")
  909. (setq line blockquoteend))
  910. ((string-match "^\\s-*$" line)
  911. ;; blank line
  912. (if bodynewline-paragraph
  913. (insert bodynewline-paragraph)))
  914. (t
  915. ;;
  916. ;; body
  917. ;;
  918. (org-export-generic-check-section "body" bodytextpre bodytextsuf)
  919. (setq line
  920. (org-export-generic-fontify line))
  921. ;; XXX: properties? list?
  922. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  923. (setq line (replace-match "\\1\\3:" t nil line)))
  924. (setq line (org-fix-indentation line org-generic-current-indentation))
  925. ;; Remove forced line breaks
  926. (if (string-match "\\\\\\\\[ \t]*$" line)
  927. (setq line (replace-match "" t t line)))
  928. (if bodylinewrap
  929. ;; XXX: was dependent on wrap var which was calculated by???
  930. (if (> (length line) bodylinewrap)
  931. (setq line
  932. (org-export-generic-wrap line bodylinewrap))
  933. (setq line line)))
  934. (insert (format bodylineform line)))))
  935. ;; if we're at a level > 0; insert the closing body level stuff
  936. (let ((counter 0))
  937. (while (> (- level counter) 0)
  938. (insert
  939. (org-export-generic-format export-plist :body-section-suffix 0
  940. (- level counter)))
  941. (setq counter (1+ counter))))
  942. (org-export-generic-check-section "bottom")
  943. (org-export-generic-push-links (nreverse link-buffer))
  944. (normal-mode)
  945. ;; insert the table of contents
  946. (when thetoc
  947. (goto-char (point-min))
  948. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  949. (progn
  950. (goto-char (match-beginning 0))
  951. (replace-match ""))
  952. (goto-char first-heading-pos))
  953. (mapc 'insert thetoc)
  954. (or (looking-at "[ \t]*\n[ \t]*\n")
  955. (insert "\n\n")))
  956. ;; Convert whitespace place holders
  957. (goto-char (point-min))
  958. (let (beg end)
  959. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  960. (setq end (next-single-property-change beg 'org-whitespace))
  961. (goto-char beg)
  962. (delete-region beg end)
  963. (insert (make-string (- end beg) ?\ ))))
  964. (save-buffer)
  965. ;; remove display and invisible chars
  966. (let (beg end)
  967. (goto-char (point-min))
  968. (while (setq beg (next-single-property-change (point) 'display))
  969. (setq end (next-single-property-change beg 'display))
  970. (delete-region beg end)
  971. (goto-char beg)
  972. (insert "=>"))
  973. (goto-char (point-min))
  974. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  975. (setq end (next-single-property-change beg 'org-cwidth))
  976. (delete-region beg end)
  977. (goto-char beg)))
  978. (goto-char (point-min))))
  979. (defun org-export-generic-format (export-plist prop &optional len n reverse)
  980. "converts a property specification to a string given types of properties
  981. The EXPORT-PLIST should be defined as the lookup plist.
  982. The PROP should be the property name to search for in it.
  983. LEN is set to the length of multi-characters strings to generate (or 0)
  984. N is the tree depth
  985. REVERSE means to reverse the list if the plist match is a list
  986. "
  987. (let* ((prefixtype (plist-get export-plist prop))
  988. subtype)
  989. (cond
  990. ((null prefixtype) "")
  991. ((and len (char-or-string-p prefixtype) (not (stringp prefixtype)))
  992. ;; sequence of chars
  993. (concat (make-string len prefixtype) "\n"))
  994. ((stringp prefixtype)
  995. prefixtype)
  996. ((and n (listp prefixtype))
  997. (if reverse
  998. (setq prefixtype (reverse prefixtype)))
  999. (setq subtype (if (> n (length prefixtype))
  1000. (car (last prefixtype))
  1001. (nth (1- n) prefixtype)))
  1002. (if (stringp subtype)
  1003. subtype
  1004. (concat (make-string len subtype) "\n")))
  1005. (t ""))
  1006. ))
  1007. (defun org-export-generic-header (header export-plist
  1008. prefixprop formatprop postfixprop
  1009. &optional n reverse)
  1010. "convert a header to an output string given formatting property names"
  1011. (let* ((formatspec (plist-get export-plist formatprop))
  1012. (len (length header)))
  1013. (concat
  1014. (org-export-generic-format export-plist prefixprop len n reverse)
  1015. (format (or formatspec "%s") header)
  1016. (org-export-generic-format export-plist postfixprop len n reverse))
  1017. ))
  1018. (defun org-export-generic-preprocess (parameters)
  1019. "Do extra work for ASCII export"
  1020. ;; Put quotes around verbatim text
  1021. (goto-char (point-min))
  1022. (while (re-search-forward org-verbatim-re nil t)
  1023. (goto-char (match-end 2))
  1024. (backward-delete-char 1) (insert "'")
  1025. (goto-char (match-beginning 2))
  1026. (delete-char 1) (insert "`")
  1027. (goto-char (match-end 2)))
  1028. ;; Remove target markers
  1029. (goto-char (point-min))
  1030. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  1031. (replace-match "\\1\\2")))
  1032. (defun org-html-expand-for-generic (line)
  1033. "Handle quoted HTML for ASCII export."
  1034. (if org-export-html-expand
  1035. (while (string-match "@<[^<>\n]*>" line)
  1036. ;; We just remove the tags for now.
  1037. (setq line (replace-match "" nil nil line))))
  1038. line)
  1039. (defun org-export-generic-wrap (line where)
  1040. "Wrap LINE at or before WHERE."
  1041. (let* ((ind (org-get-indentation line))
  1042. (indstr (make-string ind ?\ ))
  1043. (len (length line))
  1044. (result "")
  1045. pos didfirst)
  1046. (while (> len where)
  1047. (catch 'found
  1048. (loop for i from where downto (/ where 2) do
  1049. (and (equal (aref line i) ?\ )
  1050. (setq pos i)
  1051. (throw 'found t))))
  1052. (if pos
  1053. (progn
  1054. (setq result
  1055. (concat result
  1056. (if didfirst indstr "")
  1057. (substring line 0 pos)
  1058. "\n"))
  1059. (setq didfirst t)
  1060. (setq line (substring line (1+ pos)))
  1061. (setq len (length line)))
  1062. (setq result (concat result line))
  1063. (setq len 0)))
  1064. (concat result indstr line)))
  1065. (defun org-export-generic-push-links (link-buffer)
  1066. "Push out links in the buffer."
  1067. (when link-buffer
  1068. ;; We still have links to push out.
  1069. (insert "\n")
  1070. (let ((ind ""))
  1071. (save-match-data
  1072. (if (save-excursion
  1073. (re-search-backward
  1074. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  1075. (setq ind (or (match-string 2)
  1076. (make-string (length (match-string 3)) ?\ )))))
  1077. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  1078. link-buffer))
  1079. (insert "\n")))
  1080. (defun org-generic-level-start (level old-level title umax export-plist
  1081. &optional lines)
  1082. "Insert a new level in a generic export."
  1083. (let ((n (- level umax 1))
  1084. (ind 0)
  1085. (diff (- level old-level)) (counter 0)
  1086. (secnums (plist-get export-plist :body-header-section-numbers))
  1087. (secnumformat
  1088. (plist-get export-plist :body-header-section-number-format))
  1089. char tagstring)
  1090. (unless org-export-with-tags
  1091. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  1092. (setq title (replace-match "" t t title))))
  1093. (cond
  1094. ;; going deeper
  1095. ((> level old-level)
  1096. (while (< (+ old-level counter) (1- level))
  1097. (insert
  1098. (org-export-generic-format export-plist :body-section-prefix 0
  1099. (+ old-level counter)))
  1100. (setq counter (1+ counter))
  1101. ))
  1102. ;; going up
  1103. ((< level old-level)
  1104. (while (> (- old-level counter) (1- level))
  1105. (insert
  1106. (org-export-generic-format export-plist :body-section-suffix 0
  1107. (- old-level counter)))
  1108. (setq counter (1+ counter))
  1109. ))
  1110. ;; same level
  1111. ((= level old-level)
  1112. (insert
  1113. (org-export-generic-format export-plist :body-section-suffix 0 level))
  1114. )
  1115. )
  1116. (insert
  1117. (org-export-generic-format export-plist :body-section-prefix 0 level))
  1118. (if (and org-export-with-section-numbers
  1119. secnums
  1120. (or (not (numberp secnums))
  1121. (< level secnums)))
  1122. (setq title
  1123. (concat (format (or secnumformat "%s ")
  1124. (org-section-number level)) title)))
  1125. ;; handle tags and formatting
  1126. (if (string-match
  1127. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") title)
  1128. (progn
  1129. (if (plist-get export-plist :body-tags-export)
  1130. (setq tagstring (org-export-generic-header (match-string 1 title)
  1131. export-plist
  1132. :body-tags-prefix
  1133. :body-tags-format
  1134. :body-tags-suffix)))
  1135. (string-match (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$") title)
  1136. (setq title (replace-match "" t t title)))
  1137. (setq tagstring (plist-get export-plist :body-tags-none-string)))
  1138. (insert
  1139. (org-export-generic-header title export-plist
  1140. :body-section-header-prefix
  1141. :body-section-header-format
  1142. :body-section-header-suffix
  1143. level))
  1144. (if tagstring
  1145. (insert tagstring))
  1146. (setq org-generic-current-indentation '(0 . 0))))
  1147. (defun org-insert-centered (s &optional underline)
  1148. "Insert the string S centered and underline it with character UNDERLINE."
  1149. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  1150. (insert (make-string ind ?\ ) s "\n")
  1151. (if underline
  1152. (insert (make-string ind ?\ )
  1153. (make-string (string-width s) underline)
  1154. "\n"))))
  1155. (defvar org-table-colgroup-info nil)
  1156. (defun org-format-table-generic (lines)
  1157. "Format a table for ascii export."
  1158. (if (stringp lines)
  1159. (setq lines (org-split-string lines "\n")))
  1160. (if (not (string-match "^[ \t]*|" (car lines)))
  1161. ;; Table made by table.el - test for spanning
  1162. lines
  1163. ;; A normal org table
  1164. ;; Get rid of hlines at beginning and end
  1165. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1166. (setq lines (nreverse lines))
  1167. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1168. (setq lines (nreverse lines))
  1169. (when org-export-table-remove-special-lines
  1170. ;; Check if the table has a marking column. If yes remove the
  1171. ;; column and the special lines
  1172. (setq lines (org-table-clean-before-export lines)))
  1173. ;; Get rid of the vertical lines except for grouping
  1174. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  1175. rtn line vl1 start)
  1176. (while (setq line (pop lines))
  1177. (if (string-match org-table-hline-regexp line)
  1178. (and (string-match "|\\(.*\\)|" line)
  1179. (setq line (replace-match " \\1" t nil line)))
  1180. (setq start 0 vl1 vl)
  1181. (while (string-match "|" line start)
  1182. (setq start (match-end 0))
  1183. (or (pop vl1) (setq line (replace-match " " t t line)))))
  1184. (push line rtn))
  1185. (nreverse rtn))))
  1186. (defun org-colgroup-info-to-vline-list (info)
  1187. (let (vl new last)
  1188. (while info
  1189. (setq last new new (pop info))
  1190. (if (or (memq last '(:end :startend))
  1191. (memq new '(:start :startend)))
  1192. (push t vl)
  1193. (push nil vl)))
  1194. (setq vl (nreverse vl))
  1195. (and vl (setcar vl nil))
  1196. vl))
  1197. ;;; FIXME: this should probably turn into a defconstant later [2010/05/20:rpg]
  1198. (defvar org-export-generic-emphasis-alist
  1199. '(("*" format-boldify nil)
  1200. ("/" format-italicize nil)
  1201. ("_" format-underline nil)
  1202. ("+" format-strikethrough nil)
  1203. ("=" format-code t)
  1204. ("~" format-verbatim t))
  1205. "Alist of org format -> formatting variables for fontification.
  1206. Each element of the list is a list of three elements.
  1207. The first element is the character used as a marker for fontification.
  1208. The second element is a variable name, set in org-export-generic. That
  1209. variable will be dereferenced to obtain a formatting string to wrap
  1210. fontified text with.
  1211. The third element decides whether to protect converted text from other
  1212. conversions.")
  1213. ;;; Cargo-culted from the latex translation. I couldn't figure out how
  1214. ;;; to keep the structure since the generic export operates on lines, rather
  1215. ;;; than on a buffer as in the latex export, meaning that none of the
  1216. ;;; search forward code could be kept. This led me to rewrite the
  1217. ;;; whole thing recursively. A huge lose for efficiency (potentially),
  1218. ;;; but I couldn't figure out how to make the looping work.
  1219. ;;; Worse, it's /doubly/ recursive, because this function calls
  1220. ;;; org-export-generic-emph-format, which can call it recursively...
  1221. ;;; [2010/05/20:rpg]
  1222. (defun org-export-generic-fontify (string)
  1223. "Convert fontification according to generic rules."
  1224. (if (string-match org-emph-re string)
  1225. ;; The match goes one char after the *string*, except at the end of a line
  1226. (let ((emph (assoc (match-string 3 string)
  1227. org-export-generic-emphasis-alist))
  1228. (beg (match-beginning 0))
  1229. (end (match-end 0)))
  1230. (unless emph
  1231. (message "`org-export-generic-emphasis-alist' has no entry for formatting triggered by \"%s\""
  1232. (match-string 3 string)))
  1233. ;; now we need to determine whether we have strikethrough or
  1234. ;; a list, which is a bit nasty
  1235. (if (and (equal (match-string 3 string) "+")
  1236. (save-match-data
  1237. (string-match "\\`-+\\'" (match-string 4 string))))
  1238. ;; a list --- skip this match and recurse on the point after the
  1239. ;; first emph char...
  1240. (concat (substring string 0 (1+ (match-beginning 3)))
  1241. (org-export-generic-fontify (substring string (match-beginning 3))))
  1242. (concat (substring string 0 beg) ;; part before the match
  1243. (match-string 1 string)
  1244. (org-export-generic-emph-format (second emph)
  1245. (match-string 4 string)
  1246. (third emph))
  1247. (or (match-string 5 string) "")
  1248. (org-export-generic-fontify (substring string end)))))
  1249. string))
  1250. (defun org-export-generic-emph-format (format-varname string protect)
  1251. "Return a string that results from applying the markup indicated by
  1252. FORMAT-VARNAME to STRING."
  1253. (let ((format (symbol-value format-varname)))
  1254. (let ((string-to-emphasize
  1255. (if protect
  1256. string
  1257. (org-export-generic-fontify string))))
  1258. (if format
  1259. (format format string-to-emphasize)
  1260. string-to-emphasize))))
  1261. (provide 'org-generic)
  1262. (provide 'org-export-generic)
  1263. ;;; org-export-generic.el ends here