org-export.el 160 KB

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