org-export.el 162 KB

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