org-export-generic.el 47 KB

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