org-export.el 136 KB

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