org-export.el 158 KB

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