org-export.el 207 KB

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