org-export.el 135 KB

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