org-export.el 108 KB

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