org-export.el 168 KB

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