org-export.el 177 KB

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