org-export.el 207 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495
  1. ;;; org-export.el --- Generic Export Engine For Org
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; This library implements a generic export engine for Org, built on
  18. ;; its syntactical parser: Org Elements.
  19. ;;
  20. ;; Besides that parser, the generic exporter is made of three distinct
  21. ;; parts:
  22. ;;
  23. ;; - The communication channel consists in a property list, which is
  24. ;; created and updated during the process. Its use is to offer
  25. ;; every piece of information, would it be about initial environment
  26. ;; or contextual data, all in a single place. The exhaustive list
  27. ;; of properties is given in "The Communication Channel" section of
  28. ;; this file.
  29. ;;
  30. ;; - The transcoder walks the parse tree, ignores or treat as plain
  31. ;; text elements and objects according to export options, and
  32. ;; eventually calls back-end specific functions to do the real
  33. ;; transcoding, concatenating their return value along the way.
  34. ;;
  35. ;; - The filter system is activated at the very beginning and the very
  36. ;; end of the export process, and each time an element or an object
  37. ;; has been converted. It is the entry point to fine-tune standard
  38. ;; output from back-end transcoders. See "The Filter System"
  39. ;; section for more information.
  40. ;;
  41. ;; The core function is `org-export-as'. It returns the transcoded
  42. ;; buffer as a string.
  43. ;;
  44. ;; An export back-end is defined with `org-export-define-backend',
  45. ;; which defines one mandatory information: his translation table.
  46. ;; Its value is an alist whose keys are elements and objects types and
  47. ;; values translator functions. See function's docstring for more
  48. ;; information about translators.
  49. ;;
  50. ;; Optionally, `org-export-define-backend' can also support specific
  51. ;; buffer keywords, OPTION keyword's items and filters. Also refer to
  52. ;; function documentation for more information.
  53. ;;
  54. ;; If the new back-end shares most properties with another one,
  55. ;; `org-export-define-derived-backend' can be used to simplify the
  56. ;; process.
  57. ;;
  58. ;; Any back-end can define its own variables. Among them, those
  59. ;; customizable should belong to the `org-export-BACKEND' group.
  60. ;;
  61. ;; Tools for common tasks across back-ends are implemented in the
  62. ;; following part of then file.
  63. ;;
  64. ;; Then, a wrapper macro for asynchronous export,
  65. ;; `org-export-async-start', along with tools to display results. are
  66. ;; given in the penultimate part.
  67. ;;
  68. ;; Eventually, a dispatcher (`org-export-dispatch') for standard
  69. ;; back-ends is provided in the last one.
  70. ;;; Code:
  71. (eval-when-compile (require 'cl))
  72. (require 'org-element)
  73. (require 'ob-exp)
  74. (declare-function org-e-publish "org-e-publish" (project &optional force async))
  75. (declare-function org-e-publish-all "org-e-publish" (&optional force async))
  76. (declare-function
  77. org-e-publish-current-file "org-e-publish" (&optional force async))
  78. (declare-function org-e-publish-current-project "org-e-publish"
  79. (&optional force async))
  80. (defvar org-e-publish-project-alist)
  81. (defvar org-table-number-fraction)
  82. (defvar org-table-number-regexp)
  83. ;;; Internal Variables
  84. ;;
  85. ;; Among internal variables, the most important is
  86. ;; `org-export-options-alist'. This variable define the global export
  87. ;; options, shared between every exporter, and how they are acquired.
  88. (defconst org-export-max-depth 19
  89. "Maximum nesting depth for headlines, counting from 0.")
  90. (defconst org-export-options-alist
  91. '((:author "AUTHOR" nil user-full-name t)
  92. (:creator "CREATOR" nil org-export-creator-string)
  93. (:date "DATE" nil nil t)
  94. (:description "DESCRIPTION" nil nil newline)
  95. (:email "EMAIL" nil user-mail-address t)
  96. (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
  97. (:headline-levels nil "H" org-export-headline-levels)
  98. (:keywords "KEYWORDS" nil nil space)
  99. (:language "LANGUAGE" nil org-export-default-language t)
  100. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  101. (:section-numbers nil "num" org-export-with-section-numbers)
  102. (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
  103. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  104. (:title "TITLE" nil nil space)
  105. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  106. (:with-author nil "author" org-export-with-author)
  107. (:with-clocks nil "c" org-export-with-clocks)
  108. (:with-creator nil "creator" org-export-with-creator)
  109. (:with-date nil "date" org-export-with-date)
  110. (:with-drawers nil "d" org-export-with-drawers)
  111. (:with-email nil "email" org-export-with-email)
  112. (:with-emphasize nil "*" org-export-with-emphasize)
  113. (:with-entities nil "e" org-export-with-entities)
  114. (:with-fixed-width nil ":" org-export-with-fixed-width)
  115. (:with-footnotes nil "f" org-export-with-footnotes)
  116. (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
  117. (:with-plannings nil "p" org-export-with-planning)
  118. (:with-priority nil "pri" org-export-with-priority)
  119. (:with-smart-quotes nil "'" org-export-with-smart-quotes)
  120. (:with-special-strings nil "-" org-export-with-special-strings)
  121. (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
  122. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  123. (:with-toc nil "toc" org-export-with-toc)
  124. (:with-tables nil "|" org-export-with-tables)
  125. (:with-tags nil "tags" org-export-with-tags)
  126. (:with-tasks nil "tasks" org-export-with-tasks)
  127. (:with-timestamps nil "<" org-export-with-timestamps)
  128. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  129. "Alist between export properties and ways to set them.
  130. The CAR of the alist is the property name, and the CDR is a list
  131. like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
  132. KEYWORD is a string representing a buffer keyword, or nil. Each
  133. property defined this way can also be set, during subtree
  134. export, through an headline property named after the keyword
  135. with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
  136. property).
  137. OPTION is a string that could be found in an #+OPTIONS: line.
  138. DEFAULT is the default value for the property.
  139. BEHAVIOUR determines how Org should handle multiple keywords for
  140. the same property. It is a symbol among:
  141. nil Keep old value and discard the new one.
  142. t Replace old value with the new one.
  143. `space' Concatenate the values, separating them with a space.
  144. `newline' Concatenate the values, separating them with
  145. a newline.
  146. `split' Split values at white spaces, and cons them to the
  147. previous list.
  148. Values set through KEYWORD and OPTION have precedence over
  149. DEFAULT.
  150. All these properties should be back-end agnostic. Back-end
  151. specific properties are set through `org-export-define-backend'.
  152. Properties redefined there have precedence over these.")
  153. (defconst org-export-special-keywords '("FILETAGS" "SETUP_FILE" "OPTIONS")
  154. "List of in-buffer keywords that require special treatment.
  155. These keywords are not directly associated to a property. The
  156. way they are handled must be hard-coded into
  157. `org-export--get-inbuffer-options' function.")
  158. (defconst org-export-filters-alist
  159. '((:filter-bold . org-export-filter-bold-functions)
  160. (:filter-babel-call . org-export-filter-babel-call-functions)
  161. (:filter-center-block . org-export-filter-center-block-functions)
  162. (:filter-clock . org-export-filter-clock-functions)
  163. (:filter-code . org-export-filter-code-functions)
  164. (:filter-comment . org-export-filter-comment-functions)
  165. (:filter-comment-block . org-export-filter-comment-block-functions)
  166. (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
  167. (:filter-drawer . org-export-filter-drawer-functions)
  168. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  169. (:filter-entity . org-export-filter-entity-functions)
  170. (:filter-example-block . org-export-filter-example-block-functions)
  171. (:filter-export-block . org-export-filter-export-block-functions)
  172. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  173. (:filter-final-output . org-export-filter-final-output-functions)
  174. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  175. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  176. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  177. (:filter-headline . org-export-filter-headline-functions)
  178. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  179. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  180. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  181. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  182. (:filter-italic . org-export-filter-italic-functions)
  183. (:filter-item . org-export-filter-item-functions)
  184. (:filter-keyword . org-export-filter-keyword-functions)
  185. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  186. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  187. (:filter-line-break . org-export-filter-line-break-functions)
  188. (:filter-link . org-export-filter-link-functions)
  189. (:filter-macro . org-export-filter-macro-functions)
  190. (:filter-node-property . org-export-filter-node-property-functions)
  191. (:filter-paragraph . org-export-filter-paragraph-functions)
  192. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  193. (:filter-plain-list . org-export-filter-plain-list-functions)
  194. (:filter-plain-text . org-export-filter-plain-text-functions)
  195. (:filter-planning . org-export-filter-planning-functions)
  196. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  197. (:filter-quote-block . org-export-filter-quote-block-functions)
  198. (:filter-quote-section . org-export-filter-quote-section-functions)
  199. (:filter-radio-target . org-export-filter-radio-target-functions)
  200. (:filter-section . org-export-filter-section-functions)
  201. (:filter-special-block . org-export-filter-special-block-functions)
  202. (:filter-src-block . org-export-filter-src-block-functions)
  203. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  204. (:filter-strike-through . org-export-filter-strike-through-functions)
  205. (:filter-subscript . org-export-filter-subscript-functions)
  206. (:filter-superscript . org-export-filter-superscript-functions)
  207. (:filter-table . org-export-filter-table-functions)
  208. (:filter-table-cell . org-export-filter-table-cell-functions)
  209. (:filter-table-row . org-export-filter-table-row-functions)
  210. (:filter-target . org-export-filter-target-functions)
  211. (:filter-timestamp . org-export-filter-timestamp-functions)
  212. (:filter-underline . org-export-filter-underline-functions)
  213. (:filter-verbatim . org-export-filter-verbatim-functions)
  214. (:filter-verse-block . org-export-filter-verse-block-functions))
  215. "Alist between filters properties and initial values.
  216. The key of each association is a property name accessible through
  217. the communication channel. Its value is a configurable global
  218. variable defining initial filters.
  219. This list is meant to install user specified filters. Back-end
  220. developers may install their own filters using
  221. `org-export-define-backend'. Filters defined there will always
  222. be prepended to the current list, so they always get applied
  223. first.")
  224. (defconst org-export-default-inline-image-rule
  225. `(("file" .
  226. ,(format "\\.%s\\'"
  227. (regexp-opt
  228. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  229. "xpm" "pbm" "pgm" "ppm") t))))
  230. "Default rule for link matching an inline image.
  231. This rule applies to links with no description. By default, it
  232. will be considered as an inline image if it targets a local file
  233. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  234. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  235. See `org-export-inline-image-p' for more information about
  236. rules.")
  237. (defvar org-export-async-debug nil
  238. "Non-nil means asynchronous export process should leave data behind.
  239. This data is found in the appropriate \"*Org Export Process*\"
  240. buffer, and in files prefixed with \"org-export-process\" and
  241. located in `temporary-file-directory'.
  242. When non-nil, it will also set `debug-on-error' to a non-nil
  243. value in the external process.")
  244. (defvar org-export-stack-contents nil
  245. "Record asynchronously generated export results and processes.
  246. This is an alist: its CAR is the source of the
  247. result (destination file or buffer for a finished process,
  248. original buffer for a running one) and its CDR is a list
  249. containing the back-end used, as a symbol, and either a process
  250. or the time at which it finished. It is used to build the menu
  251. from `org-export-stack'.")
  252. (defvar org-export-registered-backends nil
  253. "List of backends currently available in the exporter.
  254. A backend is stored as a list where CAR is its name, as a symbol,
  255. and CDR is a plist with the following properties:
  256. `:filters-alist', `:menu-entry', `:options-alist' and
  257. `:translate-alist'.
  258. This variable is set with `org-export-define-backend' and
  259. `org-export-define-derived-backend' functions.")
  260. (defvar org-export-dispatch-last-action nil
  261. "Last command called from the dispatcher.
  262. The value should be a list. Its CAR is the action, as a symbol,
  263. and its CDR is a list of export options.")
  264. ;;; User-configurable Variables
  265. ;;
  266. ;; Configuration for the masses.
  267. ;;
  268. ;; They should never be accessed directly, as their value is to be
  269. ;; stored in a property list (cf. `org-export-options-alist').
  270. ;; Back-ends will read their value from there instead.
  271. (defgroup org-export nil
  272. "Options for exporting Org mode files."
  273. :tag "Org Export"
  274. :group 'org)
  275. (defgroup org-export-general nil
  276. "General options for export engine."
  277. :tag "Org Export General"
  278. :group 'org-export)
  279. (defcustom org-export-with-archived-trees 'headline
  280. "Whether sub-trees with the ARCHIVE tag should be exported.
  281. This can have three different values:
  282. nil Do not export, pretend this tree is not present.
  283. t Do export the entire tree.
  284. `headline' Only export the headline, but skip the tree below it.
  285. This option can also be set with the #+OPTIONS line,
  286. e.g. \"arch:nil\"."
  287. :group 'org-export-general
  288. :type '(choice
  289. (const :tag "Not at all" nil)
  290. (const :tag "Headline only" 'headline)
  291. (const :tag "Entirely" t)))
  292. (defcustom org-export-with-author t
  293. "Non-nil means insert author name into the exported file.
  294. This option can also be set with the #+OPTIONS line,
  295. e.g. \"author:nil\"."
  296. :group 'org-export-general
  297. :type 'boolean)
  298. (defcustom org-export-with-clocks nil
  299. "Non-nil means export CLOCK keywords.
  300. This option can also be set with the #+OPTIONS line,
  301. e.g. \"c:t\"."
  302. :group 'org-export-general
  303. :type 'boolean)
  304. (defcustom org-export-with-creator 'comment
  305. "Non-nil means the postamble should contain a creator sentence.
  306. The sentence can be set in `org-export-creator-string' and
  307. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  308. If the value is `comment' insert it as a comment."
  309. :group 'org-export-general
  310. :type '(choice
  311. (const :tag "No creator sentence" nil)
  312. (const :tag "Sentence as a comment" 'comment)
  313. (const :tag "Insert the sentence" t)))
  314. (defcustom org-export-with-date t
  315. "Non-nil means insert date in the exported document.
  316. This options can also be set with the OPTIONS keyword,
  317. e.g. \"date:nil\".")
  318. (defcustom org-export-creator-string
  319. (format "Generated by Org mode %s in Emacs %s."
  320. (if (fboundp 'org-version) (org-version) "(Unknown)")
  321. emacs-version)
  322. "String to insert at the end of the generated document."
  323. :group 'org-export-general
  324. :type '(string :tag "Creator string"))
  325. (defcustom org-export-with-drawers t
  326. "Non-nil means export contents of standard drawers.
  327. When t, all drawers are exported. This may also be a list of
  328. drawer names to export. This variable doesn't apply to
  329. properties drawers.
  330. This option can also be set with the #+OPTIONS line,
  331. e.g. \"d:nil\"."
  332. :group 'org-export-general
  333. :type '(choice
  334. (const :tag "All drawers" t)
  335. (const :tag "None" nil)
  336. (repeat :tag "Selected drawers"
  337. (string :tag "Drawer name"))))
  338. (defcustom org-export-with-email nil
  339. "Non-nil means insert author email into the exported file.
  340. This option can also be set with the #+OPTIONS line,
  341. e.g. \"email:t\"."
  342. :group 'org-export-general
  343. :type 'boolean)
  344. (defcustom org-export-with-emphasize t
  345. "Non-nil means interpret *word*, /word/, _word_ and +word+.
  346. If the export target supports emphasizing text, the word will be
  347. typeset in bold, italic, with an underline or strike-through,
  348. respectively.
  349. This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
  350. :group 'org-export-general
  351. :type 'boolean)
  352. (defcustom org-export-exclude-tags '("noexport")
  353. "Tags that exclude a tree from export.
  354. All trees carrying any of these tags will be excluded from
  355. export. This is without condition, so even subtrees inside that
  356. carry one of the `org-export-select-tags' will be removed.
  357. This option can also be set with the #+EXCLUDE_TAGS: keyword."
  358. :group 'org-export-general
  359. :type '(repeat (string :tag "Tag")))
  360. (defcustom org-export-with-fixed-width t
  361. "Non-nil means lines starting with \":\" will be in fixed width font.
  362. This can be used to have pre-formatted text, fragments of code
  363. etc. For example:
  364. : ;; Some Lisp examples
  365. : (while (defc cnt)
  366. : (ding))
  367. will be looking just like this in also HTML. See also the QUOTE
  368. keyword. Not all export backends support this.
  369. This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
  370. :group 'org-export-translation
  371. :type 'boolean)
  372. (defcustom org-export-with-footnotes t
  373. "Non-nil means Org footnotes should be exported.
  374. This option can also be set with the #+OPTIONS line,
  375. e.g. \"f:nil\"."
  376. :group 'org-export-general
  377. :type 'boolean)
  378. (defcustom org-export-headline-levels 3
  379. "The last level which is still exported as a headline.
  380. Inferior levels will produce itemize lists when exported.
  381. This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
  382. :group 'org-export-general
  383. :type 'integer)
  384. (defcustom org-export-default-language "en"
  385. "The default language for export and clocktable translations, as a string.
  386. This may have an association in
  387. `org-clock-clocktable-language-setup'."
  388. :group 'org-export-general
  389. :type '(string :tag "Language"))
  390. (defcustom org-export-preserve-breaks nil
  391. "Non-nil means preserve all line breaks when exporting.
  392. Normally, in HTML output paragraphs will be reformatted.
  393. This option can also be set with the #+OPTIONS line,
  394. e.g. \"\\n:t\"."
  395. :group 'org-export-general
  396. :type 'boolean)
  397. (defcustom org-export-with-entities t
  398. "Non-nil means interpret entities when exporting.
  399. For example, HTML export converts \\alpha to &alpha; and \\AA to
  400. &Aring;.
  401. For a list of supported names, see the constant `org-entities'
  402. and the user option `org-entities-user'.
  403. This option can also be set with the #+OPTIONS line,
  404. e.g. \"e:nil\"."
  405. :group 'org-export-general
  406. :type 'boolean)
  407. (defcustom org-export-with-inlinetasks t
  408. "Non-nil means inlinetasks should be exported.
  409. This option can also be set with the #+OPTIONS line,
  410. e.g. \"inline:nil\"."
  411. :group 'org-export-general
  412. :type 'boolean)
  413. (defcustom org-export-with-planning nil
  414. "Non-nil means include planning info in export.
  415. This option can also be set with the #+OPTIONS: line,
  416. e.g. \"p:t\"."
  417. :group 'org-export-general
  418. :type 'boolean)
  419. (defcustom org-export-with-priority nil
  420. "Non-nil means include priority cookies in export.
  421. This option can also be set with the #+OPTIONS line,
  422. e.g. \"pri:t\"."
  423. :group 'org-export-general
  424. :type 'boolean)
  425. (defcustom org-export-with-section-numbers t
  426. "Non-nil means add section numbers to headlines when exporting.
  427. When set to an integer n, numbering will only happen for
  428. headlines whose relative level is higher or equal to n.
  429. This option can also be set with the #+OPTIONS line,
  430. e.g. \"num:t\"."
  431. :group 'org-export-general
  432. :type 'boolean)
  433. (defcustom org-export-select-tags '("export")
  434. "Tags that select a tree for export.
  435. If any such tag is found in a buffer, all trees that do not carry
  436. one of these tags will be ignored during export. Inside trees
  437. that are selected like this, you can still deselect a subtree by
  438. tagging it with one of the `org-export-exclude-tags'.
  439. This option can also be set with the #+SELECT_TAGS: keyword."
  440. :group 'org-export-general
  441. :type '(repeat (string :tag "Tag")))
  442. (defcustom org-export-with-smart-quotes nil
  443. "Non-nil means activate smart quotes during export.
  444. This option can also be set with the #+OPTIONS: line,
  445. e.g. \"':t\"."
  446. :group 'org-export-general
  447. :type 'boolean)
  448. (defcustom org-export-with-special-strings t
  449. "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
  450. When this option is turned on, these strings will be exported as:
  451. Org HTML LaTeX UTF-8
  452. -----+----------+--------+-------
  453. \\- &shy; \\-
  454. -- &ndash; -- –
  455. --- &mdash; --- —
  456. ... &hellip; \\ldots …
  457. This option can also be set with the #+OPTIONS line,
  458. e.g. \"-:nil\"."
  459. :group 'org-export-general
  460. :type 'boolean)
  461. (defcustom org-export-with-statistics-cookies t
  462. "Non-nil means include statistics cookies in export.
  463. This option can also be set with the #+OPTIONS: line,
  464. e.g. \"stat:nil\""
  465. :group 'org-export-general
  466. :type 'boolean)
  467. (defcustom org-export-with-sub-superscripts t
  468. "Non-nil means interpret \"_\" and \"^\" for export.
  469. When this option is turned on, you can use TeX-like syntax for
  470. sub- and superscripts. Several characters after \"_\" or \"^\"
  471. will be considered as a single item - so grouping with {} is
  472. normally not needed. For example, the following things will be
  473. parsed as single sub- or superscripts.
  474. 10^24 or 10^tau several digits will be considered 1 item.
  475. 10^-12 or 10^-tau a leading sign with digits or a word
  476. x^2-y^3 will be read as x^2 - y^3, because items are
  477. terminated by almost any nonword/nondigit char.
  478. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  479. Still, ambiguity is possible - so when in doubt use {} to enclose
  480. the sub/superscript. If you set this variable to the symbol
  481. `{}', the braces are *required* in order to trigger
  482. interpretations as sub/superscript. This can be helpful in
  483. documents that need \"_\" frequently in plain text.
  484. This option can also be set with the #+OPTIONS line,
  485. e.g. \"^:nil\"."
  486. :group 'org-export-general
  487. :type '(choice
  488. (const :tag "Interpret them" t)
  489. (const :tag "Curly brackets only" {})
  490. (const :tag "Do not interpret them" nil)))
  491. (defcustom org-export-with-toc t
  492. "Non-nil means create a table of contents in exported files.
  493. The TOC contains headlines with levels up
  494. to`org-export-headline-levels'. When an integer, include levels
  495. up to N in the toc, this may then be different from
  496. `org-export-headline-levels', but it will not be allowed to be
  497. larger than the number of headline levels. When nil, no table of
  498. contents is made.
  499. This option can also be set with the #+OPTIONS line,
  500. e.g. \"toc:nil\" or \"toc:3\"."
  501. :group 'org-export-general
  502. :type '(choice
  503. (const :tag "No Table of Contents" nil)
  504. (const :tag "Full Table of Contents" t)
  505. (integer :tag "TOC to level")))
  506. (defcustom org-export-with-tables t
  507. "If non-nil, lines starting with \"|\" define a table.
  508. For example:
  509. | Name | Address | Birthday |
  510. |-------------+----------+-----------|
  511. | Arthur Dent | England | 29.2.2100 |
  512. This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
  513. :group 'org-export-general
  514. :type 'boolean)
  515. (defcustom org-export-with-tags t
  516. "If nil, do not export tags, just remove them from headlines.
  517. If this is the symbol `not-in-toc', tags will be removed from
  518. table of contents entries, but still be shown in the headlines of
  519. the document.
  520. This option can also be set with the #+OPTIONS line,
  521. e.g. \"tags:nil\"."
  522. :group 'org-export-general
  523. :type '(choice
  524. (const :tag "Off" nil)
  525. (const :tag "Not in TOC" not-in-toc)
  526. (const :tag "On" t)))
  527. (defcustom org-export-with-tasks t
  528. "Non-nil means include TODO items for export.
  529. This may have the following values:
  530. t include tasks independent of state.
  531. `todo' include only tasks that are not yet done.
  532. `done' include only tasks that are already done.
  533. nil ignore all tasks.
  534. list of keywords include tasks with these keywords.
  535. This option can also be set with the #+OPTIONS line,
  536. e.g. \"tasks:nil\"."
  537. :group 'org-export-general
  538. :type '(choice
  539. (const :tag "All tasks" t)
  540. (const :tag "No tasks" nil)
  541. (const :tag "Not-done tasks" todo)
  542. (const :tag "Only done tasks" done)
  543. (repeat :tag "Specific TODO keywords"
  544. (string :tag "Keyword"))))
  545. (defcustom org-export-time-stamp-file t
  546. "Non-nil means insert a time stamp into the exported file.
  547. The time stamp shows when the file was created.
  548. This option can also be set with the #+OPTIONS line,
  549. e.g. \"timestamp:nil\"."
  550. :group 'org-export-general
  551. :type 'boolean)
  552. (defcustom org-export-with-timestamps t
  553. "Non nil means allow timestamps in export.
  554. It can be set to `active', `inactive', t or nil, in order to
  555. export, respectively, only active timestamps, only inactive ones,
  556. all of them or none.
  557. This option can also be set with the #+OPTIONS line, e.g.
  558. \"<:nil\"."
  559. :group 'org-export-general
  560. :type '(choice
  561. (const :tag "All timestamps" t)
  562. (const :tag "Only active timestamps" active)
  563. (const :tag "Only inactive timestamps" inactive)
  564. (const :tag "No timestamp" nil)))
  565. (defcustom org-export-with-todo-keywords t
  566. "Non-nil means include TODO keywords in export.
  567. When nil, remove all these keywords from the export."
  568. :group 'org-export-general
  569. :type 'boolean)
  570. (defcustom org-export-allow-BIND 'confirm
  571. "Non-nil means allow #+BIND to define local variable values for export.
  572. This is a potential security risk, which is why the user must
  573. confirm the use of these lines."
  574. :group 'org-export-general
  575. :type '(choice
  576. (const :tag "Never" nil)
  577. (const :tag "Always" t)
  578. (const :tag "Ask a confirmation for each file" confirm)))
  579. (defcustom org-export-snippet-translation-alist nil
  580. "Alist between export snippets back-ends and exporter back-ends.
  581. This variable allows to provide shortcuts for export snippets.
  582. For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
  583. HTML back-end will recognize the contents of \"@@h:<b>@@\" as
  584. HTML code while every other back-end will ignore it."
  585. :group 'org-export-general
  586. :type '(repeat
  587. (cons
  588. (string :tag "Shortcut")
  589. (string :tag "Back-end"))))
  590. (defcustom org-export-coding-system nil
  591. "Coding system for the exported file."
  592. :group 'org-export-general
  593. :type 'coding-system)
  594. (defcustom org-export-copy-to-kill-ring t
  595. "Non-nil means exported stuff will also be pushed onto the kill ring."
  596. :group 'org-export-general
  597. :type 'boolean)
  598. (defcustom org-export-initial-scope 'buffer
  599. "The initial scope when exporting with `org-export-dispatch'.
  600. This variable can be either set to `buffer' or `subtree'."
  601. :group 'org-export-general
  602. :type '(choice
  603. (const :tag "Export current buffer" 'buffer)
  604. (const :tag "Export current subtree" 'subtree)))
  605. (defcustom org-export-show-temporary-export-buffer t
  606. "Non-nil means show buffer after exporting to temp buffer.
  607. When Org exports to a file, the buffer visiting that file is ever
  608. shown, but remains buried. However, when exporting to
  609. a temporary buffer, that buffer is popped up in a second window.
  610. When this variable is nil, the buffer remains buried also in
  611. these cases."
  612. :group 'org-export-general
  613. :type 'boolean)
  614. (defcustom org-export-in-background nil
  615. "Non-nil means export and publishing commands will run in background.
  616. Results from an asynchronous export are never displayed. You can
  617. retrieve them with \\[org-export-stack]."
  618. :group 'org-export-general
  619. :type 'boolean)
  620. (defcustom org-export-async-init-file user-init-file
  621. "File used to initialize external export process.
  622. Value must be an absolute file name. It defaults to user's
  623. initialization file. Though, a specific configuration makes the
  624. process faster and the export more portable."
  625. :group 'org-export-general
  626. :type '(file :must-match t))
  627. (defcustom org-export-dispatch-use-expert-ui nil
  628. "Non-nil means using a non-intrusive `org-export-dispatch'.
  629. In that case, no help buffer is displayed. Though, an indicator
  630. for current export scope is added to the prompt (\"b\" when
  631. output is restricted to body only, \"s\" when it is restricted to
  632. the current subtree, \"v\" when only visible elements are
  633. considered for export and \"f\" when publishing functions should
  634. be passed the FORCE argument). Also, \[?] allows to switch back
  635. to standard mode."
  636. :group 'org-export-general
  637. :type 'boolean)
  638. ;;; Defining Back-ends
  639. ;;
  640. ;; `org-export-define-backend' is the standard way to define an export
  641. ;; back-end. It allows to specify translators, filters, buffer
  642. ;; options and a menu entry. If the new back-end shares translators
  643. ;; with another back-end, `org-export-define-derived-backend' may be
  644. ;; used instead.
  645. ;;
  646. ;; Internally, a back-end is stored as a list, of which CAR is the
  647. ;; name of the back-end, as a symbol, and CDR a plist. Accessors to
  648. ;; properties of a given back-end are: `org-export-backend-filters',
  649. ;; `org-export-backend-menu', `org-export-backend-options' and
  650. ;; `org-export-backend-translate-table'.
  651. ;;
  652. ;; Eventually `org-export-barf-if-invalid-backend' returns an error
  653. ;; when a given back-end hasn't been registered yet.
  654. (defmacro org-export-define-backend (backend translators &rest body)
  655. "Define a new back-end BACKEND.
  656. TRANSLATORS is an alist between object or element types and
  657. functions handling them.
  658. These functions should return a string without any trailing
  659. space, or nil. They must accept three arguments: the object or
  660. element itself, its contents or nil when it isn't recursive and
  661. the property list used as a communication channel.
  662. Contents, when not nil, are stripped from any global indentation
  663. \(although the relative one is preserved). They also always end
  664. with a single newline character.
  665. If, for a given type, no function is found, that element or
  666. object type will simply be ignored, along with any blank line or
  667. white space at its end. The same will happen if the function
  668. returns the nil value. If that function returns the empty
  669. string, the type will be ignored, but the blank lines or white
  670. spaces will be kept.
  671. In addition to element and object types, one function can be
  672. associated to the `template' symbol and another one to the
  673. `plain-text' symbol.
  674. The former returns the final transcoded string, and can be used
  675. to add a preamble and a postamble to document's body. It must
  676. accept two arguments: the transcoded string and the property list
  677. containing export options.
  678. The latter, when defined, is to be called on every text not
  679. recognized as an element or an object. It must accept two
  680. arguments: the text string and the information channel. It is an
  681. appropriate place to protect special chars relative to the
  682. back-end.
  683. BODY can start with pre-defined keyword arguments. The following
  684. keywords are understood:
  685. :export-block
  686. String, or list of strings, representing block names that
  687. will not be parsed. This is used to specify blocks that will
  688. contain raw code specific to the back-end. These blocks
  689. still have to be handled by the relative `export-block' type
  690. translator.
  691. :filters-alist
  692. Alist between filters and function, or list of functions,
  693. specific to the back-end. See `org-export-filters-alist' for
  694. a list of all allowed filters. Filters defined here
  695. shouldn't make a back-end test, as it may prevent back-ends
  696. derived from this one to behave properly.
  697. :menu-entry
  698. Menu entry for the export dispatcher. It should be a list
  699. like:
  700. \(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
  701. where :
  702. KEY is a free character selecting the back-end.
  703. DESCRIPTION-OR-ORDINAL is either a string or a number.
  704. If it is a string, is will be used to name the back-end in
  705. its menu entry. If it is a number, the following menu will
  706. be displayed as a sub-menu of the back-end with the same
  707. KEY. Also, the number will be used to determine in which
  708. order such sub-menus will appear (lowest first).
  709. ACTION-OR-MENU is either a function or an alist.
  710. If it is an action, it will be called with four
  711. arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
  712. BODY-ONLY. See `org-export-as' for further explanations on
  713. some of them.
  714. If it is an alist, associations should follow the
  715. pattern:
  716. \(KEY DESCRIPTION ACTION)
  717. where KEY, DESCRIPTION and ACTION are described above.
  718. Valid values include:
  719. \(?m \"My Special Back-end\" my-special-export-function)
  720. or
  721. \(?l \"Export to LaTeX\"
  722. \(?p \"As PDF file\" org-e-latex-export-to-pdf)
  723. \(?o \"As PDF file and open\"
  724. \(lambda (a s v b)
  725. \(if a (org-e-latex-export-to-pdf t s v b)
  726. \(org-open-file
  727. \(org-e-latex-export-to-pdf nil s v b)))))))
  728. or the following, which will be added to the previous
  729. sub-menu,
  730. \(?l 1
  731. \((?B \"As TEX buffer (Beamer)\" org-e-beamer-export-as-latex)
  732. \(?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)))
  733. :options-alist
  734. Alist between back-end specific properties introduced in
  735. communication channel and how their value are acquired. See
  736. `org-export-options-alist' for more information about
  737. structure of the values."
  738. (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
  739. (indent 1))
  740. (let (export-block filters menu-entry options contents)
  741. (while (keywordp (car body))
  742. (case (pop body)
  743. (:export-block (let ((names (pop body)))
  744. (setq export-block
  745. (if (consp names) (mapcar 'upcase names)
  746. (list (upcase names))))))
  747. (:filters-alist (setq filters (pop body)))
  748. (:menu-entry (setq menu-entry (pop body)))
  749. (:options-alist (setq options (pop body)))
  750. (t (pop body))))
  751. (setq contents (append (list :translate-alist translators)
  752. (and filters (list :filters-alist filters))
  753. (and options (list :options-alist options))
  754. (and menu-entry (list :menu-entry menu-entry))))
  755. `(progn
  756. ;; Register back-end.
  757. (let ((registeredp (assq ',backend org-export-registered-backends)))
  758. (if registeredp (setcdr registeredp ',contents)
  759. (push (cons ',backend ',contents) org-export-registered-backends)))
  760. ;; Tell parser to not parse EXPORT-BLOCK blocks.
  761. ,(when export-block
  762. `(mapc
  763. (lambda (name)
  764. (add-to-list 'org-element-block-name-alist
  765. `(,name . org-element-export-block-parser)))
  766. ',export-block))
  767. ;; Splice in the body, if any.
  768. ,@body)))
  769. (defmacro org-export-define-derived-backend (child parent &rest body)
  770. "Create a new back-end as a variant of an existing one.
  771. CHILD is the name of the derived back-end. PARENT is the name of
  772. the parent back-end.
  773. BODY can start with pre-defined keyword arguments. The following
  774. keywords are understood:
  775. :export-block
  776. String, or list of strings, representing block names that
  777. will not be parsed. This is used to specify blocks that will
  778. contain raw code specific to the back-end. These blocks
  779. still have to be handled by the relative `export-block' type
  780. translator.
  781. :filters-alist
  782. Alist of filters that will overwrite or complete filters
  783. defined in PARENT back-end. See `org-export-filters-alist'
  784. for a list of allowed filters.
  785. :menu-entry
  786. Menu entry for the export dispatcher. See
  787. `org-export-define-backend' for more information about the
  788. expected value.
  789. :options-alist
  790. Alist of back-end specific properties that will overwrite or
  791. complete those defined in PARENT back-end. Refer to
  792. `org-export-options-alist' for more information about
  793. structure of the values.
  794. :translate-alist
  795. Alist of element and object types and transcoders that will
  796. overwrite or complete transcode table from PARENT back-end.
  797. Refer to `org-export-define-backend' for detailed information
  798. about transcoders.
  799. As an example, here is how one could define \"my-latex\" back-end
  800. as a variant of `e-latex' back-end with a custom template
  801. function:
  802. \(org-export-define-derived-backend my-latex e-latex
  803. :translate-alist ((template . my-latex-template-fun)))
  804. The back-end could then be called with, for example:
  805. \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
  806. (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
  807. (indent 2))
  808. (org-export-barf-if-invalid-backend parent)
  809. (let (export-block filters menu-entry options translators contents)
  810. (while (keywordp (car body))
  811. (case (pop body)
  812. (:export-block (let ((names (pop body)))
  813. (setq export-block
  814. (if (consp names) (mapcar 'upcase names)
  815. (list (upcase names))))))
  816. (:filters-alist (setq filters (pop body)))
  817. (:menu-entry (setq menu-entry (pop body)))
  818. (:options-alist (setq options (pop body)))
  819. (:translate-alist (setq translators (pop body)))
  820. (t (pop body))))
  821. (setq contents (append
  822. (list :parent parent)
  823. (let ((p-table (org-export-backend-translate-table parent)))
  824. (list :translate-alist (append translators p-table)))
  825. (let ((p-filters (org-export-backend-filters parent)))
  826. (list :filters-alist (append filters p-filters)))
  827. (let ((p-options (org-export-backend-options parent)))
  828. (list :options-alist (append options p-options)))
  829. (and menu-entry (list :menu-entry menu-entry))))
  830. `(progn
  831. ;; Register back-end.
  832. (let ((registeredp (assq ',child org-export-registered-backends)))
  833. (if registeredp (setcdr registeredp ',contents)
  834. (push (cons ',child ',contents) org-export-registered-backends)))
  835. ;; Tell parser to not parse EXPORT-BLOCK blocks.
  836. ,(when export-block
  837. `(mapc
  838. (lambda (name)
  839. (add-to-list 'org-element-block-name-alist
  840. `(,name . org-element-export-block-parser)))
  841. ',export-block))
  842. ;; Splice in the body, if any.
  843. ,@body)))
  844. (defun org-export-backend-filters (backend)
  845. "Return filters for BACKEND."
  846. (plist-get (cdr (assq backend org-export-registered-backends))
  847. :filters-alist))
  848. (defun org-export-backend-menu (backend)
  849. "Return menu entry for BACKEND."
  850. (plist-get (cdr (assq backend org-export-registered-backends))
  851. :menu-entry))
  852. (defun org-export-backend-options (backend)
  853. "Return export options for BACKEND."
  854. (plist-get (cdr (assq backend org-export-registered-backends))
  855. :options-alist))
  856. (defun org-export-backend-translate-table (backend)
  857. "Return translate table for BACKEND."
  858. (plist-get (cdr (assq backend org-export-registered-backends))
  859. :translate-alist))
  860. (defun org-export-barf-if-invalid-backend (backend)
  861. "Signal an error if BACKEND isn't defined."
  862. (unless (org-export-backend-translate-table backend)
  863. (error "Unknown \"%s\" back-end: Aborting export" backend)))
  864. (defun org-export-derived-backend-p (backend &rest backends)
  865. "Non-nil if BACKEND is derived from one of BACKENDS."
  866. (let ((parent backend))
  867. (while (and (not (memq parent backends))
  868. (setq parent
  869. (plist-get (cdr (assq parent
  870. org-export-registered-backends))
  871. :parent))))
  872. parent))
  873. ;;; The Communication Channel
  874. ;;
  875. ;; During export process, every function has access to a number of
  876. ;; properties. They are of two types:
  877. ;;
  878. ;; 1. Environment options are collected once at the very beginning of
  879. ;; the process, out of the original buffer and configuration.
  880. ;; Collecting them is handled by `org-export-get-environment'
  881. ;; function.
  882. ;;
  883. ;; Most environment options are defined through the
  884. ;; `org-export-options-alist' variable.
  885. ;;
  886. ;; 2. Tree properties are extracted directly from the parsed tree,
  887. ;; just before export, by `org-export-collect-tree-properties'.
  888. ;;
  889. ;; Here is the full list of properties available during transcode
  890. ;; process, with their category and their value type.
  891. ;;
  892. ;; + `:author' :: Author's name.
  893. ;; - category :: option
  894. ;; - type :: string
  895. ;;
  896. ;; + `:back-end' :: Current back-end used for transcoding.
  897. ;; - category :: tree
  898. ;; - type :: symbol
  899. ;;
  900. ;; + `:creator' :: String to write as creation information.
  901. ;; - category :: option
  902. ;; - type :: string
  903. ;;
  904. ;; + `:date' :: String to use as date.
  905. ;; - category :: option
  906. ;; - type :: string
  907. ;;
  908. ;; + `:description' :: Description text for the current data.
  909. ;; - category :: option
  910. ;; - type :: string
  911. ;;
  912. ;; + `:email' :: Author's email.
  913. ;; - category :: option
  914. ;; - type :: string
  915. ;;
  916. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  917. ;; process.
  918. ;; - category :: option
  919. ;; - type :: list of strings
  920. ;;
  921. ;; + `:exported-data' :: Hash table used for memoizing
  922. ;; `org-export-data'.
  923. ;; - category :: tree
  924. ;; - type :: hash table
  925. ;;
  926. ;; + `:filetags' :: List of global tags for buffer. Used by
  927. ;; `org-export-get-tags' to get tags with inheritance.
  928. ;; - category :: option
  929. ;; - type :: list of strings
  930. ;;
  931. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  932. ;; their definition, as parsed data. Only non-inlined footnotes
  933. ;; are represented in this alist. Also, every definition isn't
  934. ;; guaranteed to be referenced in the parse tree. The purpose of
  935. ;; this property is to preserve definitions from oblivion
  936. ;; (i.e. when the parse tree comes from a part of the original
  937. ;; buffer), it isn't meant for direct use in a back-end. To
  938. ;; retrieve a definition relative to a reference, use
  939. ;; `org-export-get-footnote-definition' instead.
  940. ;; - category :: option
  941. ;; - type :: alist (STRING . LIST)
  942. ;;
  943. ;; + `:headline-levels' :: Maximum level being exported as an
  944. ;; headline. Comparison is done with the relative level of
  945. ;; headlines in the parse tree, not necessarily with their
  946. ;; actual level.
  947. ;; - category :: option
  948. ;; - type :: integer
  949. ;;
  950. ;; + `:headline-offset' :: Difference between relative and real level
  951. ;; of headlines in the parse tree. For example, a value of -1
  952. ;; means a level 2 headline should be considered as level
  953. ;; 1 (cf. `org-export-get-relative-level').
  954. ;; - category :: tree
  955. ;; - type :: integer
  956. ;;
  957. ;; + `:headline-numbering' :: Alist between headlines and their
  958. ;; numbering, as a list of numbers
  959. ;; (cf. `org-export-get-headline-number').
  960. ;; - category :: tree
  961. ;; - type :: alist (INTEGER . LIST)
  962. ;;
  963. ;; + `:id-alist' :: Alist between ID strings and destination file's
  964. ;; path, relative to current directory. It is used by
  965. ;; `org-export-resolve-id-link' to resolve ID links targeting an
  966. ;; external file.
  967. ;; - category :: option
  968. ;; - type :: alist (STRING . STRING)
  969. ;;
  970. ;; + `:ignore-list' :: List of elements and objects that should be
  971. ;; ignored during export.
  972. ;; - category :: tree
  973. ;; - type :: list of elements and objects
  974. ;;
  975. ;; + `:input-file' :: Full path to input file, if any.
  976. ;; - category :: option
  977. ;; - type :: string or nil
  978. ;;
  979. ;; + `:keywords' :: List of keywords attached to data.
  980. ;; - category :: option
  981. ;; - type :: string
  982. ;;
  983. ;; + `:language' :: Default language used for translations.
  984. ;; - category :: option
  985. ;; - type :: string
  986. ;;
  987. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  988. ;; transcoding.
  989. ;; - category :: option
  990. ;; - type :: list (as returned by `org-element-parse-buffer')
  991. ;;
  992. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  993. ;; all line breaks.
  994. ;; - category :: option
  995. ;; - type :: symbol (nil, t)
  996. ;;
  997. ;; + `:section-numbers' :: Non-nil means transcoding should add
  998. ;; section numbers to headlines.
  999. ;; - category :: option
  1000. ;; - type :: symbol (nil, t)
  1001. ;;
  1002. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  1003. ;; in transcoding. When such a tag is present, subtrees without
  1004. ;; it are de facto excluded from the process. See
  1005. ;; `use-select-tags'.
  1006. ;; - category :: option
  1007. ;; - type :: list of strings
  1008. ;;
  1009. ;; + `:target-list' :: List of targets encountered in the parse tree.
  1010. ;; This is used to partly resolve "fuzzy" links
  1011. ;; (cf. `org-export-resolve-fuzzy-link').
  1012. ;; - category :: tree
  1013. ;; - type :: list of strings
  1014. ;;
  1015. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  1016. ;; a time stamp in the output.
  1017. ;; - category :: option
  1018. ;; - type :: symbol (nil, t)
  1019. ;;
  1020. ;; + `:translate-alist' :: Alist between element and object types and
  1021. ;; transcoding functions relative to the current back-end.
  1022. ;; Special keys `template' and `plain-text' are also possible.
  1023. ;; - category :: option
  1024. ;; - type :: alist (SYMBOL . FUNCTION)
  1025. ;;
  1026. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  1027. ;; also be transcoded. If it is set to the `headline' symbol,
  1028. ;; only the archived headline's name is retained.
  1029. ;; - category :: option
  1030. ;; - type :: symbol (nil, t, `headline')
  1031. ;;
  1032. ;; + `:with-author' :: Non-nil means author's name should be included
  1033. ;; in the output.
  1034. ;; - category :: option
  1035. ;; - type :: symbol (nil, t)
  1036. ;;
  1037. ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
  1038. ;; - category :: option
  1039. ;; - type :: symbol (nil, t)
  1040. ;;
  1041. ;; + `:with-creator' :: Non-nil means a creation sentence should be
  1042. ;; inserted at the end of the transcoded string. If the value
  1043. ;; is `comment', it should be commented.
  1044. ;; - category :: option
  1045. ;; - type :: symbol (`comment', nil, t)
  1046. ;;
  1047. ;; + `:with-date' :: Non-nil means output should contain a date.
  1048. ;; - category :: option
  1049. ;; - type :. symbol (nil, t)
  1050. ;;
  1051. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  1052. ;; its value is a list of names, only drawers with such names
  1053. ;; will be transcoded.
  1054. ;; - category :: option
  1055. ;; - type :: symbol (nil, t) or list of strings
  1056. ;;
  1057. ;; + `:with-email' :: Non-nil means output should contain author's
  1058. ;; email.
  1059. ;; - category :: option
  1060. ;; - type :: symbol (nil, t)
  1061. ;;
  1062. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  1063. ;; interpreted.
  1064. ;; - category :: option
  1065. ;; - type :: symbol (nil, t)
  1066. ;;
  1067. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  1068. ;; strings starting with a colon as a fixed-with (verbatim) area.
  1069. ;; - category :: option
  1070. ;; - type :: symbol (nil, t)
  1071. ;;
  1072. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  1073. ;; footnotes.
  1074. ;; - category :: option
  1075. ;; - type :: symbol (nil, t)
  1076. ;;
  1077. ;; + `:with-plannings' :: Non-nil means transcoding should include
  1078. ;; planning info.
  1079. ;; - category :: option
  1080. ;; - type :: symbol (nil, t)
  1081. ;;
  1082. ;; + `:with-priority' :: Non-nil means transcoding should include
  1083. ;; priority cookies.
  1084. ;; - category :: option
  1085. ;; - type :: symbol (nil, t)
  1086. ;;
  1087. ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
  1088. ;; plain text.
  1089. ;; - category :: option
  1090. ;; - type :: symbol (nil, t)
  1091. ;;
  1092. ;; + `:with-special-strings' :: Non-nil means transcoding should
  1093. ;; interpret special strings in plain text.
  1094. ;; - category :: option
  1095. ;; - type :: symbol (nil, t)
  1096. ;;
  1097. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  1098. ;; interpret subscript and superscript. With a value of "{}",
  1099. ;; only interpret those using curly brackets.
  1100. ;; - category :: option
  1101. ;; - type :: symbol (nil, {}, t)
  1102. ;;
  1103. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  1104. ;; tables.
  1105. ;; - category :: option
  1106. ;; - type :: symbol (nil, t)
  1107. ;;
  1108. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  1109. ;; headlines. A `not-in-toc' value will remove them from the
  1110. ;; table of contents, if any, nonetheless.
  1111. ;; - category :: option
  1112. ;; - type :: symbol (nil, t, `not-in-toc')
  1113. ;;
  1114. ;; + `:with-tasks' :: Non-nil means transcoding should include
  1115. ;; headlines with a TODO keyword. A `todo' value will only
  1116. ;; include headlines with a todo type keyword while a `done'
  1117. ;; value will do the contrary. If a list of strings is provided,
  1118. ;; only tasks with keywords belonging to that list will be kept.
  1119. ;; - category :: option
  1120. ;; - type :: symbol (t, todo, done, nil) or list of strings
  1121. ;;
  1122. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  1123. ;; time stamps. Special value `active' (resp. `inactive') ask to
  1124. ;; export only active (resp. inactive) timestamps. Otherwise,
  1125. ;; completely remove them.
  1126. ;; - category :: option
  1127. ;; - type :: symbol: (`active', `inactive', t, nil)
  1128. ;;
  1129. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  1130. ;; added to the output. An integer value limits its depth.
  1131. ;; - category :: option
  1132. ;; - type :: symbol (nil, t or integer)
  1133. ;;
  1134. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  1135. ;; include TODO keywords.
  1136. ;; - category :: option
  1137. ;; - type :: symbol (nil, t)
  1138. ;;;; Environment Options
  1139. ;;
  1140. ;; Environment options encompass all parameters defined outside the
  1141. ;; scope of the parsed data. They come from five sources, in
  1142. ;; increasing precedence order:
  1143. ;;
  1144. ;; - Global variables,
  1145. ;; - Buffer's attributes,
  1146. ;; - Options keyword symbols,
  1147. ;; - Buffer keywords,
  1148. ;; - Subtree properties.
  1149. ;;
  1150. ;; The central internal function with regards to environment options
  1151. ;; is `org-export-get-environment'. It updates global variables with
  1152. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  1153. ;; the different sources.
  1154. ;;
  1155. ;; The internal functions doing the retrieval are:
  1156. ;; `org-export--get-global-options',
  1157. ;; `org-export--get-buffer-attributes',
  1158. ;; `org-export--parse-option-keyword',
  1159. ;; `org-export--get-subtree-options' and
  1160. ;; `org-export--get-inbuffer-options'
  1161. ;;
  1162. ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
  1163. ;; take care of the part relative to "#+BIND:" keywords.
  1164. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  1165. "Collect export options from the current buffer.
  1166. Optional argument BACKEND is a symbol specifying which back-end
  1167. specific options to read, if any.
  1168. When optional argument SUBTREEP is non-nil, assume the export is
  1169. done against the current sub-tree.
  1170. Third optional argument EXT-PLIST is a property list with
  1171. external parameters overriding Org default settings, but still
  1172. inferior to file-local settings."
  1173. ;; First install #+BIND variables.
  1174. (org-export--install-letbind-maybe)
  1175. ;; Get and prioritize export options...
  1176. (org-combine-plists
  1177. ;; ... from global variables...
  1178. (org-export--get-global-options backend)
  1179. ;; ... from an external property list...
  1180. ext-plist
  1181. ;; ... from in-buffer settings...
  1182. (org-export--get-inbuffer-options
  1183. backend
  1184. (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
  1185. ;; ... and from subtree, when appropriate.
  1186. (and subtreep (org-export--get-subtree-options backend))
  1187. ;; Eventually add misc. properties.
  1188. (list
  1189. :back-end
  1190. backend
  1191. :translate-alist
  1192. (org-export-backend-translate-table backend)
  1193. :footnote-definition-alist
  1194. ;; Footnotes definitions must be collected in the original
  1195. ;; buffer, as there's no insurance that they will still be in
  1196. ;; the parse tree, due to possible narrowing.
  1197. (let (alist)
  1198. (org-with-wide-buffer
  1199. (goto-char (point-min))
  1200. (while (re-search-forward org-footnote-definition-re nil t)
  1201. (let ((def (save-match-data (org-element-at-point))))
  1202. (when (eq (org-element-type def) 'footnote-definition)
  1203. (push
  1204. (cons (org-element-property :label def)
  1205. (let ((cbeg (org-element-property :contents-begin def)))
  1206. (when cbeg
  1207. (org-element--parse-elements
  1208. cbeg (org-element-property :contents-end def)
  1209. nil nil nil nil (list 'org-data nil)))))
  1210. alist))))
  1211. alist))
  1212. :id-alist
  1213. ;; Collect id references.
  1214. (let (alist)
  1215. (org-with-wide-buffer
  1216. (goto-char (point-min))
  1217. (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
  1218. (let ((link (org-element-context)))
  1219. (when (eq (org-element-type link) 'link)
  1220. (let* ((id (org-element-property :path link))
  1221. (file (org-id-find-id-file id)))
  1222. (when file
  1223. (push (cons id (file-relative-name file)) alist)))))))
  1224. alist))))
  1225. (defun org-export--parse-option-keyword (options &optional backend)
  1226. "Parse an OPTIONS line and return values as a plist.
  1227. Optional argument BACKEND is a symbol specifying which back-end
  1228. specific items to read, if any."
  1229. (let* ((all (append org-export-options-alist
  1230. (and backend (org-export-backend-options backend))))
  1231. ;; Build an alist between #+OPTION: item and property-name.
  1232. (alist (delq nil
  1233. (mapcar (lambda (e)
  1234. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  1235. (car e))))
  1236. all)))
  1237. plist)
  1238. (mapc (lambda (e)
  1239. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  1240. (car e)
  1241. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  1242. options)
  1243. (setq plist (plist-put plist
  1244. (cdr e)
  1245. (car (read-from-string
  1246. (match-string 2 options)))))))
  1247. alist)
  1248. plist))
  1249. (defun org-export--get-subtree-options (&optional backend)
  1250. "Get export options in subtree at point.
  1251. Optional argument BACKEND is a symbol specifying back-end used
  1252. for export. Return options as a plist."
  1253. ;; For each buffer keyword, create an headline property setting the
  1254. ;; same property in communication channel. The name for the property
  1255. ;; is the keyword with "EXPORT_" appended to it.
  1256. (org-with-wide-buffer
  1257. (let (prop plist)
  1258. ;; Make sure point is at an heading.
  1259. (unless (org-at-heading-p) (org-back-to-heading t))
  1260. ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
  1261. ;; title as its fallback value.
  1262. (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
  1263. (progn (looking-at org-todo-line-regexp)
  1264. (org-match-string-no-properties 3))))
  1265. (setq plist
  1266. (plist-put
  1267. plist :title
  1268. (org-element-parse-secondary-string
  1269. prop (org-element-restriction 'keyword)))))
  1270. ;; EXPORT_OPTIONS are parsed in a non-standard way.
  1271. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  1272. (setq plist
  1273. (nconc plist (org-export--parse-option-keyword prop backend))))
  1274. ;; Handle other keywords. TITLE keyword is excluded as it has
  1275. ;; been handled already.
  1276. (let ((seen '("TITLE")))
  1277. (mapc
  1278. (lambda (option)
  1279. (let ((property (nth 1 option)))
  1280. (when (and property (not (member property seen)))
  1281. (let* ((subtree-prop (concat "EXPORT_" property))
  1282. ;; Export properties are not case-sensitive.
  1283. (value (let ((case-fold-search t))
  1284. (org-entry-get (point) subtree-prop))))
  1285. (push property seen)
  1286. (when value
  1287. (setq plist
  1288. (plist-put
  1289. plist
  1290. (car option)
  1291. (cond
  1292. ;; Parse VALUE if required.
  1293. ((member property org-element-document-properties)
  1294. (org-element-parse-secondary-string
  1295. value (org-element-restriction 'keyword)))
  1296. ;; If BEHAVIOUR is `split' expected value is
  1297. ;; a list of strings, not a string.
  1298. ((eq (nth 4 option) 'split) (org-split-string value))
  1299. (t value)))))))))
  1300. ;; Also look for both general keywords and back-end specific
  1301. ;; options if BACKEND is provided.
  1302. (append (and backend (org-export-backend-options backend))
  1303. org-export-options-alist)))
  1304. ;; Return value.
  1305. plist)))
  1306. (defun org-export--get-inbuffer-options (&optional backend files)
  1307. "Return current buffer export options, as a plist.
  1308. Optional argument BACKEND, when non-nil, is a symbol specifying
  1309. which back-end specific options should also be read in the
  1310. process.
  1311. Optional argument FILES is a list of setup files names read so
  1312. far, used to avoid circular dependencies.
  1313. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  1314. (org-with-wide-buffer
  1315. (goto-char (point-min))
  1316. (let ((case-fold-search t) plist)
  1317. ;; 1. Special keywords, as in `org-export-special-keywords'.
  1318. (let ((special-re
  1319. (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
  1320. (while (re-search-forward special-re nil t)
  1321. (let ((element (org-element-at-point)))
  1322. (when (eq (org-element-type element) 'keyword)
  1323. (let* ((key (org-element-property :key element))
  1324. (val (org-element-property :value element))
  1325. (prop
  1326. (cond
  1327. ((equal key "SETUP_FILE")
  1328. (let ((file
  1329. (expand-file-name
  1330. (org-remove-double-quotes (org-trim val)))))
  1331. ;; Avoid circular dependencies.
  1332. (unless (member file files)
  1333. (with-temp-buffer
  1334. (insert (org-file-contents file 'noerror))
  1335. (org-mode)
  1336. (org-export--get-inbuffer-options
  1337. backend (cons file files))))))
  1338. ((equal key "OPTIONS")
  1339. (org-export--parse-option-keyword val backend))
  1340. ((equal key "FILETAGS")
  1341. (list :filetags
  1342. (org-uniquify
  1343. (append (org-split-string val ":")
  1344. (plist-get plist :filetags))))))))
  1345. (setq plist (org-combine-plists plist prop)))))))
  1346. ;; 2. Standard options, as in `org-export-options-alist'.
  1347. (let* ((all (append org-export-options-alist
  1348. ;; Also look for back-end specific options if
  1349. ;; BACKEND is defined.
  1350. (and backend (org-export-backend-options backend))))
  1351. ;; Build ALIST between keyword name and property name.
  1352. (alist
  1353. (delq nil (mapcar
  1354. (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
  1355. all)))
  1356. ;; Build regexp matching all keywords associated to export
  1357. ;; options. Note: the search is case insensitive.
  1358. (opt-re (format "^[ \t]*#\\+%s:"
  1359. (regexp-opt
  1360. (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
  1361. (goto-char (point-min))
  1362. (while (re-search-forward opt-re nil t)
  1363. (let ((element (org-element-at-point)))
  1364. (when (eq (org-element-type element) 'keyword)
  1365. (let* ((key (org-element-property :key element))
  1366. (val (org-element-property :value element))
  1367. (prop (cdr (assoc key alist)))
  1368. (behaviour (nth 4 (assq prop all))))
  1369. (setq plist
  1370. (plist-put
  1371. plist prop
  1372. ;; Handle value depending on specified BEHAVIOUR.
  1373. (case behaviour
  1374. (space
  1375. (if (not (plist-get plist prop)) (org-trim val)
  1376. (concat (plist-get plist prop) " " (org-trim val))))
  1377. (newline
  1378. (org-trim
  1379. (concat (plist-get plist prop) "\n" (org-trim val))))
  1380. (split
  1381. `(,@(plist-get plist prop) ,@(org-split-string val)))
  1382. ('t val)
  1383. (otherwise (if (not (plist-member plist prop)) val
  1384. (plist-get plist prop))))))))))
  1385. ;; Parse keywords specified in
  1386. ;; `org-element-document-properties'.
  1387. (mapc
  1388. (lambda (key)
  1389. (let* ((prop (cdr (assoc key alist)))
  1390. (value (and prop (plist-get plist prop))))
  1391. (when (stringp value)
  1392. (setq plist
  1393. (plist-put
  1394. plist prop
  1395. (org-element-parse-secondary-string
  1396. value (org-element-restriction 'keyword)))))))
  1397. org-element-document-properties))
  1398. ;; 3. Return final value.
  1399. plist)))
  1400. (defun org-export--get-buffer-attributes ()
  1401. "Return properties related to buffer attributes, as a plist."
  1402. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1403. (list
  1404. ;; Store full path of input file name, or nil. For internal use.
  1405. :input-file visited-file
  1406. :title (or (and visited-file
  1407. (file-name-sans-extension
  1408. (file-name-nondirectory visited-file)))
  1409. (buffer-name (buffer-base-buffer))))))
  1410. (defun org-export--get-global-options (&optional backend)
  1411. "Return global export options as a plist.
  1412. Optional argument BACKEND, if non-nil, is a symbol specifying
  1413. which back-end specific export options should also be read in the
  1414. process."
  1415. (let ((all (append org-export-options-alist
  1416. (and backend (org-export-backend-options backend))))
  1417. ;; Output value.
  1418. plist)
  1419. (mapc
  1420. (lambda (cell)
  1421. (setq plist
  1422. (plist-put
  1423. plist
  1424. (car cell)
  1425. ;; Eval default value provided. If keyword is a member
  1426. ;; of `org-element-document-properties', parse it as
  1427. ;; a secondary string before storing it.
  1428. (let ((value (eval (nth 3 cell))))
  1429. (if (not (stringp value)) value
  1430. (let ((keyword (nth 1 cell)))
  1431. (if (not (member keyword org-element-document-properties))
  1432. value
  1433. (org-element-parse-secondary-string
  1434. value (org-element-restriction 'keyword)))))))))
  1435. all)
  1436. ;; Return value.
  1437. plist))
  1438. (defvar org-export--allow-BIND-local nil)
  1439. (defun org-export--confirm-letbind ()
  1440. "Can we use #+BIND values during export?
  1441. By default this will ask for confirmation by the user, to divert
  1442. possible security risks."
  1443. (cond
  1444. ((not org-export-allow-BIND) nil)
  1445. ((eq org-export-allow-BIND t) t)
  1446. ((local-variable-p 'org-export--allow-BIND-local)
  1447. org-export--allow-BIND-local)
  1448. (t (org-set-local 'org-export--allow-BIND-local
  1449. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  1450. (defun org-export--install-letbind-maybe ()
  1451. "Install the values from #+BIND lines as local variables.
  1452. Variables must be installed before in-buffer options are
  1453. retrieved."
  1454. (let ((case-fold-search t) letbind pair)
  1455. (org-with-wide-buffer
  1456. (goto-char (point-min))
  1457. (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
  1458. (let* ((element (org-element-at-point))
  1459. (value (org-element-property :value element)))
  1460. (when (and (eq (org-element-type element) 'keyword)
  1461. (not (equal value ""))
  1462. (org-export--confirm-letbind))
  1463. (push (read (format "(%s)" value)) letbind)))))
  1464. (dolist (pair (nreverse letbind))
  1465. (org-set-local (car pair) (nth 1 pair)))))
  1466. ;;;; Tree Properties
  1467. ;;
  1468. ;; Tree properties are information extracted from parse tree. They
  1469. ;; are initialized at the beginning of the transcoding process by
  1470. ;; `org-export-collect-tree-properties'.
  1471. ;;
  1472. ;; Dedicated functions focus on computing the value of specific tree
  1473. ;; properties during initialization. Thus,
  1474. ;; `org-export--populate-ignore-list' lists elements and objects that
  1475. ;; should be skipped during export, `org-export--get-min-level' gets
  1476. ;; the minimal exportable level, used as a basis to compute relative
  1477. ;; level for headlines. Eventually
  1478. ;; `org-export--collect-headline-numbering' builds an alist between
  1479. ;; headlines and their numbering.
  1480. (defun org-export-collect-tree-properties (data info)
  1481. "Extract tree properties from parse tree.
  1482. DATA is the parse tree from which information is retrieved. INFO
  1483. is a list holding export options.
  1484. Following tree properties are set or updated:
  1485. `:exported-data' Hash table used to memoize results from
  1486. `org-export-data'.
  1487. `:footnote-definition-alist' List of footnotes definitions in
  1488. original buffer and current parse tree.
  1489. `:headline-offset' Offset between true level of headlines and
  1490. local level. An offset of -1 means an headline
  1491. of level 2 should be considered as a level
  1492. 1 headline in the context.
  1493. `:headline-numbering' Alist of all headlines as key an the
  1494. associated numbering as value.
  1495. `:ignore-list' List of elements that should be ignored during
  1496. export.
  1497. `:target-list' List of all targets in the parse tree.
  1498. Return updated plist."
  1499. ;; Install the parse tree in the communication channel, in order to
  1500. ;; use `org-export-get-genealogy' and al.
  1501. (setq info (plist-put info :parse-tree data))
  1502. ;; Get the list of elements and objects to ignore, and put it into
  1503. ;; `:ignore-list'. Do not overwrite any user ignore that might have
  1504. ;; been done during parse tree filtering.
  1505. (setq info
  1506. (plist-put info
  1507. :ignore-list
  1508. (append (org-export--populate-ignore-list data info)
  1509. (plist-get info :ignore-list))))
  1510. ;; Compute `:headline-offset' in order to be able to use
  1511. ;; `org-export-get-relative-level'.
  1512. (setq info
  1513. (plist-put info
  1514. :headline-offset
  1515. (- 1 (org-export--get-min-level data info))))
  1516. ;; Update footnotes definitions list with definitions in parse tree.
  1517. ;; This is required since buffer expansion might have modified
  1518. ;; boundaries of footnote definitions contained in the parse tree.
  1519. ;; This way, definitions in `footnote-definition-alist' are bound to
  1520. ;; match those in the parse tree.
  1521. (let ((defs (plist-get info :footnote-definition-alist)))
  1522. (org-element-map
  1523. data 'footnote-definition
  1524. (lambda (fn)
  1525. (push (cons (org-element-property :label fn)
  1526. `(org-data nil ,@(org-element-contents fn)))
  1527. defs)))
  1528. (setq info (plist-put info :footnote-definition-alist defs)))
  1529. ;; Properties order doesn't matter: get the rest of the tree
  1530. ;; properties.
  1531. (nconc
  1532. `(:target-list
  1533. ,(org-element-map
  1534. data '(keyword target)
  1535. (lambda (blob)
  1536. (when (or (eq (org-element-type blob) 'target)
  1537. (string= (org-element-property :key blob) "TARGET"))
  1538. blob)) info)
  1539. :headline-numbering ,(org-export--collect-headline-numbering data info)
  1540. :exported-data ,(make-hash-table :test 'eq :size 4001))
  1541. info))
  1542. (defun org-export--get-min-level (data options)
  1543. "Return minimum exportable headline's level in DATA.
  1544. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1545. OPTIONS is a plist holding export options."
  1546. (catch 'exit
  1547. (let ((min-level 10000))
  1548. (mapc
  1549. (lambda (blob)
  1550. (when (and (eq (org-element-type blob) 'headline)
  1551. (not (memq blob (plist-get options :ignore-list))))
  1552. (setq min-level
  1553. (min (org-element-property :level blob) min-level)))
  1554. (when (= min-level 1) (throw 'exit 1)))
  1555. (org-element-contents data))
  1556. ;; If no headline was found, for the sake of consistency, set
  1557. ;; minimum level to 1 nonetheless.
  1558. (if (= min-level 10000) 1 min-level))))
  1559. (defun org-export--collect-headline-numbering (data options)
  1560. "Return numbering of all exportable headlines in a parse tree.
  1561. DATA is the parse tree. OPTIONS is the plist holding export
  1562. options.
  1563. Return an alist whose key is an headline and value is its
  1564. associated numbering \(in the shape of a list of numbers\)."
  1565. (let ((numbering (make-vector org-export-max-depth 0)))
  1566. (org-element-map
  1567. data
  1568. 'headline
  1569. (lambda (headline)
  1570. (let ((relative-level
  1571. (1- (org-export-get-relative-level headline options))))
  1572. (cons
  1573. headline
  1574. (loop for n across numbering
  1575. for idx from 0 to org-export-max-depth
  1576. when (< idx relative-level) collect n
  1577. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1578. when (> idx relative-level) do (aset numbering idx 0)))))
  1579. options)))
  1580. (defun org-export--populate-ignore-list (data options)
  1581. "Return list of elements and objects to ignore during export.
  1582. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1583. export options."
  1584. (let* (ignore
  1585. walk-data
  1586. ;; First find trees containing a select tag, if any.
  1587. (selected (org-export--selected-trees data options))
  1588. (walk-data
  1589. (lambda (data)
  1590. ;; Collect ignored elements or objects into IGNORE-LIST.
  1591. (let ((type (org-element-type data)))
  1592. (if (org-export--skip-p data options selected) (push data ignore)
  1593. (if (and (eq type 'headline)
  1594. (eq (plist-get options :with-archived-trees) 'headline)
  1595. (org-element-property :archivedp data))
  1596. ;; If headline is archived but tree below has
  1597. ;; to be skipped, add it to ignore list.
  1598. (mapc (lambda (e) (push e ignore))
  1599. (org-element-contents data))
  1600. ;; Move into secondary string, if any.
  1601. (let ((sec-prop
  1602. (cdr (assq type org-element-secondary-value-alist))))
  1603. (when sec-prop
  1604. (mapc walk-data (org-element-property sec-prop data))))
  1605. ;; Move into recursive objects/elements.
  1606. (mapc walk-data (org-element-contents data))))))))
  1607. ;; Main call.
  1608. (funcall walk-data data)
  1609. ;; Return value.
  1610. ignore))
  1611. (defun org-export--selected-trees (data info)
  1612. "Return list of headlines containing a select tag in their tree.
  1613. DATA is parsed data as returned by `org-element-parse-buffer'.
  1614. INFO is a plist holding export options."
  1615. (let* (selected-trees
  1616. walk-data ; for byte-compiler.
  1617. (walk-data
  1618. (function
  1619. (lambda (data genealogy)
  1620. (case (org-element-type data)
  1621. (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
  1622. (org-element-contents data)))
  1623. (headline
  1624. (let ((tags (org-element-property :tags data)))
  1625. (if (loop for tag in (plist-get info :select-tags)
  1626. thereis (member tag tags))
  1627. ;; When a select tag is found, mark full
  1628. ;; genealogy and every headline within the tree
  1629. ;; as acceptable.
  1630. (setq selected-trees
  1631. (append
  1632. genealogy
  1633. (org-element-map data 'headline 'identity)
  1634. selected-trees))
  1635. ;; Else, continue searching in tree, recursively.
  1636. (mapc
  1637. (lambda (el) (funcall walk-data el (cons data genealogy)))
  1638. (org-element-contents data))))))))))
  1639. (funcall walk-data data nil) selected-trees))
  1640. (defun org-export--skip-p (blob options selected)
  1641. "Non-nil when element or object BLOB should be skipped during export.
  1642. OPTIONS is the plist holding export options. SELECTED, when
  1643. non-nil, is a list of headlines belonging to a tree with a select
  1644. tag."
  1645. (case (org-element-type blob)
  1646. (clock (not (plist-get options :with-clocks)))
  1647. (drawer
  1648. (or (not (plist-get options :with-drawers))
  1649. (and (consp (plist-get options :with-drawers))
  1650. (not (member (org-element-property :drawer-name blob)
  1651. (plist-get options :with-drawers))))))
  1652. (headline
  1653. (let ((with-tasks (plist-get options :with-tasks))
  1654. (todo (org-element-property :todo-keyword blob))
  1655. (todo-type (org-element-property :todo-type blob))
  1656. (archived (plist-get options :with-archived-trees))
  1657. (tags (org-element-property :tags blob)))
  1658. (or
  1659. ;; Ignore subtrees with an exclude tag.
  1660. (loop for k in (plist-get options :exclude-tags)
  1661. thereis (member k tags))
  1662. ;; When a select tag is present in the buffer, ignore any tree
  1663. ;; without it.
  1664. (and selected (not (memq blob selected)))
  1665. ;; Ignore commented sub-trees.
  1666. (org-element-property :commentedp blob)
  1667. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1668. (and (not archived) (org-element-property :archivedp blob))
  1669. ;; Ignore tasks, if specified by `:with-tasks' property.
  1670. (and todo
  1671. (or (not with-tasks)
  1672. (and (memq with-tasks '(todo done))
  1673. (not (eq todo-type with-tasks)))
  1674. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1675. (inlinetask (not (plist-get options :with-inlinetasks)))
  1676. (planning (not (plist-get options :with-plannings)))
  1677. (statistics-cookie (not (plist-get options :with-statistics-cookies)))
  1678. (table-cell
  1679. (and (org-export-table-has-special-column-p
  1680. (org-export-get-parent-table blob))
  1681. (not (org-export-get-previous-element blob options))))
  1682. (table-row (org-export-table-row-is-special-p blob options))
  1683. (timestamp
  1684. (case (plist-get options :with-timestamps)
  1685. ;; No timestamp allowed.
  1686. ('nil t)
  1687. ;; Only active timestamps allowed and the current one isn't
  1688. ;; active.
  1689. (active
  1690. (not (memq (org-element-property :type blob)
  1691. '(active active-range))))
  1692. ;; Only inactive timestamps allowed and the current one isn't
  1693. ;; inactive.
  1694. (inactive
  1695. (not (memq (org-element-property :type blob)
  1696. '(inactive inactive-range))))))))
  1697. ;;; The Transcoder
  1698. ;;
  1699. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1700. ;; `org-element-parse-buffer') and transcodes it into a specified
  1701. ;; back-end output. It takes care of filtering out elements or
  1702. ;; objects according to export options and organizing the output blank
  1703. ;; lines and white space are preserved. The function memoizes its
  1704. ;; results, so it is cheap to call it within translators.
  1705. ;;
  1706. ;; Internally, three functions handle the filtering of objects and
  1707. ;; elements during the export. In particular,
  1708. ;; `org-export-ignore-element' marks an element or object so future
  1709. ;; parse tree traversals skip it, `org-export--interpret-p' tells which
  1710. ;; elements or objects should be seen as real Org syntax and
  1711. ;; `org-export-expand' transforms the others back into their original
  1712. ;; shape
  1713. ;;
  1714. ;; `org-export-transcoder' is an accessor returning appropriate
  1715. ;; translator function for a given element or object.
  1716. (defun org-export-transcoder (blob info)
  1717. "Return appropriate transcoder for BLOB.
  1718. INFO is a plist containing export directives."
  1719. (let ((type (org-element-type blob)))
  1720. ;; Return contents only for complete parse trees.
  1721. (if (eq type 'org-data) (lambda (blob contents info) contents)
  1722. (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
  1723. (and (functionp transcoder) transcoder)))))
  1724. (defun org-export-data (data info)
  1725. "Convert DATA into current back-end format.
  1726. DATA is a parse tree, an element or an object or a secondary
  1727. string. INFO is a plist holding export options.
  1728. Return transcoded string."
  1729. (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
  1730. (if (not (eq memo 'no-memo)) memo
  1731. (let* ((type (org-element-type data))
  1732. (results
  1733. (cond
  1734. ;; Ignored element/object.
  1735. ((memq data (plist-get info :ignore-list)) nil)
  1736. ;; Plain text.
  1737. ((eq type 'plain-text)
  1738. (org-export-filter-apply-functions
  1739. (plist-get info :filter-plain-text)
  1740. (let ((transcoder (org-export-transcoder data info)))
  1741. (if transcoder (funcall transcoder data info) data))
  1742. info))
  1743. ;; Uninterpreted element/object: change it back to Org
  1744. ;; syntax and export again resulting raw string.
  1745. ((not (org-export--interpret-p data info))
  1746. (org-export-data
  1747. (org-export-expand
  1748. data
  1749. (mapconcat (lambda (blob) (org-export-data blob info))
  1750. (org-element-contents data)
  1751. ""))
  1752. info))
  1753. ;; Secondary string.
  1754. ((not type)
  1755. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1756. ;; Element/Object without contents or, as a special case,
  1757. ;; headline with archive tag and archived trees restricted
  1758. ;; to title only.
  1759. ((or (not (org-element-contents data))
  1760. (and (eq type 'headline)
  1761. (eq (plist-get info :with-archived-trees) 'headline)
  1762. (org-element-property :archivedp data)))
  1763. (let ((transcoder (org-export-transcoder data info)))
  1764. (and (functionp transcoder)
  1765. (funcall transcoder data nil info))))
  1766. ;; Element/Object with contents.
  1767. (t
  1768. (let ((transcoder (org-export-transcoder data info)))
  1769. (when transcoder
  1770. (let* ((greaterp (memq type org-element-greater-elements))
  1771. (objectp
  1772. (and (not greaterp)
  1773. (memq type org-element-recursive-objects)))
  1774. (contents
  1775. (mapconcat
  1776. (lambda (element) (org-export-data element info))
  1777. (org-element-contents
  1778. (if (or greaterp objectp) data
  1779. ;; Elements directly containing objects
  1780. ;; must have their indentation normalized
  1781. ;; first.
  1782. (org-element-normalize-contents
  1783. data
  1784. ;; When normalizing contents of the first
  1785. ;; paragraph in an item or a footnote
  1786. ;; definition, ignore first line's
  1787. ;; indentation: there is none and it
  1788. ;; might be misleading.
  1789. (when (eq type 'paragraph)
  1790. (let ((parent (org-export-get-parent data)))
  1791. (and
  1792. (eq (car (org-element-contents parent))
  1793. data)
  1794. (memq (org-element-type parent)
  1795. '(footnote-definition item))))))))
  1796. "")))
  1797. (funcall transcoder data
  1798. (if (not greaterp) contents
  1799. (org-element-normalize-string contents))
  1800. info))))))))
  1801. ;; Final result will be memoized before being returned.
  1802. (puthash
  1803. data
  1804. (cond
  1805. ((not results) nil)
  1806. ((memq type '(org-data plain-text nil)) results)
  1807. ;; Append the same white space between elements or objects as in
  1808. ;; the original buffer, and call appropriate filters.
  1809. (t
  1810. (let ((results
  1811. (org-export-filter-apply-functions
  1812. (plist-get info (intern (format ":filter-%s" type)))
  1813. (let ((post-blank (or (org-element-property :post-blank data)
  1814. 0)))
  1815. (if (memq type org-element-all-elements)
  1816. (concat (org-element-normalize-string results)
  1817. (make-string post-blank ?\n))
  1818. (concat results (make-string post-blank ? ))))
  1819. info)))
  1820. results)))
  1821. (plist-get info :exported-data))))))
  1822. (defun org-export--interpret-p (blob info)
  1823. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1824. Check is done according to export options INFO, stored as
  1825. a plist."
  1826. (case (org-element-type blob)
  1827. ;; ... entities...
  1828. (entity (plist-get info :with-entities))
  1829. ;; ... emphasis...
  1830. ((bold italic strike-through underline)
  1831. (plist-get info :with-emphasize))
  1832. ;; ... fixed-width areas.
  1833. (fixed-width (plist-get info :with-fixed-width))
  1834. ;; ... footnotes...
  1835. ((footnote-definition footnote-reference)
  1836. (plist-get info :with-footnotes))
  1837. ;; ... sub/superscripts...
  1838. ((subscript superscript)
  1839. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1840. (if (eq sub/super-p '{})
  1841. (org-element-property :use-brackets-p blob)
  1842. sub/super-p)))
  1843. ;; ... tables...
  1844. (table (plist-get info :with-tables))
  1845. (otherwise t)))
  1846. (defun org-export-expand (blob contents)
  1847. "Expand a parsed element or object to its original state.
  1848. BLOB is either an element or an object. CONTENTS is its
  1849. contents, as a string or nil."
  1850. (funcall
  1851. (intern (format "org-element-%s-interpreter" (org-element-type blob)))
  1852. blob contents))
  1853. (defun org-export-ignore-element (element info)
  1854. "Add ELEMENT to `:ignore-list' in INFO.
  1855. Any element in `:ignore-list' will be skipped when using
  1856. `org-element-map'. INFO is modified by side effects."
  1857. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  1858. ;;; The Filter System
  1859. ;;
  1860. ;; Filters allow end-users to tweak easily the transcoded output.
  1861. ;; They are the functional counterpart of hooks, as every filter in
  1862. ;; a set is applied to the return value of the previous one.
  1863. ;;
  1864. ;; Every set is back-end agnostic. Although, a filter is always
  1865. ;; called, in addition to the string it applies to, with the back-end
  1866. ;; used as argument, so it's easy for the end-user to add back-end
  1867. ;; specific filters in the set. The communication channel, as
  1868. ;; a plist, is required as the third argument.
  1869. ;;
  1870. ;; From the developer side, filters sets can be installed in the
  1871. ;; process with the help of `org-export-define-backend', which
  1872. ;; internally stores filters as an alist. Each association has a key
  1873. ;; among the following symbols and a function or a list of functions
  1874. ;; as value.
  1875. ;;
  1876. ;; - `:filter-parse-tree' applies directly on the complete parsed
  1877. ;; tree. It's the only filters set that doesn't apply to a string.
  1878. ;; Users can set it through `org-export-filter-parse-tree-functions'
  1879. ;; variable.
  1880. ;;
  1881. ;; - `:filter-final-output' applies to the final transcoded string.
  1882. ;; Users can set it with `org-export-filter-final-output-functions'
  1883. ;; variable
  1884. ;;
  1885. ;; - `:filter-plain-text' applies to any string not recognized as Org
  1886. ;; syntax. `org-export-filter-plain-text-functions' allows users to
  1887. ;; configure it.
  1888. ;;
  1889. ;; - `:filter-TYPE' applies on the string returned after an element or
  1890. ;; object of type TYPE has been transcoded. An user can modify
  1891. ;; `org-export-filter-TYPE-functions'
  1892. ;;
  1893. ;; All filters sets are applied with
  1894. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1895. ;; applied in a LIFO fashion. It allows developers to be sure that
  1896. ;; their filters will be applied first.
  1897. ;;
  1898. ;; Filters properties are installed in communication channel with
  1899. ;; `org-export-install-filters' function.
  1900. ;;
  1901. ;; Eventually, two hooks (`org-export-before-processing-hook' and
  1902. ;; `org-export-before-parsing-hook') are run at the beginning of the
  1903. ;; export process and just before parsing to allow for heavy structure
  1904. ;; modifications.
  1905. ;;;; Hooks
  1906. (defvar org-export-before-processing-hook nil
  1907. "Hook run at the beginning of the export process.
  1908. This is run before include keywords and macros are expanded and
  1909. Babel code blocks executed, on a copy of the original buffer
  1910. being exported. Visibility and narrowing are preserved. Point
  1911. is at the beginning of the buffer.
  1912. Every function in this hook will be called with one argument: the
  1913. back-end currently used, as a symbol.")
  1914. (defvar org-export-before-parsing-hook nil
  1915. "Hook run before parsing an export buffer.
  1916. This is run after include keywords and macros have been expanded
  1917. and Babel code blocks executed, on a copy of the original buffer
  1918. being exported. Visibility and narrowing are preserved. Point
  1919. is at the beginning of the buffer.
  1920. Every function in this hook will be called with one argument: the
  1921. back-end currently used, as a symbol.")
  1922. ;;;; Special Filters
  1923. (defvar org-export-filter-parse-tree-functions nil
  1924. "List of functions applied to the parsed tree.
  1925. Each filter is called with three arguments: the parse tree, as
  1926. returned by `org-element-parse-buffer', the back-end, as
  1927. a symbol, and the communication channel, as a plist. It must
  1928. return the modified parse tree to transcode.")
  1929. (defvar org-export-filter-final-output-functions nil
  1930. "List of functions applied to the transcoded string.
  1931. Each filter is called with three arguments: the full transcoded
  1932. string, the back-end, as a symbol, and the communication channel,
  1933. as a plist. It must return a string that will be used as the
  1934. final export output.")
  1935. (defvar org-export-filter-plain-text-functions nil
  1936. "List of functions applied to plain text.
  1937. Each filter is called with three arguments: a string which
  1938. contains no Org syntax, the back-end, as a symbol, and the
  1939. communication channel, as a plist. It must return a string or
  1940. nil.")
  1941. ;;;; Elements Filters
  1942. (defvar org-export-filter-babel-call-functions nil
  1943. "List of functions applied to a transcoded babel-call.
  1944. Each filter is called with three arguments: the transcoded data,
  1945. as a string, the back-end, as a symbol, and the communication
  1946. channel, as a plist. It must return a string or nil.")
  1947. (defvar org-export-filter-center-block-functions nil
  1948. "List of functions applied to a transcoded center block.
  1949. Each filter is called with three arguments: the transcoded data,
  1950. as a string, the back-end, as a symbol, and the communication
  1951. channel, as a plist. It must return a string or nil.")
  1952. (defvar org-export-filter-clock-functions nil
  1953. "List of functions applied to a transcoded clock.
  1954. Each filter is called with three arguments: the transcoded data,
  1955. as a string, the back-end, as a symbol, and the communication
  1956. channel, as a plist. It must return a string or nil.")
  1957. (defvar org-export-filter-comment-functions nil
  1958. "List of functions applied to a transcoded comment.
  1959. Each filter is called with three arguments: the transcoded data,
  1960. as a string, the back-end, as a symbol, and the communication
  1961. channel, as a plist. It must return a string or nil.")
  1962. (defvar org-export-filter-comment-block-functions nil
  1963. "List of functions applied to a transcoded comment-block.
  1964. Each filter is called with three arguments: the transcoded data,
  1965. as a string, the back-end, as a symbol, and the communication
  1966. channel, as a plist. It must return a string or nil.")
  1967. (defvar org-export-filter-diary-sexp-functions nil
  1968. "List of functions applied to a transcoded diary-sexp.
  1969. Each filter is called with three arguments: the transcoded data,
  1970. as a string, the back-end, as a symbol, and the communication
  1971. channel, as a plist. It must return a string or nil.")
  1972. (defvar org-export-filter-drawer-functions nil
  1973. "List of functions applied to a transcoded drawer.
  1974. Each filter is called with three arguments: the transcoded data,
  1975. as a string, the back-end, as a symbol, and the communication
  1976. channel, as a plist. It must return a string or nil.")
  1977. (defvar org-export-filter-dynamic-block-functions nil
  1978. "List of functions applied to a transcoded dynamic-block.
  1979. Each filter is called with three arguments: the transcoded data,
  1980. as a string, the back-end, as a symbol, and the communication
  1981. channel, as a plist. It must return a string or nil.")
  1982. (defvar org-export-filter-example-block-functions nil
  1983. "List of functions applied to a transcoded example-block.
  1984. Each filter is called with three arguments: the transcoded data,
  1985. as a string, the back-end, as a symbol, and the communication
  1986. channel, as a plist. It must return a string or nil.")
  1987. (defvar org-export-filter-export-block-functions nil
  1988. "List of functions applied to a transcoded export-block.
  1989. Each filter is called with three arguments: the transcoded data,
  1990. as a string, the back-end, as a symbol, and the communication
  1991. channel, as a plist. It must return a string or nil.")
  1992. (defvar org-export-filter-fixed-width-functions nil
  1993. "List of functions applied to a transcoded fixed-width.
  1994. Each filter is called with three arguments: the transcoded data,
  1995. as a string, the back-end, as a symbol, and the communication
  1996. channel, as a plist. It must return a string or nil.")
  1997. (defvar org-export-filter-footnote-definition-functions nil
  1998. "List of functions applied to a transcoded footnote-definition.
  1999. Each filter is called with three arguments: the transcoded data,
  2000. as a string, the back-end, as a symbol, and the communication
  2001. channel, as a plist. It must return a string or nil.")
  2002. (defvar org-export-filter-headline-functions nil
  2003. "List of functions applied to a transcoded headline.
  2004. Each filter is called with three arguments: the transcoded data,
  2005. as a string, the back-end, as a symbol, and the communication
  2006. channel, as a plist. It must return a string or nil.")
  2007. (defvar org-export-filter-horizontal-rule-functions nil
  2008. "List of functions applied to a transcoded horizontal-rule.
  2009. Each filter is called with three arguments: the transcoded data,
  2010. as a string, the back-end, as a symbol, and the communication
  2011. channel, as a plist. It must return a string or nil.")
  2012. (defvar org-export-filter-inlinetask-functions nil
  2013. "List of functions applied to a transcoded inlinetask.
  2014. Each filter is called with three arguments: the transcoded data,
  2015. as a string, the back-end, as a symbol, and the communication
  2016. channel, as a plist. It must return a string or nil.")
  2017. (defvar org-export-filter-item-functions nil
  2018. "List of functions applied to a transcoded item.
  2019. Each filter is called with three arguments: the transcoded data,
  2020. as a string, the back-end, as a symbol, and the communication
  2021. channel, as a plist. It must return a string or nil.")
  2022. (defvar org-export-filter-keyword-functions nil
  2023. "List of functions applied to a transcoded keyword.
  2024. Each filter is called with three arguments: the transcoded data,
  2025. as a string, the back-end, as a symbol, and the communication
  2026. channel, as a plist. It must return a string or nil.")
  2027. (defvar org-export-filter-latex-environment-functions nil
  2028. "List of functions applied to a transcoded latex-environment.
  2029. Each filter is called with three arguments: the transcoded data,
  2030. as a string, the back-end, as a symbol, and the communication
  2031. channel, as a plist. It must return a string or nil.")
  2032. (defvar org-export-filter-node-property-functions nil
  2033. "List of functions applied to a transcoded node-property.
  2034. Each filter is called with three arguments: the transcoded data,
  2035. as a string, the back-end, as a symbol, and the communication
  2036. channel, as a plist. It must return a string or nil.")
  2037. (defvar org-export-filter-paragraph-functions nil
  2038. "List of functions applied to a transcoded paragraph.
  2039. Each filter is called with three arguments: the transcoded data,
  2040. as a string, the back-end, as a symbol, and the communication
  2041. channel, as a plist. It must return a string or nil.")
  2042. (defvar org-export-filter-plain-list-functions nil
  2043. "List of functions applied to a transcoded plain-list.
  2044. Each filter is called with three arguments: the transcoded data,
  2045. as a string, the back-end, as a symbol, and the communication
  2046. channel, as a plist. It must return a string or nil.")
  2047. (defvar org-export-filter-planning-functions nil
  2048. "List of functions applied to a transcoded planning.
  2049. Each filter is called with three arguments: the transcoded data,
  2050. as a string, the back-end, as a symbol, and the communication
  2051. channel, as a plist. It must return a string or nil.")
  2052. (defvar org-export-filter-property-drawer-functions nil
  2053. "List of functions applied to a transcoded property-drawer.
  2054. Each filter is called with three arguments: the transcoded data,
  2055. as a string, the back-end, as a symbol, and the communication
  2056. channel, as a plist. It must return a string or nil.")
  2057. (defvar org-export-filter-quote-block-functions nil
  2058. "List of functions applied to a transcoded quote block.
  2059. Each filter is called with three arguments: the transcoded quote
  2060. data, as a string, the back-end, as a symbol, and the
  2061. communication channel, as a plist. It must return a string or
  2062. nil.")
  2063. (defvar org-export-filter-quote-section-functions nil
  2064. "List of functions applied to a transcoded quote-section.
  2065. Each filter is called with three arguments: the transcoded data,
  2066. as a string, the back-end, as a symbol, and the communication
  2067. channel, as a plist. It must return a string or nil.")
  2068. (defvar org-export-filter-section-functions nil
  2069. "List of functions applied to a transcoded section.
  2070. Each filter is called with three arguments: the transcoded data,
  2071. as a string, the back-end, as a symbol, and the communication
  2072. channel, as a plist. It must return a string or nil.")
  2073. (defvar org-export-filter-special-block-functions nil
  2074. "List of functions applied to a transcoded special block.
  2075. Each filter is called with three arguments: the transcoded data,
  2076. as a string, the back-end, as a symbol, and the communication
  2077. channel, as a plist. It must return a string or nil.")
  2078. (defvar org-export-filter-src-block-functions nil
  2079. "List of functions applied to a transcoded src-block.
  2080. Each filter is called with three arguments: the transcoded data,
  2081. as a string, the back-end, as a symbol, and the communication
  2082. channel, as a plist. It must return a string or nil.")
  2083. (defvar org-export-filter-table-functions nil
  2084. "List of functions applied to a transcoded table.
  2085. Each filter is called with three arguments: the transcoded data,
  2086. as a string, the back-end, as a symbol, and the communication
  2087. channel, as a plist. It must return a string or nil.")
  2088. (defvar org-export-filter-table-cell-functions nil
  2089. "List of functions applied to a transcoded table-cell.
  2090. Each filter is called with three arguments: the transcoded data,
  2091. as a string, the back-end, as a symbol, and the communication
  2092. channel, as a plist. It must return a string or nil.")
  2093. (defvar org-export-filter-table-row-functions nil
  2094. "List of functions applied to a transcoded table-row.
  2095. Each filter is called with three arguments: the transcoded data,
  2096. as a string, the back-end, as a symbol, and the communication
  2097. channel, as a plist. It must return a string or nil.")
  2098. (defvar org-export-filter-verse-block-functions nil
  2099. "List of functions applied to a transcoded verse block.
  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. ;;;; Objects Filters
  2104. (defvar org-export-filter-bold-functions nil
  2105. "List of functions applied to transcoded bold text.
  2106. Each filter is called with three arguments: the transcoded data,
  2107. as a string, the back-end, as a symbol, and the communication
  2108. channel, as a plist. It must return a string or nil.")
  2109. (defvar org-export-filter-code-functions nil
  2110. "List of functions applied to transcoded code text.
  2111. Each filter is called with three arguments: the transcoded data,
  2112. as a string, the back-end, as a symbol, and the communication
  2113. channel, as a plist. It must return a string or nil.")
  2114. (defvar org-export-filter-entity-functions nil
  2115. "List of functions applied to a transcoded entity.
  2116. Each filter is called with three arguments: the transcoded data,
  2117. as a string, the back-end, as a symbol, and the communication
  2118. channel, as a plist. It must return a string or nil.")
  2119. (defvar org-export-filter-export-snippet-functions nil
  2120. "List of functions applied to a transcoded export-snippet.
  2121. Each filter is called with three arguments: the transcoded data,
  2122. as a string, the back-end, as a symbol, and the communication
  2123. channel, as a plist. It must return a string or nil.")
  2124. (defvar org-export-filter-footnote-reference-functions nil
  2125. "List of functions applied to a transcoded footnote-reference.
  2126. Each filter is called with three arguments: the transcoded data,
  2127. as a string, the back-end, as a symbol, and the communication
  2128. channel, as a plist. It must return a string or nil.")
  2129. (defvar org-export-filter-inline-babel-call-functions nil
  2130. "List of functions applied to a transcoded inline-babel-call.
  2131. Each filter is called with three arguments: the transcoded data,
  2132. as a string, the back-end, as a symbol, and the communication
  2133. channel, as a plist. It must return a string or nil.")
  2134. (defvar org-export-filter-inline-src-block-functions nil
  2135. "List of functions applied to a transcoded inline-src-block.
  2136. Each filter is called with three arguments: the transcoded data,
  2137. as a string, the back-end, as a symbol, and the communication
  2138. channel, as a plist. It must return a string or nil.")
  2139. (defvar org-export-filter-italic-functions nil
  2140. "List of functions applied to transcoded italic text.
  2141. Each filter is called with three arguments: the transcoded data,
  2142. as a string, the back-end, as a symbol, and the communication
  2143. channel, as a plist. It must return a string or nil.")
  2144. (defvar org-export-filter-latex-fragment-functions nil
  2145. "List of functions applied to a transcoded latex-fragment.
  2146. Each filter is called with three arguments: the transcoded data,
  2147. as a string, the back-end, as a symbol, and the communication
  2148. channel, as a plist. It must return a string or nil.")
  2149. (defvar org-export-filter-line-break-functions nil
  2150. "List of functions applied to a transcoded line-break.
  2151. Each filter is called with three arguments: the transcoded data,
  2152. as a string, the back-end, as a symbol, and the communication
  2153. channel, as a plist. It must return a string or nil.")
  2154. (defvar org-export-filter-link-functions nil
  2155. "List of functions applied to a transcoded link.
  2156. Each filter is called with three arguments: the transcoded data,
  2157. as a string, the back-end, as a symbol, and the communication
  2158. channel, as a plist. It must return a string or nil.")
  2159. (defvar org-export-filter-macro-functions nil
  2160. "List of functions applied to a transcoded macro.
  2161. Each filter is called with three arguments: the transcoded data,
  2162. as a string, the back-end, as a symbol, and the communication
  2163. channel, as a plist. It must return a string or nil.")
  2164. (defvar org-export-filter-radio-target-functions nil
  2165. "List of functions applied to a transcoded radio-target.
  2166. Each filter is called with three arguments: the transcoded data,
  2167. as a string, the back-end, as a symbol, and the communication
  2168. channel, as a plist. It must return a string or nil.")
  2169. (defvar org-export-filter-statistics-cookie-functions nil
  2170. "List of functions applied to a transcoded statistics-cookie.
  2171. Each filter is called with three arguments: the transcoded data,
  2172. as a string, the back-end, as a symbol, and the communication
  2173. channel, as a plist. It must return a string or nil.")
  2174. (defvar org-export-filter-strike-through-functions nil
  2175. "List of functions applied to transcoded strike-through text.
  2176. Each filter is called with three arguments: the transcoded data,
  2177. as a string, the back-end, as a symbol, and the communication
  2178. channel, as a plist. It must return a string or nil.")
  2179. (defvar org-export-filter-subscript-functions nil
  2180. "List of functions applied to a transcoded subscript.
  2181. Each filter is called with three arguments: the transcoded data,
  2182. as a string, the back-end, as a symbol, and the communication
  2183. channel, as a plist. It must return a string or nil.")
  2184. (defvar org-export-filter-superscript-functions nil
  2185. "List of functions applied to a transcoded superscript.
  2186. Each filter is called with three arguments: the transcoded data,
  2187. as a string, the back-end, as a symbol, and the communication
  2188. channel, as a plist. It must return a string or nil.")
  2189. (defvar org-export-filter-target-functions nil
  2190. "List of functions applied to a transcoded target.
  2191. Each filter is called with three arguments: the transcoded data,
  2192. as a string, the back-end, as a symbol, and the communication
  2193. channel, as a plist. It must return a string or nil.")
  2194. (defvar org-export-filter-timestamp-functions nil
  2195. "List of functions applied to a transcoded timestamp.
  2196. Each filter is called with three arguments: the transcoded data,
  2197. as a string, the back-end, as a symbol, and the communication
  2198. channel, as a plist. It must return a string or nil.")
  2199. (defvar org-export-filter-underline-functions nil
  2200. "List of functions applied to transcoded underline text.
  2201. Each filter is called with three arguments: the transcoded data,
  2202. as a string, the back-end, as a symbol, and the communication
  2203. channel, as a plist. It must return a string or nil.")
  2204. (defvar org-export-filter-verbatim-functions nil
  2205. "List of functions applied to transcoded verbatim text.
  2206. Each filter is called with three arguments: the transcoded data,
  2207. as a string, the back-end, as a symbol, and the communication
  2208. channel, as a plist. It must return a string or nil.")
  2209. ;;;; Filters Tools
  2210. ;;
  2211. ;; Internal function `org-export-install-filters' installs filters
  2212. ;; hard-coded in back-ends (developer filters) and filters from global
  2213. ;; variables (user filters) in the communication channel.
  2214. ;;
  2215. ;; Internal function `org-export-filter-apply-functions' takes care
  2216. ;; about applying each filter in order to a given data. It ignores
  2217. ;; filters returning a nil value but stops whenever a filter returns
  2218. ;; an empty string.
  2219. (defun org-export-filter-apply-functions (filters value info)
  2220. "Call every function in FILTERS.
  2221. Functions are called with arguments VALUE, current export
  2222. back-end and INFO. A function returning a nil value will be
  2223. skipped. If it returns the empty string, the process ends and
  2224. VALUE is ignored.
  2225. Call is done in a LIFO fashion, to be sure that developer
  2226. specified filters, if any, are called first."
  2227. (catch 'exit
  2228. (dolist (filter filters value)
  2229. (let ((result (funcall filter value (plist-get info :back-end) info)))
  2230. (cond ((not result) value)
  2231. ((equal value "") (throw 'exit nil))
  2232. (t (setq value result)))))))
  2233. (defun org-export-install-filters (info)
  2234. "Install filters properties in communication channel.
  2235. INFO is a plist containing the current communication channel.
  2236. Return the updated communication channel."
  2237. (let (plist)
  2238. ;; Install user defined filters with `org-export-filters-alist'.
  2239. (mapc (lambda (p)
  2240. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  2241. org-export-filters-alist)
  2242. ;; Prepend back-end specific filters to that list.
  2243. (mapc (lambda (p)
  2244. ;; Single values get consed, lists are prepended.
  2245. (let ((key (car p)) (value (cdr p)))
  2246. (when value
  2247. (setq plist
  2248. (plist-put
  2249. plist key
  2250. (if (atom value) (cons value (plist-get plist key))
  2251. (append value (plist-get plist key))))))))
  2252. (org-export-backend-filters (plist-get info :back-end)))
  2253. ;; Return new communication channel.
  2254. (org-combine-plists info plist)))
  2255. ;;; Core functions
  2256. ;;
  2257. ;; This is the room for the main function, `org-export-as', along with
  2258. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  2259. ;; They differ only by the way they output the resulting code.
  2260. ;;
  2261. ;; `org-export-output-file-name' is an auxiliary function meant to be
  2262. ;; used with `org-export-to-file'. With a given extension, it tries
  2263. ;; to provide a canonical file name to write export output to.
  2264. ;;
  2265. ;; Note that `org-export-as' doesn't really parse the current buffer,
  2266. ;; but a copy of it (with the same buffer-local variables and
  2267. ;; visibility), where macros and include keywords are expanded and
  2268. ;; Babel blocks are executed, if appropriate.
  2269. ;; `org-export-with-buffer-copy' macro prepares that copy.
  2270. ;;
  2271. ;; File inclusion is taken care of by
  2272. ;; `org-export-expand-include-keyword' and
  2273. ;; `org-export--prepare-file-contents'. Structure wise, including
  2274. ;; a whole Org file in a buffer often makes little sense. For
  2275. ;; example, if the file contains an headline and the include keyword
  2276. ;; was within an item, the item should contain the headline. That's
  2277. ;; why file inclusion should be done before any structure can be
  2278. ;; associated to the file, that is before parsing.
  2279. (defun org-export-as
  2280. (backend &optional subtreep visible-only body-only ext-plist)
  2281. "Transcode current Org buffer into BACKEND code.
  2282. If narrowing is active in the current buffer, only transcode its
  2283. narrowed part.
  2284. If a region is active, transcode that region.
  2285. When optional argument SUBTREEP is non-nil, transcode the
  2286. sub-tree at point, extracting information from the headline
  2287. properties first.
  2288. When optional argument VISIBLE-ONLY is non-nil, don't export
  2289. contents of hidden elements.
  2290. When optional argument BODY-ONLY is non-nil, only return body
  2291. code, without preamble nor postamble.
  2292. Optional argument EXT-PLIST, when provided, is a property list
  2293. with external parameters overriding Org default settings, but
  2294. still inferior to file-local settings.
  2295. Return code as a string."
  2296. ;; Barf if BACKEND isn't registered.
  2297. (org-export-barf-if-invalid-backend backend)
  2298. (save-excursion
  2299. (save-restriction
  2300. ;; Narrow buffer to an appropriate region or subtree for
  2301. ;; parsing. If parsing subtree, be sure to remove main headline
  2302. ;; too.
  2303. (cond ((org-region-active-p)
  2304. (narrow-to-region (region-beginning) (region-end)))
  2305. (subtreep
  2306. (org-narrow-to-subtree)
  2307. (goto-char (point-min))
  2308. (forward-line)
  2309. (narrow-to-region (point) (point-max))))
  2310. ;; Initialize communication channel with original buffer
  2311. ;; attributes, unavailable in its copy.
  2312. (let ((info (org-export--get-buffer-attributes)) tree)
  2313. ;; Update communication channel and get parse tree. Buffer
  2314. ;; isn't parsed directly. Instead, a temporary copy is
  2315. ;; created, where include keywords, macros are expanded and
  2316. ;; code blocks are evaluated.
  2317. (org-export-with-buffer-copy
  2318. ;; Run first hook with current back-end as argument.
  2319. (run-hook-with-args 'org-export-before-processing-hook backend)
  2320. (org-export-expand-include-keyword)
  2321. ;; Update macro templates since #+INCLUDE keywords might have
  2322. ;; added some new ones.
  2323. (org-macro-initialize-templates)
  2324. (org-macro-replace-all org-macro-templates)
  2325. (org-export-execute-babel-code)
  2326. ;; Update radio targets since keyword inclusion might have
  2327. ;; added some more.
  2328. (org-update-radio-target-regexp)
  2329. ;; Run last hook with current back-end as argument.
  2330. (goto-char (point-min))
  2331. (run-hook-with-args 'org-export-before-parsing-hook backend)
  2332. ;; Update communication channel with environment. Also
  2333. ;; install user's and developer's filters.
  2334. (setq info
  2335. (org-export-install-filters
  2336. (org-combine-plists
  2337. info (org-export-get-environment backend subtreep ext-plist))))
  2338. ;; Expand export-specific set of macros: {{{author}}},
  2339. ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
  2340. ;; once regular macros have been expanded, since document
  2341. ;; keywords may contain one of them.
  2342. (org-macro-replace-all
  2343. (list (cons "author"
  2344. (org-element-interpret-data (plist-get info :author)))
  2345. (cons "date"
  2346. (org-element-interpret-data (plist-get info :date)))
  2347. ;; EMAIL is not a parsed keyword: store it as-is.
  2348. (cons "email" (or (plist-get info :email) ""))
  2349. (cons "title"
  2350. (org-element-interpret-data (plist-get info :title)))))
  2351. ;; Eventually parse buffer. Call parse-tree filters to get
  2352. ;; the final tree.
  2353. (setq tree
  2354. (org-export-filter-apply-functions
  2355. (plist-get info :filter-parse-tree)
  2356. (org-element-parse-buffer nil visible-only) info)))
  2357. ;; Now tree is complete, compute its properties and add them
  2358. ;; to communication channel.
  2359. (setq info
  2360. (org-combine-plists
  2361. info (org-export-collect-tree-properties tree info)))
  2362. ;; Eventually transcode TREE. Wrap the resulting string into
  2363. ;; a template, if required. Finally call final-output filter.
  2364. (let* ((body (org-element-normalize-string
  2365. (or (org-export-data tree info) "")))
  2366. (template (cdr (assq 'template
  2367. (plist-get info :translate-alist))))
  2368. ;; Remove all text properties since they cannot be
  2369. ;; retrieved from an external process.
  2370. (output (org-no-properties
  2371. (org-export-filter-apply-functions
  2372. (plist-get info :filter-final-output)
  2373. (if (or (not (functionp template)) body-only) body
  2374. (funcall template body info))
  2375. info))))
  2376. ;; Maybe add final OUTPUT to kill ring, then return it.
  2377. (when (and org-export-copy-to-kill-ring (org-string-nw-p output))
  2378. (org-kill-new output))
  2379. output)))))
  2380. (defun org-export-to-buffer
  2381. (backend buffer &optional subtreep visible-only body-only ext-plist)
  2382. "Call `org-export-as' with output to a specified buffer.
  2383. BACKEND is the back-end used for transcoding, as a symbol.
  2384. BUFFER is the output buffer. If it already exists, it will be
  2385. erased first, otherwise, it will be created.
  2386. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  2387. EXT-PLIST are similar to those used in `org-export-as', which
  2388. see.
  2389. Return buffer."
  2390. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
  2391. (buffer (get-buffer-create buffer)))
  2392. (with-current-buffer buffer
  2393. (erase-buffer)
  2394. (insert out)
  2395. (goto-char (point-min)))
  2396. buffer))
  2397. (defun org-export-to-file
  2398. (backend file &optional subtreep visible-only body-only ext-plist)
  2399. "Call `org-export-as' with output to a specified file.
  2400. BACKEND is the back-end used for transcoding, as a symbol. FILE
  2401. is the name of the output file, as a string.
  2402. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  2403. EXT-PLIST are similar to those used in `org-export-as', which
  2404. see.
  2405. Return output file's name."
  2406. ;; Checks for FILE permissions. `write-file' would do the same, but
  2407. ;; we'd rather avoid needless transcoding of parse tree.
  2408. (unless (file-writable-p file) (error "Output file not writable"))
  2409. ;; Insert contents to a temporary buffer and write it to FILE.
  2410. (let ((out (org-export-as backend subtreep visible-only body-only ext-plist)))
  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-mode-map>\\[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
  4675. (org-e-publish-current-file (memq 'force optns) (memq 'async optns)))
  4676. (publish-current-project
  4677. (org-e-publish-current-project (memq 'force optns) (memq 'async optns)))
  4678. (publish-choose-project
  4679. (org-e-publish (assoc (org-icompleting-read
  4680. "Publish project: "
  4681. org-e-publish-project-alist nil t)
  4682. org-e-publish-project-alist)
  4683. (memq 'force optns)
  4684. (memq 'async optns)))
  4685. (publish-all (org-e-publish-all (memq 'force optns) (memq 'async optns)))
  4686. (otherwise (funcall action
  4687. ;; Return a symbol instead of a list to ease
  4688. ;; asynchronous export macro use.
  4689. (and (memq 'async optns) t)
  4690. (and (memq 'subtree optns) t)
  4691. (and (memq 'visible optns) t)
  4692. (and (memq 'body optns) t))))))
  4693. (defun org-export-dispatch-ui (options first-key expertp)
  4694. "Handle interface for `org-export-dispatch'.
  4695. OPTIONS is a list containing current interactive options set for
  4696. export. It can contain any of the following symbols:
  4697. `body' toggles a body-only export
  4698. `subtree' restricts export to current subtree
  4699. `visible' restricts export to visible part of buffer.
  4700. `force' force publishing files.
  4701. `async' use asynchronous export process
  4702. FIRST-KEY is the key pressed to select the first level menu. It
  4703. is nil when this menu hasn't been selected yet.
  4704. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  4705. buffer is provided, but indications about currently active
  4706. options are given in the prompt. Moreover, \[?] allows to switch
  4707. back to standard interface."
  4708. (let* ((fontify-key
  4709. (lambda (key &optional access-key)
  4710. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  4711. ;; non-nil is the required first-level key to activate
  4712. ;; KEY. When its value is t, activate KEY independently
  4713. ;; on the first key, if any. A nil value means KEY will
  4714. ;; only be activated at first level.
  4715. (if (or (eq access-key t) (eq access-key first-key))
  4716. (org-add-props key nil 'face 'org-warning)
  4717. (org-no-properties key))))
  4718. ;; Prepare menu entries by extracting them from
  4719. ;; `org-export-registered-backends', and sorting them by
  4720. ;; access key and by ordinal, if any.
  4721. (backends (sort
  4722. (sort
  4723. (delq nil
  4724. (mapcar (lambda (b)
  4725. (org-export-backend-menu (car b)))
  4726. org-export-registered-backends))
  4727. (lambda (a b)
  4728. (let ((key-a (nth 1 a))
  4729. (key-b (nth 1 b)))
  4730. (cond ((and (numberp key-a) (numberp key-b))
  4731. (< key-a key-b))
  4732. ((numberp key-b) t)))))
  4733. (lambda (a b) (< (car a) (car b)))))
  4734. ;; Compute a list of allowed keys based on the first key
  4735. ;; pressed, if any. Some keys (?1, ?2, ?3, ?4, ?5 and ?q)
  4736. ;; are always available.
  4737. (allowed-keys
  4738. (nconc (list ?1 ?2 ?3 ?4 ?5)
  4739. (if (not first-key) (org-uniquify (mapcar 'car backends))
  4740. (let (sub-menu)
  4741. (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
  4742. (when (eq (car backend) first-key)
  4743. (setq sub-menu (append (nth 2 backend) sub-menu))))))
  4744. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  4745. ((not first-key) (list ?P)))
  4746. (list ?&)
  4747. (when expertp (list ??))
  4748. (list ?q)))
  4749. ;; Build the help menu for standard UI.
  4750. (help
  4751. (unless expertp
  4752. (concat
  4753. ;; Options are hard-coded.
  4754. (format "Options
  4755. [%s] Body only: %s [%s] Visible only: %s
  4756. [%s] Export scope: %s [%s] Force publishing: %s
  4757. [%s] Asynchronous export: %s\n"
  4758. (funcall fontify-key "1" t)
  4759. (if (memq 'body options) "On " "Off")
  4760. (funcall fontify-key "2" t)
  4761. (if (memq 'visible options) "On " "Off")
  4762. (funcall fontify-key "3" t)
  4763. (if (memq 'subtree options) "Subtree" "Buffer ")
  4764. (funcall fontify-key "4" t)
  4765. (if (memq 'force options) "On " "Off")
  4766. (funcall fontify-key "5" t)
  4767. (if (memq 'async options) "On " "Off"))
  4768. ;; Display registered back-end entries. When a key
  4769. ;; appears for the second time, do not create another
  4770. ;; entry, but append its sub-menu to existing menu.
  4771. (let (last-key)
  4772. (mapconcat
  4773. (lambda (entry)
  4774. (let ((top-key (car entry)))
  4775. (concat
  4776. (unless (eq top-key last-key)
  4777. (setq last-key top-key)
  4778. (format "\n[%s] %s\n"
  4779. (funcall fontify-key (char-to-string top-key))
  4780. (nth 1 entry)))
  4781. (let ((sub-menu (nth 2 entry)))
  4782. (unless (functionp sub-menu)
  4783. ;; Split sub-menu into two columns.
  4784. (let ((index -1))
  4785. (concat
  4786. (mapconcat
  4787. (lambda (sub-entry)
  4788. (incf index)
  4789. (format
  4790. (if (zerop (mod index 2)) " [%s] %-24s"
  4791. "[%s] %s\n")
  4792. (funcall fontify-key
  4793. (char-to-string (car sub-entry))
  4794. top-key)
  4795. (nth 1 sub-entry)))
  4796. sub-menu "")
  4797. (when (zerop (mod index 2)) "\n"))))))))
  4798. backends ""))
  4799. ;; Publishing menu is hard-coded.
  4800. (format "\n[%s] Publish
  4801. [%s] Current file [%s] Current project
  4802. [%s] Choose project [%s] All projects\n\n"
  4803. (funcall fontify-key "P")
  4804. (funcall fontify-key "f" ?P)
  4805. (funcall fontify-key "p" ?P)
  4806. (funcall fontify-key "x" ?P)
  4807. (funcall fontify-key "a" ?P))
  4808. (format "\[%s] Export stack\n" (funcall fontify-key "&" t))
  4809. (format "\[%s] %s"
  4810. (funcall fontify-key "q" t)
  4811. (if first-key "Main menu" "Exit")))))
  4812. ;; Build prompts for both standard and expert UI.
  4813. (standard-prompt (unless expertp "Export command: "))
  4814. (expert-prompt
  4815. (when expertp
  4816. (format
  4817. "Export command (Options: %s%s%s%s%s) [%s]: "
  4818. (if (memq 'body options) (funcall fontify-key "b" t) "-")
  4819. (if (memq 'visible options) (funcall fontify-key "v" t) "-")
  4820. (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
  4821. (if (memq 'force options) (funcall fontify-key "f" t) "-")
  4822. (if (memq 'async options) (funcall fontify-key "a" t) "-")
  4823. (concat allowed-keys)))))
  4824. ;; With expert UI, just read key with a fancy prompt. In standard
  4825. ;; UI, display an intrusive help buffer.
  4826. (if expertp
  4827. (org-export-dispatch-action
  4828. expert-prompt allowed-keys backends options first-key expertp)
  4829. ;; At first call, create frame layout in order to display menu.
  4830. (unless (get-buffer "*Org Export Dispatcher*")
  4831. (delete-other-windows)
  4832. (org-switch-to-buffer-other-window
  4833. (get-buffer-create "*Org Export Dispatcher*"))
  4834. (setq cursor-type nil))
  4835. ;; At this point, the buffer containing the menu exists and is
  4836. ;; visible in the current window. So, refresh it.
  4837. (with-current-buffer "*Org Export Dispatcher*"
  4838. (erase-buffer)
  4839. (insert help))
  4840. (org-fit-window-to-buffer)
  4841. (org-export-dispatch-action
  4842. standard-prompt allowed-keys backends options first-key expertp))))
  4843. (defun org-export-dispatch-action
  4844. (prompt allowed-keys backends options first-key expertp)
  4845. "Read a character from command input and act accordingly.
  4846. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  4847. a list of characters available at a given step in the process.
  4848. BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
  4849. EXPERTP are the same as defined in `org-export-dispatch-ui',
  4850. which see.
  4851. Toggle export options when required. Otherwise, return value is
  4852. a list with action as CAR and a list of interactive export
  4853. options as CDR."
  4854. (let ((key (let ((k (read-char-exclusive prompt)))
  4855. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  4856. ;; depending on user's key pressed.
  4857. (if (< k 27) (+ k 96) k))))
  4858. (cond
  4859. ;; Ignore non-standard characters (i.e. "M-a") and
  4860. ;; undefined associations.
  4861. ((not (memq key allowed-keys))
  4862. (ding)
  4863. (unless expertp (message "Invalid key") (sit-for 1))
  4864. (org-export-dispatch-ui options first-key expertp))
  4865. ;; q key at first level aborts export. At second
  4866. ;; level, cancel first key instead.
  4867. ((eq key ?q) (if (not first-key) (error "Export aborted")
  4868. (org-export-dispatch-ui options nil expertp)))
  4869. ;; Help key: Switch back to standard interface if
  4870. ;; expert UI was active.
  4871. ((eq key ??) (org-export-dispatch-ui options first-key nil))
  4872. ;; Switch to asynchronous export stack.
  4873. ((eq key ?&) '(stack))
  4874. ;; Toggle export options.
  4875. ((memq key '(?1 ?2 ?3 ?4 ?5))
  4876. (org-export-dispatch-ui
  4877. (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
  4878. (?4 'force) (?5 'async))))
  4879. (if (memq option options) (remq option options)
  4880. (cons option options)))
  4881. first-key expertp))
  4882. ;; Action selected: Send key and options back to
  4883. ;; `org-export-dispatch'.
  4884. ((or first-key (functionp (nth 2 (assq key backends))))
  4885. (cons (cond
  4886. ((not first-key) (nth 2 (assq key backends)))
  4887. ;; Publishing actions are hard-coded. Send a special
  4888. ;; signal to `org-export-dispatch'.
  4889. ((eq first-key ?P)
  4890. (case key
  4891. (?f 'publish-current-file)
  4892. (?p 'publish-current-project)
  4893. (?x 'publish-choose-project)
  4894. (?a 'publish-all)))
  4895. ;; Return first action associated to FIRST-KEY + KEY
  4896. ;; path. Indeed, derived backends can share the same
  4897. ;; FIRST-KEY.
  4898. (t (catch 'found
  4899. (mapc (lambda (backend)
  4900. (let ((match (assq key (nth 2 backend))))
  4901. (when match (throw 'found (nth 2 match)))))
  4902. (member (assq first-key backends) backends)))))
  4903. options))
  4904. ;; Otherwise, enter sub-menu.
  4905. (t (org-export-dispatch-ui options key expertp)))))
  4906. (provide 'org-export)
  4907. ;;; org-export.el ends here