org-export.el 108 KB

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