org-export.el 149 KB

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