org-export-generic.el 50 KB

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