org-export-generic.el 47 KB

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