org-export.el 99 KB

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