test-ox.el 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402
  1. ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2016 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  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. ;;; Code:
  16. (require 'cl-lib)
  17. (unless (featurep 'ox)
  18. (signal 'missing-test-dependency "org-export"))
  19. (defun org-test-default-backend ()
  20. "Return a default export back-end.
  21. This back-end simply returns parsed data as Org syntax."
  22. (org-export-create-backend
  23. :transcoders
  24. (mapcar (lambda (type)
  25. (cons type
  26. (lambda (o c _)
  27. (funcall
  28. (intern (format "org-element-%s-interpreter" type))
  29. o c))))
  30. (append org-element-all-elements org-element-all-objects))))
  31. (defmacro org-test-with-parsed-data (data &rest body)
  32. "Execute body with parsed data available.
  33. DATA is a string containing the data to be parsed. BODY is the
  34. body to execute. Parse tree is available under the `tree'
  35. variable, and communication channel under `info'."
  36. (declare (debug (form body)) (indent 1))
  37. `(org-test-with-temp-text ,data
  38. (org-export--delete-comments)
  39. (let* ((tree (org-element-parse-buffer))
  40. (info (org-combine-plists
  41. (org-export--get-export-attributes)
  42. (org-export-get-environment))))
  43. (org-export--prune-tree tree info)
  44. (org-export--remove-uninterpreted-data tree info)
  45. (let ((info (org-combine-plists
  46. info (org-export--collect-tree-properties tree info))))
  47. ,@body))))
  48. ;;; Internal Tests
  49. (ert-deftest test-org-export/bind-keyword ()
  50. "Test reading #+BIND: keywords."
  51. ;; Test with `org-export-allow-bind-keywords' set to t.
  52. (should
  53. (org-test-with-temp-text "#+BIND: test-ox-var value"
  54. (let ((org-export-allow-bind-keywords t))
  55. (org-export-get-environment)
  56. (eq test-ox-var 'value))))
  57. ;; Test with `org-export-allow-bind-keywords' set to nil.
  58. (should-not
  59. (org-test-with-temp-text "#+BIND: test-ox-var value"
  60. (let ((org-export-allow-bind-keywords nil))
  61. (org-export-get-environment)
  62. (boundp 'test-ox-var))))
  63. ;; BIND keywords are case-insensitive.
  64. (should
  65. (org-test-with-temp-text "#+bind: test-ox-var value"
  66. (let ((org-export-allow-bind-keywords t))
  67. (org-export-get-environment)
  68. (eq test-ox-var 'value))))
  69. ;; Preserve order of BIND keywords.
  70. (should
  71. (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
  72. (let ((org-export-allow-bind-keywords t))
  73. (org-export-get-environment)
  74. (eq test-ox-var 2))))
  75. ;; Read BIND keywords in setup files.
  76. (should
  77. (org-test-with-temp-text
  78. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  79. (let ((org-export-allow-bind-keywords t))
  80. (org-export-get-environment)
  81. (eq variable 'value))))
  82. ;; Verify that bound variables are seen during export.
  83. (should
  84. (equal "Yes\n"
  85. (org-test-with-temp-text "#+BIND: test-ox-var value"
  86. (let ((org-export-allow-bind-keywords t))
  87. (org-export-as
  88. (org-export-create-backend
  89. :transcoders
  90. '((section . (lambda (s c i)
  91. (if (eq test-ox-var 'value) "Yes" "No")))))))))))
  92. (ert-deftest test-org-export/parse-option-keyword ()
  93. "Test reading all standard #+OPTIONS: items."
  94. (should
  95. (let ((options
  96. (org-export--parse-option-keyword
  97. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t \
  98. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil \
  99. stat:t title:t")))
  100. (and (eq (plist-get options :headline-levels) 1)
  101. (eq (plist-get options :section-numbers) t)
  102. (eq (plist-get options :preserve-breaks) t)
  103. (eq (plist-get options :time-stamp-file) t)
  104. (eq (plist-get options :with-archived-trees) t)
  105. (eq (plist-get options :with-author) t)
  106. (eq (plist-get options :with-drawers) t)
  107. (eq (plist-get options :with-email) t)
  108. (eq (plist-get options :with-emphasize) t)
  109. (eq (plist-get options :with-entities) t)
  110. (eq (plist-get options :with-fixed-width) t)
  111. (eq (plist-get options :with-footnotes) t)
  112. (eq (plist-get options :with-priority) t)
  113. (eq (plist-get options :with-special-strings) t)
  114. (eq (plist-get options :with-sub-superscript) t)
  115. (eq (plist-get options :with-toc) t)
  116. (eq (plist-get options :with-tables) t)
  117. (eq (plist-get options :with-tags) t)
  118. (eq (plist-get options :with-tasks) t)
  119. (eq (plist-get options :with-timestamps) t)
  120. (eq (plist-get options :with-todo-keywords) t)
  121. (eq (plist-get options :with-inlinetasks) nil)
  122. (eq (plist-get options :with-statistics-cookies) t)
  123. (eq (plist-get options :with-title) t))))
  124. ;; Test some special values.
  125. (should
  126. (let ((options
  127. (org-export--parse-option-keyword
  128. "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo \
  129. num:2 <:active")))
  130. (and (eq (plist-get options :with-archived-trees) 'headline)
  131. (eq (plist-get options :with-sub-superscript) '{})
  132. (eq (plist-get options :with-toc) 1)
  133. (eq (plist-get options :with-tags) 'not-in-toc)
  134. (eq (plist-get options :with-tasks) 'todo)
  135. (eq (plist-get options :section-numbers) 2)
  136. (eq (plist-get options :with-timestamps) 'active)
  137. (equal (plist-get options :with-drawers) '("TEST")))))
  138. ;; Test back-end specific values.
  139. (should
  140. (equal
  141. (org-export--parse-option-keyword
  142. "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
  143. '(:option t)))
  144. ;; More than one property can refer to the same option item.
  145. (should
  146. (equal '(:opt1 t :opt2 t)
  147. (org-export--parse-option-keyword
  148. "opt:t"
  149. (org-export-create-backend
  150. :options '((:opt1 nil "opt") (:opt2 nil "opt")))))))
  151. (ert-deftest test-org-export/get-inbuffer-options ()
  152. "Test reading all standard export keywords."
  153. ;; Properties should follow buffer order.
  154. (should
  155. (equal
  156. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  157. (org-export--get-inbuffer-options))
  158. '(:language "fr" :creator "Me" :email "email")))
  159. ;; Test `space' behaviour.
  160. (should
  161. (equal
  162. (let ((back-end (org-export-create-backend
  163. :options '((:keyword "KEYWORD" nil nil space)))))
  164. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
  165. (org-export--get-inbuffer-options back-end)))
  166. '(:keyword "With spaces")))
  167. ;; Test `newline' behaviour.
  168. (should
  169. (equal
  170. (let ((back-end (org-export-create-backend
  171. :options '((:keyword "KEYWORD" nil nil newline)))))
  172. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
  173. (org-export--get-inbuffer-options back-end)))
  174. '(:keyword "With\ntwo lines")))
  175. ;; Test `split' behaviour.
  176. (should
  177. (equal
  178. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  179. (org-export--get-inbuffer-options))
  180. '(:select-tags ("a" "b"))))
  181. ;; Test `parse' behaviour. `parse' implies `space' but preserve
  182. ;; line breaks. Multi-line objects are allowed.
  183. (should
  184. (org-element-map
  185. (org-test-with-temp-text "#+TITLE: *bold*"
  186. (plist-get (org-export--get-inbuffer-options) :title))
  187. 'bold #'identity nil t))
  188. (should
  189. (equal
  190. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  191. (plist-get (org-export--get-inbuffer-options) :title))
  192. '("Some title with spaces")))
  193. (should
  194. (org-element-map
  195. (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
  196. (plist-get (org-export--get-inbuffer-options) :title))
  197. 'line-break #'identity nil t))
  198. (should
  199. (org-element-map
  200. (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
  201. (plist-get (org-export--get-inbuffer-options) :title))
  202. 'bold #'identity nil t))
  203. ;; Options set through SETUPFILE.
  204. (should
  205. (equal
  206. (org-test-with-temp-text
  207. (format "#+DESCRIPTION: l1
  208. #+LANGUAGE: es
  209. #+SELECT_TAGS: a
  210. #+TITLE: a
  211. #+SETUPFILE: \"%s/examples/setupfile.org\"
  212. #+LANGUAGE: fr
  213. #+SELECT_TAGS: c
  214. #+TITLE: c"
  215. org-test-dir)
  216. (org-export--get-inbuffer-options))
  217. '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
  218. ;; More than one property can refer to the same buffer keyword.
  219. (should
  220. (equal '(:k2 "value" :k1 "value")
  221. (let ((backend (org-export-create-backend
  222. :options '((:k1 "KEYWORD")
  223. (:k2 "KEYWORD")))))
  224. (org-test-with-temp-text "#+KEYWORD: value"
  225. (org-export--get-inbuffer-options backend)))))
  226. ;; Keywords in commented subtrees are ignored.
  227. (should-not
  228. (equal "Me"
  229. (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
  230. (plist-get info :author))))
  231. (should-not
  232. (equal "Mine"
  233. (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
  234. (plist-get info :email)))))
  235. (ert-deftest test-org-export/get-subtree-options ()
  236. "Test setting options from headline's properties."
  237. ;; EXPORT_TITLE.
  238. (should
  239. (equal '("Subtree Title")
  240. (org-test-with-temp-text "#+TITLE: Title
  241. * Headline<point>
  242. :PROPERTIES:
  243. :EXPORT_TITLE: Subtree Title
  244. :END:
  245. Paragraph"
  246. (plist-get (org-export-get-environment nil t) :title))))
  247. ;; EXPORT_OPTIONS.
  248. (should
  249. (= 2
  250. (org-test-with-temp-text "#+OPTIONS: H:1
  251. * Headline<point>
  252. :PROPERTIES:
  253. :EXPORT_OPTIONS: H:2
  254. :END:
  255. Paragraph"
  256. (plist-get (org-export-get-environment nil t) :headline-levels))))
  257. ;; EXPORT_DATE.
  258. (should
  259. (equal '("29-03-2012")
  260. (org-test-with-temp-text "#+DATE: today
  261. * Headline<point>
  262. :PROPERTIES:
  263. :EXPORT_DATE: 29-03-2012
  264. :END:
  265. Paragraph"
  266. (plist-get (org-export-get-environment nil t) :date))))
  267. ;; Properties with `split' behaviour are stored as a list of
  268. ;; strings.
  269. (should
  270. (equal '("a" "b")
  271. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  272. * Headline<point>
  273. :PROPERTIES:
  274. :EXPORT_EXCLUDE_TAGS: a b
  275. :END:
  276. Paragraph"
  277. (plist-get (org-export-get-environment nil t) :exclude-tags))))
  278. ;; Handle :PROPERTY+: syntax.
  279. (should
  280. (equal '("a" "b")
  281. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  282. * Headline<point>
  283. :PROPERTIES:
  284. :EXPORT_EXCLUDE_TAGS: a
  285. :EXPORT_EXCLUDE_TAGS+: b
  286. :END:
  287. Paragraph"
  288. (plist-get (org-export-get-environment nil t) :exclude-tags))))
  289. ;; Export properties are case-insensitive.
  290. (should
  291. (equal '("29-03-2012")
  292. (org-test-with-temp-text "* Headline
  293. :PROPERTIES:
  294. :EXPORT_Date: 29-03-2012
  295. :END:
  296. Paragraph"
  297. (plist-get (org-export-get-environment nil t) :date))))
  298. ;; Still grab correct options when section above is empty.
  299. (should
  300. (equal '("H1")
  301. (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
  302. (plist-get (org-export-get-environment nil t) :title))))
  303. ;; More than one property can refer to the same node property.
  304. (should
  305. (equal '("1" "1")
  306. (org-test-with-temp-text
  307. "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
  308. (let* ((backend (org-export-create-backend
  309. :options '((:k1 "A")
  310. (:k2 "A"))))
  311. (options (org-export-get-environment backend t)))
  312. (list (plist-get options :k1) (plist-get options :k2)))))))
  313. (ert-deftest test-org-export/set-title ()
  314. "Test title setting."
  315. ;; Without TITLE keyword.
  316. (should
  317. (equal
  318. ""
  319. (let (org-export-filter-body-functions
  320. org-export-filter-final-output-functions)
  321. (org-test-with-temp-text "Test"
  322. (org-export-as
  323. (org-export-create-backend
  324. :transcoders
  325. '((template . (lambda (text info)
  326. (org-element-interpret-data
  327. (plist-get info :title)))))))))))
  328. ;; With a blank TITLE keyword.
  329. (should
  330. (equal
  331. ""
  332. (let (org-export-filter-body-functions
  333. org-export-filter-final-output-functions)
  334. (org-test-with-temp-text "#+TITLE:\nTest"
  335. (org-export-as
  336. (org-export-create-backend
  337. :transcoders
  338. '((template . (lambda (text info)
  339. (org-element-interpret-data
  340. (plist-get info :title)))))))))))
  341. ;; With a non-empty TITLE keyword.
  342. (should
  343. (equal
  344. "Title"
  345. (org-test-with-temp-text "#+TITLE: Title\nTest"
  346. (org-export-as
  347. (org-export-create-backend
  348. :transcoders
  349. '((template . (lambda (text info)
  350. (org-element-interpret-data
  351. (plist-get info :title))))))))))
  352. ;; When exporting a subtree, its heading becomes the headline of the
  353. ;; document...
  354. (should
  355. (equal
  356. "Headline"
  357. (org-test-with-temp-text "* Headline\nBody"
  358. (org-export-as
  359. (org-export-create-backend
  360. :transcoders
  361. '((template . (lambda (text info)
  362. (org-element-interpret-data
  363. (plist-get info :title))))))
  364. 'subtree))))
  365. ;; ... unless there is an EXPORT_TITLE property at the root of the
  366. ;; subtree.
  367. (should
  368. (equal
  369. "B"
  370. (org-test-with-temp-text
  371. "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
  372. (org-export-as
  373. (org-export-create-backend
  374. :transcoders
  375. '((template . (lambda (text info)
  376. (org-element-interpret-data
  377. (plist-get info :title))))))
  378. 'subtree)))))
  379. (ert-deftest test-org-export/handle-options ()
  380. "Test if export options have an impact on output."
  381. ;; Test exclude tags for headlines and inlinetasks.
  382. (should
  383. (equal ""
  384. (let (org-export-filter-body-functions
  385. org-export-filter-final-output-functions)
  386. (org-test-with-temp-text "* Head1 :noexp:"
  387. (org-export-as (org-test-default-backend)
  388. nil nil nil '(:exclude-tags ("noexp")))))))
  389. (should
  390. (equal "#+FILETAGS: noexp\n"
  391. (let (org-export-filter-body-functions
  392. org-export-filter-final-output-functions)
  393. (org-test-with-temp-text "#+FILETAGS: noexp\n* Head1"
  394. (org-export-as (org-test-default-backend)
  395. nil nil nil '(:exclude-tags ("noexp")))))))
  396. ;; Test include tags for headlines and inlinetasks.
  397. (should
  398. (equal (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  399. (let ((org-tags-column 0))
  400. (org-export-as (org-test-default-backend)
  401. nil nil nil '(:select-tags ("exp")))))
  402. "* H2\n** Sub :exp:\n*** Sub Sub\n"))
  403. ;; If there is an include tag, ignore the section before the first
  404. ;; headline, if any.
  405. (should
  406. (equal (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
  407. (let ((org-tags-column 0))
  408. (org-export-as (org-test-default-backend)
  409. nil nil nil '(:select-tags ("exp")))))
  410. "* H1 :exp:\nBody\n"))
  411. (should
  412. (equal (org-test-with-temp-text "#+FILETAGS: exp\nFirst section\n* H1\nBody"
  413. (org-export-as (org-test-default-backend)
  414. nil nil nil '(:select-tags ("exp"))))
  415. "* H1\nBody\n"))
  416. (should-not
  417. (equal (org-test-with-temp-text "* H1 :exp:\nBody"
  418. (let ((org-tags-column 0))
  419. (org-export-as (org-test-default-backend)
  420. nil nil nil '(:select-tags ("exp")))))
  421. "* H1 :exp:\n"))
  422. ;; Test mixing include tags and exclude tags.
  423. (should
  424. (string-match
  425. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  426. (org-test-with-temp-text "
  427. * Head1 :export:
  428. ** Sub-Head1 :noexport:
  429. ** Sub-Head2
  430. * Head2 :noexport:
  431. ** Sub-Head1 :export:"
  432. (org-export-as (org-test-default-backend) nil nil nil
  433. '(:select-tags ("export") :exclude-tags ("noexport"))))))
  434. ;; Ignore tasks.
  435. (should
  436. (equal ""
  437. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  438. org-export-filter-body-functions
  439. org-export-filter-final-output-functions)
  440. (org-test-with-temp-text "* TODO Head1"
  441. (org-export-as (org-test-default-backend)
  442. nil nil nil '(:with-tasks nil))))))
  443. (should
  444. (equal "* TODO Head1\n"
  445. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  446. (org-test-with-temp-text "* TODO Head1"
  447. (org-export-as (org-test-default-backend)
  448. nil nil nil '(:with-tasks t))))))
  449. ;; Archived tree.
  450. (should
  451. (equal ""
  452. (let (org-export-filter-body-functions
  453. org-export-filter-final-output-functions)
  454. (org-test-with-temp-text "* Head1 :archive:"
  455. (let ((org-archive-tag "archive"))
  456. (org-export-as (org-test-default-backend)
  457. nil nil nil '(:with-archived-trees nil)))))))
  458. (should
  459. (string-match
  460. "\\* Head1[ \t]+:archive:"
  461. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  462. (let ((org-archive-tag "archive"))
  463. (org-export-as (org-test-default-backend) nil nil nil
  464. '(:with-archived-trees headline))))))
  465. (should
  466. (string-match
  467. "\\`\\* Head1[ \t]+:archive:\n\\'"
  468. (org-test-with-temp-text "* Head1 :archive:"
  469. (let ((org-archive-tag "archive"))
  470. (org-export-as (org-test-default-backend)
  471. nil nil nil '(:with-archived-trees t))))))
  472. ;; Broken links. Depending on `org-export-with-broken-links', raise
  473. ;; an error, ignore link or mark is as broken in output.
  474. (should-error
  475. (org-test-with-temp-text "[[#broken][link]]"
  476. (let ((backend
  477. (org-export-create-backend
  478. :transcoders
  479. '((section . (lambda (_e c _i) c))
  480. (paragraph . (lambda (_e c _i) c))
  481. (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
  482. (org-export-as backend nil nil nil '(:with-broken-links nil)))))
  483. (should
  484. (org-test-with-temp-text "[[#broken][link]]"
  485. (let ((backend
  486. (org-export-create-backend
  487. :transcoders
  488. '((section . (lambda (_e c _i) c))
  489. (paragraph . (lambda (_e c _i) c))
  490. (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
  491. (org-export-as backend nil nil nil '(:with-broken-links t)))))
  492. (should
  493. (org-test-with-temp-text "[[#broken][link]]"
  494. (let ((backend
  495. (org-export-create-backend
  496. :transcoders
  497. '((section . (lambda (_e c _i) c))
  498. (paragraph . (lambda (_e c _i) c))
  499. (link . (lambda (l c i) (org-export-resolve-id-link l i)))))))
  500. (org-string-nw-p
  501. (org-export-as backend nil nil nil '(:with-broken-links mark))))))
  502. ;; Clocks.
  503. (should
  504. (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
  505. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  506. (org-export-as (org-test-default-backend)
  507. nil nil nil '(:with-clocks t)))))
  508. (should
  509. (equal ""
  510. (let (org-export-filter-body-functions
  511. org-export-filter-final-output-functions)
  512. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  513. (org-export-as (org-test-default-backend)
  514. nil nil nil '(:with-clocks nil))))))
  515. ;; Drawers.
  516. (should
  517. (equal ""
  518. (let (org-export-filter-body-functions
  519. org-export-filter-final-output-functions)
  520. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  521. (org-export-as (org-test-default-backend)
  522. nil nil nil '(:with-drawers nil))))))
  523. (should
  524. (equal ":TEST:\ncontents\n:END:\n"
  525. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  526. (org-export-as (org-test-default-backend)
  527. nil nil nil '(:with-drawers t)))))
  528. (should
  529. (equal ":FOO:\nkeep\n:END:\n"
  530. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  531. (org-export-as (org-test-default-backend)
  532. nil nil nil '(:with-drawers ("FOO"))))))
  533. (should
  534. (equal ":FOO:\nkeep\n:END:\n"
  535. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  536. (org-export-as (org-test-default-backend)
  537. nil nil nil '(:with-drawers (not "BAR"))))))
  538. ;; Fixed-width.
  539. (should
  540. (equal ": A\n"
  541. (org-test-with-temp-text ": A"
  542. (org-export-as (org-test-default-backend) nil nil nil
  543. '(:with-fixed-width t)))))
  544. (should
  545. (equal ""
  546. (let (org-export-filter-body-functions
  547. org-export-filter-final-output-functions)
  548. (org-test-with-temp-text ": A"
  549. (org-export-as (org-test-default-backend) nil nil nil
  550. '(:with-fixed-width nil))))))
  551. ;; Footnotes.
  552. (should
  553. (equal "Footnote?"
  554. (let ((org-footnote-section nil))
  555. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  556. (org-trim (org-export-as (org-test-default-backend)
  557. nil nil nil '(:with-footnotes nil)))))))
  558. (should
  559. (equal "Footnote?[fn:1]\n\n[fn:1] Def"
  560. (let ((org-footnote-section nil))
  561. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  562. (org-trim (org-export-as (org-test-default-backend)
  563. nil nil nil '(:with-footnotes t)))))))
  564. ;; Inlinetasks.
  565. (when (featurep 'org-inlinetask)
  566. (should
  567. (equal
  568. ""
  569. (let ((org-inlinetask-min-level 15)
  570. org-export-filter-body-functions
  571. org-export-filter-final-output-functions)
  572. (org-test-with-temp-text "*************** Task"
  573. (org-export-as (org-test-default-backend)
  574. nil nil nil '(:with-inlinetasks nil))))))
  575. (should
  576. (equal
  577. ""
  578. (let ((org-inlinetask-min-level 15)
  579. org-export-filter-body-functions
  580. org-export-filter-final-output-functions)
  581. (org-test-with-temp-text
  582. "*************** Task\nContents\n*************** END"
  583. (org-export-as (org-test-default-backend)
  584. nil nil nil '(:with-inlinetasks nil)))))))
  585. ;; Plannings.
  586. (should
  587. (string-match
  588. "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
  589. (let ((org-closed-string "CLOSED:"))
  590. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  591. (org-export-as (org-test-default-backend)
  592. nil nil nil '(:with-planning t))))))
  593. (should
  594. (equal "* H\n"
  595. (let ((org-closed-string "CLOSED:"))
  596. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  597. (org-export-as (org-test-default-backend)
  598. nil nil nil '(:with-planning nil))))))
  599. ;; Property Drawers.
  600. (should
  601. (equal "* H1\n"
  602. (org-test-with-temp-text
  603. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  604. (org-export-as (org-test-default-backend)
  605. nil nil nil '(:with-properties nil)))))
  606. (should
  607. (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
  608. (org-test-with-temp-text
  609. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  610. (org-export-as (org-test-default-backend)
  611. nil nil nil '(:with-properties t)))))
  612. (should
  613. (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
  614. (org-test-with-temp-text
  615. "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
  616. (org-export-as (org-test-default-backend)
  617. nil nil nil '(:with-properties ("B"))))))
  618. ;; Statistics cookies.
  619. (should
  620. (equal "* Stats"
  621. (let (org-export-filter-body-functions
  622. org-export-filter-final-output-functions)
  623. (org-trim
  624. (org-test-with-temp-text "* Stats [0/0]"
  625. (org-export-as (org-test-default-backend)
  626. nil nil nil '(:with-statistics-cookies nil)))))))
  627. ;; Tables.
  628. (should
  629. (equal "| A |\n"
  630. (org-test-with-temp-text "| A |"
  631. (org-export-as (org-test-default-backend) nil nil nil
  632. '(:with-tables t)))))
  633. (should
  634. (equal ""
  635. (let (org-export-filter-body-functions
  636. org-export-filter-final-output-functions)
  637. (org-test-with-temp-text "| A |"
  638. (org-export-as (org-test-default-backend) nil nil nil
  639. '(:with-tables nil)))))))
  640. (ert-deftest test-org-export/with-timestamps ()
  641. "Test `org-export-with-timestamps' specifications."
  642. ;; t value.
  643. (should
  644. (string-match
  645. "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
  646. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  647. (org-export-as (org-test-default-backend)
  648. nil nil nil '(:with-timestamps t)))))
  649. ;; nil value.
  650. (should
  651. (equal
  652. ""
  653. (let (org-export-filter-body-functions
  654. org-export-filter-final-output-functions)
  655. (org-trim
  656. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  657. (org-export-as (org-test-default-backend)
  658. nil nil nil '(:with-timestamps nil)))))))
  659. ;; `active' value.
  660. (should
  661. (string-match
  662. "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  663. (org-test-with-temp-text
  664. "<2012-03-29 Thu>[2012-03-29 Thu]
  665. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  666. (org-export-as (org-test-default-backend)
  667. nil nil nil '(:with-timestamps active)))))
  668. ;; `inactive' value.
  669. (should
  670. (string-match
  671. "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  672. (org-test-with-temp-text
  673. "<2012-03-29 Thu>[2012-03-29 Thu]
  674. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  675. (org-export-as (org-test-default-backend)
  676. nil nil nil '(:with-timestamps inactive))))))
  677. (ert-deftest test-org-export/comment-tree ()
  678. "Test if export process ignores commented trees."
  679. (should
  680. (equal ""
  681. (let (org-export-filter-body-functions
  682. org-export-filter-final-output-functions)
  683. (org-test-with-temp-text "* COMMENT Head1"
  684. (org-export-as (org-test-default-backend)))))))
  685. (ert-deftest test-org-export/uninterpreted ()
  686. "Test handling of uninterpreted elements."
  687. ;; Entities.
  688. (should
  689. (equal "dummy\n"
  690. (org-test-with-temp-text "\\alpha"
  691. (org-export-as
  692. (org-export-create-backend
  693. :transcoders '((entity . (lambda (e c i) "dummy"))
  694. (paragraph . (lambda (p c i) c))
  695. (section . (lambda (s c i) c))))
  696. nil nil nil '(:with-entities t)))))
  697. (should
  698. (equal "\\alpha\n"
  699. (org-test-with-temp-text "\\alpha"
  700. (org-export-as
  701. (org-export-create-backend
  702. :transcoders '((entity . (lambda (e c i) "dummy"))
  703. (paragraph . (lambda (p c i) c))
  704. (section . (lambda (s c i) c))))
  705. nil nil nil '(:with-entities nil)))))
  706. ;; Emphasis.
  707. (should
  708. (equal "dummy\n"
  709. (org-test-with-temp-text "*bold*"
  710. (org-export-as
  711. (org-export-create-backend
  712. :transcoders '((bold . (lambda (b c i) "dummy"))
  713. (paragraph . (lambda (p c i) c))
  714. (section . (lambda (s c i) c))))
  715. nil nil nil '(:with-emphasize t)))))
  716. (should
  717. (equal "*bold*\n"
  718. (org-test-with-temp-text "*bold*"
  719. (org-export-as
  720. (org-export-create-backend
  721. :transcoders '((bold . (lambda (b c i) "dummy"))
  722. (paragraph . (lambda (p c i) c))
  723. (section . (lambda (s c i) c))))
  724. nil nil nil '(:with-emphasize nil)))))
  725. ;; LaTeX environment.
  726. (should
  727. (equal "dummy\n"
  728. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  729. (org-export-as
  730. (org-export-create-backend
  731. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  732. (section . (lambda (s c i) c))))
  733. nil nil nil '(:with-latex t)))))
  734. (should
  735. (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
  736. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  737. (org-export-as
  738. (org-export-create-backend
  739. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  740. (section . (lambda (s c i) c))))
  741. nil nil nil '(:with-latex verbatim)))))
  742. ;; LaTeX fragment.
  743. (should
  744. (equal "dummy\n"
  745. (org-test-with-temp-text "$1$"
  746. (org-export-as
  747. (org-export-create-backend
  748. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  749. (paragraph . (lambda (p c i) c))
  750. (section . (lambda (s c i) c))))
  751. nil nil nil '(:with-latex t)))))
  752. (should
  753. (equal "$1$\n"
  754. (org-test-with-temp-text "$1$"
  755. (org-export-as
  756. (org-export-create-backend
  757. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  758. (paragraph . (lambda (p c i) c))
  759. (section . (lambda (s c i) c))))
  760. nil nil nil '(:with-latex verbatim)))))
  761. ;; Sub/superscript.
  762. (should
  763. (equal "adummy\n"
  764. (org-test-with-temp-text "a_b"
  765. (org-export-as
  766. (org-export-create-backend
  767. :transcoders '((subscript . (lambda (s c i) "dummy"))
  768. (paragraph . (lambda (p c i) c))
  769. (section . (lambda (s c i) c))))
  770. nil nil nil '(:with-sub-superscript t)))))
  771. (should
  772. (equal "a_b\n"
  773. (org-test-with-temp-text "a_b"
  774. (org-export-as
  775. (org-export-create-backend
  776. :transcoders '((subscript . (lambda (s c i) "dummy"))
  777. (paragraph . (lambda (p c i) c))
  778. (section . (lambda (s c i) c))))
  779. nil nil nil '(:with-sub-superscript nil)))))
  780. (should
  781. (equal "a_b\n"
  782. (org-test-with-temp-text "a_b"
  783. (org-export-as
  784. (org-export-create-backend
  785. :transcoders '((subscript . (lambda (s c i) "dummy"))
  786. (paragraph . (lambda (p c i) c))
  787. (section . (lambda (s c i) c))))
  788. nil nil nil '(:with-sub-superscript {})))))
  789. (should
  790. (equal "adummy\n"
  791. (org-test-with-temp-text "a_{b}"
  792. (org-export-as
  793. (org-export-create-backend
  794. :transcoders '((subscript . (lambda (s c i) "dummy"))
  795. (paragraph . (lambda (p c i) c))
  796. (section . (lambda (s c i) c))))
  797. nil nil nil '(:with-sub-superscript {})))))
  798. (should
  799. (equal "a_entity\n"
  800. (org-test-with-temp-text "a_\\alpha"
  801. (org-export-as
  802. (org-export-create-backend
  803. :transcoders '((entity . (lambda (e c i) "entity"))
  804. (subscript . (lambda (s c i) "dummy"))
  805. (paragraph . (lambda (p c i) c))
  806. (section . (lambda (s c i) c))))
  807. nil nil nil '(:with-sub-superscript nil)))))
  808. ;; Also handle uninterpreted objects in title.
  809. (should
  810. (equal "a_b"
  811. (org-test-with-temp-text "#+TITLE: a_b"
  812. (org-export-as
  813. (org-export-create-backend
  814. :transcoders
  815. '((subscript . (lambda (s c i) "dummy"))
  816. (template . (lambda (c i) (org-export-data
  817. (plist-get i :title) i)))
  818. (section . (lambda (s c i) c))))
  819. nil nil nil '(:with-sub-superscript nil)))))
  820. ;; Handle uninterpreted objects in captions.
  821. (should
  822. (equal "adummy\n"
  823. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  824. (org-export-as
  825. (org-export-create-backend
  826. :transcoders
  827. '((subscript . (lambda (s c i) "dummy"))
  828. (paragraph . (lambda (p c i)
  829. (org-export-data (org-export-get-caption p) i)))
  830. (section . (lambda (s c i) c))))
  831. nil nil nil '(:with-sub-superscript t)))))
  832. (should
  833. (equal "a_b\n"
  834. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  835. (org-export-as
  836. (org-export-create-backend
  837. :transcoders
  838. '((subscript . (lambda (s c i) "dummy"))
  839. (paragraph . (lambda (p c i)
  840. (org-export-data (org-export-get-caption p) i)))
  841. (section . (lambda (s c i) c))))
  842. nil nil nil '(:with-sub-superscript nil)))))
  843. ;; Special case: multiples uninterpreted objects in a row.
  844. (should
  845. (equal "a_b_c_d\n"
  846. (org-test-with-temp-text "a_b_c_d"
  847. (org-export-as
  848. (org-export-create-backend
  849. :transcoders '((subscript . (lambda (s c i) "dummy"))
  850. (paragraph . (lambda (p c i) c))
  851. (section . (lambda (s c i) c))))
  852. nil nil nil '(:with-sub-superscript {}))))))
  853. (ert-deftest test-org-export/export-scope ()
  854. "Test all export scopes."
  855. ;; Subtree.
  856. (should
  857. (equal "text\n*** H3\n"
  858. (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
  859. (org-export-as (org-test-default-backend) 'subtree))))
  860. (should
  861. (equal "text\n*** H3\n"
  862. (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
  863. (org-export-as (org-test-default-backend) 'subtree))))
  864. ;; Subtree with a code block calling another block outside.
  865. (should
  866. (equal ": 3\n"
  867. (org-test-with-temp-text "
  868. <point>* Head1
  869. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  870. <<test>>
  871. #+END_SRC
  872. * Head2
  873. #+NAME: test
  874. #+BEGIN_SRC emacs-lisp
  875. \(+ 1 2)
  876. #+END_SRC"
  877. (let ((org-export-use-babel t))
  878. (org-export-as (org-test-default-backend) 'subtree)))))
  879. ;; Subtree export should ignore leading planning line and property
  880. ;; drawer.
  881. (should
  882. (equal "Text\n"
  883. (org-test-with-temp-text "
  884. <point>* H
  885. SCHEDULED: <2012-03-29 Thu>
  886. :PROPERTIES:
  887. :A: 1
  888. :END:
  889. Text"
  890. (org-export-as (org-test-default-backend)
  891. 'subtree nil nil
  892. '(:with-planning t :with-properties t)))))
  893. ;; Visible.
  894. (should
  895. (equal "* H1\n"
  896. (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
  897. (org-cycle)
  898. (org-export-as (org-test-default-backend) nil 'visible))))
  899. ;; Region.
  900. (should
  901. (equal "text\n"
  902. (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
  903. (transient-mark-mode 1)
  904. (push-mark (point) t t)
  905. (end-of-line)
  906. (org-export-as (org-test-default-backend)))))
  907. ;; Body only.
  908. (should
  909. (equal "Text\n"
  910. (org-test-with-temp-text "Text"
  911. (org-export-as
  912. (org-export-create-backend
  913. :transcoders
  914. '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
  915. (section . (lambda (_s c _i) c))
  916. (paragraph . (lambda (_p c _i) c))))
  917. nil nil 'body-only))))
  918. (should
  919. (equal "BEGIN\nText\nEND"
  920. (org-test-with-temp-text "Text"
  921. (org-export-as
  922. (org-export-create-backend
  923. :transcoders
  924. '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
  925. (section . (lambda (_s c _i) c))
  926. (paragraph . (lambda (_p c _i) c))))))))
  927. ;; Pathological case: Body only on an empty buffer is expected to
  928. ;; return an empty string, not nil.
  929. (should
  930. (org-test-with-temp-text ""
  931. (org-export-as (org-test-default-backend) nil nil t))))
  932. (ert-deftest test-org-export/output-file-name ()
  933. "Test `org-export-output-file-name' specifications."
  934. ;; Export from a file: name is built from original file name.
  935. (should
  936. (org-test-with-temp-text-in-file "Test"
  937. (equal (file-name-base (buffer-file-name))
  938. (file-name-base (org-export-output-file-name ".ext")))))
  939. ;; When #+EXPORT_FILE_NAME is defined, use it.
  940. (should
  941. (equal "test.ext"
  942. (org-test-with-temp-text-in-file "#+EXPORT_FILE_NAME: test"
  943. (org-export-output-file-name ".ext" t))))
  944. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  945. (should
  946. (equal "test.ext"
  947. (org-test-with-temp-text-in-file
  948. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  949. (org-export-output-file-name ".ext" t))))
  950. (should
  951. (equal "property.ext"
  952. (org-test-with-temp-text
  953. "#+EXPORT_FILE_NAME: keyword
  954. * Test<point>
  955. :PROPERTIES:
  956. :EXPORT_FILE_NAME: property
  957. :END:"
  958. (org-export-output-file-name ".ext" t))))
  959. ;; From a buffer not associated to a file, too.
  960. (should
  961. (equal "test.ext"
  962. (org-test-with-temp-text
  963. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  964. (org-export-output-file-name ".ext" t))))
  965. ;; When provided name is absolute, preserve it.
  966. (should
  967. (org-test-with-temp-text
  968. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  969. (expand-file-name "test"))
  970. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  971. ;; When PUB-DIR argument is provided, use it.
  972. (should
  973. (equal "dir/"
  974. (org-test-with-temp-text-in-file "Test"
  975. (file-name-directory
  976. (org-export-output-file-name ".ext" nil "dir/")))))
  977. ;; PUB-DIR has precedence over EXPORT_FILE_NAME keyword or property.
  978. (should
  979. (equal "pub-dir/"
  980. (org-test-with-temp-text-in-file
  981. "#+EXPORT_FILE_NAME: /dir/keyword\nTest"
  982. (file-name-directory
  983. (org-export-output-file-name ".ext" nil "pub-dir/")))))
  984. ;; When returned name would overwrite original file, add EXTENSION
  985. ;; another time.
  986. (should
  987. (equal "normal.org.org"
  988. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  989. (org-export-output-file-name ".org")))))
  990. (ert-deftest test-org-export/expand-include ()
  991. "Test file inclusion in an Org buffer."
  992. ;; Error when file isn't specified.
  993. (should-error
  994. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  995. (org-export-expand-include-keyword)))
  996. ;; Full insertion with recursive inclusion.
  997. (should
  998. (equal
  999. (with-temp-buffer
  1000. (insert-file
  1001. (expand-file-name "examples/include.org" org-test-dir))
  1002. (replace-regexp-in-string
  1003. (regexp-quote "#+INCLUDE: \"include2.org\"")
  1004. "Success!" (buffer-string)))
  1005. (org-test-with-temp-text
  1006. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  1007. (org-export-expand-include-keyword)
  1008. (buffer-string))))
  1009. ;; Localized insertion.
  1010. (org-test-with-temp-text
  1011. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  1012. org-test-dir)
  1013. (org-export-expand-include-keyword)
  1014. (should (equal (buffer-string)
  1015. "Small Org file with an include keyword.\n")))
  1016. ;; Insertion with constraints on headlines level.
  1017. (should
  1018. (equal
  1019. "* Top heading\n** Heading\nbody\n"
  1020. (org-test-with-temp-text
  1021. (format
  1022. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  1023. org-test-dir)
  1024. (org-export-expand-include-keyword)
  1025. (buffer-string))))
  1026. (should
  1027. (equal
  1028. "* Top heading\n* Heading\nbody\n"
  1029. (org-test-with-temp-text
  1030. (format
  1031. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  1032. org-test-dir)
  1033. (org-export-expand-include-keyword)
  1034. (buffer-string))))
  1035. ;; Inclusion within an example block.
  1036. (should
  1037. (equal
  1038. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  1039. (org-test-with-temp-text
  1040. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  1041. org-test-dir)
  1042. (org-export-expand-include-keyword)
  1043. (buffer-string))))
  1044. ;; Inclusion within a src-block.
  1045. (should
  1046. (equal
  1047. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  1048. (org-test-with-temp-text
  1049. (format
  1050. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  1051. org-test-dir)
  1052. (org-export-expand-include-keyword)
  1053. (buffer-string))))
  1054. ;; Inclusion within an html export-block.
  1055. (should
  1056. (equal
  1057. "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
  1058. (org-test-with-temp-text
  1059. (format
  1060. "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
  1061. org-test-dir)
  1062. (org-export-expand-include-keyword)
  1063. (buffer-string))))
  1064. ;; Inclusion within an center paragraph
  1065. (should
  1066. (equal
  1067. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  1068. (org-test-with-temp-text
  1069. (format
  1070. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  1071. org-test-dir)
  1072. (org-export-expand-include-keyword)
  1073. (buffer-string))))
  1074. ;; Footnotes labels are local to each included file.
  1075. (should
  1076. (= 6
  1077. (length
  1078. (delete-dups
  1079. (let ((contents "
  1080. Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
  1081. \[fn:1] Footnote 1
  1082. \[fn:test] Footnote \"test\""))
  1083. (org-test-with-temp-text-in-file contents
  1084. (let ((file1 (buffer-file-name)))
  1085. (org-test-with-temp-text-in-file contents
  1086. (let ((file2 (buffer-file-name)))
  1087. (org-test-with-temp-text
  1088. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  1089. file1 file2)
  1090. (org-export-expand-include-keyword)
  1091. (org-element-map (org-element-parse-buffer)
  1092. 'footnote-reference
  1093. (lambda (r) (org-element-property :label r)))))))))))))
  1094. ;; Footnotes labels are not local to each include keyword.
  1095. (should
  1096. (= 3
  1097. (length
  1098. (delete-dups
  1099. (let ((contents "
  1100. Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
  1101. \[fn:1] Footnote 1
  1102. \[fn:test] Footnote \"test\""))
  1103. (org-test-with-temp-text-in-file contents
  1104. (let ((file (buffer-file-name)))
  1105. (org-test-with-temp-text
  1106. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  1107. (org-export-expand-include-keyword)
  1108. (org-element-map (org-element-parse-buffer)
  1109. 'footnote-reference
  1110. (lambda (ref) (org-element-property :label ref)))))))))))
  1111. ;; Footnotes are supported by :lines-like elements and unnecessary
  1112. ;; footnotes are dropped.
  1113. (should
  1114. (= 3
  1115. (length
  1116. (delete-dups
  1117. (let ((contents "
  1118. * foo
  1119. Footnotes[fn:1]
  1120. * bar
  1121. Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
  1122. \[fn:1] Footnote 1
  1123. \[fn:2] Footnote 1
  1124. * Footnotes
  1125. \[fn:test] Footnote \"test\"
  1126. "))
  1127. (org-test-with-temp-text-in-file contents
  1128. (let ((file (buffer-file-name)))
  1129. (org-test-with-temp-text
  1130. (format "#+INCLUDE: \"%s::*bar\"\n" file)
  1131. (org-export-expand-include-keyword)
  1132. (org-element-map (org-element-parse-buffer)
  1133. 'footnote-definition
  1134. (lambda (ref) (org-element-property :label ref)))))))))))
  1135. ;; If only-contents is non-nil only include contents of element.
  1136. (should
  1137. (equal
  1138. "body\n"
  1139. (org-test-with-temp-text
  1140. (concat
  1141. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
  1142. org-test-dir)
  1143. ":only-contents t")
  1144. (org-export-expand-include-keyword)
  1145. (buffer-string))))
  1146. ;; Headings can be included via CUSTOM_ID.
  1147. (should
  1148. (org-test-with-temp-text
  1149. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1150. (org-export-expand-include-keyword)
  1151. (goto-char (point-min))
  1152. (looking-at "* Another heading")))
  1153. ;; Named objects can be included.
  1154. (should
  1155. (equal
  1156. "| 1 |\n"
  1157. (org-test-with-temp-text
  1158. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
  1159. org-test-dir)
  1160. (org-export-expand-include-keyword)
  1161. (buffer-string))))
  1162. ;; Including non-existing elements should result in an error.
  1163. (should-error
  1164. (org-test-with-temp-text
  1165. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
  1166. org-test-dir)
  1167. (org-export-expand-include-keyword)))
  1168. ;; Lines work relatively to an included element.
  1169. (should
  1170. (equal
  1171. "2\n3\n"
  1172. (org-test-with-temp-text
  1173. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
  1174. :lines \"2-3\""
  1175. org-test-dir)
  1176. (org-export-expand-include-keyword)
  1177. (buffer-string))))
  1178. ;; Properties should be dropped from headlines.
  1179. (should
  1180. (equal
  1181. (org-test-with-temp-text
  1182. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
  1183. org-test-dir)
  1184. (org-export-expand-include-keyword)
  1185. (buffer-string))
  1186. (org-test-with-temp-text
  1187. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1188. (org-export-expand-include-keyword)
  1189. (buffer-string))))
  1190. ;; Properties should be dropped, drawers should not be.
  1191. (should
  1192. (equal
  1193. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1194. (org-test-with-temp-text
  1195. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
  1196. org-test-dir)
  1197. (org-export-expand-include-keyword)
  1198. (buffer-string))))
  1199. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1200. (should
  1201. (cl-every (lambda (level) (zerop (1- level)))
  1202. (org-test-with-temp-text
  1203. (concat
  1204. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
  1205. org-test-dir)
  1206. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
  1207. org-test-dir))
  1208. (org-export-expand-include-keyword)
  1209. (org-element-map (org-element-parse-buffer) 'headline
  1210. (lambda (head) (org-element-property :level head))))))
  1211. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1212. (should-not
  1213. (equal
  1214. (org-test-with-temp-text
  1215. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
  1216. org-test-dir)
  1217. (org-export-expand-include-keyword)
  1218. (buffer-string))
  1219. (org-test-with-temp-text
  1220. (format
  1221. "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
  1222. org-test-dir)
  1223. (org-export-expand-include-keyword)
  1224. (buffer-string))))
  1225. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1226. (should
  1227. (org-test-with-temp-text-in-file
  1228. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
  1229. org-test-dir)
  1230. (narrow-to-region (point) (point-max))
  1231. (org-export-expand-include-keyword)
  1232. (eq 1 (org-current-level))))
  1233. ;; If :minlevel is present do not alter it.
  1234. (should
  1235. (org-test-with-temp-text
  1236. (format
  1237. "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
  1238. org-test-dir)
  1239. (narrow-to-region (point) (point-max))
  1240. (org-export-expand-include-keyword)
  1241. (eq 3 (org-current-level)))))
  1242. (ert-deftest test-org-export/expand-macro ()
  1243. "Test macro expansion in an Org buffer."
  1244. (require 'ox-org)
  1245. ;; Standard macro expansion.
  1246. (should
  1247. (equal "#+MACRO: macro1 value\nvalue\n"
  1248. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1249. (org-export-as (org-test-default-backend)))))
  1250. ;; Include global macros. However, local macros override them.
  1251. (should
  1252. (equal "global\n"
  1253. (org-test-with-temp-text "{{{M}}}"
  1254. (let ((org-export-global-macros '(("M" . "global"))))
  1255. (org-export-as (org-test-default-backend))))))
  1256. (should
  1257. (equal "global arg\n"
  1258. (org-test-with-temp-text "{{{M(arg)}}}"
  1259. (let ((org-export-global-macros '(("M" . "global $1"))))
  1260. (org-export-as (org-test-default-backend))))))
  1261. (should
  1262. (equal "2\n"
  1263. (org-test-with-temp-text "{{{M}}}"
  1264. (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
  1265. (org-export-as (org-test-default-backend))))))
  1266. (should
  1267. (equal "#+MACRO: M local\nlocal\n"
  1268. (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
  1269. (let ((org-export-global-macros '(("M" . "global"))))
  1270. (org-export-as (org-test-default-backend))))))
  1271. ;; Allow macro in parsed keywords and associated properties.
  1272. ;; Standard macro expansion.
  1273. (should
  1274. (string-match
  1275. "#\\+K: value"
  1276. (let ((backend (org-export-create-backend
  1277. :parent 'org
  1278. :options '((:k "K" nil nil parse)))))
  1279. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1280. (org-export-as backend)))))
  1281. (should
  1282. (string-match
  1283. ":EXPORT_K: v"
  1284. (let ((backend (org-export-create-backend
  1285. :parent 'org
  1286. :options '((:k "K" nil nil parse)))))
  1287. (org-test-with-temp-text
  1288. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1289. (org-export-as backend nil nil nil '(:with-properties t))))))
  1290. ;; Expand specific macros.
  1291. (should
  1292. (equal "me 2012-03-29 me@here Title\n"
  1293. (org-test-with-temp-text
  1294. "
  1295. #+TITLE: Title
  1296. #+DATE: 2012-03-29
  1297. #+AUTHOR: me
  1298. #+EMAIL: me@here
  1299. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1300. (let ((output (org-export-as (org-test-default-backend))))
  1301. (substring output (string-match ".*\n\\'" output))))))
  1302. ;; Expand specific macros when property contained a regular macro
  1303. ;; already.
  1304. (should
  1305. (equal "value\n"
  1306. (org-test-with-temp-text "
  1307. #+MACRO: macro1 value
  1308. #+TITLE: {{{macro1}}}
  1309. {{{title}}}"
  1310. (let ((output (org-export-as (org-test-default-backend))))
  1311. (substring output (string-match ".*\n\\'" output))))))
  1312. ;; Expand macros with templates in included files.
  1313. (should
  1314. (equal "success\n"
  1315. (org-test-with-temp-text
  1316. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1317. {{{included-macro}}}" org-test-dir)
  1318. (let ((output (org-export-as (org-test-default-backend))))
  1319. (substring output (string-match ".*\n\\'" output))))))
  1320. ;; Date macro takes a optional formatting argument
  1321. (should
  1322. (equal "09-02-15\n"
  1323. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1324. (org-export-as (org-test-default-backend)))))
  1325. ;; Only single timestamps are formatted
  1326. (should
  1327. (equal "<2015-02x-09>\n"
  1328. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1329. (org-export-as (org-test-default-backend)))))
  1330. ;; Throw an error when a macro definition is missing.
  1331. (should-error
  1332. (org-test-with-temp-text "{{{missing}}}"
  1333. (org-export-as (org-test-default-backend))))
  1334. ;; Macros defined in commented subtrees are ignored.
  1335. (should-error
  1336. (org-test-with-temp-text
  1337. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1338. (org-export-as (org-test-default-backend))))
  1339. (should-error
  1340. (org-test-with-temp-text
  1341. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1342. (org-export-as (org-test-default-backend)))))
  1343. (ert-deftest test-org-export/before-processing-hook ()
  1344. "Test `org-export-before-processing-hook'."
  1345. (should
  1346. (equal
  1347. "#+MACRO: mac val\nTest\n"
  1348. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1349. (let ((org-export-before-processing-hook
  1350. '((lambda (backend)
  1351. (while (re-search-forward "{{{" nil t)
  1352. (let ((object (org-element-context)))
  1353. (when (eq (org-element-type object) 'macro)
  1354. (delete-region
  1355. (org-element-property :begin object)
  1356. (org-element-property :end object)))))))))
  1357. (org-export-as (org-test-default-backend)))))))
  1358. (ert-deftest test-org-export/before-parsing-hook ()
  1359. "Test `org-export-before-parsing-hook'."
  1360. (should
  1361. (equal "Body 1\nBody 2\n"
  1362. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1363. (let ((org-export-before-parsing-hook
  1364. '((lambda (backend)
  1365. (goto-char (point-min))
  1366. (while (re-search-forward org-outline-regexp-bol nil t)
  1367. (delete-region
  1368. (point-at-bol) (progn (forward-line) (point))))))))
  1369. (org-export-as (org-test-default-backend)))))))
  1370. ;;; Affiliated Keywords
  1371. (ert-deftest test-org-export/read-attribute ()
  1372. "Test `org-export-read-attribute' specifications."
  1373. ;; Standard test.
  1374. (should
  1375. (equal
  1376. (org-export-read-attribute
  1377. :attr_html
  1378. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1379. (org-element-at-point)))
  1380. '(:a "1" :b "2")))
  1381. ;; Return nil on empty attribute.
  1382. (should-not
  1383. (org-export-read-attribute
  1384. :attr_html
  1385. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1386. ;; Return nil on "nil" string.
  1387. (should
  1388. (equal '(:a nil :b nil)
  1389. (org-export-read-attribute
  1390. :attr_html
  1391. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1392. (org-element-at-point)))))
  1393. ;; Return nil on empty string.
  1394. (should
  1395. (equal '(:a nil :b nil)
  1396. (org-export-read-attribute
  1397. :attr_html
  1398. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1399. (org-element-at-point)))))
  1400. ;; Return empty string when value is "".
  1401. (should
  1402. (equal '(:a "")
  1403. (org-export-read-attribute
  1404. :attr_html
  1405. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1406. (org-element-at-point)))))
  1407. ;; Return \"\" when value is """".
  1408. (should
  1409. (equal '(:a "\"\"")
  1410. (org-export-read-attribute
  1411. :attr_html
  1412. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1413. (org-element-at-point)))))
  1414. ;; Ignore text before first property.
  1415. (should-not
  1416. (member "ignore"
  1417. (org-export-read-attribute
  1418. :attr_html
  1419. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1420. (org-element-at-point))))))
  1421. (ert-deftest test-org-export/get-caption ()
  1422. "Test `org-export-get-caption' specifications."
  1423. ;; Without optional argument, return long caption
  1424. (should
  1425. (equal
  1426. '("l")
  1427. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1428. (org-export-get-caption (org-element-at-point)))))
  1429. ;; With optional argument, return short caption.
  1430. (should
  1431. (equal
  1432. '("s")
  1433. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1434. (org-export-get-caption (org-element-at-point) t))))
  1435. ;; Multiple lines are separated by white spaces.
  1436. (should
  1437. (equal
  1438. '("a" " " "b")
  1439. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1440. (org-export-get-caption (org-element-at-point))))))
  1441. ;;; Back-End Tools
  1442. (ert-deftest test-org-export/define-backend ()
  1443. "Test back-end definition and accessors."
  1444. ;; Translate table.
  1445. (should
  1446. (equal '((headline . my-headline-test))
  1447. (let (org-export-registered-backends)
  1448. (org-export-define-backend 'test '((headline . my-headline-test)))
  1449. (org-export-get-all-transcoders 'test))))
  1450. ;; Filters.
  1451. (should
  1452. (equal '((:filter-headline . my-filter))
  1453. (let (org-export-registered-backends)
  1454. (org-export-define-backend 'test
  1455. '((headline . my-headline-test))
  1456. :filters-alist '((:filter-headline . my-filter)))
  1457. (org-export-backend-filters (org-export-get-backend 'test)))))
  1458. ;; Options.
  1459. (should
  1460. (equal '((:prop value))
  1461. (let (org-export-registered-backends)
  1462. (org-export-define-backend 'test
  1463. '((headline . my-headline-test))
  1464. :options-alist '((:prop value)))
  1465. (org-export-backend-options (org-export-get-backend 'test)))))
  1466. ;; Menu.
  1467. (should
  1468. (equal '(?k "Test Export" test)
  1469. (let (org-export-registered-backends)
  1470. (org-export-define-backend 'test
  1471. '((headline . my-headline-test))
  1472. :menu-entry '(?k "Test Export" test))
  1473. (org-export-backend-menu (org-export-get-backend 'test))))))
  1474. (ert-deftest test-org-export/define-derived-backend ()
  1475. "Test `org-export-define-derived-backend' specifications."
  1476. ;; Error when parent back-end is not defined.
  1477. (should-error
  1478. (let (org-export-registered-backends)
  1479. (org-export-define-derived-backend 'test 'parent)))
  1480. ;; Append translation table to parent's.
  1481. (should
  1482. (equal '((:headline . test) (:headline . parent))
  1483. (let (org-export-registered-backends)
  1484. (org-export-define-backend 'parent '((:headline . parent)))
  1485. (org-export-define-derived-backend 'test 'parent
  1486. :translate-alist '((:headline . test)))
  1487. (org-export-get-all-transcoders 'test))))
  1488. ;; Options defined in the new back have priority over those defined
  1489. ;; in parent.
  1490. (should
  1491. (eq 'test
  1492. (let (org-export-registered-backends)
  1493. (org-export-define-backend 'parent
  1494. '((:headline . parent))
  1495. :options-alist '((:a nil nil 'parent)))
  1496. (org-export-define-derived-backend 'test 'parent
  1497. :options-alist '((:a nil nil 'test)))
  1498. (plist-get (org-export--get-global-options
  1499. (org-export-get-backend 'test))
  1500. :a)))))
  1501. (ert-deftest test-org-export/derived-backend-p ()
  1502. "Test `org-export-derived-backend-p' specifications."
  1503. ;; Non-nil with direct match.
  1504. (should
  1505. (let (org-export-registered-backends)
  1506. (org-export-define-backend 'test '((headline . test)))
  1507. (org-export-derived-backend-p 'test 'test)))
  1508. (should
  1509. (let (org-export-registered-backends)
  1510. (org-export-define-backend 'test '((headline . test)))
  1511. (org-export-define-derived-backend 'test2 'test)
  1512. (org-export-derived-backend-p 'test2 'test2)))
  1513. ;; Non-nil with a direct parent.
  1514. (should
  1515. (let (org-export-registered-backends)
  1516. (org-export-define-backend 'test '((headline . test)))
  1517. (org-export-define-derived-backend 'test2 'test)
  1518. (org-export-derived-backend-p 'test2 'test)))
  1519. ;; Non-nil with an indirect parent.
  1520. (should
  1521. (let (org-export-registered-backends)
  1522. (org-export-define-backend 'test '((headline . test)))
  1523. (org-export-define-derived-backend 'test2 'test)
  1524. (org-export-define-derived-backend 'test3 'test2)
  1525. (org-export-derived-backend-p 'test3 'test)))
  1526. ;; Nil otherwise.
  1527. (should-not
  1528. (let (org-export-registered-backends)
  1529. (org-export-define-backend 'test '((headline . test)))
  1530. (org-export-define-backend 'test2 '((headline . test2)))
  1531. (org-export-derived-backend-p 'test2 'test)))
  1532. (should-not
  1533. (let (org-export-registered-backends)
  1534. (org-export-define-backend 'test '((headline . test)))
  1535. (org-export-define-backend 'test2 '((headline . test2)))
  1536. (org-export-define-derived-backend 'test3 'test2)
  1537. (org-export-derived-backend-p 'test3 'test))))
  1538. (ert-deftest test-org-export/get-all-transcoders ()
  1539. "Test `org-export-get-all-transcoders' specifications."
  1540. ;; Return nil when back-end cannot be found.
  1541. (should-not (org-export-get-all-transcoders nil))
  1542. ;; Same as `org-export-transcoders' if no parent.
  1543. (should
  1544. (equal '((headline . ignore))
  1545. (org-export-get-all-transcoders
  1546. (org-export-create-backend
  1547. :transcoders '((headline . ignore))))))
  1548. ;; But inherit from all ancestors whenever possible.
  1549. (should
  1550. (equal '((section . ignore) (headline . ignore))
  1551. (let (org-export-registered-backends)
  1552. (org-export-define-backend 'b1 '((headline . ignore)))
  1553. (org-export-get-all-transcoders
  1554. (org-export-create-backend
  1555. :parent 'b1 :transcoders '((section . ignore)))))))
  1556. (should
  1557. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1558. (let (org-export-registered-backends)
  1559. (org-export-define-backend 'b1 '((headline . ignore)))
  1560. (org-export-define-derived-backend 'b2 'b1
  1561. :translate-alist '((section . ignore)))
  1562. (org-export-get-all-transcoders
  1563. (org-export-create-backend
  1564. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1565. ;; Back-end transcoders overrule inherited ones.
  1566. (should
  1567. (eq 'b
  1568. (let (org-export-registered-backends)
  1569. (org-export-define-backend 'b1 '((headline . a)))
  1570. (cdr (assq 'headline
  1571. (org-export-get-all-transcoders
  1572. (org-export-create-backend
  1573. :parent 'b1 :transcoders '((headline . b))))))))))
  1574. (ert-deftest test-org-export/get-all-options ()
  1575. "Test `org-export-get-all-options' specifications."
  1576. ;; Return nil when back-end cannot be found.
  1577. (should-not (org-export-get-all-options nil))
  1578. ;; Same as `org-export-options' if no parent.
  1579. (should
  1580. (equal '((headline . ignore))
  1581. (org-export-get-all-options
  1582. (org-export-create-backend
  1583. :options '((headline . ignore))))))
  1584. ;; But inherit from all ancestors whenever possible.
  1585. (should
  1586. (equal '((:key2 value2) (:key1 value1))
  1587. (let (org-export-registered-backends)
  1588. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1589. (org-export-get-all-options
  1590. (org-export-create-backend
  1591. :parent 'b1 :options '((:key2 value2)))))))
  1592. (should
  1593. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1594. (let (org-export-registered-backends)
  1595. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1596. (org-export-define-derived-backend 'b2 'b1
  1597. :options-alist '((:key2 value2)))
  1598. (org-export-get-all-options
  1599. (org-export-create-backend
  1600. :parent 'b2 :options '((:key3 value3)))))))
  1601. ;; Back-end options overrule inherited ones.
  1602. (should
  1603. (eq 'b
  1604. (let (org-export-registered-backends)
  1605. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1606. (cdr (assq :key1
  1607. (org-export-get-all-options
  1608. (org-export-create-backend
  1609. :parent 'b1 :options '((:key1 . b))))))))))
  1610. (ert-deftest test-org-export/get-all-filters ()
  1611. "Test `org-export-get-all-filters' specifications."
  1612. ;; Return nil when back-end cannot be found.
  1613. (should-not (org-export-get-all-filters nil))
  1614. ;; Same as `org-export-filters' if no parent.
  1615. (should
  1616. (equal '((:filter-headline . ignore))
  1617. (org-export-get-all-filters
  1618. (org-export-create-backend
  1619. :filters '((:filter-headline . ignore))))))
  1620. ;; But inherit from all ancestors whenever possible.
  1621. (should
  1622. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1623. (let (org-export-registered-backends)
  1624. (org-export-define-backend 'b1
  1625. nil :filters-alist '((:filter-headline . ignore)))
  1626. (org-export-get-all-filters
  1627. (org-export-create-backend
  1628. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1629. (should
  1630. (equal '((:filter-paragraph . ignore)
  1631. (:filter-section . ignore)
  1632. (:filter-headline . ignore))
  1633. (let (org-export-registered-backends)
  1634. (org-export-define-backend 'b1
  1635. nil :filters-alist '((:filter-headline . ignore)))
  1636. (org-export-define-derived-backend 'b2 'b1
  1637. :filters-alist '((:filter-section . ignore)))
  1638. (org-export-get-all-filters
  1639. (org-export-create-backend
  1640. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1641. ;; Back-end filters overrule inherited ones.
  1642. (should
  1643. (eq 'b
  1644. (let (org-export-registered-backends)
  1645. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1646. (cdr (assq :filter-headline
  1647. (org-export-get-all-filters
  1648. (org-export-create-backend
  1649. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1650. (ert-deftest test-org-export/with-backend ()
  1651. "Test `org-export-with-backend' definition."
  1652. ;; Error when calling an undefined back-end
  1653. (should-error (org-export-with-backend nil "Test"))
  1654. ;; Error when called back-end doesn't have an appropriate
  1655. ;; transcoder.
  1656. (should-error
  1657. (org-export-with-backend
  1658. (org-export-create-backend :transcoders '((headline . ignore)))
  1659. "Test"))
  1660. ;; Otherwise, export using correct transcoder
  1661. (should
  1662. (equal "Success"
  1663. (let (org-export-registered-backends)
  1664. (org-export-define-backend 'test
  1665. '((plain-text . (lambda (text contents info) "Failure"))))
  1666. (org-export-define-backend 'test2
  1667. '((plain-text . (lambda (text contents info) "Success"))))
  1668. (org-export-with-backend 'test2 "Test"))))
  1669. ;; Provide correct back-end if transcoder needs to use recursive
  1670. ;; calls anyway.
  1671. (should
  1672. (equal "Success\n"
  1673. (let ((test-back-end
  1674. (org-export-create-backend
  1675. :transcoders
  1676. (list (cons 'headline
  1677. (lambda (headline contents info)
  1678. (org-export-data
  1679. (org-element-property :title headline)
  1680. info)))
  1681. (cons 'plain-text (lambda (text info) "Success"))))))
  1682. (org-export-string-as
  1683. "* Test"
  1684. (org-export-create-backend
  1685. :transcoders
  1686. (list (cons 'headline
  1687. (lambda (headline contents info)
  1688. (org-export-with-backend
  1689. test-back-end headline contents info))))))))))
  1690. (ert-deftest test-org-export/data-with-backend ()
  1691. "Test `org-export-data-with-backend' specifications."
  1692. ;; Error when calling an undefined back-end.
  1693. (should-error (org-export-data-with-backend nil "nil" nil))
  1694. ;; Otherwise, export data recursively, using correct back-end.
  1695. (should
  1696. (equal
  1697. "Success!"
  1698. (org-export-data-with-backend
  1699. '(bold nil "Test")
  1700. (org-export-create-backend
  1701. :transcoders
  1702. '((plain-text . (lambda (text info) "Success"))
  1703. (bold . (lambda (bold contents info) (concat contents "!")))))
  1704. '(:with-emphasize t)))))
  1705. ;;; Comments
  1706. (ert-deftest test-org-export/comments ()
  1707. "Test comments handling during export.
  1708. In particular, structure of the document mustn't be altered after
  1709. comments removal."
  1710. (should
  1711. (equal (org-test-with-temp-text "
  1712. Para1
  1713. # Comment
  1714. # Comment
  1715. Para2"
  1716. (org-export-as (org-test-default-backend)))
  1717. "Para1\n\nPara2\n"))
  1718. (should
  1719. (equal (org-test-with-temp-text "
  1720. Para1
  1721. # Comment
  1722. Para2"
  1723. (org-export-as (org-test-default-backend)))
  1724. "Para1\n\nPara2\n"))
  1725. (should
  1726. (equal (org-test-with-temp-text "
  1727. \[fn:1] Para1
  1728. # Inside definition
  1729. # Outside definition
  1730. Para2"
  1731. (org-export-as (org-test-default-backend)))
  1732. "[fn:1] Para1\n\n\nPara2\n"))
  1733. (should
  1734. (equal (org-test-with-temp-text "
  1735. \[fn:1] Para1
  1736. # Inside definition
  1737. # Inside definition
  1738. Para2"
  1739. (org-export-as (org-test-default-backend)))
  1740. "[fn:1] Para1\n\nPara2\n"))
  1741. (should
  1742. (equal (org-test-with-temp-text "
  1743. \[fn:1] Para1
  1744. # Inside definition
  1745. Para2"
  1746. (org-export-as (org-test-default-backend)))
  1747. "[fn:1] Para1\n\nPara2\n"))
  1748. (should
  1749. (equal (org-test-with-temp-text "
  1750. \[fn:1] Para1
  1751. # Inside definition
  1752. Para2"
  1753. (org-export-as (org-test-default-backend)))
  1754. "[fn:1] Para1\n\nPara2\n")))
  1755. ;;; Export blocks
  1756. (ert-deftest test-org-export/export-block ()
  1757. "Test export blocks transcoding."
  1758. (should
  1759. (equal "Success!\n"
  1760. (org-test-with-temp-text
  1761. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1762. (org-export-as
  1763. (org-export-create-backend
  1764. :transcoders '((export-block . (lambda (b _c _i)
  1765. (org-element-property :value b)))
  1766. (section . (lambda (_s c _i) c))))))))
  1767. (should
  1768. (equal "Success!\n"
  1769. (org-test-with-temp-text
  1770. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1771. (org-export-as
  1772. (org-export-create-backend
  1773. :transcoders
  1774. (list
  1775. (cons 'export-block
  1776. (lambda (b _c _i)
  1777. (and (equal (org-element-property :type b) "BACKEND")
  1778. (org-element-property :value b))))
  1779. (cons 'section (lambda (_s c _i) c)))))))))
  1780. ;;; Export Snippets
  1781. (ert-deftest test-org-export/export-snippet ()
  1782. "Test export snippets transcoding."
  1783. ;; Standard test.
  1784. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1785. (let ((backend (org-test-default-backend)))
  1786. (setf (org-export-backend-name backend) 'test)
  1787. (setf (org-export-backend-transcoders backend)
  1788. (cons (cons 'export-snippet
  1789. (lambda (snippet contents info)
  1790. (when (eq (org-export-snippet-backend snippet) 'test)
  1791. (org-element-property :value snippet))))
  1792. (org-export-backend-transcoders backend)))
  1793. (let ((org-export-snippet-translation-alist nil))
  1794. (should (equal (org-export-as backend) "A\n")))
  1795. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1796. (should (equal (org-export-as backend) "AB\n")))))
  1797. ;; Ignored export snippets do not remove any blank.
  1798. (should
  1799. (equal "begin end\n"
  1800. (org-test-with-parsed-data "begin @@test:A@@ end"
  1801. (org-export-data-with-backend
  1802. tree
  1803. (org-export-create-backend
  1804. :transcoders
  1805. '((paragraph . (lambda (paragraph contents info) contents))
  1806. (section . (lambda (section contents info) contents))))
  1807. info)))))
  1808. ;;; Filters
  1809. (ert-deftest test-org-export/filter-apply-functions ()
  1810. "Test `org-export-filter-apply-functions' specifications."
  1811. ;; Functions are applied in order and return values are reduced.
  1812. (should
  1813. (equal "210"
  1814. (org-export-filter-apply-functions
  1815. (list (lambda (value &rest _) (concat "1" value))
  1816. (lambda (value &rest _) (concat "2" value)))
  1817. "0" nil)))
  1818. ;; Functions returning nil are skipped.
  1819. (should
  1820. (equal "20"
  1821. (org-export-filter-apply-functions
  1822. (list #'ignore (lambda (value &rest _) (concat "2" value)))
  1823. "0" nil)))
  1824. ;; If all functions are skipped, return the initial value.
  1825. (should
  1826. (equal "0"
  1827. (org-export-filter-apply-functions (list #'ignore) "0" nil)))
  1828. ;; If any function returns the empty string, final value is the
  1829. ;; empty string.
  1830. (should
  1831. (equal ""
  1832. (org-export-filter-apply-functions
  1833. (list (lambda (value &rest _) "")
  1834. (lambda (value &rest _) (concat "2" value)))
  1835. "0" nil)))
  1836. ;; Any function returning the empty string short-circuits the
  1837. ;; process.
  1838. (should
  1839. (org-export-filter-apply-functions
  1840. (list (lambda (value &rest _) "")
  1841. (lambda (value &rest _) (error "This shouldn't happen")))
  1842. "0" nil)))
  1843. ;;; Footnotes
  1844. (ert-deftest test-org-export/footnote-first-reference-p ()
  1845. "Test `org-export-footnote-first-reference-p' specifications."
  1846. (should
  1847. (equal
  1848. '(t nil)
  1849. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1850. (let (result)
  1851. (org-export-as
  1852. (org-export-create-backend
  1853. :transcoders
  1854. `(,(cons 'footnote-reference
  1855. (lambda (f c i)
  1856. (push (org-export-footnote-first-reference-p f i)
  1857. result)
  1858. ""))
  1859. (section . (lambda (s c i) c))
  1860. (paragraph . (lambda (p c i) c))))
  1861. nil nil nil '(:with-footnotes t))
  1862. (nreverse result)))))
  1863. ;; Limit check to DATA, when non-nil.
  1864. (should
  1865. (equal
  1866. '(nil t)
  1867. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1868. (let (result)
  1869. (org-element-map tree 'footnote-reference
  1870. (lambda (ref)
  1871. (push
  1872. (org-export-footnote-first-reference-p
  1873. ref info (org-element-map tree 'headline #'identity info t))
  1874. result))
  1875. info)
  1876. (nreverse result)))))
  1877. (should
  1878. (equal
  1879. '(t nil)
  1880. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1881. (let (result)
  1882. (org-element-map tree 'footnote-reference
  1883. (lambda (ref)
  1884. (push (org-export-footnote-first-reference-p ref info) result))
  1885. info)
  1886. (nreverse result)))))
  1887. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1888. ;; in the main body of the document. Otherwise, enter footnote
  1889. ;; definitions when they are encountered.
  1890. (should
  1891. (equal
  1892. '(t nil)
  1893. (org-test-with-temp-text
  1894. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1895. (let (result)
  1896. (org-export-as
  1897. (org-export-create-backend
  1898. :transcoders
  1899. `(,(cons 'footnote-reference
  1900. (lambda (f c i)
  1901. (when (org-element-lineage f '(drawer))
  1902. (push (org-export-footnote-first-reference-p f i nil)
  1903. result))
  1904. ""))
  1905. (drawer . (lambda (d c i) c))
  1906. (footnote-definition . (lambda (d c i) c))
  1907. (section . (lambda (s c i) c))
  1908. (paragraph . (lambda (p c i) c))))
  1909. nil nil nil '(:with-footnotes t))
  1910. (nreverse result)))))
  1911. (should
  1912. (equal
  1913. '(t t)
  1914. (org-test-with-temp-text
  1915. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1916. (let (result)
  1917. (org-export-as
  1918. (org-export-create-backend
  1919. :transcoders
  1920. `(,(cons 'footnote-reference
  1921. (lambda (f c i)
  1922. (when (org-element-lineage f '(drawer))
  1923. (push (org-export-footnote-first-reference-p f i nil t)
  1924. result))
  1925. ""))
  1926. (drawer . (lambda (d c i) c))
  1927. (footnote-definition . (lambda (d c i) c))
  1928. (section . (lambda (s c i) c))
  1929. (paragraph . (lambda (p c i) c))))
  1930. nil nil nil '(:with-footnotes t))
  1931. (nreverse result))))))
  1932. (ert-deftest test-org-export/get-footnote-number ()
  1933. "Test `org-export-get-footnote-number' specifications."
  1934. (should
  1935. (equal '(1 2 1)
  1936. (org-test-with-parsed-data
  1937. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1938. (org-element-map tree 'footnote-reference
  1939. (lambda (ref) (org-export-get-footnote-number ref info))
  1940. info))))
  1941. ;; Anonymous footnotes all get a new number.
  1942. (should
  1943. (equal '(1 2)
  1944. (org-test-with-parsed-data
  1945. "Text[fn::anon1][fn::anon2]"
  1946. (org-element-map tree 'footnote-reference
  1947. (lambda (ref) (org-export-get-footnote-number ref info))
  1948. info))))
  1949. ;; Test nested footnotes order.
  1950. (should
  1951. (equal
  1952. '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
  1953. (org-test-with-parsed-data
  1954. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1955. (org-element-map tree 'footnote-reference
  1956. (lambda (ref)
  1957. (cons (org-export-get-footnote-number ref info)
  1958. (org-element-property :label ref)))
  1959. info))))
  1960. ;; Limit number to provided DATA, when non-nil.
  1961. (should
  1962. (equal
  1963. '(1)
  1964. (org-test-with-parsed-data
  1965. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1966. (org-element-map tree 'footnote-reference
  1967. (lambda (ref)
  1968. (org-export-get-footnote-number
  1969. ref info (org-element-map tree 'headline #'identity info t)))
  1970. info))))
  1971. (should
  1972. (equal
  1973. '(1 2)
  1974. (org-test-with-parsed-data
  1975. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1976. (org-element-map tree 'footnote-reference
  1977. (lambda (ref) (org-export-get-footnote-number ref info))
  1978. info))))
  1979. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1980. ;; then footnote definitions.
  1981. (should
  1982. (equal
  1983. '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
  1984. (org-test-with-parsed-data
  1985. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1986. (org-element-map tree 'footnote-reference
  1987. (lambda (ref)
  1988. (cons (org-element-property :label ref)
  1989. (org-export-get-footnote-number ref info nil t)))
  1990. info))))
  1991. (should
  1992. (equal
  1993. '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
  1994. (org-test-with-parsed-data
  1995. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1996. (org-element-map tree 'footnote-reference
  1997. (lambda (ref)
  1998. (cons (org-element-property :label ref)
  1999. (org-export-get-footnote-number ref info nil)))
  2000. info)))))
  2001. (ert-deftest test-org-export/get-footnote-definition ()
  2002. "Test `org-export-get-footnote-definition' specifications."
  2003. ;; Standard test.
  2004. (should
  2005. (equal "A\n"
  2006. (org-element-interpret-data
  2007. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  2008. (org-export-get-footnote-definition
  2009. (org-element-map tree 'footnote-reference #'identity nil t)
  2010. info)))))
  2011. ;; Raise an error if no definition is found.
  2012. (should-error
  2013. (org-test-with-parsed-data "Text[fn:1]"
  2014. (org-export-get-footnote-definition
  2015. (org-element-map tree 'footnote-reference #'identity nil t)
  2016. info)))
  2017. ;; Find inline definitions.
  2018. (should
  2019. (equal "A"
  2020. (org-element-interpret-data
  2021. (org-test-with-parsed-data "Text[fn:1:A]"
  2022. (org-export-get-footnote-definition
  2023. (org-element-map tree 'footnote-reference #'identity nil t)
  2024. info)))))
  2025. ;; Find anonymous definitions.
  2026. (should
  2027. (equal "A"
  2028. (org-element-interpret-data
  2029. (org-test-with-parsed-data "Text[fn::A]"
  2030. (org-export-get-footnote-definition
  2031. (org-element-map tree 'footnote-reference #'identity nil t)
  2032. info)))))
  2033. ;; Find empty definitions.
  2034. (should
  2035. (equal ""
  2036. (org-element-interpret-data
  2037. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  2038. (org-export-get-footnote-definition
  2039. (org-element-map tree 'footnote-reference #'identity nil t)
  2040. info)))))
  2041. (should
  2042. (equal ""
  2043. (org-element-interpret-data
  2044. (org-test-with-parsed-data "Text[fn:1:]"
  2045. (org-export-get-footnote-definition
  2046. (org-element-map tree 'footnote-reference #'identity nil t)
  2047. info)))))
  2048. (should
  2049. (equal ""
  2050. (org-element-interpret-data
  2051. (org-test-with-parsed-data "Text[fn::]"
  2052. (org-export-get-footnote-definition
  2053. (org-element-map tree 'footnote-reference #'identity nil t)
  2054. info))))))
  2055. (ert-deftest test-org-export/collect-footnote-definitions ()
  2056. "Test `org-export-collect-footnote-definitions' specifications."
  2057. (should
  2058. (= 4
  2059. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2060. \[fn:2] B [fn:3] [fn::D].
  2061. \[fn:3] C."
  2062. (length (org-export-collect-footnote-definitions info)))))
  2063. ;; Limit number to provided DATA, when non-nil.
  2064. (should
  2065. (equal
  2066. '((1 "2"))
  2067. (org-test-with-parsed-data
  2068. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2069. (mapcar #'butlast
  2070. (org-export-collect-footnote-definitions
  2071. info (org-element-map tree 'headline #'identity info t))))))
  2072. (should
  2073. (equal
  2074. '((1 "1") (2 "2"))
  2075. (org-test-with-parsed-data
  2076. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2077. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  2078. ;; With optional argument BODY-FIRST, first check body, then
  2079. ;; footnote definitions.
  2080. (should
  2081. (equal '("1" "3" "2" nil)
  2082. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2083. \[fn:2] B [fn:3] [fn::D].
  2084. \[fn:3] C."
  2085. (mapcar (lambda (e) (nth 1 e))
  2086. (org-export-collect-footnote-definitions info nil t)))))
  2087. (should-not
  2088. (equal '("1" "3" "2" nil)
  2089. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2090. \[fn:2] B [fn:3] [fn::D].
  2091. \[fn:3] C."
  2092. (mapcar (lambda (e) (nth 1 e))
  2093. (org-export-collect-footnote-definitions info))))))
  2094. (ert-deftest test-org-export/footnotes ()
  2095. "Miscellaneous tests on footnotes."
  2096. (let ((org-footnote-section nil)
  2097. (org-export-with-footnotes t))
  2098. ;; Read every type of footnote.
  2099. (should
  2100. (equal
  2101. '((1 . "A\n") (2 . "C") (3 . "D"))
  2102. (org-test-with-parsed-data
  2103. "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
  2104. (org-element-map tree 'footnote-reference
  2105. (lambda (ref)
  2106. (let ((def (org-export-get-footnote-definition ref info)))
  2107. (cons (org-export-get-footnote-number ref info)
  2108. (if (eq (org-element-property :type ref) 'inline) (car def)
  2109. (car (org-element-contents
  2110. (car (org-element-contents def))))))))
  2111. info))))
  2112. ;; Export nested footnote in invisible definitions.
  2113. (should
  2114. (= 2
  2115. (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
  2116. (narrow-to-region (point) (line-end-position))
  2117. (catch 'exit
  2118. (org-export-as
  2119. (org-export-create-backend
  2120. :transcoders
  2121. '((section
  2122. .
  2123. (lambda (s c i)
  2124. (throw 'exit (length
  2125. (org-export-collect-footnote-definitions
  2126. i))))))))))))
  2127. ;; Export footnotes defined outside parsing scope.
  2128. (should
  2129. (string-match
  2130. "Out of scope"
  2131. (org-test-with-temp-text "[fn:1] Out of scope
  2132. * Title
  2133. <point>Paragraph[fn:1]"
  2134. (org-export-as (org-test-default-backend) 'subtree))))
  2135. (should
  2136. (string-match
  2137. "Out of scope"
  2138. (org-test-with-temp-text "[fn:1] Out of scope
  2139. * Title
  2140. <point>Paragraph[fn:1]"
  2141. (narrow-to-region (point) (point-max))
  2142. (org-export-as (org-test-default-backend)))))
  2143. ;; Export nested footnotes defined outside parsing scope.
  2144. (should
  2145. (string-match
  2146. "Very out of scope"
  2147. (org-test-with-temp-text "
  2148. \[fn:1] Out of scope[fn:2]
  2149. \[fn:2] Very out of scope
  2150. * Title
  2151. <point>Paragraph[fn:1]"
  2152. (org-export-as (org-test-default-backend) 'subtree))))
  2153. (should
  2154. (string-match
  2155. "Very out of scope"
  2156. (org-test-with-temp-text "
  2157. \[fn:1] Out of scope[fn:2]
  2158. \[fn:2] Very out of scope
  2159. * Title
  2160. <point>Paragraph[fn:1]"
  2161. (narrow-to-region (point) (point-max))
  2162. (org-export-as (org-test-default-backend)))))
  2163. (should
  2164. (string-match
  2165. "D2"
  2166. (org-test-with-temp-text "
  2167. \[fn:1] Out of scope[fn:2:D2]
  2168. * Title
  2169. <point>Paragraph[fn:1]"
  2170. (narrow-to-region (point) (point-max))
  2171. (org-export-as (org-test-default-backend)))))
  2172. ;; Export footnotes in pruned parts of tree.
  2173. (should
  2174. (string-match
  2175. "Definition"
  2176. (let ((org-export-exclude-tags '("noexport")))
  2177. (org-test-with-temp-text
  2178. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  2179. (org-export-as (org-test-default-backend))))))
  2180. (should
  2181. (string-match
  2182. "Definition"
  2183. (let ((org-export-select-tags '("export")))
  2184. (org-test-with-temp-text
  2185. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2186. (org-export-as (org-test-default-backend))))))
  2187. ;; Export nested footnotes in pruned parts of tree.
  2188. (should
  2189. (string-match
  2190. "D2"
  2191. (let ((org-export-exclude-tags '("noexport")))
  2192. (org-test-with-temp-text
  2193. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2194. (org-export-as (org-test-default-backend))))))
  2195. (should
  2196. (string-match
  2197. "D2"
  2198. (let ((org-export-select-tags '("export")))
  2199. (org-test-with-temp-text
  2200. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2201. (org-export-as (org-test-default-backend))))))
  2202. ;; Handle uninterpreted data in pruned footnote definitions.
  2203. (should-not
  2204. (string-match
  2205. "|"
  2206. (let ((org-export-with-tables nil))
  2207. (org-test-with-temp-text
  2208. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2209. (org-export-as (org-test-default-backend))))))
  2210. ;; Footnotes without a definition should throw an error.
  2211. (should-error
  2212. (org-test-with-parsed-data "Text[fn:1]"
  2213. (org-export-get-footnote-definition
  2214. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2215. ;; Footnote section should be ignored in TOC and in headlines
  2216. ;; numbering.
  2217. (should
  2218. (= 1 (let ((org-footnote-section "Footnotes"))
  2219. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2220. (org-export-collect-headlines info))))))
  2221. (should
  2222. (equal '(2)
  2223. (let ((org-footnote-section "Footnotes"))
  2224. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2225. (org-element-map tree 'headline
  2226. (lambda (hl)
  2227. (when (equal (org-element-property :raw-value hl) "H2")
  2228. (org-export-get-headline-number hl info)))
  2229. info t)))))))
  2230. ;;; Headlines and Inlinetasks
  2231. (ert-deftest test-org-export/get-relative-level ()
  2232. "Test `org-export-get-relative-level' specifications."
  2233. ;; Standard test.
  2234. (should
  2235. (equal '(1 2)
  2236. (let ((org-odd-levels-only nil))
  2237. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2238. (org-element-map tree 'headline
  2239. (lambda (h) (org-export-get-relative-level h info))
  2240. info)))))
  2241. ;; Missing levels
  2242. (should
  2243. (equal '(1 3)
  2244. (let ((org-odd-levels-only nil))
  2245. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2246. (org-element-map tree 'headline
  2247. (lambda (h) (org-export-get-relative-level h info))
  2248. info))))))
  2249. (ert-deftest test-org-export/low-level-p ()
  2250. "Test `org-export-low-level-p' specifications."
  2251. (should
  2252. (equal
  2253. '(no yes)
  2254. (let ((org-odd-levels-only nil))
  2255. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2256. (org-element-map tree 'headline
  2257. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2258. (plist-put info :headline-levels 1)))))))
  2259. (ert-deftest test-org-export/get-headline-number ()
  2260. "Test `org-export-get-headline-number' specifications."
  2261. ;; Standard test.
  2262. (should
  2263. (equal
  2264. '((1) (1 1))
  2265. (let ((org-odd-levels-only nil))
  2266. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2267. (org-element-map tree 'headline
  2268. (lambda (h) (org-export-get-headline-number h info))
  2269. info)))))
  2270. ;; Missing levels are replaced with 0.
  2271. (should
  2272. (equal
  2273. '((1) (1 0 1))
  2274. (let ((org-odd-levels-only nil))
  2275. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2276. (org-element-map tree 'headline
  2277. (lambda (h) (org-export-get-headline-number h info))
  2278. info))))))
  2279. (ert-deftest test-org-export/numbered-headline-p ()
  2280. "Test `org-export-numbered-headline-p' specifications."
  2281. ;; If `:section-numbers' is nil, never number headlines.
  2282. (should-not
  2283. (org-test-with-parsed-data "* Headline"
  2284. (org-element-map tree 'headline
  2285. (lambda (h) (org-export-numbered-headline-p h info))
  2286. (plist-put info :section-numbers nil))))
  2287. ;; If `:section-numbers' is a number, only number headlines with
  2288. ;; a level greater that it.
  2289. (should
  2290. (equal
  2291. '(yes no)
  2292. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2293. (org-element-map tree 'headline
  2294. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2295. (plist-put info :section-numbers 1)))))
  2296. ;; Otherwise, headlines are always numbered.
  2297. (should
  2298. (org-test-with-parsed-data "* Headline"
  2299. (org-element-map tree 'headline
  2300. (lambda (h) (org-export-numbered-headline-p h info))
  2301. (plist-put info :section-numbers t))))
  2302. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2303. (should-not
  2304. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2305. (org-export-numbered-headline-p
  2306. (org-element-map tree 'headline 'identity info t)
  2307. info)))
  2308. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2309. (should-not
  2310. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2311. (org-export-numbered-headline-p
  2312. (org-element-map tree 'headline #'identity info t)
  2313. info)))
  2314. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  2315. ;; ancestors disables numbering.
  2316. (should
  2317. (org-test-with-parsed-data
  2318. "* H
  2319. :PROPERTIES:
  2320. :UNNUMBERED: t
  2321. :END:
  2322. ** H2
  2323. :PROPERTIES:
  2324. :UNNUMBERED: nil
  2325. :END:
  2326. *** H3"
  2327. (cl-every
  2328. (lambda (h) (not (org-export-numbered-headline-p h info)))
  2329. (org-element-map tree 'headline #'identity info)))))
  2330. (ert-deftest test-org-export/number-to-roman ()
  2331. "Test `org-export-number-to-roman' specifications."
  2332. ;; If number is negative, return it as a string.
  2333. (should (equal (org-export-number-to-roman -1) "-1"))
  2334. ;; Otherwise, return it as a roman number.
  2335. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2336. (ert-deftest test-org-export/get-optional-title ()
  2337. "Test `org-export-get-alt-title' specifications."
  2338. ;; If ALT_TITLE property is defined, use it.
  2339. (should
  2340. (equal '("opt")
  2341. (org-test-with-parsed-data
  2342. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2343. (org-export-get-alt-title
  2344. (org-element-map tree 'headline 'identity info t)
  2345. info))))
  2346. ;; Otherwise, fall-back to regular title.
  2347. (should
  2348. (equal '("Headline")
  2349. (org-test-with-parsed-data "* Headline"
  2350. (org-export-get-alt-title
  2351. (org-element-map tree 'headline 'identity info t)
  2352. info)))))
  2353. (ert-deftest test-org-export/get-tags ()
  2354. "Test `org-export-get-tags' specifications."
  2355. ;; Standard test: tags which are not a select tag, an exclude tag,
  2356. ;; or specified as optional argument shouldn't be ignored.
  2357. (should
  2358. (org-test-with-parsed-data "* Headline :tag:"
  2359. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2360. info)))
  2361. ;; Tags provided in the optional argument are ignored.
  2362. (should-not
  2363. (org-test-with-parsed-data "* Headline :ignore:"
  2364. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2365. info '("ignore"))))
  2366. ;; Allow tag inheritance.
  2367. (should
  2368. (equal
  2369. '(("tag") ("tag"))
  2370. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2371. (org-element-map tree 'headline
  2372. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2373. ;; Tag inheritance checks FILETAGS keywords.
  2374. (should
  2375. (equal
  2376. '(("a" "b" "tag"))
  2377. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2378. (org-element-map tree 'headline
  2379. (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
  2380. (ert-deftest test-org-export/get-node-property ()
  2381. "Test`org-export-get-node-property' specifications."
  2382. ;; Standard test.
  2383. (should
  2384. (equal "value"
  2385. (org-test-with-parsed-data "* Headline
  2386. :PROPERTIES:
  2387. :prop: value
  2388. :END:"
  2389. (org-export-get-node-property
  2390. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2391. ;; Test inheritance.
  2392. (should
  2393. (equal "value"
  2394. (org-test-with-parsed-data "* Parent
  2395. :PROPERTIES:
  2396. :prop: value
  2397. :END:
  2398. ** Headline
  2399. Paragraph"
  2400. (org-export-get-node-property
  2401. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2402. ;; Cannot return a value before the first headline.
  2403. (should-not
  2404. (org-test-with-parsed-data "Paragraph
  2405. * Headline
  2406. :PROPERTIES:
  2407. :prop: value
  2408. :END:"
  2409. (org-export-get-node-property
  2410. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2411. (ert-deftest test-org-export/get-category ()
  2412. "Test `org-export-get-category' specifications."
  2413. ;; Standard test.
  2414. (should
  2415. (equal "value"
  2416. (org-test-with-parsed-data "* Headline
  2417. :PROPERTIES:
  2418. :CATEGORY: value
  2419. :END:"
  2420. (org-export-get-category
  2421. (org-element-map tree 'headline 'identity nil t) info))))
  2422. ;; Test inheritance from a parent headline.
  2423. (should
  2424. (equal '("value" "value")
  2425. (org-test-with-parsed-data "* Headline1
  2426. :PROPERTIES:
  2427. :CATEGORY: value
  2428. :END:
  2429. ** Headline2"
  2430. (org-element-map tree 'headline
  2431. (lambda (hl) (org-export-get-category hl info)) info))))
  2432. ;; Test inheritance from #+CATEGORY keyword
  2433. (should
  2434. (equal "value"
  2435. (org-test-with-parsed-data "#+CATEGORY: value
  2436. * Headline"
  2437. (org-export-get-category
  2438. (org-element-map tree 'headline 'identity nil t) info))))
  2439. ;; Test inheritance from file name.
  2440. (should
  2441. (equal "test"
  2442. (org-test-with-parsed-data "* Headline"
  2443. (let ((info (plist-put info :input-file "~/test.org")))
  2444. (org-export-get-category
  2445. (org-element-map tree 'headline 'identity nil t) info)))))
  2446. ;; Fall-back value.
  2447. (should
  2448. (equal "???"
  2449. (org-test-with-parsed-data "* Headline"
  2450. (org-export-get-category
  2451. (org-element-map tree 'headline 'identity nil t) info)))))
  2452. (ert-deftest test-org-export/first-sibling-p ()
  2453. "Test `org-export-first-sibling-p' specifications."
  2454. ;; Standard test.
  2455. (should
  2456. (equal
  2457. '(yes yes no)
  2458. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2459. (org-element-map tree 'headline
  2460. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2461. info))))
  2462. (should
  2463. (equal '(yes no)
  2464. (org-test-with-parsed-data "- item\n\n para"
  2465. (org-element-map tree 'paragraph
  2466. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2467. info))))
  2468. ;; Ignore sections for headlines.
  2469. (should
  2470. (equal '(yes yes)
  2471. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2472. (org-element-map tree 'headline
  2473. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2474. info))))
  2475. ;; Ignore headlines not exported.
  2476. (should
  2477. (equal
  2478. '(yes)
  2479. (let ((org-export-exclude-tags '("ignore")))
  2480. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2481. (org-element-map tree 'headline
  2482. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2483. info))))))
  2484. (ert-deftest test-org-export/last-sibling-p ()
  2485. "Test `org-export-last-sibling-p' specifications."
  2486. ;; Standard test.
  2487. (should
  2488. (equal
  2489. '(yes no yes)
  2490. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2491. (org-element-map tree 'headline
  2492. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2493. info))))
  2494. (should
  2495. (equal '(no yes)
  2496. (org-test-with-parsed-data "- item\n\n para"
  2497. (org-element-map tree 'paragraph
  2498. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2499. info))))
  2500. ;; Ignore headlines not exported.
  2501. (should
  2502. (equal
  2503. '(yes)
  2504. (let ((org-export-exclude-tags '("ignore")))
  2505. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2506. (org-element-map tree 'headline
  2507. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2508. info))))))
  2509. (ert-deftest test-org-export/handle-inlinetasks ()
  2510. "Test inlinetask export."
  2511. ;; Inlinetask with an exclude tag.
  2512. (when (featurep 'org-inlinetask)
  2513. (should
  2514. (equal
  2515. ""
  2516. (let ((org-inlinetask-min-level 3)
  2517. org-export-filter-body-functions
  2518. org-export-filter-final-output-functions)
  2519. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2520. (org-export-as (org-test-default-backend)
  2521. nil nil nil '(:exclude-tags ("noexp")))))))
  2522. ;; Inlinetask with an include tag.
  2523. (should
  2524. (equal
  2525. "* H2\n*** Inline :exp:\n"
  2526. (let ((org-inlinetask-min-level 3)
  2527. (org-tags-column 0))
  2528. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2529. (org-export-as (org-test-default-backend)
  2530. nil nil nil '(:select-tags ("exp")))))))
  2531. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2532. (should
  2533. (equal ""
  2534. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2535. (org-inlinetask-min-level 3)
  2536. org-export-filter-body-functions
  2537. org-export-filter-final-output-functions)
  2538. (org-test-with-temp-text "*** TODO Inline"
  2539. (org-export-as (org-test-default-backend)
  2540. nil nil nil '(:with-tasks nil))))))))
  2541. ;;; Keywords
  2542. (ert-deftest test-org-export/get-date ()
  2543. "Test `org-export-get-date' specifications."
  2544. ;; Return a properly formatted string when
  2545. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2546. ;; consists in a single timestamp.
  2547. (should
  2548. (equal "29 03 2012"
  2549. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2550. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2551. (org-export-get-date info)))))
  2552. ;; Return a secondary string otherwise.
  2553. (should-not
  2554. (stringp
  2555. (let ((org-export-date-timestamp-format nil))
  2556. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2557. (org-export-get-date info)))))
  2558. (should
  2559. (equal '("Date")
  2560. (org-test-with-parsed-data "#+DATE: Date"
  2561. (org-export-get-date info))))
  2562. ;; Optional argument has precedence over
  2563. ;; `org-export-date-timestamp-format'.
  2564. (should
  2565. (equal "29 03"
  2566. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2567. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2568. (org-export-get-date info "%d %m"))))))
  2569. ;;; Links
  2570. (ert-deftest test-org-export/custom-protocol-maybe ()
  2571. "Test `org-export-custom-protocol-maybe' specifications."
  2572. (should
  2573. (string-match
  2574. "success"
  2575. (progn
  2576. (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
  2577. (org-export-string-as
  2578. "[[foo:path]]"
  2579. (org-export-create-backend
  2580. :name 'test
  2581. :transcoders
  2582. '((section . (lambda (s c i) c))
  2583. (paragraph . (lambda (p c i) c))
  2584. (link . (lambda (l c i)
  2585. (or (org-export-custom-protocol-maybe l c 'test)
  2586. "failure")))))))))
  2587. (should-not
  2588. (string-match
  2589. "success"
  2590. (progn
  2591. (org-link-set-parameters
  2592. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2593. (org-export-string-as
  2594. "[[foo:path]]"
  2595. (org-export-create-backend
  2596. :name 'no-test
  2597. :transcoders
  2598. '((section . (lambda (s c i) c))
  2599. (paragraph . (lambda (p c i) c))
  2600. (link . (lambda (l c i)
  2601. (or (org-export-custom-protocol-maybe l c 'no-test)
  2602. "failure")))))))))
  2603. ;; Ignore anonymous back-ends.
  2604. (should-not
  2605. (string-match
  2606. "success"
  2607. (progn
  2608. (org-link-set-parameters
  2609. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2610. (org-export-string-as
  2611. "[[foo:path]]"
  2612. (org-export-create-backend
  2613. :transcoders
  2614. '((section . (lambda (s c i) c))
  2615. (paragraph . (lambda (p c i) c))
  2616. (link . (lambda (l c i)
  2617. (or (org-export-custom-protocol-maybe l c nil)
  2618. "failure"))))))))))
  2619. (ert-deftest test-org-export/get-coderef-format ()
  2620. "Test `org-export-get-coderef-format' specifications."
  2621. ;; A link without description returns "%s"
  2622. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2623. "%s"))
  2624. ;; Return "%s" when path is matched within description.
  2625. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2626. "desc %s"))
  2627. ;; Otherwise return description.
  2628. (should (equal (org-export-get-coderef-format "path" "desc")
  2629. "desc")))
  2630. (ert-deftest test-org-export/inline-image-p ()
  2631. "Test `org-export-inline-image-p' specifications."
  2632. (should
  2633. (org-export-inline-image-p
  2634. (org-test-with-temp-text "[[#id]]"
  2635. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2636. '(("custom-id" . "id")))))
  2637. (ert-deftest test-org-export/insert-image-links ()
  2638. "Test `org-export-insert-image-links' specifications."
  2639. (should-not
  2640. (member "file"
  2641. (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
  2642. (org-element-map tree 'link
  2643. (lambda (l) (org-element-property :type l))))))
  2644. (should
  2645. (member "file"
  2646. (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
  2647. (org-element-map (org-export-insert-image-links tree info) 'link
  2648. (lambda (l) (org-element-property :type l))))))
  2649. ;; With optional argument RULES, recognize different links as
  2650. ;; images.
  2651. (should-not
  2652. (member "file"
  2653. (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
  2654. (org-element-map (org-export-insert-image-links tree info) 'link
  2655. (lambda (l) (org-element-property :type l))))))
  2656. (should
  2657. (member "file"
  2658. (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
  2659. (org-element-map
  2660. (org-export-insert-image-links tree info '(("file" . "xxx")))
  2661. 'link
  2662. (lambda (l) (org-element-property :type l)))))))
  2663. (ert-deftest test-org-export/fuzzy-link ()
  2664. "Test fuzzy links specifications."
  2665. ;; Link to an headline should return headline's number.
  2666. (should
  2667. ;; Note: Headline's number is in fact a list of numbers.
  2668. (equal '(2)
  2669. (org-test-with-parsed-data
  2670. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2671. (org-element-map tree 'link
  2672. (lambda (link)
  2673. (org-export-get-ordinal
  2674. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2675. ;; Link to a target in an item should return item's number.
  2676. (should
  2677. ;; Note: Item's number is in fact a list of numbers.
  2678. (equal '(1 2)
  2679. (org-test-with-parsed-data
  2680. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2681. (org-element-map tree 'link
  2682. (lambda (link)
  2683. (org-export-get-ordinal
  2684. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2685. ;; Link to a target in a footnote should return footnote's number.
  2686. (should
  2687. (equal '(2 3)
  2688. (org-test-with-parsed-data "
  2689. Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
  2690. \[fn:1] A
  2691. \[fn:2] <<test>>B"
  2692. (org-element-map tree 'link
  2693. (lambda (link)
  2694. (org-export-get-ordinal
  2695. (org-export-resolve-fuzzy-link link info) info)) info))))
  2696. ;; Link to a named element should return sequence number of that
  2697. ;; element.
  2698. (should
  2699. (= 2
  2700. (org-test-with-parsed-data
  2701. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2702. (org-element-map tree 'link
  2703. (lambda (link)
  2704. (org-export-get-ordinal
  2705. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2706. ;; Link to a target not within an item, a table, a footnote
  2707. ;; reference or definition should return section number.
  2708. (should
  2709. (equal '(2)
  2710. (org-test-with-parsed-data
  2711. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2712. (org-element-map tree 'link
  2713. (lambda (link)
  2714. (org-export-get-ordinal
  2715. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2716. ;; Space are not significant when matching a fuzzy link.
  2717. (should
  2718. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2719. (org-element-map tree 'link
  2720. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2721. info t)))
  2722. ;; Statistics cookies are ignored for headline match.
  2723. (should
  2724. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2725. (org-element-map tree 'link
  2726. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2727. info t)))
  2728. (should
  2729. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2730. (org-element-map tree 'link
  2731. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2732. info t))))
  2733. (defun test-org-gen-loc-list(text type)
  2734. (org-test-with-parsed-data text
  2735. (org-element-map tree type
  2736. (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
  2737. (ert-deftest test-org-export/get-loc ()
  2738. "Test `org-export-get-loc' specifications."
  2739. (should
  2740. ;; "-n" resets line number.
  2741. (equal '(0)
  2742. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
  2743. 'example-block)))
  2744. ;; The first "+n" has 0 lines before it
  2745. (should
  2746. (equal '(0)
  2747. (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
  2748. 'example-block)))
  2749. ;; "-n 10" resets line number but has "9 lines" before it.
  2750. (should
  2751. (equal '(9)
  2752. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
  2753. 'example-block)))
  2754. ;; -n10 with two lines then +n 15
  2755. (should
  2756. (equal '(9 25)
  2757. (test-org-gen-loc-list "
  2758. #+BEGIN_EXAMPLE -n 10
  2759. Text_10
  2760. Second line(11)
  2761. #+END_EXAMPLE
  2762. #+BEGIN_EXAMPLE +n 15
  2763. Text line (11 + 15)
  2764. #+END_EXAMPLE"
  2765. 'example-block)))
  2766. (should
  2767. (equal '(9 19 0)
  2768. (test-org-gen-loc-list "
  2769. #+BEGIN_EXAMPLE -n 10
  2770. Text
  2771. #+END_EXAMPLE
  2772. #+BEGIN_EXAMPLE +n 10
  2773. Text
  2774. #+END_EXAMPLE
  2775. #+BEGIN_EXAMPLE -n
  2776. Text
  2777. #+END_EXAMPLE"
  2778. 'example-block)))
  2779. ;; an Example Block without -n does not add to the line count.
  2780. (should
  2781. (equal '(9 no-loc 19)
  2782. (test-org-gen-loc-list "
  2783. #+BEGIN_EXAMPLE -n 10
  2784. Text
  2785. #+END_EXAMPLE
  2786. #+BEGIN_EXAMPLE
  2787. Text
  2788. #+END_EXAMPLE
  2789. #+BEGIN_EXAMPLE +n 10
  2790. Text
  2791. #+END_EXAMPLE"
  2792. 'example-block)))
  2793. ;; "-n" resets line number.
  2794. (should
  2795. (equal
  2796. '(0)
  2797. (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
  2798. 'src-block)))
  2799. ;; The first "+n" has 0 lines before it.
  2800. (should
  2801. (equal '(0)
  2802. (test-org-gen-loc-list
  2803. "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
  2804. 'src-block)))
  2805. ;; "-n 10" resets line number but has "9 lines" before it.
  2806. (should
  2807. (equal '(9)
  2808. (test-org-gen-loc-list
  2809. "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
  2810. 'src-block)))
  2811. (should
  2812. (equal '(9 25)
  2813. (test-org-gen-loc-list "
  2814. #+BEGIN_SRC emacs-lisp -n 10
  2815. (- 10 1)
  2816. (+ (- 10 1) 1)
  2817. #+END_SRC
  2818. #+BEGIN_SRC emacs-lisp +n 15
  2819. (+ (- 10 1) 2 (- 15 1))
  2820. #+END_SRC"
  2821. 'src-block)))
  2822. (should
  2823. (equal '(9 19 0)
  2824. (test-org-gen-loc-list "
  2825. #+BEGIN_SRC emacs-lisp -n 10
  2826. (- 10 1)
  2827. #+END_SRC
  2828. #+BEGIN_SRC emacs-lisp +n 10
  2829. (+ (- 10 1) 1 (- 10 1))
  2830. #+END_SRC
  2831. #+BEGIN_SRC emacs-lisp -n
  2832. (- 1 1)
  2833. #+END_SRC"
  2834. 'src-block)))
  2835. ;; A SRC Block without -n does not add to the line count.
  2836. (should
  2837. (equal '(9 no-loc 19)
  2838. (test-org-gen-loc-list
  2839. "#+BEGIN_SRC emacs-lisp -n 10
  2840. (+ (-10 1) 1)
  2841. #+END_SRC
  2842. #+BEGIN_SRC emacs-lisp
  2843. (+ 2 2)
  2844. #+END_SRC
  2845. #+BEGIN_SRC emacs-lisp +n 10
  2846. (+ (- 10 1) 1 (- 10 1))
  2847. #+END_SRC"
  2848. 'src-block))))
  2849. (ert-deftest test-org-export/resolve-coderef ()
  2850. "Test `org-export-resolve-coderef' specifications."
  2851. (let ((org-coderef-label-format "(ref:%s)"))
  2852. ;; A link to a "-n -k -r" block returns line number.
  2853. (should
  2854. (= 1
  2855. (org-test-with-parsed-data
  2856. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2857. (org-export-resolve-coderef "coderef" info))))
  2858. (should
  2859. (= 10
  2860. (org-test-with-parsed-data
  2861. "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2862. (org-export-resolve-coderef "coderef" info))))
  2863. (should
  2864. (= 135
  2865. (org-test-with-parsed-data
  2866. "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
  2867. #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2868. (org-export-resolve-coderef "coderef" info))))
  2869. (should
  2870. (= 1
  2871. (org-test-with-parsed-data
  2872. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2873. (org-export-resolve-coderef "coderef" info))))
  2874. (should
  2875. (= 10
  2876. (org-test-with-parsed-data
  2877. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2878. (org-export-resolve-coderef "coderef" info))))
  2879. (should
  2880. (= 135
  2881. (org-test-with-parsed-data
  2882. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
  2883. #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2884. (org-export-resolve-coderef "coderef" info))))
  2885. ;; A link to a "-n -r" block returns line number.
  2886. (should
  2887. (= 1
  2888. (org-test-with-parsed-data
  2889. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2890. (org-export-resolve-coderef "coderef" info))))
  2891. (should
  2892. (= 10
  2893. (org-test-with-parsed-data
  2894. "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2895. (org-export-resolve-coderef "coderef" info))))
  2896. (should
  2897. (= 135
  2898. (org-test-with-parsed-data
  2899. "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
  2900. #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2901. (org-export-resolve-coderef "coderef" info))))
  2902. (should
  2903. (= 1
  2904. (org-test-with-parsed-data
  2905. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2906. (org-export-resolve-coderef "coderef" info))))
  2907. (should
  2908. (= 10
  2909. (org-test-with-parsed-data
  2910. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2911. (org-export-resolve-coderef "coderef" info))))
  2912. (should
  2913. (= 135
  2914. (org-test-with-parsed-data
  2915. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
  2916. #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2917. (org-export-resolve-coderef "coderef" info))))
  2918. ;; A link to a "-n" block returns coderef.
  2919. (should
  2920. (equal "coderef"
  2921. (org-test-with-parsed-data
  2922. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2923. (org-export-resolve-coderef "coderef" info))))
  2924. (should
  2925. (equal "coderef"
  2926. (org-test-with-parsed-data
  2927. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2928. (org-export-resolve-coderef "coderef" info))))
  2929. ;; A link to a "-r" block returns line number.
  2930. (should
  2931. (= 1
  2932. (org-test-with-parsed-data
  2933. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2934. (org-export-resolve-coderef "coderef" info))))
  2935. (should
  2936. (= 1
  2937. (org-test-with-parsed-data
  2938. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2939. (org-export-resolve-coderef "coderef" info))))
  2940. ;; A link to a block without a switch returns coderef.
  2941. (should
  2942. (equal "coderef"
  2943. (org-test-with-parsed-data
  2944. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2945. (org-export-resolve-coderef "coderef" info))))
  2946. (org-test-with-parsed-data
  2947. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2948. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2949. ;; Correctly handle continued line numbers. A "+n" switch should
  2950. ;; resume numbering from previous block with numbered lines,
  2951. ;; ignoring blocks not numbering lines in the process. A "-n"
  2952. ;; switch resets count.
  2953. (should
  2954. (equal '(2 1)
  2955. (org-test-with-parsed-data "
  2956. #+BEGIN_EXAMPLE -n
  2957. Text.
  2958. #+END_EXAMPLE
  2959. #+BEGIN_SRC emacs-lisp
  2960. \(- 1 1)
  2961. #+END_SRC
  2962. #+BEGIN_SRC emacs-lisp +n -r
  2963. \(+ 1 1) (ref:addition)
  2964. #+END_SRC
  2965. #+BEGIN_EXAMPLE -n -r
  2966. Another text. (ref:text)
  2967. #+END_EXAMPLE"
  2968. (list (org-export-resolve-coderef "addition" info)
  2969. (org-export-resolve-coderef "text" info)))))
  2970. ;; Recognize coderef with user-specified syntax.
  2971. (should
  2972. (equal
  2973. "text"
  2974. (org-test-with-parsed-data
  2975. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2976. (org-export-resolve-coderef "text" info))))
  2977. ;; Unresolved coderefs raise a `org-link-broken' signal.
  2978. (should
  2979. (condition-case nil
  2980. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2981. (org-export-resolve-coderef "unknown" info))
  2982. (org-link-broken t)))))
  2983. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2984. "Test `org-export-resolve-fuzzy-link' specifications."
  2985. ;; Match target objects.
  2986. (should
  2987. (org-test-with-parsed-data "<<target>> [[target]]"
  2988. (org-export-resolve-fuzzy-link
  2989. (org-element-map tree 'link 'identity info t) info)))
  2990. ;; Match named elements.
  2991. (should
  2992. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2993. (org-export-resolve-fuzzy-link
  2994. (org-element-map tree 'link 'identity info t) info)))
  2995. ;; Match exact headline's name.
  2996. (should
  2997. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2998. (org-export-resolve-fuzzy-link
  2999. (org-element-map tree 'link 'identity info t) info)))
  3000. ;; Targets objects have priority over headline titles.
  3001. (should
  3002. (eq 'target
  3003. (org-test-with-parsed-data "* target\n<<target>>[[target]]"
  3004. (org-element-type
  3005. (org-export-resolve-fuzzy-link
  3006. (org-element-map tree 'link 'identity info t) info)))))
  3007. ;; Named elements have priority over headline titles.
  3008. (should
  3009. (eq 'paragraph
  3010. (org-test-with-parsed-data
  3011. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  3012. (org-element-type
  3013. (org-export-resolve-fuzzy-link
  3014. (org-element-map tree 'link 'identity info t) info)))))
  3015. ;; If link's path starts with a "*", only match headline titles,
  3016. ;; though.
  3017. (should
  3018. (eq 'headline
  3019. (org-test-with-parsed-data
  3020. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  3021. (org-element-type
  3022. (org-export-resolve-fuzzy-link
  3023. (org-element-map tree 'link 'identity info t) info)))))
  3024. ;; Raise a `org-link-broken' signal if no match.
  3025. (should
  3026. (org-test-with-parsed-data "[[target]]"
  3027. (condition-case nil
  3028. (org-export-resolve-fuzzy-link
  3029. (org-element-map tree 'link #'identity info t) info)
  3030. (org-link-broken t))))
  3031. ;; Match fuzzy link even when before first headline.
  3032. (should
  3033. (eq 'headline
  3034. (org-test-with-parsed-data "[[hl]]\n* hl"
  3035. (org-element-type
  3036. (org-export-resolve-fuzzy-link
  3037. (org-element-map tree 'link 'identity info t) info)))))
  3038. ;; Handle url-encoded fuzzy links.
  3039. (should
  3040. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  3041. (org-export-resolve-fuzzy-link
  3042. (org-element-map tree 'link #'identity info t) info))))
  3043. (ert-deftest test-org-export/resolve-id-link ()
  3044. "Test `org-export-resolve-id-link' specifications."
  3045. ;; Regular test for custom-id link.
  3046. (should
  3047. (equal '("Headline1")
  3048. (org-test-with-parsed-data "* Headline1
  3049. :PROPERTIES:
  3050. :CUSTOM_ID: test
  3051. :END:
  3052. * Headline 2
  3053. \[[#test]]"
  3054. (org-element-property
  3055. :title
  3056. (org-export-resolve-id-link
  3057. (org-element-map tree 'link 'identity info t) info)))))
  3058. ;; Raise a `org-link-broken' signal on failing searches.
  3059. (should
  3060. (org-test-with-parsed-data "* Headline1
  3061. :PROPERTIES:
  3062. :CUSTOM_ID: test
  3063. :END:
  3064. * Headline 2
  3065. \[[#no-match]]"
  3066. (condition-case nil
  3067. (org-export-resolve-id-link
  3068. (org-element-map tree 'link #'identity info t) info)
  3069. (org-link-broken t))))
  3070. ;; Test for internal id target.
  3071. (should
  3072. (equal '("Headline1")
  3073. (org-test-with-parsed-data "* Headline1
  3074. :PROPERTIES:
  3075. :ID: aaaa
  3076. :END:
  3077. * Headline 2
  3078. \[[id:aaaa]]"
  3079. (org-element-property
  3080. :title
  3081. (org-export-resolve-id-link
  3082. (org-element-map tree 'link 'identity info t) info)))))
  3083. ;; Test for external id target.
  3084. (should
  3085. (equal
  3086. "external-file"
  3087. (org-test-with-parsed-data "[[id:aaaa]]"
  3088. (org-export-resolve-id-link
  3089. (org-element-map tree 'link 'identity info t)
  3090. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  3091. (ert-deftest test-org-export/resolve-radio-link ()
  3092. "Test `org-export-resolve-radio-link' specifications."
  3093. ;; Standard test.
  3094. (should
  3095. (org-test-with-temp-text "<<<radio>>> radio"
  3096. (org-update-radio-target-regexp)
  3097. (let* ((tree (org-element-parse-buffer))
  3098. (info `(:parse-tree ,tree)))
  3099. (org-export-resolve-radio-link
  3100. (org-element-map tree 'link 'identity info t)
  3101. info))))
  3102. ;; Radio targets are case-insensitive.
  3103. (should
  3104. (org-test-with-temp-text "<<<RADIO>>> radio"
  3105. (org-update-radio-target-regexp)
  3106. (let* ((tree (org-element-parse-buffer))
  3107. (info `(:parse-tree ,tree)))
  3108. (org-export-resolve-radio-link
  3109. (org-element-map tree 'link 'identity info t)
  3110. info))))
  3111. ;; Radio target with objects.
  3112. (should
  3113. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  3114. (org-update-radio-target-regexp)
  3115. (let* ((tree (org-element-parse-buffer))
  3116. (info `(:parse-tree ,tree)))
  3117. (org-export-resolve-radio-link
  3118. (org-element-map tree 'link 'identity info t)
  3119. info))))
  3120. ;; Radio target with objects at its beginning.
  3121. (should
  3122. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  3123. (org-update-radio-target-regexp)
  3124. (let* ((tree (org-element-parse-buffer))
  3125. (info `(:parse-tree ,tree)))
  3126. (org-export-resolve-radio-link
  3127. (org-element-map tree 'link 'identity info t)
  3128. info))))
  3129. ;; Radio link next to an apostrophe.
  3130. (should
  3131. (org-test-with-temp-text "<<<radio>>> radio's"
  3132. (org-update-radio-target-regexp)
  3133. (let* ((tree (org-element-parse-buffer))
  3134. (info `(:parse-tree ,tree)))
  3135. (org-export-resolve-radio-link
  3136. (org-element-map tree 'link 'identity info t)
  3137. info))))
  3138. ;; Multiple radio targets.
  3139. (should
  3140. (equal '("radio1" "radio2")
  3141. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  3142. (org-update-radio-target-regexp)
  3143. (let* ((tree (org-element-parse-buffer))
  3144. (info `(:parse-tree ,tree)))
  3145. (org-element-map tree 'link
  3146. (lambda (link)
  3147. (org-element-property
  3148. :value (org-export-resolve-radio-link link info)))
  3149. info)))))
  3150. ;; Radio target is whitespace insensitive.
  3151. (should
  3152. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  3153. (org-update-radio-target-regexp)
  3154. (let* ((tree (org-element-parse-buffer))
  3155. (info `(:parse-tree ,tree)))
  3156. (org-element-map tree 'link
  3157. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  3158. (ert-deftest test-org-export/file-uri ()
  3159. "Test `org-export-file-uri' specifications."
  3160. ;; Preserve relative filenames.
  3161. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  3162. ;; Local files start with "file:///"
  3163. (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
  3164. ;; Remote files start with "file://"
  3165. (should (equal "file://myself@some.where:papers/last.pdf"
  3166. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  3167. (should (equal "file://localhost/etc/fstab"
  3168. (org-export-file-uri "//localhost/etc/fstab")))
  3169. ;; Expand filename starting with "~".
  3170. (should (equal (org-export-file-uri "~/file.org")
  3171. (concat "file://" (expand-file-name "~/file.org")))))
  3172. (ert-deftest test-org-export/get-reference ()
  3173. "Test `org-export-get-reference' specifications."
  3174. (should
  3175. (org-test-with-parsed-data "* Headline"
  3176. (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
  3177. info)))
  3178. ;; For a given element always return the same reference.
  3179. (should
  3180. (org-test-with-parsed-data "* Headline"
  3181. (let ((headline (org-element-map tree 'headline #'identity nil t)))
  3182. (equal (org-export-get-reference headline info)
  3183. (org-export-get-reference headline info)))))
  3184. ;; References get through local export back-ends.
  3185. (should
  3186. (org-test-with-parsed-data "* Headline"
  3187. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3188. (backend
  3189. (org-export-create-backend
  3190. :transcoders
  3191. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3192. (equal (org-trim (org-export-data-with-backend headline backend info))
  3193. (org-export-get-reference headline info)))))
  3194. (should
  3195. (org-test-with-parsed-data "* Headline"
  3196. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3197. (backend
  3198. (org-export-create-backend
  3199. :transcoders
  3200. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3201. (equal (org-export-with-backend backend headline nil info)
  3202. (org-export-get-reference headline info)))))
  3203. ;; Use search cells defined in `:crossrefs'. However, handle
  3204. ;; duplicate search cells.
  3205. (should
  3206. (equal "org0000001"
  3207. (org-test-with-parsed-data "* Headline"
  3208. (let* ((headline (org-element-map tree 'headline #'identity nil t))
  3209. (search-cell (car (org-export-search-cells headline))))
  3210. (setq info
  3211. (plist-put info :crossrefs (list (cons search-cell 1))))
  3212. (org-export-get-reference headline info)))))
  3213. (should-not
  3214. (equal '("org0000001" "org0000001")
  3215. (org-test-with-parsed-data "* H\n** H"
  3216. (org-element-map tree 'headline
  3217. (lambda (h)
  3218. (let* ((search-cell (car (org-export-search-cells h)))
  3219. (info (plist-put info :crossrefs
  3220. (list (cons search-cell 1)))))
  3221. (org-export-get-reference h info))))))))
  3222. ;;; Pseudo objects and pseudo elements
  3223. (ert-deftest test-org-export/pseudo-elements ()
  3224. "Test exporting pseudo-elements."
  3225. ;; Handle blank lines after pseudo-elements. In particular, do not
  3226. ;; replace them with white spaces.
  3227. (should
  3228. (equal "contents\n\nparagraph\n"
  3229. (let ((backend (org-export-create-backend
  3230. :transcoders
  3231. '((pseudo-element . (lambda (_p c _i) c))
  3232. (paragraph . (lambda (_p c _i) c))
  3233. (plain-text . (lambda (c _i) c)))))
  3234. (element '(pseudo-element (:post-blank 1) "contents"))
  3235. (paragraph '(paragraph nil "paragraph"))
  3236. (data '(org-data nil)))
  3237. (org-element-adopt-elements data element paragraph)
  3238. (org-export-data-with-backend data backend nil)))))
  3239. (ert-deftest test-org-export/pseudo-objects ()
  3240. "Test exporting pseudo-objects."
  3241. ;; Handle blank spaces after pseudo-objects. In particular, do not
  3242. ;; replace them with newlines.
  3243. (should
  3244. (equal "begin x end\n"
  3245. (let ((backend (org-export-create-backend
  3246. :transcoders
  3247. '((pseudo-object . (lambda (_p c _i) c))
  3248. (paragraph . (lambda (_p c _i) c))
  3249. (plain-text . (lambda (c _i) c)))))
  3250. (object '(pseudo-object (:post-blank 1) "x"))
  3251. (paragraph '(paragraph nil)))
  3252. (org-element-adopt-elements paragraph "begin " object "end")
  3253. (org-export-data-with-backend paragraph backend nil)))))
  3254. ;;; Src-block and example-block
  3255. (ert-deftest test-org-export/unravel-code ()
  3256. "Test `org-export-unravel-code' function."
  3257. ;; Code without reference.
  3258. (should
  3259. (equal '("(+ 1 1)\n")
  3260. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3261. (org-export-unravel-code (org-element-at-point)))))
  3262. ;; Code with reference.
  3263. (should
  3264. (equal '("(+ 1 1)\n" (1 . "test"))
  3265. (org-test-with-temp-text
  3266. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  3267. (let ((org-coderef-label-format "(ref:%s)"))
  3268. (org-export-unravel-code (org-element-at-point))))))
  3269. ;; Code with user-defined reference.
  3270. (should
  3271. (equal
  3272. '("(+ 1 1)\n" (1 . "test"))
  3273. (org-test-with-temp-text
  3274. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  3275. (let ((org-coderef-label-format "(ref:%s)"))
  3276. (org-export-unravel-code (org-element-at-point))))))
  3277. ;; Code references keys are relative to the current block.
  3278. (should
  3279. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  3280. (org-test-with-temp-text "
  3281. #+BEGIN_EXAMPLE -n
  3282. \(+ 1 1)
  3283. #+END_EXAMPLE
  3284. #+BEGIN_EXAMPLE +n
  3285. \(+ 2 2)
  3286. \(+ 3 3) (ref:one)
  3287. #+END_EXAMPLE"
  3288. (goto-line 5)
  3289. (let ((org-coderef-label-format "(ref:%s)"))
  3290. (org-export-unravel-code (org-element-at-point)))))))
  3291. (ert-deftest test-org-export/format-code-default ()
  3292. "Test `org-export-format-code-default' specifications."
  3293. ;; Return the empty string when code is empty.
  3294. (should
  3295. (equal ""
  3296. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  3297. (org-export-format-code-default
  3298. (org-element-map tree 'src-block 'identity info t) info))))
  3299. ;; Number lines, two whitespace characters before the actual loc.
  3300. (should
  3301. (equal "1 a\n2 b\n"
  3302. (org-test-with-parsed-data
  3303. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  3304. (org-export-format-code-default
  3305. (org-element-map tree 'src-block 'identity info t) info))))
  3306. ;; Put references 6 whitespace characters after the widest line,
  3307. ;; wrapped within parenthesis.
  3308. (should
  3309. (equal "123 (a)\n1 (b)\n"
  3310. (let ((org-coderef-label-format "(ref:%s)"))
  3311. (org-test-with-parsed-data
  3312. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  3313. (org-export-format-code-default
  3314. (org-element-map tree 'src-block 'identity info t) info))))))
  3315. ;;; Smart Quotes
  3316. (ert-deftest test-org-export/activate-smart-quotes ()
  3317. "Test `org-export-activate-smart-quotes' specifications."
  3318. ;; Double quotes: standard test.
  3319. (should
  3320. (equal
  3321. '("some &ldquo;quoted&rdquo; text")
  3322. (let ((org-export-default-language "en"))
  3323. (org-test-with-parsed-data "some \"quoted\" text"
  3324. (org-element-map tree 'plain-text
  3325. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3326. info)))))
  3327. ;; Opening quotes: at the beginning of a paragraph.
  3328. (should
  3329. (equal
  3330. '("&ldquo;begin")
  3331. (let ((org-export-default-language "en"))
  3332. (org-test-with-parsed-data "\"begin"
  3333. (org-element-map tree 'plain-text
  3334. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3335. info)))))
  3336. ;; Opening quotes: after an object.
  3337. (should
  3338. (equal
  3339. '("&ldquo;quoted&rdquo; text")
  3340. (let ((org-export-default-language "en"))
  3341. (org-test-with-parsed-data "=verb= \"quoted\" text"
  3342. (org-element-map tree 'plain-text
  3343. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3344. info)))))
  3345. ;; Closing quotes: at the end of a paragraph.
  3346. (should
  3347. (equal
  3348. '("Quoted &ldquo;text&rdquo;")
  3349. (let ((org-export-default-language "en"))
  3350. (org-test-with-parsed-data "Quoted \"text\""
  3351. (org-element-map tree 'plain-text
  3352. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3353. info)))))
  3354. ;; Inner quotes: standard test.
  3355. (should
  3356. (equal '("« outer « inner » outer »")
  3357. (let ((org-export-default-language "fr"))
  3358. (org-test-with-parsed-data "\"outer 'inner' outer\""
  3359. (org-element-map tree 'plain-text
  3360. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3361. info)))))
  3362. ;; Inner quotes: close to special symbols.
  3363. (should
  3364. (equal '("« outer (« inner ») outer »")
  3365. (let ((org-export-default-language "fr"))
  3366. (org-test-with-parsed-data "\"outer ('inner') outer\""
  3367. (org-element-map tree 'plain-text
  3368. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3369. info)))))
  3370. (should
  3371. (equal '("« « inner » »")
  3372. (let ((org-export-default-language "fr"))
  3373. (org-test-with-parsed-data "\"'inner'\""
  3374. (org-element-map tree 'plain-text
  3375. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3376. info)))))
  3377. ;; Apostrophe: standard test.
  3378. (should
  3379. (equal '("It « shouldn’t » fail")
  3380. (let ((org-export-default-language "fr"))
  3381. (org-test-with-parsed-data "It \"shouldn't\" fail"
  3382. (org-element-map tree 'plain-text
  3383. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3384. info)))))
  3385. (should
  3386. (equal '("It shouldn’t fail")
  3387. (let ((org-export-default-language "fr"))
  3388. (org-test-with-parsed-data "It shouldn't fail"
  3389. (org-element-map tree 'plain-text
  3390. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3391. info)))))
  3392. ;; Apostrophe: before an object.
  3393. (should
  3394. (equal
  3395. '("« a’" " »")
  3396. (let ((org-export-default-language "fr"))
  3397. (org-test-with-parsed-data "\"a'=b=\""
  3398. (org-element-map tree 'plain-text
  3399. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3400. info)))))
  3401. ;; Apostrophe: after an object.
  3402. (should
  3403. (equal '("« " "’s »")
  3404. (let ((org-export-default-language "fr"))
  3405. (org-test-with-parsed-data "\"=code='s\""
  3406. (org-element-map tree 'plain-text
  3407. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3408. info)))))
  3409. ;; Special case: isolated quotes.
  3410. (should
  3411. (equal '("&ldquo;" "&rdquo;")
  3412. (let ((org-export-default-language "en"))
  3413. (org-test-with-parsed-data "\"$x$\""
  3414. (org-element-map tree 'plain-text
  3415. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3416. info)))))
  3417. ;; Smart quotes in secondary strings.
  3418. (should
  3419. (equal '("&ldquo;" "&rdquo;")
  3420. (let ((org-export-default-language "en"))
  3421. (org-test-with-parsed-data "* \"$x$\""
  3422. (org-element-map tree 'plain-text
  3423. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3424. info)))))
  3425. ;; Smart quotes in document keywords.
  3426. (should
  3427. (equal '("&ldquo;" "&rdquo;")
  3428. (let ((org-export-default-language "en"))
  3429. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  3430. (org-element-map (plist-get info :title) 'plain-text
  3431. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3432. info)))))
  3433. ;; Smart quotes in parsed affiliated keywords.
  3434. (should
  3435. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  3436. (let ((org-export-default-language "en"))
  3437. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3438. (org-element-map tree 'plain-text
  3439. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3440. info nil nil t)))))
  3441. ;; Smart quotes within objects.
  3442. (should
  3443. (equal '("&ldquo;foo&rdquo;")
  3444. (let ((org-export-default-language "en"))
  3445. (org-test-with-parsed-data "| \"foo\" |"
  3446. (org-element-map tree 'plain-text
  3447. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3448. info nil nil t)))))
  3449. ;; FIXME: Test failing non-interactively.
  3450. ;;
  3451. ;; (should
  3452. ;; (equal '("&ldquo;foo&rdquo;")
  3453. ;; (let ((org-export-default-language "en"))
  3454. ;; (org-test-with-parsed-data "*\"foo\"*"
  3455. ;; (org-element-map tree 'plain-text
  3456. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3457. ;; info nil nil t)))))
  3458. )
  3459. ;;; Tables
  3460. (ert-deftest test-org-export/special-column ()
  3461. "Test if the table's special column is properly recognized."
  3462. ;; 1. First column is special if it contains only a special marking
  3463. ;; characters or empty cells.
  3464. (org-test-with-temp-text "
  3465. | ! | 1 |
  3466. | | 2 |"
  3467. (should
  3468. (org-export-table-has-special-column-p
  3469. (org-element-map
  3470. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3471. ;; 2. If the column contains anything else, it isn't special.
  3472. (org-test-with-temp-text "
  3473. | ! | 1 |
  3474. | b | 2 |"
  3475. (should-not
  3476. (org-export-table-has-special-column-p
  3477. (org-element-map
  3478. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3479. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3480. ;; and "!".
  3481. (org-test-with-temp-text "
  3482. | # | 1 |
  3483. | ^ | 2 |
  3484. | * | 3 |
  3485. | _ | 4 |
  3486. | / | 5 |
  3487. | $ | 6 |
  3488. | ! | 7 |"
  3489. (should
  3490. (org-export-table-has-special-column-p
  3491. (org-element-map
  3492. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3493. ;; 4. A first column with only empty cells isn't considered as
  3494. ;; special.
  3495. (org-test-with-temp-text "
  3496. | | 1 |
  3497. | | 2 |"
  3498. (should-not
  3499. (org-export-table-has-special-column-p
  3500. (org-element-map
  3501. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3502. (ert-deftest test-org-export/table-row-is-special-p ()
  3503. "Test `org-export-table-row-is-special-p' specifications."
  3504. ;; 1. A row is special if it has a special marking character in the
  3505. ;; special column.
  3506. (org-test-with-parsed-data "| ! | 1 |"
  3507. (should
  3508. (org-export-table-row-is-special-p
  3509. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3510. ;; 2. A row is special when its first field is "/"
  3511. (org-test-with-parsed-data "
  3512. | / | 1 |
  3513. | a | b |"
  3514. (should
  3515. (org-export-table-row-is-special-p
  3516. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3517. ;; 3. A row only containing alignment cookies is also considered as
  3518. ;; special.
  3519. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3520. (should
  3521. (org-export-table-row-is-special-p
  3522. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3523. ;; 4. Everything else isn't considered as special.
  3524. (org-test-with-parsed-data "| \alpha | | c |"
  3525. (should-not
  3526. (org-export-table-row-is-special-p
  3527. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3528. ;; 5. Table's rules are never considered as special rows.
  3529. (org-test-with-parsed-data "|---+---|"
  3530. (should-not
  3531. (org-export-table-row-is-special-p
  3532. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3533. (ert-deftest test-org-export/has-header-p ()
  3534. "Test `org-export-table-has-header-p' specifications."
  3535. ;; With an header.
  3536. (should
  3537. (org-test-with-parsed-data "
  3538. | a | b |
  3539. |---+---|
  3540. | c | d |"
  3541. (org-export-table-has-header-p
  3542. (org-element-map tree 'table 'identity info 'first-match)
  3543. info)))
  3544. ;; Without an header.
  3545. (should-not
  3546. (org-test-with-parsed-data "
  3547. | a | b |
  3548. | c | d |"
  3549. (org-export-table-has-header-p
  3550. (org-element-map tree 'table 'identity info 'first-match)
  3551. info)))
  3552. ;; Don't get fooled with starting and ending rules.
  3553. (should-not
  3554. (org-test-with-parsed-data "
  3555. |---+---|
  3556. | a | b |
  3557. | c | d |
  3558. |---+---|"
  3559. (org-export-table-has-header-p
  3560. (org-element-map tree 'table 'identity info 'first-match)
  3561. info))))
  3562. (ert-deftest test-org-export/table-row-group ()
  3563. "Test `org-export-table-row-group' specifications."
  3564. ;; A rule creates a new group.
  3565. (should
  3566. (equal '(1 rule 2)
  3567. (org-test-with-parsed-data "
  3568. | a | b |
  3569. |---+---|
  3570. | 1 | 2 |"
  3571. (org-element-map tree 'table-row
  3572. (lambda (row)
  3573. (if (eq (org-element-property :type row) 'rule) 'rule
  3574. (org-export-table-row-group row info)))))))
  3575. ;; Special rows are ignored in count.
  3576. (should
  3577. (equal
  3578. '(rule 1)
  3579. (org-test-with-parsed-data "
  3580. | / | < | > |
  3581. |---|---+---|
  3582. | | 1 | 2 |"
  3583. (org-element-map tree 'table-row
  3584. (lambda (row)
  3585. (if (eq (org-element-property :type row) 'rule) 'rule
  3586. (org-export-table-row-group row info)))
  3587. info))))
  3588. ;; Double rules also are ignored in count.
  3589. (should
  3590. (equal '(1 rule rule 2)
  3591. (org-test-with-parsed-data "
  3592. | a | b |
  3593. |---+---|
  3594. |---+---|
  3595. | 1 | 2 |"
  3596. (org-element-map tree 'table-row
  3597. (lambda (row)
  3598. (if (eq (org-element-property :type row) 'rule) 'rule
  3599. (org-export-table-row-group row info))))))))
  3600. (ert-deftest test-org-export/table-row-number ()
  3601. "Test `org-export-table-row-number' specifications."
  3602. ;; Standard test. Number is 0-indexed.
  3603. (should
  3604. (equal '(0 1)
  3605. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3606. (org-element-map tree 'table-row
  3607. (lambda (row) (org-export-table-row-number row info)) info))))
  3608. ;; Number ignores separators.
  3609. (should
  3610. (equal '(0 1)
  3611. (org-test-with-parsed-data "
  3612. | a | b | c |
  3613. |---+---+---|
  3614. | d | e | f |"
  3615. (org-element-map tree 'table-row
  3616. (lambda (row) (org-export-table-row-number row info)) info))))
  3617. ;; Number ignores special rows.
  3618. (should
  3619. (equal '(0 1)
  3620. (org-test-with-parsed-data "
  3621. | / | < | > |
  3622. | | b | c |
  3623. |---+-----+-----|
  3624. | | <c> | <c> |
  3625. | | e | f |"
  3626. (org-element-map tree 'table-row
  3627. (lambda (row) (org-export-table-row-number row info)) info)))))
  3628. (ert-deftest test-org-export/table-cell-width ()
  3629. "Test `org-export-table-cell-width' specifications."
  3630. ;; 1. Width is primarily determined by width cookies. If no cookie
  3631. ;; is found, cell's width is nil.
  3632. (org-test-with-parsed-data "
  3633. | / | <l> | <6> | <l7> |
  3634. | | a | b | c |"
  3635. (should
  3636. (equal
  3637. '(nil 6 7)
  3638. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3639. (org-element-map tree 'table-cell 'identity info)))))
  3640. ;; 2. The last width cookie has precedence.
  3641. (org-test-with-parsed-data "
  3642. | <6> |
  3643. | <7> |
  3644. | a |"
  3645. (should
  3646. (equal
  3647. '(7)
  3648. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3649. (org-element-map tree 'table-cell 'identity info)))))
  3650. ;; 3. Valid width cookies must have a specific row.
  3651. (org-test-with-parsed-data "| <6> | cell |"
  3652. (should
  3653. (equal
  3654. '(nil nil)
  3655. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3656. (org-element-map tree 'table-cell 'identity))))))
  3657. (ert-deftest test-org-export/table-cell-alignment ()
  3658. "Test `org-export-table-cell-alignment' specifications."
  3659. ;; 1. Alignment is primarily determined by alignment cookies.
  3660. (should
  3661. (equal '(left center right)
  3662. (let ((org-table-number-fraction 0.5)
  3663. (org-table-number-regexp "^[0-9]+$"))
  3664. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3665. (mapcar (lambda (cell)
  3666. (org-export-table-cell-alignment cell info))
  3667. (org-element-map tree 'table-cell 'identity))))))
  3668. ;; 2. The last alignment cookie has precedence.
  3669. (should
  3670. (equal '(right right right)
  3671. (org-test-with-parsed-data "
  3672. | <l8> |
  3673. | cell |
  3674. | <r9> |"
  3675. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3676. (org-element-map tree 'table-cell 'identity)))))
  3677. ;; 3. If there's no cookie, cell's contents determine alignment.
  3678. ;; A column mostly made of cells containing numbers will align
  3679. ;; its cells to the right.
  3680. (should
  3681. (equal '(right right right)
  3682. (let ((org-table-number-fraction 0.5)
  3683. (org-table-number-regexp "^[0-9]+$"))
  3684. (org-test-with-parsed-data "
  3685. | 123 |
  3686. | some text |
  3687. | 12345 |"
  3688. (mapcar (lambda (cell)
  3689. (org-export-table-cell-alignment cell info))
  3690. (org-element-map tree 'table-cell 'identity))))))
  3691. ;; 4. Otherwise, they will be aligned to the left.
  3692. (should
  3693. (equal '(left left left)
  3694. (org-test-with-parsed-data "
  3695. | text |
  3696. | some text |
  3697. | \alpha |"
  3698. (mapcar (lambda (cell)
  3699. (org-export-table-cell-alignment cell info))
  3700. (org-element-map tree 'table-cell 'identity info))))))
  3701. (ert-deftest test-org-export/table-cell-borders ()
  3702. "Test `org-export-table-cell-borders' specifications."
  3703. ;; 1. Recognize various column groups indicators.
  3704. (org-test-with-parsed-data "| / | < | > | <> |"
  3705. (should
  3706. (equal
  3707. '((right bottom top) (left bottom top) (right bottom top)
  3708. (right left bottom top))
  3709. (mapcar (lambda (cell)
  3710. (org-export-table-cell-borders cell info))
  3711. (org-element-map tree 'table-cell 'identity)))))
  3712. ;; 2. Accept shortcuts to define column groups.
  3713. (org-test-with-parsed-data "| / | < | < |"
  3714. (should
  3715. (equal
  3716. '((right bottom top) (right left bottom top) (left bottom top))
  3717. (mapcar (lambda (cell)
  3718. (org-export-table-cell-borders cell info))
  3719. (org-element-map tree 'table-cell 'identity)))))
  3720. ;; 3. A valid column groups row must start with a "/".
  3721. (org-test-with-parsed-data "
  3722. | | < |
  3723. | a | b |"
  3724. (should
  3725. (equal '((top) (top) (bottom) (bottom))
  3726. (mapcar (lambda (cell)
  3727. (org-export-table-cell-borders cell info))
  3728. (org-element-map tree 'table-cell 'identity)))))
  3729. ;; 4. Take table rules into consideration.
  3730. (org-test-with-parsed-data "
  3731. | 1 |
  3732. |---|
  3733. | 2 |"
  3734. (should
  3735. (equal '((below top) (bottom above))
  3736. (mapcar (lambda (cell)
  3737. (org-export-table-cell-borders cell info))
  3738. (org-element-map tree 'table-cell 'identity)))))
  3739. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3740. ;; (resp. `bottom' and `below') borders. Any special row is
  3741. ;; ignored.
  3742. (org-test-with-parsed-data "
  3743. |---+----|
  3744. | / | |
  3745. | | 1 |
  3746. |---+----|"
  3747. (should
  3748. (equal '((bottom below top above))
  3749. (last
  3750. (mapcar (lambda (cell)
  3751. (org-export-table-cell-borders cell info))
  3752. (org-element-map tree 'table-cell 'identity)))))))
  3753. (ert-deftest test-org-export/table-dimensions ()
  3754. "Test `org-export-table-dimensions' specifications."
  3755. ;; 1. Standard test.
  3756. (org-test-with-parsed-data "
  3757. | 1 | 2 | 3 |
  3758. | 4 | 5 | 6 |"
  3759. (should
  3760. (equal '(2 . 3)
  3761. (org-export-table-dimensions
  3762. (org-element-map tree 'table 'identity info 'first-match) info))))
  3763. ;; 2. Ignore horizontal rules and special columns.
  3764. (org-test-with-parsed-data "
  3765. | / | < | > |
  3766. | 1 | 2 | 3 |
  3767. |---+---+---|
  3768. | 4 | 5 | 6 |"
  3769. (should
  3770. (equal '(2 . 3)
  3771. (org-export-table-dimensions
  3772. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3773. (ert-deftest test-org-export/table-cell-address ()
  3774. "Test `org-export-table-cell-address' specifications."
  3775. ;; 1. Standard test: index is 0-based.
  3776. (org-test-with-parsed-data "| a | b |"
  3777. (should
  3778. (equal '((0 . 0) (0 . 1))
  3779. (org-element-map tree 'table-cell
  3780. (lambda (cell) (org-export-table-cell-address cell info))
  3781. info))))
  3782. ;; 2. Special column isn't counted, nor are special rows.
  3783. (org-test-with-parsed-data "
  3784. | / | <> |
  3785. | | c |"
  3786. (should
  3787. (equal '(0 . 0)
  3788. (org-export-table-cell-address
  3789. (car (last (org-element-map tree 'table-cell 'identity info)))
  3790. info))))
  3791. ;; 3. Tables rules do not count either.
  3792. (org-test-with-parsed-data "
  3793. | a |
  3794. |---|
  3795. | b |
  3796. |---|
  3797. | c |"
  3798. (should
  3799. (equal '(2 . 0)
  3800. (org-export-table-cell-address
  3801. (car (last (org-element-map tree 'table-cell 'identity info)))
  3802. info))))
  3803. ;; 4. Return nil for special cells.
  3804. (org-test-with-parsed-data "| / | a |"
  3805. (should-not
  3806. (org-export-table-cell-address
  3807. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3808. info))))
  3809. (ert-deftest test-org-export/get-table-cell-at ()
  3810. "Test `org-export-get-table-cell-at' specifications."
  3811. ;; 1. Address ignores special columns, special rows and rules.
  3812. (org-test-with-parsed-data "
  3813. | / | <> |
  3814. | | a |
  3815. |---+----|
  3816. | | b |"
  3817. (should
  3818. (equal '("b")
  3819. (org-element-contents
  3820. (org-export-get-table-cell-at
  3821. '(1 . 0)
  3822. (org-element-map tree 'table 'identity info 'first-match)
  3823. info)))))
  3824. ;; 2. Return value for a non-existent address is nil.
  3825. (org-test-with-parsed-data "| a |"
  3826. (should-not
  3827. (org-export-get-table-cell-at
  3828. '(2 . 2)
  3829. (org-element-map tree 'table 'identity info 'first-match)
  3830. info)))
  3831. (org-test-with-parsed-data "| / |"
  3832. (should-not
  3833. (org-export-get-table-cell-at
  3834. '(0 . 0)
  3835. (org-element-map tree 'table 'identity info 'first-match)
  3836. info))))
  3837. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3838. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3839. ;; 1. A cell at a beginning of a row always starts a column group.
  3840. (org-test-with-parsed-data "| a |"
  3841. (should
  3842. (org-export-table-cell-starts-colgroup-p
  3843. (org-element-map tree 'table-cell 'identity info 'first-match)
  3844. info)))
  3845. ;; 2. Special column should be ignored when determining the
  3846. ;; beginning of the row.
  3847. (org-test-with-parsed-data "
  3848. | / | |
  3849. | | a |"
  3850. (should
  3851. (org-export-table-cell-starts-colgroup-p
  3852. (org-element-map tree 'table-cell 'identity info 'first-match)
  3853. info)))
  3854. ;; 2. Explicit column groups.
  3855. (org-test-with-parsed-data "
  3856. | / | | < |
  3857. | a | b | c |"
  3858. (should
  3859. (equal
  3860. '(yes no yes)
  3861. (org-element-map tree 'table-cell
  3862. (lambda (cell)
  3863. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3864. info)))))
  3865. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3866. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3867. ;; 1. A cell at the end of a row always ends a column group.
  3868. (org-test-with-parsed-data "| a |"
  3869. (should
  3870. (org-export-table-cell-ends-colgroup-p
  3871. (org-element-map tree 'table-cell 'identity info 'first-match)
  3872. info)))
  3873. ;; 2. Special column should be ignored when determining the
  3874. ;; beginning of the row.
  3875. (org-test-with-parsed-data "
  3876. | / | |
  3877. | | a |"
  3878. (should
  3879. (org-export-table-cell-ends-colgroup-p
  3880. (org-element-map tree 'table-cell 'identity info 'first-match)
  3881. info)))
  3882. ;; 3. Explicit column groups.
  3883. (org-test-with-parsed-data "
  3884. | / | < | |
  3885. | a | b | c |"
  3886. (should
  3887. (equal
  3888. '(yes no yes)
  3889. (org-element-map tree 'table-cell
  3890. (lambda (cell)
  3891. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3892. info)))))
  3893. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3894. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3895. ;; 1. A row at the beginning of a table always starts a row group.
  3896. ;; So does a row following a table rule.
  3897. (org-test-with-parsed-data "
  3898. | a |
  3899. |---|
  3900. | b |"
  3901. (should
  3902. (equal
  3903. '(yes no yes)
  3904. (org-element-map tree 'table-row
  3905. (lambda (row)
  3906. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3907. info))))
  3908. ;; 2. Special rows should be ignored when determining the beginning
  3909. ;; of the row.
  3910. (org-test-with-parsed-data "
  3911. | / | < |
  3912. | | a |
  3913. |---+---|
  3914. | / | < |
  3915. | | b |"
  3916. (should
  3917. (equal
  3918. '(yes no yes)
  3919. (org-element-map tree 'table-row
  3920. (lambda (row)
  3921. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3922. info)))))
  3923. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3924. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3925. ;; 1. A row at the end of a table always ends a row group. So does
  3926. ;; a row preceding a table rule.
  3927. (org-test-with-parsed-data "
  3928. | a |
  3929. |---|
  3930. | b |"
  3931. (should
  3932. (equal
  3933. '(yes no yes)
  3934. (org-element-map tree 'table-row
  3935. (lambda (row)
  3936. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3937. info))))
  3938. ;; 2. Special rows should be ignored when determining the beginning
  3939. ;; of the row.
  3940. (org-test-with-parsed-data "
  3941. | | a |
  3942. | / | < |
  3943. |---+---|
  3944. | | b |
  3945. | / | < |"
  3946. (should
  3947. (equal
  3948. '(yes no yes)
  3949. (org-element-map tree 'table-row
  3950. (lambda (row)
  3951. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3952. info)))))
  3953. (ert-deftest test-org-export/table-row-in-header-p ()
  3954. "Test `org-export-table-row-in-header-p' specifications."
  3955. ;; Standard test. Separators are always nil.
  3956. (should
  3957. (equal
  3958. '(yes no no)
  3959. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3960. (org-element-map tree 'table-row
  3961. (lambda (row)
  3962. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  3963. ;; Nil when there is no header.
  3964. (should
  3965. (equal
  3966. '(no no)
  3967. (org-test-with-parsed-data "| a |\n| b |"
  3968. (org-element-map tree 'table-row
  3969. (lambda (row)
  3970. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3971. (ert-deftest test-org-export/table-row-starts-header-p ()
  3972. "Test `org-export-table-row-starts-header-p' specifications."
  3973. ;; 1. Only the row starting the first row group starts the table
  3974. ;; header.
  3975. (org-test-with-parsed-data "
  3976. | a |
  3977. | b |
  3978. |---|
  3979. | c |"
  3980. (should
  3981. (equal
  3982. '(yes no no no)
  3983. (org-element-map tree 'table-row
  3984. (lambda (row)
  3985. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3986. info))))
  3987. ;; 2. A row cannot start an header if there's no header in the
  3988. ;; table.
  3989. (org-test-with-parsed-data "
  3990. | a |
  3991. |---|"
  3992. (should-not
  3993. (org-export-table-row-starts-header-p
  3994. (org-element-map tree 'table-row 'identity info 'first-match)
  3995. info))))
  3996. (ert-deftest test-org-export/table-row-ends-header-p ()
  3997. "Test `org-export-table-row-ends-header-p' specifications."
  3998. ;; 1. Only the row starting the first row group starts the table
  3999. ;; header.
  4000. (org-test-with-parsed-data "
  4001. | a |
  4002. | b |
  4003. |---|
  4004. | c |"
  4005. (should
  4006. (equal
  4007. '(no yes no no)
  4008. (org-element-map tree 'table-row
  4009. (lambda (row)
  4010. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  4011. info))))
  4012. ;; 2. A row cannot start an header if there's no header in the
  4013. ;; table.
  4014. (org-test-with-parsed-data "
  4015. | a |
  4016. |---|"
  4017. (should-not
  4018. (org-export-table-row-ends-header-p
  4019. (org-element-map tree 'table-row 'identity info 'first-match)
  4020. info))))
  4021. ;;; Tables of Contents
  4022. (ert-deftest test-org-export/collect-headlines ()
  4023. "Test `org-export-collect-headlines' specifications."
  4024. ;; Standard test.
  4025. (should
  4026. (= 2
  4027. (length
  4028. (org-test-with-parsed-data "* H1\n** H2"
  4029. (org-export-collect-headlines info)))))
  4030. ;; Do not collect headlines below optional argument.
  4031. (should
  4032. (= 1
  4033. (length
  4034. (org-test-with-parsed-data "* H1\n** H2"
  4035. (org-export-collect-headlines info 1)))))
  4036. ;; Never collect headlines below maximum headline level.
  4037. (should
  4038. (= 1
  4039. (length
  4040. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4041. (org-export-collect-headlines info)))))
  4042. (should
  4043. (= 1
  4044. (length
  4045. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4046. (org-export-collect-headlines info 2)))))
  4047. ;; Collect headlines locally.
  4048. (should
  4049. (= 2
  4050. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  4051. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4052. (length (org-export-collect-headlines info nil scope))))))
  4053. ;; When collecting locally, optional level is relative.
  4054. (should
  4055. (= 1
  4056. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  4057. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4058. (length (org-export-collect-headlines info 1 scope)))))))
  4059. ;;; Templates
  4060. (ert-deftest test-org-export/inner-template ()
  4061. "Test `inner-template' translator specifications."
  4062. (should
  4063. (equal "Success!"
  4064. (org-test-with-temp-text "* Headline"
  4065. (org-export-as
  4066. (org-export-create-backend
  4067. :transcoders
  4068. '((inner-template . (lambda (contents info) "Success!"))
  4069. (headline . (lambda (h c i) "Headline"))))))))
  4070. ;; Inner template is applied even in a "body-only" export.
  4071. (should
  4072. (equal "Success!"
  4073. (org-test-with-temp-text "* Headline"
  4074. (org-export-as
  4075. (org-export-create-backend
  4076. :transcoders '((inner-template . (lambda (c i) "Success!"))
  4077. (headline . (lambda (h c i) "Headline"))))
  4078. nil nil 'body-only)))))
  4079. (ert-deftest test-org-export/template ()
  4080. "Test `template' translator specifications."
  4081. (should
  4082. (equal "Success!"
  4083. (org-test-with-temp-text "* Headline"
  4084. (org-export-as
  4085. (org-export-create-backend
  4086. :transcoders '((template . (lambda (contents info) "Success!"))
  4087. (headline . (lambda (h c i) "Headline"))))))))
  4088. ;; Template is not applied in a "body-only" export.
  4089. (should-not
  4090. (equal "Success!"
  4091. (org-test-with-temp-text "* Headline"
  4092. (org-export-as
  4093. (org-export-create-backend
  4094. :transcoders '((template . (lambda (contents info) "Success!"))
  4095. (headline . (lambda (h c i) "Headline"))))
  4096. nil nil 'body-only)))))
  4097. ;;; Topology
  4098. (ert-deftest test-org-export/get-next-element ()
  4099. "Test `org-export-get-next-element' specifications."
  4100. ;; Standard test.
  4101. (should
  4102. (equal "b"
  4103. (org-test-with-parsed-data "* Headline\n*a* b"
  4104. (org-export-get-next-element
  4105. (org-element-map tree 'bold 'identity info t) info))))
  4106. ;; Return nil when no previous element.
  4107. (should-not
  4108. (org-test-with-parsed-data "* Headline\na *b*"
  4109. (org-export-get-next-element
  4110. (org-element-map tree 'bold 'identity info t) info)))
  4111. ;; Non-exportable elements are ignored.
  4112. (should-not
  4113. (let ((org-export-with-timestamps nil))
  4114. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  4115. (org-export-get-next-element
  4116. (org-element-map tree 'entity 'identity info t) info))))
  4117. ;; Find next element in secondary strings.
  4118. (should
  4119. (eq 'verbatim
  4120. (org-test-with-parsed-data "* a =verb="
  4121. (org-element-type
  4122. (org-export-get-next-element
  4123. (org-element-map tree 'plain-text 'identity info t) info)))))
  4124. (should
  4125. (eq 'verbatim
  4126. (org-test-with-parsed-data "* /italic/ =verb="
  4127. (org-element-type
  4128. (org-export-get-next-element
  4129. (org-element-map tree 'italic 'identity info t) info)))))
  4130. ;; Find next element in document keywords.
  4131. (should
  4132. (eq 'verbatim
  4133. (org-test-with-parsed-data "#+TITLE: a =verb="
  4134. (org-element-type
  4135. (org-export-get-next-element
  4136. (org-element-map
  4137. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4138. ;; Find next element in parsed affiliated keywords.
  4139. (should
  4140. (eq 'verbatim
  4141. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  4142. (org-element-type
  4143. (org-export-get-next-element
  4144. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4145. ;; With optional argument N, return a list containing all the
  4146. ;; following elements.
  4147. (should
  4148. (equal
  4149. '(bold code underline)
  4150. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4151. (mapcar 'car
  4152. (org-export-get-next-element
  4153. (org-element-map tree 'italic 'identity info t) info t)))))
  4154. ;; When N is a positive integer, return a list containing up to
  4155. ;; N following elements.
  4156. (should
  4157. (equal
  4158. '(bold code)
  4159. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4160. (mapcar 'car
  4161. (org-export-get-next-element
  4162. (org-element-map tree 'italic 'identity info t) info 2))))))
  4163. (ert-deftest test-org-export/get-previous-element ()
  4164. "Test `org-export-get-previous-element' specifications."
  4165. ;; Standard test.
  4166. (should
  4167. (equal "a "
  4168. (org-test-with-parsed-data "* Headline\na *b*"
  4169. (org-export-get-previous-element
  4170. (org-element-map tree 'bold 'identity info t) info))))
  4171. ;; Return nil when no previous element.
  4172. (should-not
  4173. (org-test-with-parsed-data "* Headline\n*a* b"
  4174. (org-export-get-previous-element
  4175. (org-element-map tree 'bold 'identity info t) info)))
  4176. ;; Non-exportable elements are ignored.
  4177. (should-not
  4178. (let ((org-export-with-timestamps nil))
  4179. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  4180. (org-export-get-previous-element
  4181. (org-element-map tree 'entity 'identity info t) info))))
  4182. ;; Find previous element in secondary strings.
  4183. (should
  4184. (eq 'verbatim
  4185. (org-test-with-parsed-data "* =verb= a"
  4186. (org-element-type
  4187. (org-export-get-previous-element
  4188. (org-element-map tree 'plain-text 'identity info t) info)))))
  4189. (should
  4190. (eq 'verbatim
  4191. (org-test-with-parsed-data "* =verb= /italic/"
  4192. (org-element-type
  4193. (org-export-get-previous-element
  4194. (org-element-map tree 'italic 'identity info t) info)))))
  4195. ;; Find previous element in document keywords.
  4196. (should
  4197. (eq 'verbatim
  4198. (org-test-with-parsed-data "#+TITLE: =verb= a"
  4199. (org-element-type
  4200. (org-export-get-previous-element
  4201. (org-element-map
  4202. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4203. ;; Find previous element in parsed affiliated keywords.
  4204. (should
  4205. (eq 'verbatim
  4206. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  4207. (org-element-type
  4208. (org-export-get-previous-element
  4209. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4210. ;; With optional argument N, return a list containing up to
  4211. ;; N previous elements.
  4212. (should
  4213. (equal '(underline italic bold)
  4214. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4215. (mapcar 'car
  4216. (org-export-get-previous-element
  4217. (org-element-map tree 'code 'identity info t) info t)))))
  4218. ;; When N is a positive integer, return a list containing up to
  4219. ;; N previous elements.
  4220. (should
  4221. (equal '(italic bold)
  4222. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4223. (mapcar 'car
  4224. (org-export-get-previous-element
  4225. (org-element-map tree 'code 'identity info t) info 2))))))
  4226. (provide 'test-ox)
  4227. ;;; test-org-export.el end here