org-export.el 157 KB

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