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