org-export.el 149 KB

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