org-export.el 129 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. ;;; org-export.el --- Generic Export Engine For Org
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a generic export engine for Org, built on
  17. ;; its syntactical parser: Org Elements.
  18. ;; Besides that parser, the generic exporter is made of three distinct
  19. ;; parts:
  20. ;; - The communication channel consists in a property list, which is
  21. ;; created and updated during the process. Its use is to offer
  22. ;; every piece of information, would it be about initial environment
  23. ;; or contextual data, all in a single place. The exhaustive list
  24. ;; of properties is given in "The Communication Channel" section of
  25. ;; this file.
  26. ;; - The transcoder walks the parse tree, ignores or treat as plain
  27. ;; text elements and objects according to export options, and
  28. ;; eventually calls back-end specific functions to do the real
  29. ;; transcoding, concatenating their return value along the way.
  30. ;; - The filter system is activated at the very beginning and the very
  31. ;; end of the export process, and each time an element or an object
  32. ;; has been converted. It is the entry point to fine-tune standard
  33. ;; output from back-end transcoders.
  34. ;; The core function is `org-export-as'. It returns the transcoded
  35. ;; buffer as a string.
  36. ;; In order to derive an exporter out of this generic implementation,
  37. ;; one can define a transcode function for each element or object.
  38. ;; Such function should return a string for the corresponding element,
  39. ;; without any trailing space, or nil. It must accept three
  40. ;; arguments:
  41. ;; 1. the element or object itself,
  42. ;; 2. its contents, or nil when it isn't recursive,
  43. ;; 3. the property list used as a communication channel.
  44. ;; If no such function is found, that element or object type will
  45. ;; simply be ignored, along with any separating blank line. The same
  46. ;; will happen if the function returns the nil value. If that
  47. ;; function returns the empty string, the type will be ignored, but
  48. ;; the blank lines will be kept.
  49. ;; Contents, when not nil, are stripped from any global indentation
  50. ;; (although the relative one is preserved). They also always end
  51. ;; with a single newline character.
  52. ;; These functions must follow a strict naming convention:
  53. ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
  54. ;; export back-end and TYPE the type of the element or object handled.
  55. ;; Moreover, two additional functions can be defined. On the one
  56. ;; hand, `org-BACKEND-template' returns the final transcoded string,
  57. ;; and can be used to add a preamble and a postamble to document's
  58. ;; body. It must accept two arguments: the transcoded string and the
  59. ;; property list containing export options. On the other hand,
  60. ;; `org-BACKEND-plain-text', when defined, is to be called on every
  61. ;; text not recognized as an element or an object. It must accept two
  62. ;; arguments: the text string and the information channel.
  63. ;; Any back-end can define its own variables. Among them, those
  64. ;; customizables should belong to the `org-export-BACKEND' group.
  65. ;; Also, a special variable, `org-BACKEND-option-alist', allows to
  66. ;; define buffer keywords and "#+options:" items specific to that
  67. ;; back-end. See `org-export-option-alist' for supported defaults and
  68. ;; syntax.
  69. ;; Tools for common tasks across back-ends are implemented in the
  70. ;; penultimate part of this file. A dispatcher for standard back-ends
  71. ;; is provided in the last one.
  72. ;;; Code:
  73. (eval-when-compile (require 'cl))
  74. (require 'org-element)
  75. ;; Require major back-ends and publishing tools
  76. (require 'org-e-ascii "../../EXPERIMENTAL/org-e-ascii.el")
  77. (require 'org-e-html "../../EXPERIMENTAL/org-e-html.el")
  78. (require 'org-e-latex "../../EXPERIMENTAL/org-e-latex.el")
  79. (require 'org-e-odt "../../EXPERIMENTAL/org-e-odt.el")
  80. (require 'org-e-publish "../../EXPERIMENTAL/org-e-publish.el")
  81. ;;; Internal Variables
  82. ;; Among internal variables, the most important is
  83. ;; `org-export-option-alist'. This variable define the global export
  84. ;; options, shared between every exporter, and how they are acquired.
  85. (defconst org-export-max-depth 19
  86. "Maximum nesting depth for headlines, counting from 0.")
  87. (defconst org-export-option-alist
  88. '((:author "AUTHOR" nil user-full-name t)
  89. (:creator "CREATOR" nil org-export-creator-string)
  90. (:date "DATE" nil nil t)
  91. (:description "DESCRIPTION" nil nil newline)
  92. (:email "EMAIL" nil user-mail-address t)
  93. (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
  94. (:headline-levels nil "H" org-export-headline-levels)
  95. (:keywords "KEYWORDS" nil nil space)
  96. (:language "LANGUAGE" nil org-export-default-language t)
  97. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  98. (:section-numbers nil "num" org-export-with-section-numbers)
  99. (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
  100. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  101. (:title "TITLE" nil nil space)
  102. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  103. (:with-author nil "author" org-export-with-author)
  104. (:with-creator nil "creator" org-export-with-creator)
  105. (:with-drawers nil "d" org-export-with-drawers)
  106. (:with-email nil "email" org-export-with-email)
  107. (:with-emphasize nil "*" org-export-with-emphasize)
  108. (:with-entities nil "e" org-export-with-entities)
  109. (:with-fixed-width nil ":" org-export-with-fixed-width)
  110. (:with-footnotes nil "f" org-export-with-footnotes)
  111. (:with-priority nil "pri" org-export-with-priority)
  112. (:with-special-strings nil "-" org-export-with-special-strings)
  113. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  114. (:with-toc nil "toc" org-export-with-toc)
  115. (:with-tables nil "|" org-export-with-tables)
  116. (:with-tags nil "tags" org-export-with-tags)
  117. (:with-tasks nil "tasks" org-export-with-tasks)
  118. (:with-timestamps nil "<" org-export-with-timestamps)
  119. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  120. "Alist between export properties and ways to set them.
  121. The car of the alist is the property name, and the cdr is a list
  122. like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
  123. KEYWORD is a string representing a buffer keyword, or nil.
  124. OPTION is a string that could be found in an #+OPTIONS: line.
  125. DEFAULT is the default value for the property.
  126. BEHAVIOUR determine how Org should handle multiple keywords for
  127. the same property. It is a symbol among:
  128. nil Keep old value and discard the new one.
  129. t Replace old value with the new one.
  130. `space' Concatenate the values, separating them with a space.
  131. `newline' Concatenate the values, separating them with
  132. a newline.
  133. `split' Split values at white spaces, and cons them to the
  134. previous list.
  135. KEYWORD and OPTION have precedence over DEFAULT.
  136. All these properties should be back-end agnostic. For back-end
  137. specific properties, define a similar variable named
  138. `org-BACKEND-option-alist', replacing BACKEND with the name of
  139. the appropriate back-end. You can also redefine properties
  140. there, as they have precedence over these.")
  141. (defconst org-export-special-keywords
  142. '("SETUP_FILE" "OPTIONS" "MACRO")
  143. "List of in-buffer keywords that require special treatment.
  144. These keywords are not directly associated to a property. The
  145. way they are handled must be hard-coded into
  146. `org-export-get-inbuffer-options' function.")
  147. (defconst org-export-filters-alist
  148. '((:filter-babel-call . org-export-filter-babel-call-functions)
  149. (:filter-center-block . org-export-filter-center-block-functions)
  150. (:filter-comment . org-export-filter-comment-functions)
  151. (:filter-comment-block . org-export-filter-comment-block-functions)
  152. (:filter-drawer . org-export-filter-drawer-functions)
  153. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  154. (:filter-emphasis . org-export-filter-emphasis-functions)
  155. (:filter-entity . org-export-filter-entity-functions)
  156. (:filter-example-block . org-export-filter-example-block-functions)
  157. (:filter-export-block . org-export-filter-export-block-functions)
  158. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  159. (:filter-final-output . org-export-filter-final-output-functions)
  160. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  161. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  162. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  163. (:filter-headline . org-export-filter-headline-functions)
  164. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  165. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  166. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  167. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  168. (:filter-item . org-export-filter-item-functions)
  169. (:filter-keyword . org-export-filter-keyword-functions)
  170. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  171. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  172. (:filter-line-break . org-export-filter-line-break-functions)
  173. (:filter-link . org-export-filter-link-functions)
  174. (:filter-macro . org-export-filter-macro-functions)
  175. (:filter-paragraph . org-export-filter-paragraph-functions)
  176. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  177. (:filter-plain-list . org-export-filter-plain-list-functions)
  178. (:filter-plain-text . org-export-filter-plain-text-functions)
  179. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  180. (:filter-quote-block . org-export-filter-quote-block-functions)
  181. (:filter-quote-section . org-export-filter-quote-section-functions)
  182. (:filter-radio-target . org-export-filter-radio-target-functions)
  183. (:filter-section . org-export-filter-section-functions)
  184. (:filter-special-block . org-export-filter-special-block-functions)
  185. (:filter-src-block . org-export-filter-src-block-functions)
  186. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  187. (:filter-subscript . org-export-filter-subscript-functions)
  188. (:filter-superscript . org-export-filter-superscript-functions)
  189. (:filter-table . org-export-filter-table-functions)
  190. (:filter-target . org-export-filter-target-functions)
  191. (:filter-time-stamp . org-export-filter-time-stamp-functions)
  192. (:filter-verbatim . org-export-filter-verbatim-functions)
  193. (:filter-verse-block . org-export-filter-verse-block-functions))
  194. "Alist between filters properties and initial values.
  195. The key of each association is a property name accessible through
  196. the communication channel its value is a configurable global
  197. variable defining initial filters.
  198. This list is meant to install user specified filters. Back-end
  199. developers may install their own filters using
  200. `org-BACKEND-filters-alist', where BACKEND is the name of the
  201. considered back-end. Filters defined there will always be
  202. prepended to the current list, so they always get applied
  203. first.")
  204. (defconst org-export-default-inline-image-rule
  205. `(("file" .
  206. ,(format "\\.%s\\'"
  207. (regexp-opt
  208. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  209. "xpm" "pbm" "pgm" "ppm") t))))
  210. "Default rule for link matching an inline image.
  211. This rule applies to links with no description. By default, it
  212. will be considered as an inline image if it targets a local file
  213. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  214. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  215. See `org-export-inline-image-p' for more information about
  216. rules.")
  217. ;;; User-configurable Variables
  218. ;; Configuration for the masses.
  219. ;; They should never be evaled directly, as their value is to be
  220. ;; stored in a property list (cf. `org-export-option-alist').
  221. (defgroup org-export nil
  222. "Options for exporting Org mode files."
  223. :tag "Org Export"
  224. :group 'org)
  225. (defgroup org-export-general nil
  226. "General options for export engine."
  227. :tag "Org Export General"
  228. :group 'org-export)
  229. (defcustom org-export-with-archived-trees 'headline
  230. "Whether sub-trees with the ARCHIVE tag should be exported.
  231. This can have three different values:
  232. nil Do not export, pretend this tree is not present.
  233. t Do export the entire tree.
  234. `headline' Only export the headline, but skip the tree below it.
  235. This option can also be set with the #+OPTIONS line,
  236. e.g. \"arch:nil\"."
  237. :group 'org-export-general
  238. :type '(choice
  239. (const :tag "Not at all" nil)
  240. (const :tag "Headline only" 'headline)
  241. (const :tag "Entirely" t)))
  242. (defcustom org-export-with-author t
  243. "Non-nil means insert author name into the exported file.
  244. This option can also be set with the #+OPTIONS line,
  245. e.g. \"author:nil\"."
  246. :group 'org-export-general
  247. :type 'boolean)
  248. (defcustom org-export-with-creator 'comment
  249. "Non-nil means the postamble should contain a creator sentence.
  250. The sentence can be set in `org-export-creator-string' and
  251. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  252. If the value is `comment' insert it as a comment."
  253. :group 'org-export-general
  254. :type '(choice
  255. (const :tag "No creator sentence" nil)
  256. (const :tag "Sentence as a comment" 'comment)
  257. (const :tag "Insert the sentence" t)))
  258. (defcustom org-export-creator-string
  259. (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
  260. "String to insert at the end of the generated document."
  261. :group 'org-export-general
  262. :type '(string :tag "Creator string"))
  263. (defcustom org-export-with-drawers t
  264. "Non-nil means export contents of standard drawers.
  265. When t, all drawers are exported. This may also be a list of
  266. drawer names to export. This variable doesn't apply to
  267. properties drawers.
  268. This option can also be set with the #+OPTIONS line,
  269. e.g. \"d:nil\"."
  270. :group 'org-export-general
  271. :type '(choice
  272. (const :tag "All drawers" t)
  273. (const :tag "None" nil)
  274. (repeat :tag "Selected drawers"
  275. (string :tag "Drawer name"))))
  276. (defcustom org-export-with-email nil
  277. "Non-nil means insert author email into the exported file.
  278. This option can also be set with the #+OPTIONS line,
  279. e.g. \"email:t\"."
  280. :group 'org-export-general
  281. :type 'boolean)
  282. (defcustom org-export-with-emphasize t
  283. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  284. If the export target supports emphasizing text, the word will be
  285. typeset in bold, italic, or underlined, respectively. Not all
  286. export backends support this.
  287. This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
  288. :group 'org-export-general
  289. :type 'boolean)
  290. (defcustom org-export-exclude-tags '("noexport")
  291. "Tags that exclude a tree from export.
  292. All trees carrying any of these tags will be excluded from
  293. export. This is without condition, so even subtrees inside that
  294. carry one of the `org-export-select-tags' will be removed.
  295. This option can also be set with the #+EXPORT_EXCLUDE_TAGS:
  296. keyword."
  297. :group 'org-export-general
  298. :type '(repeat (string :tag "Tag")))
  299. (defcustom org-export-with-fixed-width t
  300. "Non-nil means lines starting with \":\" will be in fixed width font.
  301. This can be used to have pre-formatted text, fragments of code
  302. etc. For example:
  303. : ;; Some Lisp examples
  304. : (while (defc cnt)
  305. : (ding))
  306. will be looking just like this in also HTML. See also the QUOTE
  307. keyword. Not all export backends support this.
  308. This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
  309. :group 'org-export-translation
  310. :type 'boolean)
  311. (defcustom org-export-with-footnotes t
  312. "Non-nil means Org footnotes should be exported.
  313. This option can also be set with the #+OPTIONS line,
  314. e.g. \"f:nil\"."
  315. :group 'org-export-general
  316. :type 'boolean)
  317. (defcustom org-export-headline-levels 3
  318. "The last level which is still exported as a headline.
  319. Inferior levels will produce itemize lists when exported. Note
  320. that a numeric prefix argument to an exporter function overrides
  321. this setting.
  322. This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
  323. :group 'org-export-general
  324. :type 'integer)
  325. (defcustom org-export-default-language "en"
  326. "The default language for export and clocktable translations, as a string.
  327. This may have an association in
  328. `org-clock-clocktable-language-setup'."
  329. :group 'org-export-general
  330. :type '(string :tag "Language"))
  331. (defcustom org-export-preserve-breaks nil
  332. "Non-nil means preserve all line breaks when exporting.
  333. Normally, in HTML output paragraphs will be reformatted.
  334. This option can also be set with the #+OPTIONS line,
  335. e.g. \"\\n:t\"."
  336. :group 'org-export-general
  337. :type 'boolean)
  338. (defcustom org-export-with-entities t
  339. "Non-nil means interpret entities when exporting.
  340. For example, HTML export converts \\alpha to &alpha; and \\AA to
  341. &Aring;.
  342. For a list of supported names, see the constant `org-entities'
  343. and the user option `org-entities-user'.
  344. This option can also be set with the #+OPTIONS line,
  345. e.g. \"e:nil\"."
  346. :group 'org-export-general
  347. :type 'boolean)
  348. (defcustom org-export-with-priority nil
  349. "Non-nil means include priority cookies in export.
  350. When nil, remove priority cookies for export.
  351. This option can also be set with the #+OPTIONS line,
  352. e.g. \"pri:t\"."
  353. :group 'org-export-general
  354. :type 'boolean)
  355. (defcustom org-export-with-section-numbers t
  356. "Non-nil means add section numbers to headlines when exporting.
  357. When set to an integer n, numbering will only happen for
  358. headlines whose relative level is higher or equal to n.
  359. This option can also be set with the #+OPTIONS line,
  360. e.g. \"num:t\"."
  361. :group 'org-export-general
  362. :type 'boolean)
  363. (defcustom org-export-select-tags '("export")
  364. "Tags that select a tree for export.
  365. If any such tag is found in a buffer, all trees that do not carry
  366. one of these tags will be ignored during export. Inside trees
  367. that are selected like this, you can still deselect a subtree by
  368. tagging it with one of the `org-export-exclude-tags'.
  369. This option can also be set with the #+EXPORT_SELECT_TAGS:
  370. keyword."
  371. :group 'org-export-general
  372. :type '(repeat (string :tag "Tag")))
  373. (defcustom org-export-with-special-strings t
  374. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  375. When this option is turned on, these strings will be exported as:
  376. Org HTML LaTeX
  377. -----+----------+--------
  378. \\- &shy; \\-
  379. -- &ndash; --
  380. --- &mdash; ---
  381. ... &hellip; \ldots
  382. This option can also be set with the #+OPTIONS line,
  383. e.g. \"-:nil\"."
  384. :group 'org-export-general
  385. :type 'boolean)
  386. (defcustom org-export-with-sub-superscripts t
  387. "Non-nil means interpret \"_\" and \"^\" for export.
  388. When this option is turned on, you can use TeX-like syntax for
  389. sub- and superscripts. Several characters after \"_\" or \"^\"
  390. will be considered as a single item - so grouping with {} is
  391. normally not needed. For example, the following things will be
  392. parsed as single sub- or superscripts.
  393. 10^24 or 10^tau several digits will be considered 1 item.
  394. 10^-12 or 10^-tau a leading sign with digits or a word
  395. x^2-y^3 will be read as x^2 - y^3, because items are
  396. terminated by almost any nonword/nondigit char.
  397. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  398. Still, ambiguity is possible - so when in doubt use {} to enclose
  399. the sub/superscript. If you set this variable to the symbol
  400. `{}', the braces are *required* in order to trigger
  401. interpretations as sub/superscript. This can be helpful in
  402. documents that need \"_\" frequently in plain text.
  403. This option can also be set with the #+OPTIONS line,
  404. e.g. \"^:nil\"."
  405. :group 'org-export-general
  406. :type '(choice
  407. (const :tag "Interpret them" t)
  408. (const :tag "Curly brackets only" {})
  409. (const :tag "Do not interpret them" nil)))
  410. (defcustom org-export-with-toc t
  411. "Non-nil means create a table of contents in exported files.
  412. The TOC contains headlines with levels up
  413. to`org-export-headline-levels'. When an integer, include levels
  414. up to N in the toc, this may then be different from
  415. `org-export-headline-levels', but it will not be allowed to be
  416. larger than the number of headline levels. When nil, no table of
  417. contents is made.
  418. This option can also be set with the #+OPTIONS line,
  419. e.g. \"toc:nil\" or \"toc:3\"."
  420. :group 'org-export-general
  421. :type '(choice
  422. (const :tag "No Table of Contents" nil)
  423. (const :tag "Full Table of Contents" t)
  424. (integer :tag "TOC to level")))
  425. (defcustom org-export-with-tables t
  426. "If non-nil, lines starting with \"|\" define a table.
  427. For example:
  428. | Name | Address | Birthday |
  429. |-------------+----------+-----------|
  430. | Arthur Dent | England | 29.2.2100 |
  431. This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
  432. :group 'org-export-general
  433. :type 'boolean)
  434. (defcustom org-export-with-tags t
  435. "If nil, do not export tags, just remove them from headlines.
  436. If this is the symbol `not-in-toc', tags will be removed from
  437. table of contents entries, but still be shown in the headlines of
  438. the document.
  439. This option can also be set with the #+OPTIONS line,
  440. e.g. \"tags:nil\"."
  441. :group 'org-export-general
  442. :type '(choice
  443. (const :tag "Off" nil)
  444. (const :tag "Not in TOC" not-in-toc)
  445. (const :tag "On" t)))
  446. (defcustom org-export-with-tasks t
  447. "Non-nil means include TODO items for export.
  448. This may have the following values:
  449. t include tasks independent of state.
  450. todo include only tasks that are not yet done.
  451. done include only tasks that are already done.
  452. nil remove all tasks before export
  453. list of keywords keep only tasks with these keywords"
  454. :group 'org-export-general
  455. :type '(choice
  456. (const :tag "All tasks" t)
  457. (const :tag "No tasks" nil)
  458. (const :tag "Not-done tasks" todo)
  459. (const :tag "Only done tasks" done)
  460. (repeat :tag "Specific TODO keywords"
  461. (string :tag "Keyword"))))
  462. (defcustom org-export-time-stamp-file t
  463. "Non-nil means insert a time stamp into the exported file.
  464. The time stamp shows when the file was created.
  465. This option can also be set with the #+OPTIONS line,
  466. e.g. \"timestamp:nil\"."
  467. :group 'org-export-general
  468. :type 'boolean)
  469. (defcustom org-export-with-timestamps t
  470. "If nil, do not export time stamps and associated keywords."
  471. :group 'org-export-general
  472. :type 'boolean)
  473. (defcustom org-export-with-todo-keywords t
  474. "Non-nil means include TODO keywords in export.
  475. When nil, remove all these keywords from the export.")
  476. (defcustom org-export-allow-BIND 'confirm
  477. "Non-nil means allow #+BIND to define local variable values for export.
  478. This is a potential security risk, which is why the user must
  479. confirm the use of these lines."
  480. :group 'org-export-general
  481. :type '(choice
  482. (const :tag "Never" nil)
  483. (const :tag "Always" t)
  484. (const :tag "Ask a confirmation for each file" confirm)))
  485. (defcustom org-export-snippet-translation-alist nil
  486. "Alist between export snippets back-ends and exporter back-ends.
  487. This variable allows to provide shortcuts for export snippets.
  488. For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
  489. back-end will recognize the contents of \"@h{<b>}\" as HTML code
  490. while every other back-end will ignore it."
  491. :group 'org-export-general
  492. :type '(repeat
  493. (cons
  494. (string :tag "Shortcut")
  495. (string :tag "Back-end"))))
  496. (defcustom org-export-coding-system nil
  497. "Coding system for the exported file."
  498. :group 'org-export-general
  499. :type 'coding-system)
  500. (defcustom org-export-copy-to-kill-ring t
  501. "Non-nil means exported stuff will also be pushed onto the kill ring."
  502. :group 'org-export-general
  503. :type 'boolean)
  504. (defcustom org-export-initial-scope 'buffer
  505. "The initial scope when exporting with `org-export-dispatch'.
  506. This variable can be either set to `buffer' or `subtree'."
  507. :group 'org-export-general
  508. :type '(choice
  509. (const :tag "Export current buffer" 'buffer)
  510. (const :tag "Export current subtree" 'subtree)))
  511. (defcustom org-export-show-temporary-export-buffer t
  512. "Non-nil means show buffer after exporting to temp buffer.
  513. When Org exports to a file, the buffer visiting that file is ever
  514. shown, but remains buried. However, when exporting to a temporary
  515. buffer, that buffer is popped up in a second window. When this variable
  516. is nil, the buffer remains buried also in these cases."
  517. :group 'org-export-general
  518. :type 'boolean)
  519. (defcustom org-export-dispatch-use-expert-ui nil
  520. "Non-nil means using a non-intrusive `org-export-dispatch'.
  521. In that case, no help buffer is displayed. Though, an indicator
  522. for current export scope is added to the prompt \(i.e. \"b\" when
  523. output is restricted to body only, \"s\" when it is restricted to
  524. the current subtree and \"v\" when only visible elements are
  525. considered for export\). Also, \[?] allows to switch back to
  526. standard mode."
  527. :group 'org-export-general
  528. :type 'boolean)
  529. ;;; The Communication Channel
  530. ;; During export process, every function has access to a number of
  531. ;; properties. They are of three types:
  532. ;; 1. Environment options are collected once at the very beginning of
  533. ;; the process, out of the original buffer and configuration.
  534. ;; Associated to the parse tree, they make an Org closure.
  535. ;; Collecting them is handled by `org-export-get-environment'
  536. ;; function.
  537. ;;
  538. ;; Most environment options are defined through the
  539. ;; `org-export-option-alist' variable.
  540. ;;
  541. ;; 2. Tree properties are extracted directly from the parsed tree,
  542. ;; just before export, by `org-export-collect-tree-properties'.
  543. ;;
  544. ;; 3. Local options are updated during parsing, and their value
  545. ;; depends on the level of recursion. For now, only `:ignore-list'
  546. ;; belongs to that category.
  547. ;; Here is the full list of properties available during transcode
  548. ;; process, with their category (option, tree or local) and their
  549. ;; value type.
  550. ;; + `:author' :: Author's name.
  551. ;; - category :: option
  552. ;; - type :: string
  553. ;; + `:back-end' :: Current back-end used for transcoding.
  554. ;; - category :: tree
  555. ;; - type :: symbol
  556. ;; + `:creator' :: String to write as creation information.
  557. ;; - category :: option
  558. ;; - type :: string
  559. ;; + `:date' :: String to use as date.
  560. ;; - category :: option
  561. ;; - type :: string
  562. ;; + `:description' :: Description text for the current data.
  563. ;; - category :: option
  564. ;; - type :: string
  565. ;; + `:email' :: Author's email.
  566. ;; - category :: option
  567. ;; - type :: string
  568. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  569. ;; process.
  570. ;; - category :: option
  571. ;; - type :: list of strings
  572. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  573. ;; their definition, as parsed data. Only non-inlined footnotes
  574. ;; are represented in this alist. Also, every definition isn't
  575. ;; guaranteed to be referenced in the parse tree. The purpose of
  576. ;; this property is to preserve definitions from oblivion
  577. ;; (i.e. when the parse tree comes from a part of the original
  578. ;; buffer), it isn't meant for direct use in a back-end. To
  579. ;; retrieve a definition relative to a reference, use
  580. ;; `org-export-get-footnote-definition' instead.
  581. ;; - category :: option
  582. ;; - type :: alist (STRING . LIST)
  583. ;; + `:headline-levels' :: Maximum level being exported as an
  584. ;; headline. Comparison is done with the relative level of
  585. ;; headlines in the parse tree, not necessarily with their
  586. ;; actual level.
  587. ;; - category :: option
  588. ;; - type :: integer
  589. ;; + `:headline-offset' :: Difference between relative and real level
  590. ;; of headlines in the parse tree. For example, a value of -1
  591. ;; means a level 2 headline should be considered as level
  592. ;; 1 (cf. `org-export-get-relative-level').
  593. ;; - category :: tree
  594. ;; - type :: integer
  595. ;; + `:headline-numbering' :: Alist between headlines and their
  596. ;; numbering, as a list of numbers
  597. ;; (cf. `org-export-get-headline-number').
  598. ;; - category :: tree
  599. ;; - type :: alist (INTEGER . LIST)
  600. ;; + `:ignore-list' :: List of elements and objects that should be
  601. ;; ignored during export.
  602. ;; - category :: local
  603. ;; - type :: list of elements and objects
  604. ;; + `:input-file' :: Full path to input file, if any.
  605. ;; - category :: option
  606. ;; - type :: string or nil
  607. ;; + `:keywords' :: List of keywords attached to data.
  608. ;; - category :: option
  609. ;; - type :: string
  610. ;; + `:language' :: Default language used for translations.
  611. ;; - category :: option
  612. ;; - type :: string
  613. ;; + `:macro-input-file' :: Macro returning file name of input file,
  614. ;; or nil.
  615. ;; - category :: option
  616. ;; - type :: string or nil
  617. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  618. ;; transcoding.
  619. ;; - category :: global
  620. ;; - type :: list (as returned by `org-element-parse-buffer')
  621. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  622. ;; all line breaks.
  623. ;; - category :: option
  624. ;; - type :: symbol (nil, t)
  625. ;; + `:section-numbers' :: Non-nil means transcoding should add
  626. ;; section numbers to headlines.
  627. ;; - category :: option
  628. ;; - type :: symbol (nil, t)
  629. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  630. ;; in transcoding. When such a tag is present,
  631. ;; subtrees without it are de facto excluded from
  632. ;; the process. See `use-select-tags'.
  633. ;; - category :: option
  634. ;; - type :: list of strings
  635. ;; + `:target-list' :: List of targets encountered in the parse tree.
  636. ;; This is used to partly resolve "fuzzy" links
  637. ;; (cf. `org-export-resolve-fuzzy-link').
  638. ;; - category :: tree
  639. ;; - type :: list of strings
  640. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  641. ;; a time stamp in the output.
  642. ;; - category :: option
  643. ;; - type :: symbol (nil, t)
  644. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  645. ;; also be transcoded. If it is set to the `headline' symbol,
  646. ;; only the archived headline's name is retained.
  647. ;; - category :: option
  648. ;; - type :: symbol (nil, t, `headline')
  649. ;; + `:with-author' :: Non-nil means author's name should be included
  650. ;; in the output.
  651. ;; - category :: option
  652. ;; - type :: symbol (nil, t)
  653. ;; + `:with-creator' :: Non-nild means a creation sentence should be
  654. ;; inserted at the end of the transcoded string. If the value
  655. ;; is `comment', it should be commented.
  656. ;; - category :: option
  657. ;; - type :: symbol (`comment', nil, t)
  658. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  659. ;; its value is a list of names, only drawers with such names
  660. ;; will be transcoded.
  661. ;; - category :: option
  662. ;; - type :: symbol (nil, t) or list of strings
  663. ;; + `:with-email' :: Non-nil means output should contain author's
  664. ;; email.
  665. ;; - category :: option
  666. ;; - type :: symbol (nil, t)
  667. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  668. ;; interpreted.
  669. ;; - category :: option
  670. ;; - type :: symbol (nil, t)
  671. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  672. ;; strings starting with a colon as a fixed-with (verbatim) area.
  673. ;; - category :: option
  674. ;; - type :: symbol (nil, t)
  675. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  676. ;; footnotes.
  677. ;; - category :: option
  678. ;; - type :: symbol (nil, t)
  679. ;; + `:with-priority' :: Non-nil means transcoding should include
  680. ;; priority cookies.
  681. ;; - category :: option
  682. ;; - type :: symbol (nil, t)
  683. ;; + `:with-special-strings' :: Non-nil means transcoding should
  684. ;; interpret special strings in plain text.
  685. ;; - category :: option
  686. ;; - type :: symbol (nil, t)
  687. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  688. ;; interpret subscript and superscript. With a value of "{}",
  689. ;; only interpret those using curly brackets.
  690. ;; - category :: option
  691. ;; - type :: symbol (nil, {}, t)
  692. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  693. ;; tables.
  694. ;; - category :: option
  695. ;; - type :: symbol (nil, t)
  696. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  697. ;; headlines. A `not-in-toc' value will remove them
  698. ;; from the table of contents, if any, nonetheless.
  699. ;; - category :: option
  700. ;; - type :: symbol (nil, t, `not-in-toc')
  701. ;; + `:with-tasks' :: Non-nil means transcoding should include
  702. ;; headlines with a TODO keyword. A `todo' value
  703. ;; will only include headlines with a todo type
  704. ;; keyword while a `done' value will do the
  705. ;; contrary. If a list of strings is provided, only
  706. ;; tasks with keywords belonging to that list will
  707. ;; be kept.
  708. ;; - category :: option
  709. ;; - type :: symbol (t, todo, done, nil) or list of strings
  710. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  711. ;; time stamps and associated keywords. Otherwise, completely
  712. ;; remove them.
  713. ;; - category :: option
  714. ;; - type :: symbol: (t, nil)
  715. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  716. ;; added to the output. An integer value limits its
  717. ;; depth.
  718. ;; - category :: option
  719. ;; - type :: symbol (nil, t or integer)
  720. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  721. ;; include TODO keywords.
  722. ;; - category :: option
  723. ;; - type :: symbol (nil, t)
  724. ;;;; Environment Options
  725. ;; Environment options encompass all parameters defined outside the
  726. ;; scope of the parsed data. They come from five sources, in
  727. ;; increasing precedence order:
  728. ;;
  729. ;; - Global variables,
  730. ;; - Options keyword symbols,
  731. ;; - Buffer keywords,
  732. ;; - Subtree properties.
  733. ;; The central internal function with regards to environment options
  734. ;; is `org-export-get-environment'. It updates global variables with
  735. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  736. ;; the different sources.
  737. ;; The internal functions doing the retrieval are:
  738. ;; `org-export-parse-option-keyword' ,
  739. ;; `org-export-get-subtree-options' ,
  740. ;; `org-export-get-inbuffer-options' and
  741. ;; `org-export-get-global-options'.
  742. ;;
  743. ;; Some properties, which do not rely on the previous sources but
  744. ;; still depend on the original buffer, are taken care of with
  745. ;; `org-export-initial-options'.
  746. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  747. ;; take care of the part relative to "#+BIND:" keywords.
  748. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  749. "Collect export options from the current buffer.
  750. Optional argument BACKEND is a symbol specifying which back-end
  751. specific options to read, if any.
  752. When optional argument SUBTREEP is non-nil, assume the export is
  753. done against the current sub-tree.
  754. Third optional argument EXT-PLIST is a property list with
  755. external parameters overriding Org default settings, but still
  756. inferior to file-local settings."
  757. ;; First install #+BIND variables.
  758. (org-export-install-letbind-maybe)
  759. ;; Get and prioritize export options...
  760. (let ((options (org-combine-plists
  761. ;; ... from global variables...
  762. (org-export-get-global-options backend)
  763. ;; ... from buffer's name (default title)...
  764. `(:title
  765. ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
  766. (and file
  767. (file-name-sans-extension
  768. (file-name-nondirectory file))))
  769. (buffer-name (buffer-base-buffer))))
  770. ;; ... from an external property list...
  771. ext-plist
  772. ;; ... from in-buffer settings...
  773. (org-export-get-inbuffer-options
  774. backend
  775. (and buffer-file-name
  776. (org-remove-double-quotes buffer-file-name)))
  777. ;; ... and from subtree, when appropriate.
  778. (and subtreep (org-export-get-subtree-options)))))
  779. ;; Add initial options.
  780. (setq options (append (org-export-initial-options) options))
  781. ;; Return plist.
  782. options))
  783. (defun org-export-parse-option-keyword (options &optional backend)
  784. "Parse an OPTIONS line and return values as a plist.
  785. Optional argument BACKEND is a symbol specifying which back-end
  786. specific items to read, if any."
  787. (let* ((all
  788. (append org-export-option-alist
  789. (and backend
  790. (let ((var (intern
  791. (format "org-%s-option-alist" backend))))
  792. (and (boundp var) (eval var))))))
  793. ;; Build an alist between #+OPTION: item and property-name.
  794. (alist (delq nil
  795. (mapcar (lambda (e)
  796. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  797. (car e))))
  798. all)))
  799. plist)
  800. (mapc (lambda (e)
  801. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  802. (car e)
  803. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  804. options)
  805. (setq plist (plist-put plist
  806. (cdr e)
  807. (car (read-from-string
  808. (match-string 2 options)))))))
  809. alist)
  810. plist))
  811. (defun org-export-get-subtree-options ()
  812. "Get export options in subtree at point.
  813. Assume point is at subtree's beginning.
  814. Return options as a plist."
  815. (let (prop plist)
  816. (when (setq prop (progn (looking-at org-todo-line-regexp)
  817. (or (save-match-data
  818. (org-entry-get (point) "EXPORT_TITLE"))
  819. (org-match-string-no-properties 3))))
  820. (setq plist
  821. (plist-put
  822. plist :title
  823. (org-element-parse-secondary-string
  824. prop
  825. (cdr (assq 'keyword org-element-string-restrictions))))))
  826. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  827. (setq plist (plist-put plist :text prop)))
  828. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  829. (setq plist (plist-put plist :author prop)))
  830. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  831. (setq plist (plist-put plist :date prop)))
  832. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  833. (setq plist (org-export-add-options-to-plist plist prop)))
  834. plist))
  835. (defun org-export-get-inbuffer-options (&optional backend files)
  836. "Return current buffer export options, as a plist.
  837. Optional argument BACKEND, when non-nil, is a symbol specifying
  838. which back-end specific options should also be read in the
  839. process.
  840. Optional argument FILES is a list of setup files names read so
  841. far, used to avoid circular dependencies.
  842. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  843. (org-with-wide-buffer
  844. (goto-char (point-min))
  845. (let ((case-fold-search t) plist)
  846. ;; 1. Special keywords, as in `org-export-special-keywords'.
  847. (let ((special-re (org-make-options-regexp org-export-special-keywords)))
  848. (while (re-search-forward special-re nil t)
  849. (let ((element (org-element-at-point)))
  850. (when (eq (org-element-type element) 'keyword)
  851. (let* ((key (upcase (org-element-property :key element)))
  852. (val (org-element-property :value element))
  853. (prop
  854. (cond
  855. ((string= key "SETUP_FILE")
  856. (let ((file
  857. (expand-file-name
  858. (org-remove-double-quotes (org-trim val)))))
  859. ;; Avoid circular dependencies.
  860. (unless (member file files)
  861. (with-temp-buffer
  862. (insert (org-file-contents file 'noerror))
  863. (org-mode)
  864. (org-export-get-inbuffer-options
  865. backend (cons file files))))))
  866. ((string= key "OPTIONS")
  867. (org-export-parse-option-keyword val backend))
  868. ((string= key "MACRO")
  869. (when (string-match
  870. "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
  871. val)
  872. (let ((key
  873. (intern
  874. (concat ":macro-"
  875. (downcase (match-string 1 val)))))
  876. (value (org-match-string-no-properties 2 val)))
  877. (cond
  878. ((not value) nil)
  879. ;; Value will be evaled. Leave it as-is.
  880. ((string-match "\\`(eval\\>" value)
  881. (list key value))
  882. ;; Value has to be parsed for nested
  883. ;; macros.
  884. (t
  885. (list
  886. key
  887. (let ((restr
  888. (cdr
  889. (assq 'macro
  890. org-element-object-restrictions))))
  891. (org-element-parse-secondary-string
  892. ;; If user explicitly asks for
  893. ;; a newline, be sure to preserve it
  894. ;; from further filling with
  895. ;; `hard-newline'. Also replace
  896. ;; "\\n" with "\n", "\\\n" with "\\n"
  897. ;; and so on...
  898. (replace-regexp-in-string
  899. "\\(\\\\\\\\\\)n" "\\\\"
  900. (replace-regexp-in-string
  901. "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
  902. hard-newline value nil nil 1)
  903. nil nil 1)
  904. restr)))))))))))
  905. (setq plist (org-combine-plists plist prop)))))))
  906. ;; 2. Standard options, as in `org-export-option-alist'.
  907. (let* ((all (append org-export-option-alist
  908. ;; Also look for back-end specific options
  909. ;; if BACKEND is defined.
  910. (and backend
  911. (let ((var
  912. (intern
  913. (format "org-%s-option-alist" backend))))
  914. (and (boundp var) (eval var))))))
  915. ;; Build alist between keyword name and property name.
  916. (alist
  917. (delq nil (mapcar
  918. (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
  919. all)))
  920. ;; Build regexp matching all keywords associated to export
  921. ;; options. Note: the search is case insensitive.
  922. (opt-re (org-make-options-regexp
  923. (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
  924. (goto-char (point-min))
  925. (while (re-search-forward opt-re nil t)
  926. (let ((element (org-element-at-point)))
  927. (when (eq (org-element-type element) 'keyword)
  928. (let* ((key (upcase (org-element-property :key element)))
  929. (val (org-element-property :value element))
  930. (prop (cdr (assoc key alist)))
  931. (behaviour (nth 4 (assq prop all))))
  932. (setq plist
  933. (plist-put
  934. plist prop
  935. ;; Handle value depending on specified BEHAVIOUR.
  936. (case behaviour
  937. (space
  938. (if (not (plist-get plist prop)) (org-trim val)
  939. (concat (plist-get plist prop) " " (org-trim val))))
  940. (newline
  941. (org-trim
  942. (concat (plist-get plist prop) "\n" (org-trim val))))
  943. (split
  944. `(,@(plist-get plist prop) ,@(org-split-string val)))
  945. ('t val)
  946. (otherwise (if (not (plist-member plist prop)) val
  947. (plist-get plist prop))))))))))
  948. ;; Parse keywords specified in `org-element-parsed-keywords'.
  949. (mapc
  950. (lambda (key)
  951. (let* ((prop (cdr (assoc key alist)))
  952. (value (and prop (plist-get plist prop))))
  953. (when (stringp value)
  954. (setq plist
  955. (plist-put
  956. plist prop
  957. (org-element-parse-secondary-string
  958. value
  959. (cdr (assq 'keyword org-element-string-restrictions))))))))
  960. org-element-parsed-keywords))
  961. ;; 3. Return final value.
  962. plist)))
  963. (defun org-export-get-global-options (&optional backend)
  964. "Return global export options as a plist.
  965. Optional argument BACKEND, if non-nil, is a symbol specifying
  966. which back-end specific export options should also be read in the
  967. process."
  968. (let ((all (append org-export-option-alist
  969. (and backend
  970. (let ((var (intern
  971. (format "org-%s-option-alist" backend))))
  972. (and (boundp var) (eval var))))))
  973. ;; Output value.
  974. plist)
  975. (mapc (lambda (cell)
  976. (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
  977. all)
  978. ;; Return value.
  979. plist))
  980. (defun org-export-initial-options ()
  981. "Return a plist with properties related to input buffer."
  982. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  983. (list
  984. ;; Store full path of input file name, or nil. For internal use.
  985. :input-file visited-file
  986. ;; `:macro-date', `:macro-time' and `:macro-property' could as well
  987. ;; be initialized as tree properties, since they don't depend on
  988. ;; initial environment. Though, it may be more logical to keep
  989. ;; them close to other ":macro-" properties.
  990. :macro-date "(eval (format-time-string \"$1\"))"
  991. :macro-time "(eval (format-time-string \"$1\"))"
  992. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
  993. :macro-modification-time
  994. (and visited-file
  995. (file-exists-p visited-file)
  996. (concat "(eval (format-time-string \"$1\" '"
  997. (prin1-to-string (nth 5 (file-attributes visited-file)))
  998. "))"))
  999. ;; Store input file name as a macro.
  1000. :macro-input-file (and visited-file (file-name-nondirectory visited-file))
  1001. ;; Footnotes definitions must be collected in the original buffer,
  1002. ;; as there's no insurance that they will still be in the parse
  1003. ;; tree, due to some narrowing.
  1004. :footnote-definition-alist
  1005. (let (alist)
  1006. (org-with-wide-buffer
  1007. (goto-char (point-min))
  1008. (while (re-search-forward org-footnote-definition-re nil t)
  1009. (let ((def (org-footnote-at-definition-p)))
  1010. (when def
  1011. (org-skip-whitespace)
  1012. (push (cons (car def)
  1013. (save-restriction
  1014. (narrow-to-region (point) (nth 2 def))
  1015. ;; Like `org-element-parse-buffer', but
  1016. ;; makes sure the definition doesn't start
  1017. ;; with a section element.
  1018. (nconc
  1019. (list 'org-data nil)
  1020. (org-element-parse-elements
  1021. (point-min) (point-max) nil nil nil nil nil))))
  1022. alist))))
  1023. alist)))))
  1024. (defvar org-export-allow-BIND-local nil)
  1025. (defun org-export-confirm-letbind ()
  1026. "Can we use #+BIND values during export?
  1027. By default this will ask for confirmation by the user, to divert
  1028. possible security risks."
  1029. (cond
  1030. ((not org-export-allow-BIND) nil)
  1031. ((eq org-export-allow-BIND t) t)
  1032. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  1033. (t (org-set-local 'org-export-allow-BIND-local
  1034. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  1035. (defun org-export-install-letbind-maybe ()
  1036. "Install the values from #+BIND lines as local variables.
  1037. Variables must be installed before in-buffer options are
  1038. retrieved."
  1039. (let (letbind pair)
  1040. (org-with-wide-buffer
  1041. (goto-char (point-min))
  1042. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  1043. (when (org-export-confirm-letbind)
  1044. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  1045. letbind))))
  1046. (while (setq pair (pop letbind))
  1047. (org-set-local (car pair) (nth 1 pair)))))
  1048. ;;;; Tree Properties
  1049. ;; Tree properties are infromation extracted from parse tree. They
  1050. ;; are initialized at the beginning of the transcoding process by
  1051. ;; `org-export-collect-tree-properties'.
  1052. ;; Dedicated functions focus on computing the value of specific tree
  1053. ;; properties during initialization. Thus,
  1054. ;; `org-export-populate-ignore-list' lists elements and objects that
  1055. ;; should be skipped during export, `org-export-get-min-level' gets
  1056. ;; the minimal exportable level, used as a basis to compute relative
  1057. ;; level for headlines. Eventually
  1058. ;; `org-export-collect-headline-numbering' builds an alist between
  1059. ;; headlines and their numbering.
  1060. (defun org-export-collect-tree-properties (data info backend)
  1061. "Extract tree properties from parse tree.
  1062. DATA is the parse tree from which information is retrieved. INFO
  1063. is a list holding export options. BACKEND is the back-end called
  1064. for transcoding, as a symbol.
  1065. Following tree properties are set:
  1066. `:back-end' Back-end used for transcoding.
  1067. `:headline-offset' Offset between true level of headlines and
  1068. local level. An offset of -1 means an headline
  1069. of level 2 should be considered as a level
  1070. 1 headline in the context.
  1071. `:headline-numbering' Alist of all headlines as key an the
  1072. associated numbering as value.
  1073. `:ignore-list' List of elements that should be ignored during
  1074. export.
  1075. `:parse-tree' Whole parse tree.
  1076. `:target-list' List of all targets in the parse tree."
  1077. ;; First, get the list of elements and objects to ignore, and put it
  1078. ;; into `:ignore-list'.
  1079. (setq info
  1080. (plist-put info
  1081. :ignore-list (org-export-populate-ignore-list data info)))
  1082. ;; Then compute `:headline-offset' in order to be able to use
  1083. ;; `org-export-get-relative-level'.
  1084. (setq info
  1085. (plist-put info
  1086. :headline-offset (- 1 (org-export-get-min-level data info))))
  1087. ;; Now, properties order doesn't matter: get the rest of the tree
  1088. ;; properties.
  1089. (nconc
  1090. `(:parse-tree
  1091. ,data
  1092. :target-list
  1093. ,(org-element-map data 'target (lambda (target local) target) info)
  1094. :headline-numbering ,(org-export-collect-headline-numbering data info)
  1095. :back-end ,backend)
  1096. info))
  1097. (defun org-export-get-min-level (data options)
  1098. "Return minimum exportable headline's level in DATA.
  1099. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1100. OPTIONS is a plist holding export options."
  1101. (catch 'exit
  1102. (let ((min-level 10000))
  1103. (mapc
  1104. (lambda (blob)
  1105. (when (and (eq (org-element-type blob) 'headline)
  1106. (not (member blob (plist-get options :ignore-list))))
  1107. (setq min-level
  1108. (min (org-element-property :level blob) min-level)))
  1109. (when (= min-level 1) (throw 'exit 1)))
  1110. (org-element-contents data))
  1111. ;; If no headline was found, for the sake of consistency, set
  1112. ;; minimum level to 1 nonetheless.
  1113. (if (= min-level 10000) 1 min-level))))
  1114. (defun org-export-collect-headline-numbering (data options)
  1115. "Return numbering of all exportable headlines in a parse tree.
  1116. DATA is the parse tree. OPTIONS is the plist holding export
  1117. options.
  1118. Return an alist whose key is an headline and value is its
  1119. associated numbering \(in the shape of a list of numbers\)."
  1120. (let ((numbering (make-vector org-export-max-depth 0)))
  1121. (org-element-map
  1122. data
  1123. 'headline
  1124. (lambda (headline info)
  1125. (let ((relative-level
  1126. (1- (org-export-get-relative-level headline info))))
  1127. (cons
  1128. headline
  1129. (loop for n across numbering
  1130. for idx from 0 to org-export-max-depth
  1131. when (< idx relative-level) collect n
  1132. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1133. when (> idx relative-level) do (aset numbering idx 0)))))
  1134. options)))
  1135. (defun org-export-populate-ignore-list (data options)
  1136. "Return list of elements and objects to ignore during export.
  1137. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1138. export options.
  1139. Return elements or objects to ignore as a list."
  1140. (let (ignore
  1141. (walk-data
  1142. (function
  1143. (lambda (data options selected)
  1144. ;; Collect ignored elements or objects into IGNORE-LIST.
  1145. (mapc
  1146. (lambda (el)
  1147. (if (org-export--skip-p el options selected) (push el ignore)
  1148. (let ((type (org-element-type el)))
  1149. (if (and (eq (plist-get info :with-archived-trees) 'headline)
  1150. (eq (org-element-type el) 'headline)
  1151. (org-element-property :archivedp el))
  1152. ;; If headline is archived but tree below has
  1153. ;; to be skipped, add it to ignore list.
  1154. (mapc (lambda (e) (push e ignore))
  1155. (org-element-contents el))
  1156. ;; Move into recursive objects/elements.
  1157. (when (or (eq type 'org-data)
  1158. (memq type org-element-greater-elements)
  1159. (memq type org-element-recursive-objects)
  1160. (eq type 'paragraph))
  1161. (funcall walk-data el options selected))))))
  1162. (org-element-contents data))))))
  1163. ;; Main call. First find trees containing a select tag, if any.
  1164. (funcall walk-data data options (org-export--selected-trees data options))
  1165. ;; Return value.
  1166. ignore))
  1167. (defun org-export--selected-trees (data info)
  1168. "Return list of headlines containing a select tag in their tree.
  1169. DATA is parsed data as returned by `org-element-parse-buffer'.
  1170. INFO is a plist holding export options."
  1171. (let (selected-trees
  1172. (walk-data
  1173. (function
  1174. (lambda (data genealogy)
  1175. (case (org-element-type data)
  1176. (org-data
  1177. (funcall walk-data (org-element-contents data) genealogy))
  1178. (headline
  1179. (let ((tags (org-element-property :tags headline)))
  1180. (if (and tags
  1181. (loop for tag in (plist-get info :select-tags)
  1182. thereis (string-match
  1183. (format ":%s:" tag) tags)))
  1184. ;; When a select tag is found, mark as acceptable
  1185. ;; full genealogy and every headline within the
  1186. ;; tree.
  1187. (setq selected-trees
  1188. (append
  1189. (cons data genealogy)
  1190. (org-element-map data 'headline (lambda (h p) h))
  1191. selected-trees))
  1192. ;; Else, continue searching in tree, recursively.
  1193. (funcall walk-data data (cons data genealogy))))))))))
  1194. (funcall walk-data data nil) selected-trees))
  1195. (defun org-export--skip-p (blob options select-tags)
  1196. "Non-nil when element or object BLOB should be skipped during export.
  1197. OPTIONS is the plist holding export options."
  1198. (case (org-element-type blob)
  1199. ;; Check headline.
  1200. (headline
  1201. (let ((with-tasks (plist-get options :with-tasks))
  1202. (todo (org-element-property :todo-keyword blob))
  1203. (todo-type (org-element-property :todo-type blob))
  1204. (archived (plist-get options :with-archived-trees))
  1205. (tag-list (let ((tags (org-element-property :tags blob)))
  1206. (and tags (org-split-string tags ":")))))
  1207. (or
  1208. ;; Ignore subtrees with an exclude tag.
  1209. (loop for k in (plist-get options :exclude-tags)
  1210. thereis (member k tag-list))
  1211. ;; Ignore subtrees without a select tag, when such tag is
  1212. ;; found in the buffer.
  1213. (member blob select-tags)
  1214. ;; Ignore commented sub-trees.
  1215. (org-element-property :commentedp blob)
  1216. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1217. (and (not archived) (org-element-property :archivedp blob))
  1218. ;; Ignore tasks, if specified by `:with-tasks' property.
  1219. (and todo
  1220. (or (not with-tasks)
  1221. (and (memq with-tasks '(todo done))
  1222. (not (eq todo-type with-tasks)))
  1223. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1224. ;; Check time-stamp.
  1225. (time-stamp (not (plist-get options :with-timestamps)))
  1226. ;; Check drawer.
  1227. (drawer
  1228. (or (not (plist-get options :with-drawers))
  1229. (and (consp (plist-get options :with-drawers))
  1230. (not (member (org-element-property :drawer-name blob)
  1231. (plist-get options :with-drawers))))))))
  1232. ;;; The Transcoder
  1233. ;; This function reads Org data (obtained with, i.e.
  1234. ;; `org-element-parse-buffer') and transcodes it into a specified
  1235. ;; back-end output. It takes care of updating local properties,
  1236. ;; filtering out elements or objects according to export options and
  1237. ;; organizing the output blank lines and white space are preserved.
  1238. ;; Though, this function is inapropriate for secondary strings, which
  1239. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1240. ;; `org-export-secondary-string' is provided for that specific task.
  1241. ;; Internally, three functions handle the filtering of objects and
  1242. ;; elements during the export. In particular,
  1243. ;; `org-export-ignore-element' mark an element or object so future
  1244. ;; parse tree traversals skip it, `org-export-interpret-p' tells which
  1245. ;; elements or objects should be seen as real Org syntax and
  1246. ;; `org-export-expand' transforms the others back into their original
  1247. ;; shape.
  1248. (defun org-export-data (data backend info)
  1249. "Convert DATA to a string into BACKEND format.
  1250. DATA is a nested list as returned by `org-element-parse-buffer'.
  1251. BACKEND is a symbol among supported exporters.
  1252. INFO is a plist holding export options and also used as
  1253. a communication channel between elements when walking the nested
  1254. list. See `org-export-update-info' function for more
  1255. details.
  1256. Return transcoded string."
  1257. (mapconcat
  1258. ;; BLOB can be an element, an object, a string, or nil.
  1259. (lambda (blob)
  1260. (cond
  1261. ((not blob) nil)
  1262. ;; BLOB is a string. Check if the optional transcoder for plain
  1263. ;; text exists, and call it in that case. Otherwise, simply
  1264. ;; return string. Also update INFO and call
  1265. ;; `org-export-filter-plain-text-functions'.
  1266. ((stringp blob)
  1267. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1268. (org-export-filter-apply-functions
  1269. (plist-get info :filter-plain-text)
  1270. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1271. backend info)))
  1272. ;; BLOB is an element or an object.
  1273. (t
  1274. (let* ((type (org-element-type blob))
  1275. ;; 1. Determine the appropriate TRANSCODER.
  1276. (transcoder
  1277. (cond
  1278. ;; 1.0 A full Org document is inserted.
  1279. ((eq type 'org-data) 'identity)
  1280. ;; 1.1. BLOB should be ignored.
  1281. ((member blob (plist-get info :ignore-list)) nil)
  1282. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1283. ;; back into Org syntax.
  1284. ((not (org-export-interpret-p blob info)) 'org-export-expand)
  1285. ;; 1.3. Else apply naming convention.
  1286. (t (let ((trans (intern (format "org-%s-%s" backend type))))
  1287. (and (fboundp trans) trans)))))
  1288. ;; 2. Compute CONTENTS of BLOB.
  1289. (contents
  1290. (cond
  1291. ;; Case 0. No transcoder defined: ignore BLOB.
  1292. ((not transcoder) nil)
  1293. ;; Case 1. Transparently export an Org document.
  1294. ((eq type 'org-data) (org-export-data blob backend info))
  1295. ;; Case 2. For a recursive object.
  1296. ((memq type org-element-recursive-objects)
  1297. (org-export-data blob backend info))
  1298. ;; Case 3. For a recursive element.
  1299. ((memq type org-element-greater-elements)
  1300. ;; Ignore contents of an archived tree
  1301. ;; when `:with-archived-trees' is `headline'.
  1302. (unless (and
  1303. (eq type 'headline)
  1304. (eq (plist-get info :with-archived-trees) 'headline)
  1305. (org-element-property :archivedp blob))
  1306. (org-element-normalize-string
  1307. (org-export-data blob backend info))))
  1308. ;; Case 4. For a paragraph.
  1309. ((eq type 'paragraph)
  1310. (let ((paragraph
  1311. (org-element-normalize-contents
  1312. blob
  1313. ;; When normalizing contents of an item or
  1314. ;; a footnote definition, ignore first line's
  1315. ;; indentation: there is none and it might be
  1316. ;; misleading.
  1317. (and (not (org-export-get-previous-element blob info))
  1318. (let ((parent (org-export-get-parent blob info)))
  1319. (memq (org-element-type parent)
  1320. '(footnote-definition item)))))))
  1321. (org-export-data paragraph backend info)))))
  1322. ;; 3. Transcode BLOB into RESULTS string.
  1323. (results (cond
  1324. ((not transcoder) nil)
  1325. ((eq transcoder 'org-export-expand)
  1326. (org-export-data
  1327. `(org-data nil ,(funcall transcoder blob contents))
  1328. backend info))
  1329. (t (funcall transcoder blob contents info)))))
  1330. ;; 4. Return results.
  1331. (cond
  1332. ;; Discard nil results. Also ignore BLOB from further
  1333. ;; traversals in parse tree.
  1334. ((not results) (org-export-ignore-element blob info) nil)
  1335. ;; No filter for a full document.
  1336. ((eq type 'org-data) results)
  1337. ;; Otherwise, update INFO, append the same white space
  1338. ;; between elements or objects as in the original buffer,
  1339. ;; and call appropriate filters.
  1340. (t
  1341. (let ((results
  1342. (org-export-filter-apply-functions
  1343. (plist-get info (intern (format ":filter-%s" type)))
  1344. (let ((post-blank (org-element-property :post-blank blob)))
  1345. (if (memq type org-element-all-elements)
  1346. (concat (org-element-normalize-string results)
  1347. (make-string post-blank ?\n))
  1348. (concat results (make-string post-blank ? ))))
  1349. backend info)))
  1350. ;; If BLOB was transcoded into an empty string, ignore it
  1351. ;; from subsequent traversals.
  1352. (unless (org-string-nw-p results)
  1353. (org-export-ignore-element blob info))
  1354. ;; Eventually return string.
  1355. results)))))))
  1356. (org-element-contents data) ""))
  1357. (defun org-export-secondary-string (secondary backend info)
  1358. "Convert SECONDARY string into BACKEND format.
  1359. SECONDARY is a nested list as returned by
  1360. `org-element-parse-secondary-string'.
  1361. BACKEND is a symbol among supported exporters. INFO is a plist
  1362. used as a communication channel.
  1363. Return transcoded string."
  1364. ;; Make SECONDARY acceptable for `org-export-data'.
  1365. (let ((s (if (listp secondary) secondary (list secondary))))
  1366. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1367. (defun org-export-interpret-p (blob info)
  1368. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1369. Check is done according to export options INFO, stored as
  1370. a plist."
  1371. (case (org-element-type blob)
  1372. ;; ... entities...
  1373. (entity (plist-get info :with-entities))
  1374. ;; ... emphasis...
  1375. (emphasis (plist-get info :with-emphasize))
  1376. ;; ... fixed-width areas.
  1377. (fixed-width (plist-get info :with-fixed-width))
  1378. ;; ... footnotes...
  1379. ((footnote-definition footnote-reference)
  1380. (plist-get info :with-footnotes))
  1381. ;; ... sub/superscripts...
  1382. ((subscript superscript)
  1383. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1384. (if (eq sub/super-p '{})
  1385. (org-element-property :use-brackets-p blob)
  1386. sub/super-p)))
  1387. ;; ... tables...
  1388. (table (plist-get info :with-tables))
  1389. (otherwise t)))
  1390. (defsubst org-export-expand (blob contents)
  1391. "Expand a parsed element or object to its original state.
  1392. BLOB is either an element or an object. CONTENTS is its
  1393. contents, as a string or nil."
  1394. (funcall (intern (format "org-element-%s-interpreter" (org-element-type blob)))
  1395. blob contents))
  1396. (defun org-export-ignore-element (element info)
  1397. "Add ELEMENT to `:ignore-list' in INFO.
  1398. Any element in `:ignore-list' will be skipped when using
  1399. `org-element-map'. INFO is modified by side effects."
  1400. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  1401. ;;; The Filter System
  1402. ;; Filters allow end-users to tweak easily the transcoded output.
  1403. ;; They are the functional counterpart of hooks, as every filter in
  1404. ;; a set is applied to the return value of the previous one.
  1405. ;; Every set is back-end agnostic. Although, a filter is always
  1406. ;; called, in addition to the string it applies to, with the back-end
  1407. ;; used as argument, so it's easy enough for the end-user to add
  1408. ;; back-end specific filters in the set. The communication channel,
  1409. ;; as a plist, is required as the third argument.
  1410. ;; Filters sets are defined below. There are of four types:
  1411. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1412. ;; complete parsed tree. It's the only filters set that doesn't
  1413. ;; apply to a string.
  1414. ;; - `org-export-filter-final-output-functions' applies to the final
  1415. ;; transcoded string.
  1416. ;; - `org-export-filter-plain-text-functions' applies to any string
  1417. ;; not recognized as Org syntax.
  1418. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1419. ;; after an element or object of type TYPE has been transcoded.
  1420. ;; All filters sets are applied through
  1421. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1422. ;; applied in a LIFO fashion. It allows developers to be sure that
  1423. ;; their filters will be applied first.
  1424. ;; Filters properties are installed in communication channel just
  1425. ;; before parsing, with `org-export-install-filters' function.
  1426. ;;;; Special Filters
  1427. (defvar org-export-filter-parse-tree-functions nil
  1428. "Filter, or list of filters, applied to the parsed tree.
  1429. Each filter is called with three arguments: the parse tree, as
  1430. returned by `org-element-parse-buffer', the back-end, as
  1431. a symbol, and the communication channel, as a plist. It must
  1432. return the modified parse tree to transcode.")
  1433. (defvar org-export-filter-final-output-functions nil
  1434. "Filter, or list of filters, applied to the transcoded string.
  1435. Each filter is called with three arguments: the full transcoded
  1436. string, the back-end, as a symbol, and the communication channel,
  1437. as a plist. It must return a string that will be used as the
  1438. final export output.")
  1439. (defvar org-export-filter-plain-text-functions nil
  1440. "Filter, or list of filters, applied to plain text.
  1441. Each filter is called with three arguments: a string which
  1442. contains no Org syntax, the back-end, as a symbol, and the
  1443. communication channel, as a plist. It must return a string or
  1444. nil.")
  1445. ;;;; Elements Filters
  1446. (defvar org-export-filter-center-block-functions nil
  1447. "List of functions applied to a transcoded center block.
  1448. Each filter is called with three arguments: the transcoded center
  1449. block, as a string, the back-end, as a symbol, and the
  1450. communication channel, as a plist. It must return a string or
  1451. nil.")
  1452. (defvar org-export-filter-drawer-functions nil
  1453. "List of functions applied to a transcoded drawer.
  1454. Each filter is called with three arguments: the transcoded
  1455. drawer, as a string, the back-end, as a symbol, and the
  1456. communication channel, as a plist. It must return a string or
  1457. nil.")
  1458. (defvar org-export-filter-dynamic-block-functions nil
  1459. "List of functions applied to a transcoded dynamic-block.
  1460. Each filter is called with three arguments: the transcoded
  1461. dynamic-block, as a string, the back-end, as a symbol, and the
  1462. communication channel, as a plist. It must return a string or
  1463. nil.")
  1464. (defvar org-export-filter-headline-functions nil
  1465. "List of functions applied to a transcoded headline.
  1466. Each filter is called with three arguments: the transcoded
  1467. headline, as a string, the back-end, as a symbol, and the
  1468. communication channel, as a plist. It must return a string or
  1469. nil.")
  1470. (defvar org-export-filter-inlinetask-functions nil
  1471. "List of functions applied to a transcoded inlinetask.
  1472. Each filter is called with three arguments: the transcoded
  1473. inlinetask, as a string, the back-end, as a symbol, and the
  1474. communication channel, as a plist. It must return a string or
  1475. nil.")
  1476. (defvar org-export-filter-plain-list-functions nil
  1477. "List of functions applied to a transcoded plain-list.
  1478. Each filter is called with three arguments: the transcoded
  1479. plain-list, as a string, the back-end, as a symbol, and the
  1480. communication channel, as a plist. It must return a string or
  1481. nil.")
  1482. (defvar org-export-filter-item-functions nil
  1483. "List of functions applied to a transcoded item.
  1484. Each filter is called with three arguments: the transcoded item,
  1485. as a string, the back-end, as a symbol, and the communication
  1486. channel, as a plist. It must return a string or nil.")
  1487. (defvar org-export-filter-comment-functions nil
  1488. "List of functions applied to a transcoded comment.
  1489. Each filter is called with three arguments: the transcoded
  1490. comment, as a string, the back-end, as a symbol, and the
  1491. communication channel, as a plist. It must return a string or
  1492. nil.")
  1493. (defvar org-export-filter-comment-block-functions nil
  1494. "List of functions applied to a transcoded comment-comment.
  1495. Each filter is called with three arguments: the transcoded
  1496. comment-block, as a string, the back-end, as a symbol, and the
  1497. communication channel, as a plist. It must return a string or
  1498. nil.")
  1499. (defvar org-export-filter-example-block-functions nil
  1500. "List of functions applied to a transcoded example-block.
  1501. Each filter is called with three arguments: the transcoded
  1502. example-block, as a string, the back-end, as a symbol, and the
  1503. communication channel, as a plist. It must return a string or
  1504. nil.")
  1505. (defvar org-export-filter-export-block-functions nil
  1506. "List of functions applied to a transcoded export-block.
  1507. Each filter is called with three arguments: the transcoded
  1508. export-block, as a string, the back-end, as a symbol, and the
  1509. communication channel, as a plist. It must return a string or
  1510. nil.")
  1511. (defvar org-export-filter-fixed-width-functions nil
  1512. "List of functions applied to a transcoded fixed-width.
  1513. Each filter is called with three arguments: the transcoded
  1514. fixed-width, as a string, the back-end, as a symbol, and the
  1515. communication channel, as a plist. It must return a string or
  1516. nil.")
  1517. (defvar org-export-filter-footnote-definition-functions nil
  1518. "List of functions applied to a transcoded footnote-definition.
  1519. Each filter is called with three arguments: the transcoded
  1520. footnote-definition, as a string, the back-end, as a symbol, and
  1521. the communication channel, as a plist. It must return a string
  1522. or nil.")
  1523. (defvar org-export-filter-horizontal-rule-functions nil
  1524. "List of functions applied to a transcoded horizontal-rule.
  1525. Each filter is called with three arguments: the transcoded
  1526. horizontal-rule, as a string, the back-end, as a symbol, and the
  1527. communication channel, as a plist. It must return a string or
  1528. nil.")
  1529. (defvar org-export-filter-keyword-functions nil
  1530. "List of functions applied to a transcoded keyword.
  1531. Each filter is called with three arguments: the transcoded
  1532. keyword, as a string, the back-end, as a symbol, and the
  1533. communication channel, as a plist. It must return a string or
  1534. nil.")
  1535. (defvar org-export-filter-latex-environment-functions nil
  1536. "List of functions applied to a transcoded latex-environment.
  1537. Each filter is called with three arguments: the transcoded
  1538. latex-environment, as a string, the back-end, as a symbol, and
  1539. the communication channel, as a plist. It must return a string
  1540. or nil.")
  1541. (defvar org-export-filter-babel-call-functions nil
  1542. "List of functions applied to a transcoded babel-call.
  1543. Each filter is called with three arguments: the transcoded
  1544. babel-call, as a string, the back-end, as a symbol, and the
  1545. communication channel, as a plist. It must return a string or
  1546. nil.")
  1547. (defvar org-export-filter-paragraph-functions nil
  1548. "List of functions applied to a transcoded paragraph.
  1549. Each filter is called with three arguments: the transcoded
  1550. paragraph, as a string, the back-end, as a symbol, and the
  1551. communication channel, as a plist. It must return a string or
  1552. nil.")
  1553. (defvar org-export-filter-property-drawer-functions nil
  1554. "List of functions applied to a transcoded property-drawer.
  1555. Each filter is called with three arguments: the transcoded
  1556. property-drawer, as a string, the back-end, as a symbol, and the
  1557. communication channel, as a plist. It must return a string or
  1558. nil.")
  1559. (defvar org-export-filter-quote-block-functions nil
  1560. "List of functions applied to a transcoded quote block.
  1561. Each filter is called with three arguments: the transcoded quote
  1562. block, as a string, the back-end, as a symbol, and the
  1563. communication channel, as a plist. It must return a string or
  1564. nil.")
  1565. (defvar org-export-filter-quote-section-functions nil
  1566. "List of functions applied to a transcoded quote-section.
  1567. Each filter is called with three arguments: the transcoded
  1568. quote-section, as a string, the back-end, as a symbol, and the
  1569. communication channel, as a plist. It must return a string or
  1570. nil.")
  1571. (defvar org-export-filter-section-functions nil
  1572. "List of functions applied to a transcoded section.
  1573. Each filter is called with three arguments: the transcoded
  1574. section, as a string, the back-end, as a symbol, and the
  1575. communication channel, as a plist. It must return a string or
  1576. nil.")
  1577. (defvar org-export-filter-special-block-functions nil
  1578. "List of functions applied to a transcoded special block.
  1579. Each filter is called with three arguments: the transcoded
  1580. special block, as a string, the back-end, as a symbol, and the
  1581. communication channel, as a plist. It must return a string or
  1582. nil.")
  1583. (defvar org-export-filter-src-block-functions nil
  1584. "List of functions applied to a transcoded src-block.
  1585. Each filter is called with three arguments: the transcoded
  1586. src-block, as a string, the back-end, as a symbol, and the
  1587. communication channel, as a plist. It must return a string or
  1588. nil.")
  1589. (defvar org-export-filter-table-functions nil
  1590. "List of functions applied to a transcoded table.
  1591. Each filter is called with three arguments: the transcoded table,
  1592. as a string, the back-end, as a symbol, and the communication
  1593. channel, as a plist. It must return a string or nil.")
  1594. (defvar org-export-filter-verse-block-functions nil
  1595. "List of functions applied to a transcoded verse block.
  1596. Each filter is called with three arguments: the transcoded verse
  1597. block, as a string, the back-end, as a symbol, and the
  1598. communication channel, as a plist. It must return a string or
  1599. nil.")
  1600. ;;;; Objects Filters
  1601. (defvar org-export-filter-emphasis-functions nil
  1602. "List of functions applied to a transcoded emphasis.
  1603. Each filter is called with three arguments: the transcoded
  1604. emphasis, as a string, the back-end, as a symbol, and the
  1605. communication channel, as a plist. It must return a string or
  1606. nil.")
  1607. (defvar org-export-filter-entity-functions nil
  1608. "List of functions applied to a transcoded entity.
  1609. Each filter is called with three arguments: the transcoded
  1610. entity, as a string, the back-end, as a symbol, and the
  1611. communication channel, as a plist. It must return a string or
  1612. nil.")
  1613. (defvar org-export-filter-export-snippet-functions nil
  1614. "List of functions applied to a transcoded export-snippet.
  1615. Each filter is called with three arguments: the transcoded
  1616. export-snippet, as a string, the back-end, as a symbol, and the
  1617. communication channel, as a plist. It must return a string or
  1618. nil.")
  1619. (defvar org-export-filter-footnote-reference-functions nil
  1620. "List of functions applied to a transcoded footnote-reference.
  1621. Each filter is called with three arguments: the transcoded
  1622. footnote-reference, as a string, the back-end, as a symbol, and
  1623. the communication channel, as a plist. It must return a string
  1624. or nil.")
  1625. (defvar org-export-filter-inline-babel-call-functions nil
  1626. "List of functions applied to a transcoded inline-babel-call.
  1627. Each filter is called with three arguments: the transcoded
  1628. inline-babel-call, as a string, the back-end, as a symbol, and
  1629. the communication channel, as a plist. It must return a string
  1630. or nil.")
  1631. (defvar org-export-filter-inline-src-block-functions nil
  1632. "List of functions applied to a transcoded inline-src-block.
  1633. Each filter is called with three arguments: the transcoded
  1634. inline-src-block, as a string, the back-end, as a symbol, and the
  1635. communication channel, as a plist. It must return a string or
  1636. nil.")
  1637. (defvar org-export-filter-latex-fragment-functions nil
  1638. "List of functions applied to a transcoded latex-fragment.
  1639. Each filter is called with three arguments: the transcoded
  1640. latex-fragment, as a string, the back-end, as a symbol, and the
  1641. communication channel, as a plist. It must return a string or
  1642. nil.")
  1643. (defvar org-export-filter-line-break-functions nil
  1644. "List of functions applied to a transcoded line-break.
  1645. Each filter is called with three arguments: the transcoded
  1646. line-break, as a string, the back-end, as a symbol, and the
  1647. communication channel, as a plist. It must return a string or
  1648. nil.")
  1649. (defvar org-export-filter-link-functions nil
  1650. "List of functions applied to a transcoded link.
  1651. Each filter is called with three arguments: the transcoded link,
  1652. as a string, the back-end, as a symbol, and the communication
  1653. channel, as a plist. It must return a string or nil.")
  1654. (defvar org-export-filter-macro-functions nil
  1655. "List of functions applied to a transcoded macro.
  1656. Each filter is called with three arguments: the transcoded macro,
  1657. as a string, the back-end, as a symbol, and the communication
  1658. channel, as a plist. It must return a string or nil.")
  1659. (defvar org-export-filter-radio-target-functions nil
  1660. "List of functions applied to a transcoded radio-target.
  1661. Each filter is called with three arguments: the transcoded
  1662. radio-target, as a string, the back-end, as a symbol, and the
  1663. communication channel, as a plist. It must return a string or
  1664. nil.")
  1665. (defvar org-export-filter-statistics-cookie-functions nil
  1666. "List of functions applied to a transcoded statistics-cookie.
  1667. Each filter is called with three arguments: the transcoded
  1668. statistics-cookie, as a string, the back-end, as a symbol, and
  1669. the communication channel, as a plist. It must return a string
  1670. or nil.")
  1671. (defvar org-export-filter-subscript-functions nil
  1672. "List of functions applied to a transcoded subscript.
  1673. Each filter is called with three arguments: the transcoded
  1674. subscript, as a string, the back-end, as a symbol, and the
  1675. communication channel, as a plist. It must return a string or
  1676. nil.")
  1677. (defvar org-export-filter-superscript-functions nil
  1678. "List of functions applied to a transcoded superscript.
  1679. Each filter is called with three arguments: the transcoded
  1680. superscript, as a string, the back-end, as a symbol, and the
  1681. communication channel, as a plist. It must return a string or
  1682. nil.")
  1683. (defvar org-export-filter-target-functions nil
  1684. "List of functions applied to a transcoded target.
  1685. Each filter is called with three arguments: the transcoded
  1686. target, as a string, the back-end, as a symbol, and the
  1687. communication channel, as a plist. It must return a string or
  1688. nil.")
  1689. (defvar org-export-filter-time-stamp-functions nil
  1690. "List of functions applied to a transcoded time-stamp.
  1691. Each filter is called with three arguments: the transcoded
  1692. time-stamp, as a string, the back-end, as a symbol, and the
  1693. communication channel, as a plist. It must return a string or
  1694. nil.")
  1695. (defvar org-export-filter-verbatim-functions nil
  1696. "List of functions applied to a transcoded verbatim.
  1697. Each filter is called with three arguments: the transcoded
  1698. verbatim, as a string, the back-end, as a symbol, and the
  1699. communication channel, as a plist. It must return a string or
  1700. nil.")
  1701. (defun org-export-filter-apply-functions (filters value backend info)
  1702. "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
  1703. Functions are called in a LIFO fashion, to be sure that developer
  1704. specified filters, if any, are called first."
  1705. (loop for filter in filters
  1706. if (not value) return nil else
  1707. do (setq value (funcall filter value backend info)))
  1708. value)
  1709. (defun org-export-install-filters (backend info)
  1710. "Install filters properties in communication channel.
  1711. BACKEND is a symbol specifying which back-end specific filters to
  1712. install, if any. INFO is a plist containing the current
  1713. communication channel.
  1714. Return the updated communication channel."
  1715. (let (plist)
  1716. ;; Install user defined filters with `org-export-filters-alist'.
  1717. (mapc (lambda (p)
  1718. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  1719. org-export-filters-alist)
  1720. ;; Prepend back-end specific filters to that list.
  1721. (let ((back-end-filters (intern (format "org-%s-filters-alist" backend))))
  1722. (when (boundp back-end-filters)
  1723. (mapc (lambda (p)
  1724. ;; Single values get consed, lists are prepended.
  1725. (let ((key (car p)) (value (cdr p)))
  1726. (when value
  1727. (setq plist
  1728. (plist-put
  1729. plist key
  1730. (if (atom value) (cons value (plist-get plist key))
  1731. (append value (plist-get plist key))))))))
  1732. (eval back-end-filters))))
  1733. ;; Return new communication channel.
  1734. (org-combine-plists info plist)))
  1735. ;;; Core functions
  1736. ;; This is the room for the main function, `org-export-as', along with
  1737. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1738. ;; They differ only by the way they output the resulting code.
  1739. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1740. ;; used with `org-export-to-file'. With a given extension, it tries
  1741. ;; to provide a canonical file name to write export output to.
  1742. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1743. ;; but a copy of it (with the same buffer-local variables and
  1744. ;; visibility), where include keywords are expanded and Babel blocks
  1745. ;; are executed, if appropriate.
  1746. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1747. ;; File inclusion is taken care of by
  1748. ;; `org-export-expand-include-keyword' and
  1749. ;; `org-export-prepare-file-contents'. Structure wise, including
  1750. ;; a whole Org file in a buffer often makes little sense. For
  1751. ;; example, if the file contains an headline and the include keyword
  1752. ;; was within an item, the item should contain the headline. That's
  1753. ;; why file inclusion should be done before any structure can be
  1754. ;; associated to the file, that is before parsing.
  1755. (defun org-export-as
  1756. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  1757. "Transcode current Org buffer into BACKEND code.
  1758. If narrowing is active in the current buffer, only transcode its
  1759. narrowed part.
  1760. If a region is active, transcode that region.
  1761. When optional argument SUBTREEP is non-nil, transcode the
  1762. sub-tree at point, extracting information from the headline
  1763. properties first.
  1764. When optional argument VISIBLE-ONLY is non-nil, don't export
  1765. contents of hidden elements.
  1766. When optional argument BODY-ONLY is non-nil, only return body
  1767. code, without preamble nor postamble.
  1768. Optional argument EXT-PLIST, when provided, is a property list
  1769. with external parameters overriding Org default settings, but
  1770. still inferior to file-local settings.
  1771. Optional argument NOEXPAND, when non-nil, prevents included files
  1772. to be expanded and Babel code to be executed.
  1773. Return code as a string."
  1774. (save-excursion
  1775. (save-restriction
  1776. ;; Narrow buffer to an appropriate region for parsing.
  1777. (cond ((org-region-active-p)
  1778. (narrow-to-region (region-beginning) (region-end)))
  1779. (subtreep (org-narrow-to-subtree)))
  1780. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
  1781. ;; Then options can be completed with tree properties. Note:
  1782. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1783. ;; created, where include keywords are expanded and code blocks
  1784. ;; are evaluated. RAW-DATA is the parsed tree of the buffer
  1785. ;; resulting from that process. Eventually call
  1786. ;; `org-export-filter-parse-tree-functions'.
  1787. (goto-char (point-min))
  1788. (let ((info (org-export-get-environment backend subtreep ext-plist)))
  1789. ;; Remove subtree's headline from contents if subtree mode is
  1790. ;; activated.
  1791. (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
  1792. ;; Install filters in communication channel.
  1793. (setq info (org-export-install-filters backend info))
  1794. (let ((raw-data
  1795. (org-export-filter-apply-functions
  1796. (plist-get info :filter-parse-tree)
  1797. ;; If NOEXPAND is non-nil, simply parse current
  1798. ;; visible part of buffer.
  1799. (if noexpand (org-element-parse-buffer nil visible-only)
  1800. (org-export-with-current-buffer-copy
  1801. (org-export-expand-include-keyword nil)
  1802. (let ((org-current-export-file (current-buffer)))
  1803. (org-export-blocks-preprocess))
  1804. (org-element-parse-buffer nil visible-only)))
  1805. backend info)))
  1806. ;; Complete communication channel with tree properties.
  1807. (setq info
  1808. (org-combine-plists
  1809. info
  1810. (org-export-collect-tree-properties raw-data info backend)))
  1811. ;; Transcode RAW-DATA. Also call
  1812. ;; `org-export-filter-final-output-functions'.
  1813. (let* ((body (org-element-normalize-string
  1814. (org-export-data raw-data backend info)))
  1815. (template (intern (format "org-%s-template" backend)))
  1816. (output (org-export-filter-apply-functions
  1817. (plist-get info :filter-final-output)
  1818. (if (or (not (fboundp template)) body-only) body
  1819. (funcall template body info))
  1820. backend info)))
  1821. ;; Maybe add final OUTPUT to kill ring before returning it.
  1822. (when org-export-copy-to-kill-ring (org-kill-new output))
  1823. output))))))
  1824. (defun org-export-to-buffer
  1825. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  1826. "Call `org-export-as' with output to a specified buffer.
  1827. BACKEND is the back-end used for transcoding, as a symbol.
  1828. BUFFER is the output buffer. If it already exists, it will be
  1829. erased first, otherwise, it will be created.
  1830. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1831. and NOEXPAND are similar to those used in `org-export-as', which
  1832. see.
  1833. Return buffer."
  1834. (let ((out (org-export-as
  1835. backend subtreep visible-only body-only ext-plist noexpand))
  1836. (buffer (get-buffer-create buffer)))
  1837. (with-current-buffer buffer
  1838. (erase-buffer)
  1839. (insert out)
  1840. (goto-char (point-min)))
  1841. buffer))
  1842. (defun org-export-to-file
  1843. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  1844. "Call `org-export-as' with output to a specified file.
  1845. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1846. is the name of the output file, as a string.
  1847. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1848. and NOEXPAND are similar to those used in `org-export-as', which
  1849. see.
  1850. Return output file's name."
  1851. ;; Checks for FILE permissions. `write-file' would do the same, but
  1852. ;; we'd rather avoid needless transcoding of parse tree.
  1853. (unless (file-writable-p file) (error "Output file not writable"))
  1854. ;; Insert contents to a temporary buffer and write it to FILE.
  1855. (let ((out (org-export-as
  1856. backend subtreep visible-only body-only ext-plist noexpand)))
  1857. (with-temp-buffer
  1858. (insert out)
  1859. (let ((coding-system-for-write org-export-coding-system))
  1860. (write-file file))))
  1861. ;; Return full path.
  1862. file)
  1863. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  1864. "Return output file's name according to buffer specifications.
  1865. EXTENSION is a string representing the output file extension,
  1866. with the leading dot.
  1867. With a non-nil optional argument SUBTREEP, try to determine
  1868. output file's name by looking for \"EXPORT_FILE_NAME\" property
  1869. of subtree at point.
  1870. When optional argument PUB-DIR is set, use it as the publishing
  1871. directory.
  1872. Return file name as a string, or nil if it couldn't be
  1873. determined."
  1874. (let ((base-name
  1875. ;; File name may come from EXPORT_FILE_NAME subtree property,
  1876. ;; assuming point is at beginning of said sub-tree.
  1877. (file-name-sans-extension
  1878. (or (and subtreep
  1879. (org-entry-get
  1880. (save-excursion
  1881. (ignore-errors
  1882. (org-back-to-heading (not visible-only)) (point)))
  1883. "EXPORT_FILE_NAME" t))
  1884. ;; File name may be extracted from buffer's associated
  1885. ;; file, if any.
  1886. (buffer-file-name (buffer-base-buffer))
  1887. ;; Can't determine file name on our own: Ask user.
  1888. (let ((read-file-name-function
  1889. (and org-completion-use-ido 'ido-read-file-name)))
  1890. (read-file-name
  1891. "Output file: " pub-dir nil nil nil
  1892. (lambda (name)
  1893. (string= (file-name-extension name t) extension))))))))
  1894. ;; Build file name. Enforce EXTENSION over whatever user may have
  1895. ;; come up with. PUB-DIR, if defined, always has precedence over
  1896. ;; any provided path.
  1897. (cond
  1898. (pub-dir
  1899. (concat (file-name-as-directory pub-dir)
  1900. (file-name-nondirectory base-name)
  1901. extension))
  1902. ((string= (file-name-nondirectory base-name) base-name)
  1903. (concat (file-name-as-directory ".") base-name extension))
  1904. (t (concat base-name extension)))))
  1905. (defmacro org-export-with-current-buffer-copy (&rest body)
  1906. "Apply BODY in a copy of the current buffer.
  1907. The copy preserves local variables and visibility of the original
  1908. buffer.
  1909. Point is at buffer's beginning when BODY is applied."
  1910. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1911. `(let ((,original-buffer ,(current-buffer))
  1912. (,offset ,(1- (point-min)))
  1913. (,buffer-string ,(buffer-string))
  1914. (,overlays (mapcar
  1915. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1916. (with-temp-buffer
  1917. (let ((buffer-invisibility-spec nil))
  1918. (org-clone-local-variables
  1919. ,original-buffer
  1920. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  1921. (insert ,buffer-string)
  1922. (mapc (lambda (ov)
  1923. (move-overlay
  1924. ov
  1925. (- (overlay-start ov) ,offset)
  1926. (- (overlay-end ov) ,offset)
  1927. (current-buffer)))
  1928. ,overlays)
  1929. (goto-char (point-min))
  1930. (progn ,@body))))))
  1931. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1932. (defun org-export-expand-include-keyword (included)
  1933. "Expand every include keyword in buffer.
  1934. INCLUDED is a list of included file names along with their line
  1935. restriction, when appropriate. It is used to avoid infinite
  1936. recursion."
  1937. (let ((case-fold-search nil))
  1938. (goto-char (point-min))
  1939. (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
  1940. (when (eq (org-element-type (save-match-data (org-element-at-point)))
  1941. 'keyword)
  1942. (beginning-of-line)
  1943. ;; Extract arguments from keyword's value.
  1944. (let* ((value (match-string 1))
  1945. (ind (org-get-indentation))
  1946. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1947. (prog1 (expand-file-name (match-string 1 value))
  1948. (setq value (replace-match "" nil nil value)))))
  1949. (lines
  1950. (and (string-match
  1951. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1952. (prog1 (match-string 1 value)
  1953. (setq value (replace-match "" nil nil value)))))
  1954. (env (cond ((string-match "\\<example\\>" value) 'example)
  1955. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1956. (match-string 1 value))))
  1957. ;; Minimal level of included file defaults to the child
  1958. ;; level of the current headline, if any, or one. It
  1959. ;; only applies is the file is meant to be included as
  1960. ;; an Org one.
  1961. (minlevel
  1962. (and (not env)
  1963. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  1964. (prog1 (string-to-number (match-string 1 value))
  1965. (setq value (replace-match "" nil nil value)))
  1966. (let ((cur (org-current-level)))
  1967. (if cur (1+ (org-reduced-level cur)) 1))))))
  1968. ;; Remove keyword.
  1969. (delete-region (point) (progn (forward-line) (point)))
  1970. (cond
  1971. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  1972. ;; Check if files has already been parsed. Look after
  1973. ;; inclusion lines too, as different parts of the same file
  1974. ;; can be included too.
  1975. ((member (list file lines) included)
  1976. (error "Recursive file inclusion: %s" file))
  1977. (t
  1978. (cond
  1979. ((eq env 'example)
  1980. (insert
  1981. (let ((ind-str (make-string ind ? ))
  1982. (contents
  1983. ;; Protect sensitive contents with commas.
  1984. (replace-regexp-in-string
  1985. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  1986. (org-export-prepare-file-contents file lines)
  1987. nil nil 1)))
  1988. (format "%s#+begin_example\n%s%s#+end_example\n"
  1989. ind-str contents ind-str))))
  1990. ((stringp env)
  1991. (insert
  1992. (let ((ind-str (make-string ind ? ))
  1993. (contents
  1994. ;; Protect sensitive contents with commas.
  1995. (replace-regexp-in-string
  1996. (if (string= env "org") "\\(^\\)\\(.\\)"
  1997. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  1998. (org-export-prepare-file-contents file lines)
  1999. nil nil 1)))
  2000. (format "%s#+begin_src %s\n%s%s#+end_src\n"
  2001. ind-str env contents ind-str))))
  2002. (t
  2003. (insert
  2004. (with-temp-buffer
  2005. (org-mode)
  2006. (insert
  2007. (org-export-prepare-file-contents file lines ind minlevel))
  2008. (org-export-expand-include-keyword
  2009. (cons (list file lines) included))
  2010. (buffer-string))))))))))))
  2011. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  2012. "Prepare the contents of FILE for inclusion and return them as a string.
  2013. When optional argument LINES is a string specifying a range of
  2014. lines, include only those lines.
  2015. Optional argument IND, when non-nil, is an integer specifying the
  2016. global indentation of returned contents. Since its purpose is to
  2017. allow an included file to stay in the same environment it was
  2018. created \(i.e. a list item), it doesn't apply past the first
  2019. headline encountered.
  2020. Optional argument MINLEVEL, when non-nil, is an integer
  2021. specifying the level that any top-level headline in the included
  2022. file should have."
  2023. (with-temp-buffer
  2024. (insert-file-contents file)
  2025. (when lines
  2026. (let* ((lines (split-string lines "-"))
  2027. (lbeg (string-to-number (car lines)))
  2028. (lend (string-to-number (cadr lines)))
  2029. (beg (if (zerop lbeg) (point-min)
  2030. (goto-char (point-min))
  2031. (forward-line (1- lbeg))
  2032. (point)))
  2033. (end (if (zerop lend) (point-max)
  2034. (goto-char (point-min))
  2035. (forward-line (1- lend))
  2036. (point))))
  2037. (narrow-to-region beg end)))
  2038. ;; Remove blank lines at beginning and end of contents. The logic
  2039. ;; behind that removal is that blank lines around include keyword
  2040. ;; override blank lines in included file.
  2041. (goto-char (point-min))
  2042. (org-skip-whitespace)
  2043. (beginning-of-line)
  2044. (delete-region (point-min) (point))
  2045. (goto-char (point-max))
  2046. (skip-chars-backward " \r\t\n")
  2047. (forward-line)
  2048. (delete-region (point) (point-max))
  2049. ;; If IND is set, preserve indentation of include keyword until
  2050. ;; the first headline encountered.
  2051. (when ind
  2052. (unless (eq major-mode 'org-mode) (org-mode))
  2053. (goto-char (point-min))
  2054. (let ((ind-str (make-string ind ? )))
  2055. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2056. ;; Do not move footnote definitions out of column 0.
  2057. (unless (and (looking-at org-footnote-definition-re)
  2058. (eq (org-element-type (org-element-at-point))
  2059. 'footnote-definition))
  2060. (insert ind-str))
  2061. (forward-line))))
  2062. ;; When MINLEVEL is specified, compute minimal level for headlines
  2063. ;; in the file (CUR-MIN), and remove stars to each headline so
  2064. ;; that headlines with minimal level have a level of MINLEVEL.
  2065. (when minlevel
  2066. (unless (eq major-mode 'org-mode) (org-mode))
  2067. (let ((levels (org-map-entries
  2068. (lambda () (org-reduced-level (org-current-level))))))
  2069. (when levels
  2070. (let ((offset (- minlevel (apply 'min levels))))
  2071. (unless (zerop offset)
  2072. (when org-odd-levels-only (setq offset (* offset 2)))
  2073. ;; Only change stars, don't bother moving whole
  2074. ;; sections.
  2075. (org-map-entries
  2076. (lambda () (if (< offset 0) (delete-char (abs offset))
  2077. (insert (make-string offset ?*))))))))))
  2078. (buffer-string)))
  2079. ;;; Tools For Back-Ends
  2080. ;; A whole set of tools is available to help build new exporters. Any
  2081. ;; function general enough to have its use across many back-ends
  2082. ;; should be added here.
  2083. ;; As of now, functions operating on footnotes, headlines, links,
  2084. ;; macros, references, src-blocks, tables and tables of contents are
  2085. ;; implemented.
  2086. ;;;; For Export Snippets
  2087. ;; Every export snippet is transmitted to the back-end. Though, the
  2088. ;; latter will only retain one type of export-snippet, ignoring
  2089. ;; others, based on the former's target back-end. The function
  2090. ;; `org-export-snippet-backend' returns that back-end for a given
  2091. ;; export-snippet.
  2092. (defun org-export-snippet-backend (export-snippet)
  2093. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  2094. Translation, with `org-export-snippet-translation-alist', is
  2095. applied."
  2096. (let ((back-end (org-element-property :back-end export-snippet)))
  2097. (intern
  2098. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  2099. back-end))))
  2100. ;;;; For Footnotes
  2101. ;; `org-export-collect-footnote-definitions' is a tool to list
  2102. ;; actually used footnotes definitions in the whole parse tree, or in
  2103. ;; an headline, in order to add footnote listings throughout the
  2104. ;; transcoded data.
  2105. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  2106. ;; back-ends, when they need to attach the footnote definition only to
  2107. ;; the first occurrence of the corresponding label.
  2108. ;; `org-export-get-footnote-definition' and
  2109. ;; `org-export-get-footnote-number' provide easier access to
  2110. ;; additional information relative to a footnote reference.
  2111. (defun org-export-collect-footnote-definitions (data info)
  2112. "Return an alist between footnote numbers, labels and definitions.
  2113. DATA is the parse tree from which definitions are collected.
  2114. INFO is the plist used as a communication channel.
  2115. Definitions are sorted by order of references. They either
  2116. appear as Org data \(transcoded with `org-export-data'\) or as
  2117. a secondary string for inlined footnotes \(transcoded with
  2118. `org-export-secondary-string'\). Unreferenced definitions are
  2119. ignored."
  2120. (let (refs)
  2121. ;; Collect seen references in REFS.
  2122. (org-element-map
  2123. data 'footnote-reference
  2124. (lambda (footnote local)
  2125. (when (org-export-footnote-first-reference-p footnote local)
  2126. (list (org-export-get-footnote-number footnote local)
  2127. (org-element-property :label footnote)
  2128. (org-export-get-footnote-definition footnote local))))
  2129. info)))
  2130. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2131. "Non-nil when a footnote reference is the first one for its label.
  2132. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2133. INFO is the plist used as a communication channel."
  2134. (let ((label (org-element-property :label footnote-reference)))
  2135. (or (not label)
  2136. (equal
  2137. footnote-reference
  2138. (org-element-map
  2139. (plist-get info :parse-tree) 'footnote-reference
  2140. (lambda (footnote local)
  2141. (when (string= (org-element-property :label footnote) label)
  2142. footnote))
  2143. info 'first-match)))))
  2144. (defun org-export-get-footnote-definition (footnote-reference info)
  2145. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2146. INFO is the plist used as a communication channel."
  2147. (let ((label (org-element-property :label footnote-reference)))
  2148. (or (org-element-property :inline-definition footnote-reference)
  2149. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2150. (defun org-export-get-footnote-number (footnote info)
  2151. "Return number associated to a footnote.
  2152. FOOTNOTE is either a footnote reference or a footnote definition.
  2153. INFO is the plist used as a communication channel."
  2154. (let ((label (org-element-property :label footnote)) seen-refs)
  2155. (org-element-map
  2156. (plist-get info :parse-tree) 'footnote-reference
  2157. (lambda (fn local)
  2158. (let ((fn-lbl (org-element-property :label fn)))
  2159. (cond
  2160. ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
  2161. ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
  2162. ;; Anonymous footnote: it's always a new one. Also, be sure
  2163. ;; to return nil from the `cond' so `first-match' doesn't
  2164. ;; get us out of the loop.
  2165. ((not fn-lbl) (push 'inline seen-refs) nil)
  2166. ;; Label not seen so far: add it so SEEN-REFS. Again,
  2167. ;; return nil to stay in the loop.
  2168. ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
  2169. info 'first-match)))
  2170. ;;;; For Headlines
  2171. ;; `org-export-get-relative-level' is a shortcut to get headline
  2172. ;; level, relatively to the lower headline level in the parsed tree.
  2173. ;; `org-export-get-headline-number' returns the section number of an
  2174. ;; headline, while `org-export-number-to-roman' allows to convert it
  2175. ;; to roman numbers.
  2176. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2177. ;; `org-export-last-sibling-p' are three useful predicates when it
  2178. ;; comes to fulfill the `:headline-levels' property.
  2179. (defun org-export-get-relative-level (headline info)
  2180. "Return HEADLINE relative level within current parsed tree.
  2181. INFO is a plist holding contextual information."
  2182. (+ (org-element-property :level headline)
  2183. (or (plist-get info :headline-offset) 0)))
  2184. (defun org-export-low-level-p (headline info)
  2185. "Non-nil when HEADLINE is considered as low level.
  2186. INFO is a plist used as a communication channel.
  2187. A low level headlines has a relative level greater than
  2188. `:headline-levels' property value.
  2189. Return value is the difference between HEADLINE relative level
  2190. and the last level being considered as high enough, or nil."
  2191. (let ((limit (plist-get info :headline-levels)))
  2192. (when (wholenump limit)
  2193. (let ((level (org-export-get-relative-level headline info)))
  2194. (and (> level limit) (- level limit))))))
  2195. (defun org-export-get-headline-number (headline info)
  2196. "Return HEADLINE numbering as a list of numbers.
  2197. INFO is a plist holding contextual information."
  2198. (cdr (assoc headline (plist-get info :headline-numbering))))
  2199. (defun org-export-number-to-roman (n)
  2200. "Convert integer N into a roman numeral."
  2201. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2202. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2203. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2204. ( 1 . "I")))
  2205. (res ""))
  2206. (if (<= n 0)
  2207. (number-to-string n)
  2208. (while roman
  2209. (if (>= n (caar roman))
  2210. (setq n (- n (caar roman))
  2211. res (concat res (cdar roman)))
  2212. (pop roman)))
  2213. res)))
  2214. (defun org-export-first-sibling-p (headline info)
  2215. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2216. INFO is the plist used as a communication channel."
  2217. (not (eq (org-element-type (org-export-get-previous-element headline info))
  2218. 'headline)))
  2219. (defun org-export-last-sibling-p (headline info)
  2220. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2221. INFO is the plist used as a communication channel."
  2222. (not (org-export-get-next-element headline info)))
  2223. ;;;; For Links
  2224. ;; `org-export-solidify-link-text' turns a string into a safer version
  2225. ;; for links, replacing most non-standard characters with hyphens.
  2226. ;; `org-export-get-coderef-format' returns an appropriate format
  2227. ;; string for coderefs.
  2228. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2229. ;; provided should be considered as an inline image.
  2230. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2231. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2232. ;; returns an appropriate unique identifier when found, or nil.
  2233. ;; `org-export-resolve-id-link' returns the first headline with
  2234. ;; specified id or custom-id in parse tree, or nil when none was
  2235. ;; found.
  2236. ;; `org-export-resolve-coderef' associates a reference to a line
  2237. ;; number in the element it belongs, or returns the reference itself
  2238. ;; when the element isn't numbered.
  2239. (defun org-export-solidify-link-text (s)
  2240. "Take link text S and make a safe target out of it."
  2241. (save-match-data
  2242. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2243. (defun org-export-get-coderef-format (path desc)
  2244. "Return format string for code reference link.
  2245. PATH is the link path. DESC is its description."
  2246. (save-match-data
  2247. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  2248. (replace-match "%s" t t desc))
  2249. ((string= desc "") "%s")
  2250. (t desc))))
  2251. (defun org-export-inline-image-p (link &optional rules)
  2252. "Non-nil if LINK object points to an inline image.
  2253. Optional argument is a set of RULES defining inline images. It
  2254. is an alist where associations have the following shape:
  2255. \(TYPE . REGEXP)
  2256. Applying a rule means apply REGEXP against LINK's path when its
  2257. type is TYPE. The function will return a non-nil value if any of
  2258. the provided rules is non-nil. The default rule is
  2259. `org-export-default-inline-image-rule'.
  2260. This only applies to links without a description."
  2261. (and (not (org-element-contents link))
  2262. (let ((case-fold-search t)
  2263. (rules (or rules org-export-default-inline-image-rule)))
  2264. (some
  2265. (lambda (rule)
  2266. (and (string= (org-element-property :type link) (car rule))
  2267. (string-match (cdr rule)
  2268. (org-element-property :path link))))
  2269. rules))))
  2270. (defun org-export-resolve-fuzzy-link (link info)
  2271. "Return LINK destination.
  2272. INFO is a plist holding contextual information.
  2273. Return value can be an object, an element, or nil:
  2274. - If LINK path exactly matches any target, return the target
  2275. object.
  2276. - If LINK path exactly matches any headline name, return that
  2277. element. If more than one headline share that name, priority
  2278. will be given to the one with the closest common ancestor, if
  2279. any, or the first one in the parse tree otherwise.
  2280. - Otherwise, return nil.
  2281. Assume LINK type is \"fuzzy\"."
  2282. (let ((path (org-element-property :path link)))
  2283. ;; Link points to a target: return it.
  2284. (or (loop for target in (plist-get info :target-list)
  2285. when (string= (org-element-property :raw-value target) path)
  2286. return target)
  2287. ;; Link either points to an headline or nothing. Try to find
  2288. ;; the source, with priority given to headlines with the closest
  2289. ;; common ancestor. If such candidate is found, return its
  2290. ;; beginning position as an unique identifier, otherwise return
  2291. ;; nil.
  2292. (let ((find-headline
  2293. (function
  2294. ;; Return first headline whose `:raw-value' property
  2295. ;; is NAME in parse tree DATA, or nil.
  2296. (lambda (name data)
  2297. (org-element-map
  2298. data 'headline
  2299. (lambda (headline local)
  2300. (when (string=
  2301. (org-element-property :raw-value headline)
  2302. name)
  2303. headline))
  2304. info 'first-match)))))
  2305. ;; Search among headlines sharing an ancestor with link,
  2306. ;; from closest to farthest.
  2307. (or (catch 'exit
  2308. (mapc
  2309. (lambda (parent)
  2310. (when (eq (org-element-type parent) 'headline)
  2311. (let ((foundp (funcall find-headline path parent)))
  2312. (when foundp (throw 'exit foundp)))))
  2313. (org-export-get-genealogy link info)) nil)
  2314. ;; No match with a common ancestor: try the full parse-tree.
  2315. (funcall find-headline path (plist-get info :parse-tree)))))))
  2316. (defun org-export-resolve-id-link (link info)
  2317. "Return headline referenced as LINK destination.
  2318. INFO is a plist used as a communication channel.
  2319. Return value can be an headline element or nil. Assume LINK type
  2320. is either \"id\" or \"custom-id\"."
  2321. (let ((id (org-element-property :path link)))
  2322. (org-element-map
  2323. (plist-get info :parse-tree) 'headline
  2324. (lambda (headline local)
  2325. (when (or (string= (org-element-property :id headline) id)
  2326. (string= (org-element-property :custom-id headline) id))
  2327. headline))
  2328. info 'first-match)))
  2329. (defun org-export-resolve-ref-link (link info)
  2330. "Return element referenced as LINK destination.
  2331. INFO is a plist used as a communication channel.
  2332. Assume LINK type is \"ref\" and. Return value is the first
  2333. element whose `:name' property matches LINK's `:path', or nil."
  2334. (let ((name (org-element-property :path link)))
  2335. (org-element-map
  2336. (plist-get info :parse-tree) org-element-all-elements
  2337. (lambda (el local)
  2338. (when (string= (org-element-property :name el) name) el))
  2339. info 'first-match)))
  2340. (defun org-export-resolve-coderef (ref info)
  2341. "Resolve a code reference REF.
  2342. INFO is a plist used as a communication channel.
  2343. Return associated line number in source code, or REF itself,
  2344. depending on src-block or example element's switches."
  2345. (org-element-map
  2346. (plist-get info :parse-tree) '(src-block example)
  2347. (lambda (el local)
  2348. (let ((switches (or (org-element-property :switches el) "")))
  2349. (with-temp-buffer
  2350. (insert (org-trim (org-element-property :value el)))
  2351. ;; Build reference regexp.
  2352. (let* ((label
  2353. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2354. (match-string 1 switches))
  2355. org-coderef-label-format))
  2356. (ref-re
  2357. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2358. (replace-regexp-in-string "%s" ref label nil t))))
  2359. ;; Element containing REF is found. Only associate REF to
  2360. ;; a line number if element has "+n" or "-n" and "-k" or
  2361. ;; "-r" as switches. When it has "+n", count accumulated
  2362. ;; locs before, too.
  2363. (when (re-search-backward ref-re nil t)
  2364. (cond
  2365. ((not (string-match "-[kr]\\>" switches)) ref)
  2366. ((string-match "-n\\>" switches) (line-number-at-pos))
  2367. ((string-match "\\+n\\>" switches)
  2368. (+ (org-export-get-loc el local) (line-number-at-pos)))
  2369. (t ref)))))))
  2370. info 'first-match))
  2371. ;;;; For Macros
  2372. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2373. (defun org-export-expand-macro (macro info)
  2374. "Expand MACRO and return it as a string.
  2375. INFO is a plist holding export options."
  2376. (let* ((key (org-element-property :key macro))
  2377. (args (org-element-property :args macro))
  2378. ;; User's macros are stored in the communication channel with
  2379. ;; a ":macro-" prefix. If it's a string leave it as-is.
  2380. ;; Otherwise, it's a secondary string that needs to be
  2381. ;; expanded recursively.
  2382. (value
  2383. (let ((val (plist-get info (intern (format ":macro-%s" key)))))
  2384. (if (stringp val) val
  2385. (org-export-secondary-string
  2386. val (plist-get info :back-end) info)))))
  2387. ;; Replace arguments in VALUE.
  2388. (let ((s 0) n)
  2389. (while (string-match "\\$\\([0-9]+\\)" value s)
  2390. (setq s (1+ (match-beginning 0))
  2391. n (string-to-number (match-string 1 value)))
  2392. (and (>= (length args) n)
  2393. (setq value (replace-match (nth (1- n) args) t t value)))))
  2394. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2395. (when (string-match "\\`(eval\\>" value)
  2396. (setq value (eval (read value))))
  2397. ;; Return string.
  2398. (format "%s" (or value ""))))
  2399. ;;;; For References
  2400. ;; `org-export-get-ordinal' associates a sequence number to any object
  2401. ;; or element.
  2402. (defun org-export-get-ordinal
  2403. (element info &optional types within-section predicate)
  2404. "Return ordinal number of an element or object.
  2405. ELEMENT is the element or object considered. INFO is the plist
  2406. used as a communication channel.
  2407. Optional argument TYPES, when non-nil, is a list of element or
  2408. object types, as symbols, that should also be counted in.
  2409. Otherwise, only provided element's type is considered.
  2410. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2411. to the section containing ELEMENT.
  2412. Optional argument PREDICATE is a function returning a non-nil
  2413. value if the current element or object should be counted in. It
  2414. accepts one argument: the element or object being considered.
  2415. This argument allows to count only a certain type of objects,
  2416. like inline images, which are a subset of links \(in that case,
  2417. `org-export-inline-image-p' might be an useful predicate\)."
  2418. (let ((counter 0)
  2419. ;; Determine if search should apply to current section, in
  2420. ;; which case it should be retrieved first, or to full parse
  2421. ;; tree. As a special case, an element or object without
  2422. ;; a parent headline will also trigger a full search,
  2423. ;; notwithstanding WITHIN-SECTION value.
  2424. (data
  2425. (if (not within-section) (plist-get info :parse-tree)
  2426. (or (org-export-get-parent-headline element info)
  2427. (plist-get info :parse-tree)))))
  2428. ;; Increment counter until ELEMENT is found again.
  2429. (org-element-map
  2430. data (or types (org-element-type element))
  2431. (lambda (el local)
  2432. (cond
  2433. ((equal element el) (1+ counter))
  2434. ((not predicate) (incf counter) nil)
  2435. ((funcall predicate el) (incf counter) nil)))
  2436. info 'first-match)))
  2437. ;;;; For Src-Blocks
  2438. ;; `org-export-get-loc' counts number of code lines accumulated in
  2439. ;; src-block or example-block elements with a "+n" switch until
  2440. ;; a given element, excluded. Note: "-n" switches reset that count.
  2441. ;; `org-export-handle-code' takes care of line numbering and reference
  2442. ;; cleaning in source code, when appropriate.
  2443. (defun org-export-get-loc (element info)
  2444. "Return accumulated lines of code up to ELEMENT.
  2445. INFO is the plist used as a communication channel.
  2446. ELEMENT is excluded from count."
  2447. (let ((loc 0))
  2448. (org-element-map
  2449. (plist-get info :parse-tree)
  2450. `(src-block example-block ,(org-element-type element))
  2451. (lambda (el local)
  2452. (cond
  2453. ;; ELEMENT is reached: Quit the loop.
  2454. ((equal el element) t)
  2455. ;; Only count lines from src-block and example-block elements
  2456. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2457. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  2458. ((let ((switches (org-element-property :switches el)))
  2459. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2460. ;; Accumulate locs or reset them.
  2461. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2462. (lines (org-count-lines
  2463. (org-trim (org-element-property :value el)))))
  2464. (setq loc (if accumulatep lines (+ loc lines))))))
  2465. ;; Return nil to stay in the loop.
  2466. nil)))
  2467. info 'first-match)
  2468. ;; Return value.
  2469. loc))
  2470. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2471. "Handle line numbers and code references in ELEMENT.
  2472. ELEMENT has either a `src-block' an `example-block' type. INFO
  2473. is a plist used as a communication channel.
  2474. If optional argument NUM-FMT is a string, it will be used as
  2475. a format string for numbers at beginning of each line.
  2476. If optional argument REF-FMT is a string, it will be used as
  2477. a format string for each line of code containing a reference.
  2478. When optional argument DELAYED is non-nil, `org-loc' and
  2479. `org-coderef' properties, set to an adequate value, are applied
  2480. to, respectively, numbered lines and lines with a reference. No
  2481. line numbering is done and all references are stripped from the
  2482. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2483. in that situation.
  2484. Return new code as a string."
  2485. (let* ((switches (or (org-element-property :switches element) ""))
  2486. (code (org-element-property :value element))
  2487. (numberp (string-match "[-+]n\\>" switches))
  2488. (accumulatep (string-match "\\+n\\>" switches))
  2489. ;; Initialize loc counter when any kind of numbering is
  2490. ;; active.
  2491. (total-LOC (cond
  2492. (accumulatep (org-export-get-loc element info))
  2493. (numberp 0)))
  2494. ;; Get code and clean it. Remove blank lines at its
  2495. ;; beginning and end. Also remove protective commas.
  2496. (preserve-indent-p (or org-src-preserve-indentation
  2497. (string-match "-i\\>" switches)))
  2498. (replace-labels (when (string-match "-r\\>" switches)
  2499. (if (string-match "-k\\>" switches) 'keep t)))
  2500. (code (let ((c (replace-regexp-in-string
  2501. "\\`\\([ \t]*\n\\)+" ""
  2502. (replace-regexp-in-string
  2503. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2504. ;; If appropriate, remove global indentation.
  2505. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2506. ;; Free up the protected lines. Note: Org blocks
  2507. ;; have commas at the beginning or every line.
  2508. (if (string=
  2509. (or (org-element-property :language element) "")
  2510. "org")
  2511. (replace-regexp-in-string "^," "" c)
  2512. (replace-regexp-in-string
  2513. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2514. ;; Split code to process it line by line.
  2515. (code-lines (org-split-string code "\n"))
  2516. ;; If numbering is active, ensure line numbers will be
  2517. ;; correctly padded before applying the format string.
  2518. (num-fmt
  2519. (when (and (not delayed) numberp)
  2520. (format (if (stringp num-fmt) num-fmt "%s: ")
  2521. (format "%%%ds"
  2522. (length (number-to-string
  2523. (+ (length code-lines) total-LOC)))))))
  2524. ;; Get format used for references.
  2525. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2526. (match-string 1 switches))
  2527. org-coderef-label-format))
  2528. ;; Build a regexp matching a loc with a reference.
  2529. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2530. (replace-regexp-in-string
  2531. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2532. (org-element-normalize-string
  2533. (mapconcat
  2534. (lambda (loc)
  2535. ;; Maybe add line number to current line of code (LOC).
  2536. (when numberp
  2537. (incf total-LOC)
  2538. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2539. (concat (format num-fmt total-LOC) loc))))
  2540. ;; Take action if at a ref line.
  2541. (when (string-match with-ref-re loc)
  2542. (let ((ref (match-string 3 loc)))
  2543. (setq loc
  2544. ;; Option "-r" without "-k" removes labels.
  2545. ;; A non-nil DELAYED removes labels unconditionally.
  2546. (if (or delayed
  2547. (and replace-labels (not (eq replace-labels 'keep))))
  2548. (replace-match "" nil nil loc 1)
  2549. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2550. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2551. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2552. ;; If REF-FMT is defined, apply it to current LOC.
  2553. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2554. ;; Return updated LOC for concatenation.
  2555. loc)
  2556. code-lines "\n"))))
  2557. ;;;; For Tables
  2558. ;; `org-export-table-format-info' extracts formatting information
  2559. ;; (alignment, column groups and presence of a special column) from
  2560. ;; a raw table and returns it as a property list.
  2561. ;;
  2562. ;; `org-export-clean-table' cleans the raw table from any Org
  2563. ;; table-specific syntax.
  2564. (defun org-export-table-format-info (table)
  2565. "Extract info from TABLE.
  2566. Return a plist whose properties and values are:
  2567. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2568. `:column-groups' vector of symbols among `start', `end', `start-end',
  2569. `:row-groups' list of integers representing row groups.
  2570. `:special-column-p' non-nil if table has a special column.
  2571. `:width' vector of integers representing desired width of
  2572. current column, or nil."
  2573. (with-temp-buffer
  2574. (insert table)
  2575. (goto-char 1)
  2576. (org-table-align)
  2577. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2578. org-table-last-alignment)))
  2579. (width (make-vector (length org-table-last-alignment) nil))
  2580. (colgroups (make-vector (length org-table-last-alignment) nil))
  2581. (row-group 0)
  2582. (rowgroups)
  2583. (special-column-p 'empty))
  2584. (mapc (lambda (row)
  2585. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2586. (incf row-group)
  2587. ;; Determine if a special column is present by looking
  2588. ;; for special markers in the first column. More
  2589. ;; accurately, the first column is considered special
  2590. ;; if it only contains special markers and, maybe,
  2591. ;; empty cells.
  2592. (setq special-column-p
  2593. (cond
  2594. ((not special-column-p) nil)
  2595. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2596. 'special)
  2597. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2598. (cond
  2599. ;; Read forced alignment and width information, if any,
  2600. ;; and determine final alignment for the table.
  2601. ((org-table-cookie-line-p row)
  2602. (let ((col 0))
  2603. (mapc (lambda (field)
  2604. (when (string-match
  2605. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2606. (let ((align-data (match-string 1 field)))
  2607. (when align-data (aset align col align-data)))
  2608. (let ((w-data (match-string 2 field)))
  2609. (when w-data
  2610. (aset width col (string-to-number w-data)))))
  2611. (incf col))
  2612. (org-split-string row "[ \t]*|[ \t]*"))))
  2613. ;; Read column groups information.
  2614. ((org-table-colgroup-line-p row)
  2615. (let ((col 0))
  2616. (mapc (lambda (field)
  2617. (aset colgroups col
  2618. (cond ((string= "<" field) 'start)
  2619. ((string= ">" field) 'end)
  2620. ((string= "<>" field) 'start-end)))
  2621. (incf col))
  2622. (org-split-string row "[ \t]*|[ \t]*"))))
  2623. ;; Contents line.
  2624. (t (push row-group rowgroups))))
  2625. (org-split-string table "\n"))
  2626. ;; Return plist.
  2627. (list :alignment align
  2628. :column-groups colgroups
  2629. :row-groups (reverse rowgroups)
  2630. :special-column-p (eq special-column-p 'special)
  2631. :width width))))
  2632. (defun org-export-clean-table (table specialp)
  2633. "Clean string TABLE from its formatting elements.
  2634. Remove any row containing column groups or formatting cookies and
  2635. rows starting with a special marker. If SPECIALP is non-nil,
  2636. assume the table contains a special formatting column and remove
  2637. it also."
  2638. (let ((rows (org-split-string table "\n")))
  2639. (mapconcat 'identity
  2640. (delq nil
  2641. (mapcar
  2642. (lambda (row)
  2643. (cond
  2644. ((org-table-colgroup-line-p row) nil)
  2645. ((org-table-cookie-line-p row) nil)
  2646. ;; Ignore rows starting with a special marker.
  2647. ((string-match "^[ \t]*| *[!_^/$] *|" row) nil)
  2648. ;; Remove special column.
  2649. ((and specialp
  2650. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2651. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2652. (replace-match "\\1|" t nil row))
  2653. (t row)))
  2654. rows))
  2655. "\n")))
  2656. ;;;; For Tables Of Contents
  2657. ;; `org-export-collect-headlines' builds a list of all exportable
  2658. ;; headline elements, maybe limited to a certain depth. One can then
  2659. ;; easily parse it and transcode it.
  2660. ;; Building lists of tables, figures or listings is quite similar.
  2661. ;; Once the generic function `org-export-collect-elements' is defined,
  2662. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2663. ;; `org-export-collect-listings' can be derived from it.
  2664. (defun org-export-collect-headlines (info &optional n)
  2665. "Collect headlines in order to build a table of contents.
  2666. INFO is a plist used as a communication channel.
  2667. When non-nil, optional argument N must be an integer. It
  2668. specifies the depth of the table of contents.
  2669. Return a list of all exportable headlines as parsed elements."
  2670. (org-element-map
  2671. (plist-get info :parse-tree)
  2672. 'headline
  2673. (lambda (headline local)
  2674. ;; Strip contents from HEADLINE.
  2675. (let ((relative-level (org-export-get-relative-level headline local)))
  2676. (unless (and n (> relative-level n)) headline)))
  2677. info))
  2678. (defun org-export-collect-elements (type info &optional predicate)
  2679. "Collect referenceable elements of a determined type.
  2680. TYPE can be a symbol or a list of symbols specifying element
  2681. types to search. Only elements with a caption or a name are
  2682. collected.
  2683. INFO is a plist used as a communication channel.
  2684. When non-nil, optional argument PREDICATE is a function accepting
  2685. one argument, an element of type TYPE. It returns a non-nil
  2686. value when that element should be collected.
  2687. Return a list of all elements found, in order of appearance."
  2688. (org-element-map
  2689. (plist-get info :parse-tree) type
  2690. (lambda (element local)
  2691. (and (or (org-element-property :caption element)
  2692. (org-element-property :name element))
  2693. (or (not predicate) (funcall predicate element))
  2694. element)) info))
  2695. (defun org-export-collect-tables (info)
  2696. "Build a list of tables.
  2697. INFO is a plist used as a communication channel.
  2698. Return a list of table elements with a caption or a name
  2699. affiliated keyword."
  2700. (org-export-collect-elements 'table info))
  2701. (defun org-export-collect-figures (info predicate)
  2702. "Build a list of figures.
  2703. INFO is a plist used as a communication channel. PREDICATE is
  2704. a function which accepts one argument: a paragraph element and
  2705. whose return value is non-nil when that element should be
  2706. collected.
  2707. A figure is a paragraph type element, with a caption or a name,
  2708. verifying PREDICATE. The latter has to be provided since
  2709. a \"figure\" is a vague concept that may depend on back-end.
  2710. Return a list of elements recognized as figures."
  2711. (org-export-collect-elements 'paragraph info predicate))
  2712. (defun org-export-collect-listings (info)
  2713. "Build a list of src blocks.
  2714. INFO is a plist used as a communication channel.
  2715. Return a list of src-block elements with a caption or a name
  2716. affiliated keyword."
  2717. (org-export-collect-elements 'src-block info))
  2718. ;;;; Topology
  2719. ;; Here are various functions to retrieve information about the
  2720. ;; neighbourhood of a given element or object. Neighbours of interest
  2721. ;; are direct parent (`org-export-get-parent'), parent headline
  2722. ;; (`org-export-get-parent-headline'), parent paragraph
  2723. ;; (`org-export-get-parent-paragraph'), previous element or object
  2724. ;; (`org-export-get-previous-element') and next element or object
  2725. ;; (`org-export-get-next-element').
  2726. ;; All of these functions are just a specific use of the more generic
  2727. ;; `org-export-get-genealogy', which returns the genealogy relative to
  2728. ;; the element or object.
  2729. (defun org-export-get-genealogy (blob info)
  2730. "Return genealogy relative to a given element or object.
  2731. BLOB is the element or object being considered. INFO is a plist
  2732. used as a communication channel."
  2733. (let* ((end (org-element-property :end blob))
  2734. (walk-data
  2735. (lambda (data genealogy)
  2736. (mapc
  2737. (lambda (el)
  2738. (cond
  2739. ((stringp el))
  2740. ((equal el blob) (throw 'exit genealogy))
  2741. ((>= (org-element-property :end el) end)
  2742. (funcall walk-data el (cons el genealogy)))))
  2743. (org-element-contents data)))))
  2744. (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
  2745. (defun org-export-get-parent (blob info)
  2746. "Return BLOB parent or nil.
  2747. BLOB is the element or object considered. INFO is a plist used
  2748. as a communication channel."
  2749. (car (org-export-get-genealogy blob info)))
  2750. (defun org-export-get-parent-headline (blob info)
  2751. "Return closest parent headline or nil.
  2752. BLOB is the element or object being considered. INFO is a plist
  2753. used as a communication channel."
  2754. (catch 'exit
  2755. (mapc
  2756. (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
  2757. (org-export-get-genealogy blob info))
  2758. nil))
  2759. (defun org-export-get-parent-paragraph (object info)
  2760. "Return parent paragraph or nil.
  2761. INFO is a plist used as a communication channel.
  2762. Optional argument OBJECT, when provided, is the object to consider.
  2763. Otherwise, return the paragraph containing current object.
  2764. This is useful for objects, which share attributes with the
  2765. paragraph containing them."
  2766. (catch 'exit
  2767. (mapc
  2768. (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
  2769. (org-export-get-genealogy object info))
  2770. nil))
  2771. (defun org-export-get-previous-element (blob info)
  2772. "Return previous element or object.
  2773. BLOB is an element or object. INFO is a plist used as
  2774. a communication channel.
  2775. Return previous element or object, a string, or nil."
  2776. (let ((parent (org-export-get-parent blob info)))
  2777. (cadr (member blob (reverse (org-element-contents parent))))))
  2778. (defun org-export-get-next-element (blob info)
  2779. "Return next element or object.
  2780. BLOB is an element or object. INFO is a plist used as
  2781. a communication channel.
  2782. Return next element or object, a string, or nil."
  2783. (let ((parent (org-export-get-parent blob info)))
  2784. (cadr (member blob (org-element-contents parent)))))
  2785. ;;; The Dispatcher
  2786. ;; `org-export-dispatch' is the standard interactive way to start an
  2787. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2788. ;; for its interface. Most commons back-ends should have an entry in
  2789. ;; it.
  2790. (defun org-export-dispatch ()
  2791. "Export dispatcher for Org mode.
  2792. It provides an access to common export related tasks in a buffer.
  2793. Its interface comes in two flavours: standard and expert. While
  2794. both share the same set of bindings, only the former displays the
  2795. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2796. to switch to one or the other.
  2797. Return an error if key pressed has no associated command."
  2798. (interactive)
  2799. (let* ((input (org-export-dispatch-ui
  2800. (if (listp org-export-initial-scope) org-export-initial-scope
  2801. (list org-export-initial-scope))
  2802. org-export-dispatch-use-expert-ui))
  2803. (raw-key (car input))
  2804. (optns (cdr input)))
  2805. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2806. ;; depending on user's key pressed.
  2807. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2808. ;; Allow to quit with "q" key.
  2809. (?q nil)
  2810. ;; Export with `e-ascii' back-end.
  2811. ((?A ?N ?U)
  2812. (let ((outbuf
  2813. (org-export-to-buffer
  2814. 'e-ascii "*Org E-ASCII Export*"
  2815. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2816. `(:ascii-charset
  2817. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  2818. (with-current-buffer outbuf (text-mode))
  2819. (when org-export-show-temporary-export-buffer
  2820. (switch-to-buffer-other-window outbuf))))
  2821. ((?a ?n ?u)
  2822. (org-e-ascii-export-to-ascii
  2823. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2824. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  2825. ;; Export with `e-latex' back-end.
  2826. (?L
  2827. (let ((outbuf
  2828. (org-export-to-buffer
  2829. 'e-latex "*Org E-LaTeX Export*"
  2830. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2831. (with-current-buffer outbuf (latex-mode))
  2832. (when org-export-show-temporary-export-buffer
  2833. (switch-to-buffer-other-window outbuf))))
  2834. (?l (org-e-latex-export-to-latex
  2835. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2836. (?p (org-e-latex-export-to-pdf
  2837. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2838. (?d (org-open-file
  2839. (org-e-latex-export-to-pdf
  2840. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2841. ;; Export with `e-html' back-end.
  2842. (?H
  2843. (let ((outbuf
  2844. (org-export-to-buffer
  2845. 'e-html "*Org E-HTML Export*"
  2846. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2847. ;; set major mode
  2848. (with-current-buffer outbuf
  2849. (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
  2850. (when org-export-show-temporary-export-buffer
  2851. (switch-to-buffer-other-window outbuf))))
  2852. (?h (org-e-html-export-to-html
  2853. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2854. (?b (org-open-file
  2855. (org-e-html-export-to-html
  2856. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2857. ;; Export with `e-odt' back-end.
  2858. (?o (org-e-odt-export-to-odt
  2859. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2860. (?O (org-open-file
  2861. (org-e-odt-export-to-odt
  2862. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2863. ;; Publishing facilities
  2864. (?F (org-e-publish-current-file (memq 'force optns)))
  2865. (?P (org-e-publish-current-project (memq 'force optns)))
  2866. (?X (let ((project
  2867. (assoc (org-icompleting-read
  2868. "Publish project: " org-e-publish-project-alist nil t)
  2869. org-e-publish-project-alist)))
  2870. (org-e-publish project (memq 'force optns))))
  2871. (?E (org-e-publish-all (memq 'force optns)))
  2872. ;; Undefined command.
  2873. (t (error "No command associated with key %s"
  2874. (char-to-string raw-key))))))
  2875. (defun org-export-dispatch-ui (options expertp)
  2876. "Handle interface for `org-export-dispatch'.
  2877. OPTIONS is a list containing current interactive options set for
  2878. export. It can contain any of the following symbols:
  2879. `body' toggles a body-only export
  2880. `subtree' restricts export to current subtree
  2881. `visible' restricts export to visible part of buffer.
  2882. `force' force publishing files.
  2883. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2884. buffer is provided, but indications about currently active
  2885. options are given in the prompt. Moreover, \[?] allows to switch
  2886. back to standard interface.
  2887. Return value is a list with key pressed as CAR and a list of
  2888. final interactive export options as CDR."
  2889. (let ((help
  2890. (format "---- (Options) -------------------------------------------
  2891. \[1] Body only: %s [2] Export scope: %s
  2892. \[3] Visible only: %s [4] Force publishing: %s
  2893. ----(ASCII/Latin-1/UTF-8 Export)--------------------------
  2894. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  2895. ----(HTML Export)-----------------------------------------
  2896. \[h] to HTML file [b] ... and open it
  2897. \[H] to temporary buffer
  2898. ----(LaTeX Export)----------------------------------------
  2899. \[l] to TEX file [L] to temporary buffer
  2900. \[p] to PDF file [d] ... and open it
  2901. ----(ODF Export)------------------------------------------
  2902. \[o] to ODT file [O] ... and open it
  2903. ----(Publish)---------------------------------------------
  2904. \[F] current file [P] current project
  2905. \[X] a project [E] every project"
  2906. (if (memq 'body options) "On " "Off")
  2907. (if (memq 'subtree options) "Subtree" "Buffer ")
  2908. (if (memq 'visible options) "On " "Off")
  2909. (if (memq 'force options) "On " "Off")))
  2910. (standard-prompt "Export command: ")
  2911. (expert-prompt (format "Export command (%s%s%s%s): "
  2912. (if (memq 'body options) "b" "-")
  2913. (if (memq 'subtree options) "s" "-")
  2914. (if (memq 'visible options) "v" "-")
  2915. (if (memq 'force options) "f" "-")))
  2916. (handle-keypress
  2917. (function
  2918. ;; Read a character from command input, toggling interactive
  2919. ;; options when applicable. PROMPT is the displayed prompt,
  2920. ;; as a string.
  2921. (lambda (prompt)
  2922. (let ((key (read-char-exclusive prompt)))
  2923. (cond
  2924. ;; Ignore non-standard characters (i.e. "M-a").
  2925. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  2926. ;; Help key: Switch back to standard interface if
  2927. ;; expert UI was active.
  2928. ((eq key ??) (org-export-dispatch-ui options nil))
  2929. ;; Toggle export options.
  2930. ((memq key '(?1 ?2 ?3 ?4))
  2931. (org-export-dispatch-ui
  2932. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  2933. (?4 'force))))
  2934. (if (memq option options) (remq option options)
  2935. (cons option options)))
  2936. expertp))
  2937. ;; Action selected: Send key and options back to
  2938. ;; `org-export-dispatch'.
  2939. (t (cons key options))))))))
  2940. ;; With expert UI, just read key with a fancy prompt. In standard
  2941. ;; UI, display an intrusive help buffer.
  2942. (if expertp (funcall handle-keypress expert-prompt)
  2943. (save-window-excursion
  2944. (delete-other-windows)
  2945. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  2946. (org-fit-window-to-buffer
  2947. (get-buffer-window "*Org Export/Publishing Help*"))
  2948. (funcall handle-keypress standard-prompt)))))
  2949. (provide 'org-export)
  2950. ;;; org-export.el ends here