ox.el 212 KB

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