org-export.el 175 KB

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