org-export.el 105 KB

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