test-ox.el 122 KB

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