test-ox.el 112 KB

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