org-export.el 110 KB

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