ox.el 214 KB

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