org-export.el 171 KB

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