test-ox.el 121 KB

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