org-export.el 188 KB

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