org-export.el 170 KB

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