org-export.el 151 KB

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