ox.el 210 KB

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