org-export.el 106 KB

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