org-export.el 189 KB

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