test-ox.el 112 KB

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