test-ox.el 152 KB

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