org-export.el 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  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-alist' :: Alist between headlines raw name and their
  500. ;; boundaries. It is used to resolve "fuzzy" links
  501. ;; (cf. `org-export-resolve-fuzzy-link').
  502. ;; - category :: tree
  503. ;; - type :: alist (STRING INTEGER INTEGER)
  504. ;; + `headline-levels' :: Maximum level being exported as an
  505. ;; headline. Comparison is done with the relative level of
  506. ;; headlines in the parse tree, not necessarily with their
  507. ;; actual level.
  508. ;; - category :: option
  509. ;; - type :: integer
  510. ;; + `headline-offset' :: Difference between relative and real level
  511. ;; of headlines in the parse tree. For example, a value of -1
  512. ;; means a level 2 headline should be considered as level
  513. ;; 1 (cf. `org-export-get-relative-level').
  514. ;; - category :: tree
  515. ;; - type :: integer
  516. ;; + `headline-numbering' :: Alist between headlines' beginning
  517. ;; position and their numbering, as a list of numbers
  518. ;; (cf. `org-export-get-headline-number').
  519. ;; - category :: tree
  520. ;; - type :: alist (INTEGER . LIST)
  521. ;; + `included-files' :: List of files, with full path, included in
  522. ;; the current buffer, through the "#+include:" keyword. It is
  523. ;; mainly used to verify that no infinite recursive inclusion
  524. ;; happens.
  525. ;; - category :: local
  526. ;; - type :: list of strings
  527. ;; + `keywords' :: List of keywords attached to data.
  528. ;; - category :: option
  529. ;; - type :: string
  530. ;; + `language' :: Default language used for translations.
  531. ;; - category :: option
  532. ;; - type :: string
  533. ;; + `parse-tree' :: Whole parse tree, available at any time during
  534. ;; transcoding.
  535. ;; - category :: global
  536. ;; - type :: list (as returned by `org-element-parse-buffer')
  537. ;; + `point-max' :: Last ending position in the parse tree.
  538. ;; - category :: global
  539. ;; - type :: integer
  540. ;; + `preserve-breaks' :: Non-nil means transcoding should preserve
  541. ;; all line breaks.
  542. ;; - category :: option
  543. ;; - type :: symbol (nil, t)
  544. ;; + `section-numbers' :: Non-nil means transcoding should add
  545. ;; section numbers to headlines.
  546. ;; - category :: option
  547. ;; - type :: symbol (nil, t)
  548. ;; + `select-tags' :: List of tags enforcing inclusion of sub-trees in
  549. ;; transcoding. When such a tag is present,
  550. ;; subtrees without it are de facto excluded from
  551. ;; the process. See `use-select-tags'.
  552. ;; - category :: option
  553. ;; - type :: list of strings
  554. ;; + `target-list' :: List of targets raw names encoutered in the
  555. ;; parse tree. This is used to partly resolve
  556. ;; "fuzzy" links
  557. ;; (cf. `org-export-resolve-fuzzy-link').
  558. ;; - category :: tree
  559. ;; - type :: list of strings
  560. ;; + `time-stamp-file' :: Non-nil means transcoding should insert
  561. ;; a time stamp in the output.
  562. ;; - category :: option
  563. ;; - type :: symbol (nil, t)
  564. ;; + `use-select-tags' :: When non-nil, a select tags has been found
  565. ;; in the parse tree. Thus, any headline without one will be
  566. ;; filtered out. See `select-tags'.
  567. ;; - category :: tree
  568. ;; - type :: interger or nil
  569. ;; + `with-archived-trees' :: Non-nil when archived subtrees should
  570. ;; also be transcoded. If it is set to the `headline' symbol,
  571. ;; only the archived headline's name is retained.
  572. ;; - category :: option
  573. ;; - type :: symbol (nil, t, `headline')
  574. ;; + `with-author' :: Non-nil means author's name should be included
  575. ;; in the output.
  576. ;; - category :: option
  577. ;; - type :: symbol (nil, t)
  578. ;; + `with-creator' :: Non-nild means a creation sentence should be
  579. ;; inserted at the end of the transcoded string. If the value
  580. ;; is `comment', it should be commented.
  581. ;; - category :: option
  582. ;; - type :: symbol (`comment', nil, t)
  583. ;; + `with-drawers' :: Non-nil means drawers should be exported. If
  584. ;; its value is a list of names, only drawers with such names
  585. ;; will be transcoded.
  586. ;; - category :: option
  587. ;; - type :: symbol (nil, t) or list of strings
  588. ;; + `with-email' :: Non-nil means output should contain author's
  589. ;; email.
  590. ;; - category :: option
  591. ;; - type :: symbol (nil, t)
  592. ;; + `with-emphasize' :: Non-nil means emphasized text should be
  593. ;; interpreted.
  594. ;; - category :: option
  595. ;; - type :: symbol (nil, t)
  596. ;; + `with-fixed-width' :: Non-nil if transcoder should interpret
  597. ;; strings starting with a colon as a fixed-with (verbatim)
  598. ;; area.
  599. ;; - category :: option
  600. ;; - type :: symbol (nil, t)
  601. ;; + `with-footnotes' :: Non-nil if transcoder should interpret
  602. ;; footnotes.
  603. ;; - category :: option
  604. ;; - type :: symbol (nil, t)
  605. ;; + `with-priority' :: Non-nil means transcoding should include
  606. ;; priority cookies.
  607. ;; - category :: option
  608. ;; - type :: symbol (nil, t)
  609. ;; + `with-special-strings' :: Non-nil means transcoding should
  610. ;; interpret special strings in plain text.
  611. ;; - category :: option
  612. ;; - type :: symbol (nil, t)
  613. ;; + `with-sub-superscript' :: Non-nil means transcoding should
  614. ;; interpret subscript and superscript. With a value of "{}",
  615. ;; only interpret those using curly brackets.
  616. ;; - category :: option
  617. ;; - type :: symbol (nil, {}, t)
  618. ;; + `with-tables' :: Non-nil means transcoding should interpret
  619. ;; tables.
  620. ;; - category :: option
  621. ;; - type :: symbol (nil, t)
  622. ;; + `with-tags' :: Non-nil means transcoding should keep tags in
  623. ;; headlines. A `not-in-toc' value will remove them
  624. ;; from the table of contents, if any, nonetheless.
  625. ;; - category :: option
  626. ;; - type :: symbol (nil, t, `not-in-toc')
  627. ;; + `with-tasks' :: Non-nil means transcoding should include
  628. ;; headlines with a TODO keyword. A `todo' value
  629. ;; will only include headlines with a todo type
  630. ;; keyword while a `done' value will do the
  631. ;; contrary. If a list of strings is provided, only
  632. ;; tasks with keywords belonging to that list will
  633. ;; be kept.
  634. ;; - category :: option
  635. ;; - type :: symbol (t, todo, done, nil) or list of strings
  636. ;; + `with-timestamps' :: Non-nil means transcoding should include
  637. ;; time stamps and associated keywords. Otherwise, completely
  638. ;; remove them.
  639. ;; - category :: option
  640. ;; - type :: symbol: (t, nil)
  641. ;; + `with-toc' :: Non-nil means that a table of contents has to be
  642. ;; added to the output. An integer value limits its
  643. ;; depth.
  644. ;; - category :: option
  645. ;; - type :: symbol (nil, t or integer)
  646. ;; + `with-todo-keywords' :: Non-nil means transcoding should
  647. ;; include TODO keywords.
  648. ;; - category :: option
  649. ;; - type :: symbol (nil, t)
  650. ;;;; Export Options
  651. ;; Export options come from five sources, in increasing precedence
  652. ;; order:
  653. ;; - Global variables,
  654. ;; - External options provided at export time,
  655. ;; - Options keyword symbols,
  656. ;; - Buffer keywords,
  657. ;; - Subtree properties.
  658. ;; The central internal function with regards to export options is
  659. ;; `org-export-collect-options'. It updates global variables with
  660. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  661. ;; the different sources.
  662. ;; The internal functions doing the retrieval are:
  663. ;; `org-export-parse-option-keyword' ,
  664. ;; `org-export-get-subtree-options' ,
  665. ;; `org-export-get-inbuffer-options' and
  666. ;; `org-export-get-global-options'.
  667. ;;
  668. ;; Some properties do not rely on the previous sources but still
  669. ;; depend on the original buffer are taken care of in
  670. ;; `org-export-initial-options'.
  671. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  672. ;; take care of the part relative to "#+BIND:" keywords.
  673. (defun org-export-collect-options (backend subtreep ext-plist)
  674. "Collect export options from the current buffer.
  675. BACKEND is a symbol specifying the back-end to use.
  676. When SUBTREEP is non-nil, assume the export is done against the
  677. current sub-tree.
  678. EXT-PLIST is a property list with external parameters overriding
  679. org-mode's default settings, but still inferior to file-local
  680. settings."
  681. ;; First install #+BIND variables.
  682. (org-export-install-letbind-maybe)
  683. ;; Get and prioritize export options...
  684. (let ((options (org-combine-plists
  685. ;; ... from global variables...
  686. (org-export-get-global-options backend)
  687. ;; ... from buffer's name (default title)...
  688. `(:title
  689. ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
  690. (and file
  691. (file-name-sans-extension
  692. (file-name-nondirectory file))))
  693. (buffer-name (buffer-base-buffer))))
  694. ;; ... from an external property list...
  695. ext-plist
  696. ;; ... from in-buffer settings...
  697. (org-export-get-inbuffer-options
  698. (org-with-wide-buffer (buffer-string)) backend
  699. (and buffer-file-name
  700. (org-remove-double-quotes buffer-file-name)))
  701. ;; ... and from subtree, when appropriate.
  702. (and subtreep
  703. (org-export-get-subtree-options)))))
  704. ;; Add initial options.
  705. (setq options (append (org-export-initial-options options) options))
  706. ;; Return plist.
  707. options))
  708. (defun org-export-parse-option-keyword (options backend)
  709. "Parse an OPTIONS line and return values as a plist.
  710. BACKEND is a symbol specifying the back-end to use."
  711. (let* ((all (append org-export-option-alist
  712. (let ((var (intern
  713. (format "org-%s-option-alist" backend))))
  714. (and (boundp var) (eval var)))))
  715. ;; Build an alist between #+OPTION: item and property-name.
  716. (alist (delq nil
  717. (mapcar (lambda (e)
  718. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  719. (car e))))
  720. all)))
  721. plist)
  722. (mapc (lambda (e)
  723. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  724. (car e)
  725. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  726. options)
  727. (setq plist (plist-put plist
  728. (cdr e)
  729. (car (read-from-string
  730. (match-string 2 options)))))))
  731. alist)
  732. plist))
  733. (defun org-export-get-subtree-options ()
  734. "Get export options in subtree at point.
  735. Assume point is at subtree's beginning.
  736. Return options as a plist."
  737. (let (prop plist)
  738. (when (setq prop (progn (looking-at org-todo-line-regexp)
  739. (or (save-match-data
  740. (org-entry-get (point) "EXPORT_TITLE"))
  741. (org-match-string-no-properties 3))))
  742. (setq plist (plist-put plist :title prop)))
  743. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  744. (setq plist (plist-put plist :text prop)))
  745. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  746. (setq plist (plist-put plist :author prop)))
  747. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  748. (setq plist (plist-put plist :date prop)))
  749. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  750. (setq plist (org-export-add-options-to-plist plist prop)))
  751. plist))
  752. (defun org-export-get-inbuffer-options (buffer-string backend files)
  753. "Return in-buffer options as a plist.
  754. BUFFER-STRING is the string of the buffer. BACKEND is a symbol
  755. specifying which back-end should be used."
  756. (let ((case-fold-search t) plist)
  757. ;; 1. Special keywords, as in `org-export-special-keywords'.
  758. (let ((start 0)
  759. (special-re (org-make-options-regexp org-export-special-keywords)))
  760. (while (string-match special-re buffer-string start)
  761. (setq start (match-end 0))
  762. (let ((key (upcase (org-match-string-no-properties 1 buffer-string)))
  763. ;; Special keywords do not have their value expanded.
  764. (val (org-match-string-no-properties 2 buffer-string)))
  765. (setq plist
  766. (org-combine-plists
  767. (cond
  768. ((string= key "SETUP_FILE")
  769. (let ((file (expand-file-name
  770. (org-remove-double-quotes (org-trim val)))))
  771. ;; Avoid circular dependencies.
  772. (unless (member file files)
  773. (org-export-get-inbuffer-options
  774. (org-file-contents file 'noerror)
  775. backend
  776. (cons file files)))))
  777. ((string= key "OPTIONS")
  778. (org-export-parse-option-keyword val backend))
  779. ((string= key "MACRO")
  780. (string-match "^\\([-a-zA-Z0-9_]+\\)[ \t]+\\(.*?[ \t]*$\\)"
  781. val)
  782. (plist-put nil
  783. (intern (concat ":macro-"
  784. (downcase (match-string 1 val))))
  785. (match-string 2 val))))
  786. plist)))))
  787. ;; 2. Standard options, as in `org-export-option-alist'.
  788. (let* ((all (append org-export-option-alist
  789. (let ((var (intern
  790. (format "org-%s-option-alist" backend))))
  791. (and (boundp var) (eval var)))))
  792. ;; Build alist between keyword name and property name.
  793. (alist (delq nil (mapcar (lambda (e)
  794. (when (nth 1 e) (cons (nth 1 e) (car e))))
  795. all)))
  796. ;; Build regexp matching all keywords associated to export
  797. ;; options. Note: the search is case insensitive.
  798. (opt-re (org-make-options-regexp
  799. (delq nil (mapcar (lambda (e) (nth 1 e)) all))))
  800. (start 0))
  801. (while (string-match opt-re buffer-string start)
  802. (setq start (match-end 0))
  803. (let* ((key (upcase (org-match-string-no-properties 1 buffer-string)))
  804. ;; Expand value, applying restrictions for keywords.
  805. (val (org-match-string-no-properties 2 buffer-string))
  806. (prop (cdr (assoc key alist)))
  807. (behaviour (nth 4 (assq prop all))))
  808. (setq plist
  809. (plist-put
  810. plist prop
  811. ;; Handle value depending on specified BEHAVIOUR.
  812. (case behaviour
  813. (space (if (plist-get plist prop)
  814. (concat (plist-get plist prop) " " (org-trim val))
  815. (org-trim val)))
  816. (newline (org-trim
  817. (concat
  818. (plist-get plist prop) "\n" (org-trim val))))
  819. (split `(,@(plist-get plist prop) ,@(org-split-string val)))
  820. ('t val)
  821. (otherwise (plist-get plist prop)))))))
  822. ;; Parse keywords specified in `org-element-parsed-keywords'.
  823. (mapc
  824. (lambda (key)
  825. (let* ((prop (cdr (assoc (upcase key) alist)))
  826. (value (and prop (plist-get plist prop))))
  827. (when (stringp value)
  828. (setq plist
  829. (plist-put
  830. plist prop
  831. (org-element-parse-secondary-string
  832. value
  833. (cdr (assq 'keyword org-element-string-restrictions))))))))
  834. org-element-parsed-keywords))
  835. ;; Return final value.
  836. plist))
  837. (defun org-export-get-global-options (backend)
  838. "Return global export options as a plist.
  839. BACKEND is a symbol specifying which back-end should be used."
  840. (let ((all (append org-export-option-alist
  841. (let ((var (intern
  842. (format "org-%s-option-alist" backend))))
  843. (and (boundp var) (eval var)))))
  844. ;; Output value.
  845. plist)
  846. (mapc (lambda (cell)
  847. (setq plist
  848. (plist-put plist (car cell) (eval (nth 3 cell)))))
  849. all)
  850. ;; Return value.
  851. plist))
  852. (defun org-export-initial-options (options)
  853. "Return a plist with non-optional properties.
  854. OPTIONS is the export options plist computed so far."
  855. (list
  856. ;; `:macro-date', `:macro-time' and `:macro-property' could as well
  857. ;; be initialized as tree properties, since they don't depend on
  858. ;; initial environment. Though, it may be more logical to keep
  859. ;; them close to other ":macro-" properties.
  860. :macro-date "(eval (format-time-string \"$1\"))"
  861. :macro-time "(eval (format-time-string \"$1\"))"
  862. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
  863. :macro-modification-time
  864. (and (buffer-file-name)
  865. (file-exists-p (buffer-file-name))
  866. (concat "(eval (format-time-string \"$1\" '"
  867. (prin1-to-string (nth 5 (file-attributes (buffer-file-name))))
  868. "))"))
  869. :macro-input-file (and (buffer-file-name)
  870. (file-name-nondirectory (buffer-file-name)))
  871. ;; Footnotes definitions must be collected in the original buffer,
  872. ;; as there's no insurance that they will still be in the parse
  873. ;; tree, due to some narrowing.
  874. :footnote-definition-alist
  875. (let (alist)
  876. (org-with-wide-buffer
  877. (goto-char (point-min))
  878. (while (re-search-forward org-footnote-definition-re nil t)
  879. (let ((def (org-footnote-at-definition-p)))
  880. (when def
  881. (org-skip-whitespace)
  882. (push (cons (car def)
  883. (save-restriction
  884. (narrow-to-region (point) (nth 2 def))
  885. ;; Like `org-element-parse-buffer', but
  886. ;; makes sure the definition doesn't start
  887. ;; with a section element.
  888. (nconc
  889. (list 'org-data nil)
  890. (org-element-parse-elements
  891. (point-min) (point-max) nil nil nil nil nil))))
  892. alist))))
  893. alist))))
  894. (defvar org-export-allow-BIND-local nil)
  895. (defun org-export-confirm-letbind ()
  896. "Can we use #+BIND values during export?
  897. By default this will ask for confirmation by the user, to divert
  898. possible security risks."
  899. (cond
  900. ((not org-export-allow-BIND) nil)
  901. ((eq org-export-allow-BIND t) t)
  902. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  903. (t (org-set-local 'org-export-allow-BIND-local
  904. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  905. (defun org-export-install-letbind-maybe ()
  906. "Install the values from #+BIND lines as local variables.
  907. Variables must be installed before in-buffer options are
  908. retrieved."
  909. (let (letbind pair)
  910. (org-with-wide-buffer
  911. (goto-char (point-min))
  912. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  913. (when (org-export-confirm-letbind)
  914. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  915. letbind))))
  916. (while (setq pair (pop letbind))
  917. (org-set-local (car pair) (nth 1 pair)))))
  918. ;;;; Tree Properties
  919. ;; They are initialized at the beginning of the transcoding process by
  920. ;; `org-export-collect-tree-properties'.
  921. ;; Dedicated functions focus on computing the value of specific tree
  922. ;; properties during initialization. Thus,
  923. ;; `org-export-use-select-tag-p' determines if an headline makes use
  924. ;; of an export tag enforcing inclusion. `org-export-get-min-level'
  925. ;; gets the minimal exportable level, used as a basis to compute
  926. ;; relative level for headlines. `org-export-get-point-max' returns
  927. ;; the maximum exportable ending position in the parse tree.
  928. ;; Eventually `org-export-collect-headline-numbering' builds an alist
  929. ;; between headlines' beginning position and their numbering.
  930. (defun org-export-collect-tree-properties (data options backend)
  931. "Extract tree properties from parse tree.
  932. DATA is the parse tree from which information is retrieved.
  933. OPTIONS is a list holding export options. BACKEND is the
  934. back-end called for transcoding, as a symbol.
  935. Following tree properties are set:
  936. `:back-end' Back-end used for transcoding.
  937. `:headline-alist' Alist of all headlines' name as key and a list
  938. holding beginning and ending positions as
  939. value.
  940. `:headline-offset' Offset between true level of headlines and
  941. local level. An offset of -1 means an headline
  942. of level 2 should be considered as a level
  943. 1 headline in the context.
  944. `:headline-numbering' Alist of all headlines' beginning position
  945. as key an the associated numbering as value.
  946. `:parse-tree' Whole parse tree.
  947. `:point-max' Last position in the parse tree
  948. `:target-list' List of all targets' raw name in the parse tree.
  949. `:use-select-tags' Non-nil when parsed tree use a special tag to
  950. enforce transcoding of the headline."
  951. ;; First, set `:use-select-tags' property, as it will be required
  952. ;; for further computations.
  953. (setq info
  954. (org-combine-plists
  955. info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
  956. ;; Get the rest of the tree properties, now `:use-select-tags' is
  957. ;; set...
  958. (nconc
  959. `(:parse-tree
  960. ,data
  961. :headline-offset ,(- 1 (org-export-get-min-level data info))
  962. :point-max ,(org-export-get-point-max data info)
  963. :target-list
  964. ,(org-element-map
  965. data 'target
  966. (lambda (target info) (org-element-get-property :raw-value target)))
  967. :headline-alist
  968. ,(org-element-map
  969. data 'headline
  970. (lambda (headline info)
  971. (list (org-element-get-property :raw-value headline)
  972. (org-element-get-property :begin headline)
  973. (org-element-get-property :end headline))))
  974. :headline-numbering ,(org-export-collect-headline-numbering data info)
  975. :back-end ,backend)
  976. info))
  977. (defun org-export-use-select-tags-p (data options)
  978. "Non-nil when data use a tag enforcing transcoding.
  979. DATA is parsed data as returned by `org-element-parse-buffer'.
  980. OPTIONS is a plist holding export options."
  981. (org-element-map
  982. data
  983. 'headline
  984. (lambda (headline info)
  985. (let ((tags (org-element-get-property :with-tags headline)))
  986. (and tags (string-match
  987. (format ":%s:" (plist-get info :select-tags)) tags))))
  988. options
  989. 'stop-at-first-match))
  990. (defun org-export-get-min-level (data options)
  991. "Return minimum exportable headline's level in DATA.
  992. DATA is parsed tree as returned by `org-element-parse-buffer'.
  993. OPTIONS is a plist holding export options."
  994. (catch 'exit
  995. (let ((min-level 10000))
  996. (mapc (lambda (blob)
  997. (when (and (eq (car blob) 'headline)
  998. (not (org-export-skip-p blob options)))
  999. (setq min-level
  1000. (min (org-element-get-property :level blob) min-level)))
  1001. (when (= min-level 1) (throw 'exit 1)))
  1002. (org-element-get-contents data))
  1003. ;; If no headline was found, for the sake of consistency, set
  1004. ;; minimum level to 1 nonetheless.
  1005. (if (= min-level 10000) 1 min-level))))
  1006. (defun org-export-get-point-max (data options)
  1007. "Return last exportable ending position in DATA.
  1008. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1009. OPTIONS is a plist holding export options."
  1010. (let ((pos-max 1))
  1011. (mapc (lambda (blob)
  1012. (unless (and (eq (car blob) 'headline)
  1013. (org-export-skip-p blob options))
  1014. (setq pos-max (org-element-get-property :end blob))))
  1015. (org-element-get-contents data))
  1016. pos-max))
  1017. (defun org-export-collect-headline-numbering (data options)
  1018. "Return numbering of all exportable headlines in a parse tree.
  1019. DATA is the parse tree. OPTIONS is the plist holding export
  1020. options.
  1021. Return an alist whose key is headline's beginning position and
  1022. value is its associated numbering (in the shape of a list of
  1023. numbers)."
  1024. (let ((numbering (make-vector org-export-max-depth 0)))
  1025. (org-element-map
  1026. data
  1027. 'headline
  1028. (lambda (headline info)
  1029. (let ((relative-level
  1030. (1- (org-export-get-relative-level headline info))))
  1031. (cons
  1032. (org-element-get-property :begin headline)
  1033. (loop for n across numbering
  1034. for idx from 0 to org-export-max-depth
  1035. when (< idx relative-level) collect n
  1036. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1037. when (> idx relative-level) do (aset numbering idx 0)))))
  1038. options)))
  1039. ;;; The Transcoder
  1040. ;; This function reads Org data (obtained with, i.e.
  1041. ;; `org-element-parse-buffer') and transcodes it into a specified
  1042. ;; back-end output. It takes care of updating local properties,
  1043. ;; filtering out elements or objects according to export options and
  1044. ;; organizing the output blank lines and white space are preserved.
  1045. ;; Though, this function is inapropriate for secondary strings, which
  1046. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1047. ;; `org-export-secondary-string' is provided for that specific task.
  1048. ;; Internally, three functions handle the filtering of objects and
  1049. ;; elements during the export. More precisely, `org-export-skip-p'
  1050. ;; determines if the considered object or element should be ignored
  1051. ;; altogether, `org-export-interpret-p' tells which elements or
  1052. ;; objects should be seen as real Org syntax and `org-export-expand'
  1053. ;; transforms the others back into their original shape.
  1054. (defun org-export-data (data backend info)
  1055. "Convert DATA to a string into BACKEND format.
  1056. DATA is a nested list as returned by `org-element-parse-buffer'.
  1057. BACKEND is a symbol among supported exporters.
  1058. INFO is a plist holding export options and also used as
  1059. a communication channel between elements when walking the nested
  1060. list. See `org-export-update-info' function for more
  1061. details.
  1062. Return transcoded string."
  1063. (mapconcat
  1064. ;; BLOB can be an element, an object, a string, or nil.
  1065. (lambda (blob)
  1066. (cond
  1067. ((not blob) nil)
  1068. ;; BLOB is a string. Check if the optional transcoder for plain
  1069. ;; text exists, and call it in that case. Otherwise, simply
  1070. ;; return string. Also update INFO and call
  1071. ;; `org-export-filter-plain-text-functions'.
  1072. ((stringp blob)
  1073. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1074. (org-export-filter-apply-functions
  1075. org-export-filter-plain-text-functions
  1076. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1077. backend)))
  1078. ;; BLOB is an element or an object.
  1079. (t
  1080. (let* ((type (if (stringp blob) 'plain-text (car blob)))
  1081. ;; 1. Determine the appropriate TRANSCODER.
  1082. (transcoder
  1083. (cond
  1084. ;; 1.0 A full Org document is inserted.
  1085. ((eq type 'org-data) 'identity)
  1086. ;; 1.1. BLOB should be ignored.
  1087. ((org-export-skip-p blob info) nil)
  1088. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1089. ;; back into Org syntax.
  1090. ((not (org-export-interpret-p blob info))
  1091. 'org-export-expand)
  1092. ;; 1.3. Else apply naming convention.
  1093. (t (let ((trans (intern
  1094. (format "org-%s-%s" backend type))))
  1095. (and (fboundp trans) trans)))))
  1096. ;; 2. Compute CONTENTS of BLOB.
  1097. (contents
  1098. (cond
  1099. ;; Case 0. No transcoder defined: ignore BLOB.
  1100. ((not transcoder) nil)
  1101. ;; Case 1. Transparently export an Org document.
  1102. ((eq type 'org-data) (org-export-data blob backend info))
  1103. ;; Case 2. For a recursive object.
  1104. ((memq type org-element-recursive-objects)
  1105. (org-export-data
  1106. blob backend
  1107. (org-combine-plists
  1108. info
  1109. `(:genealogy ,(cons blob (plist-get info :genealogy))))))
  1110. ;; Case 3. For a recursive element.
  1111. ((memq type org-element-greater-elements)
  1112. ;; Ignore contents of an archived tree
  1113. ;; when `:with-archived-trees' is `headline'.
  1114. (unless (and
  1115. (eq type 'headline)
  1116. (eq (plist-get info :with-archived-trees) 'headline)
  1117. (org-element-get-property :archivedp blob))
  1118. (org-element-normalize-string
  1119. (org-export-data
  1120. blob backend
  1121. (org-combine-plists
  1122. info `(:genealogy
  1123. ,(cons blob (plist-get info :genealogy))))))))
  1124. ;; Case 4. For a paragraph.
  1125. ((eq type 'paragraph)
  1126. (let ((paragraph
  1127. (org-element-normalize-contents
  1128. blob
  1129. ;; When normalizing contents of an item or
  1130. ;; a footnote definition, ignore first line's
  1131. ;; indentation: there is none and it might be
  1132. ;; misleading.
  1133. (and (not (org-export-get-previous-element blob info))
  1134. (let ((parent (caar (plist-get info :genealogy))))
  1135. (memq parent '(footnote-definition item)))))))
  1136. (org-export-data
  1137. paragraph backend
  1138. (org-combine-plists
  1139. info `(:genealogy
  1140. ,(cons paragraph (plist-get info :genealogy)))))))))
  1141. ;; 3. Transcode BLOB into RESULTS string.
  1142. (results (cond
  1143. ((not transcoder) nil)
  1144. ((eq transcoder 'org-export-expand)
  1145. (org-export-data
  1146. `(org-data nil ,(funcall transcoder blob contents))
  1147. backend info))
  1148. (t (funcall transcoder blob contents info)))))
  1149. ;; 4. Discard nil results. Otherwise, update INFO, append
  1150. ;; the same white space between elements or objects as in
  1151. ;; the original buffer, and call appropriate filters.
  1152. (when results
  1153. ;; No filter for a full document.
  1154. (if (eq type 'org-data) results
  1155. (org-export-filter-apply-functions
  1156. (eval (intern (format "org-export-filter-%s-functions" type)))
  1157. (let ((post-blank (org-element-get-property :post-blank blob)))
  1158. (if (memq type org-element-all-elements)
  1159. (concat (org-element-normalize-string results)
  1160. (make-string post-blank ?\n))
  1161. (concat results (make-string post-blank ? ))))
  1162. backend)))))))
  1163. (org-element-get-contents data) ""))
  1164. (defun org-export-secondary-string (secondary backend info)
  1165. "Convert SECONDARY string into BACKEND format.
  1166. SECONDARY is a nested list as returned by
  1167. `org-element-parse-secondary-string'.
  1168. BACKEND is a symbol among supported exporters. INFO is a plist
  1169. used as a communication channel.
  1170. Return transcoded string."
  1171. ;; Make SECONDARY acceptable for `org-export-data'.
  1172. (let ((s (if (listp secondary) secondary (list secondary))))
  1173. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1174. (defun org-export-skip-p (blob info)
  1175. "Non-nil when element or object BLOB should be skipped during export.
  1176. INFO is the plist holding export options."
  1177. ;; Check headline.
  1178. (unless (stringp blob)
  1179. (case (car blob)
  1180. ('headline
  1181. (let ((with-tasks (plist-get info :with-tasks))
  1182. (todo (org-element-get-property :todo-keyword blob))
  1183. (todo-type (org-element-get-property :todo-type blob))
  1184. (archived (plist-get info :with-archived-trees))
  1185. (tag-list (let ((tags (org-element-get-property :tags blob)))
  1186. (and tags (org-split-string tags ":")))))
  1187. (or
  1188. ;; Ignore subtrees with an exclude tag.
  1189. (loop for k in (plist-get info :exclude-tags)
  1190. thereis (member k tag-list))
  1191. ;; Ignore subtrees without a select tag, when such tag is found
  1192. ;; in the buffer.
  1193. (and (plist-get info :use-select-tags)
  1194. (loop for k in (plist-get info :select-tags)
  1195. never (member k tag-list)))
  1196. ;; Ignore commented sub-trees.
  1197. (org-element-get-property :commentedp blob)
  1198. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1199. (and (not archived) (org-element-get-property :archivedp blob))
  1200. ;; Ignore tasks, if specified by `:with-tasks' property.
  1201. (and todo (not with-tasks))
  1202. (and todo
  1203. (memq with-tasks '(todo done))
  1204. (not (eq todo-type with-tasks)))
  1205. (and todo
  1206. (consp with-tasks)
  1207. (not (member todo with-tasks))))))
  1208. ;; Check time-stamp.
  1209. ('time-stamp (not (plist-get info :with-timestamps)))
  1210. ;; Check drawer.
  1211. ('drawer
  1212. (or (not (plist-get info :with-drawers))
  1213. (and (consp (plist-get info :with-drawers))
  1214. (not (member (org-element-get-property :drawer-name blob)
  1215. (plist-get info :with-drawers))))))
  1216. ;; Check export snippet.
  1217. ('export-snippet
  1218. (let* ((raw-back-end (org-element-get-property :back-end blob))
  1219. (true-back-end
  1220. (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
  1221. raw-back-end)))
  1222. (not (string= (symbol-name (plist-get info :back-end))
  1223. true-back-end)))))))
  1224. (defun org-export-interpret-p (blob info)
  1225. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1226. Check is done according to export options INFO, stored as
  1227. a plist."
  1228. (case (car blob)
  1229. ;; ... entities...
  1230. (entity (plist-get info :with-entities))
  1231. ;; ... emphasis...
  1232. (emphasis (plist-get info :with-emphasize))
  1233. ;; ... fixed-width areas.
  1234. (fixed-width (plist-get info :with-fixed-width))
  1235. ;; ... footnotes...
  1236. ((footnote-definition footnote-reference)
  1237. (plist-get info :with-footnotes))
  1238. ;; ... sub/superscripts...
  1239. ((subscript superscript)
  1240. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1241. (if (eq sub/super-p '{})
  1242. (org-element-get-property :use-brackets-p blob)
  1243. sub/super-p)))
  1244. ;; ... tables...
  1245. (table (plist-get info :with-tables))
  1246. (otherwise t)))
  1247. (defsubst org-export-expand (blob contents)
  1248. "Expand a parsed element or object to its original state.
  1249. BLOB is either an element or an object. CONTENTS is its
  1250. contents, as a string or nil."
  1251. (funcall
  1252. (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
  1253. ;;; The Filter System
  1254. ;; Filters allow end-users to tweak easily the transcoded output.
  1255. ;; They are the functional counterpart of hooks, as every filter in
  1256. ;; a set is applied to the return value of the previous one.
  1257. ;; Every set is back-end agnostic. Although, a filter is always
  1258. ;; called, in addition to the string it applies to, with the back-end
  1259. ;; used as argument, so it's easy enough for the end-user to add
  1260. ;; back-end specific filters in the set.
  1261. ;; Filters sets are defined below. There are of four types:
  1262. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1263. ;; complete parsed tree. It's the only filters set that doesn't
  1264. ;; apply to a string.
  1265. ;; - `org-export-filter-final-output-functions' applies to the final
  1266. ;; transcoded string.
  1267. ;; - `org-export-filter-plain-text-functions' applies to any string
  1268. ;; not recognized as Org syntax.
  1269. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1270. ;; after an element or object of type TYPE has been transcoded.
  1271. ;; All filters sets are applied through
  1272. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1273. ;; applied in reverse order, that is in the order of consing. It
  1274. ;; allows developers to be reasonably sure that their filters will be
  1275. ;; applied first.
  1276. ;;;; Special Filters
  1277. (defvar org-export-filter-parse-tree-functions nil
  1278. "Filter, or list of filters, applied to the parsed tree.
  1279. Each filter is called with two arguments: the parse tree, as
  1280. returned by `org-element-parse-buffer', and the back-end as
  1281. a symbol. It must return the modified parse tree to transcode.")
  1282. (defvar org-export-filter-final-output-functions nil
  1283. "Filter, or list of filters, applied to the transcoded string.
  1284. Each filter is called with two arguments: the full transcoded
  1285. string, and the back-end as a symbol. It must return a string
  1286. that will be used as the final export output.")
  1287. (defvar org-export-filter-plain-text-functions nil
  1288. "Filter, or list of filters, applied to plain text.
  1289. Each filter is called with two arguments: a string which contains
  1290. no Org syntax, and the back-end as a symbol. It must return
  1291. a string or nil.")
  1292. ;;;; Elements Filters
  1293. (defvar org-export-filter-center-block-functions nil
  1294. "Filter, or list of filters, applied to a transcoded center block.
  1295. Each filter is called with two arguments: the transcoded center
  1296. block, as a string, and the back-end, as a symbol. It must
  1297. return a string or nil.")
  1298. (defvar org-export-filter-drawer-functions nil
  1299. "Filter, or list of filters, applied to a transcoded drawer.
  1300. Each filter is called with two arguments: the transcoded drawer,
  1301. as a string, and the back-end, as a symbol. It must return
  1302. a string or nil.")
  1303. (defvar org-export-filter-dynamic-block-functions nil
  1304. "Filter, or list of filters, applied to a transcoded dynamic-block.
  1305. Each filter is called with two arguments: the transcoded
  1306. dynamic-block, as a string, and the back-end, as a symbol. It
  1307. must return a string or nil.")
  1308. (defvar org-export-filter-headline-functions nil
  1309. "Filter, or list of filters, applied to a transcoded headline.
  1310. Each filter is called with two arguments: the transcoded
  1311. headline, as a string, and the back-end, as a symbol. It must
  1312. return a string or nil.")
  1313. (defvar org-export-filter-inlinetask-functions nil
  1314. "Filter, or list of filters, applied to a transcoded inlinetask.
  1315. Each filter is called with two arguments: the transcoded
  1316. inlinetask, as a string, and the back-end, as a symbol. It must
  1317. return a string or nil.")
  1318. (defvar org-export-filter-plain-list-functions nil
  1319. "Filter, or list of filters, applied to a transcoded plain-list.
  1320. Each filter is called with two arguments: the transcoded
  1321. plain-list, as a string, and the back-end, as a symbol. It must
  1322. return a string or nil.")
  1323. (defvar org-export-filter-item-functions nil
  1324. "Filter, or list of filters, applied to a transcoded item.
  1325. Each filter is called with two arguments: the transcoded item, as
  1326. a string, and the back-end, as a symbol. It must return a string
  1327. or nil.")
  1328. (defvar org-export-filter-comment-functions nil
  1329. "Filter, or list of filters, applied to a transcoded comment.
  1330. Each filter is called with two arguments: the transcoded comment,
  1331. as a string, and the back-end, as a symbol. It must return
  1332. a string or nil.")
  1333. (defvar org-export-filter-comment-block-functions nil
  1334. "Filter, or list of filters, applied to a transcoded comment-comment.
  1335. Each filter is called with two arguments: the transcoded
  1336. comment-block, as a string, and the back-end, as a symbol. It
  1337. must return a string or nil.")
  1338. (defvar org-export-filter-example-block-functions nil
  1339. "Filter, or list of filters, applied to a transcoded example-block.
  1340. Each filter is called with two arguments: the transcoded
  1341. example-block, as a string, and the back-end, as a symbol. It
  1342. must return a string or nil.")
  1343. (defvar org-export-filter-export-block-functions nil
  1344. "Filter, or list of filters, applied to a transcoded export-block.
  1345. Each filter is called with two arguments: the transcoded
  1346. export-block, as a string, and the back-end, as a symbol. It
  1347. must return a string or nil.")
  1348. (defvar org-export-filter-fixed-width-functions nil
  1349. "Filter, or list of filters, applied to a transcoded fixed-width.
  1350. Each filter is called with two arguments: the transcoded
  1351. fixed-width, as a string, and the back-end, as a symbol. It must
  1352. return a string or nil.")
  1353. (defvar org-export-filter-footnote-definition-functions nil
  1354. "Filter, or list of filters, applied to a transcoded footnote-definition.
  1355. Each filter is called with two arguments: the transcoded
  1356. footnote-definition, as a string, and the back-end, as a symbol.
  1357. It must return a string or nil.")
  1358. (defvar org-export-filter-horizontal-rule-functions nil
  1359. "Filter, or list of filters, applied to a transcoded horizontal-rule.
  1360. Each filter is called with two arguments: the transcoded
  1361. horizontal-rule, as a string, and the back-end, as a symbol. It
  1362. must return a string or nil.")
  1363. (defvar org-export-filter-keyword-functions nil
  1364. "Filter, or list of filters, applied to a transcoded keyword.
  1365. Each filter is called with two arguments: the transcoded keyword,
  1366. as a string, and the back-end, as a symbol. It must return
  1367. a string or nil.")
  1368. (defvar org-export-filter-latex-environment-functions nil
  1369. "Filter, or list of filters, applied to a transcoded latex-environment.
  1370. Each filter is called with two arguments: the transcoded
  1371. latex-environment, as a string, and the back-end, as a symbol.
  1372. It must return a string or nil.")
  1373. (defvar org-export-filter-babel-call-functions nil
  1374. "Filter, or list of filters, applied to a transcoded babel-call.
  1375. Each filter is called with two arguments: the transcoded
  1376. babel-call, as a string, and the back-end, as a symbol. It must
  1377. return a string or nil.")
  1378. (defvar org-export-filter-paragraph-functions nil
  1379. "Filter, or list of filters, applied to a transcoded paragraph.
  1380. Each filter is called with two arguments: the transcoded
  1381. paragraph, as a string, and the back-end, as a symbol. It must
  1382. return a string or nil.")
  1383. (defvar org-export-filter-property-drawer-functions nil
  1384. "Filter, or list of filters, applied to a transcoded property-drawer.
  1385. Each filter is called with two arguments: the transcoded
  1386. property-drawer, as a string, and the back-end, as a symbol. It
  1387. must return a string or nil.")
  1388. (defvar org-export-filter-quote-block-functions nil
  1389. "Filter, or list of filters, applied to a transcoded quote block.
  1390. Each filter is called with two arguments: the transcoded quote
  1391. block, as a string, and the back-end, as a symbol. It must
  1392. return a string or nil.")
  1393. (defvar org-export-filter-quote-section-functions nil
  1394. "Filter, or list of filters, applied to a transcoded quote-section.
  1395. Each filter is called with two arguments: the transcoded
  1396. quote-section, as a string, and the back-end, as a symbol. It
  1397. must return a string or nil.")
  1398. (defvar org-export-filter-section-functions nil
  1399. "Filter, or list of filters, applied to a transcoded section.
  1400. Each filter is called with two arguments: the transcoded section,
  1401. as a string, and the back-end, as a symbol. It must return
  1402. a string or nil.")
  1403. (defvar org-export-filter-special-block-functions nil
  1404. "Filter, or list of filters, applied to a transcoded special block.
  1405. Each filter is called with two arguments: the transcoded special
  1406. block, as a string, and the back-end, as a symbol. It must
  1407. return a string or nil.")
  1408. (defvar org-export-filter-src-block-functions nil
  1409. "Filter, or list of filters, applied to a transcoded src-block.
  1410. Each filter is called with two arguments: the transcoded
  1411. src-block, as a string, and the back-end, as a symbol. It must
  1412. return a string or nil.")
  1413. (defvar org-export-filter-table-functions nil
  1414. "Filter, or list of filters, applied to a transcoded table.
  1415. Each filter is called with two arguments: the transcoded table,
  1416. as a string, and the back-end, as a symbol. It must return
  1417. a string or nil.")
  1418. (defvar org-export-filter-verse-block-functions nil
  1419. "Filter, or list of filters, applied to a transcoded verse block.
  1420. Each filter is called with two arguments: the transcoded verse
  1421. block, as a string, and the back-end, as a symbol. It must
  1422. return a string or nil.")
  1423. ;;;; Objects Filters
  1424. (defvar org-export-filter-emphasis-functions nil
  1425. "Filter, or list of filters, applied to a transcoded emphasis.
  1426. Each filter is called with two arguments: the transcoded
  1427. emphasis, as a string, and the back-end, as a symbol. It must
  1428. return a string or nil.")
  1429. (defvar org-export-filter-entity-functions nil
  1430. "Filter, or list of filters, applied to a transcoded entity.
  1431. Each filter is called with two arguments: the transcoded entity,
  1432. as a string, and the back-end, as a symbol. It must return
  1433. a string or nil.")
  1434. (defvar org-export-filter-export-snippet-functions nil
  1435. "Filter, or list of filters, applied to a transcoded export-snippet.
  1436. Each filter is called with two arguments: the transcoded
  1437. export-snippet, as a string, and the back-end, as a symbol. It
  1438. must return a string or nil.")
  1439. (defvar org-export-filter-footnote-reference-functions nil
  1440. "Filter, or list of filters, applied to a transcoded footnote-reference.
  1441. Each filter is called with two arguments: the transcoded
  1442. footnote-reference, as a string, and the back-end, as a symbol.
  1443. It must return a string or nil.")
  1444. (defvar org-export-filter-inline-babel-call-functions nil
  1445. "Filter, or list of filters, applied to a transcoded inline-babel-call.
  1446. Each filter is called with two arguments: the transcoded
  1447. inline-babel-call, as a string, and the back-end, as a symbol. It
  1448. must return a string or nil.")
  1449. (defvar org-export-filter-inline-src-block-functions nil
  1450. "Filter, or list of filters, applied to a transcoded inline-src-block.
  1451. Each filter is called with two arguments: the transcoded
  1452. inline-src-block, as a string, and the back-end, as a symbol. It
  1453. must return a string or nil.")
  1454. (defvar org-export-filter-latex-fragment-functions nil
  1455. "Filter, or list of filters, applied to a transcoded latex-fragment.
  1456. Each filter is called with two arguments: the transcoded
  1457. latex-fragment, as a string, and the back-end, as a symbol. It
  1458. must return a string or nil.")
  1459. (defvar org-export-filter-line-break-functions nil
  1460. "Filter, or list of filters, applied to a transcoded line-break.
  1461. Each filter is called with two arguments: the transcoded
  1462. line-break, as a string, and the back-end, as a symbol. It must
  1463. return a string or nil.")
  1464. (defvar org-export-filter-link-functions nil
  1465. "Filter, or list of filters, applied to a transcoded link.
  1466. Each filter is called with two arguments: the transcoded link, as
  1467. a string, and the back-end, as a symbol. It must return a string
  1468. or nil.")
  1469. (defvar org-export-filter-macro-functions nil
  1470. "Filter, or list of filters, applied to a transcoded macro.
  1471. Each filter is called with two arguments: the transcoded macro,
  1472. as a string, and the back-end, as a symbol. It must return
  1473. a string or nil.")
  1474. (defvar org-export-filter-radio-target-functions nil
  1475. "Filter, or list of filters, applied to a transcoded radio-target.
  1476. Each filter is called with two arguments: the transcoded
  1477. radio-target, as a string, and the back-end, as a symbol. It
  1478. must return a string or nil.")
  1479. (defvar org-export-filter-statistics-cookie-functions nil
  1480. "Filter, or list of filters, applied to a transcoded statistics-cookie.
  1481. Each filter is called with two arguments: the transcoded
  1482. statistics-cookie, as a string, and the back-end, as a symbol.
  1483. It must return a string or nil.")
  1484. (defvar org-export-filter-subscript-functions nil
  1485. "Filter, or list of filters, applied to a transcoded subscript.
  1486. Each filter is called with two arguments: the transcoded
  1487. subscript, as a string, and the back-end, as a symbol. It must
  1488. return a string or nil.")
  1489. (defvar org-export-filter-superscript-functions nil
  1490. "Filter, or list of filters, applied to a transcoded superscript.
  1491. Each filter is called with two arguments: the transcoded
  1492. superscript, as a string, and the back-end, as a symbol. It must
  1493. return a string or nil.")
  1494. (defvar org-export-filter-target-functions nil
  1495. "Filter, or list of filters, applied to a transcoded target.
  1496. Each filter is called with two arguments: the transcoded target,
  1497. as a string, and the back-end, as a symbol. It must return
  1498. a string or nil.")
  1499. (defvar org-export-filter-time-stamp-functions nil
  1500. "Filter, or list of filters, applied to a transcoded time-stamp.
  1501. Each filter is called with two arguments: the transcoded
  1502. time-stamp, as a string, and the back-end, as a symbol. It must
  1503. return a string or nil.")
  1504. (defvar org-export-filter-verbatim-functions nil
  1505. "Filter, or list of filters, applied to a transcoded verbatim.
  1506. Each filter is called with two arguments: the transcoded
  1507. verbatim, as a string, and the back-end, as a symbol. It must
  1508. return a string or nil.")
  1509. (defun org-export-filter-apply-functions (filters value backend)
  1510. "Call every function in FILTERS with arguments VALUE and BACKEND.
  1511. Functions are called in reverse order, to be reasonably sure that
  1512. developer-specified filters, if any, are called first."
  1513. ;; Ensure FILTERS is a list.
  1514. (let ((filters (if (listp filters) (reverse filters) (list filters))))
  1515. (loop for filter in filters
  1516. if (not value) return nil else
  1517. do (setq value (funcall filter value backend))))
  1518. value)
  1519. ;;; Core functions
  1520. ;; This is the room for the main function, `org-export-as', along with
  1521. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1522. ;; They differ only by the way they output the resulting code.
  1523. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1524. ;; but a copy of it (with the same buffer-local variables and
  1525. ;; visibility), where Babel blocks are executed, if appropriate.
  1526. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1527. (defun org-export-as (backend
  1528. &optional subtreep visible-only body-only ext-plist)
  1529. "Transcode current Org buffer into BACKEND code.
  1530. If narrowing is active in the current buffer, only transcode its
  1531. narrowed part.
  1532. If a region is active, transcode that region.
  1533. When optional argument SUBTREEP is non-nil, transcode the
  1534. sub-tree at point, extracting information from the headline
  1535. properties first.
  1536. When optional argument VISIBLE-ONLY is non-nil, don't export
  1537. contents of hidden elements.
  1538. When optional argument BODY-ONLY is non-nil, only return body
  1539. code, without preamble nor postamble.
  1540. EXT-PLIST, when provided, is a property list with external
  1541. parameters overriding Org default settings, but still inferior to
  1542. file-local settings.
  1543. Return code as a string."
  1544. (save-excursion
  1545. (save-restriction
  1546. ;; Narrow buffer to an appropriate region for parsing.
  1547. (when (org-region-active-p)
  1548. (narrow-to-region (region-beginning) (region-end))
  1549. (goto-char (point-min)))
  1550. (when (and subtreep (not (org-at-heading-p)))
  1551. ;; Ensure point is at sub-tree's beginning.
  1552. (org-with-limited-levels (org-back-to-heading (not visible-only))))
  1553. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA).
  1554. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1555. ;; created, where all code blocks are evaluated. RAW-DATA is
  1556. ;; the parsed tree of the buffer resulting from that process.
  1557. ;; Eventually call `org-export-filter-parse-tree-functions'.
  1558. (let ((info (org-export-collect-options backend subtreep ext-plist))
  1559. (raw-data (progn
  1560. (when subtreep ; Only parse subtree contents.
  1561. (let ((end (save-excursion (org-end-of-subtree t))))
  1562. (narrow-to-region
  1563. (progn (forward-line) (point)) end)))
  1564. (org-export-filter-apply-functions
  1565. org-export-filter-parse-tree-functions
  1566. (org-export-with-current-buffer-copy
  1567. (org-export-blocks-preprocess)
  1568. (org-element-parse-buffer nil visible-only))
  1569. backend))))
  1570. ;; Initialize the communication system and combine it to INFO.
  1571. (setq info
  1572. (org-combine-plists
  1573. info (org-export-collect-tree-properties raw-data info backend)))
  1574. ;; Now transcode RAW-DATA. Also call
  1575. ;; `org-export-filter-final-output-functions'.
  1576. (let* ((body (org-element-normalize-string
  1577. (org-export-data raw-data backend info)))
  1578. (template (intern (format "org-%s-template" backend)))
  1579. (output (org-export-filter-apply-functions
  1580. org-export-filter-final-output-functions
  1581. (if (or (not (fboundp template)) body-only) body
  1582. (funcall template body info))
  1583. backend)))
  1584. ;; Maybe add final OUTPUT to kill ring before returning it.
  1585. (when org-export-copy-to-kill-ring (org-kill-new output))
  1586. output)))))
  1587. (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
  1588. body-only ext-plist)
  1589. "Call `org-export-as' with output to a specified buffer.
  1590. BACKEND is the back-end used for transcoding, as a symbol.
  1591. BUFFER is the output buffer. If it already exists, it will be
  1592. erased first, otherwise, it will be created.
  1593. Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
  1594. similar to those used in `org-export-as', which see.
  1595. Return buffer."
  1596. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
  1597. (buffer (get-buffer-create buffer)))
  1598. (with-current-buffer buffer
  1599. (erase-buffer)
  1600. (insert out)
  1601. (goto-char (point-min)))
  1602. buffer))
  1603. (defun org-export-to-file (backend &optional post-process subtreep visible-only
  1604. body-only ext-plist pub-dir)
  1605. "Call `org-export-as' with output to a specified file.
  1606. BACKEND is the back-end used for transcoding, as a symbol.
  1607. Optional argument POST-PROCESS, when non-nil, is a function
  1608. applied to the output file. It expects one argument: the file
  1609. name, as a string. It can be used to call shell commands on that
  1610. file, display a specific buffer, etc.
  1611. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  1612. EXT-PLIST are similar to those used in `org-export-as', which
  1613. see.
  1614. When optional argument PUB-DIR is set, use it as the publishing
  1615. directory.
  1616. Return output file's name."
  1617. ;; First get output directory and output file name.
  1618. (let ((out-file
  1619. (concat (file-name-as-directory (or pub-dir "."))
  1620. ;; Output file name either comes from
  1621. ;; EXPORT_FILE_NAME sub-tree property, assuming input
  1622. ;; is narrowed to said sub-tree, or to the name of
  1623. ;; buffer's associated file.
  1624. (file-name-sans-extension
  1625. (or (and subtreep
  1626. (org-entry-get
  1627. (save-excursion
  1628. (ignore-errors
  1629. (org-back-to-heading (not visible-only))
  1630. (point)))
  1631. "EXPORT_FILE_NAME" t))
  1632. (file-name-nondirectory
  1633. (or (buffer-file-name (buffer-base-buffer))
  1634. (error "Output file's name undefined")))))
  1635. ".tex")))
  1636. ;; Checks for file and directory permissions.
  1637. (cond
  1638. ((not (file-exists-p out-file))
  1639. (unless (file-writable-p (or pub-dir "."))
  1640. (error "Output directory not writable")))
  1641. ((not (file-writable-p out-file)) (error "Output file not writable")))
  1642. ;; All checks passed: insert contents to a temporary buffer and
  1643. ;; write it to the specified file.
  1644. (let ((out (org-export-as
  1645. backend subtreep visible-only body-only ext-plist)))
  1646. (with-temp-buffer
  1647. (insert out)
  1648. (let ((coding-system-for-write org-export-coding-system))
  1649. (write-file out-file))))
  1650. (when post-process (funcall post-process out-file))
  1651. ;; Return full path.
  1652. out-file))
  1653. (defmacro org-export-with-current-buffer-copy (&rest body)
  1654. "Apply BODY in a copy of the current buffer.
  1655. The copy preserves local variables and visibility of the original
  1656. buffer.
  1657. Point is at buffer's beginning when BODY is applied."
  1658. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1659. `(let ((,original-buffer ,(current-buffer))
  1660. (,offset ,(1- (point-min)))
  1661. (,buffer-string ,(buffer-string))
  1662. (,overlays (mapcar
  1663. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1664. (with-temp-buffer
  1665. (let ((buffer-invisibility-spec nil))
  1666. (org-clone-local-variables
  1667. ,original-buffer "^\\(org-\\|orgtbl-\\|major-mode$\\)")
  1668. (insert ,buffer-string)
  1669. (mapc (lambda (ov)
  1670. (move-overlay
  1671. ov
  1672. (- (overlay-start ov) ,offset)
  1673. (- (overlay-end ov) ,offset)
  1674. (current-buffer)))
  1675. ,overlays)
  1676. (goto-char (point-min))
  1677. (progn ,@body))))))
  1678. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1679. ;;; Tools For Back-Ends
  1680. ;; A whole set of tools is available to help build new exporters. Any
  1681. ;; function general enough to have its use across many back-ends
  1682. ;; should be added here.
  1683. ;; As of now, functions operating on footnotes, headlines, include
  1684. ;; keywords, links, macros, references, src-blocks, tables and tables
  1685. ;; of contents are implemented.
  1686. ;;;; For Footnotes
  1687. ;; `org-export-collect-footnote-definitions' is a tool to list
  1688. ;; actually used footnotes definitions in the whole parse tree, or in
  1689. ;; an headline, in order to add footnote listings throughout the
  1690. ;; transcoded data.
  1691. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  1692. ;; back-ends, when they need to attach the footnote definition only to
  1693. ;; the first occurrence of the corresponding label.
  1694. ;; `org-export-get-footnote-definition' and
  1695. ;; `org-export-get-footnote-number' provide easier access to
  1696. ;; additional information relative to a footnote reference.
  1697. (defun org-export-collect-footnote-definitions (data info)
  1698. "Return an alist between footnote numbers, labels and definitions.
  1699. DATA is the parse tree from which definitions are collected.
  1700. INFO is the plist used as a communication channel.
  1701. Definitions are sorted by order of references. They either
  1702. appear as Org data \(transcoded with `org-export-data'\) or as
  1703. a secondary string for inlined footnotes \(transcoded with
  1704. `org-export-secondary-string'\). Unreferenced definitions are
  1705. ignored."
  1706. (let (refs)
  1707. ;; Collect seen references in REFS.
  1708. (org-element-map
  1709. data 'footnote-reference
  1710. (lambda (footnote local)
  1711. (when (org-export-footnote-first-reference-p footnote local)
  1712. (list (org-export-get-footnote-number footnote local)
  1713. (org-element-get-property :label footnote)
  1714. (org-export-get-footnote-definition footnote local))))
  1715. info)))
  1716. (defun org-export-footnote-first-reference-p (footnote-reference info)
  1717. "Non-nil when a footnote reference is the first one for its label.
  1718. FOOTNOTE-REFERENCE is the footnote reference being considered.
  1719. INFO is the plist used as a communication channel."
  1720. (let ((label (org-element-get-property :label footnote-reference)))
  1721. (or (not label)
  1722. (equal
  1723. footnote-reference
  1724. (org-element-map
  1725. (plist-get info :parse-tree) 'footnote-reference
  1726. (lambda (footnote local)
  1727. (when (string= (org-element-get-property :label footnote) label)
  1728. footnote))
  1729. info 'first-match)))))
  1730. (defun org-export-get-footnote-definition (footnote-reference info)
  1731. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  1732. INFO is the plist used as a communication channel."
  1733. (let ((label (org-element-get-property :label footnote-reference)))
  1734. (or (org-element-get-property :inline-definition footnote-reference)
  1735. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  1736. (defun org-export-get-footnote-number (footnote info)
  1737. "Return number associated to a footnote.
  1738. FOOTNOTE is either a footnote reference or a footnote definition.
  1739. INFO is the plist used as a communication channel."
  1740. (let ((label (org-element-get-property :label footnote)) seen-refs)
  1741. (org-element-map
  1742. (plist-get info :parse-tree) 'footnote-reference
  1743. (lambda (fn local)
  1744. (let ((fn-lbl (org-element-get-property :label fn)))
  1745. (cond
  1746. ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
  1747. ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
  1748. ;; Anonymous footnote: it's always a new one. Also, be sure
  1749. ;; to return nil from the `cond' so `first-match' doesn't
  1750. ;; get us out of the loop.
  1751. ((not fn-lbl) (push 'inline seen-refs) nil)
  1752. ;; Label not seen so far: add it so SEEN-REFS. Again,
  1753. ;; return nil to stay in the loop.
  1754. ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
  1755. info 'first-match)))
  1756. ;;;; For Headlines
  1757. ;; `org-export-get-relative-level' is a shortcut to get headline
  1758. ;; level, relatively to the lower headline level in the parsed tree.
  1759. ;; `org-export-get-headline-number' returns the section number of an
  1760. ;; headline, while `org-export-number-to-roman' allows to convert it
  1761. ;; to roman numbers.
  1762. ;; `org-export-first-sibling-p' and `org-export-last-sibling-p' are
  1763. ;; two useful predicates when it comes to fulfill the
  1764. ;; `:headline-levels' property.
  1765. (defun org-export-get-relative-level (headline info)
  1766. "Return HEADLINE relative level within current parsed tree.
  1767. INFO is a plist holding contextual information."
  1768. (+ (org-element-get-property :level headline)
  1769. (or (plist-get info :headline-offset) 0)))
  1770. (defun org-export-get-headline-number (headline info)
  1771. "Return HEADLINE numbering as a list of numbers.
  1772. INFO is a plist holding contextual information."
  1773. (cdr (assq (org-element-get-property :begin headline)
  1774. (plist-get info :headline-numbering))))
  1775. (defun org-export-number-to-roman (n)
  1776. "Convert integer N into a roman numeral."
  1777. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  1778. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  1779. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  1780. ( 1 . "I")))
  1781. (res ""))
  1782. (if (<= n 0)
  1783. (number-to-string n)
  1784. (while roman
  1785. (if (>= n (caar roman))
  1786. (setq n (- n (caar roman))
  1787. res (concat res (cdar roman)))
  1788. (pop roman)))
  1789. res)))
  1790. (defun org-export-first-sibling-p (headline info)
  1791. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  1792. INFO is the plist used as a communication channel."
  1793. (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
  1794. (defun org-export-last-sibling-p (headline info)
  1795. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  1796. INFO is the plist used as a communication channel."
  1797. (= (org-element-get-property :end headline)
  1798. (or (org-element-get-property
  1799. :end (org-export-get-parent-headline headline info))
  1800. (plist-get info :point-max))))
  1801. ;;;; For Include Keywords
  1802. ;; This section provides a tool to properly handle insertion of files
  1803. ;; during export: `org-export-included-files'. It recursively
  1804. ;; transcodes a file specfied by an include keyword.
  1805. ;; It uses two helper functions: `org-export-get-file-contents'
  1806. ;; returns contents of a file according to parameters specified in the
  1807. ;; keyword while `org-export-parse-included-file' parses the file
  1808. ;; specified by it.
  1809. (defun org-export-included-file (keyword backend info)
  1810. "Transcode file specified with include KEYWORD.
  1811. KEYWORD is the include keyword element transcoded. BACKEND is
  1812. the language back-end used for transcoding. INFO is the plist
  1813. used as a communication channel.
  1814. This function updates `:included-files' and `:headline-offset'
  1815. properties.
  1816. Return the transcoded string."
  1817. (let ((data (org-export-parse-included-file keyword info))
  1818. (file (let ((value (org-element-get-property :value keyword)))
  1819. (and (string-match "^\"\\(\\S-+\\)\"" value)
  1820. (match-string 1 value)))))
  1821. (org-element-normalize-string
  1822. (org-export-data
  1823. data backend
  1824. (org-combine-plists
  1825. info
  1826. ;; Store full path of already included files to avoid
  1827. ;; recursive file inclusion.
  1828. `(:included-files
  1829. ,(cons (expand-file-name file) (plist-get info :included-files))
  1830. ;; Ensure that a top-level headline in the included
  1831. ;; file becomes a direct child of the current headline
  1832. ;; in the buffer.
  1833. :headline-offset
  1834. ,(- (+ (org-element-get-property
  1835. :level (org-export-get-parent-headline keyword info))
  1836. (plist-get info :headline-offset))
  1837. (1- (org-export-get-min-level data info)))))))))
  1838. (defun org-export-get-file-contents (file &optional lines)
  1839. "Get the contents of FILE and return them as a string.
  1840. When optional argument LINES is a string specifying a range of
  1841. lines, include only those lines."
  1842. (with-temp-buffer
  1843. (insert-file-contents file)
  1844. (when lines
  1845. (let* ((lines (split-string lines "-"))
  1846. (lbeg (string-to-number (car lines)))
  1847. (lend (string-to-number (cadr lines)))
  1848. (beg (if (zerop lbeg) (point-min)
  1849. (goto-char (point-min))
  1850. (forward-line (1- lbeg))
  1851. (point)))
  1852. (end (if (zerop lend) (point-max)
  1853. (goto-char (point-min))
  1854. (forward-line (1- lend))
  1855. (point))))
  1856. (narrow-to-region beg end)))
  1857. (buffer-string)))
  1858. (defun org-export-parse-included-file (keyword info)
  1859. "Parse file specified by include KEYWORD.
  1860. KEYWORD is the include keyword element transcoded. BACKEND is the
  1861. language back-end used for transcoding. INFO is the plist used as
  1862. a communication channel.
  1863. Return the parsed tree."
  1864. (let* ((value (org-element-get-property :value keyword))
  1865. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1866. (prog1 (match-string 1 value)
  1867. (setq value (replace-match "" nil nil value)))))
  1868. (lines (and (string-match
  1869. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1870. (prog1 (match-string 1 value)
  1871. (setq value (replace-match "" nil nil value)))))
  1872. (env (cond ((string-match "\\<example\\>" value) "example")
  1873. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1874. (match-string 1 value)))))
  1875. (cond
  1876. ((or (not file)
  1877. (not (file-exists-p file))
  1878. (not (file-readable-p file)))
  1879. (format "Cannot include file %s" file))
  1880. ((and (not env)
  1881. (member (expand-file-name file) (plist-get info :included-files)))
  1882. (error "Recursive file inclusion: %S" file))
  1883. (t (let ((raw (org-element-normalize-string
  1884. (org-export-get-file-contents
  1885. (expand-file-name file) lines))))
  1886. ;; If environment isn't specified, Insert file in
  1887. ;; a temporary buffer and parse it as Org syntax.
  1888. ;; Otherwise, build the element representing the file.
  1889. (cond
  1890. ((not env)
  1891. (with-temp-buffer
  1892. (insert raw) (org-mode) (org-element-parse-buffer)))
  1893. ((string= "example" env)
  1894. `(org-data nil (example-block (:value ,raw :post-blank 0))))
  1895. (t
  1896. `(org-data
  1897. nil
  1898. (src-block (:value ,raw :language ,env :post-blank 0))))))))))
  1899. ;;;; For Links
  1900. ;; `org-export-solidify-link-text' turns a string into a safer version
  1901. ;; for links, replacing most non-standard characters with hyphens.
  1902. ;; `org-export-get-coderef-format' returns an appropriate format
  1903. ;; string for coderefs.
  1904. ;; `org-export-inline-image-p' returns a non-nil value when the link
  1905. ;; provided should be considered as an inline image.
  1906. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  1907. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  1908. ;; returns an appropriate unique identifier when found, or nil.
  1909. ;; `org-export-resolve-coderef' associates a reference to a line
  1910. ;; number in the element it belongs, or returns the reference itself
  1911. ;; when the element isn't numbered.
  1912. (defun org-export-solidify-link-text (s)
  1913. "Take link text and make a safe target out of it."
  1914. (save-match-data
  1915. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
  1916. (defun org-export-get-coderef-format (path desc)
  1917. "Return format string for code reference link.
  1918. PATH is the link path. DESC is its description."
  1919. (save-match-data
  1920. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  1921. (replace-match "%s" t t desc))
  1922. ((string= desc "") "%s")
  1923. (t desc))))
  1924. (defun org-export-inline-image-p (link &optional extensions)
  1925. "Non-nil if LINK object points to an inline image.
  1926. When non-nil, optional argument EXTENSIONS is a list of valid
  1927. extensions for image files, as strings. Otherwise, a default
  1928. list is provided \(cf. `org-image-file-name-regexp'\)."
  1929. (and (not (org-element-get-contents link))
  1930. (string= (org-element-get-property :type link) "file")
  1931. (org-file-image-p
  1932. (expand-file-name (org-element-get-property :path link))
  1933. extensions)))
  1934. (defun org-export-resolve-fuzzy-link (link info)
  1935. "Return an unique identifier for LINK destination.
  1936. INFO is a plist holding contextual information.
  1937. Return value can be a string, an buffer position, or nil:
  1938. - If LINK path exactly matches any target, return its name as the
  1939. identifier.
  1940. - If LINK path exactly matches any headline name, return
  1941. headline's beginning position as the identifier. If more than
  1942. one headline share that name, priority will be given to the one
  1943. with the closest common ancestor, if any, or the first one in
  1944. the parse tree otherwise.
  1945. - Otherwise, return nil.
  1946. Assume LINK type is \"fuzzy\"."
  1947. (let ((path (org-element-get-property :path link)))
  1948. (if (member path (plist-get info :target-list))
  1949. ;; Link points to a target: return its name as a string.
  1950. path
  1951. ;; Link either points to an headline or nothing. Try to find
  1952. ;; the source, with priority given to headlines with the closest
  1953. ;; common ancestor. If such candidate is found, return its
  1954. ;; beginning position as an unique identifier, otherwise return
  1955. ;; nil.
  1956. (let* ((head-alist (plist-get info :headline-alist))
  1957. (link-begin (org-element-get-property :begin link))
  1958. (link-end (org-element-get-property :end link))
  1959. ;; Store candidates as a list of cons cells holding their
  1960. ;; beginning and ending position.
  1961. (cands (loop for head in head-alist
  1962. when (string= (car head) path)
  1963. collect (cons (nth 1 head) (nth 2 head)))))
  1964. (cond
  1965. ;; No candidate: return nil.
  1966. ((not cands) nil)
  1967. ;; If one or more candidates share common ancestors with
  1968. ;; LINK, return beginning position of the first one matching
  1969. ;; the closer ancestor shared.
  1970. ((let ((ancestors (loop for head in head-alist
  1971. when (and (> link-begin (nth 1 head))
  1972. (<= link-end (nth 2 head)))
  1973. collect (cons (nth 1 head) (nth 2 head)))))
  1974. (loop named main for ancestor in (nreverse ancestors) do
  1975. (loop for candidate in cands
  1976. when (and (>= (car candidate) (car ancestor))
  1977. (<= (cdr candidate) (cdr ancestor)))
  1978. do (return-from main (car candidate))))))
  1979. ;; No candidate have a common ancestor with link: First match
  1980. ;; will do. Return its beginning position.
  1981. (t (caar cands)))))))
  1982. (defun org-export-resolve-coderef (ref info)
  1983. "Resolve a code reference REF.
  1984. INFO is a plist used as a communication channel.
  1985. Return associated line number in source code, or REF itself,
  1986. depending on src-block or example element's switches."
  1987. (org-element-map
  1988. (plist-get info :parse-tree) '(src-block example)
  1989. (lambda (el local)
  1990. (let ((switches (or (org-element-get-property :switches el) "")))
  1991. (with-temp-buffer
  1992. (insert (org-trim (org-element-get-property :value el)))
  1993. ;; Build reference regexp.
  1994. (let* ((label
  1995. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1996. (match-string 1 switches))
  1997. org-coderef-label-format))
  1998. (ref-re
  1999. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2000. (replace-regexp-in-string "%s" ref label nil t))))
  2001. ;; Element containing REF is found. Only associate REF to
  2002. ;; a line number if element has "+n" or "-n" and "-k" or
  2003. ;; "-r" as switches. When it has "+n", count accumulated
  2004. ;; locs before, too.
  2005. (when (re-search-backward ref-re nil t)
  2006. (cond
  2007. ((not (string-match "-[kr]\\>" switches)) ref)
  2008. ((string-match "-n\\>" switches) (line-number-at-pos))
  2009. ((string-match "\\+n\\>" switches)
  2010. (+ (org-export-get-loc el local) (line-number-at-pos)))
  2011. (t ref)))))))
  2012. info 'first-match))
  2013. ;;;; For Macros
  2014. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2015. (defun org-export-expand-macro (macro info)
  2016. "Expand MACRO and return it as a string.
  2017. INFO is a plist holding export options."
  2018. (let* ((key (org-element-get-property :key macro))
  2019. (args (org-element-get-property :args macro))
  2020. (value (plist-get info (intern (format ":macro-%s" key)))))
  2021. ;; Replace arguments in VALUE.
  2022. (let ((s 0) n)
  2023. (while (string-match "\\$\\([0-9]+\\)" value s)
  2024. (setq s (1+ (match-beginning 0))
  2025. n (string-to-number (match-string 1 value)))
  2026. (and (>= (length args) n)
  2027. (setq value (replace-match (nth (1- n) args) t t value)))))
  2028. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2029. (when (string-match "\\`(eval\\>" value)
  2030. (setq value (eval (read value))))
  2031. ;; Return expanded string.
  2032. (format "%s" value)))
  2033. ;;;; For References
  2034. ;; `org-export-get-ordinal' associates a sequence number to any object
  2035. ;; or element.
  2036. (defun org-export-get-ordinal (element info &optional within-section predicate)
  2037. "Return ordinal number of an element or object.
  2038. ELEMENT is the element or object considered. INFO is the plist
  2039. used as a communication channel.
  2040. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2041. to the section containing ELEMENT.
  2042. Optional argument PREDICATE is a function returning a non-nil
  2043. value if the current element or object should be counted in. It
  2044. accepts one argument: the element or object being considered.
  2045. This argument allows to count only a certain type of objects,
  2046. like inline images, which are a subset of links \(in that case,
  2047. `org-export-inline-image-p' might be an useful predicate\)."
  2048. (let ((counter 0)
  2049. (type (car element))
  2050. ;; Determine if search should apply to current section, in
  2051. ;; which case it should be retrieved first, or to full parse
  2052. ;; tree. As a special case, an element or object without
  2053. ;; a parent headline will also trigger a full search,
  2054. ;; notwithstanding WITHIN-SECTION value.
  2055. (data
  2056. (if (not within-section) (plist-get info :parse-tree)
  2057. (or (org-export-get-parent-headline element info)
  2058. (plist-get info :parse-tree)))))
  2059. ;; Increment counter until ELEMENT is found again.
  2060. (org-element-map
  2061. data type
  2062. (lambda (el local)
  2063. (cond
  2064. ((and (functionp predicate) (funcall predicate el)))
  2065. ((equal element el) (1+ counter))
  2066. (t (incf counter) nil)))
  2067. info 'first-match)))
  2068. ;;;; For Src-Blocks
  2069. ;; `org-export-get-loc' counts number of code lines accumulated in
  2070. ;; src-block or example-block elements with a "+n" switch until
  2071. ;; a given element, excluded. Note: "-n" switches reset that count.
  2072. ;; `org-export-handle-code' takes care of line numbering and reference
  2073. ;; cleaning in source code, when appropriate.
  2074. (defun org-export-get-loc (element info)
  2075. "Return accumulated lines of code up to ELEMENT.
  2076. INFO is the plist used as a communication channel.
  2077. ELEMENT is excluded from count."
  2078. (let ((loc 0))
  2079. (org-element-map
  2080. (plist-get info :parse-tree) `(src-block example-block ,(car element))
  2081. (lambda (el local)
  2082. (cond
  2083. ;; ELEMENT is reached: Quit the loop.
  2084. ((equal el element) t)
  2085. ;; Only count lines from src-block and example-block elements
  2086. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2087. ((not (memq (car el) '(src-block example-block))) nil)
  2088. ((let ((switches (org-element-get-property :switches el)))
  2089. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2090. ;; Accumulate locs or reset them.
  2091. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2092. (lines (org-count-lines
  2093. (org-trim (org-element-get-property :value el)))))
  2094. (setq loc (if accumulatep lines (+ loc lines))))))
  2095. ;; Return nil to stay in the loop.
  2096. nil)))
  2097. info 'first-match)
  2098. ;; Return value.
  2099. loc))
  2100. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2101. "Handle line numbers and code references in ELEMENT.
  2102. ELEMENT has either a `src-block' an `example-block' type. INFO
  2103. is a plist used as a communication channel.
  2104. If optional argument NUM-FMT is a string, it will be used as
  2105. a format string for numbers at beginning of each line.
  2106. If optional argument REF-FMT is a string, it will be used as
  2107. a format string for each line of code containing a reference.
  2108. When optional argument DELAYED is non-nil, `org-loc' and
  2109. `org-coderef' properties, set to an adequate value, are applied
  2110. to, respectively, numbered lines and lines with a reference. No
  2111. line numbering is done and all references are stripped from the
  2112. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2113. in that situation.
  2114. Return new code as a string."
  2115. (let* ((switches (or (org-element-get-property :switches element) ""))
  2116. (code (org-element-get-property :value element))
  2117. (numberp (string-match "[-+]n\\>" switches))
  2118. (accumulatep (string-match "\\+n\\>" switches))
  2119. ;; Initialize loc counter when any kind of numbering is
  2120. ;; active.
  2121. (total-LOC (cond
  2122. (accumulatep (org-export-get-loc element info))
  2123. (numberp 0)))
  2124. ;; Get code and clean it. Remove blank lines at its
  2125. ;; beginning and end. Also remove protective commas.
  2126. (preserve-indent-p (or org-src-preserve-indentation
  2127. (string-match "-i\\>" switches)))
  2128. (replace-labels (when (string-match "-r\\>" switches)
  2129. (if (string-match "-k\\>" switches) 'keep t)))
  2130. (code (let ((c (replace-regexp-in-string
  2131. "\\`\\([ \t]*\n\\)+" ""
  2132. (replace-regexp-in-string
  2133. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2134. ;; If appropriate, remove global indentation.
  2135. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2136. ;; Free up the protected lines. Note: Org blocks
  2137. ;; have commas at the beginning or every line.
  2138. (if (string=
  2139. (or (org-element-get-property :language element) "")
  2140. "org")
  2141. (replace-regexp-in-string "^," "" c)
  2142. (replace-regexp-in-string
  2143. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2144. ;; Split code to process it line by line.
  2145. (code-lines (org-split-string code "\n"))
  2146. ;; If numbering is active, ensure line numbers will be
  2147. ;; correctly padded before applying the format string.
  2148. (num-fmt
  2149. (when (and (not delayed) numberp)
  2150. (format (if (stringp num-fmt) num-fmt "%s: ")
  2151. (format "%%%ds"
  2152. (length (number-to-string
  2153. (+ (length code-lines) total-LOC)))))))
  2154. ;; Get format used for references.
  2155. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2156. (match-string 1 switches))
  2157. org-coderef-label-format))
  2158. ;; Build a regexp matching a loc with a reference.
  2159. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2160. (replace-regexp-in-string
  2161. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2162. (org-element-normalize-string
  2163. (mapconcat
  2164. (lambda (loc)
  2165. ;; Maybe add line number to current line of code (LOC).
  2166. (when numberp
  2167. (incf total-LOC)
  2168. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2169. (concat (format num-fmt total-LOC) loc))))
  2170. ;; Take action if at a ref line.
  2171. (when (string-match with-ref-re loc)
  2172. (let ((ref (match-string 3 loc)))
  2173. (setq loc
  2174. ;; Option "-r" without "-k" removes labels.
  2175. ;; A non-nil DELAYED removes labels unconditionally.
  2176. (if (or delayed
  2177. (and replace-labels (not (eq replace-labels 'keep))))
  2178. (replace-match "" nil nil loc 1)
  2179. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2180. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2181. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2182. ;; If REF-FMT is defined, apply it to current LOC.
  2183. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2184. ;; Return updated LOC for concatenation.
  2185. loc)
  2186. code-lines "\n"))))
  2187. ;;;; For Tables
  2188. ;; `org-export-table-format-info' extracts formatting information
  2189. ;; (alignment, column groups and presence of a special column) from
  2190. ;; a raw table and returns it as a property list.
  2191. ;;
  2192. ;; `org-export-clean-table' cleans the raw table from any Org
  2193. ;; table-specific syntax.
  2194. (defun org-export-table-format-info (table)
  2195. "Extract info from TABLE.
  2196. Return a plist whose properties and values are:
  2197. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2198. `:column-groups' vector of symbols among `start', `end', `start-end',
  2199. `:row-groups' list of integers representing row groups.
  2200. `:special-column-p' non-nil if table has a special column.
  2201. `:width' vector of integers representing desired width of
  2202. current column, or nil."
  2203. (with-temp-buffer
  2204. (insert table)
  2205. (goto-char 1)
  2206. (org-table-align)
  2207. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2208. org-table-last-alignment)))
  2209. (width (make-vector (length org-table-last-alignment) nil))
  2210. (colgroups (make-vector (length org-table-last-alignment) nil))
  2211. (row-group 0)
  2212. (rowgroups)
  2213. (special-column-p 'empty))
  2214. (mapc (lambda (row)
  2215. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2216. (incf row-group)
  2217. ;; Determine if a special column is present by looking
  2218. ;; for special markers in the first column. More
  2219. ;; accurately, the first column is considered special
  2220. ;; if it only contains special markers and, maybe,
  2221. ;; empty cells.
  2222. (setq special-column-p
  2223. (cond
  2224. ((not special-column-p) nil)
  2225. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2226. 'special)
  2227. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2228. (cond
  2229. ;; Read forced alignment and width information, if any,
  2230. ;; and determine final alignment for the table.
  2231. ((org-table-cookie-line-p row)
  2232. (let ((col 0))
  2233. (mapc (lambda (field)
  2234. (when (string-match
  2235. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2236. (let ((align-data (match-string 1 field)))
  2237. (when align-data (aset align col align-data)))
  2238. (let ((w-data (match-string 2 field)))
  2239. (when w-data
  2240. (aset width col (string-to-number w-data)))))
  2241. (incf col))
  2242. (org-split-string row "[ \t]*|[ \t]*"))))
  2243. ;; Read column groups information.
  2244. ((org-table-colgroup-line-p row)
  2245. (let ((col 0))
  2246. (mapc (lambda (field)
  2247. (aset colgroups col
  2248. (cond ((string= "<" field) 'start)
  2249. ((string= ">" field) 'end)
  2250. ((string= "<>" field) 'start-end)))
  2251. (incf col))
  2252. (org-split-string row "[ \t]*|[ \t]*"))))
  2253. ;; Contents line.
  2254. (t (push row-group rowgroups))))
  2255. (org-split-string table "\n"))
  2256. ;; Return plist.
  2257. (list :alignment align
  2258. :column-groups colgroups
  2259. :row-groups (reverse rowgroups)
  2260. :special-column-p (eq special-column-p 'special)
  2261. :width width))))
  2262. (defun org-export-clean-table (table specialp)
  2263. "Clean string TABLE from its formatting elements.
  2264. Remove any row containing column groups or formatting cookies and
  2265. rows starting with a special marker. If SPECIALP is non-nil,
  2266. assume the table contains a special formatting column and remove
  2267. it also."
  2268. (let ((rows (org-split-string table "\n")))
  2269. (mapconcat 'identity
  2270. (delq nil
  2271. (mapcar
  2272. (lambda (row)
  2273. (cond
  2274. ((org-table-colgroup-line-p row) nil)
  2275. ((org-table-cookie-line-p row) nil)
  2276. ;; Ignore rows starting with a special marker.
  2277. ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
  2278. ;; Remove special column.
  2279. ((and specialp
  2280. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2281. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2282. (replace-match "\\1|" t nil row))
  2283. (t row)))
  2284. rows))
  2285. "\n")))
  2286. ;;;; For Tables Of Contents
  2287. ;; `org-export-collect-headlines' builds a list of all exportable
  2288. ;; headline elements, maybe limited to a certain depth. One can then
  2289. ;; easily parse it and transcode it.
  2290. ;; Building lists of tables, figures or listings is quite similar.
  2291. ;; Once the generic function `org-export-collect-elements' is defined,
  2292. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2293. ;; `org-export-collect-listings' can be derived from it.
  2294. (defun org-export-collect-headlines (info &optional n)
  2295. "Collect headlines in order to build a table of contents.
  2296. When non-nil, optional argument N must be an integer. It
  2297. specifies the depth of the table of contents.
  2298. Return a list of all exportable headlines as parsed elements."
  2299. (org-element-map
  2300. (plist-get info :parse-tree)
  2301. 'headline
  2302. (lambda (headline local)
  2303. ;; Strip contents from HEADLINE.
  2304. (let ((relative-level (org-export-get-relative-level headline local)))
  2305. (unless (and n (> relative-level n)) headline)))
  2306. info))
  2307. (defun org-export-collect-elements (type backend info)
  2308. "Collect named elements of type TYPE.
  2309. Only elements with a caption or a name are collected.
  2310. BACKEND is the back-end used to transcode their caption or name.
  2311. INFO is a plist holding export options.
  2312. Return an alist where key is entry's name and value an unique
  2313. identifier that might be used for internal links."
  2314. (org-element-map
  2315. (plist-get info :parse-tree)
  2316. type
  2317. (lambda (element info)
  2318. (let ((entry
  2319. (cond
  2320. ((org-element-get-property :caption element)
  2321. (org-export-secondary-string
  2322. (org-element-get-property :caption element) backend info))
  2323. ((org-element-get-property :name element)
  2324. (org-export-secondary-string
  2325. (org-element-get-property :name element) backend info)))))
  2326. ;; Skip elements with neither a caption nor a name.
  2327. (when entry (cons entry (org-element-get-property :begin element)))))
  2328. info))
  2329. (defun org-export-collect-tables (backend info)
  2330. "Build a list of tables.
  2331. BACKEND is the back-end used to transcode table's name. INFO is
  2332. a plist holding export options.
  2333. Return an alist where key is the caption of the table and value
  2334. an unique identifier that might be used for internal links."
  2335. (org-export-collect-elements 'table backend info))
  2336. (defun org-export-collect-figures (backend info)
  2337. "Build a list of figures.
  2338. A figure is a paragraph type element with a caption or a name.
  2339. BACKEND is the back-end used to transcode headline's name. INFO
  2340. is a plist holding export options.
  2341. Return an alist where key is the caption of the figure and value
  2342. an unique indentifier that might be used for internal links."
  2343. (org-export-collect-elements 'paragraph backend info))
  2344. (defun org-export-collect-listings (backend info)
  2345. "Build a list of src blocks.
  2346. BACKEND is the back-end used to transcode src block's name. INFO
  2347. is a plist holding export options.
  2348. Return an alist where key is the caption of the src block and
  2349. value an unique indentifier that might be used for internal
  2350. links."
  2351. (org-export-collect-elements 'src-block backend info))
  2352. ;;;; Topology
  2353. (defun org-export-get-parent-headline (blob info)
  2354. "Return BLOB's closest parent headline or nil."
  2355. (catch 'exit
  2356. (mapc
  2357. (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
  2358. (plist-get info :genealogy))
  2359. nil))
  2360. (defun org-export-get-previous-element (blob info)
  2361. "Return previous element or object.
  2362. BLOB is an element or object. INFO is a plist used as
  2363. a communication channel.
  2364. Return previous element or object, a string, or nil."
  2365. (let ((parent (car (plist-get info :genealogy))))
  2366. (cadr (member blob (reverse (org-element-get-contents parent))))))
  2367. ;;; The Dispatcher
  2368. ;; `org-export-dispatch' is the standard interactive way to start an
  2369. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2370. ;; for its interface. Most commons back-ends should have an entry in
  2371. ;; it.
  2372. (defun org-export-dispatch ()
  2373. "Export dispatcher for Org mode.
  2374. It provides an access to common export related tasks in a buffer.
  2375. Its interface comes in two flavours: standard and expert. While
  2376. both share the same set of bindings, only the former displays the
  2377. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2378. to switch to one or the other.
  2379. Return an error if key pressed has no associated command."
  2380. (interactive)
  2381. (let* ((input (org-export-dispatch-ui
  2382. (if (listp org-export-initial-scope) org-export-initial-scope
  2383. (list org-export-initial-scope))
  2384. org-export-dispatch-use-expert-ui))
  2385. (raw-key (car input))
  2386. (scope (cdr input)))
  2387. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2388. ;; depending on user's key pressed.
  2389. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2390. ;; Export with `e-latex' back-end.
  2391. (?L (let ((outbuf (org-export-to-buffer
  2392. 'e-latex "*Org E-latex Export*"
  2393. (memq 'subtree scope)
  2394. (memq 'visible scope)
  2395. (memq 'body scope))))
  2396. (with-current-buffer outbuf (latex-mode))
  2397. (when org-export-show-temporary-export-buffer
  2398. (switch-to-buffer-other-window outbuf))))
  2399. ((?l ?p ?d)
  2400. (org-export-to-file
  2401. 'e-latex
  2402. (cond ((eq raw-key ?p) #'org-e-latex-compile)
  2403. ((eq raw-key ?d)
  2404. (lambda (file) (org-open-file (org-e-latex-compile file)))))
  2405. (memq 'subtree scope)
  2406. (memq 'visible scope)
  2407. (memq 'body scope)))
  2408. ;; Undefined command.
  2409. (t (error "No command associated with key %s"
  2410. (char-to-string raw-key))))))
  2411. (defun org-export-dispatch-ui (scope expertp)
  2412. "Handle interface for `org-export-dispatch'.
  2413. SCOPE is a list containing current interactive options set for
  2414. export. It can contain any of the following symbols:
  2415. `body' toggles a body-only export
  2416. `subtree' restricts export to current subtree
  2417. `visible' restricts export to visible part of buffer.
  2418. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2419. buffer is provided, but indications about currently active
  2420. options are given in the prompt. Moreover, \[?] allows to switch
  2421. back to standard interface.
  2422. Return value is a list with key pressed as car and a list of
  2423. final interactive export options as cdr."
  2424. (let ((help (format "------------------- General Options -------------------
  2425. \[1] Body only: %s
  2426. \[2] Export scope: %s
  2427. \[3] Visible only: %s
  2428. -------------------- LaTeX Export ---------------------
  2429. \[l] to LaTeX file [L] to temporary buffer
  2430. \[p] to PDF file [d] ... and open it"
  2431. (if (memq 'body scope) "On" "Off")
  2432. (if (memq 'subtree scope) "Subtree" "Buffer")
  2433. (if (memq 'visible scope) "On" "Off")))
  2434. (standard-prompt "Export command: ")
  2435. (expert-prompt (format "Export command (%s%s%s): "
  2436. (if (memq 'body scope) "b" "-")
  2437. (if (memq 'subtree scope) "s" "-")
  2438. (if (memq 'visible scope) "v" "-")))
  2439. (handle-keypress
  2440. (function
  2441. ;; Read a character from command input, toggling interactive
  2442. ;; options when applicable. PROMPT is the displayed prompt,
  2443. ;; as a string.
  2444. (lambda (prompt)
  2445. (let ((key (read-char-exclusive prompt)))
  2446. (cond
  2447. ;; Ignore non-standard characters (i.e. "M-a").
  2448. ((not (characterp key)) (org-export-dispatch-ui scope expertp))
  2449. ;; Switch back to standard interface.
  2450. ((and (eq key ??) expertp) (org-export-dispatch-ui scope nil))
  2451. ((eq key ?1)
  2452. (org-export-dispatch-ui
  2453. (if (memq 'body scope) (remq 'body scope) (cons 'body scope))
  2454. expertp))
  2455. ((eq key ?2)
  2456. (org-export-dispatch-ui
  2457. (if (memq 'subtree scope) (remq 'subtree scope)
  2458. (cons 'subtree scope))
  2459. expertp))
  2460. ((eq key ?3)
  2461. (org-export-dispatch-ui
  2462. (if (memq 'visible scope) (remq 'visible scope)
  2463. (cons 'visible scope))
  2464. expertp))
  2465. (t (cons key scope))))))))
  2466. ;; With expert UI, just read key with a fancy prompt. In standard
  2467. ;; UI, display an intrusive help buffer.
  2468. (if expertp (funcall handle-keypress expert-prompt)
  2469. (save-window-excursion
  2470. (delete-other-windows)
  2471. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  2472. (org-fit-window-to-buffer
  2473. (get-buffer-window "*Org Export/Publishing Help*"))
  2474. (funcall handle-keypress standard-prompt)))))
  2475. (provide 'org-export)
  2476. ;;; org-export.el ends here