ox.el 214 KB

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