org-export.el 152 KB

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