org-export.el 208 KB

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