org-export.el 207 KB

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