org-export.el 107 KB

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