test-ox.el 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237
  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. (org-test-with-temp-text "
  856. * Head1
  857. ** Head2
  858. text
  859. *** Head3"
  860. ;; Subtree.
  861. (forward-line 3)
  862. (should (equal (org-export-as (org-test-default-backend) 'subtree)
  863. "text\n*** Head3\n"))
  864. ;; Visible.
  865. (goto-char (point-min))
  866. (forward-line)
  867. (org-cycle)
  868. (should (equal (org-export-as (org-test-default-backend) nil 'visible)
  869. "* Head1\n"))
  870. ;; Region.
  871. (goto-char (point-min))
  872. (forward-line 3)
  873. (transient-mark-mode 1)
  874. (push-mark (point) t t)
  875. (goto-char (point-at-eol))
  876. (should (equal (org-export-as (org-test-default-backend)) "text\n")))
  877. ;; Subtree with a code block calling another block outside.
  878. (let ((org-export-babel-evaluate t))
  879. (should
  880. (equal ": 3\n"
  881. (org-test-with-temp-text "
  882. * Head1
  883. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  884. <<test>>
  885. #+END_SRC
  886. * Head2
  887. #+NAME: test
  888. #+BEGIN_SRC emacs-lisp
  889. \(+ 1 2)
  890. #+END_SRC"
  891. (forward-line 1)
  892. (org-export-as (org-test-default-backend) 'subtree)))))
  893. ;; Body only.
  894. (let ((backend (org-test-default-backend)))
  895. (setf (org-export-backend-transcoders backend)
  896. (cons '(template . (lambda (body i)
  897. (format "BEGIN\n%sEND" body)))
  898. (org-export-backend-transcoders backend)))
  899. (org-test-with-temp-text "Text"
  900. (should (equal (org-export-as backend nil nil 'body-only)
  901. "Text\n"))
  902. (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
  903. (ert-deftest test-org-export/output-file-name ()
  904. "Test `org-export-output-file-name' specifications."
  905. ;; Export from a file: name is built from original file name.
  906. (should
  907. (org-test-with-temp-text-in-file "Test"
  908. (equal (concat (file-name-as-directory ".")
  909. (file-name-nondirectory
  910. (file-name-sans-extension (buffer-file-name))))
  911. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  912. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  913. (should
  914. (org-test-with-temp-text-in-file
  915. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  916. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  917. ;; From a buffer not associated to a file, too.
  918. (should
  919. (org-test-with-temp-text
  920. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  921. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  922. ;; When provided name is absolute, preserve it.
  923. (should
  924. (org-test-with-temp-text
  925. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  926. (expand-file-name "test"))
  927. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  928. ;; When PUB-DIR argument is provided, use it.
  929. (should
  930. (org-test-with-temp-text-in-file "Test"
  931. (equal (file-name-directory
  932. (org-export-output-file-name ".ext" nil "dir/"))
  933. "dir/")))
  934. ;; When returned name would overwrite original file, add EXTENSION
  935. ;; another time.
  936. (should
  937. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  938. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  939. (ert-deftest test-org-export/expand-include ()
  940. "Test file inclusion in an Org buffer."
  941. ;; Error when file isn't specified.
  942. (should-error
  943. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  944. (org-export-expand-include-keyword)))
  945. ;; Full insertion with recursive inclusion.
  946. (should
  947. (equal
  948. (with-temp-buffer
  949. (insert-file
  950. (expand-file-name "examples/include.org" org-test-dir))
  951. (replace-regexp-in-string
  952. (regexp-quote "#+INCLUDE: \"include2.org\"")
  953. "Success!" (buffer-string)))
  954. (org-test-with-temp-text
  955. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  956. (org-export-expand-include-keyword)
  957. (buffer-string))))
  958. ;; Localized insertion.
  959. (org-test-with-temp-text
  960. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  961. org-test-dir)
  962. (org-export-expand-include-keyword)
  963. (should (equal (buffer-string)
  964. "Small Org file with an include keyword.\n")))
  965. ;; Insertion with constraints on headlines level.
  966. (should
  967. (equal
  968. "* Top heading\n** Heading\nbody\n"
  969. (org-test-with-temp-text
  970. (format
  971. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  972. org-test-dir)
  973. (org-export-expand-include-keyword)
  974. (buffer-string))))
  975. (should
  976. (equal
  977. "* Top heading\n* Heading\nbody\n"
  978. (org-test-with-temp-text
  979. (format
  980. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  981. org-test-dir)
  982. (org-export-expand-include-keyword)
  983. (buffer-string))))
  984. ;; Inclusion within an example block.
  985. (should
  986. (equal
  987. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  988. (org-test-with-temp-text
  989. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  990. org-test-dir)
  991. (org-export-expand-include-keyword)
  992. (buffer-string))))
  993. ;; Inclusion within a src-block.
  994. (should
  995. (equal
  996. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  997. (org-test-with-temp-text
  998. (format
  999. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  1000. org-test-dir)
  1001. (org-export-expand-include-keyword)
  1002. (buffer-string))))
  1003. ;; Inclusion within an html export-block.
  1004. (should
  1005. (equal
  1006. "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
  1007. (org-test-with-temp-text
  1008. (format
  1009. "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
  1010. org-test-dir)
  1011. (org-export-expand-include-keyword)
  1012. (buffer-string))))
  1013. ;; Inclusion within an center paragraph
  1014. (should
  1015. (equal
  1016. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  1017. (org-test-with-temp-text
  1018. (format
  1019. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  1020. org-test-dir)
  1021. (org-export-expand-include-keyword)
  1022. (buffer-string))))
  1023. ;; Footnotes labels are local to each included file.
  1024. (should
  1025. (= 6
  1026. (length
  1027. (delete-dups
  1028. (let ((contents "
  1029. Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
  1030. \[fn:1] Footnote 1
  1031. \[fn:test] Footnote \"test\""))
  1032. (org-test-with-temp-text-in-file contents
  1033. (let ((file1 (buffer-file-name)))
  1034. (org-test-with-temp-text-in-file contents
  1035. (let ((file2 (buffer-file-name)))
  1036. (org-test-with-temp-text
  1037. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  1038. file1 file2)
  1039. (org-export-expand-include-keyword)
  1040. (org-element-map (org-element-parse-buffer)
  1041. 'footnote-reference
  1042. (lambda (r) (org-element-property :label r)))))))))))))
  1043. ;; Footnotes labels are not local to each include keyword.
  1044. (should
  1045. (= 3
  1046. (length
  1047. (delete-dups
  1048. (let ((contents "
  1049. Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
  1050. \[fn:1] Footnote 1
  1051. \[fn:test] Footnote \"test\""))
  1052. (org-test-with-temp-text-in-file contents
  1053. (let ((file (buffer-file-name)))
  1054. (org-test-with-temp-text
  1055. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  1056. (org-export-expand-include-keyword)
  1057. (org-element-map (org-element-parse-buffer)
  1058. 'footnote-reference
  1059. (lambda (ref) (org-element-property :label ref)))))))))))
  1060. ;; Footnotes are supported by :lines-like elements and unnecessary
  1061. ;; footnotes are dropped.
  1062. (should
  1063. (= 3
  1064. (length
  1065. (delete-dups
  1066. (let ((contents "
  1067. * foo
  1068. Footnotes[fn:1]
  1069. * bar
  1070. Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
  1071. \[fn:1] Footnote 1
  1072. \[fn:2] Footnote 1
  1073. * Footnotes
  1074. \[fn:test] Footnote \"test\"
  1075. "))
  1076. (org-test-with-temp-text-in-file contents
  1077. (let ((file (buffer-file-name)))
  1078. (org-test-with-temp-text
  1079. (format "#+INCLUDE: \"%s::*bar\"\n" file)
  1080. (org-export-expand-include-keyword)
  1081. (org-element-map (org-element-parse-buffer)
  1082. 'footnote-definition
  1083. (lambda (ref) (org-element-property :label ref)))))))))))
  1084. ;; If only-contents is non-nil only include contents of element.
  1085. (should
  1086. (equal
  1087. "body\n"
  1088. (org-test-with-temp-text
  1089. (concat
  1090. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
  1091. org-test-dir)
  1092. ":only-contents t")
  1093. (org-export-expand-include-keyword)
  1094. (buffer-string))))
  1095. ;; Headings can be included via CUSTOM_ID.
  1096. (should
  1097. (org-test-with-temp-text
  1098. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1099. (org-export-expand-include-keyword)
  1100. (goto-char (point-min))
  1101. (looking-at "* Another heading")))
  1102. ;; Named objects can be included.
  1103. (should
  1104. (equal
  1105. "| 1 |\n"
  1106. (org-test-with-temp-text
  1107. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
  1108. org-test-dir)
  1109. (org-export-expand-include-keyword)
  1110. (buffer-string))))
  1111. ;; Including non-existing elements should result in an error.
  1112. (should-error
  1113. (org-test-with-temp-text
  1114. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
  1115. org-test-dir)
  1116. (org-export-expand-include-keyword)))
  1117. ;; Lines work relatively to an included element.
  1118. (should
  1119. (equal
  1120. "2\n3\n"
  1121. (org-test-with-temp-text
  1122. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
  1123. :lines \"2-3\""
  1124. org-test-dir)
  1125. (org-export-expand-include-keyword)
  1126. (buffer-string))))
  1127. ;; Properties should be dropped from headlines.
  1128. (should
  1129. (equal
  1130. (org-test-with-temp-text
  1131. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
  1132. org-test-dir)
  1133. (org-export-expand-include-keyword)
  1134. (buffer-string))
  1135. (org-test-with-temp-text
  1136. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1137. (org-export-expand-include-keyword)
  1138. (buffer-string))))
  1139. ;; Properties should be dropped, drawers should not be.
  1140. (should
  1141. (equal
  1142. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1143. (org-test-with-temp-text
  1144. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
  1145. org-test-dir)
  1146. (org-export-expand-include-keyword)
  1147. (buffer-string))))
  1148. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1149. (should
  1150. (cl-every (lambda (level) (zerop (1- level)))
  1151. (org-test-with-temp-text
  1152. (concat
  1153. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
  1154. org-test-dir)
  1155. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
  1156. org-test-dir))
  1157. (org-export-expand-include-keyword)
  1158. (org-element-map (org-element-parse-buffer) 'headline
  1159. (lambda (head) (org-element-property :level head))))))
  1160. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1161. (should-not
  1162. (equal
  1163. (org-test-with-temp-text
  1164. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
  1165. org-test-dir)
  1166. (org-export-expand-include-keyword)
  1167. (buffer-string))
  1168. (org-test-with-temp-text
  1169. (format
  1170. "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
  1171. org-test-dir)
  1172. (org-export-expand-include-keyword)
  1173. (buffer-string))))
  1174. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1175. (should
  1176. (org-test-with-temp-text-in-file
  1177. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
  1178. org-test-dir)
  1179. (narrow-to-region (point) (point-max))
  1180. (org-export-expand-include-keyword)
  1181. (eq 1 (org-current-level))))
  1182. ;; If :minlevel is present do not alter it.
  1183. (should
  1184. (org-test-with-temp-text
  1185. (format
  1186. "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
  1187. org-test-dir)
  1188. (narrow-to-region (point) (point-max))
  1189. (org-export-expand-include-keyword)
  1190. (eq 3 (org-current-level)))))
  1191. (ert-deftest test-org-export/expand-macro ()
  1192. "Test macro expansion in an Org buffer."
  1193. (require 'ox-org)
  1194. ;; Standard macro expansion.
  1195. (should
  1196. (equal "#+MACRO: macro1 value\nvalue\n"
  1197. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1198. (org-export-as (org-test-default-backend)))))
  1199. ;; Allow macro in parsed keywords and associated properties.
  1200. ;; Standard macro expansion.
  1201. (should
  1202. (string-match
  1203. "#\\+K: value"
  1204. (let ((backend (org-export-create-backend
  1205. :parent 'org
  1206. :options '((:k "K" nil nil parse)))))
  1207. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1208. (org-export-as backend)))))
  1209. (should
  1210. (string-match
  1211. ":EXPORT_K: v"
  1212. (let ((backend (org-export-create-backend
  1213. :parent 'org
  1214. :options '((:k "K" nil nil parse)))))
  1215. (org-test-with-temp-text
  1216. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1217. (org-export-as backend nil nil nil '(:with-properties t))))))
  1218. ;; Expand specific macros.
  1219. (should
  1220. (equal "me 2012-03-29 me@here Title\n"
  1221. (org-test-with-temp-text
  1222. "
  1223. #+TITLE: Title
  1224. #+DATE: 2012-03-29
  1225. #+AUTHOR: me
  1226. #+EMAIL: me@here
  1227. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1228. (let ((output (org-export-as (org-test-default-backend))))
  1229. (substring output (string-match ".*\n\\'" output))))))
  1230. ;; Expand specific macros when property contained a regular macro
  1231. ;; already.
  1232. (should
  1233. (equal "value\n"
  1234. (org-test-with-temp-text "
  1235. #+MACRO: macro1 value
  1236. #+TITLE: {{{macro1}}}
  1237. {{{title}}}"
  1238. (let ((output (org-export-as (org-test-default-backend))))
  1239. (substring output (string-match ".*\n\\'" output))))))
  1240. ;; Expand macros with templates in included files.
  1241. (should
  1242. (equal "success\n"
  1243. (org-test-with-temp-text
  1244. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1245. {{{included-macro}}}" org-test-dir)
  1246. (let ((output (org-export-as (org-test-default-backend))))
  1247. (substring output (string-match ".*\n\\'" output))))))
  1248. ;; Date macro takes a optional formatting argument
  1249. (should
  1250. (equal "09-02-15\n"
  1251. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1252. (org-export-as (org-test-default-backend)))))
  1253. ;; Only single timestamps are formatted
  1254. (should
  1255. (equal "<2015-02x-09>\n"
  1256. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1257. (org-export-as (org-test-default-backend)))))
  1258. ;; Throw an error when a macro definition is missing.
  1259. (should-error
  1260. (org-test-with-temp-text "{{{missing}}}"
  1261. (org-export-as (org-test-default-backend))))
  1262. ;; Macros defined in commented subtrees are ignored.
  1263. (should-error
  1264. (org-test-with-temp-text
  1265. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1266. (org-export-as (org-test-default-backend))))
  1267. (should-error
  1268. (org-test-with-temp-text
  1269. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1270. (org-export-as (org-test-default-backend)))))
  1271. (ert-deftest test-org-export/before-processing-hook ()
  1272. "Test `org-export-before-processing-hook'."
  1273. (should
  1274. (equal
  1275. "#+MACRO: mac val\nTest\n"
  1276. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1277. (let ((org-export-before-processing-hook
  1278. '((lambda (backend)
  1279. (while (re-search-forward "{{{" nil t)
  1280. (let ((object (org-element-context)))
  1281. (when (eq (org-element-type object) 'macro)
  1282. (delete-region
  1283. (org-element-property :begin object)
  1284. (org-element-property :end object)))))))))
  1285. (org-export-as (org-test-default-backend)))))))
  1286. (ert-deftest test-org-export/before-parsing-hook ()
  1287. "Test `org-export-before-parsing-hook'."
  1288. (should
  1289. (equal "Body 1\nBody 2\n"
  1290. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1291. (let ((org-export-before-parsing-hook
  1292. '((lambda (backend)
  1293. (goto-char (point-min))
  1294. (while (re-search-forward org-outline-regexp-bol nil t)
  1295. (delete-region
  1296. (point-at-bol) (progn (forward-line) (point))))))))
  1297. (org-export-as (org-test-default-backend)))))))
  1298. ;;; Affiliated Keywords
  1299. (ert-deftest test-org-export/read-attribute ()
  1300. "Test `org-export-read-attribute' specifications."
  1301. ;; Standard test.
  1302. (should
  1303. (equal
  1304. (org-export-read-attribute
  1305. :attr_html
  1306. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1307. (org-element-at-point)))
  1308. '(:a "1" :b "2")))
  1309. ;; Return nil on empty attribute.
  1310. (should-not
  1311. (org-export-read-attribute
  1312. :attr_html
  1313. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1314. ;; Return nil on "nil" string.
  1315. (should
  1316. (equal '(:a nil :b nil)
  1317. (org-export-read-attribute
  1318. :attr_html
  1319. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1320. (org-element-at-point)))))
  1321. ;; Return nil on empty string.
  1322. (should
  1323. (equal '(:a nil :b nil)
  1324. (org-export-read-attribute
  1325. :attr_html
  1326. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1327. (org-element-at-point)))))
  1328. ;; Return empty string when value is "".
  1329. (should
  1330. (equal '(:a "")
  1331. (org-export-read-attribute
  1332. :attr_html
  1333. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1334. (org-element-at-point)))))
  1335. ;; Return \"\" when value is """".
  1336. (should
  1337. (equal '(:a "\"\"")
  1338. (org-export-read-attribute
  1339. :attr_html
  1340. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1341. (org-element-at-point)))))
  1342. ;; Ignore text before first property.
  1343. (should-not
  1344. (member "ignore"
  1345. (org-export-read-attribute
  1346. :attr_html
  1347. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1348. (org-element-at-point))))))
  1349. (ert-deftest test-org-export/get-caption ()
  1350. "Test `org-export-get-caption' specifications."
  1351. ;; Without optional argument, return long caption
  1352. (should
  1353. (equal
  1354. '("l")
  1355. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1356. (org-export-get-caption (org-element-at-point)))))
  1357. ;; With optional argument, return short caption.
  1358. (should
  1359. (equal
  1360. '("s")
  1361. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1362. (org-export-get-caption (org-element-at-point) t))))
  1363. ;; Multiple lines are separated by white spaces.
  1364. (should
  1365. (equal
  1366. '("a" " " "b")
  1367. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1368. (org-export-get-caption (org-element-at-point))))))
  1369. ;;; Back-End Tools
  1370. (ert-deftest test-org-export/define-backend ()
  1371. "Test back-end definition and accessors."
  1372. ;; Translate table.
  1373. (should
  1374. (equal '((headline . my-headline-test))
  1375. (let (org-export-registered-backends)
  1376. (org-export-define-backend 'test '((headline . my-headline-test)))
  1377. (org-export-get-all-transcoders 'test))))
  1378. ;; Filters.
  1379. (should
  1380. (equal '((:filter-headline . my-filter))
  1381. (let (org-export-registered-backends)
  1382. (org-export-define-backend 'test
  1383. '((headline . my-headline-test))
  1384. :filters-alist '((:filter-headline . my-filter)))
  1385. (org-export-backend-filters (org-export-get-backend 'test)))))
  1386. ;; Options.
  1387. (should
  1388. (equal '((:prop value))
  1389. (let (org-export-registered-backends)
  1390. (org-export-define-backend 'test
  1391. '((headline . my-headline-test))
  1392. :options-alist '((:prop value)))
  1393. (org-export-backend-options (org-export-get-backend 'test)))))
  1394. ;; Menu.
  1395. (should
  1396. (equal '(?k "Test Export" test)
  1397. (let (org-export-registered-backends)
  1398. (org-export-define-backend 'test
  1399. '((headline . my-headline-test))
  1400. :menu-entry '(?k "Test Export" test))
  1401. (org-export-backend-menu (org-export-get-backend 'test))))))
  1402. (ert-deftest test-org-export/define-derived-backend ()
  1403. "Test `org-export-define-derived-backend' specifications."
  1404. ;; Error when parent back-end is not defined.
  1405. (should-error
  1406. (let (org-export-registered-backends)
  1407. (org-export-define-derived-backend 'test 'parent)))
  1408. ;; Append translation table to parent's.
  1409. (should
  1410. (equal '((:headline . test) (:headline . parent))
  1411. (let (org-export-registered-backends)
  1412. (org-export-define-backend 'parent '((:headline . parent)))
  1413. (org-export-define-derived-backend 'test 'parent
  1414. :translate-alist '((:headline . test)))
  1415. (org-export-get-all-transcoders 'test))))
  1416. ;; Options defined in the new back have priority over those defined
  1417. ;; in parent.
  1418. (should
  1419. (eq 'test
  1420. (let (org-export-registered-backends)
  1421. (org-export-define-backend 'parent
  1422. '((:headline . parent))
  1423. :options-alist '((:a nil nil 'parent)))
  1424. (org-export-define-derived-backend 'test 'parent
  1425. :options-alist '((:a nil nil 'test)))
  1426. (plist-get (org-export--get-global-options
  1427. (org-export-get-backend 'test))
  1428. :a)))))
  1429. (ert-deftest test-org-export/derived-backend-p ()
  1430. "Test `org-export-derived-backend-p' specifications."
  1431. ;; Non-nil with direct match.
  1432. (should
  1433. (let (org-export-registered-backends)
  1434. (org-export-define-backend 'test '((headline . test)))
  1435. (org-export-derived-backend-p 'test 'test)))
  1436. (should
  1437. (let (org-export-registered-backends)
  1438. (org-export-define-backend 'test '((headline . test)))
  1439. (org-export-define-derived-backend 'test2 'test)
  1440. (org-export-derived-backend-p 'test2 'test2)))
  1441. ;; Non-nil with a direct parent.
  1442. (should
  1443. (let (org-export-registered-backends)
  1444. (org-export-define-backend 'test '((headline . test)))
  1445. (org-export-define-derived-backend 'test2 'test)
  1446. (org-export-derived-backend-p 'test2 'test)))
  1447. ;; Non-nil with an indirect parent.
  1448. (should
  1449. (let (org-export-registered-backends)
  1450. (org-export-define-backend 'test '((headline . test)))
  1451. (org-export-define-derived-backend 'test2 'test)
  1452. (org-export-define-derived-backend 'test3 'test2)
  1453. (org-export-derived-backend-p 'test3 'test)))
  1454. ;; Nil otherwise.
  1455. (should-not
  1456. (let (org-export-registered-backends)
  1457. (org-export-define-backend 'test '((headline . test)))
  1458. (org-export-define-backend 'test2 '((headline . test2)))
  1459. (org-export-derived-backend-p 'test2 'test)))
  1460. (should-not
  1461. (let (org-export-registered-backends)
  1462. (org-export-define-backend 'test '((headline . test)))
  1463. (org-export-define-backend 'test2 '((headline . test2)))
  1464. (org-export-define-derived-backend 'test3 'test2)
  1465. (org-export-derived-backend-p 'test3 'test))))
  1466. (ert-deftest test-org-export/get-all-transcoders ()
  1467. "Test `org-export-get-all-transcoders' specifications."
  1468. ;; Return nil when back-end cannot be found.
  1469. (should-not (org-export-get-all-transcoders nil))
  1470. ;; Same as `org-export-transcoders' if no parent.
  1471. (should
  1472. (equal '((headline . ignore))
  1473. (org-export-get-all-transcoders
  1474. (org-export-create-backend
  1475. :transcoders '((headline . ignore))))))
  1476. ;; But inherit from all ancestors whenever possible.
  1477. (should
  1478. (equal '((section . ignore) (headline . ignore))
  1479. (let (org-export-registered-backends)
  1480. (org-export-define-backend 'b1 '((headline . ignore)))
  1481. (org-export-get-all-transcoders
  1482. (org-export-create-backend
  1483. :parent 'b1 :transcoders '((section . ignore)))))))
  1484. (should
  1485. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1486. (let (org-export-registered-backends)
  1487. (org-export-define-backend 'b1 '((headline . ignore)))
  1488. (org-export-define-derived-backend 'b2 'b1
  1489. :translate-alist '((section . ignore)))
  1490. (org-export-get-all-transcoders
  1491. (org-export-create-backend
  1492. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1493. ;; Back-end transcoders overrule inherited ones.
  1494. (should
  1495. (eq 'b
  1496. (let (org-export-registered-backends)
  1497. (org-export-define-backend 'b1 '((headline . a)))
  1498. (cdr (assq 'headline
  1499. (org-export-get-all-transcoders
  1500. (org-export-create-backend
  1501. :parent 'b1 :transcoders '((headline . b))))))))))
  1502. (ert-deftest test-org-export/get-all-options ()
  1503. "Test `org-export-get-all-options' specifications."
  1504. ;; Return nil when back-end cannot be found.
  1505. (should-not (org-export-get-all-options nil))
  1506. ;; Same as `org-export-options' if no parent.
  1507. (should
  1508. (equal '((headline . ignore))
  1509. (org-export-get-all-options
  1510. (org-export-create-backend
  1511. :options '((headline . ignore))))))
  1512. ;; But inherit from all ancestors whenever possible.
  1513. (should
  1514. (equal '((:key2 value2) (:key1 value1))
  1515. (let (org-export-registered-backends)
  1516. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1517. (org-export-get-all-options
  1518. (org-export-create-backend
  1519. :parent 'b1 :options '((:key2 value2)))))))
  1520. (should
  1521. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1522. (let (org-export-registered-backends)
  1523. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1524. (org-export-define-derived-backend 'b2 'b1
  1525. :options-alist '((:key2 value2)))
  1526. (org-export-get-all-options
  1527. (org-export-create-backend
  1528. :parent 'b2 :options '((:key3 value3)))))))
  1529. ;; Back-end options overrule inherited ones.
  1530. (should
  1531. (eq 'b
  1532. (let (org-export-registered-backends)
  1533. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1534. (cdr (assq :key1
  1535. (org-export-get-all-options
  1536. (org-export-create-backend
  1537. :parent 'b1 :options '((:key1 . b))))))))))
  1538. (ert-deftest test-org-export/get-all-filters ()
  1539. "Test `org-export-get-all-filters' specifications."
  1540. ;; Return nil when back-end cannot be found.
  1541. (should-not (org-export-get-all-filters nil))
  1542. ;; Same as `org-export-filters' if no parent.
  1543. (should
  1544. (equal '((:filter-headline . ignore))
  1545. (org-export-get-all-filters
  1546. (org-export-create-backend
  1547. :filters '((:filter-headline . ignore))))))
  1548. ;; But inherit from all ancestors whenever possible.
  1549. (should
  1550. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1551. (let (org-export-registered-backends)
  1552. (org-export-define-backend 'b1
  1553. nil :filters-alist '((:filter-headline . ignore)))
  1554. (org-export-get-all-filters
  1555. (org-export-create-backend
  1556. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1557. (should
  1558. (equal '((:filter-paragraph . ignore)
  1559. (:filter-section . ignore)
  1560. (:filter-headline . ignore))
  1561. (let (org-export-registered-backends)
  1562. (org-export-define-backend 'b1
  1563. nil :filters-alist '((:filter-headline . ignore)))
  1564. (org-export-define-derived-backend 'b2 'b1
  1565. :filters-alist '((:filter-section . ignore)))
  1566. (org-export-get-all-filters
  1567. (org-export-create-backend
  1568. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1569. ;; Back-end filters overrule inherited ones.
  1570. (should
  1571. (eq 'b
  1572. (let (org-export-registered-backends)
  1573. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1574. (cdr (assq :filter-headline
  1575. (org-export-get-all-filters
  1576. (org-export-create-backend
  1577. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1578. (ert-deftest test-org-export/with-backend ()
  1579. "Test `org-export-with-backend' definition."
  1580. ;; Error when calling an undefined back-end
  1581. (should-error (org-export-with-backend nil "Test"))
  1582. ;; Error when called back-end doesn't have an appropriate
  1583. ;; transcoder.
  1584. (should-error
  1585. (org-export-with-backend
  1586. (org-export-create-backend :transcoders '((headline . ignore)))
  1587. "Test"))
  1588. ;; Otherwise, export using correct transcoder
  1589. (should
  1590. (equal "Success"
  1591. (let (org-export-registered-backends)
  1592. (org-export-define-backend 'test
  1593. '((plain-text . (lambda (text contents info) "Failure"))))
  1594. (org-export-define-backend 'test2
  1595. '((plain-text . (lambda (text contents info) "Success"))))
  1596. (org-export-with-backend 'test2 "Test"))))
  1597. ;; Provide correct back-end if transcoder needs to use recursive
  1598. ;; calls anyway.
  1599. (should
  1600. (equal "Success\n"
  1601. (let ((test-back-end
  1602. (org-export-create-backend
  1603. :transcoders
  1604. (list (cons 'headline
  1605. (lambda (headline contents info)
  1606. (org-export-data
  1607. (org-element-property :title headline)
  1608. info)))
  1609. (cons 'plain-text (lambda (text info) "Success"))))))
  1610. (org-export-string-as
  1611. "* Test"
  1612. (org-export-create-backend
  1613. :transcoders
  1614. (list (cons 'headline
  1615. (lambda (headline contents info)
  1616. (org-export-with-backend
  1617. test-back-end headline contents info))))))))))
  1618. (ert-deftest test-org-export/data-with-backend ()
  1619. "Test `org-export-data-with-backend' specifications."
  1620. ;; Error when calling an undefined back-end.
  1621. (should-error (org-export-data-with-backend nil "nil" nil))
  1622. ;; Otherwise, export data recursively, using correct back-end.
  1623. (should
  1624. (equal
  1625. "Success!"
  1626. (org-export-data-with-backend
  1627. '(bold nil "Test")
  1628. (org-export-create-backend
  1629. :transcoders
  1630. '((plain-text . (lambda (text info) "Success"))
  1631. (bold . (lambda (bold contents info) (concat contents "!")))))
  1632. '(:with-emphasize t)))))
  1633. ;;; Comments
  1634. (ert-deftest test-org-export/comments ()
  1635. "Test comments handling during export.
  1636. In particular, structure of the document mustn't be altered after
  1637. comments removal."
  1638. (should
  1639. (equal (org-test-with-temp-text "
  1640. Para1
  1641. # Comment
  1642. # Comment
  1643. Para2"
  1644. (org-export-as (org-test-default-backend)))
  1645. "Para1\n\nPara2\n"))
  1646. (should
  1647. (equal (org-test-with-temp-text "
  1648. Para1
  1649. # Comment
  1650. Para2"
  1651. (org-export-as (org-test-default-backend)))
  1652. "Para1\n\nPara2\n"))
  1653. (should
  1654. (equal (org-test-with-temp-text "
  1655. \[fn:1] Para1
  1656. # Inside definition
  1657. # Outside definition
  1658. Para2"
  1659. (org-export-as (org-test-default-backend)))
  1660. "[fn:1] Para1\n\n\nPara2\n"))
  1661. (should
  1662. (equal (org-test-with-temp-text "
  1663. \[fn:1] Para1
  1664. # Inside definition
  1665. # Inside definition
  1666. Para2"
  1667. (org-export-as (org-test-default-backend)))
  1668. "[fn:1] Para1\n\nPara2\n"))
  1669. (should
  1670. (equal (org-test-with-temp-text "
  1671. \[fn:1] Para1
  1672. # Inside definition
  1673. Para2"
  1674. (org-export-as (org-test-default-backend)))
  1675. "[fn:1] Para1\n\nPara2\n"))
  1676. (should
  1677. (equal (org-test-with-temp-text "
  1678. \[fn:1] Para1
  1679. # Inside definition
  1680. Para2"
  1681. (org-export-as (org-test-default-backend)))
  1682. "[fn:1] Para1\n\nPara2\n")))
  1683. ;;; Export blocks
  1684. (ert-deftest test-org-export/export-block ()
  1685. "Test export blocks transcoding."
  1686. (should
  1687. (equal "Success!\n"
  1688. (org-test-with-temp-text
  1689. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1690. (org-export-as
  1691. (org-export-create-backend
  1692. :transcoders '((export-block . (lambda (b _c _i)
  1693. (org-element-property :value b)))
  1694. (section . (lambda (_s c _i) c))))))))
  1695. (should
  1696. (equal "Success!\n"
  1697. (org-test-with-temp-text
  1698. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1699. (org-export-as
  1700. (org-export-create-backend
  1701. :transcoders
  1702. (list
  1703. (cons 'export-block
  1704. (lambda (b _c _i)
  1705. (and (equal (org-element-property :type b) "BACKEND")
  1706. (org-element-property :value b))))
  1707. (cons 'section (lambda (_s c _i) c)))))))))
  1708. ;;; Export Snippets
  1709. (ert-deftest test-org-export/export-snippet ()
  1710. "Test export snippets transcoding."
  1711. ;; Standard test.
  1712. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1713. (let ((backend (org-test-default-backend)))
  1714. (setf (org-export-backend-name backend) 'test)
  1715. (setf (org-export-backend-transcoders backend)
  1716. (cons (cons 'export-snippet
  1717. (lambda (snippet contents info)
  1718. (when (eq (org-export-snippet-backend snippet) 'test)
  1719. (org-element-property :value snippet))))
  1720. (org-export-backend-transcoders backend)))
  1721. (let ((org-export-snippet-translation-alist nil))
  1722. (should (equal (org-export-as backend) "A\n")))
  1723. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1724. (should (equal (org-export-as backend) "AB\n")))))
  1725. ;; Ignored export snippets do not remove any blank.
  1726. (should
  1727. (equal "begin end\n"
  1728. (org-test-with-parsed-data "begin @@test:A@@ end"
  1729. (org-export-data-with-backend
  1730. tree
  1731. (org-export-create-backend
  1732. :transcoders
  1733. '((paragraph . (lambda (paragraph contents info) contents))
  1734. (section . (lambda (section contents info) contents))))
  1735. info)))))
  1736. ;;; Footnotes
  1737. (ert-deftest test-org-export/footnote-first-reference-p ()
  1738. "Test `org-export-footnote-first-reference-p' specifications."
  1739. (should
  1740. (equal
  1741. '(t nil)
  1742. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1743. (let (result)
  1744. (org-export-as
  1745. (org-export-create-backend
  1746. :transcoders
  1747. `(,(cons 'footnote-reference
  1748. (lambda (f c i)
  1749. (push (org-export-footnote-first-reference-p f i)
  1750. result)
  1751. ""))
  1752. (section . (lambda (s c i) c))
  1753. (paragraph . (lambda (p c i) c))))
  1754. nil nil nil '(:with-footnotes t))
  1755. (nreverse result)))))
  1756. ;; Limit check to DATA, when non-nil.
  1757. (should
  1758. (equal
  1759. '(nil t)
  1760. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1761. (let (result)
  1762. (org-element-map tree 'footnote-reference
  1763. (lambda (ref)
  1764. (push
  1765. (org-export-footnote-first-reference-p
  1766. ref info (org-element-map tree 'headline #'identity info t))
  1767. result))
  1768. info)
  1769. (nreverse result)))))
  1770. (should
  1771. (equal
  1772. '(t nil)
  1773. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1774. (let (result)
  1775. (org-element-map tree 'footnote-reference
  1776. (lambda (ref)
  1777. (push (org-export-footnote-first-reference-p ref info) result))
  1778. info)
  1779. (nreverse result)))))
  1780. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1781. ;; in the main body of the document. Otherwise, enter footnote
  1782. ;; definitions when they are encountered.
  1783. (should
  1784. (equal
  1785. '(t nil)
  1786. (org-test-with-temp-text
  1787. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1788. (let (result)
  1789. (org-export-as
  1790. (org-export-create-backend
  1791. :transcoders
  1792. `(,(cons 'footnote-reference
  1793. (lambda (f c i)
  1794. (when (org-element-lineage f '(drawer))
  1795. (push (org-export-footnote-first-reference-p f i nil)
  1796. result))
  1797. ""))
  1798. (drawer . (lambda (d c i) c))
  1799. (footnote-definition . (lambda (d c i) c))
  1800. (section . (lambda (s c i) c))
  1801. (paragraph . (lambda (p c i) c))))
  1802. nil nil nil '(:with-footnotes t))
  1803. (nreverse result)))))
  1804. (should
  1805. (equal
  1806. '(t t)
  1807. (org-test-with-temp-text
  1808. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1809. (let (result)
  1810. (org-export-as
  1811. (org-export-create-backend
  1812. :transcoders
  1813. `(,(cons 'footnote-reference
  1814. (lambda (f c i)
  1815. (when (org-element-lineage f '(drawer))
  1816. (push (org-export-footnote-first-reference-p f i nil t)
  1817. result))
  1818. ""))
  1819. (drawer . (lambda (d c i) c))
  1820. (footnote-definition . (lambda (d c i) c))
  1821. (section . (lambda (s c i) c))
  1822. (paragraph . (lambda (p c i) c))))
  1823. nil nil nil '(:with-footnotes t))
  1824. (nreverse result))))))
  1825. (ert-deftest test-org-export/get-footnote-number ()
  1826. "Test `org-export-get-footnote-number' specifications."
  1827. (should
  1828. (equal '(1 2 1)
  1829. (org-test-with-parsed-data
  1830. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1831. (org-element-map tree 'footnote-reference
  1832. (lambda (ref) (org-export-get-footnote-number ref info))
  1833. info))))
  1834. ;; Anonymous footnotes all get a new number.
  1835. (should
  1836. (equal '(1 2)
  1837. (org-test-with-parsed-data
  1838. "Text[fn::anon1][fn::anon2]"
  1839. (org-element-map tree 'footnote-reference
  1840. (lambda (ref) (org-export-get-footnote-number ref info))
  1841. info))))
  1842. ;; Test nested footnotes order.
  1843. (should
  1844. (equal
  1845. '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
  1846. (org-test-with-parsed-data
  1847. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1848. (org-element-map tree 'footnote-reference
  1849. (lambda (ref)
  1850. (cons (org-export-get-footnote-number ref info)
  1851. (org-element-property :label ref)))
  1852. info))))
  1853. ;; Limit number to provided DATA, when non-nil.
  1854. (should
  1855. (equal
  1856. '(1)
  1857. (org-test-with-parsed-data
  1858. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1859. (org-element-map tree 'footnote-reference
  1860. (lambda (ref)
  1861. (org-export-get-footnote-number
  1862. ref info (org-element-map tree 'headline #'identity info t)))
  1863. info))))
  1864. (should
  1865. (equal
  1866. '(1 2)
  1867. (org-test-with-parsed-data
  1868. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1869. (org-element-map tree 'footnote-reference
  1870. (lambda (ref) (org-export-get-footnote-number ref info))
  1871. info))))
  1872. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1873. ;; then footnote definitions.
  1874. (should
  1875. (equal
  1876. '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
  1877. (org-test-with-parsed-data
  1878. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1879. (org-element-map tree 'footnote-reference
  1880. (lambda (ref)
  1881. (cons (org-element-property :label ref)
  1882. (org-export-get-footnote-number ref info nil t)))
  1883. info))))
  1884. (should
  1885. (equal
  1886. '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
  1887. (org-test-with-parsed-data
  1888. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1889. (org-element-map tree 'footnote-reference
  1890. (lambda (ref)
  1891. (cons (org-element-property :label ref)
  1892. (org-export-get-footnote-number ref info nil)))
  1893. info)))))
  1894. (ert-deftest test-org-export/get-footnote-definition ()
  1895. "Test `org-export-get-footnote-definition' specifications."
  1896. ;; Standard test.
  1897. (should
  1898. (equal "A\n"
  1899. (org-element-interpret-data
  1900. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  1901. (org-export-get-footnote-definition
  1902. (org-element-map tree 'footnote-reference #'identity nil t)
  1903. info)))))
  1904. ;; Raise an error if no definition is found.
  1905. (should-error
  1906. (org-test-with-parsed-data "Text[fn:1]"
  1907. (org-export-get-footnote-definition
  1908. (org-element-map tree 'footnote-reference #'identity nil t)
  1909. info)))
  1910. ;; Find inline definitions.
  1911. (should
  1912. (equal "A"
  1913. (org-element-interpret-data
  1914. (org-test-with-parsed-data "Text[fn:1:A]"
  1915. (org-export-get-footnote-definition
  1916. (org-element-map tree 'footnote-reference #'identity nil t)
  1917. info)))))
  1918. ;; Find anonymous definitions.
  1919. (should
  1920. (equal "A"
  1921. (org-element-interpret-data
  1922. (org-test-with-parsed-data "Text[fn::A]"
  1923. (org-export-get-footnote-definition
  1924. (org-element-map tree 'footnote-reference #'identity nil t)
  1925. info)))))
  1926. ;; Find empty definitions.
  1927. (should
  1928. (equal ""
  1929. (org-element-interpret-data
  1930. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  1931. (org-export-get-footnote-definition
  1932. (org-element-map tree 'footnote-reference #'identity nil t)
  1933. info)))))
  1934. (should
  1935. (equal ""
  1936. (org-element-interpret-data
  1937. (org-test-with-parsed-data "Text[fn:1:]"
  1938. (org-export-get-footnote-definition
  1939. (org-element-map tree 'footnote-reference #'identity nil t)
  1940. info)))))
  1941. (should
  1942. (equal ""
  1943. (org-element-interpret-data
  1944. (org-test-with-parsed-data "Text[fn::]"
  1945. (org-export-get-footnote-definition
  1946. (org-element-map tree 'footnote-reference #'identity nil t)
  1947. info))))))
  1948. (ert-deftest test-org-export/collect-footnote-definitions ()
  1949. "Test `org-export-collect-footnote-definitions' specifications."
  1950. (should
  1951. (= 4
  1952. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1953. \[fn:2] B [fn:3] [fn::D].
  1954. \[fn:3] C."
  1955. (length (org-export-collect-footnote-definitions info)))))
  1956. ;; Limit number to provided DATA, when non-nil.
  1957. (should
  1958. (equal
  1959. '((1 "2"))
  1960. (org-test-with-parsed-data
  1961. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1962. (mapcar #'butlast
  1963. (org-export-collect-footnote-definitions
  1964. info (org-element-map tree 'headline #'identity info t))))))
  1965. (should
  1966. (equal
  1967. '((1 "1") (2 "2"))
  1968. (org-test-with-parsed-data
  1969. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1970. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  1971. ;; With optional argument BODY-FIRST, first check body, then
  1972. ;; footnote definitions.
  1973. (should
  1974. (equal '("1" "3" "2" nil)
  1975. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1976. \[fn:2] B [fn:3] [fn::D].
  1977. \[fn:3] C."
  1978. (mapcar (lambda (e) (nth 1 e))
  1979. (org-export-collect-footnote-definitions info nil t)))))
  1980. (should-not
  1981. (equal '("1" "3" "2" nil)
  1982. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1983. \[fn:2] B [fn:3] [fn::D].
  1984. \[fn:3] C."
  1985. (mapcar (lambda (e) (nth 1 e))
  1986. (org-export-collect-footnote-definitions info))))))
  1987. (ert-deftest test-org-export/footnotes ()
  1988. "Miscellaneous tests on footnotes."
  1989. (let ((org-footnote-section nil)
  1990. (org-export-with-footnotes t))
  1991. ;; Read every type of footnote.
  1992. (should
  1993. (equal
  1994. '((1 . "A\n") (2 . "C") (3 . "D"))
  1995. (org-test-with-parsed-data
  1996. "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
  1997. (org-element-map tree 'footnote-reference
  1998. (lambda (ref)
  1999. (let ((def (org-export-get-footnote-definition ref info)))
  2000. (cons (org-export-get-footnote-number ref info)
  2001. (if (eq (org-element-property :type ref) 'inline) (car def)
  2002. (car (org-element-contents
  2003. (car (org-element-contents def))))))))
  2004. info))))
  2005. ;; Export nested footnote in invisible definitions.
  2006. (should
  2007. (= 2
  2008. (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
  2009. (narrow-to-region (point) (line-end-position))
  2010. (catch 'exit
  2011. (org-export-as
  2012. (org-export-create-backend
  2013. :transcoders
  2014. '((section
  2015. .
  2016. (lambda (s c i)
  2017. (throw 'exit (length
  2018. (org-export-collect-footnote-definitions
  2019. i))))))))))))
  2020. ;; Export footnotes defined outside parsing scope.
  2021. (should
  2022. (string-match
  2023. "Out of scope"
  2024. (org-test-with-temp-text "[fn:1] Out of scope
  2025. * Title
  2026. <point>Paragraph[fn:1]"
  2027. (org-export-as (org-test-default-backend) 'subtree))))
  2028. (should
  2029. (string-match
  2030. "Out of scope"
  2031. (org-test-with-temp-text "[fn:1] Out of scope
  2032. * Title
  2033. <point>Paragraph[fn:1]"
  2034. (narrow-to-region (point) (point-max))
  2035. (org-export-as (org-test-default-backend)))))
  2036. ;; Export nested footnotes defined outside parsing scope.
  2037. (should
  2038. (string-match
  2039. "Very out of scope"
  2040. (org-test-with-temp-text "
  2041. \[fn:1] Out of scope[fn:2]
  2042. \[fn:2] Very out of scope
  2043. * Title
  2044. <point>Paragraph[fn:1]"
  2045. (org-export-as (org-test-default-backend) 'subtree))))
  2046. (should
  2047. (string-match
  2048. "Very out of scope"
  2049. (org-test-with-temp-text "
  2050. \[fn:1] Out of scope[fn:2]
  2051. \[fn:2] Very out of scope
  2052. * Title
  2053. <point>Paragraph[fn:1]"
  2054. (narrow-to-region (point) (point-max))
  2055. (org-export-as (org-test-default-backend)))))
  2056. (should
  2057. (string-match
  2058. "D2"
  2059. (org-test-with-temp-text "
  2060. \[fn:1] Out of scope[fn:2:D2]
  2061. * Title
  2062. <point>Paragraph[fn:1]"
  2063. (narrow-to-region (point) (point-max))
  2064. (org-export-as (org-test-default-backend)))))
  2065. ;; Export footnotes in pruned parts of tree.
  2066. (should
  2067. (string-match
  2068. "Definition"
  2069. (let ((org-export-exclude-tags '("noexport")))
  2070. (org-test-with-temp-text
  2071. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  2072. (org-export-as (org-test-default-backend))))))
  2073. (should
  2074. (string-match
  2075. "Definition"
  2076. (let ((org-export-select-tags '("export")))
  2077. (org-test-with-temp-text
  2078. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2079. (org-export-as (org-test-default-backend))))))
  2080. ;; Export nested footnotes in pruned parts of tree.
  2081. (should
  2082. (string-match
  2083. "D2"
  2084. (let ((org-export-exclude-tags '("noexport")))
  2085. (org-test-with-temp-text
  2086. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2087. (org-export-as (org-test-default-backend))))))
  2088. (should
  2089. (string-match
  2090. "D2"
  2091. (let ((org-export-select-tags '("export")))
  2092. (org-test-with-temp-text
  2093. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2094. (org-export-as (org-test-default-backend))))))
  2095. ;; Handle uninterpreted data in pruned footnote definitions.
  2096. (should-not
  2097. (string-match
  2098. "|"
  2099. (let ((org-export-with-tables nil))
  2100. (org-test-with-temp-text
  2101. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2102. (org-export-as (org-test-default-backend))))))
  2103. ;; Footnotes without a definition should throw an error.
  2104. (should-error
  2105. (org-test-with-parsed-data "Text[fn:1]"
  2106. (org-export-get-footnote-definition
  2107. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2108. ;; Footnote section should be ignored in TOC and in headlines
  2109. ;; numbering.
  2110. (should
  2111. (= 1 (let ((org-footnote-section "Footnotes"))
  2112. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2113. (org-export-collect-headlines info))))))
  2114. (should
  2115. (equal '(2)
  2116. (let ((org-footnote-section "Footnotes"))
  2117. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2118. (org-element-map tree 'headline
  2119. (lambda (hl)
  2120. (when (equal (org-element-property :raw-value hl) "H2")
  2121. (org-export-get-headline-number hl info)))
  2122. info t)))))))
  2123. ;;; Headlines and Inlinetasks
  2124. (ert-deftest test-org-export/get-relative-level ()
  2125. "Test `org-export-get-relative-level' specifications."
  2126. ;; Standard test.
  2127. (should
  2128. (equal '(1 2)
  2129. (let ((org-odd-levels-only nil))
  2130. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2131. (org-element-map tree 'headline
  2132. (lambda (h) (org-export-get-relative-level h info))
  2133. info)))))
  2134. ;; Missing levels
  2135. (should
  2136. (equal '(1 3)
  2137. (let ((org-odd-levels-only nil))
  2138. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2139. (org-element-map tree 'headline
  2140. (lambda (h) (org-export-get-relative-level h info))
  2141. info))))))
  2142. (ert-deftest test-org-export/low-level-p ()
  2143. "Test `org-export-low-level-p' specifications."
  2144. (should
  2145. (equal
  2146. '(no yes)
  2147. (let ((org-odd-levels-only nil))
  2148. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2149. (org-element-map tree 'headline
  2150. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2151. (plist-put info :headline-levels 1)))))))
  2152. (ert-deftest test-org-export/get-headline-number ()
  2153. "Test `org-export-get-headline-number' specifications."
  2154. ;; Standard test.
  2155. (should
  2156. (equal
  2157. '((1) (1 1))
  2158. (let ((org-odd-levels-only nil))
  2159. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2160. (org-element-map tree 'headline
  2161. (lambda (h) (org-export-get-headline-number h info))
  2162. info)))))
  2163. ;; Missing levels are replaced with 0.
  2164. (should
  2165. (equal
  2166. '((1) (1 0 1))
  2167. (let ((org-odd-levels-only nil))
  2168. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2169. (org-element-map tree 'headline
  2170. (lambda (h) (org-export-get-headline-number h info))
  2171. info))))))
  2172. (ert-deftest test-org-export/numbered-headline-p ()
  2173. "Test `org-export-numbered-headline-p' specifications."
  2174. ;; If `:section-numbers' is nil, never number headlines.
  2175. (should-not
  2176. (org-test-with-parsed-data "* Headline"
  2177. (org-element-map tree 'headline
  2178. (lambda (h) (org-export-numbered-headline-p h info))
  2179. (plist-put info :section-numbers nil))))
  2180. ;; If `:section-numbers' is a number, only number headlines with
  2181. ;; a level greater that it.
  2182. (should
  2183. (equal
  2184. '(yes no)
  2185. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2186. (org-element-map tree 'headline
  2187. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2188. (plist-put info :section-numbers 1)))))
  2189. ;; Otherwise, headlines are always numbered.
  2190. (should
  2191. (org-test-with-parsed-data "* Headline"
  2192. (org-element-map tree 'headline
  2193. (lambda (h) (org-export-numbered-headline-p h info))
  2194. (plist-put info :section-numbers t))))
  2195. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2196. (should-not
  2197. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2198. (org-export-numbered-headline-p
  2199. (org-element-map tree 'headline 'identity info t)
  2200. info)))
  2201. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2202. (should-not
  2203. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2204. (org-export-numbered-headline-p
  2205. (org-element-map tree 'headline #'identity info t)
  2206. info)))
  2207. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  2208. ;; ancestors disables numbering.
  2209. (should
  2210. (org-test-with-parsed-data
  2211. "* H
  2212. :PROPERTIES:
  2213. :UNNUMBERED: t
  2214. :END:
  2215. ** H2
  2216. :PROPERTIES:
  2217. :UNNUMBERED: nil
  2218. :END:
  2219. *** H3"
  2220. (cl-every
  2221. (lambda (h) (not (org-export-numbered-headline-p h info)))
  2222. (org-element-map tree 'headline #'identity info)))))
  2223. (ert-deftest test-org-export/number-to-roman ()
  2224. "Test `org-export-number-to-roman' specifications."
  2225. ;; If number is negative, return it as a string.
  2226. (should (equal (org-export-number-to-roman -1) "-1"))
  2227. ;; Otherwise, return it as a roman number.
  2228. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2229. (ert-deftest test-org-export/get-optional-title ()
  2230. "Test `org-export-get-alt-title' specifications."
  2231. ;; If ALT_TITLE property is defined, use it.
  2232. (should
  2233. (equal '("opt")
  2234. (org-test-with-parsed-data
  2235. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2236. (org-export-get-alt-title
  2237. (org-element-map tree 'headline 'identity info t)
  2238. info))))
  2239. ;; Otherwise, fall-back to regular title.
  2240. (should
  2241. (equal '("Headline")
  2242. (org-test-with-parsed-data "* Headline"
  2243. (org-export-get-alt-title
  2244. (org-element-map tree 'headline 'identity info t)
  2245. info)))))
  2246. (ert-deftest test-org-export/get-tags ()
  2247. "Test `org-export-get-tags' specifications."
  2248. ;; Standard test: tags which are not a select tag, an exclude tag,
  2249. ;; or specified as optional argument shouldn't be ignored.
  2250. (should
  2251. (org-test-with-parsed-data "* Headline :tag:"
  2252. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2253. info)))
  2254. ;; Tags provided in the optional argument are ignored.
  2255. (should-not
  2256. (org-test-with-parsed-data "* Headline :ignore:"
  2257. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2258. info '("ignore"))))
  2259. ;; Allow tag inheritance.
  2260. (should
  2261. (equal
  2262. '(("tag") ("tag"))
  2263. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2264. (org-element-map tree 'headline
  2265. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2266. ;; Tag inheritance checks FILETAGS keywords.
  2267. (should
  2268. (equal
  2269. '(("a" "b" "tag"))
  2270. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2271. (org-element-map tree 'headline
  2272. (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
  2273. (ert-deftest test-org-export/get-node-property ()
  2274. "Test`org-export-get-node-property' specifications."
  2275. ;; Standard test.
  2276. (should
  2277. (equal "value"
  2278. (org-test-with-parsed-data "* Headline
  2279. :PROPERTIES:
  2280. :prop: value
  2281. :END:"
  2282. (org-export-get-node-property
  2283. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2284. ;; Test inheritance.
  2285. (should
  2286. (equal "value"
  2287. (org-test-with-parsed-data "* Parent
  2288. :PROPERTIES:
  2289. :prop: value
  2290. :END:
  2291. ** Headline
  2292. Paragraph"
  2293. (org-export-get-node-property
  2294. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2295. ;; Cannot return a value before the first headline.
  2296. (should-not
  2297. (org-test-with-parsed-data "Paragraph
  2298. * Headline
  2299. :PROPERTIES:
  2300. :prop: value
  2301. :END:"
  2302. (org-export-get-node-property
  2303. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2304. (ert-deftest test-org-export/get-category ()
  2305. "Test `org-export-get-category' specifications."
  2306. ;; Standard test.
  2307. (should
  2308. (equal "value"
  2309. (org-test-with-parsed-data "* Headline
  2310. :PROPERTIES:
  2311. :CATEGORY: value
  2312. :END:"
  2313. (org-export-get-category
  2314. (org-element-map tree 'headline 'identity nil t) info))))
  2315. ;; Test inheritance from a parent headline.
  2316. (should
  2317. (equal '("value" "value")
  2318. (org-test-with-parsed-data "* Headline1
  2319. :PROPERTIES:
  2320. :CATEGORY: value
  2321. :END:
  2322. ** Headline2"
  2323. (org-element-map tree 'headline
  2324. (lambda (hl) (org-export-get-category hl info)) info))))
  2325. ;; Test inheritance from #+CATEGORY keyword
  2326. (should
  2327. (equal "value"
  2328. (org-test-with-parsed-data "#+CATEGORY: value
  2329. * Headline"
  2330. (org-export-get-category
  2331. (org-element-map tree 'headline 'identity nil t) info))))
  2332. ;; Test inheritance from file name.
  2333. (should
  2334. (equal "test"
  2335. (org-test-with-parsed-data "* Headline"
  2336. (let ((info (plist-put info :input-file "~/test.org")))
  2337. (org-export-get-category
  2338. (org-element-map tree 'headline 'identity nil t) info)))))
  2339. ;; Fall-back value.
  2340. (should
  2341. (equal "???"
  2342. (org-test-with-parsed-data "* Headline"
  2343. (org-export-get-category
  2344. (org-element-map tree 'headline 'identity nil t) info)))))
  2345. (ert-deftest test-org-export/first-sibling-p ()
  2346. "Test `org-export-first-sibling-p' specifications."
  2347. ;; Standard test.
  2348. (should
  2349. (equal
  2350. '(yes yes no)
  2351. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2352. (org-element-map tree 'headline
  2353. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2354. info))))
  2355. (should
  2356. (equal '(yes no)
  2357. (org-test-with-parsed-data "- item\n\n para"
  2358. (org-element-map tree 'paragraph
  2359. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2360. info))))
  2361. ;; Ignore sections for headlines.
  2362. (should
  2363. (equal '(yes yes)
  2364. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2365. (org-element-map tree 'headline
  2366. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2367. info))))
  2368. ;; Ignore headlines not exported.
  2369. (should
  2370. (equal
  2371. '(yes)
  2372. (let ((org-export-exclude-tags '("ignore")))
  2373. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2374. (org-element-map tree 'headline
  2375. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2376. info))))))
  2377. (ert-deftest test-org-export/last-sibling-p ()
  2378. "Test `org-export-last-sibling-p' specifications."
  2379. ;; Standard test.
  2380. (should
  2381. (equal
  2382. '(yes no yes)
  2383. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2384. (org-element-map tree 'headline
  2385. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2386. info))))
  2387. (should
  2388. (equal '(no yes)
  2389. (org-test-with-parsed-data "- item\n\n para"
  2390. (org-element-map tree 'paragraph
  2391. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2392. info))))
  2393. ;; Ignore headlines not exported.
  2394. (should
  2395. (equal
  2396. '(yes)
  2397. (let ((org-export-exclude-tags '("ignore")))
  2398. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2399. (org-element-map tree 'headline
  2400. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2401. info))))))
  2402. (ert-deftest test-org-export/handle-inlinetasks ()
  2403. "Test inlinetask export."
  2404. ;; Inlinetask with an exclude tag.
  2405. (when (featurep 'org-inlinetask)
  2406. (should
  2407. (equal
  2408. ""
  2409. (let ((org-inlinetask-min-level 3)
  2410. org-export-filter-body-functions
  2411. org-export-filter-final-output-functions)
  2412. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2413. (org-export-as (org-test-default-backend)
  2414. nil nil nil '(:exclude-tags ("noexp")))))))
  2415. ;; Inlinetask with an include tag.
  2416. (should
  2417. (equal
  2418. "* H2\n*** Inline :exp:\n"
  2419. (let ((org-inlinetask-min-level 3)
  2420. (org-tags-column 0))
  2421. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2422. (org-export-as (org-test-default-backend)
  2423. nil nil nil '(:select-tags ("exp")))))))
  2424. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2425. (should
  2426. (equal ""
  2427. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2428. (org-inlinetask-min-level 3)
  2429. org-export-filter-body-functions
  2430. org-export-filter-final-output-functions)
  2431. (org-test-with-temp-text "*** TODO Inline"
  2432. (org-export-as (org-test-default-backend)
  2433. nil nil nil '(:with-tasks nil))))))))
  2434. ;;; Keywords
  2435. (ert-deftest test-org-export/get-date ()
  2436. "Test `org-export-get-date' specifications."
  2437. ;; Return a properly formatted string when
  2438. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2439. ;; consists in a single timestamp.
  2440. (should
  2441. (equal "29 03 2012"
  2442. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2443. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2444. (org-export-get-date info)))))
  2445. ;; Return a secondary string otherwise.
  2446. (should-not
  2447. (stringp
  2448. (let ((org-export-date-timestamp-format nil))
  2449. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2450. (org-export-get-date info)))))
  2451. (should
  2452. (equal '("Date")
  2453. (org-test-with-parsed-data "#+DATE: Date"
  2454. (org-export-get-date info))))
  2455. ;; Optional argument has precedence over
  2456. ;; `org-export-date-timestamp-format'.
  2457. (should
  2458. (equal "29 03"
  2459. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2460. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2461. (org-export-get-date info "%d %m"))))))
  2462. ;;; Links
  2463. (ert-deftest test-org-export/custom-protocol-maybe ()
  2464. "Test `org-export-custom-protocol-maybe' specifications."
  2465. (should
  2466. (string-match
  2467. "success"
  2468. (progn
  2469. (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
  2470. (org-export-string-as
  2471. "[[foo:path]]"
  2472. (org-export-create-backend
  2473. :name 'test
  2474. :transcoders
  2475. '((section . (lambda (s c i) c))
  2476. (paragraph . (lambda (p c i) c))
  2477. (link . (lambda (l c i)
  2478. (or (org-export-custom-protocol-maybe l c 'test)
  2479. "failure")))))))))
  2480. (should-not
  2481. (string-match
  2482. "success"
  2483. (progn
  2484. (org-link-set-parameters
  2485. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2486. (org-export-string-as
  2487. "[[foo:path]]"
  2488. (org-export-create-backend
  2489. :name 'no-test
  2490. :transcoders
  2491. '((section . (lambda (s c i) c))
  2492. (paragraph . (lambda (p c i) c))
  2493. (link . (lambda (l c i)
  2494. (or (org-export-custom-protocol-maybe l c 'no-test)
  2495. "failure")))))))))
  2496. ;; Ignore anonymous back-ends.
  2497. (should-not
  2498. (string-match
  2499. "success"
  2500. (progn
  2501. (org-link-set-parameters
  2502. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2503. (org-export-string-as
  2504. "[[foo:path]]"
  2505. (org-export-create-backend
  2506. :transcoders
  2507. '((section . (lambda (s c i) c))
  2508. (paragraph . (lambda (p c i) c))
  2509. (link . (lambda (l c i)
  2510. (or (org-export-custom-protocol-maybe l c nil)
  2511. "failure"))))))))))
  2512. (ert-deftest test-org-export/get-coderef-format ()
  2513. "Test `org-export-get-coderef-format' specifications."
  2514. ;; A link without description returns "%s"
  2515. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2516. "%s"))
  2517. ;; Return "%s" when path is matched within description.
  2518. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2519. "desc %s"))
  2520. ;; Otherwise return description.
  2521. (should (equal (org-export-get-coderef-format "path" "desc")
  2522. "desc")))
  2523. (ert-deftest test-org-export/inline-image-p ()
  2524. "Test `org-export-inline-image-p' specifications."
  2525. (should
  2526. (org-export-inline-image-p
  2527. (org-test-with-temp-text "[[#id]]"
  2528. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2529. '(("custom-id" . "id")))))
  2530. (ert-deftest test-org-export/fuzzy-link ()
  2531. "Test fuzzy links specifications."
  2532. ;; Link to an headline should return headline's number.
  2533. (should
  2534. ;; Note: Headline's number is in fact a list of numbers.
  2535. (equal '(2)
  2536. (org-test-with-parsed-data
  2537. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2538. (org-element-map tree 'link
  2539. (lambda (link)
  2540. (org-export-get-ordinal
  2541. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2542. ;; Link to a target in an item should return item's number.
  2543. (should
  2544. ;; Note: Item's number is in fact a list of numbers.
  2545. (equal '(1 2)
  2546. (org-test-with-parsed-data
  2547. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2548. (org-element-map tree 'link
  2549. (lambda (link)
  2550. (org-export-get-ordinal
  2551. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2552. ;; Link to a target in a footnote should return footnote's number.
  2553. (should
  2554. (equal '(2 3)
  2555. (org-test-with-parsed-data "
  2556. Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
  2557. \[fn:1] A
  2558. \[fn:2] <<test>>B"
  2559. (org-element-map tree 'link
  2560. (lambda (link)
  2561. (org-export-get-ordinal
  2562. (org-export-resolve-fuzzy-link link info) info)) info))))
  2563. ;; Link to a named element should return sequence number of that
  2564. ;; element.
  2565. (should
  2566. (= 2
  2567. (org-test-with-parsed-data
  2568. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2569. (org-element-map tree 'link
  2570. (lambda (link)
  2571. (org-export-get-ordinal
  2572. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2573. ;; Link to a target not within an item, a table, a footnote
  2574. ;; reference or definition should return section number.
  2575. (should
  2576. (equal '(2)
  2577. (org-test-with-parsed-data
  2578. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2579. (org-element-map tree 'link
  2580. (lambda (link)
  2581. (org-export-get-ordinal
  2582. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2583. ;; Space are not significant when matching a fuzzy link.
  2584. (should
  2585. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2586. (org-element-map tree 'link
  2587. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2588. info t)))
  2589. ;; Statistics cookies are ignored for headline match.
  2590. (should
  2591. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2592. (org-element-map tree 'link
  2593. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2594. info t)))
  2595. (should
  2596. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2597. (org-element-map tree 'link
  2598. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2599. info t))))
  2600. (defun test-org-gen-loc-list(text type)
  2601. (org-test-with-parsed-data text
  2602. (org-element-map tree type
  2603. (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
  2604. (ert-deftest test-org-export/get-loc ()
  2605. "Test `org-export-get-loc' specifications."
  2606. (should
  2607. ;; "-n" resets line number.
  2608. (equal '(0)
  2609. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
  2610. 'example-block)))
  2611. ;; The first "+n" has 0 lines before it
  2612. (should
  2613. (equal '(0)
  2614. (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
  2615. 'example-block)))
  2616. ;; "-n 10" resets line number but has "9 lines" before it.
  2617. (should
  2618. (equal '(9)
  2619. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
  2620. 'example-block)))
  2621. ;; -n10 with two lines then +n 15
  2622. (should
  2623. (equal '(9 25)
  2624. (test-org-gen-loc-list "
  2625. #+BEGIN_EXAMPLE -n 10
  2626. Text_10
  2627. Second line(11)
  2628. #+END_EXAMPLE
  2629. #+BEGIN_EXAMPLE +n 15
  2630. Text line (11 + 15)
  2631. #+END_EXAMPLE"
  2632. 'example-block)))
  2633. (should
  2634. (equal '(9 19 0)
  2635. (test-org-gen-loc-list "
  2636. #+BEGIN_EXAMPLE -n 10
  2637. Text
  2638. #+END_EXAMPLE
  2639. #+BEGIN_EXAMPLE +n 10
  2640. Text
  2641. #+END_EXAMPLE
  2642. #+BEGIN_EXAMPLE -n
  2643. Text
  2644. #+END_EXAMPLE"
  2645. 'example-block)))
  2646. ;; an Example Block without -n does not add to the line count.
  2647. (should
  2648. (equal '(9 no-loc 19)
  2649. (test-org-gen-loc-list "
  2650. #+BEGIN_EXAMPLE -n 10
  2651. Text
  2652. #+END_EXAMPLE
  2653. #+BEGIN_EXAMPLE
  2654. Text
  2655. #+END_EXAMPLE
  2656. #+BEGIN_EXAMPLE +n 10
  2657. Text
  2658. #+END_EXAMPLE"
  2659. 'example-block)))
  2660. ;; "-n" resets line number.
  2661. (should
  2662. (equal
  2663. '(0)
  2664. (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
  2665. 'src-block)))
  2666. ;; The first "+n" has 0 lines before it.
  2667. (should
  2668. (equal '(0)
  2669. (test-org-gen-loc-list
  2670. "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
  2671. 'src-block)))
  2672. ;; "-n 10" resets line number but has "9 lines" before it.
  2673. (should
  2674. (equal '(9)
  2675. (test-org-gen-loc-list
  2676. "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
  2677. 'src-block)))
  2678. (should
  2679. (equal '(9 25)
  2680. (test-org-gen-loc-list "
  2681. #+BEGIN_SRC emacs-lisp -n 10
  2682. (- 10 1)
  2683. (+ (- 10 1) 1)
  2684. #+END_SRC
  2685. #+BEGIN_SRC emacs-lisp +n 15
  2686. (+ (- 10 1) 2 (- 15 1))
  2687. #+END_SRC"
  2688. 'src-block)))
  2689. (should
  2690. (equal '(9 19 0)
  2691. (test-org-gen-loc-list "
  2692. #+BEGIN_SRC emacs-lisp -n 10
  2693. (- 10 1)
  2694. #+END_SRC
  2695. #+BEGIN_SRC emacs-lisp +n 10
  2696. (+ (- 10 1) 1 (- 10 1))
  2697. #+END_SRC
  2698. #+BEGIN_SRC emacs-lisp -n
  2699. (- 1 1)
  2700. #+END_SRC"
  2701. 'src-block)))
  2702. ;; A SRC Block without -n does not add to the line count.
  2703. (should
  2704. (equal '(9 no-loc 19)
  2705. (test-org-gen-loc-list
  2706. "#+BEGIN_SRC emacs-lisp -n 10
  2707. (+ (-10 1) 1)
  2708. #+END_SRC
  2709. #+BEGIN_SRC emacs-lisp
  2710. (+ 2 2)
  2711. #+END_SRC
  2712. #+BEGIN_SRC emacs-lisp +n 10
  2713. (+ (- 10 1) 1 (- 10 1))
  2714. #+END_SRC"
  2715. 'src-block))))
  2716. (ert-deftest test-org-export/resolve-coderef ()
  2717. "Test `org-export-resolve-coderef' specifications."
  2718. (let ((org-coderef-label-format "(ref:%s)"))
  2719. ;; A link to a "-n -k -r" block returns line number.
  2720. (should
  2721. (= 1
  2722. (org-test-with-parsed-data
  2723. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2724. (org-export-resolve-coderef "coderef" info))))
  2725. (should
  2726. (= 10
  2727. (org-test-with-parsed-data
  2728. "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2729. (org-export-resolve-coderef "coderef" info))))
  2730. (should
  2731. (= 135
  2732. (org-test-with-parsed-data
  2733. "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
  2734. #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2735. (org-export-resolve-coderef "coderef" info))))
  2736. (should
  2737. (= 1
  2738. (org-test-with-parsed-data
  2739. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2740. (org-export-resolve-coderef "coderef" info))))
  2741. (should
  2742. (= 10
  2743. (org-test-with-parsed-data
  2744. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2745. (org-export-resolve-coderef "coderef" info))))
  2746. (should
  2747. (= 135
  2748. (org-test-with-parsed-data
  2749. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
  2750. #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2751. (org-export-resolve-coderef "coderef" info))))
  2752. ;; A link to a "-n -r" block returns line number.
  2753. (should
  2754. (= 1
  2755. (org-test-with-parsed-data
  2756. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2757. (org-export-resolve-coderef "coderef" info))))
  2758. (should
  2759. (= 10
  2760. (org-test-with-parsed-data
  2761. "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2762. (org-export-resolve-coderef "coderef" info))))
  2763. (should
  2764. (= 135
  2765. (org-test-with-parsed-data
  2766. "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
  2767. #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2768. (org-export-resolve-coderef "coderef" info))))
  2769. (should
  2770. (= 1
  2771. (org-test-with-parsed-data
  2772. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2773. (org-export-resolve-coderef "coderef" info))))
  2774. (should
  2775. (= 10
  2776. (org-test-with-parsed-data
  2777. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2778. (org-export-resolve-coderef "coderef" info))))
  2779. (should
  2780. (= 135
  2781. (org-test-with-parsed-data
  2782. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
  2783. #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2784. (org-export-resolve-coderef "coderef" info))))
  2785. ;; A link to a "-n" block returns coderef.
  2786. (should
  2787. (equal "coderef"
  2788. (org-test-with-parsed-data
  2789. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2790. (org-export-resolve-coderef "coderef" info))))
  2791. (should
  2792. (equal "coderef"
  2793. (org-test-with-parsed-data
  2794. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2795. (org-export-resolve-coderef "coderef" info))))
  2796. ;; A link to a "-r" block returns line number.
  2797. (should
  2798. (= 1
  2799. (org-test-with-parsed-data
  2800. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2801. (org-export-resolve-coderef "coderef" info))))
  2802. (should
  2803. (= 1
  2804. (org-test-with-parsed-data
  2805. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2806. (org-export-resolve-coderef "coderef" info))))
  2807. ;; A link to a block without a switch returns coderef.
  2808. (should
  2809. (equal "coderef"
  2810. (org-test-with-parsed-data
  2811. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2812. (org-export-resolve-coderef "coderef" info))))
  2813. (org-test-with-parsed-data
  2814. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2815. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2816. ;; Correctly handle continued line numbers. A "+n" switch should
  2817. ;; resume numbering from previous block with numbered lines,
  2818. ;; ignoring blocks not numbering lines in the process. A "-n"
  2819. ;; switch resets count.
  2820. (should
  2821. (equal '(2 1)
  2822. (org-test-with-parsed-data "
  2823. #+BEGIN_EXAMPLE -n
  2824. Text.
  2825. #+END_EXAMPLE
  2826. #+BEGIN_SRC emacs-lisp
  2827. \(- 1 1)
  2828. #+END_SRC
  2829. #+BEGIN_SRC emacs-lisp +n -r
  2830. \(+ 1 1) (ref:addition)
  2831. #+END_SRC
  2832. #+BEGIN_EXAMPLE -n -r
  2833. Another text. (ref:text)
  2834. #+END_EXAMPLE"
  2835. (list (org-export-resolve-coderef "addition" info)
  2836. (org-export-resolve-coderef "text" info)))))
  2837. ;; Recognize coderef with user-specified syntax.
  2838. (should
  2839. (equal
  2840. "text"
  2841. (org-test-with-parsed-data
  2842. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2843. (org-export-resolve-coderef "text" info))))
  2844. ;; Unresolved coderefs raise a `org-link-broken' signal.
  2845. (should
  2846. (condition-case nil
  2847. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2848. (org-export-resolve-coderef "unknown" info))
  2849. (org-link-broken t)))))
  2850. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2851. "Test `org-export-resolve-fuzzy-link' specifications."
  2852. ;; Match target objects.
  2853. (should
  2854. (org-test-with-parsed-data "<<target>> [[target]]"
  2855. (org-export-resolve-fuzzy-link
  2856. (org-element-map tree 'link 'identity info t) info)))
  2857. ;; Match named elements.
  2858. (should
  2859. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2860. (org-export-resolve-fuzzy-link
  2861. (org-element-map tree 'link 'identity info t) info)))
  2862. ;; Match exact headline's name.
  2863. (should
  2864. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2865. (org-export-resolve-fuzzy-link
  2866. (org-element-map tree 'link 'identity info t) info)))
  2867. ;; Targets objects have priority over headline titles.
  2868. (should
  2869. (eq 'target
  2870. (org-test-with-parsed-data "* target\n<<target>>[[target]]"
  2871. (org-element-type
  2872. (org-export-resolve-fuzzy-link
  2873. (org-element-map tree 'link 'identity info t) info)))))
  2874. ;; Named elements have priority over headline titles.
  2875. (should
  2876. (eq 'paragraph
  2877. (org-test-with-parsed-data
  2878. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2879. (org-element-type
  2880. (org-export-resolve-fuzzy-link
  2881. (org-element-map tree 'link 'identity info t) info)))))
  2882. ;; If link's path starts with a "*", only match headline titles,
  2883. ;; though.
  2884. (should
  2885. (eq 'headline
  2886. (org-test-with-parsed-data
  2887. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2888. (org-element-type
  2889. (org-export-resolve-fuzzy-link
  2890. (org-element-map tree 'link 'identity info t) info)))))
  2891. ;; Raise a `org-link-broken' signal if no match.
  2892. (should
  2893. (org-test-with-parsed-data "[[target]]"
  2894. (condition-case nil
  2895. (org-export-resolve-fuzzy-link
  2896. (org-element-map tree 'link #'identity info t) info)
  2897. (org-link-broken t))))
  2898. ;; Match fuzzy link even when before first headline.
  2899. (should
  2900. (eq 'headline
  2901. (org-test-with-parsed-data "[[hl]]\n* hl"
  2902. (org-element-type
  2903. (org-export-resolve-fuzzy-link
  2904. (org-element-map tree 'link 'identity info t) info)))))
  2905. ;; Handle url-encoded fuzzy links.
  2906. (should
  2907. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  2908. (org-export-resolve-fuzzy-link
  2909. (org-element-map tree 'link #'identity info t) info))))
  2910. (ert-deftest test-org-export/resolve-id-link ()
  2911. "Test `org-export-resolve-id-link' specifications."
  2912. ;; Regular test for custom-id link.
  2913. (should
  2914. (equal '("Headline1")
  2915. (org-test-with-parsed-data "* Headline1
  2916. :PROPERTIES:
  2917. :CUSTOM_ID: test
  2918. :END:
  2919. * Headline 2
  2920. \[[#test]]"
  2921. (org-element-property
  2922. :title
  2923. (org-export-resolve-id-link
  2924. (org-element-map tree 'link 'identity info t) info)))))
  2925. ;; Raise a `org-link-broken' signal on failing searches.
  2926. (should
  2927. (org-test-with-parsed-data "* Headline1
  2928. :PROPERTIES:
  2929. :CUSTOM_ID: test
  2930. :END:
  2931. * Headline 2
  2932. \[[#no-match]]"
  2933. (condition-case nil
  2934. (org-export-resolve-id-link
  2935. (org-element-map tree 'link #'identity info t) info)
  2936. (org-link-broken t))))
  2937. ;; Test for internal id target.
  2938. (should
  2939. (equal '("Headline1")
  2940. (org-test-with-parsed-data "* Headline1
  2941. :PROPERTIES:
  2942. :ID: aaaa
  2943. :END:
  2944. * Headline 2
  2945. \[[id:aaaa]]"
  2946. (org-element-property
  2947. :title
  2948. (org-export-resolve-id-link
  2949. (org-element-map tree 'link 'identity info t) info)))))
  2950. ;; Test for external id target.
  2951. (should
  2952. (equal
  2953. "external-file"
  2954. (org-test-with-parsed-data "[[id:aaaa]]"
  2955. (org-export-resolve-id-link
  2956. (org-element-map tree 'link 'identity info t)
  2957. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  2958. (ert-deftest test-org-export/resolve-radio-link ()
  2959. "Test `org-export-resolve-radio-link' specifications."
  2960. ;; Standard test.
  2961. (should
  2962. (org-test-with-temp-text "<<<radio>>> radio"
  2963. (org-update-radio-target-regexp)
  2964. (let* ((tree (org-element-parse-buffer))
  2965. (info `(:parse-tree ,tree)))
  2966. (org-export-resolve-radio-link
  2967. (org-element-map tree 'link 'identity info t)
  2968. info))))
  2969. ;; Radio targets are case-insensitive.
  2970. (should
  2971. (org-test-with-temp-text "<<<RADIO>>> radio"
  2972. (org-update-radio-target-regexp)
  2973. (let* ((tree (org-element-parse-buffer))
  2974. (info `(:parse-tree ,tree)))
  2975. (org-export-resolve-radio-link
  2976. (org-element-map tree 'link 'identity info t)
  2977. info))))
  2978. ;; Radio target with objects.
  2979. (should
  2980. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2981. (org-update-radio-target-regexp)
  2982. (let* ((tree (org-element-parse-buffer))
  2983. (info `(:parse-tree ,tree)))
  2984. (org-export-resolve-radio-link
  2985. (org-element-map tree 'link 'identity info t)
  2986. info))))
  2987. ;; Radio target with objects at its beginning.
  2988. (should
  2989. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2990. (org-update-radio-target-regexp)
  2991. (let* ((tree (org-element-parse-buffer))
  2992. (info `(:parse-tree ,tree)))
  2993. (org-export-resolve-radio-link
  2994. (org-element-map tree 'link 'identity info t)
  2995. info))))
  2996. ;; Radio link next to an apostrophe.
  2997. (should
  2998. (org-test-with-temp-text "<<<radio>>> radio's"
  2999. (org-update-radio-target-regexp)
  3000. (let* ((tree (org-element-parse-buffer))
  3001. (info `(:parse-tree ,tree)))
  3002. (org-export-resolve-radio-link
  3003. (org-element-map tree 'link 'identity info t)
  3004. info))))
  3005. ;; Multiple radio targets.
  3006. (should
  3007. (equal '("radio1" "radio2")
  3008. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  3009. (org-update-radio-target-regexp)
  3010. (let* ((tree (org-element-parse-buffer))
  3011. (info `(:parse-tree ,tree)))
  3012. (org-element-map tree 'link
  3013. (lambda (link)
  3014. (org-element-property
  3015. :value (org-export-resolve-radio-link link info)))
  3016. info)))))
  3017. ;; Radio target is whitespace insensitive.
  3018. (should
  3019. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  3020. (org-update-radio-target-regexp)
  3021. (let* ((tree (org-element-parse-buffer))
  3022. (info `(:parse-tree ,tree)))
  3023. (org-element-map tree 'link
  3024. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  3025. (ert-deftest test-org-export/file-uri ()
  3026. "Test `org-export-file-uri' specifications."
  3027. ;; Preserve relative filenames.
  3028. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  3029. ;; Local files start with "file:///"
  3030. (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
  3031. ;; Remote files start with "file://"
  3032. (should (equal "file://myself@some.where:papers/last.pdf"
  3033. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  3034. (should (equal "file://localhost/etc/fstab"
  3035. (org-export-file-uri "//localhost/etc/fstab")))
  3036. ;; Expand filename starting with "~".
  3037. (should (equal (org-export-file-uri "~/file.org")
  3038. (concat "file://" (expand-file-name "~/file.org")))))
  3039. (ert-deftest test-org-export/get-reference ()
  3040. "Test `org-export-get-reference' specifications."
  3041. (should
  3042. (org-test-with-parsed-data "* Headline"
  3043. (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
  3044. info)))
  3045. ;; For a given element always return the same reference.
  3046. (should
  3047. (org-test-with-parsed-data "* Headline"
  3048. (let ((headline (org-element-map tree 'headline #'identity nil t)))
  3049. (equal (org-export-get-reference headline info)
  3050. (org-export-get-reference headline info)))))
  3051. ;; Use search cells defined in `:crossrefs'.
  3052. (should
  3053. (equal "org0000001"
  3054. (org-test-with-parsed-data "* Headline"
  3055. (let* ((headline (org-element-map tree 'headline #'identity nil t))
  3056. (search-cell (car (org-export-search-cells headline))))
  3057. (setq info
  3058. (plist-put info :crossrefs (list (cons search-cell 1))))
  3059. (org-export-get-reference headline info))))))
  3060. ;;; Pseudo objects and pseudo elements
  3061. (ert-deftest test-org-export/pseudo-elements ()
  3062. "Test exporting pseudo-elements."
  3063. ;; Handle blank lines after pseudo-elements. In particular, do not
  3064. ;; replace them with white spaces.
  3065. (should
  3066. (equal "contents\n\nparagraph\n"
  3067. (let ((backend (org-export-create-backend
  3068. :transcoders
  3069. '((pseudo-element . (lambda (_p c _i) c))
  3070. (paragraph . (lambda (_p c _i) c))
  3071. (plain-text . (lambda (c _i) c)))))
  3072. (element '(pseudo-element (:post-blank 1) "contents"))
  3073. (paragraph '(paragraph nil "paragraph"))
  3074. (data '(org-data nil)))
  3075. (org-element-adopt-elements data element paragraph)
  3076. (org-export-data-with-backend data backend nil)))))
  3077. (ert-deftest test-org-export/pseudo-objects ()
  3078. "Test exporting pseudo-objects."
  3079. ;; Handle blank spaces after pseudo-objects. In particular, do not
  3080. ;; replace them with newlines.
  3081. (should
  3082. (equal "begin x end\n"
  3083. (let ((backend (org-export-create-backend
  3084. :transcoders
  3085. '((pseudo-object . (lambda (_p c _i) c))
  3086. (paragraph . (lambda (_p c _i) c))
  3087. (plain-text . (lambda (c _i) c)))))
  3088. (object '(pseudo-object (:post-blank 1) "x"))
  3089. (paragraph '(paragraph nil)))
  3090. (org-element-adopt-elements paragraph "begin " object "end")
  3091. (org-export-data-with-backend paragraph backend nil)))))
  3092. ;;; Src-block and example-block
  3093. (ert-deftest test-org-export/unravel-code ()
  3094. "Test `org-export-unravel-code' function."
  3095. ;; Code without reference.
  3096. (should
  3097. (equal '("(+ 1 1)\n")
  3098. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3099. (org-export-unravel-code (org-element-at-point)))))
  3100. ;; Code with reference.
  3101. (should
  3102. (equal '("(+ 1 1)\n" (1 . "test"))
  3103. (org-test-with-temp-text
  3104. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  3105. (let ((org-coderef-label-format "(ref:%s)"))
  3106. (org-export-unravel-code (org-element-at-point))))))
  3107. ;; Code with user-defined reference.
  3108. (should
  3109. (equal
  3110. '("(+ 1 1)\n" (1 . "test"))
  3111. (org-test-with-temp-text
  3112. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  3113. (let ((org-coderef-label-format "(ref:%s)"))
  3114. (org-export-unravel-code (org-element-at-point))))))
  3115. ;; Code references keys are relative to the current block.
  3116. (should
  3117. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  3118. (org-test-with-temp-text "
  3119. #+BEGIN_EXAMPLE -n
  3120. \(+ 1 1)
  3121. #+END_EXAMPLE
  3122. #+BEGIN_EXAMPLE +n
  3123. \(+ 2 2)
  3124. \(+ 3 3) (ref:one)
  3125. #+END_EXAMPLE"
  3126. (goto-line 5)
  3127. (let ((org-coderef-label-format "(ref:%s)"))
  3128. (org-export-unravel-code (org-element-at-point)))))))
  3129. (ert-deftest test-org-export/format-code-default ()
  3130. "Test `org-export-format-code-default' specifications."
  3131. ;; Return the empty string when code is empty.
  3132. (should
  3133. (equal ""
  3134. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  3135. (org-export-format-code-default
  3136. (org-element-map tree 'src-block 'identity info t) info))))
  3137. ;; Number lines, two whitespace characters before the actual loc.
  3138. (should
  3139. (equal "1 a\n2 b\n"
  3140. (org-test-with-parsed-data
  3141. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  3142. (org-export-format-code-default
  3143. (org-element-map tree 'src-block 'identity info t) info))))
  3144. ;; Put references 6 whitespace characters after the widest line,
  3145. ;; wrapped within parenthesis.
  3146. (should
  3147. (equal "123 (a)\n1 (b)\n"
  3148. (let ((org-coderef-label-format "(ref:%s)"))
  3149. (org-test-with-parsed-data
  3150. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  3151. (org-export-format-code-default
  3152. (org-element-map tree 'src-block 'identity info t) info))))))
  3153. ;;; Smart Quotes
  3154. (ert-deftest test-org-export/activate-smart-quotes ()
  3155. "Test `org-export-activate-smart-quotes' specifications."
  3156. ;; Double quotes: standard test.
  3157. (should
  3158. (equal
  3159. '("some &ldquo;quoted&rdquo; text")
  3160. (let ((org-export-default-language "en"))
  3161. (org-test-with-parsed-data "some \"quoted\" text"
  3162. (org-element-map tree 'plain-text
  3163. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3164. info)))))
  3165. ;; Opening quotes: at the beginning of a paragraph.
  3166. (should
  3167. (equal
  3168. '("&ldquo;begin")
  3169. (let ((org-export-default-language "en"))
  3170. (org-test-with-parsed-data "\"begin"
  3171. (org-element-map tree 'plain-text
  3172. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3173. info)))))
  3174. ;; Opening quotes: after an object.
  3175. (should
  3176. (equal
  3177. '("&ldquo;quoted&rdquo; text")
  3178. (let ((org-export-default-language "en"))
  3179. (org-test-with-parsed-data "=verb= \"quoted\" text"
  3180. (org-element-map tree 'plain-text
  3181. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3182. info)))))
  3183. ;; Closing quotes: at the end of a paragraph.
  3184. (should
  3185. (equal
  3186. '("Quoted &ldquo;text&rdquo;")
  3187. (let ((org-export-default-language "en"))
  3188. (org-test-with-parsed-data "Quoted \"text\""
  3189. (org-element-map tree 'plain-text
  3190. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3191. info)))))
  3192. ;; Inner quotes: standard test.
  3193. (should
  3194. (equal '("« outer « inner » outer »")
  3195. (let ((org-export-default-language "fr"))
  3196. (org-test-with-parsed-data "\"outer 'inner' outer\""
  3197. (org-element-map tree 'plain-text
  3198. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3199. info)))))
  3200. ;; Inner quotes: close to special symbols.
  3201. (should
  3202. (equal '("« outer (« inner ») outer »")
  3203. (let ((org-export-default-language "fr"))
  3204. (org-test-with-parsed-data "\"outer ('inner') outer\""
  3205. (org-element-map tree 'plain-text
  3206. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3207. info)))))
  3208. (should
  3209. (equal '("« « inner » »")
  3210. (let ((org-export-default-language "fr"))
  3211. (org-test-with-parsed-data "\"'inner'\""
  3212. (org-element-map tree 'plain-text
  3213. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3214. info)))))
  3215. ;; Apostrophe: standard test.
  3216. (should
  3217. (equal '("It « shouldn’t » fail")
  3218. (let ((org-export-default-language "fr"))
  3219. (org-test-with-parsed-data "It \"shouldn't\" fail"
  3220. (org-element-map tree 'plain-text
  3221. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3222. info)))))
  3223. (should
  3224. (equal '("It shouldn’t fail")
  3225. (let ((org-export-default-language "fr"))
  3226. (org-test-with-parsed-data "It shouldn't fail"
  3227. (org-element-map tree 'plain-text
  3228. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3229. info)))))
  3230. ;; Apostrophe: before an object.
  3231. (should
  3232. (equal
  3233. '("« a’" " »")
  3234. (let ((org-export-default-language "fr"))
  3235. (org-test-with-parsed-data "\"a'=b=\""
  3236. (org-element-map tree 'plain-text
  3237. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3238. info)))))
  3239. ;; Apostrophe: after an object.
  3240. (should
  3241. (equal '("« " "’s »")
  3242. (let ((org-export-default-language "fr"))
  3243. (org-test-with-parsed-data "\"=code='s\""
  3244. (org-element-map tree 'plain-text
  3245. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3246. info)))))
  3247. ;; Special case: isolated quotes.
  3248. (should
  3249. (equal '("&ldquo;" "&rdquo;")
  3250. (let ((org-export-default-language "en"))
  3251. (org-test-with-parsed-data "\"$x$\""
  3252. (org-element-map tree 'plain-text
  3253. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3254. info)))))
  3255. ;; Smart quotes in secondary strings.
  3256. (should
  3257. (equal '("&ldquo;" "&rdquo;")
  3258. (let ((org-export-default-language "en"))
  3259. (org-test-with-parsed-data "* \"$x$\""
  3260. (org-element-map tree 'plain-text
  3261. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3262. info)))))
  3263. ;; Smart quotes in document keywords.
  3264. (should
  3265. (equal '("&ldquo;" "&rdquo;")
  3266. (let ((org-export-default-language "en"))
  3267. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  3268. (org-element-map (plist-get info :title) 'plain-text
  3269. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3270. info)))))
  3271. ;; Smart quotes in parsed affiliated keywords.
  3272. (should
  3273. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  3274. (let ((org-export-default-language "en"))
  3275. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3276. (org-element-map tree 'plain-text
  3277. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3278. info nil nil t)))))
  3279. ;; Smart quotes within objects.
  3280. (should
  3281. (equal '("&ldquo;foo&rdquo;")
  3282. (let ((org-export-default-language "en"))
  3283. (org-test-with-parsed-data "| \"foo\" |"
  3284. (org-element-map tree 'plain-text
  3285. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3286. info nil nil t)))))
  3287. ;; FIXME: Test failing non-interactively.
  3288. ;;
  3289. ;; (should
  3290. ;; (equal '("&ldquo;foo&rdquo;")
  3291. ;; (let ((org-export-default-language "en"))
  3292. ;; (org-test-with-parsed-data "*\"foo\"*"
  3293. ;; (org-element-map tree 'plain-text
  3294. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3295. ;; info nil nil t)))))
  3296. )
  3297. ;;; Tables
  3298. (ert-deftest test-org-export/special-column ()
  3299. "Test if the table's special column is properly recognized."
  3300. ;; 1. First column is special if it contains only a special marking
  3301. ;; characters or empty cells.
  3302. (org-test-with-temp-text "
  3303. | ! | 1 |
  3304. | | 2 |"
  3305. (should
  3306. (org-export-table-has-special-column-p
  3307. (org-element-map
  3308. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3309. ;; 2. If the column contains anything else, it isn't special.
  3310. (org-test-with-temp-text "
  3311. | ! | 1 |
  3312. | b | 2 |"
  3313. (should-not
  3314. (org-export-table-has-special-column-p
  3315. (org-element-map
  3316. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3317. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3318. ;; and "!".
  3319. (org-test-with-temp-text "
  3320. | # | 1 |
  3321. | ^ | 2 |
  3322. | * | 3 |
  3323. | _ | 4 |
  3324. | / | 5 |
  3325. | $ | 6 |
  3326. | ! | 7 |"
  3327. (should
  3328. (org-export-table-has-special-column-p
  3329. (org-element-map
  3330. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3331. ;; 4. A first column with only empty cells isn't considered as
  3332. ;; special.
  3333. (org-test-with-temp-text "
  3334. | | 1 |
  3335. | | 2 |"
  3336. (should-not
  3337. (org-export-table-has-special-column-p
  3338. (org-element-map
  3339. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3340. (ert-deftest test-org-export/table-row-is-special-p ()
  3341. "Test `org-export-table-row-is-special-p' specifications."
  3342. ;; 1. A row is special if it has a special marking character in the
  3343. ;; special column.
  3344. (org-test-with-parsed-data "| ! | 1 |"
  3345. (should
  3346. (org-export-table-row-is-special-p
  3347. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3348. ;; 2. A row is special when its first field is "/"
  3349. (org-test-with-parsed-data "
  3350. | / | 1 |
  3351. | a | b |"
  3352. (should
  3353. (org-export-table-row-is-special-p
  3354. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3355. ;; 3. A row only containing alignment cookies is also considered as
  3356. ;; special.
  3357. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3358. (should
  3359. (org-export-table-row-is-special-p
  3360. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3361. ;; 4. Everything else isn't considered as special.
  3362. (org-test-with-parsed-data "| \alpha | | c |"
  3363. (should-not
  3364. (org-export-table-row-is-special-p
  3365. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3366. ;; 5. Table's rules are never considered as special rows.
  3367. (org-test-with-parsed-data "|---+---|"
  3368. (should-not
  3369. (org-export-table-row-is-special-p
  3370. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3371. (ert-deftest test-org-export/has-header-p ()
  3372. "Test `org-export-table-has-header-p' specifications."
  3373. ;; 1. With an header.
  3374. (org-test-with-parsed-data "
  3375. | a | b |
  3376. |---+---|
  3377. | c | d |"
  3378. (should
  3379. (org-export-table-has-header-p
  3380. (org-element-map tree 'table 'identity info 'first-match)
  3381. info)))
  3382. ;; 2. Without an header.
  3383. (org-test-with-parsed-data "
  3384. | a | b |
  3385. | c | d |"
  3386. (should-not
  3387. (org-export-table-has-header-p
  3388. (org-element-map tree 'table 'identity info 'first-match)
  3389. info)))
  3390. ;; 3. Don't get fooled with starting and ending rules.
  3391. (org-test-with-parsed-data "
  3392. |---+---|
  3393. | a | b |
  3394. | c | d |
  3395. |---+---|"
  3396. (should-not
  3397. (org-export-table-has-header-p
  3398. (org-element-map tree 'table 'identity info 'first-match)
  3399. info))))
  3400. (ert-deftest test-org-export/table-row-group ()
  3401. "Test `org-export-table-row-group' specifications."
  3402. ;; 1. A rule creates a new group.
  3403. (should
  3404. (equal '(1 rule 2)
  3405. (org-test-with-parsed-data "
  3406. | a | b |
  3407. |---+---|
  3408. | 1 | 2 |"
  3409. (org-element-map tree 'table-row
  3410. (lambda (row)
  3411. (if (eq (org-element-property :type row) 'rule) 'rule
  3412. (org-export-table-row-group row info)))))))
  3413. ;; 2. Special rows are ignored in count.
  3414. (should
  3415. (equal
  3416. '(rule 1)
  3417. (org-test-with-parsed-data "
  3418. | / | < | > |
  3419. |---|---+---|
  3420. | | 1 | 2 |"
  3421. (org-element-map tree 'table-row
  3422. (lambda (row)
  3423. (if (eq (org-element-property :type row) 'rule) 'rule
  3424. (org-export-table-row-group row info)))
  3425. info))))
  3426. ;; 3. Double rules also are ignored in count.
  3427. (should
  3428. (equal '(1 rule rule 2)
  3429. (org-test-with-parsed-data "
  3430. | a | b |
  3431. |---+---|
  3432. |---+---|
  3433. | 1 | 2 |"
  3434. (org-element-map tree 'table-row
  3435. (lambda (row)
  3436. (if (eq (org-element-property :type row) 'rule) 'rule
  3437. (org-export-table-row-group row info))))))))
  3438. (ert-deftest test-org-export/table-row-number ()
  3439. "Test `org-export-table-row-number' specifications."
  3440. ;; Standard test. Number is 0-indexed.
  3441. (should
  3442. (equal '(0 1)
  3443. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3444. (org-element-map tree 'table-row
  3445. (lambda (row) (org-export-table-row-number row info)) info))))
  3446. ;; Number ignores separators.
  3447. (should
  3448. (equal '(0 1)
  3449. (org-test-with-parsed-data "
  3450. | a | b | c |
  3451. |---+---+---|
  3452. | d | e | f |"
  3453. (org-element-map tree 'table-row
  3454. (lambda (row) (org-export-table-row-number row info)) info))))
  3455. ;; Number ignores special rows.
  3456. (should
  3457. (equal '(0 1)
  3458. (org-test-with-parsed-data "
  3459. | / | < | > |
  3460. | | b | c |
  3461. |---+-----+-----|
  3462. | | <c> | <c> |
  3463. | | e | f |"
  3464. (org-element-map tree 'table-row
  3465. (lambda (row) (org-export-table-row-number row info)) info)))))
  3466. (ert-deftest test-org-export/table-cell-width ()
  3467. "Test `org-export-table-cell-width' specifications."
  3468. ;; 1. Width is primarily determined by width cookies. If no cookie
  3469. ;; is found, cell's width is nil.
  3470. (org-test-with-parsed-data "
  3471. | / | <l> | <6> | <l7> |
  3472. | | a | b | c |"
  3473. (should
  3474. (equal
  3475. '(nil 6 7)
  3476. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3477. (org-element-map tree 'table-cell 'identity info)))))
  3478. ;; 2. The last width cookie has precedence.
  3479. (org-test-with-parsed-data "
  3480. | <6> |
  3481. | <7> |
  3482. | a |"
  3483. (should
  3484. (equal
  3485. '(7)
  3486. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3487. (org-element-map tree 'table-cell 'identity info)))))
  3488. ;; 3. Valid width cookies must have a specific row.
  3489. (org-test-with-parsed-data "| <6> | cell |"
  3490. (should
  3491. (equal
  3492. '(nil nil)
  3493. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3494. (org-element-map tree 'table-cell 'identity))))))
  3495. (ert-deftest test-org-export/table-cell-alignment ()
  3496. "Test `org-export-table-cell-alignment' specifications."
  3497. ;; 1. Alignment is primarily determined by alignment cookies.
  3498. (should
  3499. (equal '(left center right)
  3500. (let ((org-table-number-fraction 0.5)
  3501. (org-table-number-regexp "^[0-9]+$"))
  3502. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3503. (mapcar (lambda (cell)
  3504. (org-export-table-cell-alignment cell info))
  3505. (org-element-map tree 'table-cell 'identity))))))
  3506. ;; 2. The last alignment cookie has precedence.
  3507. (should
  3508. (equal '(right right right)
  3509. (org-test-with-parsed-data "
  3510. | <l8> |
  3511. | cell |
  3512. | <r9> |"
  3513. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3514. (org-element-map tree 'table-cell 'identity)))))
  3515. ;; 3. If there's no cookie, cell's contents determine alignment.
  3516. ;; A column mostly made of cells containing numbers will align
  3517. ;; its cells to the right.
  3518. (should
  3519. (equal '(right right right)
  3520. (let ((org-table-number-fraction 0.5)
  3521. (org-table-number-regexp "^[0-9]+$"))
  3522. (org-test-with-parsed-data "
  3523. | 123 |
  3524. | some text |
  3525. | 12345 |"
  3526. (mapcar (lambda (cell)
  3527. (org-export-table-cell-alignment cell info))
  3528. (org-element-map tree 'table-cell 'identity))))))
  3529. ;; 4. Otherwise, they will be aligned to the left.
  3530. (should
  3531. (equal '(left left left)
  3532. (org-test-with-parsed-data "
  3533. | text |
  3534. | some text |
  3535. | \alpha |"
  3536. (mapcar (lambda (cell)
  3537. (org-export-table-cell-alignment cell info))
  3538. (org-element-map tree 'table-cell 'identity info))))))
  3539. (ert-deftest test-org-export/table-cell-borders ()
  3540. "Test `org-export-table-cell-borders' specifications."
  3541. ;; 1. Recognize various column groups indicators.
  3542. (org-test-with-parsed-data "| / | < | > | <> |"
  3543. (should
  3544. (equal
  3545. '((right bottom top) (left bottom top) (right bottom top)
  3546. (right left bottom top))
  3547. (mapcar (lambda (cell)
  3548. (org-export-table-cell-borders cell info))
  3549. (org-element-map tree 'table-cell 'identity)))))
  3550. ;; 2. Accept shortcuts to define column groups.
  3551. (org-test-with-parsed-data "| / | < | < |"
  3552. (should
  3553. (equal
  3554. '((right bottom top) (right left bottom top) (left bottom top))
  3555. (mapcar (lambda (cell)
  3556. (org-export-table-cell-borders cell info))
  3557. (org-element-map tree 'table-cell 'identity)))))
  3558. ;; 3. A valid column groups row must start with a "/".
  3559. (org-test-with-parsed-data "
  3560. | | < |
  3561. | a | b |"
  3562. (should
  3563. (equal '((top) (top) (bottom) (bottom))
  3564. (mapcar (lambda (cell)
  3565. (org-export-table-cell-borders cell info))
  3566. (org-element-map tree 'table-cell 'identity)))))
  3567. ;; 4. Take table rules into consideration.
  3568. (org-test-with-parsed-data "
  3569. | 1 |
  3570. |---|
  3571. | 2 |"
  3572. (should
  3573. (equal '((below top) (bottom above))
  3574. (mapcar (lambda (cell)
  3575. (org-export-table-cell-borders cell info))
  3576. (org-element-map tree 'table-cell 'identity)))))
  3577. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3578. ;; (resp. `bottom' and `below') borders. Any special row is
  3579. ;; ignored.
  3580. (org-test-with-parsed-data "
  3581. |---+----|
  3582. | / | |
  3583. | | 1 |
  3584. |---+----|"
  3585. (should
  3586. (equal '((bottom below top above))
  3587. (last
  3588. (mapcar (lambda (cell)
  3589. (org-export-table-cell-borders cell info))
  3590. (org-element-map tree 'table-cell 'identity)))))))
  3591. (ert-deftest test-org-export/table-dimensions ()
  3592. "Test `org-export-table-dimensions' specifications."
  3593. ;; 1. Standard test.
  3594. (org-test-with-parsed-data "
  3595. | 1 | 2 | 3 |
  3596. | 4 | 5 | 6 |"
  3597. (should
  3598. (equal '(2 . 3)
  3599. (org-export-table-dimensions
  3600. (org-element-map tree 'table 'identity info 'first-match) info))))
  3601. ;; 2. Ignore horizontal rules and special columns.
  3602. (org-test-with-parsed-data "
  3603. | / | < | > |
  3604. | 1 | 2 | 3 |
  3605. |---+---+---|
  3606. | 4 | 5 | 6 |"
  3607. (should
  3608. (equal '(2 . 3)
  3609. (org-export-table-dimensions
  3610. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3611. (ert-deftest test-org-export/table-cell-address ()
  3612. "Test `org-export-table-cell-address' specifications."
  3613. ;; 1. Standard test: index is 0-based.
  3614. (org-test-with-parsed-data "| a | b |"
  3615. (should
  3616. (equal '((0 . 0) (0 . 1))
  3617. (org-element-map tree 'table-cell
  3618. (lambda (cell) (org-export-table-cell-address cell info))
  3619. info))))
  3620. ;; 2. Special column isn't counted, nor are special rows.
  3621. (org-test-with-parsed-data "
  3622. | / | <> |
  3623. | | c |"
  3624. (should
  3625. (equal '(0 . 0)
  3626. (org-export-table-cell-address
  3627. (car (last (org-element-map tree 'table-cell 'identity info)))
  3628. info))))
  3629. ;; 3. Tables rules do not count either.
  3630. (org-test-with-parsed-data "
  3631. | a |
  3632. |---|
  3633. | b |
  3634. |---|
  3635. | c |"
  3636. (should
  3637. (equal '(2 . 0)
  3638. (org-export-table-cell-address
  3639. (car (last (org-element-map tree 'table-cell 'identity info)))
  3640. info))))
  3641. ;; 4. Return nil for special cells.
  3642. (org-test-with-parsed-data "| / | a |"
  3643. (should-not
  3644. (org-export-table-cell-address
  3645. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3646. info))))
  3647. (ert-deftest test-org-export/get-table-cell-at ()
  3648. "Test `org-export-get-table-cell-at' specifications."
  3649. ;; 1. Address ignores special columns, special rows and rules.
  3650. (org-test-with-parsed-data "
  3651. | / | <> |
  3652. | | a |
  3653. |---+----|
  3654. | | b |"
  3655. (should
  3656. (equal '("b")
  3657. (org-element-contents
  3658. (org-export-get-table-cell-at
  3659. '(1 . 0)
  3660. (org-element-map tree 'table 'identity info 'first-match)
  3661. info)))))
  3662. ;; 2. Return value for a non-existent address is nil.
  3663. (org-test-with-parsed-data "| a |"
  3664. (should-not
  3665. (org-export-get-table-cell-at
  3666. '(2 . 2)
  3667. (org-element-map tree 'table 'identity info 'first-match)
  3668. info)))
  3669. (org-test-with-parsed-data "| / |"
  3670. (should-not
  3671. (org-export-get-table-cell-at
  3672. '(0 . 0)
  3673. (org-element-map tree 'table 'identity info 'first-match)
  3674. info))))
  3675. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3676. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3677. ;; 1. A cell at a beginning of a row always starts a column group.
  3678. (org-test-with-parsed-data "| a |"
  3679. (should
  3680. (org-export-table-cell-starts-colgroup-p
  3681. (org-element-map tree 'table-cell 'identity info 'first-match)
  3682. info)))
  3683. ;; 2. Special column should be ignored when determining the
  3684. ;; beginning of the row.
  3685. (org-test-with-parsed-data "
  3686. | / | |
  3687. | | a |"
  3688. (should
  3689. (org-export-table-cell-starts-colgroup-p
  3690. (org-element-map tree 'table-cell 'identity info 'first-match)
  3691. info)))
  3692. ;; 2. Explicit column groups.
  3693. (org-test-with-parsed-data "
  3694. | / | | < |
  3695. | a | b | c |"
  3696. (should
  3697. (equal
  3698. '(yes no yes)
  3699. (org-element-map tree 'table-cell
  3700. (lambda (cell)
  3701. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3702. info)))))
  3703. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3704. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3705. ;; 1. A cell at the end of a row always ends a column group.
  3706. (org-test-with-parsed-data "| a |"
  3707. (should
  3708. (org-export-table-cell-ends-colgroup-p
  3709. (org-element-map tree 'table-cell 'identity info 'first-match)
  3710. info)))
  3711. ;; 2. Special column should be ignored when determining the
  3712. ;; beginning of the row.
  3713. (org-test-with-parsed-data "
  3714. | / | |
  3715. | | a |"
  3716. (should
  3717. (org-export-table-cell-ends-colgroup-p
  3718. (org-element-map tree 'table-cell 'identity info 'first-match)
  3719. info)))
  3720. ;; 3. Explicit column groups.
  3721. (org-test-with-parsed-data "
  3722. | / | < | |
  3723. | a | b | c |"
  3724. (should
  3725. (equal
  3726. '(yes no yes)
  3727. (org-element-map tree 'table-cell
  3728. (lambda (cell)
  3729. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3730. info)))))
  3731. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3732. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3733. ;; 1. A row at the beginning of a table always starts a row group.
  3734. ;; So does a row following a table rule.
  3735. (org-test-with-parsed-data "
  3736. | a |
  3737. |---|
  3738. | b |"
  3739. (should
  3740. (equal
  3741. '(yes no yes)
  3742. (org-element-map tree 'table-row
  3743. (lambda (row)
  3744. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3745. info))))
  3746. ;; 2. Special rows should be ignored when determining the beginning
  3747. ;; of the row.
  3748. (org-test-with-parsed-data "
  3749. | / | < |
  3750. | | a |
  3751. |---+---|
  3752. | / | < |
  3753. | | b |"
  3754. (should
  3755. (equal
  3756. '(yes no yes)
  3757. (org-element-map tree 'table-row
  3758. (lambda (row)
  3759. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3760. info)))))
  3761. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3762. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3763. ;; 1. A row at the end of a table always ends a row group. So does
  3764. ;; a row preceding a table rule.
  3765. (org-test-with-parsed-data "
  3766. | a |
  3767. |---|
  3768. | b |"
  3769. (should
  3770. (equal
  3771. '(yes no yes)
  3772. (org-element-map tree 'table-row
  3773. (lambda (row)
  3774. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3775. info))))
  3776. ;; 2. Special rows should be ignored when determining the beginning
  3777. ;; of the row.
  3778. (org-test-with-parsed-data "
  3779. | | a |
  3780. | / | < |
  3781. |---+---|
  3782. | | b |
  3783. | / | < |"
  3784. (should
  3785. (equal
  3786. '(yes no yes)
  3787. (org-element-map tree 'table-row
  3788. (lambda (row)
  3789. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3790. info)))))
  3791. (ert-deftest test-org-export/table-row-in-header-p ()
  3792. "Test `org-export-table-row-in-header-p' specifications."
  3793. ;; Standard test. Separators are always nil.
  3794. (should
  3795. (equal
  3796. '(yes no no)
  3797. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3798. (org-element-map tree 'table-row
  3799. (lambda (row)
  3800. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  3801. ;; Nil when there is no header.
  3802. (should
  3803. (equal
  3804. '(no no)
  3805. (org-test-with-parsed-data "| a |\n| b |"
  3806. (org-element-map tree 'table-row
  3807. (lambda (row)
  3808. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3809. (ert-deftest test-org-export/table-row-starts-header-p ()
  3810. "Test `org-export-table-row-starts-header-p' specifications."
  3811. ;; 1. Only the row starting the first row group starts the table
  3812. ;; header.
  3813. (org-test-with-parsed-data "
  3814. | a |
  3815. | b |
  3816. |---|
  3817. | c |"
  3818. (should
  3819. (equal
  3820. '(yes no no no)
  3821. (org-element-map tree 'table-row
  3822. (lambda (row)
  3823. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3824. info))))
  3825. ;; 2. A row cannot start an header if there's no header in the
  3826. ;; table.
  3827. (org-test-with-parsed-data "
  3828. | a |
  3829. |---|"
  3830. (should-not
  3831. (org-export-table-row-starts-header-p
  3832. (org-element-map tree 'table-row 'identity info 'first-match)
  3833. info))))
  3834. (ert-deftest test-org-export/table-row-ends-header-p ()
  3835. "Test `org-export-table-row-ends-header-p' specifications."
  3836. ;; 1. Only the row starting the first row group starts the table
  3837. ;; header.
  3838. (org-test-with-parsed-data "
  3839. | a |
  3840. | b |
  3841. |---|
  3842. | c |"
  3843. (should
  3844. (equal
  3845. '(no yes no no)
  3846. (org-element-map tree 'table-row
  3847. (lambda (row)
  3848. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3849. info))))
  3850. ;; 2. A row cannot start an header if there's no header in the
  3851. ;; table.
  3852. (org-test-with-parsed-data "
  3853. | a |
  3854. |---|"
  3855. (should-not
  3856. (org-export-table-row-ends-header-p
  3857. (org-element-map tree 'table-row 'identity info 'first-match)
  3858. info))))
  3859. ;;; Tables of Contents
  3860. (ert-deftest test-org-export/collect-headlines ()
  3861. "Test `org-export-collect-headlines' specifications."
  3862. ;; Standard test.
  3863. (should
  3864. (= 2
  3865. (length
  3866. (org-test-with-parsed-data "* H1\n** H2"
  3867. (org-export-collect-headlines info)))))
  3868. ;; Do not collect headlines below optional argument.
  3869. (should
  3870. (= 1
  3871. (length
  3872. (org-test-with-parsed-data "* H1\n** H2"
  3873. (org-export-collect-headlines info 1)))))
  3874. ;; Never collect headlines below maximum headline level.
  3875. (should
  3876. (= 1
  3877. (length
  3878. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3879. (org-export-collect-headlines info)))))
  3880. (should
  3881. (= 1
  3882. (length
  3883. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3884. (org-export-collect-headlines info 2)))))
  3885. ;; Collect headlines locally.
  3886. (should
  3887. (= 2
  3888. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  3889. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3890. (length (org-export-collect-headlines info nil scope))))))
  3891. ;; When collecting locally, optional level is relative.
  3892. (should
  3893. (= 1
  3894. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  3895. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3896. (length (org-export-collect-headlines info 1 scope)))))))
  3897. ;;; Templates
  3898. (ert-deftest test-org-export/inner-template ()
  3899. "Test `inner-template' translator specifications."
  3900. (should
  3901. (equal "Success!"
  3902. (org-test-with-temp-text "* Headline"
  3903. (org-export-as
  3904. (org-export-create-backend
  3905. :transcoders
  3906. '((inner-template . (lambda (contents info) "Success!"))
  3907. (headline . (lambda (h c i) "Headline"))))))))
  3908. ;; Inner template is applied even in a "body-only" export.
  3909. (should
  3910. (equal "Success!"
  3911. (org-test-with-temp-text "* Headline"
  3912. (org-export-as
  3913. (org-export-create-backend
  3914. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3915. (headline . (lambda (h c i) "Headline"))))
  3916. nil nil 'body-only)))))
  3917. (ert-deftest test-org-export/template ()
  3918. "Test `template' translator specifications."
  3919. (should
  3920. (equal "Success!"
  3921. (org-test-with-temp-text "* Headline"
  3922. (org-export-as
  3923. (org-export-create-backend
  3924. :transcoders '((template . (lambda (contents info) "Success!"))
  3925. (headline . (lambda (h c i) "Headline"))))))))
  3926. ;; Template is not applied in a "body-only" export.
  3927. (should-not
  3928. (equal "Success!"
  3929. (org-test-with-temp-text "* Headline"
  3930. (org-export-as
  3931. (org-export-create-backend
  3932. :transcoders '((template . (lambda (contents info) "Success!"))
  3933. (headline . (lambda (h c i) "Headline"))))
  3934. nil nil 'body-only)))))
  3935. ;;; Topology
  3936. (ert-deftest test-org-export/get-next-element ()
  3937. "Test `org-export-get-next-element' specifications."
  3938. ;; Standard test.
  3939. (should
  3940. (equal "b"
  3941. (org-test-with-parsed-data "* Headline\n*a* b"
  3942. (org-export-get-next-element
  3943. (org-element-map tree 'bold 'identity info t) info))))
  3944. ;; Return nil when no previous element.
  3945. (should-not
  3946. (org-test-with-parsed-data "* Headline\na *b*"
  3947. (org-export-get-next-element
  3948. (org-element-map tree 'bold 'identity info t) info)))
  3949. ;; Non-exportable elements are ignored.
  3950. (should-not
  3951. (let ((org-export-with-timestamps nil))
  3952. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3953. (org-export-get-next-element
  3954. (org-element-map tree 'entity 'identity info t) info))))
  3955. ;; Find next element in secondary strings.
  3956. (should
  3957. (eq 'verbatim
  3958. (org-test-with-parsed-data "* a =verb="
  3959. (org-element-type
  3960. (org-export-get-next-element
  3961. (org-element-map tree 'plain-text 'identity info t) info)))))
  3962. (should
  3963. (eq 'verbatim
  3964. (org-test-with-parsed-data "* /italic/ =verb="
  3965. (org-element-type
  3966. (org-export-get-next-element
  3967. (org-element-map tree 'italic 'identity info t) info)))))
  3968. ;; Find next element in document keywords.
  3969. (should
  3970. (eq 'verbatim
  3971. (org-test-with-parsed-data "#+TITLE: a =verb="
  3972. (org-element-type
  3973. (org-export-get-next-element
  3974. (org-element-map
  3975. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3976. ;; Find next element in parsed affiliated keywords.
  3977. (should
  3978. (eq 'verbatim
  3979. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3980. (org-element-type
  3981. (org-export-get-next-element
  3982. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3983. ;; With optional argument N, return a list containing all the
  3984. ;; following elements.
  3985. (should
  3986. (equal
  3987. '(bold code underline)
  3988. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3989. (mapcar 'car
  3990. (org-export-get-next-element
  3991. (org-element-map tree 'italic 'identity info t) info t)))))
  3992. ;; When N is a positive integer, return a list containing up to
  3993. ;; N following elements.
  3994. (should
  3995. (equal
  3996. '(bold code)
  3997. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3998. (mapcar 'car
  3999. (org-export-get-next-element
  4000. (org-element-map tree 'italic 'identity info t) info 2))))))
  4001. (ert-deftest test-org-export/get-previous-element ()
  4002. "Test `org-export-get-previous-element' specifications."
  4003. ;; Standard test.
  4004. (should
  4005. (equal "a "
  4006. (org-test-with-parsed-data "* Headline\na *b*"
  4007. (org-export-get-previous-element
  4008. (org-element-map tree 'bold 'identity info t) info))))
  4009. ;; Return nil when no previous element.
  4010. (should-not
  4011. (org-test-with-parsed-data "* Headline\n*a* b"
  4012. (org-export-get-previous-element
  4013. (org-element-map tree 'bold 'identity info t) info)))
  4014. ;; Non-exportable elements are ignored.
  4015. (should-not
  4016. (let ((org-export-with-timestamps nil))
  4017. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  4018. (org-export-get-previous-element
  4019. (org-element-map tree 'entity 'identity info t) info))))
  4020. ;; Find previous element in secondary strings.
  4021. (should
  4022. (eq 'verbatim
  4023. (org-test-with-parsed-data "* =verb= a"
  4024. (org-element-type
  4025. (org-export-get-previous-element
  4026. (org-element-map tree 'plain-text 'identity info t) info)))))
  4027. (should
  4028. (eq 'verbatim
  4029. (org-test-with-parsed-data "* =verb= /italic/"
  4030. (org-element-type
  4031. (org-export-get-previous-element
  4032. (org-element-map tree 'italic 'identity info t) info)))))
  4033. ;; Find previous element in document keywords.
  4034. (should
  4035. (eq 'verbatim
  4036. (org-test-with-parsed-data "#+TITLE: =verb= a"
  4037. (org-element-type
  4038. (org-export-get-previous-element
  4039. (org-element-map
  4040. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4041. ;; Find previous element in parsed affiliated keywords.
  4042. (should
  4043. (eq 'verbatim
  4044. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  4045. (org-element-type
  4046. (org-export-get-previous-element
  4047. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4048. ;; With optional argument N, return a list containing up to
  4049. ;; N previous elements.
  4050. (should
  4051. (equal '(underline italic bold)
  4052. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4053. (mapcar 'car
  4054. (org-export-get-previous-element
  4055. (org-element-map tree 'code 'identity info t) info t)))))
  4056. ;; When N is a positive integer, return a list containing up to
  4057. ;; N previous elements.
  4058. (should
  4059. (equal '(italic bold)
  4060. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4061. (mapcar 'car
  4062. (org-export-get-previous-element
  4063. (org-element-map tree 'code 'identity info t) info 2))))))
  4064. (provide 'test-ox)
  4065. ;;; test-org-export.el end here