org-export.el 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  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 about initial environment
  23. ;; or contextual data, all in a single place. The exhaustive list
  24. ;; of 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. Environment options are collected once at the very beginning of
  500. ;; the process, out of the original buffer and configuration.
  501. ;; Associated to the parse tree, they make an Org closure.
  502. ;; Collecting them is handled by `org-export-get-environment'
  503. ;; function.
  504. ;;
  505. ;; Most environment options are defined through the
  506. ;; `org-export-option-alist' variable.
  507. ;;
  508. ;; 2. Tree properties are extracted directly from the parsed tree,
  509. ;; just before export, by `org-export-collect-tree-properties'.
  510. ;;
  511. ;; 3. Local options are updated during parsing, and their value
  512. ;; depends on the level of recursion. For now, only `:genealogy'
  513. ;; belongs to that category.
  514. ;; Here is the full list of properties available during transcode
  515. ;; process, with their category (option, tree or local) and their
  516. ;; value type.
  517. ;; + `:author' :: Author's name.
  518. ;; - category :: option
  519. ;; - type :: string
  520. ;; + `:back-end' :: Current back-end used for transcoding.
  521. ;; - category :: tree
  522. ;; - type :: symbol
  523. ;; + `:creator' :: String to write as creation information.
  524. ;; - category :: option
  525. ;; - type :: string
  526. ;; + `:date' :: String to use as date.
  527. ;; - category :: option
  528. ;; - type :: string
  529. ;; + `:description' :: Description text for the current data.
  530. ;; - category :: option
  531. ;; - type :: string
  532. ;; + `:email' :: Author's email.
  533. ;; - category :: option
  534. ;; - type :: string
  535. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  536. ;; process.
  537. ;; - category :: option
  538. ;; - type :: list of strings
  539. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  540. ;; their definition, as parsed data. Only non-inlined footnotes
  541. ;; are represented in this alist. Also, every definition isn't
  542. ;; guaranteed to be referenced in the parse tree. The purpose of
  543. ;; this property is to preserve definitions from oblivion
  544. ;; (i.e. when the parse tree comes from a part of the original
  545. ;; buffer), it isn't meant for direct use in a back-end. To
  546. ;; retrieve a definition relative to a reference, use
  547. ;; `org-export-get-footnote-definition' instead.
  548. ;; - category :: option
  549. ;; - type :: alist (STRING . LIST)
  550. ;; + `:genealogy' :: Flat list of current object or element's parents
  551. ;; from closest to farthest.
  552. ;; - category :: local
  553. ;; - type :: list of elements and objects
  554. ;; + `:headline-levels' :: Maximum level being exported as an
  555. ;; headline. Comparison is done with the relative level of
  556. ;; headlines in the parse tree, not necessarily with their
  557. ;; actual level.
  558. ;; - category :: option
  559. ;; - type :: integer
  560. ;; + `:headline-offset' :: Difference between relative and real level
  561. ;; of headlines in the parse tree. For example, a value of -1
  562. ;; means a level 2 headline should be considered as level
  563. ;; 1 (cf. `org-export-get-relative-level').
  564. ;; - category :: tree
  565. ;; - type :: integer
  566. ;; + `:headline-numbering' :: Alist between headlines' beginning
  567. ;; position and their numbering, as a list of numbers
  568. ;; (cf. `org-export-get-headline-number').
  569. ;; - category :: tree
  570. ;; - type :: alist (INTEGER . LIST)
  571. ;; + `:input-file' :: Full path to input file, if any.
  572. ;; - category :: option
  573. ;; - type :: string or nil
  574. ;; + `:keywords' :: List of keywords attached to data.
  575. ;; - category :: option
  576. ;; - type :: string
  577. ;; + `:language' :: Default language used for translations.
  578. ;; - category :: option
  579. ;; - type :: string
  580. ;; + `:macro-input-file' :: Macro returning file name of input file,
  581. ;; or nil.
  582. ;; - category :: option
  583. ;; - type :: string or nil
  584. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  585. ;; transcoding.
  586. ;; - category :: global
  587. ;; - type :: list (as returned by `org-element-parse-buffer')
  588. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  589. ;; all line breaks.
  590. ;; - category :: option
  591. ;; - type :: symbol (nil, t)
  592. ;; + `:section-numbers' :: Non-nil means transcoding should add
  593. ;; section numbers to headlines.
  594. ;; - category :: option
  595. ;; - type :: symbol (nil, t)
  596. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  597. ;; in transcoding. When such a tag is present,
  598. ;; subtrees without it are de facto excluded from
  599. ;; the process. See `use-select-tags'.
  600. ;; - category :: option
  601. ;; - type :: list of strings
  602. ;; + `:target-list' :: List of targets encountered in the parse tree.
  603. ;; This is used to partly resolve "fuzzy" links
  604. ;; (cf. `org-export-resolve-fuzzy-link').
  605. ;; - category :: tree
  606. ;; - type :: list of strings
  607. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  608. ;; a time stamp in the output.
  609. ;; - category :: option
  610. ;; - type :: symbol (nil, t)
  611. ;; + `:use-select-tags' :: When non-nil, a select tags has been found
  612. ;; in the parse tree. Thus, any headline without one will be
  613. ;; filtered out. See `select-tags'.
  614. ;; - category :: tree
  615. ;; - type :: interger or nil
  616. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  617. ;; also be transcoded. If it is set to the `headline' symbol,
  618. ;; only the archived headline's name is retained.
  619. ;; - category :: option
  620. ;; - type :: symbol (nil, t, `headline')
  621. ;; + `:with-author' :: Non-nil means author's name should be included
  622. ;; in the output.
  623. ;; - category :: option
  624. ;; - type :: symbol (nil, t)
  625. ;; + `:with-creator' :: Non-nild means a creation sentence should be
  626. ;; inserted at the end of the transcoded string. If the value
  627. ;; is `comment', it should be commented.
  628. ;; - category :: option
  629. ;; - type :: symbol (`comment', nil, t)
  630. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  631. ;; its value is a list of names, only drawers with such names
  632. ;; will be transcoded.
  633. ;; - category :: option
  634. ;; - type :: symbol (nil, t) or list of strings
  635. ;; + `:with-email' :: Non-nil means output should contain author's
  636. ;; email.
  637. ;; - category :: option
  638. ;; - type :: symbol (nil, t)
  639. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  640. ;; interpreted.
  641. ;; - category :: option
  642. ;; - type :: symbol (nil, t)
  643. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  644. ;; strings starting with a colon as a fixed-with (verbatim) area.
  645. ;; - category :: option
  646. ;; - type :: symbol (nil, t)
  647. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  648. ;; footnotes.
  649. ;; - category :: option
  650. ;; - type :: symbol (nil, t)
  651. ;; + `:with-priority' :: Non-nil means transcoding should include
  652. ;; priority cookies.
  653. ;; - category :: option
  654. ;; - type :: symbol (nil, t)
  655. ;; + `:with-special-strings' :: Non-nil means transcoding should
  656. ;; interpret special strings in plain text.
  657. ;; - category :: option
  658. ;; - type :: symbol (nil, t)
  659. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  660. ;; interpret subscript and superscript. With a value of "{}",
  661. ;; only interpret those using curly brackets.
  662. ;; - category :: option
  663. ;; - type :: symbol (nil, {}, t)
  664. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  665. ;; tables.
  666. ;; - category :: option
  667. ;; - type :: symbol (nil, t)
  668. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  669. ;; headlines. A `not-in-toc' value will remove them
  670. ;; from the table of contents, if any, nonetheless.
  671. ;; - category :: option
  672. ;; - type :: symbol (nil, t, `not-in-toc')
  673. ;; + `:with-tasks' :: Non-nil means transcoding should include
  674. ;; headlines with a TODO keyword. A `todo' value
  675. ;; will only include headlines with a todo type
  676. ;; keyword while a `done' value will do the
  677. ;; contrary. If a list of strings is provided, only
  678. ;; tasks with keywords belonging to that list will
  679. ;; be kept.
  680. ;; - category :: option
  681. ;; - type :: symbol (t, todo, done, nil) or list of strings
  682. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  683. ;; time stamps and associated keywords. Otherwise, completely
  684. ;; remove them.
  685. ;; - category :: option
  686. ;; - type :: symbol: (t, nil)
  687. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  688. ;; added to the output. An integer value limits its
  689. ;; depth.
  690. ;; - category :: option
  691. ;; - type :: symbol (nil, t or integer)
  692. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  693. ;; include TODO keywords.
  694. ;; - category :: option
  695. ;; - type :: symbol (nil, t)
  696. ;;;; Environment Options
  697. ;; Environment options encompass all parameters defined outside the
  698. ;; scope of the parsed data. They come from five sources, in
  699. ;; increasing precedence order:
  700. ;;
  701. ;; - Global variables,
  702. ;; - Options keyword symbols,
  703. ;; - Buffer keywords,
  704. ;; - Subtree properties.
  705. ;; The central internal function with regards to environment options
  706. ;; is `org-export-get-environment'. It updates global variables with
  707. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  708. ;; the different sources.
  709. ;; The internal functions doing the retrieval are:
  710. ;; `org-export-parse-option-keyword' ,
  711. ;; `org-export-get-subtree-options' ,
  712. ;; `org-export-get-inbuffer-options' and
  713. ;; `org-export-get-global-options'.
  714. ;;
  715. ;; Some properties, which do not rely on the previous sources but
  716. ;; still depend on the original buffer, are taken care of with
  717. ;; `org-export-initial-options'.
  718. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  719. ;; take care of the part relative to "#+BIND:" keywords.
  720. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  721. "Collect export options from the current buffer.
  722. Optional argument BACKEND is a symbol specifying which back-end
  723. specific options to read, if any.
  724. When optional argument SUBTREEP is non-nil, assume the export is
  725. done against the current sub-tree.
  726. Third optional argument EXT-PLIST is a property list with
  727. external parameters overriding Org default settings, but still
  728. inferior to file-local settings."
  729. ;; First install #+BIND variables.
  730. (org-export-install-letbind-maybe)
  731. ;; Get and prioritize export options...
  732. (let ((options (org-combine-plists
  733. ;; ... from global variables...
  734. (org-export-get-global-options backend)
  735. ;; ... from buffer's name (default title)...
  736. `(:title
  737. ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
  738. (and file
  739. (file-name-sans-extension
  740. (file-name-nondirectory file))))
  741. (buffer-name (buffer-base-buffer))))
  742. ;; ... from an external property list...
  743. ext-plist
  744. ;; ... from in-buffer settings...
  745. (org-export-get-inbuffer-options
  746. backend
  747. (and buffer-file-name
  748. (org-remove-double-quotes buffer-file-name)))
  749. ;; ... and from subtree, when appropriate.
  750. (and subtreep (org-export-get-subtree-options)))))
  751. ;; Add initial options.
  752. (setq options (append (org-export-initial-options) options))
  753. ;; Return plist.
  754. options))
  755. (defun org-export-parse-option-keyword (options &optional backend)
  756. "Parse an OPTIONS line and return values as a plist.
  757. Optional argument BACKEND is a symbol specifying which back-end
  758. specific items to read, if any."
  759. (let* ((all
  760. (append org-export-option-alist
  761. (and backend
  762. (let ((var (intern
  763. (format "org-%s-option-alist" backend))))
  764. (and (boundp var) (eval var))))))
  765. ;; Build an alist between #+OPTION: item and property-name.
  766. (alist (delq nil
  767. (mapcar (lambda (e)
  768. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  769. (car e))))
  770. all)))
  771. plist)
  772. (mapc (lambda (e)
  773. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  774. (car e)
  775. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  776. options)
  777. (setq plist (plist-put plist
  778. (cdr e)
  779. (car (read-from-string
  780. (match-string 2 options)))))))
  781. alist)
  782. plist))
  783. (defun org-export-get-subtree-options ()
  784. "Get export options in subtree at point.
  785. Assume point is at subtree's beginning.
  786. Return options as a plist."
  787. (let (prop plist)
  788. (when (setq prop (progn (looking-at org-todo-line-regexp)
  789. (or (save-match-data
  790. (org-entry-get (point) "EXPORT_TITLE"))
  791. (org-match-string-no-properties 3))))
  792. (setq plist
  793. (plist-put
  794. plist :title
  795. (org-element-parse-secondary-string
  796. prop
  797. (cdr (assq 'keyword org-element-string-restrictions))))))
  798. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  799. (setq plist (plist-put plist :text prop)))
  800. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  801. (setq plist (plist-put plist :author prop)))
  802. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  803. (setq plist (plist-put plist :date prop)))
  804. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  805. (setq plist (org-export-add-options-to-plist plist prop)))
  806. plist))
  807. (defun org-export-get-inbuffer-options (&optional backend files)
  808. "Return current buffer export options, as a plist.
  809. Optional argument BACKEND, when non-nil, is a symbol specifying
  810. which back-end specific options should also be read in the
  811. process.
  812. Optional argument FILES is a list of setup files names read so
  813. far, used to avoid circular dependencies.
  814. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  815. (org-with-wide-buffer
  816. (goto-char (point-min))
  817. (let ((case-fold-search t) plist)
  818. ;; 1. Special keywords, as in `org-export-special-keywords'.
  819. (let ((special-re (org-make-options-regexp org-export-special-keywords)))
  820. (while (re-search-forward special-re nil t)
  821. (let ((element (org-element-at-point)))
  822. (when (eq (car element) 'keyword)
  823. (let ((key (upcase (org-element-get-property :key element)))
  824. (val (org-element-get-property :value element)))
  825. (setq plist
  826. (org-combine-plists
  827. plist
  828. (cond
  829. ((string= key "SETUP_FILE")
  830. (let ((file
  831. (expand-file-name
  832. (org-remove-double-quotes (org-trim val)))))
  833. ;; Avoid circular dependencies.
  834. (unless (member file files)
  835. (with-temp-buffer
  836. (insert (org-file-contents file 'noerror))
  837. (org-mode)
  838. (org-export-get-inbuffer-options
  839. backend (cons file files))))))
  840. ((string= key "OPTIONS")
  841. (org-export-parse-option-keyword val backend))
  842. ((string= key "MACRO")
  843. (when (string-match
  844. "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
  845. val)
  846. (let ((key
  847. (intern
  848. (concat ":macro-"
  849. (downcase (match-string 1 val)))))
  850. (value (match-string 2 val)))
  851. (cond
  852. ((not value) "")
  853. ((string-match "\\`(eval\\>" value)
  854. (list key value))
  855. (t
  856. (list
  857. key
  858. ;; If user explicitly asks for a newline, be
  859. ;; sure to preserve it from further filling
  860. ;; with `hard-newline'.
  861. (replace-regexp-in-string
  862. "\\\\n" hard-newline value)))))))))))))))
  863. ;; 2. Standard options, as in `org-export-option-alist'.
  864. (let* ((all (append org-export-option-alist
  865. ;; Also look for back-end specific options
  866. ;; if BACKEND is defined.
  867. (and backend
  868. (let ((var
  869. (intern
  870. (format "org-%s-option-alist" backend))))
  871. (and (boundp var) (eval var))))))
  872. ;; Build alist between keyword name and property name.
  873. (alist
  874. (delq nil (mapcar
  875. (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
  876. all)))
  877. ;; Build regexp matching all keywords associated to export
  878. ;; options. Note: the search is case insensitive.
  879. (opt-re (org-make-options-regexp
  880. (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
  881. (goto-char (point-min))
  882. (while (re-search-forward opt-re nil t)
  883. (let ((element (org-element-at-point)))
  884. (when (eq (car element) 'keyword)
  885. (let* ((key (upcase (org-element-get-property :key element)))
  886. (val (org-element-get-property :value element))
  887. (prop (cdr (assoc key alist)))
  888. (behaviour (nth 4 (assq prop all))))
  889. (setq plist
  890. (plist-put
  891. plist prop
  892. ;; Handle value depending on specified BEHAVIOUR.
  893. (case behaviour
  894. (space
  895. (if (not (plist-get plist prop)) (org-trim val)
  896. (concat (plist-get plist prop) " " (org-trim val))))
  897. (newline
  898. (org-trim
  899. (concat (plist-get plist prop) "\n" (org-trim val))))
  900. (split
  901. `(,@(plist-get plist prop) ,@(org-split-string val)))
  902. ('t val)
  903. (otherwise (plist-get plist prop)))))))))
  904. ;; Parse keywords specified in `org-element-parsed-keywords'.
  905. (mapc
  906. (lambda (key)
  907. (let* ((prop (cdr (assoc key alist)))
  908. (value (and prop (plist-get plist prop))))
  909. (when (stringp value)
  910. (setq plist
  911. (plist-put
  912. plist prop
  913. (org-element-parse-secondary-string
  914. value
  915. (cdr (assq 'keyword org-element-string-restrictions))))))))
  916. org-element-parsed-keywords))
  917. ;; 3. Return final value.
  918. plist)))
  919. (defun org-export-get-global-options (&optional backend)
  920. "Return global export options as a plist.
  921. Optional argument BACKEND, if non-nil, is a symbol specifying
  922. which back-end specific export options should also be read in the
  923. process."
  924. (let ((all (append org-export-option-alist
  925. (and backend
  926. (let ((var (intern
  927. (format "org-%s-option-alist" backend))))
  928. (and (boundp var) (eval var))))))
  929. ;; Output value.
  930. plist)
  931. (mapc (lambda (cell)
  932. (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
  933. all)
  934. ;; Return value.
  935. plist))
  936. (defun org-export-initial-options ()
  937. "Return a plist with properties related to input buffer."
  938. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  939. (list
  940. ;; Store full path of input file name, or nil. For internal use.
  941. :input-file visited-file
  942. ;; `:macro-date', `:macro-time' and `:macro-property' could as well
  943. ;; be initialized as tree properties, since they don't depend on
  944. ;; initial environment. Though, it may be more logical to keep
  945. ;; them close to other ":macro-" properties.
  946. :macro-date "(eval (format-time-string \"$1\"))"
  947. :macro-time "(eval (format-time-string \"$1\"))"
  948. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
  949. :macro-modification-time
  950. (and visited-file
  951. (file-exists-p visited-file)
  952. (concat "(eval (format-time-string \"$1\" '"
  953. (prin1-to-string (nth 5 (file-attributes visited-file)))
  954. "))"))
  955. ;; Store input file name as a macro.
  956. :macro-input-file (and visited-file (file-name-nondirectory visited-file))
  957. ;; Footnotes definitions must be collected in the original buffer,
  958. ;; as there's no insurance that they will still be in the parse
  959. ;; tree, due to some narrowing.
  960. :footnote-definition-alist
  961. (let (alist)
  962. (org-with-wide-buffer
  963. (goto-char (point-min))
  964. (while (re-search-forward org-footnote-definition-re nil t)
  965. (let ((def (org-footnote-at-definition-p)))
  966. (when def
  967. (org-skip-whitespace)
  968. (push (cons (car def)
  969. (save-restriction
  970. (narrow-to-region (point) (nth 2 def))
  971. ;; Like `org-element-parse-buffer', but
  972. ;; makes sure the definition doesn't start
  973. ;; with a section element.
  974. (nconc
  975. (list 'org-data nil)
  976. (org-element-parse-elements
  977. (point-min) (point-max) nil nil nil nil nil))))
  978. alist))))
  979. alist)))))
  980. (defvar org-export-allow-BIND-local nil)
  981. (defun org-export-confirm-letbind ()
  982. "Can we use #+BIND values during export?
  983. By default this will ask for confirmation by the user, to divert
  984. possible security risks."
  985. (cond
  986. ((not org-export-allow-BIND) nil)
  987. ((eq org-export-allow-BIND t) t)
  988. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  989. (t (org-set-local 'org-export-allow-BIND-local
  990. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  991. (defun org-export-install-letbind-maybe ()
  992. "Install the values from #+BIND lines as local variables.
  993. Variables must be installed before in-buffer options are
  994. retrieved."
  995. (let (letbind pair)
  996. (org-with-wide-buffer
  997. (goto-char (point-min))
  998. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  999. (when (org-export-confirm-letbind)
  1000. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  1001. letbind))))
  1002. (while (setq pair (pop letbind))
  1003. (org-set-local (car pair) (nth 1 pair)))))
  1004. ;;;; Tree Properties
  1005. ;; Tree properties are infromation extracted from parse tree. They
  1006. ;; are initialized at the beginning of the transcoding process by
  1007. ;; `org-export-collect-tree-properties'.
  1008. ;; Dedicated functions focus on computing the value of specific tree
  1009. ;; properties during initialization. Thus,
  1010. ;; `org-export-use-select-tag-p' determines if an headline makes use
  1011. ;; of an export tag enforcing inclusion. `org-export-get-min-level'
  1012. ;; gets the minimal exportable level, used as a basis to compute
  1013. ;; relative level for headlines. `org-export-get-point-max' returns
  1014. ;; the maximum exportable ending position in the parse tree.
  1015. ;; Eventually `org-export-collect-headline-numbering' builds an alist
  1016. ;; between headlines' beginning position and their numbering.
  1017. (defun org-export-collect-tree-properties (data info backend)
  1018. "Extract tree properties from parse tree.
  1019. DATA is the parse tree from which information is retrieved. INFO
  1020. is a list holding export options. BACKEND is the back-end called
  1021. for transcoding, as a symbol.
  1022. Following tree properties are set:
  1023. `:back-end' Back-end used for transcoding.
  1024. `:headline-offset' Offset between true level of headlines and
  1025. local level. An offset of -1 means an headline
  1026. of level 2 should be considered as a level
  1027. 1 headline in the context.
  1028. `:headline-numbering' Alist of all headlines' beginning position
  1029. as key an the associated numbering as value.
  1030. `:parse-tree' Whole parse tree.
  1031. `:target-list' List of all targets in the parse tree.
  1032. `:use-select-tags' Non-nil when parsed tree use a special tag to
  1033. enforce transcoding of the headline."
  1034. ;; First, set `:use-select-tags' property, as it will be required
  1035. ;; for further computations.
  1036. (setq info
  1037. (org-combine-plists
  1038. info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
  1039. ;; Then get `:headline-offset' in order to be able to use
  1040. ;; `org-export-get-relative-level'.
  1041. (setq info
  1042. (org-combine-plists
  1043. info `(:headline-offset ,(- 1 (org-export-get-min-level data info)))))
  1044. ;; Now, get the rest of the tree properties, now `:use-select-tags'
  1045. ;; is set...
  1046. (nconc
  1047. `(:parse-tree
  1048. ,data
  1049. :target-list
  1050. ,(org-element-map data 'target (lambda (target local) target) info)
  1051. :headline-numbering ,(org-export-collect-headline-numbering data info)
  1052. :back-end ,backend)
  1053. info))
  1054. (defun org-export-use-select-tags-p (data options)
  1055. "Non-nil when data use a tag enforcing transcoding.
  1056. DATA is parsed data as returned by `org-element-parse-buffer'.
  1057. OPTIONS is a plist holding export options."
  1058. (org-element-map
  1059. data
  1060. 'headline
  1061. (lambda (headline info)
  1062. (let ((tags (org-element-get-property :with-tags headline)))
  1063. (and tags (string-match
  1064. (format ":%s:" (plist-get info :select-tags)) tags))))
  1065. options
  1066. 'stop-at-first-match))
  1067. (defun org-export-get-min-level (data options)
  1068. "Return minimum exportable headline's level in DATA.
  1069. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1070. OPTIONS is a plist holding export options."
  1071. (catch 'exit
  1072. (let ((min-level 10000))
  1073. (mapc (lambda (blob)
  1074. (when (and (eq (car blob) 'headline)
  1075. (not (org-export-skip-p blob options)))
  1076. (setq min-level
  1077. (min (org-element-get-property :level blob) min-level)))
  1078. (when (= min-level 1) (throw 'exit 1)))
  1079. (org-element-get-contents data))
  1080. ;; If no headline was found, for the sake of consistency, set
  1081. ;; minimum level to 1 nonetheless.
  1082. (if (= min-level 10000) 1 min-level))))
  1083. (defun org-export-collect-headline-numbering (data options)
  1084. "Return numbering of all exportable headlines in a parse tree.
  1085. DATA is the parse tree. OPTIONS is the plist holding export
  1086. options.
  1087. Return an alist whose key is an headline and value is its
  1088. associated numbering \(in the shape of a list of numbers\)."
  1089. (let ((numbering (make-vector org-export-max-depth 0)))
  1090. (org-element-map
  1091. data
  1092. 'headline
  1093. (lambda (headline info)
  1094. (let ((relative-level
  1095. (1- (org-export-get-relative-level headline info))))
  1096. (cons
  1097. headline
  1098. (loop for n across numbering
  1099. for idx from 0 to org-export-max-depth
  1100. when (< idx relative-level) collect n
  1101. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1102. when (> idx relative-level) do (aset numbering idx 0)))))
  1103. options)))
  1104. ;;; The Transcoder
  1105. ;; This function reads Org data (obtained with, i.e.
  1106. ;; `org-element-parse-buffer') and transcodes it into a specified
  1107. ;; back-end output. It takes care of updating local properties,
  1108. ;; filtering out elements or objects according to export options and
  1109. ;; organizing the output blank lines and white space are preserved.
  1110. ;; Though, this function is inapropriate for secondary strings, which
  1111. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1112. ;; `org-export-secondary-string' is provided for that specific task.
  1113. ;; Internally, three functions handle the filtering of objects and
  1114. ;; elements during the export. More precisely, `org-export-skip-p'
  1115. ;; determines if the considered object or element should be ignored
  1116. ;; altogether, `org-export-interpret-p' tells which elements or
  1117. ;; objects should be seen as real Org syntax and `org-export-expand'
  1118. ;; transforms the others back into their original shape.
  1119. (defun org-export-data (data backend info)
  1120. "Convert DATA to a string into BACKEND format.
  1121. DATA is a nested list as returned by `org-element-parse-buffer'.
  1122. BACKEND is a symbol among supported exporters.
  1123. INFO is a plist holding export options and also used as
  1124. a communication channel between elements when walking the nested
  1125. list. See `org-export-update-info' function for more
  1126. details.
  1127. Return transcoded string."
  1128. (mapconcat
  1129. ;; BLOB can be an element, an object, a string, or nil.
  1130. (lambda (blob)
  1131. (cond
  1132. ((not blob) nil)
  1133. ;; BLOB is a string. Check if the optional transcoder for plain
  1134. ;; text exists, and call it in that case. Otherwise, simply
  1135. ;; return string. Also update INFO and call
  1136. ;; `org-export-filter-plain-text-functions'.
  1137. ((stringp blob)
  1138. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1139. (org-export-filter-apply-functions
  1140. (plist-get info :filter-plain-text)
  1141. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1142. backend info)))
  1143. ;; BLOB is an element or an object.
  1144. (t
  1145. (let* ((type (if (stringp blob) 'plain-text (car blob)))
  1146. ;; 1. Determine the appropriate TRANSCODER.
  1147. (transcoder
  1148. (cond
  1149. ;; 1.0 A full Org document is inserted.
  1150. ((eq type 'org-data) 'identity)
  1151. ;; 1.1. BLOB should be ignored.
  1152. ((org-export-skip-p blob info) nil)
  1153. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1154. ;; back into Org syntax.
  1155. ((not (org-export-interpret-p blob info))
  1156. 'org-export-expand)
  1157. ;; 1.3. Else apply naming convention.
  1158. (t (let ((trans (intern
  1159. (format "org-%s-%s" backend type))))
  1160. (and (fboundp trans) trans)))))
  1161. ;; 2. Compute CONTENTS of BLOB.
  1162. (contents
  1163. (cond
  1164. ;; Case 0. No transcoder defined: ignore BLOB.
  1165. ((not transcoder) nil)
  1166. ;; Case 1. Transparently export an Org document.
  1167. ((eq type 'org-data) (org-export-data blob backend info))
  1168. ;; Case 2. For a recursive object.
  1169. ((memq type org-element-recursive-objects)
  1170. (org-export-data
  1171. blob backend
  1172. (org-combine-plists
  1173. info
  1174. `(:genealogy ,(cons blob (plist-get info :genealogy))))))
  1175. ;; Case 3. For a recursive element.
  1176. ((memq type org-element-greater-elements)
  1177. ;; Ignore contents of an archived tree
  1178. ;; when `:with-archived-trees' is `headline'.
  1179. (unless (and
  1180. (eq type 'headline)
  1181. (eq (plist-get info :with-archived-trees) 'headline)
  1182. (org-element-get-property :archivedp blob))
  1183. (org-element-normalize-string
  1184. (org-export-data
  1185. blob backend
  1186. (org-combine-plists
  1187. info `(:genealogy
  1188. ,(cons blob (plist-get info :genealogy))))))))
  1189. ;; Case 4. For a paragraph.
  1190. ((eq type 'paragraph)
  1191. (let ((paragraph
  1192. (org-element-normalize-contents
  1193. blob
  1194. ;; When normalizing contents of an item or
  1195. ;; a footnote definition, ignore first line's
  1196. ;; indentation: there is none and it might be
  1197. ;; misleading.
  1198. (and (not (org-export-get-previous-element blob info))
  1199. (let ((parent (caar (plist-get info :genealogy))))
  1200. (memq parent '(footnote-definition item)))))))
  1201. (org-export-data
  1202. paragraph backend
  1203. (org-combine-plists
  1204. info `(:genealogy
  1205. ,(cons paragraph (plist-get info :genealogy)))))))))
  1206. ;; 3. Transcode BLOB into RESULTS string.
  1207. (results (cond
  1208. ((not transcoder) nil)
  1209. ((eq transcoder 'org-export-expand)
  1210. (org-export-data
  1211. `(org-data nil ,(funcall transcoder blob contents))
  1212. backend info))
  1213. (t (funcall transcoder blob contents info)))))
  1214. ;; 4. Discard nil results. Otherwise, update INFO, append
  1215. ;; the same white space between elements or objects as in
  1216. ;; the original buffer, and call appropriate filters.
  1217. (when results
  1218. ;; No filter for a full document.
  1219. (if (eq type 'org-data) results
  1220. (org-export-filter-apply-functions
  1221. (plist-get info (intern (format ":filter-%s" type)))
  1222. (let ((post-blank (org-element-get-property :post-blank blob)))
  1223. (if (memq type org-element-all-elements)
  1224. (concat (org-element-normalize-string results)
  1225. (make-string post-blank ?\n))
  1226. (concat results (make-string post-blank ? ))))
  1227. backend info)))))))
  1228. (org-element-get-contents data) ""))
  1229. (defun org-export-secondary-string (secondary backend info)
  1230. "Convert SECONDARY string into BACKEND format.
  1231. SECONDARY is a nested list as returned by
  1232. `org-element-parse-secondary-string'.
  1233. BACKEND is a symbol among supported exporters. INFO is a plist
  1234. used as a communication channel.
  1235. Return transcoded string."
  1236. ;; Make SECONDARY acceptable for `org-export-data'.
  1237. (let ((s (if (listp secondary) secondary (list secondary))))
  1238. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1239. (defun org-export-skip-p (blob info)
  1240. "Non-nil when element or object BLOB should be skipped during export.
  1241. INFO is the plist holding export options."
  1242. ;; Check headline.
  1243. (unless (stringp blob)
  1244. (case (car blob)
  1245. ('headline
  1246. (let ((with-tasks (plist-get info :with-tasks))
  1247. (todo (org-element-get-property :todo-keyword blob))
  1248. (todo-type (org-element-get-property :todo-type blob))
  1249. (archived (plist-get info :with-archived-trees))
  1250. (tag-list (let ((tags (org-element-get-property :tags blob)))
  1251. (and tags (org-split-string tags ":")))))
  1252. (or
  1253. ;; Ignore subtrees with an exclude tag.
  1254. (loop for k in (plist-get info :exclude-tags)
  1255. thereis (member k tag-list))
  1256. ;; Ignore subtrees without a select tag, when such tag is found
  1257. ;; in the buffer.
  1258. (and (plist-get info :use-select-tags)
  1259. (loop for k in (plist-get info :select-tags)
  1260. never (member k tag-list)))
  1261. ;; Ignore commented sub-trees.
  1262. (org-element-get-property :commentedp blob)
  1263. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1264. (and (not archived) (org-element-get-property :archivedp blob))
  1265. ;; Ignore tasks, if specified by `:with-tasks' property.
  1266. (and todo (not with-tasks))
  1267. (and todo
  1268. (memq with-tasks '(todo done))
  1269. (not (eq todo-type with-tasks)))
  1270. (and todo
  1271. (consp with-tasks)
  1272. (not (member todo with-tasks))))))
  1273. ;; Check time-stamp.
  1274. ('time-stamp (not (plist-get info :with-timestamps)))
  1275. ;; Check drawer.
  1276. ('drawer
  1277. (or (not (plist-get info :with-drawers))
  1278. (and (consp (plist-get info :with-drawers))
  1279. (not (member (org-element-get-property :drawer-name blob)
  1280. (plist-get info :with-drawers))))))
  1281. ;; Check export snippet.
  1282. ('export-snippet
  1283. (let* ((raw-back-end (org-element-get-property :back-end blob))
  1284. (true-back-end
  1285. (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
  1286. raw-back-end)))
  1287. (not (string= (symbol-name (plist-get info :back-end))
  1288. true-back-end)))))))
  1289. (defun org-export-interpret-p (blob info)
  1290. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1291. Check is done according to export options INFO, stored as
  1292. a plist."
  1293. (case (car blob)
  1294. ;; ... entities...
  1295. (entity (plist-get info :with-entities))
  1296. ;; ... emphasis...
  1297. (emphasis (plist-get info :with-emphasize))
  1298. ;; ... fixed-width areas.
  1299. (fixed-width (plist-get info :with-fixed-width))
  1300. ;; ... footnotes...
  1301. ((footnote-definition footnote-reference)
  1302. (plist-get info :with-footnotes))
  1303. ;; ... sub/superscripts...
  1304. ((subscript superscript)
  1305. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1306. (if (eq sub/super-p '{})
  1307. (org-element-get-property :use-brackets-p blob)
  1308. sub/super-p)))
  1309. ;; ... tables...
  1310. (table (plist-get info :with-tables))
  1311. (otherwise t)))
  1312. (defsubst org-export-expand (blob contents)
  1313. "Expand a parsed element or object to its original state.
  1314. BLOB is either an element or an object. CONTENTS is its
  1315. contents, as a string or nil."
  1316. (funcall
  1317. (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
  1318. ;;; The Filter System
  1319. ;; Filters allow end-users to tweak easily the transcoded output.
  1320. ;; They are the functional counterpart of hooks, as every filter in
  1321. ;; a set is applied to the return value of the previous one.
  1322. ;; Every set is back-end agnostic. Although, a filter is always
  1323. ;; called, in addition to the string it applies to, with the back-end
  1324. ;; used as argument, so it's easy enough for the end-user to add
  1325. ;; back-end specific filters in the set. The communication channel,
  1326. ;; as a plist, is required as the third argument.
  1327. ;; Filters sets are defined below. There are of four types:
  1328. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1329. ;; complete parsed tree. It's the only filters set that doesn't
  1330. ;; apply to a string.
  1331. ;; - `org-export-filter-final-output-functions' applies to the final
  1332. ;; transcoded string.
  1333. ;; - `org-export-filter-plain-text-functions' applies to any string
  1334. ;; not recognized as Org syntax.
  1335. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1336. ;; after an element or object of type TYPE has been transcoded.
  1337. ;; All filters sets are applied through
  1338. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1339. ;; applied in a LIFO fashion. It allows developers to be sure that
  1340. ;; their filters will be applied first.
  1341. ;;;; Special Filters
  1342. (defvar org-export-filter-parse-tree-functions nil
  1343. "Filter, or list of filters, applied to the parsed tree.
  1344. Each filter is called with three arguments: the parse tree, as
  1345. returned by `org-element-parse-buffer', the back-end, as
  1346. a symbol, and the communication channel, as a plist. It must
  1347. return the modified parse tree to transcode.")
  1348. (defvar org-export-filter-final-output-functions nil
  1349. "Filter, or list of filters, applied to the transcoded string.
  1350. Each filter is called with three arguments: the full transcoded
  1351. string, the back-end, as a symbol, and the communication channel,
  1352. as a plist. It must return a string that will be used as the
  1353. final export output.")
  1354. (defvar org-export-filter-plain-text-functions nil
  1355. "Filter, or list of filters, applied to plain text.
  1356. Each filter is called with three arguments: a string which
  1357. contains no Org syntax, the back-end, as a symbol, and the
  1358. communication channel, as a plist. It must return a string or
  1359. nil.")
  1360. ;;;; Elements Filters
  1361. (defvar org-export-filter-center-block-functions nil
  1362. "List of functions applied to a transcoded center block.
  1363. Each filter is called with three arguments: the transcoded center
  1364. block, as a string, the back-end, as a symbol, and the
  1365. communication channel, as a plist. It must return a string or
  1366. nil.")
  1367. (defvar org-export-filter-drawer-functions nil
  1368. "List of functions applied to a transcoded drawer.
  1369. Each filter is called with three arguments: the transcoded
  1370. drawer, as a string, the back-end, as a symbol, and the
  1371. communication channel, as a plist. It must return a string or
  1372. nil.")
  1373. (defvar org-export-filter-dynamic-block-functions nil
  1374. "List of functions applied to a transcoded dynamic-block.
  1375. Each filter is called with three arguments: the transcoded
  1376. dynamic-block, as a string, the back-end, as a symbol, and the
  1377. communication channel, as a plist. It must return a string or
  1378. nil.")
  1379. (defvar org-export-filter-headline-functions nil
  1380. "List of functions applied to a transcoded headline.
  1381. Each filter is called with three arguments: the transcoded
  1382. headline, as a string, 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-inlinetask-functions nil
  1386. "List of functions applied to a transcoded inlinetask.
  1387. Each filter is called with three arguments: the transcoded
  1388. inlinetask, as a string, the back-end, as a symbol, and the
  1389. communication channel, as a plist. It must return a string or
  1390. nil.")
  1391. (defvar org-export-filter-plain-list-functions nil
  1392. "List of functions applied to a transcoded plain-list.
  1393. Each filter is called with three arguments: the transcoded
  1394. plain-list, as a string, the back-end, as a symbol, and the
  1395. communication channel, as a plist. It must return a string or
  1396. nil.")
  1397. (defvar org-export-filter-item-functions nil
  1398. "List of functions applied to a transcoded item.
  1399. Each filter is called with three arguments: the transcoded item,
  1400. as a string, the back-end, as a symbol, and the communication
  1401. channel, as a plist. It must return a string or nil.")
  1402. (defvar org-export-filter-comment-functions nil
  1403. "List of functions applied to a transcoded comment.
  1404. Each filter is called with three arguments: the transcoded
  1405. comment, as a string, the back-end, as a symbol, and the
  1406. communication channel, as a plist. It must return a string or
  1407. nil.")
  1408. (defvar org-export-filter-comment-block-functions nil
  1409. "List of functions applied to a transcoded comment-comment.
  1410. Each filter is called with three arguments: the transcoded
  1411. comment-block, as a string, the back-end, as a symbol, and the
  1412. communication channel, as a plist. It must return a string or
  1413. nil.")
  1414. (defvar org-export-filter-example-block-functions nil
  1415. "List of functions applied to a transcoded example-block.
  1416. Each filter is called with three arguments: the transcoded
  1417. example-block, as a string, the back-end, as a symbol, and the
  1418. communication channel, as a plist. It must return a string or
  1419. nil.")
  1420. (defvar org-export-filter-export-block-functions nil
  1421. "List of functions applied to a transcoded export-block.
  1422. Each filter is called with three arguments: the transcoded
  1423. export-block, as a string, the back-end, as a symbol, and the
  1424. communication channel, as a plist. It must return a string or
  1425. nil.")
  1426. (defvar org-export-filter-fixed-width-functions nil
  1427. "List of functions applied to a transcoded fixed-width.
  1428. Each filter is called with three arguments: the transcoded
  1429. fixed-width, as a string, the back-end, as a symbol, and the
  1430. communication channel, as a plist. It must return a string or
  1431. nil.")
  1432. (defvar org-export-filter-footnote-definition-functions nil
  1433. "List of functions applied to a transcoded footnote-definition.
  1434. Each filter is called with three arguments: the transcoded
  1435. footnote-definition, as a string, the back-end, as a symbol, and
  1436. the communication channel, as a plist. It must return a string
  1437. or nil.")
  1438. (defvar org-export-filter-horizontal-rule-functions nil
  1439. "List of functions applied to a transcoded horizontal-rule.
  1440. Each filter is called with three arguments: the transcoded
  1441. horizontal-rule, as a string, the back-end, as a symbol, and the
  1442. communication channel, as a plist. It must return a string or
  1443. nil.")
  1444. (defvar org-export-filter-keyword-functions nil
  1445. "List of functions applied to a transcoded keyword.
  1446. Each filter is called with three arguments: the transcoded
  1447. keyword, as a string, the back-end, as a symbol, and the
  1448. communication channel, as a plist. It must return a string or
  1449. nil.")
  1450. (defvar org-export-filter-latex-environment-functions nil
  1451. "List of functions applied to a transcoded latex-environment.
  1452. Each filter is called with three arguments: the transcoded
  1453. latex-environment, as a string, the back-end, as a symbol, and
  1454. the communication channel, as a plist. It must return a string
  1455. or nil.")
  1456. (defvar org-export-filter-babel-call-functions nil
  1457. "List of functions applied to a transcoded babel-call.
  1458. Each filter is called with three arguments: the transcoded
  1459. babel-call, as a string, the back-end, as a symbol, and the
  1460. communication channel, as a plist. It must return a string or
  1461. nil.")
  1462. (defvar org-export-filter-paragraph-functions nil
  1463. "List of functions applied to a transcoded paragraph.
  1464. Each filter is called with three arguments: the transcoded
  1465. paragraph, as a string, the back-end, as a symbol, and the
  1466. communication channel, as a plist. It must return a string or
  1467. nil.")
  1468. (defvar org-export-filter-property-drawer-functions nil
  1469. "List of functions applied to a transcoded property-drawer.
  1470. Each filter is called with three arguments: the transcoded
  1471. property-drawer, as a string, the back-end, as a symbol, and the
  1472. communication channel, as a plist. It must return a string or
  1473. nil.")
  1474. (defvar org-export-filter-quote-block-functions nil
  1475. "List of functions applied to a transcoded quote block.
  1476. Each filter is called with three arguments: the transcoded quote
  1477. block, as a string, the back-end, as a symbol, and the
  1478. communication channel, as a plist. It must return a string or
  1479. nil.")
  1480. (defvar org-export-filter-quote-section-functions nil
  1481. "List of functions applied to a transcoded quote-section.
  1482. Each filter is called with three arguments: the transcoded
  1483. quote-section, as a string, the back-end, as a symbol, and the
  1484. communication channel, as a plist. It must return a string or
  1485. nil.")
  1486. (defvar org-export-filter-section-functions nil
  1487. "List of functions applied to a transcoded section.
  1488. Each filter is called with three arguments: the transcoded
  1489. section, as a string, the back-end, as a symbol, and the
  1490. communication channel, as a plist. It must return a string or
  1491. nil.")
  1492. (defvar org-export-filter-special-block-functions nil
  1493. "List of functions applied to a transcoded special block.
  1494. Each filter is called with three arguments: the transcoded
  1495. special block, as a string, the back-end, as a symbol, and the
  1496. communication channel, as a plist. It must return a string or
  1497. nil.")
  1498. (defvar org-export-filter-src-block-functions nil
  1499. "List of functions applied to a transcoded src-block.
  1500. Each filter is called with three arguments: the transcoded
  1501. src-block, as a string, the back-end, as a symbol, and the
  1502. communication channel, as a plist. It must return a string or
  1503. nil.")
  1504. (defvar org-export-filter-table-functions nil
  1505. "List of functions applied to a transcoded table.
  1506. Each filter is called with three arguments: the transcoded table,
  1507. as a string, the back-end, as a symbol, and the communication
  1508. channel, as a plist. It must return a string or nil.")
  1509. (defvar org-export-filter-verse-block-functions nil
  1510. "List of functions applied to a transcoded verse block.
  1511. Each filter is called with three arguments: the transcoded verse
  1512. block, as a string, the back-end, as a symbol, and the
  1513. communication channel, as a plist. It must return a string or
  1514. nil.")
  1515. ;;;; Objects Filters
  1516. (defvar org-export-filter-emphasis-functions nil
  1517. "List of functions applied to a transcoded emphasis.
  1518. Each filter is called with three arguments: the transcoded
  1519. emphasis, as a string, the back-end, as a symbol, and the
  1520. communication channel, as a plist. It must return a string or
  1521. nil.")
  1522. (defvar org-export-filter-entity-functions nil
  1523. "List of functions applied to a transcoded entity.
  1524. Each filter is called with three arguments: the transcoded
  1525. entity, as a string, the back-end, as a symbol, and the
  1526. communication channel, as a plist. It must return a string or
  1527. nil.")
  1528. (defvar org-export-filter-export-snippet-functions nil
  1529. "List of functions applied to a transcoded export-snippet.
  1530. Each filter is called with three arguments: the transcoded
  1531. export-snippet, as a string, the back-end, as a symbol, and the
  1532. communication channel, as a plist. It must return a string or
  1533. nil.")
  1534. (defvar org-export-filter-footnote-reference-functions nil
  1535. "List of functions applied to a transcoded footnote-reference.
  1536. Each filter is called with three arguments: the transcoded
  1537. footnote-reference, as a string, the back-end, as a symbol, and
  1538. the communication channel, as a plist. It must return a string
  1539. or nil.")
  1540. (defvar org-export-filter-inline-babel-call-functions nil
  1541. "List of functions applied to a transcoded inline-babel-call.
  1542. Each filter is called with three arguments: the transcoded
  1543. inline-babel-call, as a string, the back-end, as a symbol, and
  1544. the communication channel, as a plist. It must return a string
  1545. or nil.")
  1546. (defvar org-export-filter-inline-src-block-functions nil
  1547. "List of functions applied to a transcoded inline-src-block.
  1548. Each filter is called with three arguments: the transcoded
  1549. inline-src-block, as a string, the back-end, as a symbol, and the
  1550. communication channel, as a plist. It must return a string or
  1551. nil.")
  1552. (defvar org-export-filter-latex-fragment-functions nil
  1553. "List of functions applied to a transcoded latex-fragment.
  1554. Each filter is called with three arguments: the transcoded
  1555. latex-fragment, as a string, the back-end, as a symbol, and the
  1556. communication channel, as a plist. It must return a string or
  1557. nil.")
  1558. (defvar org-export-filter-line-break-functions nil
  1559. "List of functions applied to a transcoded line-break.
  1560. Each filter is called with three arguments: the transcoded
  1561. line-break, as a string, the back-end, as a symbol, and the
  1562. communication channel, as a plist. It must return a string or
  1563. nil.")
  1564. (defvar org-export-filter-link-functions nil
  1565. "List of functions applied to a transcoded link.
  1566. Each filter is called with three arguments: the transcoded link,
  1567. as a string, the back-end, as a symbol, and the communication
  1568. channel, as a plist. It must return a string or nil.")
  1569. (defvar org-export-filter-macro-functions nil
  1570. "List of functions applied to a transcoded macro.
  1571. Each filter is called with three arguments: the transcoded macro,
  1572. as a string, the back-end, as a symbol, and the communication
  1573. channel, as a plist. It must return a string or nil.")
  1574. (defvar org-export-filter-radio-target-functions nil
  1575. "List of functions applied to a transcoded radio-target.
  1576. Each filter is called with three arguments: the transcoded
  1577. radio-target, as a string, the back-end, as a symbol, and the
  1578. communication channel, as a plist. It must return a string or
  1579. nil.")
  1580. (defvar org-export-filter-statistics-cookie-functions nil
  1581. "List of functions applied to a transcoded statistics-cookie.
  1582. Each filter is called with three arguments: the transcoded
  1583. statistics-cookie, as a string, the back-end, as a symbol, and
  1584. the communication channel, as a plist. It must return a string
  1585. or nil.")
  1586. (defvar org-export-filter-subscript-functions nil
  1587. "List of functions applied to a transcoded subscript.
  1588. Each filter is called with three arguments: the transcoded
  1589. subscript, as a string, the back-end, as a symbol, and the
  1590. communication channel, as a plist. It must return a string or
  1591. nil.")
  1592. (defvar org-export-filter-superscript-functions nil
  1593. "List of functions applied to a transcoded superscript.
  1594. Each filter is called with three arguments: the transcoded
  1595. superscript, as a string, the back-end, as a symbol, and the
  1596. communication channel, as a plist. It must return a string or
  1597. nil.")
  1598. (defvar org-export-filter-target-functions nil
  1599. "List of functions applied to a transcoded target.
  1600. Each filter is called with three arguments: the transcoded
  1601. target, as a string, the back-end, as a symbol, and the
  1602. communication channel, as a plist. It must return a string or
  1603. nil.")
  1604. (defvar org-export-filter-time-stamp-functions nil
  1605. "List of functions applied to a transcoded time-stamp.
  1606. Each filter is called with three arguments: the transcoded
  1607. time-stamp, as a string, the back-end, as a symbol, and the
  1608. communication channel, as a plist. It must return a string or
  1609. nil.")
  1610. (defvar org-export-filter-verbatim-functions nil
  1611. "List of functions applied to a transcoded verbatim.
  1612. Each filter is called with three arguments: the transcoded
  1613. verbatim, as a string, the back-end, as a symbol, and the
  1614. communication channel, as a plist. It must return a string or
  1615. nil.")
  1616. (defun org-export-filter-apply-functions (filters value backend info)
  1617. "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
  1618. Functions are called in a LIFO fashion, to be sure that developer
  1619. specified filters, if any, are called first."
  1620. (loop for filter in filters
  1621. if (not value) return nil else
  1622. do (setq value (funcall filter value backend info)))
  1623. value)
  1624. ;;; Core functions
  1625. ;; This is the room for the main function, `org-export-as', along with
  1626. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1627. ;; They differ only by the way they output the resulting code.
  1628. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1629. ;; used with `org-export-to-file'. With a given extension, it tries
  1630. ;; to provide a canonical file name to write export output to.
  1631. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1632. ;; but a copy of it (with the same buffer-local variables and
  1633. ;; visibility), where include keywords are expanded and Babel blocks
  1634. ;; are executed, if appropriate.
  1635. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1636. ;; File inclusion is taken care of by
  1637. ;; `org-export-expand-include-keyword' and
  1638. ;; `org-export-prepare-file-contents'. Structure wise, including
  1639. ;; a whole Org file in a buffer often makes little sense. For
  1640. ;; example, if the file contains an headline and the include keyword
  1641. ;; was within an item, the item should contain the headline. That's
  1642. ;; why file inclusion should be done before any structure can be
  1643. ;; associated to the file, that is before parsing.
  1644. (defun org-export-as (backend
  1645. &optional subtreep visible-only body-only ext-plist)
  1646. "Transcode current Org buffer into BACKEND code.
  1647. If narrowing is active in the current buffer, only transcode its
  1648. narrowed part.
  1649. If a region is active, transcode that region.
  1650. When optional argument SUBTREEP is non-nil, transcode the
  1651. sub-tree at point, extracting information from the headline
  1652. properties first.
  1653. When optional argument VISIBLE-ONLY is non-nil, don't export
  1654. contents of hidden elements.
  1655. When optional argument BODY-ONLY is non-nil, only return body
  1656. code, without preamble nor postamble.
  1657. EXT-PLIST, when provided, is a property list with external
  1658. parameters overriding Org default settings, but still inferior to
  1659. file-local settings.
  1660. Return code as a string."
  1661. (save-excursion
  1662. (save-restriction
  1663. ;; Narrow buffer to an appropriate region for parsing.
  1664. (when (org-region-active-p)
  1665. (narrow-to-region (region-beginning) (region-end)))
  1666. (when (and subtreep (not (org-at-heading-p)))
  1667. ;; Ensure point is at sub-tree's beginning.
  1668. (org-narrow-to-subtree))
  1669. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
  1670. ;; Then options can be completed with tree properties. Note:
  1671. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1672. ;; created, where include keywords are expanded and code blocks
  1673. ;; are evaluated. RAW-DATA is the parsed tree of the buffer
  1674. ;; resulting from that process. Eventually call
  1675. ;; `org-export-filter-parse-tree-functions'.
  1676. (goto-char (point-min))
  1677. (let ((info (org-export-get-environment backend subtreep ext-plist)))
  1678. ;; Remove subtree's headline from contents if subtree mode is
  1679. ;; activated.
  1680. (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
  1681. (let ((raw-data (org-export-filter-apply-functions
  1682. (plist-get info :filter-parse-tree)
  1683. (org-export-with-current-buffer-copy
  1684. (org-export-expand-include-keyword nil)
  1685. (let ((org-current-export-file (current-buffer)))
  1686. (org-export-blocks-preprocess))
  1687. (org-element-parse-buffer nil visible-only))
  1688. backend info)))
  1689. ;; Complete communication channel with tree properties.
  1690. (setq info
  1691. (org-combine-plists
  1692. info
  1693. (org-export-collect-tree-properties raw-data info backend)))
  1694. ;; Transcode RAW-DATA. Also call
  1695. ;; `org-export-filter-final-output-functions'.
  1696. (let* ((body (org-element-normalize-string
  1697. (org-export-data raw-data backend info)))
  1698. (template (intern (format "org-%s-template" backend)))
  1699. (output (org-export-filter-apply-functions
  1700. (plist-get info :filter-final-output)
  1701. (if (or (not (fboundp template)) body-only) body
  1702. (funcall template body info))
  1703. backend info)))
  1704. ;; Maybe add final OUTPUT to kill ring before returning it.
  1705. (when org-export-copy-to-kill-ring (org-kill-new output))
  1706. output))))))
  1707. (defun org-export-to-buffer (backend buffer &optional subtreep visible-only
  1708. body-only ext-plist)
  1709. "Call `org-export-as' with output to a specified buffer.
  1710. BACKEND is the back-end used for transcoding, as a symbol.
  1711. BUFFER is the output buffer. If it already exists, it will be
  1712. erased first, otherwise, it will be created.
  1713. Arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and EXT-PLIST are
  1714. similar to those used in `org-export-as', which see.
  1715. Return buffer."
  1716. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
  1717. (buffer (get-buffer-create buffer)))
  1718. (with-current-buffer buffer
  1719. (erase-buffer)
  1720. (insert out)
  1721. (goto-char (point-min)))
  1722. buffer))
  1723. (defun org-export-to-file
  1724. (backend file &optional subtreep visible-only body-only ext-plist)
  1725. "Call `org-export-as' with output to a specified file.
  1726. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1727. is the name of the output file, as a string.
  1728. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  1729. EXT-PLIST are similar to those used in `org-export-as', which
  1730. see.
  1731. Return output file's name."
  1732. ;; Checks for FILE permissions. `write-file' would do the same, but
  1733. ;; we'd rather avoid needless transcoding of parse tree.
  1734. (unless (file-writable-p file) (error "Output file not writable"))
  1735. ;; Insert contents to a temporary buffer and write it to FILE.
  1736. (let ((out (org-export-as
  1737. backend subtreep visible-only body-only ext-plist)))
  1738. (with-temp-buffer
  1739. (insert out)
  1740. (let ((coding-system-for-write org-export-coding-system))
  1741. (write-file file))))
  1742. ;; Return full path.
  1743. file)
  1744. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  1745. "Return output file's name according to buffer specifications.
  1746. EXTENSION is a string representing the output file extension,
  1747. with the leading dot.
  1748. With a non-nil optional argument SUBTREEP, try to determine
  1749. output file's name by looking for \"EXPORT_FILE_NAME\" property
  1750. of subtree at point.
  1751. When optional argument PUB-DIR is set, use it as the publishing
  1752. directory.
  1753. Return file name as a string, or nil if it couldn't be
  1754. determined."
  1755. (let ((base-name
  1756. ;; File name may come from EXPORT_FILE_NAME subtree property,
  1757. ;; assuming point is at beginning of said sub-tree.
  1758. (file-name-sans-extension
  1759. (or (and subtreep
  1760. (org-entry-get
  1761. (save-excursion
  1762. (ignore-errors
  1763. (org-back-to-heading (not visible-only)) (point)))
  1764. "EXPORT_FILE_NAME" t))
  1765. ;; File name may be extracted from buffer's associated
  1766. ;; file, if any.
  1767. (buffer-file-name (buffer-base-buffer))
  1768. ;; Can't determine file name on our own: Ask user.
  1769. (let ((read-file-name-function
  1770. (and org-completion-use-ido 'ido-read-file-name)))
  1771. (read-file-name
  1772. "Output file: " pub-dir nil nil nil
  1773. (lambda (name)
  1774. (string= (file-name-extension name t) extension))))))))
  1775. ;; Build file name. Enforce EXTENSION over whatever user may have
  1776. ;; come up with. PUB-DIR, if defined, always has precedence over
  1777. ;; any provided path.
  1778. (cond
  1779. (pub-dir
  1780. (concat (file-name-as-directory pub-dir)
  1781. (file-name-nondirectory base-name)
  1782. extension))
  1783. ((string= (file-name-nondirectory base-name) base-name)
  1784. (concat (file-name-as-directory ".") base-name extension))
  1785. (t (concat base-name extension)))))
  1786. (defmacro org-export-with-current-buffer-copy (&rest body)
  1787. "Apply BODY in a copy of the current buffer.
  1788. The copy preserves local variables and visibility of the original
  1789. buffer.
  1790. Point is at buffer's beginning when BODY is applied."
  1791. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1792. `(let ((,original-buffer ,(current-buffer))
  1793. (,offset ,(1- (point-min)))
  1794. (,buffer-string ,(buffer-string))
  1795. (,overlays (mapcar
  1796. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1797. (with-temp-buffer
  1798. (let ((buffer-invisibility-spec nil))
  1799. (org-clone-local-variables
  1800. ,original-buffer
  1801. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  1802. (insert ,buffer-string)
  1803. (mapc (lambda (ov)
  1804. (move-overlay
  1805. ov
  1806. (- (overlay-start ov) ,offset)
  1807. (- (overlay-end ov) ,offset)
  1808. (current-buffer)))
  1809. ,overlays)
  1810. (goto-char (point-min))
  1811. (progn ,@body))))))
  1812. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1813. (defun org-export-expand-include-keyword (included)
  1814. "Expand every include keyword in buffer.
  1815. INCLUDED is a list of included file names along with their line
  1816. restriction, when appropriate. It is used to avoid infinite
  1817. recursion."
  1818. (let ((case-fold-search nil))
  1819. (goto-char (point-min))
  1820. (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
  1821. (when (eq (car (save-match-data (org-element-at-point))) 'keyword)
  1822. (beginning-of-line)
  1823. ;; Extract arguments from keyword's value.
  1824. (let* ((value (match-string 1))
  1825. (ind (org-get-indentation))
  1826. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1827. (prog1 (expand-file-name (match-string 1 value))
  1828. (setq value (replace-match "" nil nil value)))))
  1829. (lines
  1830. (and (string-match
  1831. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1832. (prog1 (match-string 1 value)
  1833. (setq value (replace-match "" nil nil value)))))
  1834. (env (cond ((string-match "\\<example\\>" value) 'example)
  1835. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1836. (match-string 1 value))))
  1837. ;; Minimal level of included file defaults to the child
  1838. ;; level of the current headline, if any, or one. It
  1839. ;; only applies is the file is meant to be included as
  1840. ;; an Org one.
  1841. (minlevel
  1842. (and (not env)
  1843. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  1844. (prog1 (string-to-number (match-string 1 value))
  1845. (setq value (replace-match "" nil nil value)))
  1846. (let ((cur (org-current-level)))
  1847. (if cur (1+ (org-reduced-level cur)) 1))))))
  1848. ;; Remove keyword.
  1849. (delete-region (point) (progn (forward-line) (point)))
  1850. (cond
  1851. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  1852. ;; Check if files has already been parsed. Look after
  1853. ;; inclusion lines too, as different parts of the same file
  1854. ;; can be included too.
  1855. ((member (list file lines) included)
  1856. (error "Recursive file inclusion: %s" file))
  1857. (t
  1858. (cond
  1859. ((eq env 'example)
  1860. (insert
  1861. (let ((ind-str (make-string ind ? ))
  1862. (contents
  1863. ;; Protect sensitive contents with commas.
  1864. (replace-regexp-in-string
  1865. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  1866. (org-export-prepare-file-contents file lines)
  1867. nil nil 1)))
  1868. (format "%s#+begin_example\n%s%s#+end_example\n"
  1869. ind-str contents ind-str))))
  1870. ((stringp env)
  1871. (insert
  1872. (let ((ind-str (make-string ind ? ))
  1873. (contents
  1874. ;; Protect sensitive contents with commas.
  1875. (replace-regexp-in-string
  1876. (if (string= env "org") "\\(^\\)\\(.\\)"
  1877. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  1878. (org-export-prepare-file-contents file lines)
  1879. nil nil 1)))
  1880. (format "%s#+begin_src %s\n%s%s#+end_src\n"
  1881. ind-str env contents ind-str))))
  1882. (t
  1883. (insert
  1884. (with-temp-buffer
  1885. (org-mode)
  1886. (insert
  1887. (org-export-prepare-file-contents file lines ind minlevel))
  1888. (org-export-expand-include-keyword
  1889. (cons (list file lines) included))
  1890. (buffer-string))))))))))))
  1891. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  1892. "Prepare the contents of FILE for inclusion and return them as a string.
  1893. When optional argument LINES is a string specifying a range of
  1894. lines, include only those lines.
  1895. Optional argument IND, when non-nil, is an integer specifying the
  1896. global indentation of returned contents. Since its purpose is to
  1897. allow an included file to stay in the same environment it was
  1898. created \(i.e. a list item), it doesn't apply past the first
  1899. headline encountered.
  1900. Optional argument MINLEVEL, when non-nil, is an integer
  1901. specifying the level that any top-level headline in the included
  1902. file should have."
  1903. (with-temp-buffer
  1904. (insert-file-contents file)
  1905. (when lines
  1906. (let* ((lines (split-string lines "-"))
  1907. (lbeg (string-to-number (car lines)))
  1908. (lend (string-to-number (cadr lines)))
  1909. (beg (if (zerop lbeg) (point-min)
  1910. (goto-char (point-min))
  1911. (forward-line (1- lbeg))
  1912. (point)))
  1913. (end (if (zerop lend) (point-max)
  1914. (goto-char (point-min))
  1915. (forward-line (1- lend))
  1916. (point))))
  1917. (narrow-to-region beg end)))
  1918. ;; Remove blank lines at beginning and end of contents. The logic
  1919. ;; behind that removal is that blank lines around include keyword
  1920. ;; override blank lines in included file.
  1921. (goto-char (point-min))
  1922. (org-skip-whitespace)
  1923. (beginning-of-line)
  1924. (delete-region (point-min) (point))
  1925. (goto-char (point-max))
  1926. (skip-chars-backward " \r\t\n")
  1927. (forward-line)
  1928. (delete-region (point) (point-max))
  1929. ;; If IND is set, preserve indentation of include keyword until
  1930. ;; the first headline encountered.
  1931. (when ind
  1932. (unless (eq major-mode 'org-mode) (org-mode))
  1933. (goto-char (point-min))
  1934. (let ((ind-str (make-string ind ? )))
  1935. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  1936. ;; Do not move footnote definitions out of column 0.
  1937. (unless (and (looking-at org-footnote-definition-re)
  1938. (eq (car (org-element-at-point)) 'footnote-definition))
  1939. (insert ind-str))
  1940. (forward-line))))
  1941. ;; When MINLEVEL is specified, compute minimal level for headlines
  1942. ;; in the file (CUR-MIN), and remove stars to each headline so
  1943. ;; that headlines with minimal level have a level of MINLEVEL.
  1944. (when minlevel
  1945. (unless (eq major-mode 'org-mode) (org-mode))
  1946. (let ((levels (org-map-entries
  1947. (lambda () (org-reduced-level (org-current-level))))))
  1948. (when levels
  1949. (let ((offset (- minlevel (apply 'min levels))))
  1950. (unless (zerop offset)
  1951. (when org-odd-levels-only (setq offset (* offset 2)))
  1952. ;; Only change stars, don't bother moving whole
  1953. ;; sections.
  1954. (org-map-entries
  1955. (lambda () (if (< offset 0) (delete-char (abs offset))
  1956. (insert (make-string offset ?*))))))))))
  1957. (buffer-string)))
  1958. ;;; Tools For Back-Ends
  1959. ;; A whole set of tools is available to help build new exporters. Any
  1960. ;; function general enough to have its use across many back-ends
  1961. ;; should be added here.
  1962. ;; As of now, functions operating on footnotes, headlines, links,
  1963. ;; macros, references, src-blocks, tables and tables of contents are
  1964. ;; implemented.
  1965. ;;;; For Footnotes
  1966. ;; `org-export-collect-footnote-definitions' is a tool to list
  1967. ;; actually used footnotes definitions in the whole parse tree, or in
  1968. ;; an headline, in order to add footnote listings throughout the
  1969. ;; transcoded data.
  1970. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  1971. ;; back-ends, when they need to attach the footnote definition only to
  1972. ;; the first occurrence of the corresponding label.
  1973. ;; `org-export-get-footnote-definition' and
  1974. ;; `org-export-get-footnote-number' provide easier access to
  1975. ;; additional information relative to a footnote reference.
  1976. (defun org-export-collect-footnote-definitions (data info)
  1977. "Return an alist between footnote numbers, labels and definitions.
  1978. DATA is the parse tree from which definitions are collected.
  1979. INFO is the plist used as a communication channel.
  1980. Definitions are sorted by order of references. They either
  1981. appear as Org data \(transcoded with `org-export-data'\) or as
  1982. a secondary string for inlined footnotes \(transcoded with
  1983. `org-export-secondary-string'\). Unreferenced definitions are
  1984. ignored."
  1985. (let (refs)
  1986. ;; Collect seen references in REFS.
  1987. (org-element-map
  1988. data 'footnote-reference
  1989. (lambda (footnote local)
  1990. (when (org-export-footnote-first-reference-p footnote local)
  1991. (list (org-export-get-footnote-number footnote local)
  1992. (org-element-get-property :label footnote)
  1993. (org-export-get-footnote-definition footnote local))))
  1994. info)))
  1995. (defun org-export-footnote-first-reference-p (footnote-reference info)
  1996. "Non-nil when a footnote reference is the first one for its label.
  1997. FOOTNOTE-REFERENCE is the footnote reference being considered.
  1998. INFO is the plist used as a communication channel."
  1999. (let ((label (org-element-get-property :label footnote-reference)))
  2000. (or (not label)
  2001. (equal
  2002. footnote-reference
  2003. (org-element-map
  2004. (plist-get info :parse-tree) 'footnote-reference
  2005. (lambda (footnote local)
  2006. (when (string= (org-element-get-property :label footnote) label)
  2007. footnote))
  2008. info 'first-match)))))
  2009. (defun org-export-get-footnote-definition (footnote-reference info)
  2010. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2011. INFO is the plist used as a communication channel."
  2012. (let ((label (org-element-get-property :label footnote-reference)))
  2013. (or (org-element-get-property :inline-definition footnote-reference)
  2014. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2015. (defun org-export-get-footnote-number (footnote info)
  2016. "Return number associated to a footnote.
  2017. FOOTNOTE is either a footnote reference or a footnote definition.
  2018. INFO is the plist used as a communication channel."
  2019. (let ((label (org-element-get-property :label footnote)) seen-refs)
  2020. (org-element-map
  2021. (plist-get info :parse-tree) 'footnote-reference
  2022. (lambda (fn local)
  2023. (let ((fn-lbl (org-element-get-property :label fn)))
  2024. (cond
  2025. ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
  2026. ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
  2027. ;; Anonymous footnote: it's always a new one. Also, be sure
  2028. ;; to return nil from the `cond' so `first-match' doesn't
  2029. ;; get us out of the loop.
  2030. ((not fn-lbl) (push 'inline seen-refs) nil)
  2031. ;; Label not seen so far: add it so SEEN-REFS. Again,
  2032. ;; return nil to stay in the loop.
  2033. ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
  2034. info 'first-match)))
  2035. ;;;; For Headlines
  2036. ;; `org-export-get-relative-level' is a shortcut to get headline
  2037. ;; level, relatively to the lower headline level in the parsed tree.
  2038. ;; `org-export-get-headline-number' returns the section number of an
  2039. ;; headline, while `org-export-number-to-roman' allows to convert it
  2040. ;; to roman numbers.
  2041. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2042. ;; `org-export-last-sibling-p' are three useful predicates when it
  2043. ;; comes to fulfill the `:headline-levels' property.
  2044. (defun org-export-get-relative-level (headline info)
  2045. "Return HEADLINE relative level within current parsed tree.
  2046. INFO is a plist holding contextual information."
  2047. (+ (org-element-get-property :level headline)
  2048. (or (plist-get info :headline-offset) 0)))
  2049. (defun org-export-low-level-p (headline info)
  2050. "Non-nil when HEADLINE is considered as low level.
  2051. INFO is a plist used as a communication channel.
  2052. A low level headlines has a relative level greater than
  2053. `:headline-levels' property value.
  2054. Return value is the difference between HEADLINE relative level
  2055. and the last level being considered as high enough, or nil."
  2056. (let ((limit (plist-get info :headline-levels)))
  2057. (when (wholenump limit)
  2058. (let ((level (org-export-get-relative-level headline info)))
  2059. (and (> level limit) (- level limit))))))
  2060. (defun org-export-get-headline-number (headline info)
  2061. "Return HEADLINE numbering as a list of numbers.
  2062. INFO is a plist holding contextual information."
  2063. (cdr (assoc headline (plist-get info :headline-numbering))))
  2064. (defun org-export-number-to-roman (n)
  2065. "Convert integer N into a roman numeral."
  2066. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2067. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2068. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2069. ( 1 . "I")))
  2070. (res ""))
  2071. (if (<= n 0)
  2072. (number-to-string n)
  2073. (while roman
  2074. (if (>= n (caar roman))
  2075. (setq n (- n (caar roman))
  2076. res (concat res (cdar roman)))
  2077. (pop roman)))
  2078. res)))
  2079. (defun org-export-first-sibling-p (headline info)
  2080. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2081. INFO is the plist used as a communication channel."
  2082. (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
  2083. (defun org-export-last-sibling-p (headline info)
  2084. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2085. INFO is the plist used as a communication channel."
  2086. (equal
  2087. (car (last (org-element-get-contents (car (plist-get info :genealogy)))))
  2088. headline))
  2089. ;;;; For Links
  2090. ;; `org-export-solidify-link-text' turns a string into a safer version
  2091. ;; for links, replacing most non-standard characters with hyphens.
  2092. ;; `org-export-get-coderef-format' returns an appropriate format
  2093. ;; string for coderefs.
  2094. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2095. ;; provided should be considered as an inline image.
  2096. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2097. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2098. ;; returns an appropriate unique identifier when found, or nil.
  2099. ;; `org-export-resolve-id-link' returns the first headline with
  2100. ;; specified id or custom-id in parse tree, or nil when none was
  2101. ;; found.
  2102. ;; `org-export-resolve-coderef' associates a reference to a line
  2103. ;; number in the element it belongs, or returns the reference itself
  2104. ;; when the element isn't numbered.
  2105. (defun org-export-solidify-link-text (s)
  2106. "Take link text S and make a safe target out of it."
  2107. (save-match-data
  2108. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_\\.-]+") "-")))
  2109. (defun org-export-get-coderef-format (path desc)
  2110. "Return format string for code reference link.
  2111. PATH is the link path. DESC is its description."
  2112. (save-match-data
  2113. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  2114. (replace-match "%s" t t desc))
  2115. ((string= desc "") "%s")
  2116. (t desc))))
  2117. (defun org-export-inline-image-p (link &optional extensions)
  2118. "Non-nil if LINK object points to an inline image.
  2119. When non-nil, optional argument EXTENSIONS is a list of valid
  2120. extensions for image files, as strings. Otherwise, a default
  2121. list is provided \(cf `org-image-file-name-regexp'\)."
  2122. (and (not (org-element-get-contents link))
  2123. (string= (org-element-get-property :type link) "file")
  2124. (org-file-image-p
  2125. (expand-file-name (org-element-get-property :path link))
  2126. extensions)))
  2127. (defun org-export-resolve-fuzzy-link (link info)
  2128. "Return LINK destination.
  2129. INFO is a plist holding contextual information.
  2130. Return value can be an object, an element, or nil:
  2131. - If LINK path exactly matches any target, return the target
  2132. object.
  2133. - If LINK path exactly matches any headline name, return that
  2134. element. If more than one headline share that name, priority
  2135. will be given to the one with the closest common ancestor, if
  2136. any, or the first one in the parse tree otherwise.
  2137. - Otherwise, return nil.
  2138. Assume LINK type is \"fuzzy\"."
  2139. (let ((path (org-element-get-property :path link)))
  2140. ;; Link points to a target: return it.
  2141. (or (loop for target in (plist-get info :target-list)
  2142. when (string= (org-element-get-property :raw-value target) path)
  2143. return target)
  2144. ;; Link either points to an headline or nothing. Try to find
  2145. ;; the source, with priority given to headlines with the closest
  2146. ;; common ancestor. If such candidate is found, return its
  2147. ;; beginning position as an unique identifier, otherwise return
  2148. ;; nil.
  2149. (let ((find-headline
  2150. (function
  2151. ;; Return first headline whose `:raw-value' property
  2152. ;; is NAME in parse tree DATA, or nil.
  2153. (lambda (name data)
  2154. (org-element-map
  2155. data 'headline
  2156. (lambda (headline local)
  2157. (when (string=
  2158. (org-element-get-property :raw-value headline)
  2159. name)
  2160. headline))
  2161. info 'first-match)))))
  2162. ;; Search among headlines sharing an ancestor with link,
  2163. ;; from closest to farthest.
  2164. (or (catch 'exit
  2165. (mapc
  2166. (lambda (parent)
  2167. (when (eq (car parent) 'headline)
  2168. (let ((foundp (funcall find-headline path parent)))
  2169. (when foundp (throw 'exit foundp)))))
  2170. (plist-get info :genealogy)) nil)
  2171. ;; No match with a common ancestor: try the full parse-tree.
  2172. (funcall find-headline path (plist-get info :parse-tree)))))))
  2173. (defun org-export-resolve-id-link (link info)
  2174. "Return headline referenced as LINK destination.
  2175. INFO is a plist used as a communication channel.
  2176. Return value can be an headline element or nil. Assume LINK type
  2177. is either \"id\" or \"custom-id\"."
  2178. (let ((id (org-element-get-property :path link)))
  2179. (org-element-map
  2180. (plist-get info :parse-tree) 'headline
  2181. (lambda (headline local)
  2182. (when (or (string= (org-element-get-property :id headline) id)
  2183. (string= (org-element-get-property :custom-id headline) id))
  2184. headline))
  2185. info 'first-match)))
  2186. (defun org-export-resolve-coderef (ref info)
  2187. "Resolve a code reference REF.
  2188. INFO is a plist used as a communication channel.
  2189. Return associated line number in source code, or REF itself,
  2190. depending on src-block or example element's switches."
  2191. (org-element-map
  2192. (plist-get info :parse-tree) '(src-block example)
  2193. (lambda (el local)
  2194. (let ((switches (or (org-element-get-property :switches el) "")))
  2195. (with-temp-buffer
  2196. (insert (org-trim (org-element-get-property :value el)))
  2197. ;; Build reference regexp.
  2198. (let* ((label
  2199. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2200. (match-string 1 switches))
  2201. org-coderef-label-format))
  2202. (ref-re
  2203. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2204. (replace-regexp-in-string "%s" ref label nil t))))
  2205. ;; Element containing REF is found. Only associate REF to
  2206. ;; a line number if element has "+n" or "-n" and "-k" or
  2207. ;; "-r" as switches. When it has "+n", count accumulated
  2208. ;; locs before, too.
  2209. (when (re-search-backward ref-re nil t)
  2210. (cond
  2211. ((not (string-match "-[kr]\\>" switches)) ref)
  2212. ((string-match "-n\\>" switches) (line-number-at-pos))
  2213. ((string-match "\\+n\\>" switches)
  2214. (+ (org-export-get-loc el local) (line-number-at-pos)))
  2215. (t ref)))))))
  2216. info 'first-match))
  2217. ;;;; For Macros
  2218. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2219. (defun org-export-expand-macro (macro info)
  2220. "Expand MACRO and return it as a string.
  2221. INFO is a plist holding export options."
  2222. (let* ((key (org-element-get-property :key macro))
  2223. (args (org-element-get-property :args macro))
  2224. ;; User's macros are stored in the communication channel with
  2225. ;; a ":macro-" prefix.
  2226. (value (plist-get info (intern (format ":macro-%s" key)))))
  2227. ;; Replace arguments in VALUE. A nil VALUE removes the macro call
  2228. ;; from export.
  2229. (when (stringp value)
  2230. (let ((s 0) n)
  2231. (while (string-match "\\$\\([0-9]+\\)" value s)
  2232. (setq s (1+ (match-beginning 0))
  2233. n (string-to-number (match-string 1 value)))
  2234. (and (>= (length args) n)
  2235. (setq value (replace-match (nth (1- n) args) t t value)))))
  2236. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2237. (when (string-match "\\`(eval\\>" value)
  2238. (setq value (eval (read value))))
  2239. ;; Return string.
  2240. (format "%s" (or value "")))))
  2241. ;;;; For References
  2242. ;; `org-export-get-ordinal' associates a sequence number to any object
  2243. ;; or element.
  2244. (defun org-export-get-ordinal
  2245. (element info &optional types within-section predicate)
  2246. "Return ordinal number of an element or object.
  2247. ELEMENT is the element or object considered. INFO is the plist
  2248. used as a communication channel.
  2249. Optional argument TYPES, when non-nil, is a list of element or
  2250. object types, as symbols, that should also be counted in.
  2251. Otherwise, only provided element's type is considered.
  2252. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2253. to the section containing ELEMENT.
  2254. Optional argument PREDICATE is a function returning a non-nil
  2255. value if the current element or object should be counted in. It
  2256. accepts one argument: the element or object being considered.
  2257. This argument allows to count only a certain type of objects,
  2258. like inline images, which are a subset of links \(in that case,
  2259. `org-export-inline-image-p' might be an useful predicate\)."
  2260. (let ((counter 0)
  2261. ;; Determine if search should apply to current section, in
  2262. ;; which case it should be retrieved first, or to full parse
  2263. ;; tree. As a special case, an element or object without
  2264. ;; a parent headline will also trigger a full search,
  2265. ;; notwithstanding WITHIN-SECTION value.
  2266. (data
  2267. (if (not within-section) (plist-get info :parse-tree)
  2268. (or (org-export-get-parent-headline element info)
  2269. (plist-get info :parse-tree)))))
  2270. ;; Increment counter until ELEMENT is found again.
  2271. (org-element-map
  2272. data (or types (car element))
  2273. (lambda (el local)
  2274. (cond
  2275. ((equal element el) (1+ counter))
  2276. ((not predicate) (incf counter) nil)
  2277. ((funcall predicate el) (incf counter) nil)))
  2278. info 'first-match)))
  2279. ;;;; For Src-Blocks
  2280. ;; `org-export-get-loc' counts number of code lines accumulated in
  2281. ;; src-block or example-block elements with a "+n" switch until
  2282. ;; a given element, excluded. Note: "-n" switches reset that count.
  2283. ;; `org-export-handle-code' takes care of line numbering and reference
  2284. ;; cleaning in source code, when appropriate.
  2285. (defun org-export-get-loc (element info)
  2286. "Return accumulated lines of code up to ELEMENT.
  2287. INFO is the plist used as a communication channel.
  2288. ELEMENT is excluded from count."
  2289. (let ((loc 0))
  2290. (org-element-map
  2291. (plist-get info :parse-tree) `(src-block example-block ,(car element))
  2292. (lambda (el local)
  2293. (cond
  2294. ;; ELEMENT is reached: Quit the loop.
  2295. ((equal el element) t)
  2296. ;; Only count lines from src-block and example-block elements
  2297. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2298. ((not (memq (car el) '(src-block example-block))) nil)
  2299. ((let ((switches (org-element-get-property :switches el)))
  2300. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2301. ;; Accumulate locs or reset them.
  2302. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2303. (lines (org-count-lines
  2304. (org-trim (org-element-get-property :value el)))))
  2305. (setq loc (if accumulatep lines (+ loc lines))))))
  2306. ;; Return nil to stay in the loop.
  2307. nil)))
  2308. info 'first-match)
  2309. ;; Return value.
  2310. loc))
  2311. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2312. "Handle line numbers and code references in ELEMENT.
  2313. ELEMENT has either a `src-block' an `example-block' type. INFO
  2314. is a plist used as a communication channel.
  2315. If optional argument NUM-FMT is a string, it will be used as
  2316. a format string for numbers at beginning of each line.
  2317. If optional argument REF-FMT is a string, it will be used as
  2318. a format string for each line of code containing a reference.
  2319. When optional argument DELAYED is non-nil, `org-loc' and
  2320. `org-coderef' properties, set to an adequate value, are applied
  2321. to, respectively, numbered lines and lines with a reference. No
  2322. line numbering is done and all references are stripped from the
  2323. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2324. in that situation.
  2325. Return new code as a string."
  2326. (let* ((switches (or (org-element-get-property :switches element) ""))
  2327. (code (org-element-get-property :value element))
  2328. (numberp (string-match "[-+]n\\>" switches))
  2329. (accumulatep (string-match "\\+n\\>" switches))
  2330. ;; Initialize loc counter when any kind of numbering is
  2331. ;; active.
  2332. (total-LOC (cond
  2333. (accumulatep (org-export-get-loc element info))
  2334. (numberp 0)))
  2335. ;; Get code and clean it. Remove blank lines at its
  2336. ;; beginning and end. Also remove protective commas.
  2337. (preserve-indent-p (or org-src-preserve-indentation
  2338. (string-match "-i\\>" switches)))
  2339. (replace-labels (when (string-match "-r\\>" switches)
  2340. (if (string-match "-k\\>" switches) 'keep t)))
  2341. (code (let ((c (replace-regexp-in-string
  2342. "\\`\\([ \t]*\n\\)+" ""
  2343. (replace-regexp-in-string
  2344. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2345. ;; If appropriate, remove global indentation.
  2346. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2347. ;; Free up the protected lines. Note: Org blocks
  2348. ;; have commas at the beginning or every line.
  2349. (if (string=
  2350. (or (org-element-get-property :language element) "")
  2351. "org")
  2352. (replace-regexp-in-string "^," "" c)
  2353. (replace-regexp-in-string
  2354. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2355. ;; Split code to process it line by line.
  2356. (code-lines (org-split-string code "\n"))
  2357. ;; If numbering is active, ensure line numbers will be
  2358. ;; correctly padded before applying the format string.
  2359. (num-fmt
  2360. (when (and (not delayed) numberp)
  2361. (format (if (stringp num-fmt) num-fmt "%s: ")
  2362. (format "%%%ds"
  2363. (length (number-to-string
  2364. (+ (length code-lines) total-LOC)))))))
  2365. ;; Get format used for references.
  2366. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2367. (match-string 1 switches))
  2368. org-coderef-label-format))
  2369. ;; Build a regexp matching a loc with a reference.
  2370. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2371. (replace-regexp-in-string
  2372. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2373. (org-element-normalize-string
  2374. (mapconcat
  2375. (lambda (loc)
  2376. ;; Maybe add line number to current line of code (LOC).
  2377. (when numberp
  2378. (incf total-LOC)
  2379. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2380. (concat (format num-fmt total-LOC) loc))))
  2381. ;; Take action if at a ref line.
  2382. (when (string-match with-ref-re loc)
  2383. (let ((ref (match-string 3 loc)))
  2384. (setq loc
  2385. ;; Option "-r" without "-k" removes labels.
  2386. ;; A non-nil DELAYED removes labels unconditionally.
  2387. (if (or delayed
  2388. (and replace-labels (not (eq replace-labels 'keep))))
  2389. (replace-match "" nil nil loc 1)
  2390. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2391. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2392. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2393. ;; If REF-FMT is defined, apply it to current LOC.
  2394. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2395. ;; Return updated LOC for concatenation.
  2396. loc)
  2397. code-lines "\n"))))
  2398. ;;;; For Tables
  2399. ;; `org-export-table-format-info' extracts formatting information
  2400. ;; (alignment, column groups and presence of a special column) from
  2401. ;; a raw table and returns it as a property list.
  2402. ;;
  2403. ;; `org-export-clean-table' cleans the raw table from any Org
  2404. ;; table-specific syntax.
  2405. (defun org-export-table-format-info (table)
  2406. "Extract info from TABLE.
  2407. Return a plist whose properties and values are:
  2408. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2409. `:column-groups' vector of symbols among `start', `end', `start-end',
  2410. `:row-groups' list of integers representing row groups.
  2411. `:special-column-p' non-nil if table has a special column.
  2412. `:width' vector of integers representing desired width of
  2413. current column, or nil."
  2414. (with-temp-buffer
  2415. (insert table)
  2416. (goto-char 1)
  2417. (org-table-align)
  2418. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2419. org-table-last-alignment)))
  2420. (width (make-vector (length org-table-last-alignment) nil))
  2421. (colgroups (make-vector (length org-table-last-alignment) nil))
  2422. (row-group 0)
  2423. (rowgroups)
  2424. (special-column-p 'empty))
  2425. (mapc (lambda (row)
  2426. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2427. (incf row-group)
  2428. ;; Determine if a special column is present by looking
  2429. ;; for special markers in the first column. More
  2430. ;; accurately, the first column is considered special
  2431. ;; if it only contains special markers and, maybe,
  2432. ;; empty cells.
  2433. (setq special-column-p
  2434. (cond
  2435. ((not special-column-p) nil)
  2436. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2437. 'special)
  2438. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2439. (cond
  2440. ;; Read forced alignment and width information, if any,
  2441. ;; and determine final alignment for the table.
  2442. ((org-table-cookie-line-p row)
  2443. (let ((col 0))
  2444. (mapc (lambda (field)
  2445. (when (string-match
  2446. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2447. (let ((align-data (match-string 1 field)))
  2448. (when align-data (aset align col align-data)))
  2449. (let ((w-data (match-string 2 field)))
  2450. (when w-data
  2451. (aset width col (string-to-number w-data)))))
  2452. (incf col))
  2453. (org-split-string row "[ \t]*|[ \t]*"))))
  2454. ;; Read column groups information.
  2455. ((org-table-colgroup-line-p row)
  2456. (let ((col 0))
  2457. (mapc (lambda (field)
  2458. (aset colgroups col
  2459. (cond ((string= "<" field) 'start)
  2460. ((string= ">" field) 'end)
  2461. ((string= "<>" field) 'start-end)))
  2462. (incf col))
  2463. (org-split-string row "[ \t]*|[ \t]*"))))
  2464. ;; Contents line.
  2465. (t (push row-group rowgroups))))
  2466. (org-split-string table "\n"))
  2467. ;; Return plist.
  2468. (list :alignment align
  2469. :column-groups colgroups
  2470. :row-groups (reverse rowgroups)
  2471. :special-column-p (eq special-column-p 'special)
  2472. :width width))))
  2473. (defun org-export-clean-table (table specialp)
  2474. "Clean string TABLE from its formatting elements.
  2475. Remove any row containing column groups or formatting cookies and
  2476. rows starting with a special marker. If SPECIALP is non-nil,
  2477. assume the table contains a special formatting column and remove
  2478. it also."
  2479. (let ((rows (org-split-string table "\n")))
  2480. (mapconcat 'identity
  2481. (delq nil
  2482. (mapcar
  2483. (lambda (row)
  2484. (cond
  2485. ((org-table-colgroup-line-p row) nil)
  2486. ((org-table-cookie-line-p row) nil)
  2487. ;; Ignore rows starting with a special marker.
  2488. ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
  2489. ;; Remove special column.
  2490. ((and specialp
  2491. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2492. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2493. (replace-match "\\1|" t nil row))
  2494. (t row)))
  2495. rows))
  2496. "\n")))
  2497. ;;;; For Tables Of Contents
  2498. ;; `org-export-collect-headlines' builds a list of all exportable
  2499. ;; headline elements, maybe limited to a certain depth. One can then
  2500. ;; easily parse it and transcode it.
  2501. ;; Building lists of tables, figures or listings is quite similar.
  2502. ;; Once the generic function `org-export-collect-elements' is defined,
  2503. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2504. ;; `org-export-collect-listings' can be derived from it.
  2505. (defun org-export-collect-headlines (info &optional n)
  2506. "Collect headlines in order to build a table of contents.
  2507. INFO is a plist used as a communication channel.
  2508. When non-nil, optional argument N must be an integer. It
  2509. specifies the depth of the table of contents.
  2510. Return a list of all exportable headlines as parsed elements."
  2511. (org-element-map
  2512. (plist-get info :parse-tree)
  2513. 'headline
  2514. (lambda (headline local)
  2515. ;; Strip contents from HEADLINE.
  2516. (let ((relative-level (org-export-get-relative-level headline local)))
  2517. (unless (and n (> relative-level n)) headline)))
  2518. info))
  2519. (defun org-export-collect-elements (type info &optional predicate)
  2520. "Collect referenceable elements of a determined type.
  2521. TYPE can be a symbol or a list of symbols specifying element
  2522. types to search. Only elements with a caption or a name are
  2523. collected.
  2524. INFO is a plist used as a communication channel.
  2525. When non-nil, optional argument PREDICATE is a function accepting
  2526. one argument, an element of type TYPE. It returns a non-nil
  2527. value when that element should be collected.
  2528. Return a list of all elements found, in order of appearance."
  2529. (org-element-map
  2530. (plist-get info :parse-tree) type
  2531. (lambda (element local)
  2532. (and (or (org-element-get-property :caption element)
  2533. (org-element-get-property :name element))
  2534. (or (not predicate) (funcall predicate element))
  2535. element)) info))
  2536. (defun org-export-collect-tables (info)
  2537. "Build a list of tables.
  2538. INFO is a plist used as a communication channel.
  2539. Return a list of table elements with a caption or a name
  2540. affiliated keyword."
  2541. (org-export-collect-elements 'table info))
  2542. (defun org-export-collect-figures (info predicate)
  2543. "Build a list of figures.
  2544. INFO is a plist used as a communication channel. PREDICATE is
  2545. a function which accepts one argument: a paragraph element and
  2546. whose return value is non-nil when that element should be
  2547. collected.
  2548. A figure is a paragraph type element, with a caption or a name,
  2549. verifying PREDICATE. The latter has to be provided since
  2550. a \"figure\" is a vague concept that may depend on back-end.
  2551. Return a list of elements recognized as figures."
  2552. (org-export-collect-elements 'paragraph info predicate))
  2553. (defun org-export-collect-listings (info)
  2554. "Build a list of src blocks.
  2555. INFO is a plist used as a communication channel.
  2556. Return a list of src-block elements with a caption or a name
  2557. affiliated keyword."
  2558. (org-export-collect-elements 'src-block info))
  2559. ;;;; Topology
  2560. (defun org-export-get-parent-headline (blob info)
  2561. "Return BLOB's closest parent headline or nil.
  2562. INFO is a plist used as a communication channel."
  2563. (catch 'exit
  2564. (mapc
  2565. (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
  2566. (plist-get info :genealogy))
  2567. nil))
  2568. (defun org-export-get-previous-element (blob info)
  2569. "Return previous element or object.
  2570. BLOB is an element or object. INFO is a plist used as
  2571. a communication channel.
  2572. Return previous element or object, a string, or nil."
  2573. (let ((parent (car (plist-get info :genealogy))))
  2574. (cadr (member blob (reverse (org-element-get-contents parent))))))
  2575. (defun org-export-get-next-element (blob info)
  2576. "Return next element or object.
  2577. BLOB is an element or object. INFO is a plist used as
  2578. a communication channel.
  2579. Return next element or object, a string, or nil."
  2580. (let ((parent (car (plist-get info :genealogy))))
  2581. (cadr (member blob (org-element-get-contents parent)))))
  2582. ;;; The Dispatcher
  2583. ;; `org-export-dispatch' is the standard interactive way to start an
  2584. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2585. ;; for its interface. Most commons back-ends should have an entry in
  2586. ;; it.
  2587. (defun org-export-dispatch ()
  2588. "Export dispatcher for Org mode.
  2589. It provides an access to common export related tasks in a buffer.
  2590. Its interface comes in two flavours: standard and expert. While
  2591. both share the same set of bindings, only the former displays the
  2592. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2593. to switch to one or the other.
  2594. Return an error if key pressed has no associated command."
  2595. (interactive)
  2596. (let* ((input (org-export-dispatch-ui
  2597. (if (listp org-export-initial-scope) org-export-initial-scope
  2598. (list org-export-initial-scope))
  2599. org-export-dispatch-use-expert-ui))
  2600. (raw-key (car input))
  2601. (scope (cdr input)))
  2602. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2603. ;; depending on user's key pressed.
  2604. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2605. ;; Export with `e-ascii' back-end.
  2606. ((?A ?N ?U)
  2607. (let ((outbuf
  2608. (org-export-to-buffer
  2609. 'e-ascii "*Org E-ASCII Export*"
  2610. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
  2611. `(:ascii-charset
  2612. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  2613. (with-current-buffer outbuf (text-mode))
  2614. (when org-export-show-temporary-export-buffer
  2615. (switch-to-buffer-other-window outbuf))))
  2616. ((?a ?n ?u)
  2617. (org-e-ascii-export-to-ascii
  2618. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)
  2619. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  2620. ;; Export with `e-latex' back-end.
  2621. (?L
  2622. (let ((outbuf
  2623. (org-export-to-buffer
  2624. 'e-latex "*Org E-LaTeX Export*"
  2625. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
  2626. (with-current-buffer outbuf (latex-mode))
  2627. (when org-export-show-temporary-export-buffer
  2628. (switch-to-buffer-other-window outbuf))))
  2629. (?l (org-e-latex-export-to-latex
  2630. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
  2631. (?p (org-e-latex-export-to-pdf
  2632. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope)))
  2633. (?d (org-open-file
  2634. (org-e-latex-export-to-pdf
  2635. (memq 'subtree scope) (memq 'visible scope) (memq 'body scope))))
  2636. ;; Undefined command.
  2637. (t (error "No command associated with key %s"
  2638. (char-to-string raw-key))))))
  2639. (defun org-export-dispatch-ui (scope expertp)
  2640. "Handle interface for `org-export-dispatch'.
  2641. SCOPE is a list containing current interactive options set for
  2642. export. It can contain any of the following symbols:
  2643. `body' toggles a body-only export
  2644. `subtree' restricts export to current subtree
  2645. `visible' restricts export to visible part of buffer.
  2646. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2647. buffer is provided, but indications about currently active
  2648. options are given in the prompt. Moreover, \[?] allows to switch
  2649. back to standard interface.
  2650. Return value is a list with key pressed as car and a list of
  2651. final interactive export options as cdr."
  2652. (let ((help (format "------------------- General Options --------------------
  2653. \[1] Body only: %s
  2654. \[2] Export scope: %s
  2655. \[3] Visible only: %s
  2656. -------------- ASCII/Latin-1/UTF-8 Export --------------
  2657. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  2658. --------------------- LaTeX Export ---------------------
  2659. \[l] to TEX file [L] to temporary buffer
  2660. \[p] to PDF file [d] ... and open it"
  2661. (if (memq 'body scope) "On" "Off")
  2662. (if (memq 'subtree scope) "Subtree" "Buffer")
  2663. (if (memq 'visible scope) "On" "Off")))
  2664. (standard-prompt "Export command: ")
  2665. (expert-prompt (format "Export command (%s%s%s): "
  2666. (if (memq 'body scope) "b" "-")
  2667. (if (memq 'subtree scope) "s" "-")
  2668. (if (memq 'visible scope) "v" "-")))
  2669. (handle-keypress
  2670. (function
  2671. ;; Read a character from command input, toggling interactive
  2672. ;; options when applicable. PROMPT is the displayed prompt,
  2673. ;; as a string.
  2674. (lambda (prompt)
  2675. (let ((key (read-char-exclusive prompt)))
  2676. (cond
  2677. ;; Ignore non-standard characters (i.e. "M-a").
  2678. ((not (characterp key)) (org-export-dispatch-ui scope expertp))
  2679. ;; Switch back to standard interface.
  2680. ((and (eq key ??) expertp) (org-export-dispatch-ui scope nil))
  2681. ((eq key ?1)
  2682. (org-export-dispatch-ui
  2683. (if (memq 'body scope) (remq 'body scope) (cons 'body scope))
  2684. expertp))
  2685. ((eq key ?2)
  2686. (org-export-dispatch-ui
  2687. (if (memq 'subtree scope) (remq 'subtree scope)
  2688. (cons 'subtree scope))
  2689. expertp))
  2690. ((eq key ?3)
  2691. (org-export-dispatch-ui
  2692. (if (memq 'visible scope) (remq 'visible scope)
  2693. (cons 'visible scope))
  2694. expertp))
  2695. (t (cons key scope))))))))
  2696. ;; With expert UI, just read key with a fancy prompt. In standard
  2697. ;; UI, display an intrusive help buffer.
  2698. (if expertp (funcall handle-keypress expert-prompt)
  2699. (save-window-excursion
  2700. (delete-other-windows)
  2701. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  2702. (org-fit-window-to-buffer
  2703. (get-buffer-window "*Org Export/Publishing Help*"))
  2704. (funcall handle-keypress standard-prompt)))))
  2705. (provide 'org-export)
  2706. ;;; org-export.el ends here