org-export.el 170 KB

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