org-export.el 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516
  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. (defvar org-current-export-file) ; Dynamically scoped
  2240. (defun org-export-as
  2241. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  2242. "Transcode current Org buffer into BACKEND code.
  2243. If narrowing is active in the current buffer, only transcode its
  2244. narrowed part.
  2245. If a region is active, transcode that region.
  2246. When optional argument SUBTREEP is non-nil, transcode the
  2247. sub-tree at point, extracting information from the headline
  2248. properties first.
  2249. When optional argument VISIBLE-ONLY is non-nil, don't export
  2250. contents of hidden elements.
  2251. When optional argument BODY-ONLY is non-nil, only return body
  2252. code, without preamble nor postamble.
  2253. Optional argument EXT-PLIST, when provided, is a property list
  2254. with external parameters overriding Org default settings, but
  2255. still inferior to file-local settings.
  2256. Optional argument NOEXPAND, when non-nil, prevents included files
  2257. to be expanded and Babel code to be executed.
  2258. Return code as a string."
  2259. (save-excursion
  2260. (save-restriction
  2261. ;; Narrow buffer to an appropriate region or subtree for
  2262. ;; parsing. If parsing subtree, be sure to remove main headline
  2263. ;; too.
  2264. (cond ((org-region-active-p)
  2265. (narrow-to-region (region-beginning) (region-end)))
  2266. (subtreep
  2267. (org-narrow-to-subtree)
  2268. (goto-char (point-min))
  2269. (forward-line)
  2270. (narrow-to-region (point) (point-max))))
  2271. ;; 1. Get export environment from original buffer. Also install
  2272. ;; user's and developer's filters.
  2273. (let ((info (org-export-install-filters
  2274. (org-export-get-environment backend subtreep ext-plist)))
  2275. ;; 2. Get parse tree. Buffer isn't parsed directly.
  2276. ;; Instead, a temporary copy is created, where include
  2277. ;; keywords are expanded and code blocks are evaluated.
  2278. (tree (let ((buf (or (buffer-file-name (buffer-base-buffer))
  2279. (current-buffer))))
  2280. (org-export-with-current-buffer-copy
  2281. (unless noexpand
  2282. (org-export-expand-include-keyword)
  2283. ;; Setting `org-current-export-file' is
  2284. ;; required by Org Babel to properly resolve
  2285. ;; noweb references.
  2286. (let ((org-current-export-file buf))
  2287. (org-export-blocks-preprocess)))
  2288. (goto-char (point-min))
  2289. ;; Run hook
  2290. ;; `org-export-before-parsing-hook'. with current
  2291. ;; back-end as argument.
  2292. (run-hook-with-args
  2293. 'org-export-before-parsing-hook backend)
  2294. ;; Eventually parse buffer.
  2295. (org-element-parse-buffer nil visible-only)))))
  2296. ;; 3. Call parse-tree filters to get the final tree.
  2297. (setq tree
  2298. (org-export-filter-apply-functions
  2299. (plist-get info :filter-parse-tree) tree info))
  2300. ;; 4. Now tree is complete, compute its properties and add
  2301. ;; them to communication channel.
  2302. (setq info
  2303. (org-combine-plists
  2304. info (org-export-collect-tree-properties tree info)))
  2305. ;; 5. Eventually transcode TREE. Wrap the resulting string
  2306. ;; into a template, if required. Eventually call
  2307. ;; final-output filter.
  2308. (let* ((body (org-element-normalize-string (org-export-data tree info)))
  2309. (template (cdr (assq 'template
  2310. (plist-get info :translate-alist))))
  2311. (output (org-export-filter-apply-functions
  2312. (plist-get info :filter-final-output)
  2313. (if (or (not (functionp template)) body-only) body
  2314. (funcall template body info))
  2315. info)))
  2316. ;; Maybe add final OUTPUT to kill ring, then return it.
  2317. (when org-export-copy-to-kill-ring (org-kill-new output))
  2318. output)))))
  2319. (defun org-export-to-buffer
  2320. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  2321. "Call `org-export-as' with output to a specified buffer.
  2322. BACKEND is the back-end used for transcoding, as a symbol.
  2323. BUFFER is the output buffer. If it already exists, it will be
  2324. erased first, otherwise, it will be created.
  2325. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  2326. and NOEXPAND are similar to those used in `org-export-as', which
  2327. see.
  2328. Return buffer."
  2329. (let ((out (org-export-as
  2330. backend subtreep visible-only body-only ext-plist noexpand))
  2331. (buffer (get-buffer-create buffer)))
  2332. (with-current-buffer buffer
  2333. (erase-buffer)
  2334. (insert out)
  2335. (goto-char (point-min)))
  2336. buffer))
  2337. (defun org-export-to-file
  2338. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  2339. "Call `org-export-as' with output to a specified file.
  2340. BACKEND is the back-end used for transcoding, as a symbol. FILE
  2341. is the name of the output file, as a string.
  2342. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  2343. and NOEXPAND are similar to those used in `org-export-as', which
  2344. see.
  2345. Return output file's name."
  2346. ;; Checks for FILE permissions. `write-file' would do the same, but
  2347. ;; we'd rather avoid needless transcoding of parse tree.
  2348. (unless (file-writable-p file) (error "Output file not writable"))
  2349. ;; Insert contents to a temporary buffer and write it to FILE.
  2350. (let ((out (org-export-as
  2351. backend subtreep visible-only body-only ext-plist noexpand)))
  2352. (with-temp-buffer
  2353. (insert out)
  2354. (let ((coding-system-for-write org-export-coding-system))
  2355. (write-file file))))
  2356. ;; Return full path.
  2357. file)
  2358. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  2359. "Return output file's name according to buffer specifications.
  2360. EXTENSION is a string representing the output file extension,
  2361. with the leading dot.
  2362. With a non-nil optional argument SUBTREEP, try to determine
  2363. output file's name by looking for \"EXPORT_FILE_NAME\" property
  2364. of subtree at point.
  2365. When optional argument PUB-DIR is set, use it as the publishing
  2366. directory.
  2367. When optional argument VISIBLE-ONLY is non-nil, don't export
  2368. contents of hidden elements.
  2369. Return file name as a string, or nil if it couldn't be
  2370. determined."
  2371. (let ((base-name
  2372. ;; File name may come from EXPORT_FILE_NAME subtree property,
  2373. ;; assuming point is at beginning of said sub-tree.
  2374. (file-name-sans-extension
  2375. (or (and subtreep
  2376. (org-entry-get
  2377. (save-excursion
  2378. (ignore-errors (org-back-to-heading) (point)))
  2379. "EXPORT_FILE_NAME" t))
  2380. ;; File name may be extracted from buffer's associated
  2381. ;; file, if any.
  2382. (buffer-file-name (buffer-base-buffer))
  2383. ;; Can't determine file name on our own: Ask user.
  2384. (let ((read-file-name-function
  2385. (and org-completion-use-ido 'ido-read-file-name)))
  2386. (read-file-name
  2387. "Output file: " pub-dir nil nil nil
  2388. (lambda (name)
  2389. (string= (file-name-extension name t) extension))))))))
  2390. ;; Build file name. Enforce EXTENSION over whatever user may have
  2391. ;; come up with. PUB-DIR, if defined, always has precedence over
  2392. ;; any provided path.
  2393. (cond
  2394. (pub-dir
  2395. (concat (file-name-as-directory pub-dir)
  2396. (file-name-nondirectory base-name)
  2397. extension))
  2398. ((string= (file-name-nondirectory base-name) base-name)
  2399. (concat (file-name-as-directory ".") base-name extension))
  2400. (t (concat base-name extension)))))
  2401. (defmacro org-export-with-current-buffer-copy (&rest body)
  2402. "Apply BODY in a copy of the current buffer.
  2403. The copy preserves local variables and visibility of the original
  2404. buffer.
  2405. Point is at buffer's beginning when BODY is applied."
  2406. (org-with-gensyms (original-buffer offset buffer-string overlays)
  2407. `(let ((,original-buffer (current-buffer))
  2408. (,offset (1- (point-min)))
  2409. (,buffer-string (buffer-string))
  2410. (,overlays (mapcar
  2411. 'copy-overlay (overlays-in (point-min) (point-max)))))
  2412. (with-temp-buffer
  2413. (let ((buffer-invisibility-spec nil))
  2414. (org-clone-local-variables
  2415. ,original-buffer
  2416. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  2417. (insert ,buffer-string)
  2418. (mapc (lambda (ov)
  2419. (move-overlay
  2420. ov
  2421. (- (overlay-start ov) ,offset)
  2422. (- (overlay-end ov) ,offset)
  2423. (current-buffer)))
  2424. ,overlays)
  2425. (goto-char (point-min))
  2426. (progn ,@body))))))
  2427. (def-edebug-spec org-export-with-current-buffer-copy (body))
  2428. (defun org-export-expand-include-keyword (&optional included dir)
  2429. "Expand every include keyword in buffer.
  2430. Optional argument INCLUDED is a list of included file names along
  2431. with their line restriction, when appropriate. It is used to
  2432. avoid infinite recursion. Optional argument DIR is the current
  2433. working directory. It is used to properly resolve relative
  2434. paths."
  2435. (let ((case-fold-search t))
  2436. (goto-char (point-min))
  2437. (while (re-search-forward "^[ \t]*#\\+INCLUDE: \\(.*\\)" nil t)
  2438. (when (eq (org-element-type (save-match-data (org-element-at-point)))
  2439. 'keyword)
  2440. (beginning-of-line)
  2441. ;; Extract arguments from keyword's value.
  2442. (let* ((value (match-string 1))
  2443. (ind (org-get-indentation))
  2444. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  2445. (prog1 (expand-file-name (match-string 1 value) dir)
  2446. (setq value (replace-match "" nil nil value)))))
  2447. (lines
  2448. (and (string-match
  2449. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  2450. (prog1 (match-string 1 value)
  2451. (setq value (replace-match "" nil nil value)))))
  2452. (env (cond ((string-match "\\<example\\>" value) 'example)
  2453. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2454. (match-string 1 value))))
  2455. ;; Minimal level of included file defaults to the child
  2456. ;; level of the current headline, if any, or one. It
  2457. ;; only applies is the file is meant to be included as
  2458. ;; an Org one.
  2459. (minlevel
  2460. (and (not env)
  2461. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2462. (prog1 (string-to-number (match-string 1 value))
  2463. (setq value (replace-match "" nil nil value)))
  2464. (let ((cur (org-current-level)))
  2465. (if cur (1+ (org-reduced-level cur)) 1))))))
  2466. ;; Remove keyword.
  2467. (delete-region (point) (progn (forward-line) (point)))
  2468. (cond
  2469. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  2470. ;; Check if files has already been parsed. Look after
  2471. ;; inclusion lines too, as different parts of the same file
  2472. ;; can be included too.
  2473. ((member (list file lines) included)
  2474. (error "Recursive file inclusion: %s" file))
  2475. (t
  2476. (cond
  2477. ((eq env 'example)
  2478. (insert
  2479. (let ((ind-str (make-string ind ? ))
  2480. (contents
  2481. ;; Protect sensitive contents with commas.
  2482. (replace-regexp-in-string
  2483. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  2484. (org-export--prepare-file-contents file lines)
  2485. nil nil 1)))
  2486. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2487. ind-str contents ind-str))))
  2488. ((stringp env)
  2489. (insert
  2490. (let ((ind-str (make-string ind ? ))
  2491. (contents
  2492. ;; Protect sensitive contents with commas.
  2493. (replace-regexp-in-string
  2494. (if (string= env "org") "\\(^\\)\\(.\\)"
  2495. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  2496. (org-export--prepare-file-contents file lines)
  2497. nil nil 1)))
  2498. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2499. ind-str env contents ind-str))))
  2500. (t
  2501. (insert
  2502. (with-temp-buffer
  2503. (org-mode)
  2504. (insert
  2505. (org-export--prepare-file-contents file lines ind minlevel))
  2506. (org-export-expand-include-keyword
  2507. (cons (list file lines) included)
  2508. (file-name-directory file))
  2509. (buffer-string))))))))))))
  2510. (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
  2511. "Prepare the contents of FILE for inclusion and return them as a string.
  2512. When optional argument LINES is a string specifying a range of
  2513. lines, include only those lines.
  2514. Optional argument IND, when non-nil, is an integer specifying the
  2515. global indentation of returned contents. Since its purpose is to
  2516. allow an included file to stay in the same environment it was
  2517. created \(i.e. a list item), it doesn't apply past the first
  2518. headline encountered.
  2519. Optional argument MINLEVEL, when non-nil, is an integer
  2520. specifying the level that any top-level headline in the included
  2521. file should have."
  2522. (with-temp-buffer
  2523. (insert-file-contents file)
  2524. (when lines
  2525. (let* ((lines (split-string lines "-"))
  2526. (lbeg (string-to-number (car lines)))
  2527. (lend (string-to-number (cadr lines)))
  2528. (beg (if (zerop lbeg) (point-min)
  2529. (goto-char (point-min))
  2530. (forward-line (1- lbeg))
  2531. (point)))
  2532. (end (if (zerop lend) (point-max)
  2533. (goto-char (point-min))
  2534. (forward-line (1- lend))
  2535. (point))))
  2536. (narrow-to-region beg end)))
  2537. ;; Remove blank lines at beginning and end of contents. The logic
  2538. ;; behind that removal is that blank lines around include keyword
  2539. ;; override blank lines in included file.
  2540. (goto-char (point-min))
  2541. (org-skip-whitespace)
  2542. (beginning-of-line)
  2543. (delete-region (point-min) (point))
  2544. (goto-char (point-max))
  2545. (skip-chars-backward " \r\t\n")
  2546. (forward-line)
  2547. (delete-region (point) (point-max))
  2548. ;; If IND is set, preserve indentation of include keyword until
  2549. ;; the first headline encountered.
  2550. (when ind
  2551. (unless (eq major-mode 'org-mode) (org-mode))
  2552. (goto-char (point-min))
  2553. (let ((ind-str (make-string ind ? )))
  2554. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2555. ;; Do not move footnote definitions out of column 0.
  2556. (unless (and (looking-at org-footnote-definition-re)
  2557. (eq (org-element-type (org-element-at-point))
  2558. 'footnote-definition))
  2559. (insert ind-str))
  2560. (forward-line))))
  2561. ;; When MINLEVEL is specified, compute minimal level for headlines
  2562. ;; in the file (CUR-MIN), and remove stars to each headline so
  2563. ;; that headlines with minimal level have a level of MINLEVEL.
  2564. (when minlevel
  2565. (unless (eq major-mode 'org-mode) (org-mode))
  2566. (let ((levels (org-map-entries
  2567. (lambda () (org-reduced-level (org-current-level))))))
  2568. (when levels
  2569. (let ((offset (- minlevel (apply 'min levels))))
  2570. (unless (zerop offset)
  2571. (when org-odd-levels-only (setq offset (* offset 2)))
  2572. ;; Only change stars, don't bother moving whole
  2573. ;; sections.
  2574. (org-map-entries
  2575. (lambda () (if (< offset 0) (delete-char (abs offset))
  2576. (insert (make-string offset ?*))))))))))
  2577. (buffer-string)))
  2578. ;;; Tools For Back-Ends
  2579. ;;
  2580. ;; A whole set of tools is available to help build new exporters. Any
  2581. ;; function general enough to have its use across many back-ends
  2582. ;; should be added here.
  2583. ;;
  2584. ;; As of now, functions operating on footnotes, headlines, links,
  2585. ;; macros, references, src-blocks, tables and tables of contents are
  2586. ;; implemented.
  2587. ;;;; For Affiliated Keywords
  2588. ;;
  2589. ;; `org-export-read-attribute' reads a property from a given element
  2590. ;; as a plist. It can be used to normalize affiliated keywords'
  2591. ;; syntax.
  2592. (defun org-export-read-attribute (attribute element &optional property)
  2593. "Turn ATTRIBUTE property from ELEMENT into a plist.
  2594. When optional argument PROPERTY is non-nil, return the value of
  2595. that property within attributes.
  2596. This function assumes attributes are defined as \":keyword
  2597. value\" pairs. It is appropriate for `:attr_html' like
  2598. properties."
  2599. (let ((attributes
  2600. (let ((value (org-element-property attribute element)))
  2601. (and value
  2602. (read (format "(%s)" (mapconcat 'identity value " ")))))))
  2603. (if property (plist-get attributes property) attributes)))
  2604. ;;;; For Export Snippets
  2605. ;;
  2606. ;; Every export snippet is transmitted to the back-end. Though, the
  2607. ;; latter will only retain one type of export-snippet, ignoring
  2608. ;; others, based on the former's target back-end. The function
  2609. ;; `org-export-snippet-backend' returns that back-end for a given
  2610. ;; export-snippet.
  2611. (defun org-export-snippet-backend (export-snippet)
  2612. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  2613. Translation, with `org-export-snippet-translation-alist', is
  2614. applied."
  2615. (let ((back-end (org-element-property :back-end export-snippet)))
  2616. (intern
  2617. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  2618. back-end))))
  2619. ;;;; For Footnotes
  2620. ;;
  2621. ;; `org-export-collect-footnote-definitions' is a tool to list
  2622. ;; actually used footnotes definitions in the whole parse tree, or in
  2623. ;; an headline, in order to add footnote listings throughout the
  2624. ;; transcoded data.
  2625. ;;
  2626. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  2627. ;; back-ends, when they need to attach the footnote definition only to
  2628. ;; the first occurrence of the corresponding label.
  2629. ;;
  2630. ;; `org-export-get-footnote-definition' and
  2631. ;; `org-export-get-footnote-number' provide easier access to
  2632. ;; additional information relative to a footnote reference.
  2633. (defun org-export-collect-footnote-definitions (data info)
  2634. "Return an alist between footnote numbers, labels and definitions.
  2635. DATA is the parse tree from which definitions are collected.
  2636. INFO is the plist used as a communication channel.
  2637. Definitions are sorted by order of references. They either
  2638. appear as Org data or as a secondary string for inlined
  2639. footnotes. Unreferenced definitions are ignored."
  2640. (let* (num-alist
  2641. collect-fn ; for byte-compiler.
  2642. (collect-fn
  2643. (function
  2644. (lambda (data)
  2645. ;; Collect footnote number, label and definition in DATA.
  2646. (org-element-map
  2647. data 'footnote-reference
  2648. (lambda (fn)
  2649. (when (org-export-footnote-first-reference-p fn info)
  2650. (let ((def (org-export-get-footnote-definition fn info)))
  2651. (push
  2652. (list (org-export-get-footnote-number fn info)
  2653. (org-element-property :label fn)
  2654. def)
  2655. num-alist)
  2656. ;; Also search in definition for nested footnotes.
  2657. (when (eq (org-element-property :type fn) 'standard)
  2658. (funcall collect-fn def)))))
  2659. ;; Don't enter footnote definitions since it will happen
  2660. ;; when their first reference is found.
  2661. info nil 'footnote-definition)))))
  2662. (funcall collect-fn (plist-get info :parse-tree))
  2663. (reverse num-alist)))
  2664. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2665. "Non-nil when a footnote reference is the first one for its label.
  2666. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2667. INFO is the plist used as a communication channel."
  2668. (let ((label (org-element-property :label footnote-reference)))
  2669. ;; Anonymous footnotes are always a first reference.
  2670. (if (not label) t
  2671. ;; Otherwise, return the first footnote with the same LABEL and
  2672. ;; test if it is equal to FOOTNOTE-REFERENCE.
  2673. (let* (search-refs ; for byte-compiler.
  2674. (search-refs
  2675. (function
  2676. (lambda (data)
  2677. (org-element-map
  2678. data 'footnote-reference
  2679. (lambda (fn)
  2680. (cond
  2681. ((string= (org-element-property :label fn) label)
  2682. (throw 'exit fn))
  2683. ;; If FN isn't inlined, be sure to traverse its
  2684. ;; definition before resuming search. See
  2685. ;; comments in `org-export-get-footnote-number'
  2686. ;; for more information.
  2687. ((eq (org-element-property :type fn) 'standard)
  2688. (funcall search-refs
  2689. (org-export-get-footnote-definition fn info)))))
  2690. ;; Don't enter footnote definitions since it will
  2691. ;; happen when their first reference is found.
  2692. info 'first-match 'footnote-definition)))))
  2693. (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  2694. footnote-reference)))))
  2695. (defun org-export-get-footnote-definition (footnote-reference info)
  2696. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2697. INFO is the plist used as a communication channel."
  2698. (let ((label (org-element-property :label footnote-reference)))
  2699. (or (org-element-property :inline-definition footnote-reference)
  2700. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2701. (defun org-export-get-footnote-number (footnote info)
  2702. "Return number associated to a footnote.
  2703. FOOTNOTE is either a footnote reference or a footnote definition.
  2704. INFO is the plist used as a communication channel."
  2705. (let* ((label (org-element-property :label footnote))
  2706. seen-refs
  2707. search-ref ; For byte-compiler.
  2708. (search-ref
  2709. (function
  2710. (lambda (data)
  2711. ;; Search footnote references through DATA, filling
  2712. ;; SEEN-REFS along the way.
  2713. (org-element-map
  2714. data 'footnote-reference
  2715. (lambda (fn)
  2716. (let ((fn-lbl (org-element-property :label fn)))
  2717. (cond
  2718. ;; Anonymous footnote match: return number.
  2719. ((and (not fn-lbl) (eq fn footnote))
  2720. (throw 'exit (1+ (length seen-refs))))
  2721. ;; Labels match: return number.
  2722. ((and label (string= label fn-lbl))
  2723. (throw 'exit (1+ (length seen-refs))))
  2724. ;; Anonymous footnote: it's always a new one. Also,
  2725. ;; be sure to return nil from the `cond' so
  2726. ;; `first-match' doesn't get us out of the loop.
  2727. ((not fn-lbl) (push 'inline seen-refs) nil)
  2728. ;; Label not seen so far: add it so SEEN-REFS.
  2729. ;;
  2730. ;; Also search for subsequent references in
  2731. ;; footnote definition so numbering follows reading
  2732. ;; logic. Note that we don't have to care about
  2733. ;; inline definitions, since `org-element-map'
  2734. ;; already traverses them at the right time.
  2735. ;;
  2736. ;; Once again, return nil to stay in the loop.
  2737. ((not (member fn-lbl seen-refs))
  2738. (push fn-lbl seen-refs)
  2739. (funcall search-ref
  2740. (org-export-get-footnote-definition fn info))
  2741. nil))))
  2742. ;; Don't enter footnote definitions since it will happen
  2743. ;; when their first reference is found.
  2744. info 'first-match 'footnote-definition)))))
  2745. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  2746. ;;;; For Headlines
  2747. ;;
  2748. ;; `org-export-get-relative-level' is a shortcut to get headline
  2749. ;; level, relatively to the lower headline level in the parsed tree.
  2750. ;;
  2751. ;; `org-export-get-headline-number' returns the section number of an
  2752. ;; headline, while `org-export-number-to-roman' allows to convert it
  2753. ;; to roman numbers.
  2754. ;;
  2755. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2756. ;; `org-export-last-sibling-p' are three useful predicates when it
  2757. ;; comes to fulfill the `:headline-levels' property.
  2758. (defun org-export-get-relative-level (headline info)
  2759. "Return HEADLINE relative level within current parsed tree.
  2760. INFO is a plist holding contextual information."
  2761. (+ (org-element-property :level headline)
  2762. (or (plist-get info :headline-offset) 0)))
  2763. (defun org-export-low-level-p (headline info)
  2764. "Non-nil when HEADLINE is considered as low level.
  2765. INFO is a plist used as a communication channel.
  2766. A low level headlines has a relative level greater than
  2767. `:headline-levels' property value.
  2768. Return value is the difference between HEADLINE relative level
  2769. and the last level being considered as high enough, or nil."
  2770. (let ((limit (plist-get info :headline-levels)))
  2771. (when (wholenump limit)
  2772. (let ((level (org-export-get-relative-level headline info)))
  2773. (and (> level limit) (- level limit))))))
  2774. (defun org-export-get-headline-number (headline info)
  2775. "Return HEADLINE numbering as a list of numbers.
  2776. INFO is a plist holding contextual information."
  2777. (cdr (assoc headline (plist-get info :headline-numbering))))
  2778. (defun org-export-numbered-headline-p (headline info)
  2779. "Return a non-nil value if HEADLINE element should be numbered.
  2780. INFO is a plist used as a communication channel."
  2781. (let ((sec-num (plist-get info :section-numbers))
  2782. (level (org-export-get-relative-level headline info)))
  2783. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  2784. (defun org-export-number-to-roman (n)
  2785. "Convert integer N into a roman numeral."
  2786. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2787. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2788. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2789. ( 1 . "I")))
  2790. (res ""))
  2791. (if (<= n 0)
  2792. (number-to-string n)
  2793. (while roman
  2794. (if (>= n (caar roman))
  2795. (setq n (- n (caar roman))
  2796. res (concat res (cdar roman)))
  2797. (pop roman)))
  2798. res)))
  2799. (defun org-export-get-tags (element info &optional tags)
  2800. "Return list of tags associated to ELEMENT.
  2801. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  2802. is a plist used as a communication channel.
  2803. Select tags (see `org-export-select-tags') and exclude tags (see
  2804. `org-export-exclude-tags') are removed from the list.
  2805. When non-nil, optional argument TAGS should be a list of strings.
  2806. Any tag belonging to this list will also be removed."
  2807. (org-remove-if (lambda (tag) (or (member tag (plist-get info :select-tags))
  2808. (member tag (plist-get info :exclude-tags))
  2809. (member tag tags)))
  2810. (org-element-property :tags element)))
  2811. (defun org-export-first-sibling-p (headline info)
  2812. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2813. INFO is a plist used as a communication channel."
  2814. (not (eq (org-element-type (org-export-get-previous-element headline info))
  2815. 'headline)))
  2816. (defun org-export-last-sibling-p (headline info)
  2817. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2818. INFO is a plist used as a communication channel."
  2819. (not (org-export-get-next-element headline info)))
  2820. ;;;; For Links
  2821. ;;
  2822. ;; `org-export-solidify-link-text' turns a string into a safer version
  2823. ;; for links, replacing most non-standard characters with hyphens.
  2824. ;;
  2825. ;; `org-export-get-coderef-format' returns an appropriate format
  2826. ;; string for coderefs.
  2827. ;;
  2828. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2829. ;; provided should be considered as an inline image.
  2830. ;;
  2831. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2832. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2833. ;; returns an appropriate unique identifier when found, or nil.
  2834. ;;
  2835. ;; `org-export-resolve-id-link' returns the first headline with
  2836. ;; specified id or custom-id in parse tree, the path to the external
  2837. ;; file with the id or nil when neither was found.
  2838. ;;
  2839. ;; `org-export-resolve-coderef' associates a reference to a line
  2840. ;; number in the element it belongs, or returns the reference itself
  2841. ;; when the element isn't numbered.
  2842. (defun org-export-solidify-link-text (s)
  2843. "Take link text S and make a safe target out of it."
  2844. (save-match-data
  2845. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2846. (defun org-export-get-coderef-format (path desc)
  2847. "Return format string for code reference link.
  2848. PATH is the link path. DESC is its description."
  2849. (save-match-data
  2850. (cond ((not desc) "%s")
  2851. ((string-match (regexp-quote (concat "(" path ")")) desc)
  2852. (replace-match "%s" t t desc))
  2853. (t desc))))
  2854. (defun org-export-inline-image-p (link &optional rules)
  2855. "Non-nil if LINK object points to an inline image.
  2856. Optional argument is a set of RULES defining inline images. It
  2857. is an alist where associations have the following shape:
  2858. \(TYPE . REGEXP)
  2859. Applying a rule means apply REGEXP against LINK's path when its
  2860. type is TYPE. The function will return a non-nil value if any of
  2861. the provided rules is non-nil. The default rule is
  2862. `org-export-default-inline-image-rule'.
  2863. This only applies to links without a description."
  2864. (and (not (org-element-contents link))
  2865. (let ((case-fold-search t)
  2866. (rules (or rules org-export-default-inline-image-rule)))
  2867. (catch 'exit
  2868. (mapc
  2869. (lambda (rule)
  2870. (and (string= (org-element-property :type link) (car rule))
  2871. (string-match (cdr rule)
  2872. (org-element-property :path link))
  2873. (throw 'exit t)))
  2874. rules)
  2875. ;; Return nil if no rule matched.
  2876. nil))))
  2877. (defun org-export-resolve-coderef (ref info)
  2878. "Resolve a code reference REF.
  2879. INFO is a plist used as a communication channel.
  2880. Return associated line number in source code, or REF itself,
  2881. depending on src-block or example element's switches."
  2882. (org-element-map
  2883. (plist-get info :parse-tree) '(example-block src-block)
  2884. (lambda (el)
  2885. (with-temp-buffer
  2886. (insert (org-trim (org-element-property :value el)))
  2887. (let* ((label-fmt (regexp-quote
  2888. (or (org-element-property :label-fmt el)
  2889. org-coderef-label-format)))
  2890. (ref-re
  2891. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2892. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  2893. ;; Element containing REF is found. Resolve it to either
  2894. ;; a label or a line number, as needed.
  2895. (when (re-search-backward ref-re nil t)
  2896. (cond
  2897. ((org-element-property :use-labels el) ref)
  2898. ((eq (org-element-property :number-lines el) 'continued)
  2899. (+ (org-export-get-loc el info) (line-number-at-pos)))
  2900. (t (line-number-at-pos)))))))
  2901. info 'first-match))
  2902. (defun org-export-resolve-fuzzy-link (link info)
  2903. "Return LINK destination.
  2904. INFO is a plist holding contextual information.
  2905. Return value can be an object, an element, or nil:
  2906. - If LINK path matches a target object (i.e. <<path>>) or
  2907. element (i.e. \"#+TARGET: path\"), return it.
  2908. - If LINK path exactly matches the name affiliated keyword
  2909. \(i.e. #+NAME: path) of an element, return that element.
  2910. - If LINK path exactly matches any headline name, return that
  2911. element. If more than one headline share that name, priority
  2912. will be given to the one with the closest common ancestor, if
  2913. any, or the first one in the parse tree otherwise.
  2914. - Otherwise, return nil.
  2915. Assume LINK type is \"fuzzy\"."
  2916. (let* ((path (org-element-property :path link))
  2917. (match-title-p (eq (aref path 0) ?*)))
  2918. (cond
  2919. ;; First try to find a matching "<<path>>" unless user specified
  2920. ;; he was looking for an headline (path starts with a *
  2921. ;; character).
  2922. ((and (not match-title-p)
  2923. (loop for target in (plist-get info :target-list)
  2924. when (string= (org-element-property :value target) path)
  2925. return target)))
  2926. ;; Then try to find an element with a matching "#+NAME: path"
  2927. ;; affiliated keyword.
  2928. ((and (not match-title-p)
  2929. (org-element-map
  2930. (plist-get info :parse-tree) org-element-all-elements
  2931. (lambda (el)
  2932. (when (string= (org-element-property :name el) path) el))
  2933. info 'first-match)))
  2934. ;; Last case: link either points to an headline or to
  2935. ;; nothingness. Try to find the source, with priority given to
  2936. ;; headlines with the closest common ancestor. If such candidate
  2937. ;; is found, return it, otherwise return nil.
  2938. (t
  2939. (let ((find-headline
  2940. (function
  2941. ;; Return first headline whose `:raw-value' property
  2942. ;; is NAME in parse tree DATA, or nil.
  2943. (lambda (name data)
  2944. (org-element-map
  2945. data 'headline
  2946. (lambda (headline)
  2947. (when (string=
  2948. (org-element-property :raw-value headline)
  2949. name)
  2950. headline))
  2951. info 'first-match)))))
  2952. ;; Search among headlines sharing an ancestor with link,
  2953. ;; from closest to farthest.
  2954. (or (catch 'exit
  2955. (mapc
  2956. (lambda (parent)
  2957. (when (eq (org-element-type parent) 'headline)
  2958. (let ((foundp (funcall find-headline path parent)))
  2959. (when foundp (throw 'exit foundp)))))
  2960. (org-export-get-genealogy link)) nil)
  2961. ;; No match with a common ancestor: try the full parse-tree.
  2962. (funcall find-headline
  2963. (if match-title-p (substring path 1) path)
  2964. (plist-get info :parse-tree))))))))
  2965. (defun org-export-resolve-id-link (link info)
  2966. "Return headline referenced as LINK destination.
  2967. INFO is a plist used as a communication channel.
  2968. Return value can be the headline element matched in current parse
  2969. tree, a file name or nil. Assume LINK type is either \"id\" or
  2970. \"custom-id\"."
  2971. (let ((id (org-element-property :path link)))
  2972. ;; First check if id is within the current parse tree.
  2973. (or (org-element-map
  2974. (plist-get info :parse-tree) 'headline
  2975. (lambda (headline)
  2976. (when (or (string= (org-element-property :id headline) id)
  2977. (string= (org-element-property :custom-id headline) id))
  2978. headline))
  2979. info 'first-match)
  2980. ;; Otherwise, look for external files.
  2981. (cdr (assoc id (plist-get info :id-alist))))))
  2982. (defun org-export-resolve-radio-link (link info)
  2983. "Return radio-target object referenced as LINK destination.
  2984. INFO is a plist used as a communication channel.
  2985. Return value can be a radio-target object or nil. Assume LINK
  2986. has type \"radio\"."
  2987. (let ((path (org-element-property :path link)))
  2988. (org-element-map
  2989. (plist-get info :parse-tree) 'radio-target
  2990. (lambda (radio)
  2991. (when (equal (org-element-property :value radio) path) radio))
  2992. info 'first-match)))
  2993. ;;;; For Macros
  2994. ;;
  2995. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2996. (defun org-export-expand-macro (macro info)
  2997. "Expand MACRO and return it as a string.
  2998. INFO is a plist holding export options."
  2999. (let* ((key (org-element-property :key macro))
  3000. (args (org-element-property :args macro))
  3001. ;; User's macros are stored in the communication channel with
  3002. ;; a ":macro-" prefix. Replace arguments in VALUE. Also
  3003. ;; expand recursively macros within.
  3004. (value (org-export-data
  3005. (mapcar
  3006. (lambda (obj)
  3007. (if (not (stringp obj)) (org-export-data obj info)
  3008. (replace-regexp-in-string
  3009. "\\$[0-9]+"
  3010. (lambda (arg)
  3011. (nth (1- (string-to-number (substring arg 1))) args))
  3012. obj)))
  3013. (plist-get info (intern (format ":macro-%s" key))))
  3014. info)))
  3015. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  3016. (when (string-match "\\`(eval\\>" value) (setq value (eval (read value))))
  3017. ;; Return string.
  3018. (format "%s" (or value ""))))
  3019. ;;;; For References
  3020. ;;
  3021. ;; `org-export-get-ordinal' associates a sequence number to any object
  3022. ;; or element.
  3023. (defun org-export-get-ordinal (element info &optional types predicate)
  3024. "Return ordinal number of an element or object.
  3025. ELEMENT is the element or object considered. INFO is the plist
  3026. used as a communication channel.
  3027. Optional argument TYPES, when non-nil, is a list of element or
  3028. object types, as symbols, that should also be counted in.
  3029. Otherwise, only provided element's type is considered.
  3030. Optional argument PREDICATE is a function returning a non-nil
  3031. value if the current element or object should be counted in. It
  3032. accepts two arguments: the element or object being considered and
  3033. the plist used as a communication channel. This allows to count
  3034. only a certain type of objects (i.e. inline images).
  3035. Return value is a list of numbers if ELEMENT is an headline or an
  3036. item. It is nil for keywords. It represents the footnote number
  3037. for footnote definitions and footnote references. If ELEMENT is
  3038. a target, return the same value as if ELEMENT was the closest
  3039. table, item or headline containing the target. In any other
  3040. case, return the sequence number of ELEMENT among elements or
  3041. objects of the same type."
  3042. ;; A target keyword, representing an invisible target, never has
  3043. ;; a sequence number.
  3044. (unless (eq (org-element-type element) 'keyword)
  3045. ;; Ordinal of a target object refer to the ordinal of the closest
  3046. ;; table, item, or headline containing the object.
  3047. (when (eq (org-element-type element) 'target)
  3048. (setq element
  3049. (loop for parent in (org-export-get-genealogy element)
  3050. when
  3051. (memq
  3052. (org-element-type parent)
  3053. '(footnote-definition footnote-reference headline item
  3054. table))
  3055. return parent)))
  3056. (case (org-element-type element)
  3057. ;; Special case 1: An headline returns its number as a list.
  3058. (headline (org-export-get-headline-number element info))
  3059. ;; Special case 2: An item returns its number as a list.
  3060. (item (let ((struct (org-element-property :structure element)))
  3061. (org-list-get-item-number
  3062. (org-element-property :begin element)
  3063. struct
  3064. (org-list-prevs-alist struct)
  3065. (org-list-parents-alist struct))))
  3066. ((footnote-definition footnote-reference)
  3067. (org-export-get-footnote-number element info))
  3068. (otherwise
  3069. (let ((counter 0))
  3070. ;; Increment counter until ELEMENT is found again.
  3071. (org-element-map
  3072. (plist-get info :parse-tree) (or types (org-element-type element))
  3073. (lambda (el)
  3074. (cond
  3075. ((eq element el) (1+ counter))
  3076. ((not predicate) (incf counter) nil)
  3077. ((funcall predicate el info) (incf counter) nil)))
  3078. info 'first-match))))))
  3079. ;;;; For Src-Blocks
  3080. ;;
  3081. ;; `org-export-get-loc' counts number of code lines accumulated in
  3082. ;; src-block or example-block elements with a "+n" switch until
  3083. ;; a given element, excluded. Note: "-n" switches reset that count.
  3084. ;;
  3085. ;; `org-export-unravel-code' extracts source code (along with a code
  3086. ;; references alist) from an `element-block' or `src-block' type
  3087. ;; element.
  3088. ;;
  3089. ;; `org-export-format-code' applies a formatting function to each line
  3090. ;; of code, providing relative line number and code reference when
  3091. ;; appropriate. Since it doesn't access the original element from
  3092. ;; which the source code is coming, it expects from the code calling
  3093. ;; it to know if lines should be numbered and if code references
  3094. ;; should appear.
  3095. ;;
  3096. ;; Eventually, `org-export-format-code-default' is a higher-level
  3097. ;; function (it makes use of the two previous functions) which handles
  3098. ;; line numbering and code references inclusion, and returns source
  3099. ;; code in a format suitable for plain text or verbatim output.
  3100. (defun org-export-get-loc (element info)
  3101. "Return accumulated lines of code up to ELEMENT.
  3102. INFO is the plist used as a communication channel.
  3103. ELEMENT is excluded from count."
  3104. (let ((loc 0))
  3105. (org-element-map
  3106. (plist-get info :parse-tree)
  3107. `(src-block example-block ,(org-element-type element))
  3108. (lambda (el)
  3109. (cond
  3110. ;; ELEMENT is reached: Quit the loop.
  3111. ((eq el element))
  3112. ;; Only count lines from src-block and example-block elements
  3113. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3114. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3115. ((let ((linums (org-element-property :number-lines el)))
  3116. (when linums
  3117. ;; Accumulate locs or reset them.
  3118. (let ((lines (org-count-lines
  3119. (org-trim (org-element-property :value el)))))
  3120. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3121. ;; Return nil to stay in the loop.
  3122. nil)))
  3123. info 'first-match)
  3124. ;; Return value.
  3125. loc))
  3126. (defun org-export-unravel-code (element)
  3127. "Clean source code and extract references out of it.
  3128. ELEMENT has either a `src-block' an `example-block' type.
  3129. Return a cons cell whose CAR is the source code, cleaned from any
  3130. reference and protective comma and CDR is an alist between
  3131. relative line number (integer) and name of code reference on that
  3132. line (string)."
  3133. (let* ((line 0) refs
  3134. ;; Get code and clean it. Remove blank lines at its
  3135. ;; beginning and end. Also remove protective commas.
  3136. (code (let ((c (replace-regexp-in-string
  3137. "\\`\\([ \t]*\n\\)+" ""
  3138. (replace-regexp-in-string
  3139. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
  3140. (org-element-property :value element)))))
  3141. ;; If appropriate, remove global indentation.
  3142. (unless (or org-src-preserve-indentation
  3143. (org-element-property :preserve-indent element))
  3144. (setq c (org-remove-indentation c)))
  3145. ;; Free up the protected lines. Note: Org blocks
  3146. ;; have commas at the beginning or every line.
  3147. (if (string= (org-element-property :language element) "org")
  3148. (replace-regexp-in-string "^," "" c)
  3149. (replace-regexp-in-string
  3150. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  3151. ;; Get format used for references.
  3152. (label-fmt (regexp-quote
  3153. (or (org-element-property :label-fmt element)
  3154. org-coderef-label-format)))
  3155. ;; Build a regexp matching a loc with a reference.
  3156. (with-ref-re
  3157. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3158. (replace-regexp-in-string
  3159. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3160. ;; Return value.
  3161. (cons
  3162. ;; Code with references removed.
  3163. (org-element-normalize-string
  3164. (mapconcat
  3165. (lambda (loc)
  3166. (incf line)
  3167. (if (not (string-match with-ref-re loc)) loc
  3168. ;; Ref line: remove ref, and signal its position in REFS.
  3169. (push (cons line (match-string 3 loc)) refs)
  3170. (replace-match "" nil nil loc 1)))
  3171. (org-split-string code "\n") "\n"))
  3172. ;; Reference alist.
  3173. refs)))
  3174. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3175. "Format CODE by applying FUN line-wise and return it.
  3176. CODE is a string representing the code to format. FUN is
  3177. a function. It must accept three arguments: a line of
  3178. code (string), the current line number (integer) or nil and the
  3179. reference associated to the current line (string) or nil.
  3180. Optional argument NUM-LINES can be an integer representing the
  3181. number of code lines accumulated until the current code. Line
  3182. numbers passed to FUN will take it into account. If it is nil,
  3183. FUN's second argument will always be nil. This number can be
  3184. obtained with `org-export-get-loc' function.
  3185. Optional argument REF-ALIST can be an alist between relative line
  3186. number (i.e. ignoring NUM-LINES) and the name of the code
  3187. reference on it. If it is nil, FUN's third argument will always
  3188. be nil. It can be obtained through the use of
  3189. `org-export-unravel-code' function."
  3190. (let ((--locs (org-split-string code "\n"))
  3191. (--line 0))
  3192. (org-element-normalize-string
  3193. (mapconcat
  3194. (lambda (--loc)
  3195. (incf --line)
  3196. (let ((--ref (cdr (assq --line ref-alist))))
  3197. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  3198. --locs "\n"))))
  3199. (defun org-export-format-code-default (element info)
  3200. "Return source code from ELEMENT, formatted in a standard way.
  3201. ELEMENT is either a `src-block' or `example-block' element. INFO
  3202. is a plist used as a communication channel.
  3203. This function takes care of line numbering and code references
  3204. inclusion. Line numbers, when applicable, appear at the
  3205. beginning of the line, separated from the code by two white
  3206. spaces. Code references, on the other hand, appear flushed to
  3207. the right, separated by six white spaces from the widest line of
  3208. code."
  3209. ;; Extract code and references.
  3210. (let* ((code-info (org-export-unravel-code element))
  3211. (code (car code-info))
  3212. (code-lines (org-split-string code "\n"))
  3213. (refs (and (org-element-property :retain-labels element)
  3214. (cdr code-info)))
  3215. ;; Handle line numbering.
  3216. (num-start (case (org-element-property :number-lines element)
  3217. (continued (org-export-get-loc element info))
  3218. (new 0)))
  3219. (num-fmt
  3220. (and num-start
  3221. (format "%%%ds "
  3222. (length (number-to-string
  3223. (+ (length code-lines) num-start))))))
  3224. ;; Prepare references display, if required. Any reference
  3225. ;; should start six columns after the widest line of code,
  3226. ;; wrapped with parenthesis.
  3227. (max-width
  3228. (+ (apply 'max (mapcar 'length code-lines))
  3229. (if (not num-start) 0 (length (format num-fmt num-start))))))
  3230. (org-export-format-code
  3231. code
  3232. (lambda (loc line-num ref)
  3233. (let ((number-str (and num-fmt (format num-fmt line-num))))
  3234. (concat
  3235. number-str
  3236. loc
  3237. (and ref
  3238. (concat (make-string
  3239. (- (+ 6 max-width)
  3240. (+ (length loc) (length number-str))) ? )
  3241. (format "(%s)" ref))))))
  3242. num-start refs)))
  3243. ;;;; For Tables
  3244. ;;
  3245. ;; `org-export-table-has-special-column-p' and and
  3246. ;; `org-export-table-row-is-special-p' are predicates used to look for
  3247. ;; meta-information about the table structure.
  3248. ;;
  3249. ;; `org-table-has-header-p' tells when the rows before the first rule
  3250. ;; should be considered as table's header.
  3251. ;;
  3252. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  3253. ;; and `org-export-table-cell-borders' extract information from
  3254. ;; a table-cell element.
  3255. ;;
  3256. ;; `org-export-table-dimensions' gives the number on rows and columns
  3257. ;; in the table, ignoring horizontal rules and special columns.
  3258. ;; `org-export-table-cell-address', given a table-cell object, returns
  3259. ;; the absolute address of a cell. On the other hand,
  3260. ;; `org-export-get-table-cell-at' does the contrary.
  3261. ;;
  3262. ;; `org-export-table-cell-starts-colgroup-p',
  3263. ;; `org-export-table-cell-ends-colgroup-p',
  3264. ;; `org-export-table-row-starts-rowgroup-p',
  3265. ;; `org-export-table-row-ends-rowgroup-p',
  3266. ;; `org-export-table-row-starts-header-p' and
  3267. ;; `org-export-table-row-ends-header-p' indicate position of current
  3268. ;; row or cell within the table.
  3269. (defun org-export-table-has-special-column-p (table)
  3270. "Non-nil when TABLE has a special column.
  3271. All special columns will be ignored during export."
  3272. ;; The table has a special column when every first cell of every row
  3273. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  3274. ;; "*" "_" and "^". Though, do not consider a first row containing
  3275. ;; only empty cells as special.
  3276. (let ((special-column-p 'empty))
  3277. (catch 'exit
  3278. (mapc
  3279. (lambda (row)
  3280. (when (eq (org-element-property :type row) 'standard)
  3281. (let ((value (org-element-contents
  3282. (car (org-element-contents row)))))
  3283. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  3284. (setq special-column-p 'special))
  3285. ((not value))
  3286. (t (throw 'exit nil))))))
  3287. (org-element-contents table))
  3288. (eq special-column-p 'special))))
  3289. (defun org-export-table-has-header-p (table info)
  3290. "Non-nil when TABLE has an header.
  3291. INFO is a plist used as a communication channel.
  3292. A table has an header when it contains at least two row groups."
  3293. (let ((rowgroup 1) row-flag)
  3294. (org-element-map
  3295. table 'table-row
  3296. (lambda (row)
  3297. (cond
  3298. ((> rowgroup 1) t)
  3299. ((and row-flag (eq (org-element-property :type row) 'rule))
  3300. (incf rowgroup) (setq row-flag nil))
  3301. ((and (not row-flag) (eq (org-element-property :type row) 'standard))
  3302. (setq row-flag t) nil)))
  3303. info)))
  3304. (defun org-export-table-row-is-special-p (table-row info)
  3305. "Non-nil if TABLE-ROW is considered special.
  3306. INFO is a plist used as the communication channel.
  3307. All special rows will be ignored during export."
  3308. (when (eq (org-element-property :type table-row) 'standard)
  3309. (let ((first-cell (org-element-contents
  3310. (car (org-element-contents table-row)))))
  3311. ;; A row is special either when...
  3312. (or
  3313. ;; ... it starts with a field only containing "/",
  3314. (equal first-cell '("/"))
  3315. ;; ... the table contains a special column and the row start
  3316. ;; with a marking character among, "^", "_", "$" or "!",
  3317. (and (org-export-table-has-special-column-p
  3318. (org-export-get-parent table-row))
  3319. (member first-cell '(("^") ("_") ("$") ("!"))))
  3320. ;; ... it contains only alignment cookies and empty cells.
  3321. (let ((special-row-p 'empty))
  3322. (catch 'exit
  3323. (mapc
  3324. (lambda (cell)
  3325. (let ((value (org-element-contents cell)))
  3326. ;; Since VALUE is a secondary string, the following
  3327. ;; checks avoid expanding it with `org-export-data'.
  3328. (cond ((not value))
  3329. ((and (not (cdr value))
  3330. (stringp (car value))
  3331. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  3332. (car value)))
  3333. (setq special-row-p 'cookie))
  3334. (t (throw 'exit nil)))))
  3335. (org-element-contents table-row))
  3336. (eq special-row-p 'cookie)))))))
  3337. (defun org-export-table-row-group (table-row info)
  3338. "Return TABLE-ROW's group.
  3339. INFO is a plist used as the communication channel.
  3340. Return value is the group number, as an integer, or nil special
  3341. rows and table rules. Group 1 is also table's header."
  3342. (unless (or (eq (org-element-property :type table-row) 'rule)
  3343. (org-export-table-row-is-special-p table-row info))
  3344. (let ((group 0) row-flag)
  3345. (catch 'found
  3346. (mapc
  3347. (lambda (row)
  3348. (cond
  3349. ((and (eq (org-element-property :type row) 'standard)
  3350. (not (org-export-table-row-is-special-p row info)))
  3351. (unless row-flag (incf group) (setq row-flag t)))
  3352. ((eq (org-element-property :type row) 'rule)
  3353. (setq row-flag nil)))
  3354. (when (eq table-row row) (throw 'found group)))
  3355. (org-element-contents (org-export-get-parent table-row)))))))
  3356. (defun org-export-table-cell-width (table-cell info)
  3357. "Return TABLE-CELL contents width.
  3358. INFO is a plist used as the communication channel.
  3359. Return value is the width given by the last width cookie in the
  3360. same column as TABLE-CELL, or nil."
  3361. (let* ((row (org-export-get-parent table-cell))
  3362. (column (let ((cells (org-element-contents row)))
  3363. (- (length cells) (length (memq table-cell cells)))))
  3364. (table (org-export-get-parent-table table-cell))
  3365. cookie-width)
  3366. (mapc
  3367. (lambda (row)
  3368. (cond
  3369. ;; In a special row, try to find a width cookie at COLUMN.
  3370. ((org-export-table-row-is-special-p row info)
  3371. (let ((value (org-element-contents
  3372. (elt (org-element-contents row) column))))
  3373. ;; The following checks avoid expanding unnecessarily the
  3374. ;; cell with `org-export-data'
  3375. (when (and value
  3376. (not (cdr value))
  3377. (stringp (car value))
  3378. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
  3379. (match-string 1 (car value)))
  3380. (setq cookie-width
  3381. (string-to-number (match-string 1 (car value)))))))
  3382. ;; Ignore table rules.
  3383. ((eq (org-element-property :type row) 'rule))))
  3384. (org-element-contents table))
  3385. ;; Return value.
  3386. cookie-width))
  3387. (defun org-export-table-cell-alignment (table-cell info)
  3388. "Return TABLE-CELL contents alignment.
  3389. INFO is a plist used as the communication channel.
  3390. Return alignment as specified by the last alignment cookie in the
  3391. same column as TABLE-CELL. If no such cookie is found, a default
  3392. alignment value will be deduced from fraction of numbers in the
  3393. column (see `org-table-number-fraction' for more information).
  3394. Possible values are `left', `right' and `center'."
  3395. (let* ((row (org-export-get-parent table-cell))
  3396. (column (let ((cells (org-element-contents row)))
  3397. (- (length cells) (length (memq table-cell cells)))))
  3398. (table (org-export-get-parent-table table-cell))
  3399. (number-cells 0)
  3400. (total-cells 0)
  3401. cookie-align)
  3402. (mapc
  3403. (lambda (row)
  3404. (cond
  3405. ;; In a special row, try to find an alignment cookie at
  3406. ;; COLUMN.
  3407. ((org-export-table-row-is-special-p row info)
  3408. (let ((value (org-element-contents
  3409. (elt (org-element-contents row) column))))
  3410. ;; Since VALUE is a secondary string, the following checks
  3411. ;; avoid useless expansion through `org-export-data'.
  3412. (when (and value
  3413. (not (cdr value))
  3414. (stringp (car value))
  3415. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  3416. (car value))
  3417. (match-string 1 (car value)))
  3418. (setq cookie-align (match-string 1 (car value))))))
  3419. ;; Ignore table rules.
  3420. ((eq (org-element-property :type row) 'rule))
  3421. ;; In a standard row, check if cell's contents are expressing
  3422. ;; some kind of number. Increase NUMBER-CELLS accordingly.
  3423. ;; Though, don't bother if an alignment cookie has already
  3424. ;; defined cell's alignment.
  3425. ((not cookie-align)
  3426. (let ((value (org-export-data
  3427. (org-element-contents
  3428. (elt (org-element-contents row) column))
  3429. info)))
  3430. (incf total-cells)
  3431. (when (string-match org-table-number-regexp value)
  3432. (incf number-cells))))))
  3433. (org-element-contents table))
  3434. ;; Return value. Alignment specified by cookies has precedence
  3435. ;; over alignment deduced from cells contents.
  3436. (cond ((equal cookie-align "l") 'left)
  3437. ((equal cookie-align "r") 'right)
  3438. ((equal cookie-align "c") 'center)
  3439. ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
  3440. 'right)
  3441. (t 'left))))
  3442. (defun org-export-table-cell-borders (table-cell info)
  3443. "Return TABLE-CELL borders.
  3444. INFO is a plist used as a communication channel.
  3445. Return value is a list of symbols, or nil. Possible values are:
  3446. `top', `bottom', `above', `below', `left' and `right'. Note:
  3447. `top' (resp. `bottom') only happen for a cell in the first
  3448. row (resp. last row) of the table, ignoring table rules, if any.
  3449. Returned borders ignore special rows."
  3450. (let* ((row (org-export-get-parent table-cell))
  3451. (table (org-export-get-parent-table table-cell))
  3452. borders)
  3453. ;; Top/above border? TABLE-CELL has a border above when a rule
  3454. ;; used to demarcate row groups can be found above. Hence,
  3455. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  3456. ;; another regular row has to be found above that rule.
  3457. (let (rule-flag)
  3458. (catch 'exit
  3459. (mapc (lambda (row)
  3460. (cond ((eq (org-element-property :type row) 'rule)
  3461. (setq rule-flag t))
  3462. ((not (org-export-table-row-is-special-p row info))
  3463. (if rule-flag (throw 'exit (push 'above borders))
  3464. (throw 'exit nil)))))
  3465. ;; Look at every row before the current one.
  3466. (cdr (memq row (reverse (org-element-contents table)))))
  3467. ;; No rule above, or rule found starts the table (ignoring any
  3468. ;; special row): TABLE-CELL is at the top of the table.
  3469. (when rule-flag (push 'above borders))
  3470. (push 'top borders)))
  3471. ;; Bottom/below border? TABLE-CELL has a border below when next
  3472. ;; non-regular row below is a rule.
  3473. (let (rule-flag)
  3474. (catch 'exit
  3475. (mapc (lambda (row)
  3476. (cond ((eq (org-element-property :type row) 'rule)
  3477. (setq rule-flag t))
  3478. ((not (org-export-table-row-is-special-p row info))
  3479. (if rule-flag (throw 'exit (push 'below borders))
  3480. (throw 'exit nil)))))
  3481. ;; Look at every row after the current one.
  3482. (cdr (memq row (org-element-contents table))))
  3483. ;; No rule below, or rule found ends the table (modulo some
  3484. ;; special row): TABLE-CELL is at the bottom of the table.
  3485. (when rule-flag (push 'below borders))
  3486. (push 'bottom borders)))
  3487. ;; Right/left borders? They can only be specified by column
  3488. ;; groups. Column groups are defined in a row starting with "/".
  3489. ;; Also a column groups row only contains "<", "<>", ">" or blank
  3490. ;; cells.
  3491. (catch 'exit
  3492. (let ((column (let ((cells (org-element-contents row)))
  3493. (- (length cells) (length (memq table-cell cells))))))
  3494. (mapc
  3495. (lambda (row)
  3496. (unless (eq (org-element-property :type row) 'rule)
  3497. (when (equal (org-element-contents
  3498. (car (org-element-contents row)))
  3499. '("/"))
  3500. (let ((column-groups
  3501. (mapcar
  3502. (lambda (cell)
  3503. (let ((value (org-element-contents cell)))
  3504. (when (member value '(("<") ("<>") (">") nil))
  3505. (car value))))
  3506. (org-element-contents row))))
  3507. ;; There's a left border when previous cell, if
  3508. ;; any, ends a group, or current one starts one.
  3509. (when (or (and (not (zerop column))
  3510. (member (elt column-groups (1- column))
  3511. '(">" "<>")))
  3512. (member (elt column-groups column) '("<" "<>")))
  3513. (push 'left borders))
  3514. ;; There's a right border when next cell, if any,
  3515. ;; starts a group, or current one ends one.
  3516. (when (or (and (/= (1+ column) (length column-groups))
  3517. (member (elt column-groups (1+ column))
  3518. '("<" "<>")))
  3519. (member (elt column-groups column) '(">" "<>")))
  3520. (push 'right borders))
  3521. (throw 'exit nil)))))
  3522. ;; Table rows are read in reverse order so last column groups
  3523. ;; row has precedence over any previous one.
  3524. (reverse (org-element-contents table)))))
  3525. ;; Return value.
  3526. borders))
  3527. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  3528. "Non-nil when TABLE-CELL is at the beginning of a row group.
  3529. INFO is a plist used as a communication channel."
  3530. ;; A cell starts a column group either when it is at the beginning
  3531. ;; of a row (or after the special column, if any) or when it has
  3532. ;; a left border.
  3533. (or (eq (org-element-map
  3534. (org-export-get-parent table-cell)
  3535. 'table-cell 'identity info 'first-match)
  3536. table-cell)
  3537. (memq 'left (org-export-table-cell-borders table-cell info))))
  3538. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  3539. "Non-nil when TABLE-CELL is at the end of a row group.
  3540. INFO is a plist used as a communication channel."
  3541. ;; A cell ends a column group either when it is at the end of a row
  3542. ;; or when it has a right border.
  3543. (or (eq (car (last (org-element-contents
  3544. (org-export-get-parent table-cell))))
  3545. table-cell)
  3546. (memq 'right (org-export-table-cell-borders table-cell info))))
  3547. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  3548. "Non-nil when TABLE-ROW is at the beginning of a column group.
  3549. INFO is a plist used as a communication channel."
  3550. (unless (or (eq (org-element-property :type table-row) 'rule)
  3551. (org-export-table-row-is-special-p table-row info))
  3552. (let ((borders (org-export-table-cell-borders
  3553. (car (org-element-contents table-row)) info)))
  3554. (or (memq 'top borders) (memq 'above borders)))))
  3555. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  3556. "Non-nil when TABLE-ROW is at the end of a column group.
  3557. INFO is a plist used as a communication channel."
  3558. (unless (or (eq (org-element-property :type table-row) 'rule)
  3559. (org-export-table-row-is-special-p table-row info))
  3560. (let ((borders (org-export-table-cell-borders
  3561. (car (org-element-contents table-row)) info)))
  3562. (or (memq 'bottom borders) (memq 'below borders)))))
  3563. (defun org-export-table-row-starts-header-p (table-row info)
  3564. "Non-nil when TABLE-ROW is the first table header's row.
  3565. INFO is a plist used as a communication channel."
  3566. (and (org-export-table-has-header-p
  3567. (org-export-get-parent-table table-row) info)
  3568. (org-export-table-row-starts-rowgroup-p table-row info)
  3569. (= (org-export-table-row-group table-row info) 1)))
  3570. (defun org-export-table-row-ends-header-p (table-row info)
  3571. "Non-nil when TABLE-ROW is the last table header's row.
  3572. INFO is a plist used as a communication channel."
  3573. (and (org-export-table-has-header-p
  3574. (org-export-get-parent-table table-row) info)
  3575. (org-export-table-row-ends-rowgroup-p table-row info)
  3576. (= (org-export-table-row-group table-row info) 1)))
  3577. (defun org-export-table-dimensions (table info)
  3578. "Return TABLE dimensions.
  3579. INFO is a plist used as a communication channel.
  3580. Return value is a CONS like (ROWS . COLUMNS) where
  3581. ROWS (resp. COLUMNS) is the number of exportable
  3582. rows (resp. columns)."
  3583. (let (first-row (columns 0) (rows 0))
  3584. ;; Set number of rows, and extract first one.
  3585. (org-element-map
  3586. table 'table-row
  3587. (lambda (row)
  3588. (when (eq (org-element-property :type row) 'standard)
  3589. (incf rows)
  3590. (unless first-row (setq first-row row)))) info)
  3591. ;; Set number of columns.
  3592. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  3593. ;; Return value.
  3594. (cons rows columns)))
  3595. (defun org-export-table-cell-address (table-cell info)
  3596. "Return address of a regular TABLE-CELL object.
  3597. TABLE-CELL is the cell considered. INFO is a plist used as
  3598. a communication channel.
  3599. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3600. zero-based index. Only exportable cells are considered. The
  3601. function returns nil for other cells."
  3602. (let* ((table-row (org-export-get-parent table-cell))
  3603. (table (org-export-get-parent-table table-cell)))
  3604. ;; Ignore cells in special rows or in special column.
  3605. (unless (or (org-export-table-row-is-special-p table-row info)
  3606. (and (org-export-table-has-special-column-p table)
  3607. (eq (car (org-element-contents table-row)) table-cell)))
  3608. (cons
  3609. ;; Row number.
  3610. (let ((row-count 0))
  3611. (org-element-map
  3612. table 'table-row
  3613. (lambda (row)
  3614. (cond ((eq (org-element-property :type row) 'rule) nil)
  3615. ((eq row table-row) row-count)
  3616. (t (incf row-count) nil)))
  3617. info 'first-match))
  3618. ;; Column number.
  3619. (let ((col-count 0))
  3620. (org-element-map
  3621. table-row 'table-cell
  3622. (lambda (cell)
  3623. (if (eq cell table-cell) col-count (incf col-count) nil))
  3624. info 'first-match))))))
  3625. (defun org-export-get-table-cell-at (address table info)
  3626. "Return regular table-cell object at ADDRESS in TABLE.
  3627. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  3628. zero-based index. TABLE is a table type element. INFO is
  3629. a plist used as a communication channel.
  3630. If no table-cell, among exportable cells, is found at ADDRESS,
  3631. return nil."
  3632. (let ((column-pos (cdr address)) (column-count 0))
  3633. (org-element-map
  3634. ;; Row at (car address) or nil.
  3635. (let ((row-pos (car address)) (row-count 0))
  3636. (org-element-map
  3637. table 'table-row
  3638. (lambda (row)
  3639. (cond ((eq (org-element-property :type row) 'rule) nil)
  3640. ((= row-count row-pos) row)
  3641. (t (incf row-count) nil)))
  3642. info 'first-match))
  3643. 'table-cell
  3644. (lambda (cell)
  3645. (if (= column-count column-pos) cell
  3646. (incf column-count) nil))
  3647. info 'first-match)))
  3648. ;;;; For Tables Of Contents
  3649. ;;
  3650. ;; `org-export-collect-headlines' builds a list of all exportable
  3651. ;; headline elements, maybe limited to a certain depth. One can then
  3652. ;; easily parse it and transcode it.
  3653. ;;
  3654. ;; Building lists of tables, figures or listings is quite similar.
  3655. ;; Once the generic function `org-export-collect-elements' is defined,
  3656. ;; `org-export-collect-tables', `org-export-collect-figures' and
  3657. ;; `org-export-collect-listings' can be derived from it.
  3658. (defun org-export-collect-headlines (info &optional n)
  3659. "Collect headlines in order to build a table of contents.
  3660. INFO is a plist used as a communication channel.
  3661. When optional argument N is an integer, it specifies the depth of
  3662. the table of contents. Otherwise, it is set to the value of the
  3663. last headline level. See `org-export-headline-levels' for more
  3664. information.
  3665. Return a list of all exportable headlines as parsed elements."
  3666. (unless (wholenump n) (setq n (plist-get info :headline-levels)))
  3667. (org-element-map
  3668. (plist-get info :parse-tree)
  3669. 'headline
  3670. (lambda (headline)
  3671. ;; Strip contents from HEADLINE.
  3672. (let ((relative-level (org-export-get-relative-level headline info)))
  3673. (unless (> relative-level n) headline)))
  3674. info))
  3675. (defun org-export-collect-elements (type info &optional predicate)
  3676. "Collect referenceable elements of a determined type.
  3677. TYPE can be a symbol or a list of symbols specifying element
  3678. types to search. Only elements with a caption are collected.
  3679. INFO is a plist used as a communication channel.
  3680. When non-nil, optional argument PREDICATE is a function accepting
  3681. one argument, an element of type TYPE. It returns a non-nil
  3682. value when that element should be collected.
  3683. Return a list of all elements found, in order of appearance."
  3684. (org-element-map
  3685. (plist-get info :parse-tree) type
  3686. (lambda (element)
  3687. (and (org-element-property :caption element)
  3688. (or (not predicate) (funcall predicate element))
  3689. element))
  3690. info))
  3691. (defun org-export-collect-tables (info)
  3692. "Build a list of tables.
  3693. INFO is a plist used as a communication channel.
  3694. Return a list of table elements with a caption."
  3695. (org-export-collect-elements 'table info))
  3696. (defun org-export-collect-figures (info predicate)
  3697. "Build a list of figures.
  3698. INFO is a plist used as a communication channel. PREDICATE is
  3699. a function which accepts one argument: a paragraph element and
  3700. whose return value is non-nil when that element should be
  3701. collected.
  3702. A figure is a paragraph type element, with a caption, verifying
  3703. PREDICATE. The latter has to be provided since a \"figure\" is
  3704. a vague concept that may depend on back-end.
  3705. Return a list of elements recognized as figures."
  3706. (org-export-collect-elements 'paragraph info predicate))
  3707. (defun org-export-collect-listings (info)
  3708. "Build a list of src blocks.
  3709. INFO is a plist used as a communication channel.
  3710. Return a list of src-block elements with a caption."
  3711. (org-export-collect-elements 'src-block info))
  3712. ;;;; Topology
  3713. ;;
  3714. ;; Here are various functions to retrieve information about the
  3715. ;; neighbourhood of a given element or object. Neighbours of interest
  3716. ;; are direct parent (`org-export-get-parent'), parent headline
  3717. ;; (`org-export-get-parent-headline'), first element containing an
  3718. ;; object, (`org-export-get-parent-element'), parent table
  3719. ;; (`org-export-get-parent-table'), previous element or object
  3720. ;; (`org-export-get-previous-element') and next element or object
  3721. ;; (`org-export-get-next-element').
  3722. ;;
  3723. ;; `org-export-get-genealogy' returns the full genealogy of a given
  3724. ;; element or object, from closest parent to full parse tree.
  3725. (defun org-export-get-parent (blob)
  3726. "Return BLOB parent or nil.
  3727. BLOB is the element or object considered."
  3728. (org-element-property :parent blob))
  3729. (defun org-export-get-genealogy (blob)
  3730. "Return full genealogy relative to a given element or object.
  3731. BLOB is the element or object being considered.
  3732. Ancestors are returned from closest to farthest, the last one
  3733. being the full parse tree."
  3734. (let (genealogy (parent blob))
  3735. (while (setq parent (org-element-property :parent parent))
  3736. (push parent genealogy))
  3737. (nreverse genealogy)))
  3738. (defun org-export-get-parent-headline (blob)
  3739. "Return BLOB parent headline or nil.
  3740. BLOB is the element or object being considered."
  3741. (let ((parent blob))
  3742. (while (and (setq parent (org-element-property :parent parent))
  3743. (not (eq (org-element-type parent) 'headline))))
  3744. parent))
  3745. (defun org-export-get-parent-element (object)
  3746. "Return first element containing OBJECT or nil.
  3747. OBJECT is the object to consider."
  3748. (let ((parent object))
  3749. (while (and (setq parent (org-element-property :parent parent))
  3750. (memq (org-element-type parent) org-element-all-objects)))
  3751. parent))
  3752. (defun org-export-get-parent-table (object)
  3753. "Return OBJECT parent table or nil.
  3754. OBJECT is either a `table-cell' or `table-element' type object."
  3755. (let ((parent object))
  3756. (while (and (setq parent (org-element-property :parent parent))
  3757. (not (eq (org-element-type parent) 'table))))
  3758. parent))
  3759. (defun org-export-get-previous-element (blob info)
  3760. "Return previous element or object.
  3761. BLOB is an element or object. INFO is a plist used as
  3762. a communication channel. Return previous exportable element or
  3763. object, a string, or nil."
  3764. (let (prev)
  3765. (catch 'exit
  3766. (mapc (lambda (obj)
  3767. (cond ((eq obj blob) (throw 'exit prev))
  3768. ((memq obj (plist-get info :ignore-list)))
  3769. (t (setq prev obj))))
  3770. (org-element-contents (org-export-get-parent blob))))))
  3771. (defun org-export-get-next-element (blob info)
  3772. "Return next element or object.
  3773. BLOB is an element or object. INFO is a plist used as
  3774. a communication channel. Return next exportable element or
  3775. object, a string, or nil."
  3776. (catch 'found
  3777. (mapc (lambda (obj)
  3778. (unless (memq obj (plist-get info :ignore-list))
  3779. (throw 'found obj)))
  3780. (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
  3781. nil))
  3782. ;;;; Translation
  3783. ;;
  3784. ;; `org-export-translate' translates a string according to language
  3785. ;; specified by LANGUAGE keyword or `org-export-language-setup'
  3786. ;; variable and a specified charset. `org-export-dictionary' contains
  3787. ;; the dictionary used for the translation.
  3788. (defconst org-export-dictionary
  3789. '(("Author"
  3790. ("fr"
  3791. :ascii "Auteur"
  3792. :latin1 "Auteur"
  3793. :utf-8 "Auteur"))
  3794. ("Date"
  3795. ("fr"
  3796. :ascii "Date"
  3797. :latin1 "Date"
  3798. :utf-8 "Date"))
  3799. ("Equation")
  3800. ("Figure")
  3801. ("Footnotes"
  3802. ("fr"
  3803. :ascii "Notes de bas de page"
  3804. :latin1 "Notes de bas de page"
  3805. :utf-8 "Notes de bas de page"))
  3806. ("List of Listings"
  3807. ("fr"
  3808. :ascii "Liste des programmes"
  3809. :latin1 "Liste des programmes"
  3810. :utf-8 "Liste des programmes"))
  3811. ("List of Tables"
  3812. ("fr"
  3813. :ascii "Liste des tableaux"
  3814. :latin1 "Liste des tableaux"
  3815. :utf-8 "Liste des tableaux"))
  3816. ("Listing %d:"
  3817. ("fr"
  3818. :ascii "Programme %d :"
  3819. :latin1 "Programme %d :"
  3820. :utf-8 "Programme nº %d :"))
  3821. ("Listing %d: %s"
  3822. ("fr"
  3823. :ascii "Programme %d : %s"
  3824. :latin1 "Programme %d : %s"
  3825. :utf-8 "Programme nº %d : %s"))
  3826. ("See section %s"
  3827. ("fr"
  3828. :ascii "cf. section %s"
  3829. :latin1 "cf. section %s"
  3830. :utf-8 "cf. section %s"))
  3831. ("Table %d:"
  3832. ("fr"
  3833. :ascii "Tableau %d :"
  3834. :latin1 "Tableau %d :"
  3835. :utf-8 "Tableau nº %d :"))
  3836. ("Table %d: %s"
  3837. ("fr"
  3838. :ascii "Tableau %d : %s"
  3839. :latin1 "Tableau %d : %s"
  3840. :utf-8 "Tableau nº %d : %s"))
  3841. ("Table of Contents"
  3842. ("fr"
  3843. :ascii "Sommaire"
  3844. :latin1 "Table des matières"
  3845. :utf-8 "Table des matières"))
  3846. ("Unknown reference"
  3847. ("fr"
  3848. :ascii "Destination inconnue"
  3849. :latin1 "Référence inconnue"
  3850. :utf-8 "Référence inconnue")))
  3851. "Dictionary for export engine.
  3852. Alist whose CAR is the string to translate and CDR is an alist
  3853. whose CAR is the language string and CDR is a plist whose
  3854. properties are possible charsets and values translated terms.
  3855. It is used as a database for `org-export-translate'. Since this
  3856. function returns the string as-is if no translation was found,
  3857. the variable only needs to record values different from the
  3858. entry.")
  3859. (defun org-export-translate (s encoding info)
  3860. "Translate string S according to language specification.
  3861. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  3862. and `:utf-8'. INFO is a plist used as a communication channel.
  3863. Translation depends on `:language' property. Return the
  3864. translated string. If no translation is found return S."
  3865. (let ((lang (plist-get info :language))
  3866. (translations (cdr (assoc s org-export-dictionary))))
  3867. (or (plist-get (cdr (assoc lang translations)) encoding) s)))
  3868. ;;; The Dispatcher
  3869. ;;
  3870. ;; `org-export-dispatch' is the standard interactive way to start an
  3871. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  3872. ;; for its interface. Most commons back-ends should have an entry in
  3873. ;; it.
  3874. ;;;###autoload
  3875. (defun org-export-dispatch ()
  3876. "Export dispatcher for Org mode.
  3877. It provides an access to common export related tasks in a buffer.
  3878. Its interface comes in two flavours: standard and expert. While
  3879. both share the same set of bindings, only the former displays the
  3880. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  3881. to switch to one or the other.
  3882. Return an error if key pressed has no associated command."
  3883. (interactive)
  3884. (let* ((input (org-export-dispatch-ui
  3885. (if (listp org-export-initial-scope) org-export-initial-scope
  3886. (list org-export-initial-scope))
  3887. org-export-dispatch-use-expert-ui))
  3888. (raw-key (car input))
  3889. (optns (cdr input)))
  3890. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  3891. ;; depending on user's key pressed.
  3892. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  3893. ;; Allow to quit with "q" key.
  3894. (?q nil)
  3895. ;; Export with `e-ascii' back-end.
  3896. ((?A ?N ?U)
  3897. (org-e-ascii-export-as-ascii
  3898. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3899. `(:ascii-charset ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8)))))
  3900. ((?a ?n ?u)
  3901. (org-e-ascii-export-to-ascii
  3902. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  3903. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  3904. ;; Export with `e-latex' back-end.
  3905. (?L (org-e-latex-export-as-latex
  3906. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3907. (?l
  3908. (org-e-latex-export-to-latex
  3909. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3910. (?p
  3911. (org-e-latex-export-to-pdf
  3912. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3913. (?d
  3914. (org-open-file
  3915. (org-e-latex-export-to-pdf
  3916. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3917. ;; Export with `e-html' back-end.
  3918. (?H
  3919. (org-e-html-export-as-html
  3920. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3921. (?h
  3922. (org-e-html-export-to-html
  3923. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3924. (?b
  3925. (org-open-file
  3926. (org-e-html-export-to-html
  3927. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3928. ;; Export with `e-odt' back-end.
  3929. (?o
  3930. (org-e-odt-export-to-odt
  3931. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  3932. (?O
  3933. (org-open-file
  3934. (org-e-odt-export-to-odt
  3935. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  3936. ;; Publishing facilities
  3937. (?F
  3938. (org-e-publish-current-file (memq 'force optns)))
  3939. (?P
  3940. (org-e-publish-current-project (memq 'force optns)))
  3941. (?X
  3942. (let ((project
  3943. (assoc (org-icompleting-read
  3944. "Publish project: " org-e-publish-project-alist nil t)
  3945. org-e-publish-project-alist)))
  3946. (org-e-publish project (memq 'force optns))))
  3947. (?E
  3948. (org-e-publish-all (memq 'force optns)))
  3949. ;; Undefined command.
  3950. (t (error "No command associated with key %s"
  3951. (char-to-string raw-key))))))
  3952. (defun org-export-dispatch-ui (options expertp)
  3953. "Handle interface for `org-export-dispatch'.
  3954. OPTIONS is a list containing current interactive options set for
  3955. export. It can contain any of the following symbols:
  3956. `body' toggles a body-only export
  3957. `subtree' restricts export to current subtree
  3958. `visible' restricts export to visible part of buffer.
  3959. `force' force publishing files.
  3960. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  3961. buffer is provided, but indications about currently active
  3962. options are given in the prompt. Moreover, \[?] allows to switch
  3963. back to standard interface.
  3964. Return value is a list with key pressed as CAR and a list of
  3965. final interactive export options as CDR."
  3966. (let ((help
  3967. (format "---- (Options) -------------------------------------------
  3968. \[1] Body only: %s [2] Export scope: %s
  3969. \[3] Visible only: %s [4] Force publishing: %s
  3970. --- (ASCII/Latin-1/UTF-8 Export) -------------------------
  3971. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  3972. --- (HTML Export) ----------------------------------------
  3973. \[h] to HTML file [b] ... and open it
  3974. \[H] to temporary buffer
  3975. --- (LaTeX Export) ---------------------------------------
  3976. \[l] to TEX file [L] to temporary buffer
  3977. \[p] to PDF file [d] ... and open it
  3978. --- (ODF Export) -----------------------------------------
  3979. \[o] to ODT file [O] ... and open it
  3980. --- (Publish) --------------------------------------------
  3981. \[F] current file [P] current project
  3982. \[X] a project [E] every project"
  3983. (if (memq 'body options) "On " "Off")
  3984. (if (memq 'subtree options) "Subtree" "Buffer ")
  3985. (if (memq 'visible options) "On " "Off")
  3986. (if (memq 'force options) "On " "Off")))
  3987. (standard-prompt "Export command: ")
  3988. (expert-prompt (format "Export command (%s%s%s%s): "
  3989. (if (memq 'body options) "b" "-")
  3990. (if (memq 'subtree options) "s" "-")
  3991. (if (memq 'visible options) "v" "-")
  3992. (if (memq 'force options) "f" "-")))
  3993. (handle-keypress
  3994. (function
  3995. ;; Read a character from command input, toggling interactive
  3996. ;; options when applicable. PROMPT is the displayed prompt,
  3997. ;; as a string.
  3998. (lambda (prompt)
  3999. (let ((key (read-char-exclusive prompt)))
  4000. (cond
  4001. ;; Ignore non-standard characters (i.e. "M-a").
  4002. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  4003. ;; Help key: Switch back to standard interface if
  4004. ;; expert UI was active.
  4005. ((eq key ??) (org-export-dispatch-ui options nil))
  4006. ;; Toggle export options.
  4007. ((memq key '(?1 ?2 ?3 ?4))
  4008. (org-export-dispatch-ui
  4009. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  4010. (?4 'force))))
  4011. (if (memq option options) (remq option options)
  4012. (cons option options)))
  4013. expertp))
  4014. ;; Action selected: Send key and options back to
  4015. ;; `org-export-dispatch'.
  4016. (t (cons key options))))))))
  4017. ;; With expert UI, just read key with a fancy prompt. In standard
  4018. ;; UI, display an intrusive help buffer.
  4019. (if expertp (funcall handle-keypress expert-prompt)
  4020. (save-window-excursion
  4021. (delete-other-windows)
  4022. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  4023. (org-fit-window-to-buffer
  4024. (get-buffer-window "*Org Export/Publishing Help*"))
  4025. (funcall handle-keypress standard-prompt)))))
  4026. (provide 'org-export)
  4027. ;;; org-export.el ends here