test-ox.el 129 KB

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