org-export.el 116 KB

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