org-export.el 162 KB

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