org-export.el 169 KB

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