org-export.el 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499
  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'. Do not overwrite any user ignore that might
  1079. ;; have been done during parse tree filtering.
  1080. (setq info
  1081. (plist-put info
  1082. :ignore-list
  1083. (append (org-export-populate-ignore-list data info)
  1084. (plist-get info :ignore-list))))
  1085. ;; Then compute `:headline-offset' in order to be able to use
  1086. ;; `org-export-get-relative-level'.
  1087. (setq info
  1088. (plist-put info
  1089. :headline-offset (- 1 (org-export-get-min-level data info))))
  1090. ;; Now, properties order doesn't matter: get the rest of the tree
  1091. ;; properties.
  1092. (nconc
  1093. `(:parse-tree
  1094. ,data
  1095. :target-list
  1096. ,(org-element-map data 'target 'identity info)
  1097. :headline-numbering ,(org-export-collect-headline-numbering data info)
  1098. :back-end ,backend)
  1099. info))
  1100. (defun org-export-get-min-level (data options)
  1101. "Return minimum exportable headline's level in DATA.
  1102. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1103. OPTIONS is a plist holding export options."
  1104. (catch 'exit
  1105. (let ((min-level 10000))
  1106. (mapc
  1107. (lambda (blob)
  1108. (when (and (eq (org-element-type blob) 'headline)
  1109. (not (member blob (plist-get options :ignore-list))))
  1110. (setq min-level
  1111. (min (org-element-property :level blob) min-level)))
  1112. (when (= min-level 1) (throw 'exit 1)))
  1113. (org-element-contents data))
  1114. ;; If no headline was found, for the sake of consistency, set
  1115. ;; minimum level to 1 nonetheless.
  1116. (if (= min-level 10000) 1 min-level))))
  1117. (defun org-export-collect-headline-numbering (data options)
  1118. "Return numbering of all exportable headlines in a parse tree.
  1119. DATA is the parse tree. OPTIONS is the plist holding export
  1120. options.
  1121. Return an alist whose key is an headline and value is its
  1122. associated numbering \(in the shape of a list of numbers\)."
  1123. (let ((numbering (make-vector org-export-max-depth 0)))
  1124. (org-element-map
  1125. data
  1126. 'headline
  1127. (lambda (headline)
  1128. (let ((relative-level
  1129. (1- (org-export-get-relative-level headline options))))
  1130. (cons
  1131. headline
  1132. (loop for n across numbering
  1133. for idx from 0 to org-export-max-depth
  1134. when (< idx relative-level) collect n
  1135. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1136. when (> idx relative-level) do (aset numbering idx 0)))))
  1137. options)))
  1138. (defun org-export-populate-ignore-list (data options)
  1139. "Return list of elements and objects to ignore during export.
  1140. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1141. export options.
  1142. Return elements or objects to ignore as a list."
  1143. (let (ignore
  1144. (walk-data
  1145. (function
  1146. (lambda (data options selected)
  1147. ;; Collect ignored elements or objects into IGNORE-LIST.
  1148. (mapc
  1149. (lambda (el)
  1150. (if (org-export--skip-p el options selected) (push el ignore)
  1151. (let ((type (org-element-type el)))
  1152. (if (and (eq (plist-get info :with-archived-trees) 'headline)
  1153. (eq (org-element-type el) 'headline)
  1154. (org-element-property :archivedp el))
  1155. ;; If headline is archived but tree below has
  1156. ;; to be skipped, add it to ignore list.
  1157. (mapc (lambda (e) (push e ignore))
  1158. (org-element-contents el))
  1159. ;; Move into recursive objects/elements.
  1160. (when (or (eq type 'org-data)
  1161. (memq type org-element-greater-elements)
  1162. (memq type org-element-recursive-objects)
  1163. (eq type 'paragraph))
  1164. (funcall walk-data el options selected))))))
  1165. (org-element-contents data))))))
  1166. ;; Main call. First find trees containing a select tag, if any.
  1167. (funcall walk-data data options (org-export--selected-trees data options))
  1168. ;; Return value.
  1169. ignore))
  1170. (defun org-export--selected-trees (data info)
  1171. "Return list of headlines containing a select tag in their tree.
  1172. DATA is parsed data as returned by `org-element-parse-buffer'.
  1173. INFO is a plist holding export options."
  1174. (let (selected-trees
  1175. (walk-data
  1176. (function
  1177. (lambda (data genealogy)
  1178. (case (org-element-type data)
  1179. (org-data
  1180. (funcall walk-data (org-element-contents data) genealogy))
  1181. (headline
  1182. (let ((tags (org-element-property :tags headline)))
  1183. (if (and tags
  1184. (loop for tag in (plist-get info :select-tags)
  1185. thereis (string-match
  1186. (format ":%s:" tag) tags)))
  1187. ;; When a select tag is found, mark as acceptable
  1188. ;; full genealogy and every headline within the
  1189. ;; tree.
  1190. (setq selected-trees
  1191. (append
  1192. (cons data genealogy)
  1193. (org-element-map data 'headline 'identity)
  1194. selected-trees))
  1195. ;; Else, continue searching in tree, recursively.
  1196. (funcall walk-data data (cons data genealogy))))))))))
  1197. (funcall walk-data data nil) selected-trees))
  1198. (defun org-export--skip-p (blob options select-tags)
  1199. "Non-nil when element or object BLOB should be skipped during export.
  1200. OPTIONS is the plist holding export options."
  1201. (case (org-element-type blob)
  1202. ;; Check headline.
  1203. (headline
  1204. (let ((with-tasks (plist-get options :with-tasks))
  1205. (todo (org-element-property :todo-keyword blob))
  1206. (todo-type (org-element-property :todo-type blob))
  1207. (archived (plist-get options :with-archived-trees))
  1208. (tag-list (let ((tags (org-element-property :tags blob)))
  1209. (and tags (org-split-string tags ":")))))
  1210. (or
  1211. ;; Ignore subtrees with an exclude tag.
  1212. (loop for k in (plist-get options :exclude-tags)
  1213. thereis (member k tag-list))
  1214. ;; Ignore subtrees without a select tag, when such tag is
  1215. ;; found in the buffer.
  1216. (member blob select-tags)
  1217. ;; Ignore commented sub-trees.
  1218. (org-element-property :commentedp blob)
  1219. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1220. (and (not archived) (org-element-property :archivedp blob))
  1221. ;; Ignore tasks, if specified by `:with-tasks' property.
  1222. (and todo
  1223. (or (not with-tasks)
  1224. (and (memq with-tasks '(todo done))
  1225. (not (eq todo-type with-tasks)))
  1226. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1227. ;; Check time-stamp.
  1228. (time-stamp (not (plist-get options :with-timestamps)))
  1229. ;; Check drawer.
  1230. (drawer
  1231. (or (not (plist-get options :with-drawers))
  1232. (and (consp (plist-get options :with-drawers))
  1233. (not (member (org-element-property :drawer-name blob)
  1234. (plist-get options :with-drawers))))))))
  1235. ;;; The Transcoder
  1236. ;; This function reads Org data (obtained with, i.e.
  1237. ;; `org-element-parse-buffer') and transcodes it into a specified
  1238. ;; back-end output. It takes care of updating local properties,
  1239. ;; filtering out elements or objects according to export options and
  1240. ;; organizing the output blank lines and white space are preserved.
  1241. ;; Though, this function is inapropriate for secondary strings, which
  1242. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1243. ;; `org-export-secondary-string' is provided for that specific task.
  1244. ;; Internally, three functions handle the filtering of objects and
  1245. ;; elements during the export. In particular,
  1246. ;; `org-export-ignore-element' mark an element or object so future
  1247. ;; parse tree traversals skip it, `org-export-interpret-p' tells which
  1248. ;; elements or objects should be seen as real Org syntax and
  1249. ;; `org-export-expand' transforms the others back into their original
  1250. ;; shape.
  1251. (defun org-export-data (data backend info)
  1252. "Convert DATA to a string into BACKEND format.
  1253. DATA is a nested list as returned by `org-element-parse-buffer'.
  1254. BACKEND is a symbol among supported exporters.
  1255. INFO is a plist holding export options and also used as
  1256. a communication channel between elements when walking the nested
  1257. list. See `org-export-update-info' function for more
  1258. details.
  1259. Return transcoded string."
  1260. (mapconcat
  1261. ;; BLOB can be an element, an object, a string, or nil.
  1262. (lambda (blob)
  1263. (cond
  1264. ((not blob) nil)
  1265. ;; BLOB is a string. Check if the optional transcoder for plain
  1266. ;; text exists, and call it in that case. Otherwise, simply
  1267. ;; return string. Also update INFO and call
  1268. ;; `org-export-filter-plain-text-functions'.
  1269. ((stringp blob)
  1270. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1271. (org-export-filter-apply-functions
  1272. (plist-get info :filter-plain-text)
  1273. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1274. backend info)))
  1275. ;; BLOB is an element or an object.
  1276. (t
  1277. (let* ((type (org-element-type blob))
  1278. ;; 1. Determine the appropriate TRANSCODER.
  1279. (transcoder
  1280. (cond
  1281. ;; 1.0 A full Org document is inserted.
  1282. ((eq type 'org-data) 'identity)
  1283. ;; 1.1. BLOB should be ignored.
  1284. ((member blob (plist-get info :ignore-list)) nil)
  1285. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1286. ;; back into Org syntax.
  1287. ((not (org-export-interpret-p blob info)) 'org-export-expand)
  1288. ;; 1.3. Else apply naming convention.
  1289. (t (let ((trans (intern (format "org-%s-%s" backend type))))
  1290. (and (fboundp trans) trans)))))
  1291. ;; 2. Compute CONTENTS of BLOB.
  1292. (contents
  1293. (cond
  1294. ;; Case 0. No transcoder defined: ignore BLOB.
  1295. ((not transcoder) nil)
  1296. ;; Case 1. Transparently export an Org document.
  1297. ((eq type 'org-data) (org-export-data blob backend info))
  1298. ;; Case 2. For a recursive object.
  1299. ((memq type org-element-recursive-objects)
  1300. (org-export-data blob backend info))
  1301. ;; Case 3. For a recursive element.
  1302. ((memq type org-element-greater-elements)
  1303. ;; Ignore contents of an archived tree
  1304. ;; when `:with-archived-trees' is `headline'.
  1305. (unless (and
  1306. (eq type 'headline)
  1307. (eq (plist-get info :with-archived-trees) 'headline)
  1308. (org-element-property :archivedp blob))
  1309. (org-element-normalize-string
  1310. (org-export-data blob backend info))))
  1311. ;; Case 4. For a paragraph.
  1312. ((eq type 'paragraph)
  1313. (let ((paragraph
  1314. (org-element-normalize-contents
  1315. blob
  1316. ;; When normalizing contents of an item or
  1317. ;; a footnote definition, ignore first line's
  1318. ;; indentation: there is none and it might be
  1319. ;; misleading.
  1320. (and (not (org-export-get-previous-element blob info))
  1321. (let ((parent (org-export-get-parent blob info)))
  1322. (memq (org-element-type parent)
  1323. '(footnote-definition item)))))))
  1324. (org-export-data paragraph backend info)))))
  1325. ;; 3. Transcode BLOB into RESULTS string.
  1326. (results (cond
  1327. ((not transcoder) nil)
  1328. ((eq transcoder 'org-export-expand)
  1329. (org-export-data
  1330. `(org-data nil ,(funcall transcoder blob contents))
  1331. backend info))
  1332. (t (funcall transcoder blob contents info)))))
  1333. ;; 4. Return results.
  1334. (cond
  1335. ((not results) nil)
  1336. ;; No filter for a full document.
  1337. ((eq type 'org-data) results)
  1338. ;; Otherwise, update INFO, append the same white space
  1339. ;; between elements or objects as in the original buffer,
  1340. ;; and call appropriate filters.
  1341. (t
  1342. (let ((results
  1343. (org-export-filter-apply-functions
  1344. (plist-get info (intern (format ":filter-%s" type)))
  1345. (let ((post-blank (org-element-property :post-blank blob)))
  1346. (if (memq type org-element-all-elements)
  1347. (concat (org-element-normalize-string results)
  1348. (make-string post-blank ?\n))
  1349. (concat results (make-string post-blank ? ))))
  1350. backend info)))
  1351. ;; Eventually return string.
  1352. results)))))))
  1353. (org-element-contents data) ""))
  1354. (defun org-export-secondary-string (secondary backend info)
  1355. "Convert SECONDARY string into BACKEND format.
  1356. SECONDARY is a nested list as returned by
  1357. `org-element-parse-secondary-string'.
  1358. BACKEND is a symbol among supported exporters. INFO is a plist
  1359. used as a communication channel.
  1360. Return transcoded string."
  1361. ;; Make SECONDARY acceptable for `org-export-data'.
  1362. (let ((s (if (listp secondary) secondary (list secondary))))
  1363. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1364. (defun org-export-interpret-p (blob info)
  1365. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1366. Check is done according to export options INFO, stored as
  1367. a plist."
  1368. (case (org-element-type blob)
  1369. ;; ... entities...
  1370. (entity (plist-get info :with-entities))
  1371. ;; ... emphasis...
  1372. (emphasis (plist-get info :with-emphasize))
  1373. ;; ... fixed-width areas.
  1374. (fixed-width (plist-get info :with-fixed-width))
  1375. ;; ... footnotes...
  1376. ((footnote-definition footnote-reference)
  1377. (plist-get info :with-footnotes))
  1378. ;; ... sub/superscripts...
  1379. ((subscript superscript)
  1380. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1381. (if (eq sub/super-p '{})
  1382. (org-element-property :use-brackets-p blob)
  1383. sub/super-p)))
  1384. ;; ... tables...
  1385. (table (plist-get info :with-tables))
  1386. (otherwise t)))
  1387. (defsubst org-export-expand (blob contents)
  1388. "Expand a parsed element or object to its original state.
  1389. BLOB is either an element or an object. CONTENTS is its
  1390. contents, as a string or nil."
  1391. (funcall (intern (format "org-element-%s-interpreter" (org-element-type blob)))
  1392. blob contents))
  1393. (defun org-export-ignore-element (element info)
  1394. "Add ELEMENT to `:ignore-list' in INFO.
  1395. Any element in `:ignore-list' will be skipped when using
  1396. `org-element-map'. INFO is modified by side effects."
  1397. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  1398. ;;; The Filter System
  1399. ;; Filters allow end-users to tweak easily the transcoded output.
  1400. ;; They are the functional counterpart of hooks, as every filter in
  1401. ;; a set is applied to the return value of the previous one.
  1402. ;; Every set is back-end agnostic. Although, a filter is always
  1403. ;; called, in addition to the string it applies to, with the back-end
  1404. ;; used as argument, so it's easy enough for the end-user to add
  1405. ;; back-end specific filters in the set. The communication channel,
  1406. ;; as a plist, is required as the third argument.
  1407. ;; Filters sets are defined below. There are of four types:
  1408. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1409. ;; complete parsed tree. It's the only filters set that doesn't
  1410. ;; apply to a string.
  1411. ;; - `org-export-filter-final-output-functions' applies to the final
  1412. ;; transcoded string.
  1413. ;; - `org-export-filter-plain-text-functions' applies to any string
  1414. ;; not recognized as Org syntax.
  1415. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1416. ;; after an element or object of type TYPE has been transcoded.
  1417. ;; All filters sets are applied through
  1418. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1419. ;; applied in a LIFO fashion. It allows developers to be sure that
  1420. ;; their filters will be applied first.
  1421. ;; Filters properties are installed in communication channel just
  1422. ;; before parsing, with `org-export-install-filters' function.
  1423. ;;;; Special Filters
  1424. (defvar org-export-filter-parse-tree-functions nil
  1425. "Filter, or list of filters, applied to the parsed tree.
  1426. Each filter is called with three arguments: the parse tree, as
  1427. returned by `org-element-parse-buffer', the back-end, as
  1428. a symbol, and the communication channel, as a plist. It must
  1429. return the modified parse tree to transcode.")
  1430. (defvar org-export-filter-final-output-functions nil
  1431. "Filter, or list of filters, applied to the transcoded string.
  1432. Each filter is called with three arguments: the full transcoded
  1433. string, the back-end, as a symbol, and the communication channel,
  1434. as a plist. It must return a string that will be used as the
  1435. final export output.")
  1436. (defvar org-export-filter-plain-text-functions nil
  1437. "Filter, or list of filters, applied to plain text.
  1438. Each filter is called with three arguments: a string which
  1439. contains no Org syntax, the back-end, as a symbol, and the
  1440. communication channel, as a plist. It must return a string or
  1441. nil.")
  1442. ;;;; Elements Filters
  1443. (defvar org-export-filter-center-block-functions nil
  1444. "List of functions applied to a transcoded center block.
  1445. Each filter is called with three arguments: the transcoded center
  1446. block, as a string, the back-end, as a symbol, and the
  1447. communication channel, as a plist. It must return a string or
  1448. nil.")
  1449. (defvar org-export-filter-drawer-functions nil
  1450. "List of functions applied to a transcoded drawer.
  1451. Each filter is called with three arguments: the transcoded
  1452. drawer, as a string, the back-end, as a symbol, and the
  1453. communication channel, as a plist. It must return a string or
  1454. nil.")
  1455. (defvar org-export-filter-dynamic-block-functions nil
  1456. "List of functions applied to a transcoded dynamic-block.
  1457. Each filter is called with three arguments: the transcoded
  1458. dynamic-block, as a string, the back-end, as a symbol, and the
  1459. communication channel, as a plist. It must return a string or
  1460. nil.")
  1461. (defvar org-export-filter-headline-functions nil
  1462. "List of functions applied to a transcoded headline.
  1463. Each filter is called with three arguments: the transcoded
  1464. headline, as a string, the back-end, as a symbol, and the
  1465. communication channel, as a plist. It must return a string or
  1466. nil.")
  1467. (defvar org-export-filter-inlinetask-functions nil
  1468. "List of functions applied to a transcoded inlinetask.
  1469. Each filter is called with three arguments: the transcoded
  1470. inlinetask, as a string, the back-end, as a symbol, and the
  1471. communication channel, as a plist. It must return a string or
  1472. nil.")
  1473. (defvar org-export-filter-plain-list-functions nil
  1474. "List of functions applied to a transcoded plain-list.
  1475. Each filter is called with three arguments: the transcoded
  1476. plain-list, as a string, the back-end, as a symbol, and the
  1477. communication channel, as a plist. It must return a string or
  1478. nil.")
  1479. (defvar org-export-filter-item-functions nil
  1480. "List of functions applied to a transcoded item.
  1481. Each filter is called with three arguments: the transcoded item,
  1482. as a string, the back-end, as a symbol, and the communication
  1483. channel, as a plist. It must return a string or nil.")
  1484. (defvar org-export-filter-comment-functions nil
  1485. "List of functions applied to a transcoded comment.
  1486. Each filter is called with three arguments: the transcoded
  1487. comment, as a string, the back-end, as a symbol, and the
  1488. communication channel, as a plist. It must return a string or
  1489. nil.")
  1490. (defvar org-export-filter-comment-block-functions nil
  1491. "List of functions applied to a transcoded comment-comment.
  1492. Each filter is called with three arguments: the transcoded
  1493. comment-block, as a string, the back-end, as a symbol, and the
  1494. communication channel, as a plist. It must return a string or
  1495. nil.")
  1496. (defvar org-export-filter-example-block-functions nil
  1497. "List of functions applied to a transcoded example-block.
  1498. Each filter is called with three arguments: the transcoded
  1499. example-block, as a string, the back-end, as a symbol, and the
  1500. communication channel, as a plist. It must return a string or
  1501. nil.")
  1502. (defvar org-export-filter-export-block-functions nil
  1503. "List of functions applied to a transcoded export-block.
  1504. Each filter is called with three arguments: the transcoded
  1505. export-block, as a string, the back-end, as a symbol, and the
  1506. communication channel, as a plist. It must return a string or
  1507. nil.")
  1508. (defvar org-export-filter-fixed-width-functions nil
  1509. "List of functions applied to a transcoded fixed-width.
  1510. Each filter is called with three arguments: the transcoded
  1511. fixed-width, as a string, the back-end, as a symbol, and the
  1512. communication channel, as a plist. It must return a string or
  1513. nil.")
  1514. (defvar org-export-filter-footnote-definition-functions nil
  1515. "List of functions applied to a transcoded footnote-definition.
  1516. Each filter is called with three arguments: the transcoded
  1517. footnote-definition, as a string, the back-end, as a symbol, and
  1518. the communication channel, as a plist. It must return a string
  1519. or nil.")
  1520. (defvar org-export-filter-horizontal-rule-functions nil
  1521. "List of functions applied to a transcoded horizontal-rule.
  1522. Each filter is called with three arguments: the transcoded
  1523. horizontal-rule, as a string, the back-end, as a symbol, and the
  1524. communication channel, as a plist. It must return a string or
  1525. nil.")
  1526. (defvar org-export-filter-keyword-functions nil
  1527. "List of functions applied to a transcoded keyword.
  1528. Each filter is called with three arguments: the transcoded
  1529. keyword, as a string, the back-end, as a symbol, and the
  1530. communication channel, as a plist. It must return a string or
  1531. nil.")
  1532. (defvar org-export-filter-latex-environment-functions nil
  1533. "List of functions applied to a transcoded latex-environment.
  1534. Each filter is called with three arguments: the transcoded
  1535. latex-environment, as a string, the back-end, as a symbol, and
  1536. the communication channel, as a plist. It must return a string
  1537. or nil.")
  1538. (defvar org-export-filter-babel-call-functions nil
  1539. "List of functions applied to a transcoded babel-call.
  1540. Each filter is called with three arguments: the transcoded
  1541. babel-call, as a string, the back-end, as a symbol, and the
  1542. communication channel, as a plist. It must return a string or
  1543. nil.")
  1544. (defvar org-export-filter-paragraph-functions nil
  1545. "List of functions applied to a transcoded paragraph.
  1546. Each filter is called with three arguments: the transcoded
  1547. paragraph, as a string, the back-end, as a symbol, and the
  1548. communication channel, as a plist. It must return a string or
  1549. nil.")
  1550. (defvar org-export-filter-property-drawer-functions nil
  1551. "List of functions applied to a transcoded property-drawer.
  1552. Each filter is called with three arguments: the transcoded
  1553. property-drawer, as a string, the back-end, as a symbol, and the
  1554. communication channel, as a plist. It must return a string or
  1555. nil.")
  1556. (defvar org-export-filter-quote-block-functions nil
  1557. "List of functions applied to a transcoded quote block.
  1558. Each filter is called with three arguments: the transcoded quote
  1559. block, as a string, the back-end, as a symbol, and the
  1560. communication channel, as a plist. It must return a string or
  1561. nil.")
  1562. (defvar org-export-filter-quote-section-functions nil
  1563. "List of functions applied to a transcoded quote-section.
  1564. Each filter is called with three arguments: the transcoded
  1565. quote-section, as a string, the back-end, as a symbol, and the
  1566. communication channel, as a plist. It must return a string or
  1567. nil.")
  1568. (defvar org-export-filter-section-functions nil
  1569. "List of functions applied to a transcoded section.
  1570. Each filter is called with three arguments: the transcoded
  1571. section, as a string, the back-end, as a symbol, and the
  1572. communication channel, as a plist. It must return a string or
  1573. nil.")
  1574. (defvar org-export-filter-special-block-functions nil
  1575. "List of functions applied to a transcoded special block.
  1576. Each filter is called with three arguments: the transcoded
  1577. special block, as a string, the back-end, as a symbol, and the
  1578. communication channel, as a plist. It must return a string or
  1579. nil.")
  1580. (defvar org-export-filter-src-block-functions nil
  1581. "List of functions applied to a transcoded src-block.
  1582. Each filter is called with three arguments: the transcoded
  1583. src-block, as a string, the back-end, as a symbol, and the
  1584. communication channel, as a plist. It must return a string or
  1585. nil.")
  1586. (defvar org-export-filter-table-functions nil
  1587. "List of functions applied to a transcoded table.
  1588. Each filter is called with three arguments: the transcoded table,
  1589. as a string, the back-end, as a symbol, and the communication
  1590. channel, as a plist. It must return a string or nil.")
  1591. (defvar org-export-filter-verse-block-functions nil
  1592. "List of functions applied to a transcoded verse block.
  1593. Each filter is called with three arguments: the transcoded verse
  1594. block, as a string, the back-end, as a symbol, and the
  1595. communication channel, as a plist. It must return a string or
  1596. nil.")
  1597. ;;;; Objects Filters
  1598. (defvar org-export-filter-emphasis-functions nil
  1599. "List of functions applied to a transcoded emphasis.
  1600. Each filter is called with three arguments: the transcoded
  1601. emphasis, as a string, the back-end, as a symbol, and the
  1602. communication channel, as a plist. It must return a string or
  1603. nil.")
  1604. (defvar org-export-filter-entity-functions nil
  1605. "List of functions applied to a transcoded entity.
  1606. Each filter is called with three arguments: the transcoded
  1607. entity, as a string, the back-end, as a symbol, and the
  1608. communication channel, as a plist. It must return a string or
  1609. nil.")
  1610. (defvar org-export-filter-export-snippet-functions nil
  1611. "List of functions applied to a transcoded export-snippet.
  1612. Each filter is called with three arguments: the transcoded
  1613. export-snippet, as a string, the back-end, as a symbol, and the
  1614. communication channel, as a plist. It must return a string or
  1615. nil.")
  1616. (defvar org-export-filter-footnote-reference-functions nil
  1617. "List of functions applied to a transcoded footnote-reference.
  1618. Each filter is called with three arguments: the transcoded
  1619. footnote-reference, as a string, the back-end, as a symbol, and
  1620. the communication channel, as a plist. It must return a string
  1621. or nil.")
  1622. (defvar org-export-filter-inline-babel-call-functions nil
  1623. "List of functions applied to a transcoded inline-babel-call.
  1624. Each filter is called with three arguments: the transcoded
  1625. inline-babel-call, as a string, the back-end, as a symbol, and
  1626. the communication channel, as a plist. It must return a string
  1627. or nil.")
  1628. (defvar org-export-filter-inline-src-block-functions nil
  1629. "List of functions applied to a transcoded inline-src-block.
  1630. Each filter is called with three arguments: the transcoded
  1631. inline-src-block, as a string, the back-end, as a symbol, and the
  1632. communication channel, as a plist. It must return a string or
  1633. nil.")
  1634. (defvar org-export-filter-latex-fragment-functions nil
  1635. "List of functions applied to a transcoded latex-fragment.
  1636. Each filter is called with three arguments: the transcoded
  1637. latex-fragment, as a string, the back-end, as a symbol, and the
  1638. communication channel, as a plist. It must return a string or
  1639. nil.")
  1640. (defvar org-export-filter-line-break-functions nil
  1641. "List of functions applied to a transcoded line-break.
  1642. Each filter is called with three arguments: the transcoded
  1643. line-break, as a string, the back-end, as a symbol, and the
  1644. communication channel, as a plist. It must return a string or
  1645. nil.")
  1646. (defvar org-export-filter-link-functions nil
  1647. "List of functions applied to a transcoded link.
  1648. Each filter is called with three arguments: the transcoded link,
  1649. as a string, the back-end, as a symbol, and the communication
  1650. channel, as a plist. It must return a string or nil.")
  1651. (defvar org-export-filter-macro-functions nil
  1652. "List of functions applied to a transcoded macro.
  1653. Each filter is called with three arguments: the transcoded macro,
  1654. as a string, the back-end, as a symbol, and the communication
  1655. channel, as a plist. It must return a string or nil.")
  1656. (defvar org-export-filter-radio-target-functions nil
  1657. "List of functions applied to a transcoded radio-target.
  1658. Each filter is called with three arguments: the transcoded
  1659. radio-target, as a string, the back-end, as a symbol, and the
  1660. communication channel, as a plist. It must return a string or
  1661. nil.")
  1662. (defvar org-export-filter-statistics-cookie-functions nil
  1663. "List of functions applied to a transcoded statistics-cookie.
  1664. Each filter is called with three arguments: the transcoded
  1665. statistics-cookie, as a string, the back-end, as a symbol, and
  1666. the communication channel, as a plist. It must return a string
  1667. or nil.")
  1668. (defvar org-export-filter-subscript-functions nil
  1669. "List of functions applied to a transcoded subscript.
  1670. Each filter is called with three arguments: the transcoded
  1671. subscript, as a string, the back-end, as a symbol, and the
  1672. communication channel, as a plist. It must return a string or
  1673. nil.")
  1674. (defvar org-export-filter-superscript-functions nil
  1675. "List of functions applied to a transcoded superscript.
  1676. Each filter is called with three arguments: the transcoded
  1677. superscript, as a string, the back-end, as a symbol, and the
  1678. communication channel, as a plist. It must return a string or
  1679. nil.")
  1680. (defvar org-export-filter-target-functions nil
  1681. "List of functions applied to a transcoded target.
  1682. Each filter is called with three arguments: the transcoded
  1683. target, as a string, the back-end, as a symbol, and the
  1684. communication channel, as a plist. It must return a string or
  1685. nil.")
  1686. (defvar org-export-filter-time-stamp-functions nil
  1687. "List of functions applied to a transcoded time-stamp.
  1688. Each filter is called with three arguments: the transcoded
  1689. time-stamp, as a string, the back-end, as a symbol, and the
  1690. communication channel, as a plist. It must return a string or
  1691. nil.")
  1692. (defvar org-export-filter-verbatim-functions nil
  1693. "List of functions applied to a transcoded verbatim.
  1694. Each filter is called with three arguments: the transcoded
  1695. verbatim, as a string, the back-end, as a symbol, and the
  1696. communication channel, as a plist. It must return a string or
  1697. nil.")
  1698. (defun org-export-filter-apply-functions (filters value backend info)
  1699. "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
  1700. Functions are called in a LIFO fashion, to be sure that developer
  1701. specified filters, if any, are called first."
  1702. (loop for filter in filters
  1703. if (not value) return nil else
  1704. do (setq value (funcall filter value backend info)))
  1705. value)
  1706. (defun org-export-install-filters (backend info)
  1707. "Install filters properties in communication channel.
  1708. BACKEND is a symbol specifying which back-end specific filters to
  1709. install, if any. INFO is a plist containing the current
  1710. communication channel.
  1711. Return the updated communication channel."
  1712. (let (plist)
  1713. ;; Install user defined filters with `org-export-filters-alist'.
  1714. (mapc (lambda (p)
  1715. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  1716. org-export-filters-alist)
  1717. ;; Prepend back-end specific filters to that list.
  1718. (let ((back-end-filters (intern (format "org-%s-filters-alist" backend))))
  1719. (when (boundp back-end-filters)
  1720. (mapc (lambda (p)
  1721. ;; Single values get consed, lists are prepended.
  1722. (let ((key (car p)) (value (cdr p)))
  1723. (when value
  1724. (setq plist
  1725. (plist-put
  1726. plist key
  1727. (if (atom value) (cons value (plist-get plist key))
  1728. (append value (plist-get plist key))))))))
  1729. (eval back-end-filters))))
  1730. ;; Return new communication channel.
  1731. (org-combine-plists info plist)))
  1732. ;;; Core functions
  1733. ;; This is the room for the main function, `org-export-as', along with
  1734. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1735. ;; They differ only by the way they output the resulting code.
  1736. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1737. ;; used with `org-export-to-file'. With a given extension, it tries
  1738. ;; to provide a canonical file name to write export output to.
  1739. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1740. ;; but a copy of it (with the same buffer-local variables and
  1741. ;; visibility), where include keywords are expanded and Babel blocks
  1742. ;; are executed, if appropriate.
  1743. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1744. ;; File inclusion is taken care of by
  1745. ;; `org-export-expand-include-keyword' and
  1746. ;; `org-export-prepare-file-contents'. Structure wise, including
  1747. ;; a whole Org file in a buffer often makes little sense. For
  1748. ;; example, if the file contains an headline and the include keyword
  1749. ;; was within an item, the item should contain the headline. That's
  1750. ;; why file inclusion should be done before any structure can be
  1751. ;; associated to the file, that is before parsing.
  1752. (defun org-export-as
  1753. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  1754. "Transcode current Org buffer into BACKEND code.
  1755. If narrowing is active in the current buffer, only transcode its
  1756. narrowed part.
  1757. If a region is active, transcode that region.
  1758. When optional argument SUBTREEP is non-nil, transcode the
  1759. sub-tree at point, extracting information from the headline
  1760. properties first.
  1761. When optional argument VISIBLE-ONLY is non-nil, don't export
  1762. contents of hidden elements.
  1763. When optional argument BODY-ONLY is non-nil, only return body
  1764. code, without preamble nor postamble.
  1765. Optional argument EXT-PLIST, when provided, is a property list
  1766. with external parameters overriding Org default settings, but
  1767. still inferior to file-local settings.
  1768. Optional argument NOEXPAND, when non-nil, prevents included files
  1769. to be expanded and Babel code to be executed.
  1770. Return code as a string."
  1771. (save-excursion
  1772. (save-restriction
  1773. ;; Narrow buffer to an appropriate region for parsing.
  1774. (cond ((org-region-active-p)
  1775. (narrow-to-region (region-beginning) (region-end)))
  1776. (subtreep (org-narrow-to-subtree)))
  1777. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
  1778. ;; Then options can be completed with tree properties. Note:
  1779. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1780. ;; created, where include keywords are expanded and code blocks
  1781. ;; are evaluated. RAW-DATA is the parsed tree of the buffer
  1782. ;; resulting from that process. Eventually call
  1783. ;; `org-export-filter-parse-tree-functions'.
  1784. (goto-char (point-min))
  1785. (let ((info (org-export-get-environment backend subtreep ext-plist)))
  1786. ;; Remove subtree's headline from contents if subtree mode is
  1787. ;; activated.
  1788. (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
  1789. ;; Install filters in communication channel.
  1790. (setq info (org-export-install-filters backend info))
  1791. (let ((raw-data
  1792. (org-export-filter-apply-functions
  1793. (plist-get info :filter-parse-tree)
  1794. ;; If NOEXPAND is non-nil, simply parse current
  1795. ;; visible part of buffer.
  1796. (if noexpand (org-element-parse-buffer nil visible-only)
  1797. (org-export-with-current-buffer-copy
  1798. (org-export-expand-include-keyword)
  1799. (let ((org-current-export-file (current-buffer)))
  1800. (org-export-blocks-preprocess))
  1801. (org-element-parse-buffer nil visible-only)))
  1802. backend info)))
  1803. ;; Complete communication channel with tree properties.
  1804. (setq info
  1805. (org-combine-plists
  1806. info
  1807. (org-export-collect-tree-properties raw-data info backend)))
  1808. ;; Transcode RAW-DATA. Also call
  1809. ;; `org-export-filter-final-output-functions'.
  1810. (let* ((body (org-element-normalize-string
  1811. (org-export-data raw-data backend info)))
  1812. (template (intern (format "org-%s-template" backend)))
  1813. (output (org-export-filter-apply-functions
  1814. (plist-get info :filter-final-output)
  1815. (if (or (not (fboundp template)) body-only) body
  1816. (funcall template body info))
  1817. backend info)))
  1818. ;; Maybe add final OUTPUT to kill ring before returning it.
  1819. (when org-export-copy-to-kill-ring (org-kill-new output))
  1820. output))))))
  1821. (defun org-export-to-buffer
  1822. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  1823. "Call `org-export-as' with output to a specified buffer.
  1824. BACKEND is the back-end used for transcoding, as a symbol.
  1825. BUFFER is the output buffer. If it already exists, it will be
  1826. erased first, otherwise, it will be created.
  1827. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1828. and NOEXPAND are similar to those used in `org-export-as', which
  1829. see.
  1830. Return buffer."
  1831. (let ((out (org-export-as
  1832. backend subtreep visible-only body-only ext-plist noexpand))
  1833. (buffer (get-buffer-create buffer)))
  1834. (with-current-buffer buffer
  1835. (erase-buffer)
  1836. (insert out)
  1837. (goto-char (point-min)))
  1838. buffer))
  1839. (defun org-export-to-file
  1840. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  1841. "Call `org-export-as' with output to a specified file.
  1842. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1843. is the name of the output file, as a string.
  1844. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1845. and NOEXPAND are similar to those used in `org-export-as', which
  1846. see.
  1847. Return output file's name."
  1848. ;; Checks for FILE permissions. `write-file' would do the same, but
  1849. ;; we'd rather avoid needless transcoding of parse tree.
  1850. (unless (file-writable-p file) (error "Output file not writable"))
  1851. ;; Insert contents to a temporary buffer and write it to FILE.
  1852. (let ((out (org-export-as
  1853. backend subtreep visible-only body-only ext-plist noexpand)))
  1854. (with-temp-buffer
  1855. (insert out)
  1856. (let ((coding-system-for-write org-export-coding-system))
  1857. (write-file file))))
  1858. ;; Return full path.
  1859. file)
  1860. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  1861. "Return output file's name according to buffer specifications.
  1862. EXTENSION is a string representing the output file extension,
  1863. with the leading dot.
  1864. With a non-nil optional argument SUBTREEP, try to determine
  1865. output file's name by looking for \"EXPORT_FILE_NAME\" property
  1866. of subtree at point.
  1867. When optional argument PUB-DIR is set, use it as the publishing
  1868. directory.
  1869. Return file name as a string, or nil if it couldn't be
  1870. determined."
  1871. (let ((base-name
  1872. ;; File name may come from EXPORT_FILE_NAME subtree property,
  1873. ;; assuming point is at beginning of said sub-tree.
  1874. (file-name-sans-extension
  1875. (or (and subtreep
  1876. (org-entry-get
  1877. (save-excursion
  1878. (ignore-errors
  1879. (org-back-to-heading (not visible-only)) (point)))
  1880. "EXPORT_FILE_NAME" t))
  1881. ;; File name may be extracted from buffer's associated
  1882. ;; file, if any.
  1883. (buffer-file-name (buffer-base-buffer))
  1884. ;; Can't determine file name on our own: Ask user.
  1885. (let ((read-file-name-function
  1886. (and org-completion-use-ido 'ido-read-file-name)))
  1887. (read-file-name
  1888. "Output file: " pub-dir nil nil nil
  1889. (lambda (name)
  1890. (string= (file-name-extension name t) extension))))))))
  1891. ;; Build file name. Enforce EXTENSION over whatever user may have
  1892. ;; come up with. PUB-DIR, if defined, always has precedence over
  1893. ;; any provided path.
  1894. (cond
  1895. (pub-dir
  1896. (concat (file-name-as-directory pub-dir)
  1897. (file-name-nondirectory base-name)
  1898. extension))
  1899. ((string= (file-name-nondirectory base-name) base-name)
  1900. (concat (file-name-as-directory ".") base-name extension))
  1901. (t (concat base-name extension)))))
  1902. (defmacro org-export-with-current-buffer-copy (&rest body)
  1903. "Apply BODY in a copy of the current buffer.
  1904. The copy preserves local variables and visibility of the original
  1905. buffer.
  1906. Point is at buffer's beginning when BODY is applied."
  1907. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1908. `(let ((,original-buffer ,(current-buffer))
  1909. (,offset ,(1- (point-min)))
  1910. (,buffer-string ,(buffer-string))
  1911. (,overlays (mapcar
  1912. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1913. (with-temp-buffer
  1914. (let ((buffer-invisibility-spec nil))
  1915. (org-clone-local-variables
  1916. ,original-buffer
  1917. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  1918. (insert ,buffer-string)
  1919. (mapc (lambda (ov)
  1920. (move-overlay
  1921. ov
  1922. (- (overlay-start ov) ,offset)
  1923. (- (overlay-end ov) ,offset)
  1924. (current-buffer)))
  1925. ,overlays)
  1926. (goto-char (point-min))
  1927. (progn ,@body))))))
  1928. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1929. (defun org-export-expand-include-keyword (&optional included dir)
  1930. "Expand every include keyword in buffer.
  1931. Optional argument INCLUDED is a list of included file names along
  1932. with their line restriction, when appropriate. It is used to
  1933. avoid infinite recursion. Optional argument DIR is the current
  1934. working directory. It is used to properly resolve relative
  1935. paths."
  1936. (let ((case-fold-search t))
  1937. (goto-char (point-min))
  1938. (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
  1939. (when (eq (org-element-type (save-match-data (org-element-at-point)))
  1940. 'keyword)
  1941. (beginning-of-line)
  1942. ;; Extract arguments from keyword's value.
  1943. (let* ((value (match-string 1))
  1944. (ind (org-get-indentation))
  1945. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1946. (prog1 (expand-file-name (match-string 1 value) dir)
  1947. (setq value (replace-match "" nil nil value)))))
  1948. (lines
  1949. (and (string-match
  1950. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1951. (prog1 (match-string 1 value)
  1952. (setq value (replace-match "" nil nil value)))))
  1953. (env (cond ((string-match "\\<example\\>" value) 'example)
  1954. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1955. (match-string 1 value))))
  1956. ;; Minimal level of included file defaults to the child
  1957. ;; level of the current headline, if any, or one. It
  1958. ;; only applies is the file is meant to be included as
  1959. ;; an Org one.
  1960. (minlevel
  1961. (and (not env)
  1962. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  1963. (prog1 (string-to-number (match-string 1 value))
  1964. (setq value (replace-match "" nil nil value)))
  1965. (let ((cur (org-current-level)))
  1966. (if cur (1+ (org-reduced-level cur)) 1))))))
  1967. ;; Remove keyword.
  1968. (delete-region (point) (progn (forward-line) (point)))
  1969. (cond
  1970. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  1971. ;; Check if files has already been parsed. Look after
  1972. ;; inclusion lines too, as different parts of the same file
  1973. ;; can be included too.
  1974. ((member (list file lines) included)
  1975. (error "Recursive file inclusion: %s" file))
  1976. (t
  1977. (cond
  1978. ((eq env 'example)
  1979. (insert
  1980. (let ((ind-str (make-string ind ? ))
  1981. (contents
  1982. ;; Protect sensitive contents with commas.
  1983. (replace-regexp-in-string
  1984. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  1985. (org-export-prepare-file-contents file lines)
  1986. nil nil 1)))
  1987. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  1988. ind-str contents ind-str))))
  1989. ((stringp env)
  1990. (insert
  1991. (let ((ind-str (make-string ind ? ))
  1992. (contents
  1993. ;; Protect sensitive contents with commas.
  1994. (replace-regexp-in-string
  1995. (if (string= env "org") "\\(^\\)\\(.\\)"
  1996. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  1997. (org-export-prepare-file-contents file lines)
  1998. nil nil 1)))
  1999. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2000. ind-str env contents ind-str))))
  2001. (t
  2002. (insert
  2003. (with-temp-buffer
  2004. (org-mode)
  2005. (insert
  2006. (org-export-prepare-file-contents file lines ind minlevel))
  2007. (org-export-expand-include-keyword
  2008. (cons (list file lines) included)
  2009. (file-name-directory file))
  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 (num-alist
  2121. (collect-fn
  2122. (function
  2123. (lambda (data)
  2124. ;; Collect footnote number, label and definition in DATA.
  2125. (org-element-map
  2126. data 'footnote-reference
  2127. (lambda (fn)
  2128. (when (org-export-footnote-first-reference-p fn info)
  2129. (let ((def (org-export-get-footnote-definition fn info)))
  2130. (push
  2131. (list (org-export-get-footnote-number fn info)
  2132. (org-element-property :label fn)
  2133. def)
  2134. num-alist)
  2135. ;; Also search in definition for nested footnotes.
  2136. (when (eq (org-element-property :type fn) 'standard)
  2137. (funcall collect-fn def)))))
  2138. info)
  2139. ;; Return final value.
  2140. (reverse num-alist)))))
  2141. (funcall collect-fn (plist-get info :parse-tree))))
  2142. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2143. "Non-nil when a footnote reference is the first one for its label.
  2144. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2145. INFO is the plist used as a communication channel."
  2146. (let ((label (org-element-property :label footnote-reference)))
  2147. ;; Anonymous footnotes are always a first reference.
  2148. (if (not label) t
  2149. ;; Otherwise, return the first footnote with the same LABEL and
  2150. ;; test if it is equal to FOOTNOTE-REFERENCE.
  2151. (let ((search-refs
  2152. (function
  2153. (lambda (data)
  2154. (org-element-map
  2155. data 'footnote-reference
  2156. (lambda (fn)
  2157. (cond
  2158. ((string= (org-element-property :label fn) label)
  2159. (throw 'exit fn))
  2160. ;; If FN isn't inlined, be sure to traverse its
  2161. ;; definition before resuming search. See
  2162. ;; comments in `org-export-get-footnote-number'
  2163. ;; for more information.
  2164. ((eq (org-element-property :type fn) 'standard)
  2165. (funcall search-refs
  2166. (org-export-get-footnote-definition fn info)))))
  2167. info 'first-match)))))
  2168. (equal (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  2169. footnote-reference)))))
  2170. (defun org-export-get-footnote-definition (footnote-reference info)
  2171. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2172. INFO is the plist used as a communication channel."
  2173. (let ((label (org-element-property :label footnote-reference)))
  2174. (or (org-element-property :inline-definition footnote-reference)
  2175. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2176. (defun org-export-get-footnote-number (footnote info)
  2177. "Return number associated to a footnote.
  2178. FOOTNOTE is either a footnote reference or a footnote definition.
  2179. INFO is the plist used as a communication channel."
  2180. (let ((label (org-element-property :label footnote))
  2181. seen-refs
  2182. (search-ref
  2183. (function
  2184. (lambda (data)
  2185. ;; Search footnote references through DATA, filling
  2186. ;; SEEN-REFS along the way.
  2187. (org-element-map
  2188. data 'footnote-reference
  2189. (lambda (fn)
  2190. (let ((fn-lbl (org-element-property :label fn)))
  2191. (cond
  2192. ;; Anonymous footnote match: return number.
  2193. ((and (not fn-lbl) (equal fn footnote))
  2194. (throw 'exit (1+ (length seen-refs))))
  2195. ;; Labels match: return number.
  2196. ((and label (string= label fn-lbl))
  2197. (throw 'exit (1+ (length seen-refs))))
  2198. ;; Anonymous footnote: it's always a new one. Also,
  2199. ;; be sure to return nil from the `cond' so
  2200. ;; `first-match' doesn't get us out of the loop.
  2201. ((not fn-lbl) (push 'inline seen-refs) nil)
  2202. ;; Label not seen so far: add it so SEEN-REFS.
  2203. ;;
  2204. ;; Also search for subsequent references in footnote
  2205. ;; definition so numbering following reading logic.
  2206. ;; Note that we don't have to care about inline
  2207. ;; definitions, since `org-element-map' already
  2208. ;; traverse them at the right time.
  2209. ;;
  2210. ;; Once again, return nil to stay in the loop.
  2211. ((not (member fn-lbl seen-refs))
  2212. (push fn-lbl seen-refs)
  2213. (funcall search-ref
  2214. (org-export-get-footnote-definition fn info))
  2215. nil))))
  2216. info 'first-match)))))
  2217. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  2218. ;;;; For Headlines
  2219. ;; `org-export-get-relative-level' is a shortcut to get headline
  2220. ;; level, relatively to the lower headline level in the parsed tree.
  2221. ;; `org-export-get-headline-number' returns the section number of an
  2222. ;; headline, while `org-export-number-to-roman' allows to convert it
  2223. ;; to roman numbers.
  2224. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2225. ;; `org-export-last-sibling-p' are three useful predicates when it
  2226. ;; comes to fulfill the `:headline-levels' property.
  2227. (defun org-export-get-relative-level (headline info)
  2228. "Return HEADLINE relative level within current parsed tree.
  2229. INFO is a plist holding contextual information."
  2230. (+ (org-element-property :level headline)
  2231. (or (plist-get info :headline-offset) 0)))
  2232. (defun org-export-low-level-p (headline info)
  2233. "Non-nil when HEADLINE is considered as low level.
  2234. INFO is a plist used as a communication channel.
  2235. A low level headlines has a relative level greater than
  2236. `:headline-levels' property value.
  2237. Return value is the difference between HEADLINE relative level
  2238. and the last level being considered as high enough, or nil."
  2239. (let ((limit (plist-get info :headline-levels)))
  2240. (when (wholenump limit)
  2241. (let ((level (org-export-get-relative-level headline info)))
  2242. (and (> level limit) (- level limit))))))
  2243. (defun org-export-get-headline-number (headline info)
  2244. "Return HEADLINE numbering as a list of numbers.
  2245. INFO is a plist holding contextual information."
  2246. (cdr (assoc headline (plist-get info :headline-numbering))))
  2247. (defun org-export-number-to-roman (n)
  2248. "Convert integer N into a roman numeral."
  2249. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2250. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2251. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2252. ( 1 . "I")))
  2253. (res ""))
  2254. (if (<= n 0)
  2255. (number-to-string n)
  2256. (while roman
  2257. (if (>= n (caar roman))
  2258. (setq n (- n (caar roman))
  2259. res (concat res (cdar roman)))
  2260. (pop roman)))
  2261. res)))
  2262. (defun org-export-first-sibling-p (headline info)
  2263. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2264. INFO is the plist used as a communication channel."
  2265. (not (eq (org-element-type (org-export-get-previous-element headline info))
  2266. 'headline)))
  2267. (defun org-export-last-sibling-p (headline info)
  2268. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2269. INFO is the plist used as a communication channel."
  2270. (not (org-export-get-next-element headline info)))
  2271. ;;;; For Links
  2272. ;; `org-export-solidify-link-text' turns a string into a safer version
  2273. ;; for links, replacing most non-standard characters with hyphens.
  2274. ;; `org-export-get-coderef-format' returns an appropriate format
  2275. ;; string for coderefs.
  2276. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2277. ;; provided should be considered as an inline image.
  2278. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2279. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2280. ;; returns an appropriate unique identifier when found, or nil.
  2281. ;; `org-export-resolve-id-link' returns the first headline with
  2282. ;; specified id or custom-id in parse tree, or nil when none was
  2283. ;; found.
  2284. ;; `org-export-resolve-coderef' associates a reference to a line
  2285. ;; number in the element it belongs, or returns the reference itself
  2286. ;; when the element isn't numbered.
  2287. (defun org-export-solidify-link-text (s)
  2288. "Take link text S and make a safe target out of it."
  2289. (save-match-data
  2290. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2291. (defun org-export-get-coderef-format (path desc)
  2292. "Return format string for code reference link.
  2293. PATH is the link path. DESC is its description."
  2294. (save-match-data
  2295. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  2296. (replace-match "%s" t t desc))
  2297. ((string= desc "") "%s")
  2298. (t desc))))
  2299. (defun org-export-inline-image-p (link &optional rules)
  2300. "Non-nil if LINK object points to an inline image.
  2301. Optional argument is a set of RULES defining inline images. It
  2302. is an alist where associations have the following shape:
  2303. \(TYPE . REGEXP)
  2304. Applying a rule means apply REGEXP against LINK's path when its
  2305. type is TYPE. The function will return a non-nil value if any of
  2306. the provided rules is non-nil. The default rule is
  2307. `org-export-default-inline-image-rule'.
  2308. This only applies to links without a description."
  2309. (and (not (org-element-contents link))
  2310. (let ((case-fold-search t)
  2311. (rules (or rules org-export-default-inline-image-rule)))
  2312. (some
  2313. (lambda (rule)
  2314. (and (string= (org-element-property :type link) (car rule))
  2315. (string-match (cdr rule)
  2316. (org-element-property :path link))))
  2317. rules))))
  2318. (defun org-export-resolve-fuzzy-link (link info)
  2319. "Return LINK destination.
  2320. INFO is a plist holding contextual information.
  2321. Return value can be an object, an element, or nil:
  2322. - If LINK path exactly matches any target, return the target
  2323. object.
  2324. - If LINK path exactly matches any headline name, return that
  2325. element. If more than one headline share that name, priority
  2326. will be given to the one with the closest common ancestor, if
  2327. any, or the first one in the parse tree otherwise.
  2328. - Otherwise, return nil.
  2329. Assume LINK type is \"fuzzy\"."
  2330. (let ((path (org-element-property :path link)))
  2331. ;; Link points to a target: return it.
  2332. (or (loop for target in (plist-get info :target-list)
  2333. when (string= (org-element-property :raw-value target) path)
  2334. return target)
  2335. ;; Link either points to an headline or nothing. Try to find
  2336. ;; the source, with priority given to headlines with the closest
  2337. ;; common ancestor. If such candidate is found, return its
  2338. ;; beginning position as an unique identifier, otherwise return
  2339. ;; nil.
  2340. (let ((find-headline
  2341. (function
  2342. ;; Return first headline whose `:raw-value' property
  2343. ;; is NAME in parse tree DATA, or nil.
  2344. (lambda (name data)
  2345. (org-element-map
  2346. data 'headline
  2347. (lambda (headline)
  2348. (when (string=
  2349. (org-element-property :raw-value headline)
  2350. name)
  2351. headline))
  2352. info 'first-match)))))
  2353. ;; Search among headlines sharing an ancestor with link,
  2354. ;; from closest to farthest.
  2355. (or (catch 'exit
  2356. (mapc
  2357. (lambda (parent)
  2358. (when (eq (org-element-type parent) 'headline)
  2359. (let ((foundp (funcall find-headline path parent)))
  2360. (when foundp (throw 'exit foundp)))))
  2361. (org-export-get-genealogy link info)) nil)
  2362. ;; No match with a common ancestor: try the full parse-tree.
  2363. (funcall find-headline path (plist-get info :parse-tree)))))))
  2364. (defun org-export-resolve-id-link (link info)
  2365. "Return headline referenced as LINK destination.
  2366. INFO is a plist used as a communication channel.
  2367. Return value can be an headline element or nil. Assume LINK type
  2368. is either \"id\" or \"custom-id\"."
  2369. (let ((id (org-element-property :path link)))
  2370. (org-element-map
  2371. (plist-get info :parse-tree) 'headline
  2372. (lambda (headline)
  2373. (when (or (string= (org-element-property :id headline) id)
  2374. (string= (org-element-property :custom-id headline) id))
  2375. headline))
  2376. info 'first-match)))
  2377. (defun org-export-resolve-ref-link (link info)
  2378. "Return element referenced as LINK destination.
  2379. INFO is a plist used as a communication channel.
  2380. Assume LINK type is \"ref\" and. Return value is the first
  2381. element whose `:name' property matches LINK's `:path', or nil."
  2382. (let ((name (org-element-property :path link)))
  2383. (org-element-map
  2384. (plist-get info :parse-tree) org-element-all-elements
  2385. (lambda (el)
  2386. (when (string= (org-element-property :name el) name) el))
  2387. info 'first-match)))
  2388. (defun org-export-resolve-coderef (ref info)
  2389. "Resolve a code reference REF.
  2390. INFO is a plist used as a communication channel.
  2391. Return associated line number in source code, or REF itself,
  2392. depending on src-block or example element's switches."
  2393. (org-element-map
  2394. (plist-get info :parse-tree) '(src-block example)
  2395. (lambda (el)
  2396. (let ((switches (or (org-element-property :switches el) "")))
  2397. (with-temp-buffer
  2398. (insert (org-trim (org-element-property :value el)))
  2399. ;; Build reference regexp.
  2400. (let* ((label
  2401. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2402. (match-string 1 switches))
  2403. org-coderef-label-format))
  2404. (ref-re
  2405. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2406. (replace-regexp-in-string "%s" ref label nil t))))
  2407. ;; Element containing REF is found. Only associate REF to
  2408. ;; a line number if element has "+n" or "-n" and "-k" or
  2409. ;; "-r" as switches. When it has "+n", count accumulated
  2410. ;; locs before, too.
  2411. (when (re-search-backward ref-re nil t)
  2412. (cond
  2413. ((not (string-match "-[kr]\\>" switches)) ref)
  2414. ((string-match "-n\\>" switches) (line-number-at-pos))
  2415. ((string-match "\\+n\\>" switches)
  2416. (+ (org-export-get-loc el info) (line-number-at-pos)))
  2417. (t ref)))))))
  2418. info 'first-match))
  2419. ;;;; For Macros
  2420. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2421. (defun org-export-expand-macro (macro info)
  2422. "Expand MACRO and return it as a string.
  2423. INFO is a plist holding export options."
  2424. (let* ((key (org-element-property :key macro))
  2425. (args (org-element-property :args macro))
  2426. ;; User's macros are stored in the communication channel with
  2427. ;; a ":macro-" prefix. If it's a string leave it as-is.
  2428. ;; Otherwise, it's a secondary string that needs to be
  2429. ;; expanded recursively.
  2430. (value
  2431. (let ((val (plist-get info (intern (format ":macro-%s" key)))))
  2432. (if (stringp val) val
  2433. (org-export-secondary-string
  2434. val (plist-get info :back-end) info)))))
  2435. ;; Replace arguments in VALUE.
  2436. (let ((s 0) n)
  2437. (while (string-match "\\$\\([0-9]+\\)" value s)
  2438. (setq s (1+ (match-beginning 0))
  2439. n (string-to-number (match-string 1 value)))
  2440. (and (>= (length args) n)
  2441. (setq value (replace-match (nth (1- n) args) t t value)))))
  2442. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2443. (when (string-match "\\`(eval\\>" value)
  2444. (setq value (eval (read value))))
  2445. ;; Return string.
  2446. (format "%s" (or value ""))))
  2447. ;;;; For References
  2448. ;; `org-export-get-ordinal' associates a sequence number to any object
  2449. ;; or element.
  2450. (defun org-export-get-ordinal
  2451. (element info &optional types within-section predicate)
  2452. "Return ordinal number of an element or object.
  2453. ELEMENT is the element or object considered. INFO is the plist
  2454. used as a communication channel.
  2455. Optional argument TYPES, when non-nil, is a list of element or
  2456. object types, as symbols, that should also be counted in.
  2457. Otherwise, only provided element's type is considered.
  2458. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2459. to the section containing ELEMENT.
  2460. Optional argument PREDICATE is a function returning a non-nil
  2461. value if the current element or object should be counted in. It
  2462. accepts one argument: the element or object being considered.
  2463. This argument allows to count only a certain type of objects,
  2464. like inline images, which are a subset of links \(in that case,
  2465. `org-export-inline-image-p' might be an useful predicate\)."
  2466. (let ((counter 0)
  2467. ;; Determine if search should apply to current section, in
  2468. ;; which case it should be retrieved first, or to full parse
  2469. ;; tree. As a special case, an element or object without
  2470. ;; a parent headline will also trigger a full search,
  2471. ;; notwithstanding WITHIN-SECTION value.
  2472. (data
  2473. (if (not within-section) (plist-get info :parse-tree)
  2474. (or (org-export-get-parent-headline element info)
  2475. (plist-get info :parse-tree)))))
  2476. ;; Increment counter until ELEMENT is found again.
  2477. (org-element-map
  2478. data (or types (org-element-type element))
  2479. (lambda (el)
  2480. (cond
  2481. ((equal element el) (1+ counter))
  2482. ((not predicate) (incf counter) nil)
  2483. ((funcall predicate el) (incf counter) nil)))
  2484. info 'first-match)))
  2485. ;;;; For Src-Blocks
  2486. ;; `org-export-get-loc' counts number of code lines accumulated in
  2487. ;; src-block or example-block elements with a "+n" switch until
  2488. ;; a given element, excluded. Note: "-n" switches reset that count.
  2489. ;; `org-export-handle-code' takes care of line numbering and reference
  2490. ;; cleaning in source code, when appropriate.
  2491. (defun org-export-get-loc (element info)
  2492. "Return accumulated lines of code up to ELEMENT.
  2493. INFO is the plist used as a communication channel.
  2494. ELEMENT is excluded from count."
  2495. (let ((loc 0))
  2496. (org-element-map
  2497. (plist-get info :parse-tree)
  2498. `(src-block example-block ,(org-element-type element))
  2499. (lambda (el)
  2500. (cond
  2501. ;; ELEMENT is reached: Quit the loop.
  2502. ((equal el element) t)
  2503. ;; Only count lines from src-block and example-block elements
  2504. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2505. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  2506. ((let ((switches (org-element-property :switches el)))
  2507. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2508. ;; Accumulate locs or reset them.
  2509. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2510. (lines (org-count-lines
  2511. (org-trim (org-element-property :value el)))))
  2512. (setq loc (if accumulatep lines (+ loc lines))))))
  2513. ;; Return nil to stay in the loop.
  2514. nil)))
  2515. info 'first-match)
  2516. ;; Return value.
  2517. loc))
  2518. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2519. "Handle line numbers and code references in ELEMENT.
  2520. ELEMENT has either a `src-block' an `example-block' type. INFO
  2521. is a plist used as a communication channel.
  2522. If optional argument NUM-FMT is a string, it will be used as
  2523. a format string for numbers at beginning of each line.
  2524. If optional argument REF-FMT is a string, it will be used as
  2525. a format string for each line of code containing a reference.
  2526. When optional argument DELAYED is non-nil, `org-loc' and
  2527. `org-coderef' properties, set to an adequate value, are applied
  2528. to, respectively, numbered lines and lines with a reference. No
  2529. line numbering is done and all references are stripped from the
  2530. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2531. in that situation.
  2532. Return new code as a string."
  2533. (let* ((switches (or (org-element-property :switches element) ""))
  2534. (code (org-element-property :value element))
  2535. (numberp (string-match "[-+]n\\>" switches))
  2536. (accumulatep (string-match "\\+n\\>" switches))
  2537. ;; Initialize loc counter when any kind of numbering is
  2538. ;; active.
  2539. (total-LOC (cond
  2540. (accumulatep (org-export-get-loc element info))
  2541. (numberp 0)))
  2542. ;; Get code and clean it. Remove blank lines at its
  2543. ;; beginning and end. Also remove protective commas.
  2544. (preserve-indent-p (or org-src-preserve-indentation
  2545. (string-match "-i\\>" switches)))
  2546. (replace-labels (when (string-match "-r\\>" switches)
  2547. (if (string-match "-k\\>" switches) 'keep t)))
  2548. (code (let ((c (replace-regexp-in-string
  2549. "\\`\\([ \t]*\n\\)+" ""
  2550. (replace-regexp-in-string
  2551. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2552. ;; If appropriate, remove global indentation.
  2553. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2554. ;; Free up the protected lines. Note: Org blocks
  2555. ;; have commas at the beginning or every line.
  2556. (if (string=
  2557. (or (org-element-property :language element) "")
  2558. "org")
  2559. (replace-regexp-in-string "^," "" c)
  2560. (replace-regexp-in-string
  2561. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2562. ;; Split code to process it line by line.
  2563. (code-lines (org-split-string code "\n"))
  2564. ;; If numbering is active, ensure line numbers will be
  2565. ;; correctly padded before applying the format string.
  2566. (num-fmt
  2567. (when (and (not delayed) numberp)
  2568. (format (if (stringp num-fmt) num-fmt "%s: ")
  2569. (format "%%%ds"
  2570. (length (number-to-string
  2571. (+ (length code-lines) total-LOC)))))))
  2572. ;; Get format used for references.
  2573. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2574. (match-string 1 switches))
  2575. org-coderef-label-format))
  2576. ;; Build a regexp matching a loc with a reference.
  2577. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2578. (replace-regexp-in-string
  2579. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2580. (org-element-normalize-string
  2581. (mapconcat
  2582. (lambda (loc)
  2583. ;; Maybe add line number to current line of code (LOC).
  2584. (when numberp
  2585. (incf total-LOC)
  2586. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2587. (concat (format num-fmt total-LOC) loc))))
  2588. ;; Take action if at a ref line.
  2589. (when (string-match with-ref-re loc)
  2590. (let ((ref (match-string 3 loc)))
  2591. (setq loc
  2592. ;; Option "-r" without "-k" removes labels.
  2593. ;; A non-nil DELAYED removes labels unconditionally.
  2594. (if (or delayed
  2595. (and replace-labels (not (eq replace-labels 'keep))))
  2596. (replace-match "" nil nil loc 1)
  2597. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2598. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2599. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2600. ;; If REF-FMT is defined, apply it to current LOC.
  2601. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2602. ;; Return updated LOC for concatenation.
  2603. loc)
  2604. code-lines "\n"))))
  2605. ;;;; For Tables
  2606. ;; `org-export-table-format-info' extracts formatting information
  2607. ;; (alignment, column groups and presence of a special column) from
  2608. ;; a raw table and returns it as a property list.
  2609. ;;
  2610. ;; `org-export-clean-table' cleans the raw table from any Org
  2611. ;; table-specific syntax.
  2612. (defun org-export-table-format-info (table)
  2613. "Extract info from TABLE.
  2614. Return a plist whose properties and values are:
  2615. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2616. `:column-groups' vector of symbols among `start', `end', `start-end',
  2617. `:row-groups' list of integers representing row groups.
  2618. `:special-column-p' non-nil if table has a special column.
  2619. `:width' vector of integers representing desired width of
  2620. current column, or nil."
  2621. (with-temp-buffer
  2622. (insert table)
  2623. (goto-char 1)
  2624. (org-table-align)
  2625. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2626. org-table-last-alignment)))
  2627. (width (make-vector (length org-table-last-alignment) nil))
  2628. (colgroups (make-vector (length org-table-last-alignment) nil))
  2629. (row-group 0)
  2630. (rowgroups)
  2631. (special-column-p 'empty))
  2632. (mapc (lambda (row)
  2633. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2634. (incf row-group)
  2635. ;; Determine if a special column is present by looking
  2636. ;; for special markers in the first column. More
  2637. ;; accurately, the first column is considered special
  2638. ;; if it only contains special markers and, maybe,
  2639. ;; empty cells.
  2640. (setq special-column-p
  2641. (cond
  2642. ((not special-column-p) nil)
  2643. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2644. 'special)
  2645. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2646. (cond
  2647. ;; Read forced alignment and width information, if any,
  2648. ;; and determine final alignment for the table.
  2649. ((org-table-cookie-line-p row)
  2650. (let ((col 0))
  2651. (mapc (lambda (field)
  2652. (when (string-match
  2653. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2654. (let ((align-data (match-string 1 field)))
  2655. (when align-data (aset align col align-data)))
  2656. (let ((w-data (match-string 2 field)))
  2657. (when w-data
  2658. (aset width col (string-to-number w-data)))))
  2659. (incf col))
  2660. (org-split-string row "[ \t]*|[ \t]*"))))
  2661. ;; Read column groups information.
  2662. ((org-table-colgroup-line-p row)
  2663. (let ((col 0))
  2664. (mapc (lambda (field)
  2665. (aset colgroups col
  2666. (cond ((string= "<" field) 'start)
  2667. ((string= ">" field) 'end)
  2668. ((string= "<>" field) 'start-end)))
  2669. (incf col))
  2670. (org-split-string row "[ \t]*|[ \t]*"))))
  2671. ;; Contents line.
  2672. (t (push row-group rowgroups))))
  2673. (org-split-string table "\n"))
  2674. ;; Return plist.
  2675. (list :alignment align
  2676. :column-groups colgroups
  2677. :row-groups (reverse rowgroups)
  2678. :special-column-p (eq special-column-p 'special)
  2679. :width width))))
  2680. (defun org-export-clean-table (table specialp)
  2681. "Clean string TABLE from its formatting elements.
  2682. Remove any row containing column groups or formatting cookies and
  2683. rows starting with a special marker. If SPECIALP is non-nil,
  2684. assume the table contains a special formatting column and remove
  2685. it also."
  2686. (let ((rows (org-split-string table "\n")))
  2687. (mapconcat 'identity
  2688. (delq nil
  2689. (mapcar
  2690. (lambda (row)
  2691. (cond
  2692. ((org-table-colgroup-line-p row) nil)
  2693. ((org-table-cookie-line-p row) nil)
  2694. ;; Ignore rows starting with a special marker.
  2695. ((string-match "^[ \t]*| *[!_^/$] *|" row) nil)
  2696. ;; Remove special column.
  2697. ((and specialp
  2698. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2699. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2700. (replace-match "\\1|" t nil row))
  2701. (t row)))
  2702. rows))
  2703. "\n")))
  2704. ;;;; For Tables Of Contents
  2705. ;; `org-export-collect-headlines' builds a list of all exportable
  2706. ;; headline elements, maybe limited to a certain depth. One can then
  2707. ;; easily parse it and transcode it.
  2708. ;; Building lists of tables, figures or listings is quite similar.
  2709. ;; Once the generic function `org-export-collect-elements' is defined,
  2710. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2711. ;; `org-export-collect-listings' can be derived from it.
  2712. (defun org-export-collect-headlines (info &optional n)
  2713. "Collect headlines in order to build a table of contents.
  2714. INFO is a plist used as a communication channel.
  2715. When non-nil, optional argument N must be an integer. It
  2716. specifies the depth of the table of contents.
  2717. Return a list of all exportable headlines as parsed elements."
  2718. (org-element-map
  2719. (plist-get info :parse-tree)
  2720. 'headline
  2721. (lambda (headline)
  2722. ;; Strip contents from HEADLINE.
  2723. (let ((relative-level (org-export-get-relative-level headline info)))
  2724. (unless (and n (> relative-level n)) headline)))
  2725. info))
  2726. (defun org-export-collect-elements (type info &optional predicate)
  2727. "Collect referenceable elements of a determined type.
  2728. TYPE can be a symbol or a list of symbols specifying element
  2729. types to search. Only elements with a caption or a name are
  2730. collected.
  2731. INFO is a plist used as a communication channel.
  2732. When non-nil, optional argument PREDICATE is a function accepting
  2733. one argument, an element of type TYPE. It returns a non-nil
  2734. value when that element should be collected.
  2735. Return a list of all elements found, in order of appearance."
  2736. (org-element-map
  2737. (plist-get info :parse-tree) type
  2738. (lambda (element)
  2739. (and (or (org-element-property :caption element)
  2740. (org-element-property :name element))
  2741. (or (not predicate) (funcall predicate element))
  2742. element)) info))
  2743. (defun org-export-collect-tables (info)
  2744. "Build a list of tables.
  2745. INFO is a plist used as a communication channel.
  2746. Return a list of table elements with a caption or a name
  2747. affiliated keyword."
  2748. (org-export-collect-elements 'table info))
  2749. (defun org-export-collect-figures (info predicate)
  2750. "Build a list of figures.
  2751. INFO is a plist used as a communication channel. PREDICATE is
  2752. a function which accepts one argument: a paragraph element and
  2753. whose return value is non-nil when that element should be
  2754. collected.
  2755. A figure is a paragraph type element, with a caption or a name,
  2756. verifying PREDICATE. The latter has to be provided since
  2757. a \"figure\" is a vague concept that may depend on back-end.
  2758. Return a list of elements recognized as figures."
  2759. (org-export-collect-elements 'paragraph info predicate))
  2760. (defun org-export-collect-listings (info)
  2761. "Build a list of src blocks.
  2762. INFO is a plist used as a communication channel.
  2763. Return a list of src-block elements with a caption or a name
  2764. affiliated keyword."
  2765. (org-export-collect-elements 'src-block info))
  2766. ;;;; Topology
  2767. ;; Here are various functions to retrieve information about the
  2768. ;; neighbourhood of a given element or object. Neighbours of interest
  2769. ;; are direct parent (`org-export-get-parent'), parent headline
  2770. ;; (`org-export-get-parent-headline'), parent paragraph
  2771. ;; (`org-export-get-parent-paragraph'), previous element or object
  2772. ;; (`org-export-get-previous-element') and next element or object
  2773. ;; (`org-export-get-next-element').
  2774. ;; All of these functions are just a specific use of the more generic
  2775. ;; `org-export-get-genealogy', which returns the genealogy relative to
  2776. ;; the element or object.
  2777. (defun org-export-get-genealogy (blob info)
  2778. "Return genealogy relative to a given element or object.
  2779. BLOB is the element or object being considered. INFO is a plist
  2780. used as a communication channel."
  2781. (let* ((type (org-element-type blob))
  2782. (end (org-element-property :end blob))
  2783. (walk-data
  2784. (lambda (data genealogy)
  2785. ;; Walk DATA, looking for BLOB. GENEALOGY is the list of
  2786. ;; parents of all elements in DATA.
  2787. (mapc
  2788. (lambda (el)
  2789. (cond
  2790. ((stringp el) nil)
  2791. ((equal el blob) (throw 'exit genealogy))
  2792. ((>= (org-element-property :end el) end)
  2793. ;; If BLOB is an object and EL contains a secondary
  2794. ;; string, be sure to check it.
  2795. (when (memq type org-element-all-objects)
  2796. (let ((sec-prop
  2797. (cdr (assq (org-element-type el)
  2798. org-element-secondary-value-alist))))
  2799. (when sec-prop
  2800. (funcall
  2801. walk-data
  2802. (cons 'org-data
  2803. (cons nil (org-element-property sec-prop el)))
  2804. (cons el genealogy)))))
  2805. (funcall walk-data el (cons el genealogy)))))
  2806. (org-element-contents data)))))
  2807. (catch 'exit (funcall walk-data (plist-get info :parse-tree) nil) nil)))
  2808. (defun org-export-get-parent (blob info)
  2809. "Return BLOB parent or nil.
  2810. BLOB is the element or object considered. INFO is a plist used
  2811. as a communication channel."
  2812. (car (org-export-get-genealogy blob info)))
  2813. (defun org-export-get-parent-headline (blob info)
  2814. "Return closest parent headline or nil.
  2815. BLOB is the element or object being considered. INFO is a plist
  2816. used as a communication channel."
  2817. (catch 'exit
  2818. (mapc
  2819. (lambda (el) (when (eq (org-element-type el) 'headline) (throw 'exit el)))
  2820. (org-export-get-genealogy blob info))
  2821. nil))
  2822. (defun org-export-get-parent-paragraph (object info)
  2823. "Return parent paragraph or nil.
  2824. INFO is a plist used as a communication channel.
  2825. Optional argument OBJECT, when provided, is the object to consider.
  2826. Otherwise, return the paragraph containing current object.
  2827. This is useful for objects, which share attributes with the
  2828. paragraph containing them."
  2829. (catch 'exit
  2830. (mapc
  2831. (lambda (el) (when (eq (org-element-type el) 'paragraph) (throw 'exit el)))
  2832. (org-export-get-genealogy object info))
  2833. nil))
  2834. (defun org-export-get-previous-element (blob info)
  2835. "Return previous element or object.
  2836. BLOB is an element or object. INFO is a plist used as
  2837. a communication channel.
  2838. Return previous element or object, a string, or nil."
  2839. (let ((parent (org-export-get-parent blob info)))
  2840. (cadr (member blob (reverse (org-element-contents parent))))))
  2841. (defun org-export-get-next-element (blob info)
  2842. "Return next element or object.
  2843. BLOB is an element or object. INFO is a plist used as
  2844. a communication channel.
  2845. Return next element or object, a string, or nil."
  2846. (let ((parent (org-export-get-parent blob info)))
  2847. (cadr (member blob (org-element-contents parent)))))
  2848. ;;; The Dispatcher
  2849. ;; `org-export-dispatch' is the standard interactive way to start an
  2850. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2851. ;; for its interface. Most commons back-ends should have an entry in
  2852. ;; it.
  2853. (defun org-export-dispatch ()
  2854. "Export dispatcher for Org mode.
  2855. It provides an access to common export related tasks in a buffer.
  2856. Its interface comes in two flavours: standard and expert. While
  2857. both share the same set of bindings, only the former displays the
  2858. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2859. to switch to one or the other.
  2860. Return an error if key pressed has no associated command."
  2861. (interactive)
  2862. (let* ((input (org-export-dispatch-ui
  2863. (if (listp org-export-initial-scope) org-export-initial-scope
  2864. (list org-export-initial-scope))
  2865. org-export-dispatch-use-expert-ui))
  2866. (raw-key (car input))
  2867. (optns (cdr input)))
  2868. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2869. ;; depending on user's key pressed.
  2870. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2871. ;; Allow to quit with "q" key.
  2872. (?q nil)
  2873. ;; Export with `e-ascii' back-end.
  2874. ((?A ?N ?U)
  2875. (let ((outbuf
  2876. (org-export-to-buffer
  2877. 'e-ascii "*Org E-ASCII Export*"
  2878. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2879. `(:ascii-charset
  2880. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  2881. (with-current-buffer outbuf (text-mode))
  2882. (when org-export-show-temporary-export-buffer
  2883. (switch-to-buffer-other-window outbuf))))
  2884. ((?a ?n ?u)
  2885. (org-e-ascii-export-to-ascii
  2886. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2887. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  2888. ;; Export with `e-latex' back-end.
  2889. (?L
  2890. (let ((outbuf
  2891. (org-export-to-buffer
  2892. 'e-latex "*Org E-LaTeX Export*"
  2893. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2894. (with-current-buffer outbuf (latex-mode))
  2895. (when org-export-show-temporary-export-buffer
  2896. (switch-to-buffer-other-window outbuf))))
  2897. (?l (org-e-latex-export-to-latex
  2898. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2899. (?p (org-e-latex-export-to-pdf
  2900. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2901. (?d (org-open-file
  2902. (org-e-latex-export-to-pdf
  2903. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2904. ;; Export with `e-html' back-end.
  2905. (?H
  2906. (let ((outbuf
  2907. (org-export-to-buffer
  2908. 'e-html "*Org E-HTML Export*"
  2909. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2910. ;; set major mode
  2911. (with-current-buffer outbuf
  2912. (if (featurep 'nxhtml-mode) (nxhtml-mode) (nxml-mode)))
  2913. (when org-export-show-temporary-export-buffer
  2914. (switch-to-buffer-other-window outbuf))))
  2915. (?h (org-e-html-export-to-html
  2916. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2917. (?b (org-open-file
  2918. (org-e-html-export-to-html
  2919. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2920. ;; Export with `e-odt' back-end.
  2921. (?o (org-e-odt-export-to-odt
  2922. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2923. (?O (org-open-file
  2924. (org-e-odt-export-to-odt
  2925. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2926. ;; Publishing facilities
  2927. (?F (org-e-publish-current-file (memq 'force optns)))
  2928. (?P (org-e-publish-current-project (memq 'force optns)))
  2929. (?X (let ((project
  2930. (assoc (org-icompleting-read
  2931. "Publish project: " org-e-publish-project-alist nil t)
  2932. org-e-publish-project-alist)))
  2933. (org-e-publish project (memq 'force optns))))
  2934. (?E (org-e-publish-all (memq 'force optns)))
  2935. ;; Undefined command.
  2936. (t (error "No command associated with key %s"
  2937. (char-to-string raw-key))))))
  2938. (defun org-export-dispatch-ui (options expertp)
  2939. "Handle interface for `org-export-dispatch'.
  2940. OPTIONS is a list containing current interactive options set for
  2941. export. It can contain any of the following symbols:
  2942. `body' toggles a body-only export
  2943. `subtree' restricts export to current subtree
  2944. `visible' restricts export to visible part of buffer.
  2945. `force' force publishing files.
  2946. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2947. buffer is provided, but indications about currently active
  2948. options are given in the prompt. Moreover, \[?] allows to switch
  2949. back to standard interface.
  2950. Return value is a list with key pressed as CAR and a list of
  2951. final interactive export options as CDR."
  2952. (let ((help
  2953. (format "---- (Options) -------------------------------------------
  2954. \[1] Body only: %s [2] Export scope: %s
  2955. \[3] Visible only: %s [4] Force publishing: %s
  2956. --- (ASCII/Latin-1/UTF-8 Export) -------------------------
  2957. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  2958. --- (HTML Export) ----------------------------------------
  2959. \[h] to HTML file [b] ... and open it
  2960. \[H] to temporary buffer
  2961. --- (LaTeX Export) ---------------------------------------
  2962. \[l] to TEX file [L] to temporary buffer
  2963. \[p] to PDF file [d] ... and open it
  2964. --- (ODF Export) -----------------------------------------
  2965. \[o] to ODT file [O] ... and open it
  2966. --- (Publish) --------------------------------------------
  2967. \[F] current file [P] current project
  2968. \[X] a project [E] every project"
  2969. (if (memq 'body options) "On " "Off")
  2970. (if (memq 'subtree options) "Subtree" "Buffer ")
  2971. (if (memq 'visible options) "On " "Off")
  2972. (if (memq 'force options) "On " "Off")))
  2973. (standard-prompt "Export command: ")
  2974. (expert-prompt (format "Export command (%s%s%s%s): "
  2975. (if (memq 'body options) "b" "-")
  2976. (if (memq 'subtree options) "s" "-")
  2977. (if (memq 'visible options) "v" "-")
  2978. (if (memq 'force options) "f" "-")))
  2979. (handle-keypress
  2980. (function
  2981. ;; Read a character from command input, toggling interactive
  2982. ;; options when applicable. PROMPT is the displayed prompt,
  2983. ;; as a string.
  2984. (lambda (prompt)
  2985. (let ((key (read-char-exclusive prompt)))
  2986. (cond
  2987. ;; Ignore non-standard characters (i.e. "M-a").
  2988. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  2989. ;; Help key: Switch back to standard interface if
  2990. ;; expert UI was active.
  2991. ((eq key ??) (org-export-dispatch-ui options nil))
  2992. ;; Toggle export options.
  2993. ((memq key '(?1 ?2 ?3 ?4))
  2994. (org-export-dispatch-ui
  2995. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  2996. (?4 'force))))
  2997. (if (memq option options) (remq option options)
  2998. (cons option options)))
  2999. expertp))
  3000. ;; Action selected: Send key and options back to
  3001. ;; `org-export-dispatch'.
  3002. (t (cons key options))))))))
  3003. ;; With expert UI, just read key with a fancy prompt. In standard
  3004. ;; UI, display an intrusive help buffer.
  3005. (if expertp (funcall handle-keypress expert-prompt)
  3006. (save-window-excursion
  3007. (delete-other-windows)
  3008. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  3009. (org-fit-window-to-buffer
  3010. (get-buffer-window "*Org Export/Publishing Help*"))
  3011. (funcall handle-keypress standard-prompt)))))
  3012. (provide 'org-export)
  3013. ;;; org-export.el ends here