org-export.el 208 KB

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