org-export.el 158 KB

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