org-export.el 174 KB

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