org-export.el 174 KB

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