org-export.el 169 KB

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