org-export.el 150 KB

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