org-export.el 176 KB

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