test-ox.el 153 KB

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