ox.el 213 KB

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