org-export.el 128 KB

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