org-export.el 153 KB

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