org-export.el 169 KB

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