org-export.el 160 KB

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