org-export.el 209 KB

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