ox.el 213 KB

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