org-export.el 151 KB

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