org-export.el 185 KB

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