org-export.el 177 KB

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