ox.el 215 KB

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