org-export.el 157 KB

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