org-export.el 128 KB

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