org-export.el 185 KB

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