org-export.el 185 KB

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