org-export.el 129 KB

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