ox.el 216 KB

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