org-export.el 176 KB

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