ox.el 218 KB

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