test-ox.el 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903
  1. ;;; test-ox.el --- Tests for ox.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2016, 2019 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-include/links ()
  1336. "Test links modifications when including files."
  1337. ;; Preserve relative plain links.
  1338. (should
  1339. (string-prefix-p
  1340. "file:org-includee-"
  1341. (let* ((subdir (make-temp-file "org-includee-" t))
  1342. (includee (expand-file-name "includee.org" subdir))
  1343. (includer (make-temp-file "org-includer-")))
  1344. (write-region "file:foo.org" nil includee)
  1345. (write-region (format "#+INCLUDE: %S"
  1346. (file-relative-name includee
  1347. temporary-file-directory))
  1348. nil includer)
  1349. (let ((buffer (find-file-noselect includer t)))
  1350. (unwind-protect
  1351. (with-current-buffer buffer
  1352. (org-export-expand-include-keyword)
  1353. (org-trim (buffer-string)))
  1354. (when (buffer-live-p buffer)
  1355. (with-current-buffer buffer (set-buffer-modified-p nil))
  1356. (kill-buffer buffer))
  1357. (when (file-exists-p subdir) (delete-directory subdir t))
  1358. (when (file-exists-p includer) (delete-file includer)))))))
  1359. ;; Preserve relative angular links.
  1360. (should
  1361. (string-prefix-p
  1362. "<file:org-includee-"
  1363. (let* ((subdir (make-temp-file "org-includee-" t))
  1364. (includee (expand-file-name "includee.org" subdir))
  1365. (includer (make-temp-file "org-includer-")))
  1366. (write-region "<file:foo.org>" nil includee)
  1367. (write-region (format "#+INCLUDE: %S"
  1368. (file-relative-name includee
  1369. temporary-file-directory))
  1370. nil includer)
  1371. (let ((buffer (find-file-noselect includer t)))
  1372. (unwind-protect
  1373. (with-current-buffer buffer
  1374. (org-export-expand-include-keyword)
  1375. (org-trim (buffer-string)))
  1376. (when (buffer-live-p buffer)
  1377. (with-current-buffer buffer (set-buffer-modified-p nil))
  1378. (kill-buffer buffer))
  1379. (when (file-exists-p subdir) (delete-directory subdir t))
  1380. (when (file-exists-p includer) (delete-file includer)))))))
  1381. ;; Preserve relative bracket links without description.
  1382. (should
  1383. (string-prefix-p
  1384. "[[file:org-includee-"
  1385. (let* ((subdir (make-temp-file "org-includee-" t))
  1386. (includee (expand-file-name "includee.org" subdir))
  1387. (includer (make-temp-file "org-includer-")))
  1388. (write-region "[[file:foo.org]]" nil includee)
  1389. (write-region (format "#+INCLUDE: %S"
  1390. (file-relative-name includee
  1391. temporary-file-directory))
  1392. nil includer)
  1393. (let ((buffer (find-file-noselect includer t)))
  1394. (unwind-protect
  1395. (with-current-buffer buffer
  1396. (org-export-expand-include-keyword)
  1397. (org-trim (buffer-string)))
  1398. (when (buffer-live-p buffer)
  1399. (with-current-buffer buffer (set-buffer-modified-p nil))
  1400. (kill-buffer buffer))
  1401. (when (file-exists-p subdir) (delete-directory subdir t))
  1402. (when (file-exists-p includer) (delete-file includer)))))))
  1403. ;; Preserve relative bracket links with description.
  1404. (should
  1405. (string-prefix-p
  1406. "[[file:org-includee-"
  1407. (let* ((subdir (make-temp-file "org-includee-" t))
  1408. (includee (expand-file-name "includee.org" subdir))
  1409. (includer (make-temp-file "org-includer-")))
  1410. (write-region "[[file:foo.org][description]]" nil includee)
  1411. (write-region (format "#+INCLUDE: %S"
  1412. (file-relative-name includee
  1413. temporary-file-directory))
  1414. nil includer)
  1415. (let ((buffer (find-file-noselect includer t)))
  1416. (unwind-protect
  1417. (with-current-buffer buffer
  1418. (org-export-expand-include-keyword)
  1419. (org-trim (buffer-string)))
  1420. (when (buffer-live-p buffer)
  1421. (with-current-buffer buffer (set-buffer-modified-p nil))
  1422. (kill-buffer buffer))
  1423. (when (file-exists-p subdir) (delete-directory subdir t))
  1424. (when (file-exists-p includer) (delete-file includer)))))))
  1425. ;; Preserve absolute links.
  1426. (should
  1427. (string=
  1428. "[[file:/foo/bar.org]]"
  1429. (let* ((subdir (make-temp-file "org-includee-" t))
  1430. (includee (expand-file-name "includee.org" subdir))
  1431. (includer (make-temp-file "org-includer-")))
  1432. (write-region "[[file:/foo/bar.org]]" nil includee)
  1433. (write-region (format "#+INCLUDE: %S"
  1434. (file-relative-name includee
  1435. temporary-file-directory))
  1436. nil includer)
  1437. (let ((buffer (find-file-noselect includer t)))
  1438. (unwind-protect
  1439. (with-current-buffer buffer
  1440. (org-export-expand-include-keyword)
  1441. (org-trim (buffer-string)))
  1442. (when (buffer-live-p buffer)
  1443. (with-current-buffer buffer (set-buffer-modified-p nil))
  1444. (kill-buffer buffer))
  1445. (when (file-exists-p subdir) (delete-directory subdir t))
  1446. (when (file-exists-p includer) (delete-file includer)))))))
  1447. ;; Pathological case: Do not error when fixing a path in a headline.
  1448. (should
  1449. (let* ((subdir (make-temp-file "org-includee-" t))
  1450. (includee (expand-file-name "includee.org" subdir))
  1451. (includer (make-temp-file "org-includer-")))
  1452. (write-region "* [[file:foo.org]]" nil includee)
  1453. (write-region (format "#+INCLUDE: %S"
  1454. (file-relative-name includee
  1455. temporary-file-directory))
  1456. nil includer)
  1457. (let ((buffer (find-file-noselect includer t)))
  1458. (unwind-protect
  1459. (with-current-buffer buffer
  1460. (org-export-expand-include-keyword)
  1461. (org-trim (buffer-string)))
  1462. (when (buffer-live-p buffer)
  1463. (with-current-buffer buffer (set-buffer-modified-p nil))
  1464. (kill-buffer buffer))
  1465. (when (file-exists-p subdir) (delete-directory subdir t))
  1466. (when (file-exists-p includer) (delete-file includer)))))))
  1467. (ert-deftest test-org-export/expand-macro ()
  1468. "Test macro expansion in an Org buffer."
  1469. (require 'ox-org)
  1470. ;; Standard macro expansion.
  1471. (should
  1472. (equal "#+macro: macro1 value\nvalue\n"
  1473. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1474. (org-export-as (org-test-default-backend)))))
  1475. ;; Include global macros. However, local macros override them.
  1476. (should
  1477. (equal "global\n"
  1478. (org-test-with-temp-text "{{{M}}}"
  1479. (let ((org-export-global-macros '(("M" . "global"))))
  1480. (org-export-as (org-test-default-backend))))))
  1481. (should
  1482. (equal "global arg\n"
  1483. (org-test-with-temp-text "{{{M(arg)}}}"
  1484. (let ((org-export-global-macros '(("M" . "global $1"))))
  1485. (org-export-as (org-test-default-backend))))))
  1486. (should
  1487. (equal "2\n"
  1488. (org-test-with-temp-text "{{{M}}}"
  1489. (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
  1490. (org-export-as (org-test-default-backend))))))
  1491. (should
  1492. (equal "#+macro: M local\nlocal\n"
  1493. (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
  1494. (let ((org-export-global-macros '(("M" . "global"))))
  1495. (org-export-as (org-test-default-backend))))))
  1496. ;; Allow macro in parsed keywords and associated properties.
  1497. ;; Standard macro expansion.
  1498. (should
  1499. (string-match
  1500. "#\\+k: value"
  1501. (let ((backend (org-export-create-backend
  1502. :parent 'org
  1503. :options '((:k "K" nil nil parse)))))
  1504. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1505. (org-export-as backend)))))
  1506. (should
  1507. (string-match
  1508. ":EXPORT_K: v"
  1509. (let ((backend (org-export-create-backend
  1510. :parent 'org
  1511. :options '((:k "K" nil nil parse)))))
  1512. (org-test-with-temp-text
  1513. "#+macro: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1514. (org-export-as backend nil nil nil '(:with-properties t))))))
  1515. ;; Expand specific macros.
  1516. (should
  1517. (equal "me 2012-03-29 me@here Title\n"
  1518. (org-test-with-temp-text
  1519. "
  1520. #+TITLE: Title
  1521. #+DATE: 2012-03-29
  1522. #+AUTHOR: me
  1523. #+EMAIL: me@here
  1524. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1525. (let ((output (org-export-as (org-test-default-backend))))
  1526. (substring output (string-match ".*\n\\'" output))))))
  1527. ;; Expand specific macros when property contained a regular macro
  1528. ;; already.
  1529. (should
  1530. (equal "value\n"
  1531. (org-test-with-temp-text "
  1532. #+MACRO: macro1 value
  1533. #+TITLE: {{{macro1}}}
  1534. {{{title}}}"
  1535. (let ((output (org-export-as (org-test-default-backend))))
  1536. (substring output (string-match ".*\n\\'" output))))))
  1537. ;; Expand macros with templates in included files.
  1538. (should
  1539. (equal "success\n"
  1540. (org-test-with-temp-text
  1541. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1542. {{{included-macro}}}" org-test-dir)
  1543. (let ((output (org-export-as (org-test-default-backend))))
  1544. (substring output (string-match ".*\n\\'" output))))))
  1545. ;; Date macro takes a optional formatting argument
  1546. (should
  1547. (equal "09-02-15\n"
  1548. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1549. (org-export-as (org-test-default-backend)))))
  1550. ;; Only single timestamps are formatted
  1551. (should
  1552. (equal "<2015-02x-09>\n"
  1553. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1554. (org-export-as (org-test-default-backend)))))
  1555. ;; Throw an error when a macro definition is missing.
  1556. (should-error
  1557. (org-test-with-temp-text "{{{missing}}}"
  1558. (org-export-as (org-test-default-backend))))
  1559. ;; Inline source blocks generate {{{results}}} macros. Evaluate
  1560. ;; those.
  1561. (should
  1562. (equal "=2=\n"
  1563. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  1564. (let ((org-export-use-babel t)
  1565. (org-babel-inline-result-wrap "=%s="))
  1566. (org-export-as (org-test-default-backend))))))
  1567. ;; If inline source block is already associated to a "results"
  1568. ;; macro, do not duplicate it.
  1569. (should
  1570. (equal "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
  1571. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
  1572. (let ((org-export-use-babel t)
  1573. (org-babel-inline-result-wrap "=%s="))
  1574. (org-export-as (org-test-default-backend)))
  1575. (buffer-string)))))
  1576. (ert-deftest test-org-export/before-processing-hook ()
  1577. "Test `org-export-before-processing-hook'."
  1578. (should
  1579. (equal
  1580. "#+macro: mac val\nTest\n"
  1581. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1582. (let ((org-export-before-processing-hook
  1583. '((lambda (backend)
  1584. (while (re-search-forward "{{{" nil t)
  1585. (let ((object (org-element-context)))
  1586. (when (eq (org-element-type object) 'macro)
  1587. (delete-region
  1588. (org-element-property :begin object)
  1589. (org-element-property :end object)))))))))
  1590. (org-export-as (org-test-default-backend)))))))
  1591. (ert-deftest test-org-export/before-parsing-hook ()
  1592. "Test `org-export-before-parsing-hook'."
  1593. (should
  1594. (equal "Body 1\nBody 2\n"
  1595. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1596. (let ((org-export-before-parsing-hook
  1597. '((lambda (backend)
  1598. (goto-char (point-min))
  1599. (while (re-search-forward org-outline-regexp-bol nil t)
  1600. (delete-region
  1601. (point-at-bol) (progn (forward-line) (point))))))))
  1602. (org-export-as (org-test-default-backend)))))))
  1603. ;;; Affiliated Keywords
  1604. (ert-deftest test-org-export/read-attribute ()
  1605. "Test `org-export-read-attribute' specifications."
  1606. ;; Standard test.
  1607. (should
  1608. (equal
  1609. (org-export-read-attribute
  1610. :attr_html
  1611. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1612. (org-element-at-point)))
  1613. '(:a "1" :b "2")))
  1614. ;; Return nil on empty attribute.
  1615. (should-not
  1616. (org-export-read-attribute
  1617. :attr_html
  1618. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1619. ;; Return nil on "nil" string.
  1620. (should
  1621. (equal '(:a nil :b nil)
  1622. (org-export-read-attribute
  1623. :attr_html
  1624. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1625. (org-element-at-point)))))
  1626. ;; Return nil on empty string.
  1627. (should
  1628. (equal '(:a nil :b nil)
  1629. (org-export-read-attribute
  1630. :attr_html
  1631. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1632. (org-element-at-point)))))
  1633. ;; Return empty string when value is "".
  1634. (should
  1635. (equal '(:a "")
  1636. (org-export-read-attribute
  1637. :attr_html
  1638. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1639. (org-element-at-point)))))
  1640. ;; Return \"\" when value is """".
  1641. (should
  1642. (equal '(:a "\"\"")
  1643. (org-export-read-attribute
  1644. :attr_html
  1645. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1646. (org-element-at-point)))))
  1647. ;; Ignore text before first property.
  1648. (should-not
  1649. (member "ignore"
  1650. (org-export-read-attribute
  1651. :attr_html
  1652. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1653. (org-element-at-point))))))
  1654. (ert-deftest test-org-export/get-caption ()
  1655. "Test `org-export-get-caption' specifications."
  1656. ;; Without optional argument, return long caption
  1657. (should
  1658. (equal
  1659. '("l")
  1660. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1661. (org-export-get-caption (org-element-at-point)))))
  1662. ;; With optional argument, return short caption.
  1663. (should
  1664. (equal
  1665. '("s")
  1666. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1667. (org-export-get-caption (org-element-at-point) t))))
  1668. ;; Multiple lines are separated by white spaces.
  1669. (should
  1670. (equal
  1671. '("a" " " "b")
  1672. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1673. (org-export-get-caption (org-element-at-point))))))
  1674. ;;; Back-End Tools
  1675. (ert-deftest test-org-export/define-backend ()
  1676. "Test back-end definition and accessors."
  1677. ;; Translate table.
  1678. (should
  1679. (equal '((headline . my-headline-test))
  1680. (let (org-export-registered-backends)
  1681. (org-export-define-backend 'test '((headline . my-headline-test)))
  1682. (org-export-get-all-transcoders 'test))))
  1683. ;; Filters.
  1684. (should
  1685. (equal '((:filter-headline . my-filter))
  1686. (let (org-export-registered-backends)
  1687. (org-export-define-backend 'test
  1688. '((headline . my-headline-test))
  1689. :filters-alist '((:filter-headline . my-filter)))
  1690. (org-export-backend-filters (org-export-get-backend 'test)))))
  1691. ;; Options.
  1692. (should
  1693. (equal '((:prop value))
  1694. (let (org-export-registered-backends)
  1695. (org-export-define-backend 'test
  1696. '((headline . my-headline-test))
  1697. :options-alist '((:prop value)))
  1698. (org-export-backend-options (org-export-get-backend 'test)))))
  1699. ;; Menu.
  1700. (should
  1701. (equal '(?k "Test Export" test)
  1702. (let (org-export-registered-backends)
  1703. (org-export-define-backend 'test
  1704. '((headline . my-headline-test))
  1705. :menu-entry '(?k "Test Export" test))
  1706. (org-export-backend-menu (org-export-get-backend 'test))))))
  1707. (ert-deftest test-org-export/define-derived-backend ()
  1708. "Test `org-export-define-derived-backend' specifications."
  1709. ;; Error when parent back-end is not defined.
  1710. (should-error
  1711. (let (org-export-registered-backends)
  1712. (org-export-define-derived-backend 'test 'parent)))
  1713. ;; Append translation table to parent's.
  1714. (should
  1715. (equal '((:headline . test) (:headline . parent))
  1716. (let (org-export-registered-backends)
  1717. (org-export-define-backend 'parent '((:headline . parent)))
  1718. (org-export-define-derived-backend 'test 'parent
  1719. :translate-alist '((:headline . test)))
  1720. (org-export-get-all-transcoders 'test))))
  1721. ;; Options defined in the new back have priority over those defined
  1722. ;; in parent.
  1723. (should
  1724. (eq 'test
  1725. (let (org-export-registered-backends)
  1726. (org-export-define-backend 'parent
  1727. '((:headline . parent))
  1728. :options-alist '((:a nil nil 'parent)))
  1729. (org-export-define-derived-backend 'test 'parent
  1730. :options-alist '((:a nil nil 'test)))
  1731. (plist-get (org-export--get-global-options
  1732. (org-export-get-backend 'test))
  1733. :a)))))
  1734. (ert-deftest test-org-export/derived-backend-p ()
  1735. "Test `org-export-derived-backend-p' specifications."
  1736. ;; Non-nil with direct match.
  1737. (should
  1738. (let (org-export-registered-backends)
  1739. (org-export-define-backend 'test '((headline . test)))
  1740. (org-export-derived-backend-p 'test 'test)))
  1741. (should
  1742. (let (org-export-registered-backends)
  1743. (org-export-define-backend 'test '((headline . test)))
  1744. (org-export-define-derived-backend 'test2 'test)
  1745. (org-export-derived-backend-p 'test2 'test2)))
  1746. ;; Non-nil with a direct parent.
  1747. (should
  1748. (let (org-export-registered-backends)
  1749. (org-export-define-backend 'test '((headline . test)))
  1750. (org-export-define-derived-backend 'test2 'test)
  1751. (org-export-derived-backend-p 'test2 'test)))
  1752. ;; Non-nil with an indirect parent.
  1753. (should
  1754. (let (org-export-registered-backends)
  1755. (org-export-define-backend 'test '((headline . test)))
  1756. (org-export-define-derived-backend 'test2 'test)
  1757. (org-export-define-derived-backend 'test3 'test2)
  1758. (org-export-derived-backend-p 'test3 'test)))
  1759. ;; Nil otherwise.
  1760. (should-not
  1761. (let (org-export-registered-backends)
  1762. (org-export-define-backend 'test '((headline . test)))
  1763. (org-export-define-backend 'test2 '((headline . test2)))
  1764. (org-export-derived-backend-p 'test2 'test)))
  1765. (should-not
  1766. (let (org-export-registered-backends)
  1767. (org-export-define-backend 'test '((headline . test)))
  1768. (org-export-define-backend 'test2 '((headline . test2)))
  1769. (org-export-define-derived-backend 'test3 'test2)
  1770. (org-export-derived-backend-p 'test3 'test))))
  1771. (ert-deftest test-org-export/get-all-transcoders ()
  1772. "Test `org-export-get-all-transcoders' specifications."
  1773. ;; Return nil when back-end cannot be found.
  1774. (should-not (org-export-get-all-transcoders nil))
  1775. ;; Same as `org-export-transcoders' if no parent.
  1776. (should
  1777. (equal '((headline . ignore))
  1778. (org-export-get-all-transcoders
  1779. (org-export-create-backend
  1780. :transcoders '((headline . ignore))))))
  1781. ;; But inherit from all ancestors whenever possible.
  1782. (should
  1783. (equal '((section . ignore) (headline . ignore))
  1784. (let (org-export-registered-backends)
  1785. (org-export-define-backend 'b1 '((headline . ignore)))
  1786. (org-export-get-all-transcoders
  1787. (org-export-create-backend
  1788. :parent 'b1 :transcoders '((section . ignore)))))))
  1789. (should
  1790. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1791. (let (org-export-registered-backends)
  1792. (org-export-define-backend 'b1 '((headline . ignore)))
  1793. (org-export-define-derived-backend 'b2 'b1
  1794. :translate-alist '((section . ignore)))
  1795. (org-export-get-all-transcoders
  1796. (org-export-create-backend
  1797. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1798. ;; Back-end transcoders overrule inherited ones.
  1799. (should
  1800. (eq 'b
  1801. (let (org-export-registered-backends)
  1802. (org-export-define-backend 'b1 '((headline . a)))
  1803. (cdr (assq 'headline
  1804. (org-export-get-all-transcoders
  1805. (org-export-create-backend
  1806. :parent 'b1 :transcoders '((headline . b))))))))))
  1807. (ert-deftest test-org-export/get-all-options ()
  1808. "Test `org-export-get-all-options' specifications."
  1809. ;; Return nil when back-end cannot be found.
  1810. (should-not (org-export-get-all-options nil))
  1811. ;; Same as `org-export-options' if no parent.
  1812. (should
  1813. (equal '((headline . ignore))
  1814. (org-export-get-all-options
  1815. (org-export-create-backend
  1816. :options '((headline . ignore))))))
  1817. ;; But inherit from all ancestors whenever possible.
  1818. (should
  1819. (equal '((:key2 value2) (:key1 value1))
  1820. (let (org-export-registered-backends)
  1821. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1822. (org-export-get-all-options
  1823. (org-export-create-backend
  1824. :parent 'b1 :options '((:key2 value2)))))))
  1825. (should
  1826. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1827. (let (org-export-registered-backends)
  1828. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1829. (org-export-define-derived-backend 'b2 'b1
  1830. :options-alist '((:key2 value2)))
  1831. (org-export-get-all-options
  1832. (org-export-create-backend
  1833. :parent 'b2 :options '((:key3 value3)))))))
  1834. ;; Back-end options overrule inherited ones.
  1835. (should
  1836. (eq 'b
  1837. (let (org-export-registered-backends)
  1838. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1839. (cdr (assq :key1
  1840. (org-export-get-all-options
  1841. (org-export-create-backend
  1842. :parent 'b1 :options '((:key1 . b))))))))))
  1843. (ert-deftest test-org-export/get-all-filters ()
  1844. "Test `org-export-get-all-filters' specifications."
  1845. ;; Return nil when back-end cannot be found.
  1846. (should-not (org-export-get-all-filters nil))
  1847. ;; Same as `org-export-filters' if no parent.
  1848. (should
  1849. (equal '((:filter-headline . ignore))
  1850. (org-export-get-all-filters
  1851. (org-export-create-backend
  1852. :filters '((:filter-headline . ignore))))))
  1853. ;; But inherit from all ancestors whenever possible.
  1854. (should
  1855. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1856. (let (org-export-registered-backends)
  1857. (org-export-define-backend 'b1
  1858. nil :filters-alist '((:filter-headline . ignore)))
  1859. (org-export-get-all-filters
  1860. (org-export-create-backend
  1861. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1862. (should
  1863. (equal '((:filter-paragraph . ignore)
  1864. (:filter-section . ignore)
  1865. (:filter-headline . ignore))
  1866. (let (org-export-registered-backends)
  1867. (org-export-define-backend 'b1
  1868. nil :filters-alist '((:filter-headline . ignore)))
  1869. (org-export-define-derived-backend 'b2 'b1
  1870. :filters-alist '((:filter-section . ignore)))
  1871. (org-export-get-all-filters
  1872. (org-export-create-backend
  1873. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1874. ;; Back-end filters overrule inherited ones.
  1875. (should
  1876. (eq 'b
  1877. (let (org-export-registered-backends)
  1878. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1879. (cdr (assq :filter-headline
  1880. (org-export-get-all-filters
  1881. (org-export-create-backend
  1882. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1883. (ert-deftest test-org-export/with-backend ()
  1884. "Test `org-export-with-backend' definition."
  1885. ;; Error when calling an undefined back-end
  1886. (should-error (org-export-with-backend nil "Test"))
  1887. ;; Error when called back-end doesn't have an appropriate
  1888. ;; transcoder.
  1889. (should-error
  1890. (org-export-with-backend
  1891. (org-export-create-backend :transcoders '((headline . ignore)))
  1892. "Test"))
  1893. ;; Otherwise, export using correct transcoder
  1894. (should
  1895. (equal "Success"
  1896. (let (org-export-registered-backends)
  1897. (org-export-define-backend 'test
  1898. '((verbatim . (lambda (text contents info) "Failure"))))
  1899. (org-export-define-backend 'test2
  1900. '((verbatim . (lambda (text contents info) "Success"))))
  1901. (org-export-with-backend 'test2 '(verbatim (:value "=Test="))))))
  1902. ;; Corner case: plain-text transcoders have a different arity.
  1903. (should
  1904. (equal "Success"
  1905. (org-export-with-backend
  1906. (org-export-create-backend
  1907. :transcoders '((plain-text . (lambda (text info) "Success"))))
  1908. "Test")))
  1909. ;; Provide correct back-end if transcoder needs to use recursive
  1910. ;; calls.
  1911. (should
  1912. (equal "Success\n"
  1913. (let ((test-back-end
  1914. (org-export-create-backend
  1915. :transcoders
  1916. (list (cons 'headline
  1917. (lambda (headline contents info)
  1918. (org-export-data
  1919. (org-element-property :title headline)
  1920. info)))
  1921. (cons 'plain-text (lambda (text info) "Success"))))))
  1922. (org-export-string-as
  1923. "* Test"
  1924. (org-export-create-backend
  1925. :transcoders
  1926. (list (cons 'headline
  1927. (lambda (headline contents info)
  1928. (org-export-with-backend
  1929. test-back-end headline contents info))))))))))
  1930. (ert-deftest test-org-export/data-with-backend ()
  1931. "Test `org-export-data-with-backend' specifications."
  1932. ;; Error when calling an undefined back-end.
  1933. (should-error (org-export-data-with-backend nil "nil" nil))
  1934. ;; Otherwise, export data recursively, using correct back-end.
  1935. (should
  1936. (equal
  1937. "Success!"
  1938. (org-export-data-with-backend
  1939. '(bold nil "Test")
  1940. (org-export-create-backend
  1941. :transcoders
  1942. '((plain-text . (lambda (text info) "Success"))
  1943. (bold . (lambda (bold contents info) (concat contents "!")))))
  1944. '(:with-emphasize t)))))
  1945. ;;; Comments
  1946. (ert-deftest test-org-export/comments ()
  1947. "Test comments handling during export.
  1948. In particular, structure of the document mustn't be altered after
  1949. comments removal."
  1950. (should
  1951. (equal "Para1\n\nPara2\n"
  1952. (org-test-with-temp-text "
  1953. Para1
  1954. # Comment
  1955. # Comment
  1956. Para2"
  1957. (org-export-as (org-test-default-backend)))))
  1958. (should
  1959. (equal "Para1\n\nPara2\n"
  1960. (org-test-with-temp-text "
  1961. Para1
  1962. # Comment
  1963. Para2"
  1964. (org-export-as (org-test-default-backend)))))
  1965. (should
  1966. (equal "[fn:1] Para1\n\n\nPara2\n"
  1967. (org-test-with-temp-text "
  1968. \[fn:1] Para1
  1969. # Inside definition
  1970. # Outside definition
  1971. Para2"
  1972. (org-export-as (org-test-default-backend)))))
  1973. (should
  1974. (equal "[fn:1] Para1\n\nPara2\n"
  1975. (org-test-with-temp-text "
  1976. \[fn:1] Para1
  1977. # Inside definition
  1978. # Inside definition
  1979. Para2"
  1980. (org-export-as (org-test-default-backend)))))
  1981. (should
  1982. (equal "[fn:1] Para1\n\nPara2\n"
  1983. (org-test-with-temp-text "
  1984. \[fn:1] Para1
  1985. # Inside definition
  1986. Para2"
  1987. (org-export-as (org-test-default-backend)))))
  1988. (should
  1989. (equal "[fn:1] Para1\n\nPara2\n"
  1990. (org-test-with-temp-text "
  1991. \[fn:1] Para1
  1992. # Inside definition
  1993. Para2"
  1994. (org-export-as (org-test-default-backend)))))
  1995. (should
  1996. (equal "- item 1\n\n- item 2\n"
  1997. (org-test-with-temp-text "
  1998. - item 1
  1999. # Comment
  2000. - item 2"
  2001. (org-export-as (org-test-default-backend))))))
  2002. ;;; Export blocks
  2003. (ert-deftest test-org-export/export-block ()
  2004. "Test export blocks transcoding."
  2005. (should
  2006. (equal "Success!\n"
  2007. (org-test-with-temp-text
  2008. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  2009. (org-export-as
  2010. (org-export-create-backend
  2011. :transcoders '((export-block . (lambda (b _c _i)
  2012. (org-element-property :value b)))
  2013. (section . (lambda (_s c _i) c))))))))
  2014. (should
  2015. (equal "Success!\n"
  2016. (org-test-with-temp-text
  2017. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  2018. (org-export-as
  2019. (org-export-create-backend
  2020. :transcoders
  2021. (list
  2022. (cons 'export-block
  2023. (lambda (b _c _i)
  2024. (and (equal (org-element-property :type b) "BACKEND")
  2025. (org-element-property :value b))))
  2026. (cons 'section (lambda (_s c _i) c)))))))))
  2027. ;;; Export Snippets
  2028. (ert-deftest test-org-export/export-snippet ()
  2029. "Test export snippets transcoding."
  2030. ;; Standard test.
  2031. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  2032. (let ((backend (org-test-default-backend)))
  2033. (setf (org-export-backend-name backend) 'test)
  2034. (setf (org-export-backend-transcoders backend)
  2035. (cons (cons 'export-snippet
  2036. (lambda (snippet contents info)
  2037. (when (eq (org-export-snippet-backend snippet) 'test)
  2038. (org-element-property :value snippet))))
  2039. (org-export-backend-transcoders backend)))
  2040. (let ((org-export-snippet-translation-alist nil))
  2041. (should (equal (org-export-as backend) "A\n")))
  2042. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  2043. (should (equal (org-export-as backend) "AB\n")))))
  2044. ;; Ignored export snippets do not remove any blank.
  2045. (should
  2046. (equal "begin end\n"
  2047. (org-test-with-parsed-data "begin @@test:A@@ end"
  2048. (org-export-data-with-backend
  2049. tree
  2050. (org-export-create-backend
  2051. :transcoders
  2052. '((paragraph . (lambda (paragraph contents info) contents))
  2053. (section . (lambda (section contents info) contents))))
  2054. info)))))
  2055. ;;; Filters
  2056. (ert-deftest test-org-export/filter-apply-functions ()
  2057. "Test `org-export-filter-apply-functions' specifications."
  2058. ;; Functions are applied in order and return values are reduced.
  2059. (should
  2060. (equal "210"
  2061. (org-export-filter-apply-functions
  2062. (list (lambda (value &rest _) (concat "1" value))
  2063. (lambda (value &rest _) (concat "2" value)))
  2064. "0" nil)))
  2065. ;; Functions returning nil are skipped.
  2066. (should
  2067. (equal "20"
  2068. (org-export-filter-apply-functions
  2069. (list #'ignore (lambda (value &rest _) (concat "2" value)))
  2070. "0" nil)))
  2071. ;; If all functions are skipped, return the initial value.
  2072. (should
  2073. (equal "0"
  2074. (org-export-filter-apply-functions (list #'ignore) "0" nil)))
  2075. ;; If any function returns the empty string, final value is the
  2076. ;; empty string.
  2077. (should
  2078. (equal ""
  2079. (org-export-filter-apply-functions
  2080. (list (lambda (value &rest _) "")
  2081. (lambda (value &rest _) (concat "2" value)))
  2082. "0" nil)))
  2083. ;; Any function returning the empty string short-circuits the
  2084. ;; process.
  2085. (should
  2086. (org-export-filter-apply-functions
  2087. (list (lambda (value &rest _) "")
  2088. (lambda (value &rest _) (error "This shouldn't happen")))
  2089. "0" nil)))
  2090. ;;; Footnotes
  2091. (ert-deftest test-org-export/footnote-first-reference-p ()
  2092. "Test `org-export-footnote-first-reference-p' specifications."
  2093. (should
  2094. (equal
  2095. '(t nil)
  2096. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  2097. (let (result)
  2098. (org-export-as
  2099. (org-export-create-backend
  2100. :transcoders
  2101. `(,(cons 'footnote-reference
  2102. (lambda (f c i)
  2103. (push (org-export-footnote-first-reference-p f i)
  2104. result)
  2105. ""))
  2106. (section . (lambda (s c i) c))
  2107. (paragraph . (lambda (p c i) c))))
  2108. nil nil nil '(:with-footnotes t))
  2109. (nreverse result)))))
  2110. ;; Limit check to DATA, when non-nil.
  2111. (should
  2112. (equal
  2113. '(nil t)
  2114. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  2115. (let (result)
  2116. (org-element-map tree 'footnote-reference
  2117. (lambda (ref)
  2118. (push
  2119. (org-export-footnote-first-reference-p
  2120. ref info (org-element-map tree 'headline #'identity info t))
  2121. result))
  2122. info)
  2123. (nreverse result)))))
  2124. (should
  2125. (equal
  2126. '(t nil)
  2127. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  2128. (let (result)
  2129. (org-element-map tree 'footnote-reference
  2130. (lambda (ref)
  2131. (push (org-export-footnote-first-reference-p ref info) result))
  2132. info)
  2133. (nreverse result)))))
  2134. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  2135. ;; in the main body of the document. Otherwise, enter footnote
  2136. ;; definitions when they are encountered.
  2137. (should
  2138. (equal
  2139. '(t nil)
  2140. (org-test-with-temp-text
  2141. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  2142. (let (result)
  2143. (org-export-as
  2144. (org-export-create-backend
  2145. :transcoders
  2146. `(,(cons 'footnote-reference
  2147. (lambda (f c i)
  2148. (when (org-element-lineage f '(drawer))
  2149. (push (org-export-footnote-first-reference-p f i nil)
  2150. result))
  2151. ""))
  2152. (drawer . (lambda (d c i) c))
  2153. (footnote-definition . (lambda (d c i) c))
  2154. (section . (lambda (s c i) c))
  2155. (paragraph . (lambda (p c i) c))))
  2156. nil nil nil '(:with-footnotes t))
  2157. (nreverse result)))))
  2158. (should
  2159. (equal
  2160. '(t t)
  2161. (org-test-with-temp-text
  2162. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  2163. (let (result)
  2164. (org-export-as
  2165. (org-export-create-backend
  2166. :transcoders
  2167. `(,(cons 'footnote-reference
  2168. (lambda (f c i)
  2169. (when (org-element-lineage f '(drawer))
  2170. (push (org-export-footnote-first-reference-p f i nil t)
  2171. result))
  2172. ""))
  2173. (drawer . (lambda (d c i) c))
  2174. (footnote-definition . (lambda (d c i) c))
  2175. (section . (lambda (s c i) c))
  2176. (paragraph . (lambda (p c i) c))))
  2177. nil nil nil '(:with-footnotes t))
  2178. (nreverse result))))))
  2179. (ert-deftest test-org-export/get-footnote-number ()
  2180. "Test `org-export-get-footnote-number' specifications."
  2181. (should
  2182. (equal '(1 2 1)
  2183. (org-test-with-parsed-data
  2184. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  2185. (org-element-map tree 'footnote-reference
  2186. (lambda (ref) (org-export-get-footnote-number ref info))
  2187. info))))
  2188. ;; Anonymous footnotes all get a new number.
  2189. (should
  2190. (equal '(1 2)
  2191. (org-test-with-parsed-data
  2192. "Text[fn::anon1][fn::anon2]"
  2193. (org-element-map tree 'footnote-reference
  2194. (lambda (ref) (org-export-get-footnote-number ref info))
  2195. info))))
  2196. ;; Test nested footnotes order.
  2197. (should
  2198. (equal
  2199. '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
  2200. (org-test-with-parsed-data
  2201. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  2202. (org-element-map tree 'footnote-reference
  2203. (lambda (ref)
  2204. (cons (org-export-get-footnote-number ref info)
  2205. (org-element-property :label ref)))
  2206. info))))
  2207. ;; Limit number to provided DATA, when non-nil.
  2208. (should
  2209. (equal
  2210. '(1)
  2211. (org-test-with-parsed-data
  2212. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2213. (org-element-map tree 'footnote-reference
  2214. (lambda (ref)
  2215. (org-export-get-footnote-number
  2216. ref info (org-element-map tree 'headline #'identity info t)))
  2217. info))))
  2218. (should
  2219. (equal
  2220. '(1 2)
  2221. (org-test-with-parsed-data
  2222. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  2223. (org-element-map tree 'footnote-reference
  2224. (lambda (ref) (org-export-get-footnote-number ref info))
  2225. info))))
  2226. ;; With a non-nil BODY-FIRST optional argument, first check body,
  2227. ;; then footnote definitions.
  2228. (should
  2229. (equal
  2230. '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
  2231. (org-test-with-parsed-data
  2232. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2233. (org-element-map tree 'footnote-reference
  2234. (lambda (ref)
  2235. (cons (org-element-property :label ref)
  2236. (org-export-get-footnote-number ref info nil t)))
  2237. info))))
  2238. (should
  2239. (equal
  2240. '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
  2241. (org-test-with-parsed-data
  2242. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2243. (org-element-map tree 'footnote-reference
  2244. (lambda (ref)
  2245. (cons (org-element-property :label ref)
  2246. (org-export-get-footnote-number ref info nil)))
  2247. info)))))
  2248. (ert-deftest test-org-export/get-footnote-definition ()
  2249. "Test `org-export-get-footnote-definition' specifications."
  2250. ;; Standard test.
  2251. (should
  2252. (equal "A\n"
  2253. (org-element-interpret-data
  2254. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  2255. (org-export-get-footnote-definition
  2256. (org-element-map tree 'footnote-reference #'identity nil t)
  2257. info)))))
  2258. ;; Raise an error if no definition is found.
  2259. (should-error
  2260. (org-test-with-parsed-data "Text[fn:1]"
  2261. (org-export-get-footnote-definition
  2262. (org-element-map tree 'footnote-reference #'identity nil t)
  2263. info)))
  2264. ;; Find inline definitions.
  2265. (should
  2266. (equal "A"
  2267. (org-element-interpret-data
  2268. (org-test-with-parsed-data "Text[fn:1:A]"
  2269. (org-export-get-footnote-definition
  2270. (org-element-map tree 'footnote-reference #'identity nil t)
  2271. info)))))
  2272. ;; Find anonymous definitions.
  2273. (should
  2274. (equal "A"
  2275. (org-element-interpret-data
  2276. (org-test-with-parsed-data "Text[fn::A]"
  2277. (org-export-get-footnote-definition
  2278. (org-element-map tree 'footnote-reference #'identity nil t)
  2279. info)))))
  2280. ;; Find empty definitions.
  2281. (should
  2282. (equal ""
  2283. (org-element-interpret-data
  2284. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  2285. (org-export-get-footnote-definition
  2286. (org-element-map tree 'footnote-reference #'identity nil t)
  2287. info)))))
  2288. (should
  2289. (equal ""
  2290. (org-element-interpret-data
  2291. (org-test-with-parsed-data "Text[fn:1:]"
  2292. (org-export-get-footnote-definition
  2293. (org-element-map tree 'footnote-reference #'identity nil t)
  2294. info)))))
  2295. (should
  2296. (equal ""
  2297. (org-element-interpret-data
  2298. (org-test-with-parsed-data "Text[fn::]"
  2299. (org-export-get-footnote-definition
  2300. (org-element-map tree 'footnote-reference #'identity nil t)
  2301. info))))))
  2302. (ert-deftest test-org-export/collect-footnote-definitions ()
  2303. "Test `org-export-collect-footnote-definitions' specifications."
  2304. (should
  2305. (= 4
  2306. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2307. \[fn:2] B [fn:3] [fn::D].
  2308. \[fn:3] C."
  2309. (length (org-export-collect-footnote-definitions info)))))
  2310. ;; Limit number to provided DATA, when non-nil.
  2311. (should
  2312. (equal
  2313. '((1 "2"))
  2314. (org-test-with-parsed-data
  2315. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2316. (mapcar #'butlast
  2317. (org-export-collect-footnote-definitions
  2318. info (org-element-map tree 'headline #'identity info t))))))
  2319. (should
  2320. (equal
  2321. '((1 "1") (2 "2"))
  2322. (org-test-with-parsed-data
  2323. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2324. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  2325. ;; With optional argument BODY-FIRST, first check body, then
  2326. ;; footnote definitions.
  2327. (should
  2328. (equal '("1" "3" "2" nil)
  2329. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2330. \[fn:2] B [fn:3] [fn::D].
  2331. \[fn:3] C."
  2332. (mapcar (lambda (e) (nth 1 e))
  2333. (org-export-collect-footnote-definitions info nil t)))))
  2334. (should-not
  2335. (equal '("1" "3" "2" nil)
  2336. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2337. \[fn:2] B [fn:3] [fn::D].
  2338. \[fn:3] C."
  2339. (mapcar (lambda (e) (nth 1 e))
  2340. (org-export-collect-footnote-definitions info))))))
  2341. (ert-deftest test-org-export/footnotes ()
  2342. "Miscellaneous tests on footnotes."
  2343. (let ((org-footnote-section nil)
  2344. (org-export-with-footnotes t))
  2345. ;; Read every type of footnote.
  2346. (should
  2347. (equal
  2348. '((1 . "A\n") (2 . "C") (3 . "D"))
  2349. (org-test-with-parsed-data
  2350. "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
  2351. (org-element-map tree 'footnote-reference
  2352. (lambda (ref)
  2353. (let ((def (org-export-get-footnote-definition ref info)))
  2354. (cons (org-export-get-footnote-number ref info)
  2355. (if (eq (org-element-property :type ref) 'inline) (car def)
  2356. (car (org-element-contents
  2357. (car (org-element-contents def))))))))
  2358. info))))
  2359. ;; Export nested footnote in invisible definitions.
  2360. (should
  2361. (= 2
  2362. (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
  2363. (narrow-to-region (point) (line-end-position))
  2364. (catch 'exit
  2365. (org-export-as
  2366. (org-export-create-backend
  2367. :transcoders
  2368. '((section
  2369. .
  2370. (lambda (s c i)
  2371. (throw 'exit (length
  2372. (org-export-collect-footnote-definitions
  2373. i))))))))))))
  2374. ;; Export footnotes defined outside parsing scope.
  2375. (should
  2376. (string-match
  2377. "Out of scope"
  2378. (org-test-with-temp-text "[fn:1] Out of scope
  2379. * Title
  2380. <point>Paragraph[fn:1]"
  2381. (org-export-as (org-test-default-backend) 'subtree))))
  2382. (should
  2383. (string-match
  2384. "Out of scope"
  2385. (org-test-with-temp-text "[fn:1] Out of scope
  2386. * Title
  2387. <point>Paragraph[fn:1]"
  2388. (narrow-to-region (point) (point-max))
  2389. (org-export-as (org-test-default-backend)))))
  2390. ;; Export nested footnotes defined outside parsing scope.
  2391. (should
  2392. (string-match
  2393. "Very out of scope"
  2394. (org-test-with-temp-text "
  2395. \[fn:1] Out of scope[fn:2]
  2396. \[fn:2] Very out of scope
  2397. * Title
  2398. <point>Paragraph[fn:1]"
  2399. (org-export-as (org-test-default-backend) 'subtree))))
  2400. (should
  2401. (string-match
  2402. "Very out of scope"
  2403. (org-test-with-temp-text "
  2404. \[fn:1] Out of scope[fn:2]
  2405. \[fn:2] Very out of scope
  2406. * Title
  2407. <point>Paragraph[fn:1]"
  2408. (narrow-to-region (point) (point-max))
  2409. (org-export-as (org-test-default-backend)))))
  2410. (should
  2411. (string-match
  2412. "D2"
  2413. (org-test-with-temp-text "
  2414. \[fn:1] Out of scope[fn:2:D2]
  2415. * Title
  2416. <point>Paragraph[fn:1]"
  2417. (narrow-to-region (point) (point-max))
  2418. (org-export-as (org-test-default-backend)))))
  2419. ;; Export footnotes in pruned parts of tree.
  2420. (should
  2421. (string-match
  2422. "Definition"
  2423. (let ((org-export-exclude-tags '("noexport")))
  2424. (org-test-with-temp-text
  2425. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  2426. (org-export-as (org-test-default-backend))))))
  2427. (should
  2428. (string-match
  2429. "Definition"
  2430. (let ((org-export-select-tags '("export")))
  2431. (org-test-with-temp-text
  2432. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2433. (org-export-as (org-test-default-backend))))))
  2434. ;; Export nested footnotes in pruned parts of tree.
  2435. (should
  2436. (string-match
  2437. "D2"
  2438. (let ((org-export-exclude-tags '("noexport")))
  2439. (org-test-with-temp-text
  2440. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2441. (org-export-as (org-test-default-backend))))))
  2442. (should
  2443. (string-match
  2444. "D2"
  2445. (let ((org-export-select-tags '("export")))
  2446. (org-test-with-temp-text
  2447. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2448. (org-export-as (org-test-default-backend))))))
  2449. ;; Handle uninterpreted data in pruned footnote definitions.
  2450. (should-not
  2451. (string-match
  2452. "|"
  2453. (let ((org-export-with-tables nil))
  2454. (org-test-with-temp-text
  2455. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2456. (org-export-as (org-test-default-backend))))))
  2457. ;; Footnotes without a definition should throw an error.
  2458. (should-error
  2459. (org-test-with-parsed-data "Text[fn:1]"
  2460. (org-export-get-footnote-definition
  2461. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2462. ;; Footnote section should be ignored in TOC and in headlines
  2463. ;; numbering.
  2464. (should
  2465. (= 1 (let ((org-footnote-section "Footnotes"))
  2466. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2467. (org-export-collect-headlines info))))))
  2468. (should
  2469. (equal '(2)
  2470. (let ((org-footnote-section "Footnotes"))
  2471. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2472. (org-element-map tree 'headline
  2473. (lambda (hl)
  2474. (when (equal (org-element-property :raw-value hl) "H2")
  2475. (org-export-get-headline-number hl info)))
  2476. info t)))))))
  2477. ;;; Headlines and Inlinetasks
  2478. (ert-deftest test-org-export/get-relative-level ()
  2479. "Test `org-export-get-relative-level' specifications."
  2480. ;; Standard test.
  2481. (should
  2482. (equal '(1 2)
  2483. (let ((org-odd-levels-only nil))
  2484. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2485. (org-element-map tree 'headline
  2486. (lambda (h) (org-export-get-relative-level h info))
  2487. info)))))
  2488. ;; Missing levels
  2489. (should
  2490. (equal '(1 3)
  2491. (let ((org-odd-levels-only nil))
  2492. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2493. (org-element-map tree 'headline
  2494. (lambda (h) (org-export-get-relative-level h info))
  2495. info))))))
  2496. (ert-deftest test-org-export/low-level-p ()
  2497. "Test `org-export-low-level-p' specifications."
  2498. (should
  2499. (equal
  2500. '(no yes)
  2501. (let ((org-odd-levels-only nil))
  2502. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2503. (org-element-map tree 'headline
  2504. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2505. (plist-put info :headline-levels 1)))))))
  2506. (ert-deftest test-org-export/get-headline-number ()
  2507. "Test `org-export-get-headline-number' specifications."
  2508. ;; Standard test.
  2509. (should
  2510. (equal
  2511. '((1) (1 1))
  2512. (let ((org-odd-levels-only nil))
  2513. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2514. (org-element-map tree 'headline
  2515. (lambda (h) (org-export-get-headline-number h info))
  2516. info)))))
  2517. ;; Missing levels are replaced with 0.
  2518. (should
  2519. (equal
  2520. '((1) (1 0 1))
  2521. (let ((org-odd-levels-only nil))
  2522. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2523. (org-element-map tree 'headline
  2524. (lambda (h) (org-export-get-headline-number h info))
  2525. info))))))
  2526. (ert-deftest test-org-export/numbered-headline-p ()
  2527. "Test `org-export-numbered-headline-p' specifications."
  2528. ;; If `:section-numbers' is nil, never number headlines.
  2529. (should-not
  2530. (org-test-with-parsed-data "* Headline"
  2531. (org-element-map tree 'headline
  2532. (lambda (h) (org-export-numbered-headline-p h info))
  2533. (plist-put info :section-numbers nil))))
  2534. ;; If `:section-numbers' is a number, only number headlines with
  2535. ;; a level greater that it.
  2536. (should
  2537. (equal
  2538. '(yes no)
  2539. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2540. (org-element-map tree 'headline
  2541. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2542. (plist-put info :section-numbers 1)))))
  2543. ;; Otherwise, headlines are always numbered.
  2544. (should
  2545. (org-test-with-parsed-data "* Headline"
  2546. (org-element-map tree 'headline
  2547. (lambda (h) (org-export-numbered-headline-p h info))
  2548. (plist-put info :section-numbers t))))
  2549. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2550. (should-not
  2551. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2552. (org-export-numbered-headline-p
  2553. (org-element-map tree 'headline 'identity info t)
  2554. info)))
  2555. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2556. (should-not
  2557. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2558. (org-export-numbered-headline-p
  2559. (org-element-map tree 'headline #'identity info t)
  2560. info)))
  2561. ;; UNNUMBERED is inherited.
  2562. (should
  2563. (equal '(unnumbered numbered unnumbered)
  2564. (org-test-with-parsed-data
  2565. "* H
  2566. :PROPERTIES:
  2567. :UNNUMBERED: t
  2568. :END:
  2569. ** H2
  2570. :PROPERTIES:
  2571. :UNNUMBERED: nil
  2572. :END:
  2573. ** H3"
  2574. (org-element-map tree 'headline
  2575. (lambda (h)
  2576. (if (org-export-numbered-headline-p h info) 'numbered
  2577. 'unnumbered))
  2578. info)))))
  2579. (ert-deftest test-org-export/number-to-roman ()
  2580. "Test `org-export-number-to-roman' specifications."
  2581. ;; If number is negative, return it as a string.
  2582. (should (equal (org-export-number-to-roman -1) "-1"))
  2583. ;; Otherwise, return it as a roman number.
  2584. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2585. (ert-deftest test-org-export/get-optional-title ()
  2586. "Test `org-export-get-alt-title' specifications."
  2587. ;; If ALT_TITLE property is defined, use it.
  2588. (should
  2589. (equal '("opt")
  2590. (org-test-with-parsed-data
  2591. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2592. (org-export-get-alt-title
  2593. (org-element-map tree 'headline 'identity info t)
  2594. info))))
  2595. ;; Otherwise, fall-back to regular title.
  2596. (should
  2597. (equal '("Headline")
  2598. (org-test-with-parsed-data "* Headline"
  2599. (org-export-get-alt-title
  2600. (org-element-map tree 'headline 'identity info t)
  2601. info)))))
  2602. (ert-deftest test-org-export/get-tags ()
  2603. "Test `org-export-get-tags' specifications."
  2604. ;; Standard test: tags which are not a select tag, an exclude tag,
  2605. ;; or specified as optional argument shouldn't be ignored.
  2606. (should
  2607. (org-test-with-parsed-data "* Headline :tag:"
  2608. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2609. info)))
  2610. ;; Tags provided in the optional argument are ignored.
  2611. (should-not
  2612. (org-test-with-parsed-data "* Headline :ignore:"
  2613. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2614. info '("ignore"))))
  2615. ;; Allow tag inheritance.
  2616. (should
  2617. (equal
  2618. '(("tag") ("tag"))
  2619. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2620. (org-element-map tree 'headline
  2621. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2622. ;; Tag inheritance checks FILETAGS keywords.
  2623. (should
  2624. (equal
  2625. '(("a" "b" "tag"))
  2626. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2627. (org-element-map tree 'headline
  2628. (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
  2629. (ert-deftest test-org-export/get-node-property ()
  2630. "Test`org-export-get-node-property' specifications."
  2631. ;; Standard test.
  2632. (should
  2633. (equal "value"
  2634. (org-test-with-parsed-data "* Headline
  2635. :PROPERTIES:
  2636. :prop: value
  2637. :END:"
  2638. (org-export-get-node-property
  2639. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2640. ;; Test inheritance.
  2641. (should
  2642. (equal "value"
  2643. (org-test-with-parsed-data "* Parent
  2644. :PROPERTIES:
  2645. :prop: value
  2646. :END:
  2647. ** Headline
  2648. Paragraph"
  2649. (org-export-get-node-property
  2650. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2651. ;; Cannot return a value before the first headline.
  2652. (should-not
  2653. (org-test-with-parsed-data "Paragraph
  2654. * Headline
  2655. :PROPERTIES:
  2656. :prop: value
  2657. :END:"
  2658. (org-export-get-node-property
  2659. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2660. (ert-deftest test-org-export/get-category ()
  2661. "Test `org-export-get-category' specifications."
  2662. ;; Standard test.
  2663. (should
  2664. (equal "value"
  2665. (org-test-with-parsed-data "* Headline
  2666. :PROPERTIES:
  2667. :CATEGORY: value
  2668. :END:"
  2669. (org-export-get-category
  2670. (org-element-map tree 'headline 'identity nil t) info))))
  2671. ;; Test inheritance from a parent headline.
  2672. (should
  2673. (equal '("value" "value")
  2674. (org-test-with-parsed-data "* Headline1
  2675. :PROPERTIES:
  2676. :CATEGORY: value
  2677. :END:
  2678. ** Headline2"
  2679. (org-element-map tree 'headline
  2680. (lambda (hl) (org-export-get-category hl info)) info))))
  2681. ;; Test inheritance from #+CATEGORY keyword
  2682. (should
  2683. (equal "value"
  2684. (org-test-with-parsed-data "#+CATEGORY: value
  2685. * Headline"
  2686. (org-export-get-category
  2687. (org-element-map tree 'headline 'identity nil t) info))))
  2688. ;; Test inheritance from file name.
  2689. (should
  2690. (equal "test"
  2691. (org-test-with-parsed-data "* Headline"
  2692. (let ((info (plist-put info :input-file "~/test.org")))
  2693. (org-export-get-category
  2694. (org-element-map tree 'headline 'identity nil t) info)))))
  2695. ;; Fall-back value.
  2696. (should
  2697. (equal "???"
  2698. (org-test-with-parsed-data "* Headline"
  2699. (org-export-get-category
  2700. (org-element-map tree 'headline 'identity nil t) info)))))
  2701. (ert-deftest test-org-export/first-sibling-p ()
  2702. "Test `org-export-first-sibling-p' specifications."
  2703. ;; Standard test.
  2704. (should
  2705. (equal
  2706. '(yes yes no)
  2707. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2708. (org-element-map tree 'headline
  2709. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2710. info))))
  2711. (should
  2712. (equal '(yes no)
  2713. (org-test-with-parsed-data "- item\n\n para"
  2714. (org-element-map tree 'paragraph
  2715. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2716. info))))
  2717. ;; Ignore sections for headlines.
  2718. (should
  2719. (equal '(yes yes)
  2720. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2721. (org-element-map tree 'headline
  2722. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2723. info))))
  2724. ;; Ignore headlines not exported.
  2725. (should
  2726. (equal
  2727. '(yes)
  2728. (let ((org-export-exclude-tags '("ignore")))
  2729. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2730. (org-element-map tree 'headline
  2731. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2732. info))))))
  2733. (ert-deftest test-org-export/last-sibling-p ()
  2734. "Test `org-export-last-sibling-p' specifications."
  2735. ;; Standard test.
  2736. (should
  2737. (equal
  2738. '(yes no yes)
  2739. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2740. (org-element-map tree 'headline
  2741. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2742. info))))
  2743. (should
  2744. (equal '(no yes)
  2745. (org-test-with-parsed-data "- item\n\n para"
  2746. (org-element-map tree 'paragraph
  2747. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2748. info))))
  2749. ;; Ignore headlines not exported.
  2750. (should
  2751. (equal
  2752. '(yes)
  2753. (let ((org-export-exclude-tags '("ignore")))
  2754. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2755. (org-element-map tree 'headline
  2756. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2757. info)))))
  2758. ;; Handle gracefully discontinuous headings.
  2759. (should
  2760. (equal '(yes yes)
  2761. (org-test-with-parsed-data "** S\n* H"
  2762. (org-element-map tree 'headline
  2763. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no)))))))
  2764. (ert-deftest test-org-export/handle-inlinetasks ()
  2765. "Test inlinetask export."
  2766. ;; Inlinetask with an exclude tag.
  2767. (when (featurep 'org-inlinetask)
  2768. (should
  2769. (equal
  2770. ""
  2771. (let ((org-inlinetask-min-level 3)
  2772. org-export-filter-body-functions
  2773. org-export-filter-final-output-functions)
  2774. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2775. (org-export-as (org-test-default-backend)
  2776. nil nil nil '(:exclude-tags ("noexp")))))))
  2777. ;; Inlinetask with an include tag.
  2778. (should
  2779. (equal
  2780. "* H2\n*** Inline :exp:\n"
  2781. (let ((org-inlinetask-min-level 3)
  2782. (org-tags-column 0))
  2783. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2784. (org-export-as (org-test-default-backend)
  2785. nil nil nil '(:select-tags ("exp")))))))
  2786. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2787. (should
  2788. (equal ""
  2789. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2790. (org-inlinetask-min-level 3)
  2791. org-export-filter-body-functions
  2792. org-export-filter-final-output-functions)
  2793. (org-test-with-temp-text "*** TODO Inline"
  2794. (org-export-as (org-test-default-backend)
  2795. nil nil nil '(:with-tasks nil))))))))
  2796. ;;; Keywords
  2797. (ert-deftest test-org-export/get-date ()
  2798. "Test `org-export-get-date' specifications."
  2799. ;; Return a properly formatted string when
  2800. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2801. ;; consists in a single timestamp.
  2802. (should
  2803. (equal "29 03 2012"
  2804. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2805. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2806. (org-export-get-date info)))))
  2807. ;; Return a secondary string otherwise.
  2808. (should-not
  2809. (stringp
  2810. (let ((org-export-date-timestamp-format nil))
  2811. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2812. (org-export-get-date info)))))
  2813. (should
  2814. (equal '("Date")
  2815. (org-test-with-parsed-data "#+DATE: Date"
  2816. (org-export-get-date info))))
  2817. ;; Optional argument has precedence over
  2818. ;; `org-export-date-timestamp-format'.
  2819. (should
  2820. (equal "29 03"
  2821. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2822. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2823. (org-export-get-date info "%d %m"))))))
  2824. ;;; Links
  2825. (ert-deftest test-org-export/custom-protocol-maybe ()
  2826. "Test `org-export-custom-protocol-maybe' specifications."
  2827. (should
  2828. (string-match
  2829. "success"
  2830. (progn
  2831. (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
  2832. (org-export-string-as
  2833. "[[foo:path]]"
  2834. (org-export-create-backend
  2835. :name 'test
  2836. :transcoders
  2837. '((section . (lambda (s c i) c))
  2838. (paragraph . (lambda (p c i) c))
  2839. (link . (lambda (l c i)
  2840. (or (org-export-custom-protocol-maybe l c 'test)
  2841. "failure")))))))))
  2842. (should-not
  2843. (string-match
  2844. "success"
  2845. (progn
  2846. (org-link-set-parameters
  2847. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2848. (org-export-string-as
  2849. "[[foo:path]]"
  2850. (org-export-create-backend
  2851. :name 'no-test
  2852. :transcoders
  2853. '((section . (lambda (s c i) c))
  2854. (paragraph . (lambda (p c i) c))
  2855. (link . (lambda (l c i)
  2856. (or (org-export-custom-protocol-maybe l c 'no-test)
  2857. "failure")))))))))
  2858. ;; Ignore anonymous back-ends.
  2859. (should-not
  2860. (string-match
  2861. "success"
  2862. (progn
  2863. (org-link-set-parameters
  2864. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2865. (org-export-string-as
  2866. "[[foo:path]]"
  2867. (org-export-create-backend
  2868. :transcoders
  2869. '((section . (lambda (s c i) c))
  2870. (paragraph . (lambda (p c i) c))
  2871. (link . (lambda (l c i)
  2872. (or (org-export-custom-protocol-maybe l c nil)
  2873. "failure"))))))))))
  2874. (ert-deftest test-org-export/get-coderef-format ()
  2875. "Test `org-export-get-coderef-format' specifications."
  2876. ;; A link without description returns "%s"
  2877. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2878. "%s"))
  2879. ;; Return "%s" when path is matched within description.
  2880. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2881. "desc %s"))
  2882. ;; Otherwise return description.
  2883. (should (equal (org-export-get-coderef-format "path" "desc")
  2884. "desc")))
  2885. (ert-deftest test-org-export/inline-image-p ()
  2886. "Test `org-export-inline-image-p' specifications."
  2887. (should
  2888. (org-export-inline-image-p
  2889. (org-test-with-temp-text "[[#id]]"
  2890. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2891. '(("custom-id" . "id")))))
  2892. (ert-deftest test-org-export/insert-image-links ()
  2893. "Test `org-export-insert-image-links' specifications."
  2894. (should-not
  2895. (member "file"
  2896. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2897. (org-element-map tree 'link
  2898. (lambda (l) (org-element-property :type l))))))
  2899. (should
  2900. (member "file"
  2901. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2902. (org-element-map (org-export-insert-image-links tree info) 'link
  2903. (lambda (l) (org-element-property :type l))))))
  2904. ;; Properly set `:parent' property when replace contents with image
  2905. ;; link.
  2906. (should
  2907. (memq 'link
  2908. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2909. (org-element-map (org-export-insert-image-links tree info) 'link
  2910. (lambda (l)
  2911. (org-element-type (org-element-property :parent l)))))))
  2912. ;; With optional argument RULES, recognize different links as
  2913. ;; images.
  2914. (should-not
  2915. (member "file"
  2916. (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
  2917. (org-element-map (org-export-insert-image-links tree info) 'link
  2918. (lambda (l) (org-element-property :type l))))))
  2919. (should
  2920. (member "file"
  2921. (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
  2922. (org-element-map
  2923. (org-export-insert-image-links tree info '(("file" . "xxx")))
  2924. 'link
  2925. (lambda (l) (org-element-property :type l))))))
  2926. ;; If an image link was included from another file, make sure to
  2927. ;; shift any relative path accordingly.
  2928. (should
  2929. (string-prefix-p
  2930. "file:org-includee-"
  2931. (let* ((subdir (make-temp-file "org-includee-" t))
  2932. (includee (expand-file-name "includee.org" subdir))
  2933. (includer (make-temp-file "org-includer-")))
  2934. (write-region "file:foo.png" nil includee)
  2935. (write-region (format "#+INCLUDE: %S"
  2936. (file-relative-name includee
  2937. temporary-file-directory))
  2938. nil includer)
  2939. (let ((buffer (find-file-noselect includer t)))
  2940. (unwind-protect
  2941. (with-current-buffer buffer
  2942. (org-export-as
  2943. (org-export-create-backend
  2944. :transcoders
  2945. '((section . (lambda (_s c _i) c))
  2946. (paragraph . (lambda (_p c _i) c))
  2947. (link . (lambda (l c _i) (org-element-link-interpreter l c))))
  2948. :filters
  2949. '((:filter-parse-tree
  2950. (lambda (d _b i) (org-export-insert-image-links d i)))))))
  2951. (when (buffer-live-p buffer)
  2952. (with-current-buffer buffer (set-buffer-modified-p nil))
  2953. (kill-buffer buffer))
  2954. (when (file-exists-p subdir) (delete-directory subdir t))
  2955. (when (file-exists-p includer) (delete-file includer)))))))
  2956. (should
  2957. (string-match-p
  2958. "file:org-includee-.+?foo\\.png"
  2959. (let* ((subdir (make-temp-file "org-includee-" t))
  2960. (includee (expand-file-name "includee.org" subdir))
  2961. (includer (make-temp-file "org-includer-")))
  2962. (write-region "[[https://orgmode.org][file:foo.png]]" nil includee)
  2963. (write-region (format "#+INCLUDE: %S"
  2964. (file-relative-name includee
  2965. temporary-file-directory))
  2966. nil includer)
  2967. (let ((buffer (find-file-noselect includer t)))
  2968. (unwind-protect
  2969. (with-current-buffer buffer
  2970. (org-export-as
  2971. (org-export-create-backend
  2972. :transcoders
  2973. '((section . (lambda (_s c _i) c))
  2974. (paragraph . (lambda (_p c _i) c))
  2975. (link . (lambda (l c _i) (org-element-link-interpreter l c))))
  2976. :filters
  2977. '((:filter-parse-tree
  2978. (lambda (d _b i) (org-export-insert-image-links d i)))))))
  2979. (when (buffer-live-p buffer)
  2980. (with-current-buffer buffer (set-buffer-modified-p nil))
  2981. (kill-buffer buffer))
  2982. (when (file-exists-p subdir) (delete-directory subdir t))
  2983. (when (file-exists-p includer) (delete-file includer)))))))
  2984. (should
  2985. (string-match-p
  2986. "file:org-includee.+?file:org-includee"
  2987. (let* ((subdir (make-temp-file "org-includee-" t))
  2988. (includee (expand-file-name "includee.org" subdir))
  2989. (includer (make-temp-file "org-includer-")))
  2990. (write-region "[[file:bar.png][file:foo.png]]" nil includee)
  2991. (write-region (format "#+INCLUDE: %S"
  2992. (file-relative-name includee
  2993. temporary-file-directory))
  2994. nil includer)
  2995. (let ((buffer (find-file-noselect includer t)))
  2996. (unwind-protect
  2997. (with-current-buffer buffer
  2998. (org-export-as
  2999. (org-export-create-backend
  3000. :transcoders
  3001. '((section . (lambda (_s c _i) c))
  3002. (paragraph . (lambda (_p c _i) c))
  3003. (link . (lambda (l c _i) (org-element-link-interpreter l c))))
  3004. :filters
  3005. '((:filter-parse-tree
  3006. (lambda (d _b i) (org-export-insert-image-links d i)))))))
  3007. (when (buffer-live-p buffer)
  3008. (with-current-buffer buffer (set-buffer-modified-p nil))
  3009. (kill-buffer buffer))
  3010. (when (file-exists-p subdir) (delete-directory subdir t))
  3011. (when (file-exists-p includer) (delete-file includer))))))))
  3012. (ert-deftest test-org-export/fuzzy-link ()
  3013. "Test fuzzy links specifications."
  3014. ;; Link to an headline should return headline's number.
  3015. (should
  3016. ;; Note: Headline's number is in fact a list of numbers.
  3017. (equal '(2)
  3018. (org-test-with-parsed-data
  3019. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  3020. (org-element-map tree 'link
  3021. (lambda (link)
  3022. (org-export-get-ordinal
  3023. (org-export-resolve-fuzzy-link link info) info)) info t))))
  3024. ;; Link to a target in an item should return item's number.
  3025. (should
  3026. ;; Note: Item's number is in fact a list of numbers.
  3027. (equal '(1 2)
  3028. (org-test-with-parsed-data
  3029. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  3030. (org-element-map tree 'link
  3031. (lambda (link)
  3032. (org-export-get-ordinal
  3033. (org-export-resolve-fuzzy-link link info) info)) info t))))
  3034. ;; Link to a target in a footnote should return footnote's number.
  3035. (should
  3036. (equal '(2 3)
  3037. (org-test-with-parsed-data "
  3038. Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
  3039. \[fn:1] A
  3040. \[fn:2] <<test>>B"
  3041. (org-element-map tree 'link
  3042. (lambda (link)
  3043. (org-export-get-ordinal
  3044. (org-export-resolve-fuzzy-link link info) info)) info))))
  3045. ;; Link to a named element should return sequence number of that
  3046. ;; element.
  3047. (should
  3048. (= 2
  3049. (org-test-with-parsed-data
  3050. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  3051. (org-element-map tree 'link
  3052. (lambda (link)
  3053. (org-export-get-ordinal
  3054. (org-export-resolve-fuzzy-link link info) info)) info t))))
  3055. ;; Link to a target not within an item, a table, a footnote
  3056. ;; reference or definition should return section number.
  3057. (should
  3058. (equal '(2)
  3059. (org-test-with-parsed-data
  3060. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  3061. (org-element-map tree 'link
  3062. (lambda (link)
  3063. (org-export-get-ordinal
  3064. (org-export-resolve-fuzzy-link link info) info)) info t))))
  3065. ;; Space are not significant when matching a fuzzy link.
  3066. (should
  3067. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  3068. (org-element-map tree 'link
  3069. (lambda (link) (org-export-resolve-fuzzy-link link info))
  3070. info t)))
  3071. ;; Statistics cookies are ignored for headline match.
  3072. (should
  3073. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  3074. (org-element-map tree 'link
  3075. (lambda (link) (org-export-resolve-fuzzy-link link info))
  3076. info t)))
  3077. (should
  3078. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  3079. (org-element-map tree 'link
  3080. (lambda (link) (org-export-resolve-fuzzy-link link info))
  3081. info t))))
  3082. (defun test-org-gen-loc-list(text type)
  3083. (org-test-with-parsed-data text
  3084. (org-element-map tree type
  3085. (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
  3086. (ert-deftest test-org-export/get-loc ()
  3087. "Test `org-export-get-loc' specifications."
  3088. (should
  3089. ;; "-n" resets line number.
  3090. (equal '(0)
  3091. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
  3092. 'example-block)))
  3093. ;; The first "+n" has 0 lines before it
  3094. (should
  3095. (equal '(0)
  3096. (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
  3097. 'example-block)))
  3098. ;; "-n 10" resets line number but has "9 lines" before it.
  3099. (should
  3100. (equal '(9)
  3101. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
  3102. 'example-block)))
  3103. ;; -n10 with two lines then +n 15
  3104. (should
  3105. (equal '(9 25)
  3106. (test-org-gen-loc-list "
  3107. #+BEGIN_EXAMPLE -n 10
  3108. Text_10
  3109. Second line(11)
  3110. #+END_EXAMPLE
  3111. #+BEGIN_EXAMPLE +n 15
  3112. Text line (11 + 15)
  3113. #+END_EXAMPLE"
  3114. 'example-block)))
  3115. (should
  3116. (equal '(9 19 0)
  3117. (test-org-gen-loc-list "
  3118. #+BEGIN_EXAMPLE -n 10
  3119. Text
  3120. #+END_EXAMPLE
  3121. #+BEGIN_EXAMPLE +n 10
  3122. Text
  3123. #+END_EXAMPLE
  3124. #+BEGIN_EXAMPLE -n
  3125. Text
  3126. #+END_EXAMPLE"
  3127. 'example-block)))
  3128. ;; an Example Block without -n does not add to the line count.
  3129. (should
  3130. (equal '(9 no-loc 19)
  3131. (test-org-gen-loc-list "
  3132. #+BEGIN_EXAMPLE -n 10
  3133. Text
  3134. #+END_EXAMPLE
  3135. #+BEGIN_EXAMPLE
  3136. Text
  3137. #+END_EXAMPLE
  3138. #+BEGIN_EXAMPLE +n 10
  3139. Text
  3140. #+END_EXAMPLE"
  3141. 'example-block)))
  3142. ;; "-n" resets line number.
  3143. (should
  3144. (equal
  3145. '(0)
  3146. (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
  3147. 'src-block)))
  3148. ;; The first "+n" has 0 lines before it.
  3149. (should
  3150. (equal '(0)
  3151. (test-org-gen-loc-list
  3152. "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
  3153. 'src-block)))
  3154. ;; "-n 10" resets line number but has "9 lines" before it.
  3155. (should
  3156. (equal '(9)
  3157. (test-org-gen-loc-list
  3158. "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
  3159. 'src-block)))
  3160. (should
  3161. (equal '(9 25)
  3162. (test-org-gen-loc-list "
  3163. #+BEGIN_SRC emacs-lisp -n 10
  3164. (- 10 1)
  3165. (+ (- 10 1) 1)
  3166. #+END_SRC
  3167. #+BEGIN_SRC emacs-lisp +n 15
  3168. (+ (- 10 1) 2 (- 15 1))
  3169. #+END_SRC"
  3170. 'src-block)))
  3171. (should
  3172. (equal '(9 19 0)
  3173. (test-org-gen-loc-list "
  3174. #+BEGIN_SRC emacs-lisp -n 10
  3175. (- 10 1)
  3176. #+END_SRC
  3177. #+BEGIN_SRC emacs-lisp +n 10
  3178. (+ (- 10 1) 1 (- 10 1))
  3179. #+END_SRC
  3180. #+BEGIN_SRC emacs-lisp -n
  3181. (- 1 1)
  3182. #+END_SRC"
  3183. 'src-block)))
  3184. ;; A SRC Block without -n does not add to the line count.
  3185. (should
  3186. (equal '(9 no-loc 19)
  3187. (test-org-gen-loc-list
  3188. "#+BEGIN_SRC emacs-lisp -n 10
  3189. (+ (-10 1) 1)
  3190. #+END_SRC
  3191. #+BEGIN_SRC emacs-lisp
  3192. (+ 2 2)
  3193. #+END_SRC
  3194. #+BEGIN_SRC emacs-lisp +n 10
  3195. (+ (- 10 1) 1 (- 10 1))
  3196. #+END_SRC"
  3197. 'src-block))))
  3198. (ert-deftest test-org-export/resolve-coderef ()
  3199. "Test `org-export-resolve-coderef' specifications."
  3200. (let ((org-coderef-label-format "(ref:%s)"))
  3201. ;; A link to a "-n -k -r" block returns line number.
  3202. (should
  3203. (= 1
  3204. (org-test-with-parsed-data
  3205. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3206. (org-export-resolve-coderef "coderef" info))))
  3207. (should
  3208. (= 10
  3209. (org-test-with-parsed-data
  3210. "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3211. (org-export-resolve-coderef "coderef" info))))
  3212. (should
  3213. (= 135
  3214. (org-test-with-parsed-data
  3215. "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
  3216. #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3217. (org-export-resolve-coderef "coderef" info))))
  3218. (should
  3219. (= 1
  3220. (org-test-with-parsed-data
  3221. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3222. (org-export-resolve-coderef "coderef" info))))
  3223. (should
  3224. (= 10
  3225. (org-test-with-parsed-data
  3226. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3227. (org-export-resolve-coderef "coderef" info))))
  3228. (should
  3229. (= 135
  3230. (org-test-with-parsed-data
  3231. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
  3232. #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3233. (org-export-resolve-coderef "coderef" info))))
  3234. ;; A link to a "-n -r" block returns line number.
  3235. (should
  3236. (= 1
  3237. (org-test-with-parsed-data
  3238. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3239. (org-export-resolve-coderef "coderef" info))))
  3240. (should
  3241. (= 10
  3242. (org-test-with-parsed-data
  3243. "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3244. (org-export-resolve-coderef "coderef" info))))
  3245. (should
  3246. (= 135
  3247. (org-test-with-parsed-data
  3248. "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
  3249. #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3250. (org-export-resolve-coderef "coderef" info))))
  3251. (should
  3252. (= 1
  3253. (org-test-with-parsed-data
  3254. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3255. (org-export-resolve-coderef "coderef" info))))
  3256. (should
  3257. (= 10
  3258. (org-test-with-parsed-data
  3259. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3260. (org-export-resolve-coderef "coderef" info))))
  3261. (should
  3262. (= 135
  3263. (org-test-with-parsed-data
  3264. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
  3265. #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3266. (org-export-resolve-coderef "coderef" info))))
  3267. ;; A link to a "-n" block returns coderef.
  3268. (should
  3269. (equal "coderef"
  3270. (org-test-with-parsed-data
  3271. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3272. (org-export-resolve-coderef "coderef" info))))
  3273. (should
  3274. (equal "coderef"
  3275. (org-test-with-parsed-data
  3276. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  3277. (org-export-resolve-coderef "coderef" info))))
  3278. ;; A link to a "-r" block returns line number.
  3279. (should
  3280. (= 1
  3281. (org-test-with-parsed-data
  3282. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3283. (org-export-resolve-coderef "coderef" info))))
  3284. (should
  3285. (= 1
  3286. (org-test-with-parsed-data
  3287. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3288. (org-export-resolve-coderef "coderef" info))))
  3289. ;; A link to a block without a switch returns coderef.
  3290. (should
  3291. (equal "coderef"
  3292. (org-test-with-parsed-data
  3293. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3294. (org-export-resolve-coderef "coderef" info))))
  3295. (org-test-with-parsed-data
  3296. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  3297. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  3298. ;; Correctly handle continued line numbers. A "+n" switch should
  3299. ;; resume numbering from previous block with numbered lines,
  3300. ;; ignoring blocks not numbering lines in the process. A "-n"
  3301. ;; switch resets count.
  3302. (should
  3303. (equal '(2 1)
  3304. (org-test-with-parsed-data "
  3305. #+BEGIN_EXAMPLE -n
  3306. Text.
  3307. #+END_EXAMPLE
  3308. #+BEGIN_SRC emacs-lisp
  3309. \(- 1 1)
  3310. #+END_SRC
  3311. #+BEGIN_SRC emacs-lisp +n -r
  3312. \(+ 1 1) (ref:addition)
  3313. #+END_SRC
  3314. #+BEGIN_EXAMPLE -n -r
  3315. Another text. (ref:text)
  3316. #+END_EXAMPLE"
  3317. (list (org-export-resolve-coderef "addition" info)
  3318. (org-export-resolve-coderef "text" info)))))
  3319. ;; Recognize coderef with user-specified syntax.
  3320. (should
  3321. (equal
  3322. "text"
  3323. (org-test-with-parsed-data
  3324. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  3325. (org-export-resolve-coderef "text" info))))
  3326. ;; Unresolved coderefs raise a `org-link-broken' signal.
  3327. (should
  3328. (condition-case nil
  3329. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3330. (org-export-resolve-coderef "unknown" info))
  3331. (org-link-broken t)))))
  3332. (ert-deftest test-org-export/resolve-fuzzy-link ()
  3333. "Test `org-export-resolve-fuzzy-link' specifications."
  3334. ;; Match target objects.
  3335. (should
  3336. (org-test-with-parsed-data "<<target>> [[target]]"
  3337. (org-export-resolve-fuzzy-link
  3338. (org-element-map tree 'link 'identity info t) info)))
  3339. ;; Match named elements.
  3340. (should
  3341. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  3342. (org-export-resolve-fuzzy-link
  3343. (org-element-map tree 'link 'identity info t) info)))
  3344. ;; Match exact headline's name.
  3345. (should
  3346. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  3347. (org-export-resolve-fuzzy-link
  3348. (org-element-map tree 'link 'identity info t) info)))
  3349. ;; Targets objects have priority over headline titles.
  3350. (should
  3351. (eq 'target
  3352. (org-test-with-parsed-data "* target\n<<target>>[[target]]"
  3353. (org-element-type
  3354. (org-export-resolve-fuzzy-link
  3355. (org-element-map tree 'link 'identity info t) info)))))
  3356. ;; Named elements have priority over headline titles.
  3357. (should
  3358. (eq 'paragraph
  3359. (org-test-with-parsed-data
  3360. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  3361. (org-element-type
  3362. (org-export-resolve-fuzzy-link
  3363. (org-element-map tree 'link 'identity info t) info)))))
  3364. ;; If link's path starts with a "*", only match headline titles,
  3365. ;; though.
  3366. (should
  3367. (eq 'headline
  3368. (org-test-with-parsed-data
  3369. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  3370. (org-element-type
  3371. (org-export-resolve-fuzzy-link
  3372. (org-element-map tree 'link 'identity info t) info)))))
  3373. ;; Raise a `org-link-broken' signal if no match.
  3374. (should
  3375. (org-test-with-parsed-data "[[target]]"
  3376. (condition-case nil
  3377. (org-export-resolve-fuzzy-link
  3378. (org-element-map tree 'link #'identity info t) info)
  3379. (org-link-broken t))))
  3380. ;; Match fuzzy link even when before first headline.
  3381. (should
  3382. (eq 'headline
  3383. (org-test-with-parsed-data "[[hl]]\n* hl"
  3384. (org-element-type
  3385. (org-export-resolve-fuzzy-link
  3386. (org-element-map tree 'link 'identity info t) info)))))
  3387. ;; Handle escaped fuzzy links.
  3388. (should
  3389. (org-test-with-parsed-data "* [foo]\n[[[foo\\]]]"
  3390. (org-export-resolve-fuzzy-link
  3391. (org-element-map tree 'link #'identity info t) info))))
  3392. (ert-deftest test-org-export/resolve-id-link ()
  3393. "Test `org-export-resolve-id-link' specifications."
  3394. ;; Regular test for custom-id link.
  3395. (should
  3396. (equal '("Headline1")
  3397. (org-test-with-parsed-data "* Headline1
  3398. :PROPERTIES:
  3399. :CUSTOM_ID: test
  3400. :END:
  3401. * Headline 2
  3402. \[[#test]]"
  3403. (org-element-property
  3404. :title
  3405. (org-export-resolve-id-link
  3406. (org-element-map tree 'link 'identity info t) info)))))
  3407. ;; Raise a `org-link-broken' signal on failing searches.
  3408. (should
  3409. (org-test-with-parsed-data "* Headline1
  3410. :PROPERTIES:
  3411. :CUSTOM_ID: test
  3412. :END:
  3413. * Headline 2
  3414. \[[#no-match]]"
  3415. (condition-case nil
  3416. (org-export-resolve-id-link
  3417. (org-element-map tree 'link #'identity info t) info)
  3418. (org-link-broken t))))
  3419. ;; Test for internal id target.
  3420. (should
  3421. (equal '("Headline1")
  3422. (org-test-with-parsed-data "* Headline1
  3423. :PROPERTIES:
  3424. :ID: aaaa
  3425. :END:
  3426. * Headline 2
  3427. \[[id:aaaa]]"
  3428. (org-element-property
  3429. :title
  3430. (org-export-resolve-id-link
  3431. (org-element-map tree 'link 'identity info t) info)))))
  3432. ;; Test for external id target.
  3433. (should
  3434. (equal
  3435. "external-file"
  3436. (org-test-with-parsed-data "[[id:aaaa]]"
  3437. (org-export-resolve-id-link
  3438. (org-element-map tree 'link 'identity info t)
  3439. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  3440. (ert-deftest test-org-export/resolve-radio-link ()
  3441. "Test `org-export-resolve-radio-link' specifications."
  3442. ;; Standard test.
  3443. (should
  3444. (org-test-with-temp-text "<<<radio>>> radio"
  3445. (org-update-radio-target-regexp)
  3446. (let* ((tree (org-element-parse-buffer))
  3447. (info `(:parse-tree ,tree)))
  3448. (org-export-resolve-radio-link
  3449. (org-element-map tree 'link 'identity info t)
  3450. info))))
  3451. ;; Radio targets are case-insensitive.
  3452. (should
  3453. (org-test-with-temp-text "<<<RADIO>>> radio"
  3454. (org-update-radio-target-regexp)
  3455. (let* ((tree (org-element-parse-buffer))
  3456. (info `(:parse-tree ,tree)))
  3457. (org-export-resolve-radio-link
  3458. (org-element-map tree 'link 'identity info t)
  3459. info))))
  3460. ;; Radio target with objects.
  3461. (should
  3462. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  3463. (org-update-radio-target-regexp)
  3464. (let* ((tree (org-element-parse-buffer))
  3465. (info `(:parse-tree ,tree)))
  3466. (org-export-resolve-radio-link
  3467. (org-element-map tree 'link 'identity info t)
  3468. info))))
  3469. ;; Radio target with objects at its beginning.
  3470. (should
  3471. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  3472. (org-update-radio-target-regexp)
  3473. (let* ((tree (org-element-parse-buffer))
  3474. (info `(:parse-tree ,tree)))
  3475. (org-export-resolve-radio-link
  3476. (org-element-map tree 'link 'identity info t)
  3477. info))))
  3478. ;; Radio link next to an apostrophe.
  3479. (should
  3480. (org-test-with-temp-text "<<<radio>>> radio's"
  3481. (org-update-radio-target-regexp)
  3482. (let* ((tree (org-element-parse-buffer))
  3483. (info `(:parse-tree ,tree)))
  3484. (org-export-resolve-radio-link
  3485. (org-element-map tree 'link 'identity info t)
  3486. info))))
  3487. ;; Multiple radio targets.
  3488. (should
  3489. (equal '("radio1" "radio2")
  3490. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  3491. (org-update-radio-target-regexp)
  3492. (let* ((tree (org-element-parse-buffer))
  3493. (info `(:parse-tree ,tree)))
  3494. (org-element-map tree 'link
  3495. (lambda (link)
  3496. (org-element-property
  3497. :value (org-export-resolve-radio-link link info)))
  3498. info)))))
  3499. ;; Radio target is whitespace insensitive.
  3500. (should
  3501. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  3502. (org-update-radio-target-regexp)
  3503. (let* ((tree (org-element-parse-buffer))
  3504. (info `(:parse-tree ,tree)))
  3505. (org-element-map tree 'link
  3506. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  3507. (ert-deftest test-org-export/file-uri ()
  3508. "Test `org-export-file-uri' specifications."
  3509. ;; Preserve relative filenames.
  3510. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  3511. ;; Local files start with "file://"
  3512. (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
  3513. (org-export-file-uri "/local.org")))
  3514. ;; Remote files start with "file://"
  3515. (should (equal "file://ssh:myself@some.where:papers/last.pdf"
  3516. (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
  3517. (should (equal "file://localhost/etc/fstab"
  3518. (org-export-file-uri "//localhost/etc/fstab")))
  3519. ;; Expand filename starting with "~".
  3520. (should (equal (org-export-file-uri "~/file.org")
  3521. (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
  3522. (ert-deftest test-org-export/get-reference ()
  3523. "Test `org-export-get-reference' specifications."
  3524. (should
  3525. (org-test-with-parsed-data "* Headline"
  3526. (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
  3527. info)))
  3528. ;; For a given element always return the same reference.
  3529. (should
  3530. (org-test-with-parsed-data "* Headline"
  3531. (let ((headline (org-element-map tree 'headline #'identity nil t)))
  3532. (equal (org-export-get-reference headline info)
  3533. (org-export-get-reference headline info)))))
  3534. ;; References get through local export back-ends.
  3535. (should
  3536. (org-test-with-parsed-data "* Headline"
  3537. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3538. (backend
  3539. (org-export-create-backend
  3540. :transcoders
  3541. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3542. (equal (org-trim (org-export-data-with-backend headline backend info))
  3543. (org-export-get-reference headline info)))))
  3544. (should
  3545. (org-test-with-parsed-data "* Headline"
  3546. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3547. (backend
  3548. (org-export-create-backend
  3549. :transcoders
  3550. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3551. (equal (org-export-with-backend backend headline nil info)
  3552. (org-export-get-reference headline info)))))
  3553. ;; Use search cells defined in `:crossrefs'. However, handle
  3554. ;; duplicate search cells.
  3555. (should
  3556. (equal "org0000001"
  3557. (org-test-with-parsed-data "* Headline"
  3558. (let* ((headline (org-element-map tree 'headline #'identity nil t))
  3559. (search-cell (car (org-export-search-cells headline))))
  3560. (setq info
  3561. (plist-put info :crossrefs (list (cons search-cell 1))))
  3562. (org-export-get-reference headline info)))))
  3563. (should-not
  3564. (equal '("org0000001" "org0000001")
  3565. (org-test-with-parsed-data "* H\n** H"
  3566. (org-element-map tree 'headline
  3567. (lambda (h)
  3568. (let* ((search-cell (car (org-export-search-cells h)))
  3569. (info (plist-put info :crossrefs
  3570. (list (cons search-cell 1)))))
  3571. (org-export-get-reference h info))))))))
  3572. ;;; Pseudo objects and pseudo elements
  3573. (ert-deftest test-org-export/pseudo-elements ()
  3574. "Test exporting pseudo-elements."
  3575. ;; Handle blank lines after pseudo-elements. In particular, do not
  3576. ;; replace them with white spaces.
  3577. (should
  3578. (equal "contents\n\nparagraph\n"
  3579. (let ((backend (org-export-create-backend
  3580. :transcoders
  3581. '((pseudo-element . (lambda (_p c _i) c))
  3582. (paragraph . (lambda (_p c _i) c))
  3583. (plain-text . (lambda (c _i) c)))))
  3584. (element '(pseudo-element (:post-blank 1) "contents"))
  3585. (paragraph '(paragraph nil "paragraph"))
  3586. (data '(org-data nil)))
  3587. (org-element-adopt-elements data element paragraph)
  3588. (org-export-data-with-backend data backend nil)))))
  3589. (ert-deftest test-org-export/pseudo-objects ()
  3590. "Test exporting pseudo-objects."
  3591. ;; Handle blank spaces after pseudo-objects. In particular, do not
  3592. ;; replace them with newlines.
  3593. (should
  3594. (equal "begin x end\n"
  3595. (let ((backend (org-export-create-backend
  3596. :transcoders
  3597. '((pseudo-object . (lambda (_p c _i) c))
  3598. (paragraph . (lambda (_p c _i) c))
  3599. (plain-text . (lambda (c _i) c)))))
  3600. (object '(pseudo-object (:post-blank 1) "x"))
  3601. (paragraph '(paragraph nil)))
  3602. (org-element-adopt-elements paragraph "begin " object "end")
  3603. (org-export-data-with-backend paragraph backend nil)))))
  3604. ;;; Src-block and example-block
  3605. (ert-deftest test-org-export/unravel-code ()
  3606. "Test `org-export-unravel-code' function."
  3607. ;; Code without reference.
  3608. (should
  3609. (equal '("(+ 1 1)")
  3610. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3611. (org-export-unravel-code (org-element-at-point)))))
  3612. ;; Code with reference.
  3613. (should
  3614. (equal '("(+ 1 1)" (1 . "test"))
  3615. (org-test-with-temp-text
  3616. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  3617. (let ((org-coderef-label-format "(ref:%s)"))
  3618. (org-export-unravel-code (org-element-at-point))))))
  3619. ;; Code with user-defined reference.
  3620. (should
  3621. (equal
  3622. '("(+ 1 1)" (1 . "test"))
  3623. (org-test-with-temp-text
  3624. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  3625. (let ((org-coderef-label-format "(ref:%s)"))
  3626. (org-export-unravel-code (org-element-at-point))))))
  3627. ;; Code references keys are relative to the current block.
  3628. (should
  3629. (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
  3630. (org-test-with-temp-text "
  3631. #+BEGIN_EXAMPLE -n
  3632. \(+ 1 1)
  3633. #+END_EXAMPLE
  3634. #+BEGIN_EXAMPLE +n
  3635. \(+ 2 2)
  3636. \(+ 3 3) (ref:one)
  3637. #+END_EXAMPLE"
  3638. (goto-line 5)
  3639. (let ((org-coderef-label-format "(ref:%s)"))
  3640. (org-export-unravel-code (org-element-at-point)))))))
  3641. (ert-deftest test-org-export/format-code-default ()
  3642. "Test `org-export-format-code-default' specifications."
  3643. ;; Preserve blank lines, even when code is empty.
  3644. (should
  3645. (equal "\n\n"
  3646. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  3647. (org-export-format-code-default
  3648. (org-element-map tree 'src-block #'identity info t) info))))
  3649. ;; Likewise, preserve leading and trailing blank lines in the code.
  3650. (should
  3651. (equal "\n(+ 1 1)\n"
  3652. (org-test-with-parsed-data
  3653. "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
  3654. (org-export-format-code-default
  3655. (org-element-map tree 'src-block #'identity info t) info))))
  3656. (should
  3657. (equal "(+ 1 1)\n\n"
  3658. (org-test-with-parsed-data
  3659. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
  3660. (org-export-format-code-default
  3661. (org-element-map tree 'src-block #'identity info t) info))))
  3662. ;; Number lines, two whitespace characters before the actual loc.
  3663. (should
  3664. (equal "1 a\n2 b\n"
  3665. (org-test-with-parsed-data
  3666. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  3667. (org-export-format-code-default
  3668. (org-element-map tree 'src-block #'identity info t) info))))
  3669. ;; Numbering includes blank lines.
  3670. (should
  3671. (equal "1 \n2 a\n3 \n4 b\n5 \n"
  3672. (org-test-with-parsed-data
  3673. "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
  3674. (org-export-format-code-default
  3675. (org-element-map tree 'src-block #'identity info t) info))))
  3676. ;; Put references 6 whitespace characters after the widest line,
  3677. ;; wrapped within parenthesis.
  3678. (should
  3679. (equal "123 (a)\n1 (b)\n"
  3680. (let ((org-coderef-label-format "(ref:%s)"))
  3681. (org-test-with-parsed-data
  3682. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  3683. (org-export-format-code-default
  3684. (org-element-map tree 'src-block #'identity info t) info))))))
  3685. ;;; Smart Quotes
  3686. (ert-deftest test-org-export/activate-smart-quotes ()
  3687. "Test `org-export-activate-smart-quotes' specifications."
  3688. ;; Double quotes: standard test.
  3689. (should
  3690. (equal
  3691. '("some &ldquo;quoted&rdquo; text")
  3692. (let ((org-export-default-language "en"))
  3693. (org-test-with-parsed-data "some \"quoted\" text"
  3694. (org-element-map tree 'plain-text
  3695. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3696. info)))))
  3697. ;; Opening quotes: at the beginning of a paragraph.
  3698. (should
  3699. (equal
  3700. '("&ldquo;begin")
  3701. (let ((org-export-default-language "en"))
  3702. (org-test-with-parsed-data "\"begin"
  3703. (org-element-map tree 'plain-text
  3704. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3705. info)))))
  3706. ;; Opening quotes: after an object.
  3707. (should
  3708. (equal
  3709. '("&ldquo;quoted&rdquo; text")
  3710. (let ((org-export-default-language "en"))
  3711. (org-test-with-parsed-data "=verb= \"quoted\" text"
  3712. (org-element-map tree 'plain-text
  3713. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3714. info)))))
  3715. ;; Closing quotes: at the end of a paragraph.
  3716. (should
  3717. (equal
  3718. '("Quoted &ldquo;text&rdquo;")
  3719. (let ((org-export-default-language "en"))
  3720. (org-test-with-parsed-data "Quoted \"text\""
  3721. (org-element-map tree 'plain-text
  3722. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3723. info)))))
  3724. ;; Inner quotes: standard test.
  3725. (should
  3726. (equal '("« outer « inner » outer »")
  3727. (let ((org-export-default-language "fr"))
  3728. (org-test-with-parsed-data "\"outer 'inner' outer\""
  3729. (org-element-map tree 'plain-text
  3730. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3731. info)))))
  3732. ;; Inner quotes: close to special symbols.
  3733. (should
  3734. (equal '("« outer (« inner ») outer »")
  3735. (let ((org-export-default-language "fr"))
  3736. (org-test-with-parsed-data "\"outer ('inner') outer\""
  3737. (org-element-map tree 'plain-text
  3738. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3739. info)))))
  3740. (should
  3741. (equal '("« « inner » »")
  3742. (let ((org-export-default-language "fr"))
  3743. (org-test-with-parsed-data "\"'inner'\""
  3744. (org-element-map tree 'plain-text
  3745. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3746. info)))))
  3747. ;; Apostrophe: standard test.
  3748. (should
  3749. (equal '("It « shouldn’t » fail")
  3750. (let ((org-export-default-language "fr"))
  3751. (org-test-with-parsed-data "It \"shouldn't\" fail"
  3752. (org-element-map tree 'plain-text
  3753. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3754. info)))))
  3755. (should
  3756. (equal '("It shouldn’t fail")
  3757. (let ((org-export-default-language "fr"))
  3758. (org-test-with-parsed-data "It shouldn't fail"
  3759. (org-element-map tree 'plain-text
  3760. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3761. info)))))
  3762. ;; Apostrophe: before an object.
  3763. (should
  3764. (equal
  3765. '("« a’" " »")
  3766. (let ((org-export-default-language "fr"))
  3767. (org-test-with-parsed-data "\"a'=b=\""
  3768. (org-element-map tree 'plain-text
  3769. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3770. info)))))
  3771. ;; Apostrophe: after an object.
  3772. (should
  3773. (equal '("« " "’s »")
  3774. (let ((org-export-default-language "fr"))
  3775. (org-test-with-parsed-data "\"=code='s\""
  3776. (org-element-map tree 'plain-text
  3777. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3778. info)))))
  3779. ;; Special case: isolated quotes.
  3780. (should
  3781. (equal '("&ldquo;" "&rdquo;")
  3782. (let ((org-export-default-language "en"))
  3783. (org-test-with-parsed-data "\"$x$\""
  3784. (org-element-map tree 'plain-text
  3785. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3786. info)))))
  3787. ;; Smart quotes in secondary strings.
  3788. (should
  3789. (equal '("&ldquo;" "&rdquo;")
  3790. (let ((org-export-default-language "en"))
  3791. (org-test-with-parsed-data "* \"$x$\""
  3792. (org-element-map tree 'plain-text
  3793. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3794. info)))))
  3795. ;; Smart quotes in document keywords.
  3796. (should
  3797. (equal '("&ldquo;" "&rdquo;")
  3798. (let ((org-export-default-language "en"))
  3799. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  3800. (org-element-map (plist-get info :title) 'plain-text
  3801. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3802. info)))))
  3803. ;; Smart quotes in parsed affiliated keywords.
  3804. (should
  3805. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  3806. (let ((org-export-default-language "en"))
  3807. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3808. (org-element-map tree 'plain-text
  3809. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3810. info nil nil t)))))
  3811. ;; Smart quotes within objects.
  3812. (should
  3813. (equal '("&ldquo;foo&rdquo;")
  3814. (let ((org-export-default-language "en"))
  3815. (org-test-with-parsed-data "| \"foo\" |"
  3816. (org-element-map tree 'plain-text
  3817. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3818. info nil nil t)))))
  3819. ;; FIXME: Test failing non-interactively.
  3820. ;;
  3821. ;; (should
  3822. ;; (equal '("&ldquo;foo&rdquo;")
  3823. ;; (let ((org-export-default-language "en"))
  3824. ;; (org-test-with-parsed-data "*\"foo\"*"
  3825. ;; (org-element-map tree 'plain-text
  3826. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3827. ;; info nil nil t)))))
  3828. )
  3829. ;;; Tables
  3830. (ert-deftest test-org-export/special-column ()
  3831. "Test if the table's special column is properly recognized."
  3832. ;; 1. First column is special if it contains only a special marking
  3833. ;; characters or empty cells.
  3834. (org-test-with-temp-text "
  3835. | ! | 1 |
  3836. | | 2 |"
  3837. (should
  3838. (org-export-table-has-special-column-p
  3839. (org-element-map
  3840. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3841. ;; 2. If the column contains anything else, it isn't special.
  3842. (org-test-with-temp-text "
  3843. | ! | 1 |
  3844. | b | 2 |"
  3845. (should-not
  3846. (org-export-table-has-special-column-p
  3847. (org-element-map
  3848. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3849. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3850. ;; and "!".
  3851. (org-test-with-temp-text "
  3852. | # | 1 |
  3853. | ^ | 2 |
  3854. | * | 3 |
  3855. | _ | 4 |
  3856. | / | 5 |
  3857. | $ | 6 |
  3858. | ! | 7 |"
  3859. (should
  3860. (org-export-table-has-special-column-p
  3861. (org-element-map
  3862. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3863. ;; 4. A first column with only empty cells isn't considered as
  3864. ;; special.
  3865. (org-test-with-temp-text "
  3866. | | 1 |
  3867. | | 2 |"
  3868. (should-not
  3869. (org-export-table-has-special-column-p
  3870. (org-element-map
  3871. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3872. (ert-deftest test-org-export/table-row-is-special-p ()
  3873. "Test `org-export-table-row-is-special-p' specifications."
  3874. ;; 1. A row is special if it has a special marking character in the
  3875. ;; special column.
  3876. (org-test-with-parsed-data "| ! | 1 |"
  3877. (should
  3878. (org-export-table-row-is-special-p
  3879. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3880. ;; 2. A row is special when its first field is "/"
  3881. (org-test-with-parsed-data "
  3882. | / | 1 |
  3883. | a | b |"
  3884. (should
  3885. (org-export-table-row-is-special-p
  3886. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3887. ;; 3. A row only containing alignment cookies is also considered as
  3888. ;; special.
  3889. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3890. (should
  3891. (org-export-table-row-is-special-p
  3892. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3893. ;; 4. Everything else isn't considered as special.
  3894. (org-test-with-parsed-data "| \alpha | | c |"
  3895. (should-not
  3896. (org-export-table-row-is-special-p
  3897. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3898. ;; 5. Table's rules are never considered as special rows.
  3899. (org-test-with-parsed-data "|---+---|"
  3900. (should-not
  3901. (org-export-table-row-is-special-p
  3902. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3903. (ert-deftest test-org-export/has-header-p ()
  3904. "Test `org-export-table-has-header-p' specifications."
  3905. ;; With an header.
  3906. (should
  3907. (org-test-with-parsed-data "
  3908. | a | b |
  3909. |---+---|
  3910. | c | d |"
  3911. (org-export-table-has-header-p
  3912. (org-element-map tree 'table 'identity info 'first-match)
  3913. info)))
  3914. ;; Without an header.
  3915. (should-not
  3916. (org-test-with-parsed-data "
  3917. | a | b |
  3918. | c | d |"
  3919. (org-export-table-has-header-p
  3920. (org-element-map tree 'table 'identity info 'first-match)
  3921. info)))
  3922. ;; Don't get fooled with starting and ending rules.
  3923. (should-not
  3924. (org-test-with-parsed-data "
  3925. |---+---|
  3926. | a | b |
  3927. | c | d |
  3928. |---+---|"
  3929. (org-export-table-has-header-p
  3930. (org-element-map tree 'table 'identity info 'first-match)
  3931. info))))
  3932. (ert-deftest test-org-export/table-row-group ()
  3933. "Test `org-export-table-row-group' specifications."
  3934. ;; A rule creates a new group.
  3935. (should
  3936. (equal '(1 rule 2)
  3937. (org-test-with-parsed-data "
  3938. | a | b |
  3939. |---+---|
  3940. | 1 | 2 |"
  3941. (org-element-map tree 'table-row
  3942. (lambda (row)
  3943. (if (eq (org-element-property :type row) 'rule) 'rule
  3944. (org-export-table-row-group row info)))))))
  3945. ;; Special rows are ignored in count.
  3946. (should
  3947. (equal
  3948. '(rule 1)
  3949. (org-test-with-parsed-data "
  3950. | / | < | > |
  3951. |---|---+---|
  3952. | | 1 | 2 |"
  3953. (org-element-map tree 'table-row
  3954. (lambda (row)
  3955. (if (eq (org-element-property :type row) 'rule) 'rule
  3956. (org-export-table-row-group row info)))
  3957. info))))
  3958. ;; Double rules also are ignored in count.
  3959. (should
  3960. (equal '(1 rule rule 2)
  3961. (org-test-with-parsed-data "
  3962. | a | b |
  3963. |---+---|
  3964. |---+---|
  3965. | 1 | 2 |"
  3966. (org-element-map tree 'table-row
  3967. (lambda (row)
  3968. (if (eq (org-element-property :type row) 'rule) 'rule
  3969. (org-export-table-row-group row info))))))))
  3970. (ert-deftest test-org-export/table-row-number ()
  3971. "Test `org-export-table-row-number' specifications."
  3972. ;; Standard test. Number is 0-indexed.
  3973. (should
  3974. (equal '(0 1)
  3975. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3976. (org-element-map tree 'table-row
  3977. (lambda (row) (org-export-table-row-number row info)) info))))
  3978. ;; Number ignores separators.
  3979. (should
  3980. (equal '(0 1)
  3981. (org-test-with-parsed-data "
  3982. | a | b | c |
  3983. |---+---+---|
  3984. | d | e | f |"
  3985. (org-element-map tree 'table-row
  3986. (lambda (row) (org-export-table-row-number row info)) info))))
  3987. ;; Number ignores special rows.
  3988. (should
  3989. (equal '(0 1)
  3990. (org-test-with-parsed-data "
  3991. | / | < | > |
  3992. | | b | c |
  3993. |---+-----+-----|
  3994. | | <c> | <c> |
  3995. | | e | f |"
  3996. (org-element-map tree 'table-row
  3997. (lambda (row) (org-export-table-row-number row info)) info)))))
  3998. (ert-deftest test-org-export/table-cell-width ()
  3999. "Test `org-export-table-cell-width' specifications."
  4000. ;; 1. Width is primarily determined by width cookies. If no cookie
  4001. ;; is found, cell's width is nil.
  4002. (org-test-with-parsed-data "
  4003. | / | <l> | <6> | <l7> |
  4004. | | a | b | c |"
  4005. (should
  4006. (equal
  4007. '(nil 6 7)
  4008. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  4009. (org-element-map tree 'table-cell 'identity info)))))
  4010. ;; 2. The last width cookie has precedence.
  4011. (org-test-with-parsed-data "
  4012. | <6> |
  4013. | <7> |
  4014. | a |"
  4015. (should
  4016. (equal
  4017. '(7)
  4018. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  4019. (org-element-map tree 'table-cell 'identity info)))))
  4020. ;; 3. Valid width cookies must have a specific row.
  4021. (org-test-with-parsed-data "| <6> | cell |"
  4022. (should
  4023. (equal
  4024. '(nil nil)
  4025. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  4026. (org-element-map tree 'table-cell 'identity))))))
  4027. (ert-deftest test-org-export/table-cell-alignment ()
  4028. "Test `org-export-table-cell-alignment' specifications."
  4029. ;; 1. Alignment is primarily determined by alignment cookies.
  4030. (should
  4031. (equal '(left center right)
  4032. (let ((org-table-number-fraction 0.5)
  4033. (org-table-number-regexp "^[0-9]+$"))
  4034. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  4035. (mapcar (lambda (cell)
  4036. (org-export-table-cell-alignment cell info))
  4037. (org-element-map tree 'table-cell 'identity))))))
  4038. ;; 2. The last alignment cookie has precedence.
  4039. (should
  4040. (equal '(right right right)
  4041. (org-test-with-parsed-data "
  4042. | <l8> |
  4043. | cell |
  4044. | <r9> |"
  4045. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  4046. (org-element-map tree 'table-cell 'identity)))))
  4047. ;; 3. If there's no cookie, cell's contents determine alignment.
  4048. ;; A column mostly made of cells containing numbers will align
  4049. ;; its cells to the right.
  4050. (should
  4051. (equal '(right right right)
  4052. (let ((org-table-number-fraction 0.5)
  4053. (org-table-number-regexp "^[0-9]+$"))
  4054. (org-test-with-parsed-data "
  4055. | 123 |
  4056. | some text |
  4057. | 12345 |"
  4058. (mapcar (lambda (cell)
  4059. (org-export-table-cell-alignment cell info))
  4060. (org-element-map tree 'table-cell 'identity))))))
  4061. ;; 4. Otherwise, they will be aligned to the left.
  4062. (should
  4063. (equal '(left left left)
  4064. (org-test-with-parsed-data "
  4065. | text |
  4066. | some text |
  4067. | \alpha |"
  4068. (mapcar (lambda (cell)
  4069. (org-export-table-cell-alignment cell info))
  4070. (org-element-map tree 'table-cell 'identity info))))))
  4071. (ert-deftest test-org-export/table-cell-borders ()
  4072. "Test `org-export-table-cell-borders' specifications."
  4073. ;; 1. Recognize various column groups indicators.
  4074. (org-test-with-parsed-data "| / | < | > | <> |"
  4075. (should
  4076. (equal
  4077. '((right bottom top) (left bottom top) (right bottom top)
  4078. (right left bottom top))
  4079. (mapcar (lambda (cell)
  4080. (org-export-table-cell-borders cell info))
  4081. (org-element-map tree 'table-cell 'identity)))))
  4082. ;; 2. Accept shortcuts to define column groups.
  4083. (org-test-with-parsed-data "| / | < | < |"
  4084. (should
  4085. (equal
  4086. '((right bottom top) (right left bottom top) (left bottom top))
  4087. (mapcar (lambda (cell)
  4088. (org-export-table-cell-borders cell info))
  4089. (org-element-map tree 'table-cell 'identity)))))
  4090. ;; 3. A valid column groups row must start with a "/".
  4091. (org-test-with-parsed-data "
  4092. | | < |
  4093. | a | b |"
  4094. (should
  4095. (equal '((top) (top) (bottom) (bottom))
  4096. (mapcar (lambda (cell)
  4097. (org-export-table-cell-borders cell info))
  4098. (org-element-map tree 'table-cell 'identity)))))
  4099. ;; 4. Take table rules into consideration.
  4100. (org-test-with-parsed-data "
  4101. | 1 |
  4102. |---|
  4103. | 2 |"
  4104. (should
  4105. (equal '((below top) (bottom above))
  4106. (mapcar (lambda (cell)
  4107. (org-export-table-cell-borders cell info))
  4108. (org-element-map tree 'table-cell 'identity)))))
  4109. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  4110. ;; (resp. `bottom' and `below') borders. Any special row is
  4111. ;; ignored.
  4112. (org-test-with-parsed-data "
  4113. |---+----|
  4114. | / | |
  4115. | | 1 |
  4116. |---+----|"
  4117. (should
  4118. (equal '((bottom below top above))
  4119. (last
  4120. (mapcar (lambda (cell)
  4121. (org-export-table-cell-borders cell info))
  4122. (org-element-map tree 'table-cell 'identity)))))))
  4123. (ert-deftest test-org-export/table-dimensions ()
  4124. "Test `org-export-table-dimensions' specifications."
  4125. ;; 1. Standard test.
  4126. (org-test-with-parsed-data "
  4127. | 1 | 2 | 3 |
  4128. | 4 | 5 | 6 |"
  4129. (should
  4130. (equal '(2 . 3)
  4131. (org-export-table-dimensions
  4132. (org-element-map tree 'table 'identity info 'first-match) info))))
  4133. ;; 2. Ignore horizontal rules and special columns.
  4134. (org-test-with-parsed-data "
  4135. | / | < | > |
  4136. | 1 | 2 | 3 |
  4137. |---+---+---|
  4138. | 4 | 5 | 6 |"
  4139. (should
  4140. (equal '(2 . 3)
  4141. (org-export-table-dimensions
  4142. (org-element-map tree 'table 'identity info 'first-match) info)))))
  4143. (ert-deftest test-org-export/table-cell-address ()
  4144. "Test `org-export-table-cell-address' specifications."
  4145. ;; 1. Standard test: index is 0-based.
  4146. (org-test-with-parsed-data "| a | b |"
  4147. (should
  4148. (equal '((0 . 0) (0 . 1))
  4149. (org-element-map tree 'table-cell
  4150. (lambda (cell) (org-export-table-cell-address cell info))
  4151. info))))
  4152. ;; 2. Special column isn't counted, nor are special rows.
  4153. (org-test-with-parsed-data "
  4154. | / | <> |
  4155. | | c |"
  4156. (should
  4157. (equal '(0 . 0)
  4158. (org-export-table-cell-address
  4159. (car (last (org-element-map tree 'table-cell 'identity info)))
  4160. info))))
  4161. ;; 3. Tables rules do not count either.
  4162. (org-test-with-parsed-data "
  4163. | a |
  4164. |---|
  4165. | b |
  4166. |---|
  4167. | c |"
  4168. (should
  4169. (equal '(2 . 0)
  4170. (org-export-table-cell-address
  4171. (car (last (org-element-map tree 'table-cell 'identity info)))
  4172. info))))
  4173. ;; 4. Return nil for special cells.
  4174. (org-test-with-parsed-data "| / | a |"
  4175. (should-not
  4176. (org-export-table-cell-address
  4177. (org-element-map tree 'table-cell 'identity nil 'first-match)
  4178. info))))
  4179. (ert-deftest test-org-export/get-table-cell-at ()
  4180. "Test `org-export-get-table-cell-at' specifications."
  4181. ;; 1. Address ignores special columns, special rows and rules.
  4182. (org-test-with-parsed-data "
  4183. | / | <> |
  4184. | | a |
  4185. |---+----|
  4186. | | b |"
  4187. (should
  4188. (equal '("b")
  4189. (org-element-contents
  4190. (org-export-get-table-cell-at
  4191. '(1 . 0)
  4192. (org-element-map tree 'table 'identity info 'first-match)
  4193. info)))))
  4194. ;; 2. Return value for a non-existent address is nil.
  4195. (org-test-with-parsed-data "| a |"
  4196. (should-not
  4197. (org-export-get-table-cell-at
  4198. '(2 . 2)
  4199. (org-element-map tree 'table 'identity info 'first-match)
  4200. info)))
  4201. (org-test-with-parsed-data "| / |"
  4202. (should-not
  4203. (org-export-get-table-cell-at
  4204. '(0 . 0)
  4205. (org-element-map tree 'table 'identity info 'first-match)
  4206. info))))
  4207. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  4208. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  4209. ;; 1. A cell at a beginning of a row always starts a column group.
  4210. (org-test-with-parsed-data "| a |"
  4211. (should
  4212. (org-export-table-cell-starts-colgroup-p
  4213. (org-element-map tree 'table-cell 'identity info 'first-match)
  4214. info)))
  4215. ;; 2. Special column should be ignored when determining the
  4216. ;; beginning of the row.
  4217. (org-test-with-parsed-data "
  4218. | / | |
  4219. | | a |"
  4220. (should
  4221. (org-export-table-cell-starts-colgroup-p
  4222. (org-element-map tree 'table-cell 'identity info 'first-match)
  4223. info)))
  4224. ;; 2. Explicit column groups.
  4225. (org-test-with-parsed-data "
  4226. | / | | < |
  4227. | a | b | c |"
  4228. (should
  4229. (equal
  4230. '(yes no yes)
  4231. (org-element-map tree 'table-cell
  4232. (lambda (cell)
  4233. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  4234. info)))))
  4235. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  4236. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  4237. ;; 1. A cell at the end of a row always ends a column group.
  4238. (org-test-with-parsed-data "| a |"
  4239. (should
  4240. (org-export-table-cell-ends-colgroup-p
  4241. (org-element-map tree 'table-cell 'identity info 'first-match)
  4242. info)))
  4243. ;; 2. Special column should be ignored when determining the
  4244. ;; beginning of the row.
  4245. (org-test-with-parsed-data "
  4246. | / | |
  4247. | | a |"
  4248. (should
  4249. (org-export-table-cell-ends-colgroup-p
  4250. (org-element-map tree 'table-cell 'identity info 'first-match)
  4251. info)))
  4252. ;; 3. Explicit column groups.
  4253. (org-test-with-parsed-data "
  4254. | / | < | |
  4255. | a | b | c |"
  4256. (should
  4257. (equal
  4258. '(yes no yes)
  4259. (org-element-map tree 'table-cell
  4260. (lambda (cell)
  4261. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  4262. info)))))
  4263. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  4264. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  4265. ;; 1. A row at the beginning of a table always starts a row group.
  4266. ;; So does a row following a table rule.
  4267. (org-test-with-parsed-data "
  4268. | a |
  4269. |---|
  4270. | b |"
  4271. (should
  4272. (equal
  4273. '(yes no yes)
  4274. (org-element-map tree 'table-row
  4275. (lambda (row)
  4276. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  4277. info))))
  4278. ;; 2. Special rows should be ignored when determining the beginning
  4279. ;; of the row.
  4280. (org-test-with-parsed-data "
  4281. | / | < |
  4282. | | a |
  4283. |---+---|
  4284. | / | < |
  4285. | | b |"
  4286. (should
  4287. (equal
  4288. '(yes no yes)
  4289. (org-element-map tree 'table-row
  4290. (lambda (row)
  4291. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  4292. info)))))
  4293. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  4294. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  4295. ;; 1. A row at the end of a table always ends a row group. So does
  4296. ;; a row preceding a table rule.
  4297. (org-test-with-parsed-data "
  4298. | a |
  4299. |---|
  4300. | b |"
  4301. (should
  4302. (equal
  4303. '(yes no yes)
  4304. (org-element-map tree 'table-row
  4305. (lambda (row)
  4306. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  4307. info))))
  4308. ;; 2. Special rows should be ignored when determining the beginning
  4309. ;; of the row.
  4310. (org-test-with-parsed-data "
  4311. | | a |
  4312. | / | < |
  4313. |---+---|
  4314. | | b |
  4315. | / | < |"
  4316. (should
  4317. (equal
  4318. '(yes no yes)
  4319. (org-element-map tree 'table-row
  4320. (lambda (row)
  4321. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  4322. info)))))
  4323. (ert-deftest test-org-export/table-row-in-header-p ()
  4324. "Test `org-export-table-row-in-header-p' specifications."
  4325. ;; Standard test. Separators are always nil.
  4326. (should
  4327. (equal
  4328. '(yes no no)
  4329. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  4330. (org-element-map tree 'table-row
  4331. (lambda (row)
  4332. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  4333. ;; Nil when there is no header.
  4334. (should
  4335. (equal
  4336. '(no no)
  4337. (org-test-with-parsed-data "| a |\n| b |"
  4338. (org-element-map tree 'table-row
  4339. (lambda (row)
  4340. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  4341. (ert-deftest test-org-export/table-row-starts-header-p ()
  4342. "Test `org-export-table-row-starts-header-p' specifications."
  4343. ;; 1. Only the row starting the first row group starts the table
  4344. ;; header.
  4345. (org-test-with-parsed-data "
  4346. | a |
  4347. | b |
  4348. |---|
  4349. | c |"
  4350. (should
  4351. (equal
  4352. '(yes no no no)
  4353. (org-element-map tree 'table-row
  4354. (lambda (row)
  4355. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  4356. info))))
  4357. ;; 2. A row cannot start an header if there's no header in the
  4358. ;; table.
  4359. (org-test-with-parsed-data "
  4360. | a |
  4361. |---|"
  4362. (should-not
  4363. (org-export-table-row-starts-header-p
  4364. (org-element-map tree 'table-row 'identity info 'first-match)
  4365. info))))
  4366. (ert-deftest test-org-export/table-row-ends-header-p ()
  4367. "Test `org-export-table-row-ends-header-p' specifications."
  4368. ;; 1. Only the row starting the first row group starts the table
  4369. ;; header.
  4370. (org-test-with-parsed-data "
  4371. | a |
  4372. | b |
  4373. |---|
  4374. | c |"
  4375. (should
  4376. (equal
  4377. '(no yes no no)
  4378. (org-element-map tree 'table-row
  4379. (lambda (row)
  4380. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  4381. info))))
  4382. ;; 2. A row cannot start an header if there's no header in the
  4383. ;; table.
  4384. (org-test-with-parsed-data "
  4385. | a |
  4386. |---|"
  4387. (should-not
  4388. (org-export-table-row-ends-header-p
  4389. (org-element-map tree 'table-row 'identity info 'first-match)
  4390. info))))
  4391. ;;; Tables of Contents
  4392. (ert-deftest test-org-export/collect-headlines ()
  4393. "Test `org-export-collect-headlines' specifications."
  4394. ;; Standard test.
  4395. (should
  4396. (equal '("H1" "H2")
  4397. (org-test-with-parsed-data "* H1\n** H2"
  4398. (mapcar (lambda (h) (org-element-property :raw-value h))
  4399. (org-export-collect-headlines info)))))
  4400. ;; Do not collect headlines below optional argument.
  4401. (should
  4402. (equal '("H1")
  4403. (org-test-with-parsed-data "* H1\n** H2"
  4404. (mapcar (lambda (h) (org-element-property :raw-value h))
  4405. (org-export-collect-headlines info 1)))))
  4406. ;; Never collect headlines below maximum headline level.
  4407. (should
  4408. (equal '("H1")
  4409. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4410. (mapcar (lambda (h) (org-element-property :raw-value h))
  4411. (org-export-collect-headlines info)))))
  4412. (should
  4413. (equal '("H1")
  4414. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4415. (mapcar (lambda (h) (org-element-property :raw-value h))
  4416. (org-export-collect-headlines info 2)))))
  4417. ;; Do not collect footnote section.
  4418. (should
  4419. (equal '("H1")
  4420. (let ((org-footnote-section "Footnotes"))
  4421. (org-test-with-parsed-data "* H1\n** Footnotes"
  4422. (mapcar (lambda (h) (org-element-property :raw-value h))
  4423. (org-export-collect-headlines info))))))
  4424. ;; Do not collect headlines with UNNUMBERED property set to "notoc".
  4425. ;; Headlines with another value for the property are still
  4426. ;; collected. UNNUMBERED property is inherited.
  4427. (should
  4428. (equal '("H1")
  4429. (org-test-with-parsed-data
  4430. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
  4431. (mapcar (lambda (h) (org-element-property :raw-value h))
  4432. (org-export-collect-headlines info)))))
  4433. (should-not
  4434. (org-test-with-parsed-data
  4435. "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
  4436. (mapcar (lambda (h) (org-element-property :raw-value h))
  4437. (org-export-collect-headlines info))))
  4438. (should
  4439. (equal '("H1" "H2")
  4440. (org-test-with-parsed-data
  4441. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  4442. (mapcar (lambda (h) (org-element-property :raw-value h))
  4443. (org-export-collect-headlines info)))))
  4444. ;; Collect headlines locally.
  4445. (should
  4446. (equal '("H2" "H3")
  4447. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  4448. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4449. (mapcar (lambda (h) (org-element-property :raw-value h))
  4450. (org-export-collect-headlines info nil scope))))))
  4451. ;; When collecting locally, optional level is relative.
  4452. (should
  4453. (equal '("H2")
  4454. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  4455. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4456. (mapcar (lambda (h) (org-element-property :raw-value h))
  4457. (org-export-collect-headlines info 1 scope)))))))
  4458. (ert-deftest test-org-export/excluded-from-toc-p ()
  4459. "Test `org-export-excluded-from-toc-p' specifications."
  4460. ;; By default, headlines are not excluded.
  4461. (should-not
  4462. (org-test-with-parsed-data "* H1"
  4463. (org-element-map tree 'headline
  4464. (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  4465. ;; Exclude according to a maximum level.
  4466. (should
  4467. (equal '(in out)
  4468. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4469. (org-element-map tree 'headline
  4470. (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
  4471. info))))
  4472. ;; Exclude according to UNNUMBERED property.
  4473. (should
  4474. (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
  4475. (org-element-map tree 'headline
  4476. (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  4477. ;; UNNUMBERED property is inherited, so is "notoc" value.
  4478. (should
  4479. (equal '(out out)
  4480. (org-test-with-parsed-data
  4481. "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
  4482. (org-element-map tree 'headline
  4483. (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
  4484. info)))))
  4485. (ert-deftest test-org-export/toc-entry-backend ()
  4486. "Test `org-export-toc-entry-backend' specifications."
  4487. ;; Ignore targets.
  4488. (should
  4489. (equal "H \n"
  4490. (org-test-with-temp-text "* H <<target>>"
  4491. (let (org-export-registered-backends)
  4492. (org-export-define-backend 'test
  4493. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4494. (org-element-property :title h)
  4495. (org-export-toc-entry-backend 'test)
  4496. i)))))
  4497. (org-export-as 'test)))))
  4498. ;; Ignore footnote references.
  4499. (should
  4500. (equal "H \n"
  4501. (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
  4502. (let (org-export-registered-backends)
  4503. (org-export-define-backend 'test
  4504. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4505. (org-element-property :title h)
  4506. (org-export-toc-entry-backend 'test)
  4507. i)))))
  4508. (org-export-as 'test)))))
  4509. ;; Replace plain links with contents, or with path.
  4510. (should
  4511. (equal "H Org mode\n"
  4512. (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
  4513. (let (org-export-registered-backends)
  4514. (org-export-define-backend 'test
  4515. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4516. (org-element-property :title h)
  4517. (org-export-toc-entry-backend 'test)
  4518. i)))))
  4519. (org-export-as 'test)))))
  4520. (should
  4521. (equal "H https://orgmode.org\n"
  4522. (org-test-with-temp-text "* H [[https://orgmode.org]]"
  4523. (let (org-export-registered-backends)
  4524. (org-export-define-backend 'test
  4525. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4526. (org-element-property :title h)
  4527. (org-export-toc-entry-backend 'test)
  4528. i)))))
  4529. (org-export-as 'test)))))
  4530. ;; Replace radio targets with contents.
  4531. (should
  4532. (equal "H radio\n"
  4533. (org-test-with-temp-text "* H <<<radio>>>"
  4534. (let (org-export-registered-backends)
  4535. (org-export-define-backend 'test
  4536. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4537. (org-element-property :title h)
  4538. (org-export-toc-entry-backend 'test)
  4539. i)))))
  4540. (org-export-as 'test)))))
  4541. ;; With optional argument TRANSCODERS, specify other
  4542. ;; transformations.
  4543. (should
  4544. (equal "H bold\n"
  4545. (org-test-with-temp-text "* H *bold*"
  4546. (let (org-export-registered-backends)
  4547. (org-export-define-backend 'test
  4548. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4549. (org-element-property :title h)
  4550. (org-export-toc-entry-backend 'test
  4551. '(bold . (lambda (_b c _i) c)))
  4552. i)))))
  4553. (org-export-as 'test))))))
  4554. ;;; Templates
  4555. (ert-deftest test-org-export/inner-template ()
  4556. "Test `inner-template' translator specifications."
  4557. (should
  4558. (equal "Success!"
  4559. (org-test-with-temp-text "* Headline"
  4560. (org-export-as
  4561. (org-export-create-backend
  4562. :transcoders
  4563. '((inner-template . (lambda (contents info) "Success!"))
  4564. (headline . (lambda (h c i) "Headline"))))))))
  4565. ;; Inner template is applied even in a "body-only" export.
  4566. (should
  4567. (equal "Success!"
  4568. (org-test-with-temp-text "* Headline"
  4569. (org-export-as
  4570. (org-export-create-backend
  4571. :transcoders '((inner-template . (lambda (c i) "Success!"))
  4572. (headline . (lambda (h c i) "Headline"))))
  4573. nil nil 'body-only)))))
  4574. (ert-deftest test-org-export/template ()
  4575. "Test `template' translator specifications."
  4576. (should
  4577. (equal "Success!"
  4578. (org-test-with-temp-text "* Headline"
  4579. (org-export-as
  4580. (org-export-create-backend
  4581. :transcoders '((template . (lambda (contents info) "Success!"))
  4582. (headline . (lambda (h c i) "Headline"))))))))
  4583. ;; Template is not applied in a "body-only" export.
  4584. (should-not
  4585. (equal "Success!"
  4586. (org-test-with-temp-text "* Headline"
  4587. (org-export-as
  4588. (org-export-create-backend
  4589. :transcoders '((template . (lambda (contents info) "Success!"))
  4590. (headline . (lambda (h c i) "Headline"))))
  4591. nil nil 'body-only)))))
  4592. ;;; Topology
  4593. (ert-deftest test-org-export/get-next-element ()
  4594. "Test `org-export-get-next-element' specifications."
  4595. ;; Standard test.
  4596. (should
  4597. (equal "b"
  4598. (org-test-with-parsed-data "* Headline\n*a* b"
  4599. (org-export-get-next-element
  4600. (org-element-map tree 'bold 'identity info t) info))))
  4601. ;; Return nil when no previous element.
  4602. (should-not
  4603. (org-test-with-parsed-data "* Headline\na *b*"
  4604. (org-export-get-next-element
  4605. (org-element-map tree 'bold 'identity info t) info)))
  4606. ;; Non-exportable elements are ignored.
  4607. (should-not
  4608. (let ((org-export-with-timestamps nil))
  4609. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  4610. (org-export-get-next-element
  4611. (org-element-map tree 'entity 'identity info t) info))))
  4612. ;; Find next element in secondary strings.
  4613. (should
  4614. (eq 'verbatim
  4615. (org-test-with-parsed-data "* a =verb="
  4616. (org-element-type
  4617. (org-export-get-next-element
  4618. (org-element-map tree 'plain-text 'identity info t) info)))))
  4619. (should
  4620. (eq 'verbatim
  4621. (org-test-with-parsed-data "* /italic/ =verb="
  4622. (org-element-type
  4623. (org-export-get-next-element
  4624. (org-element-map tree 'italic 'identity info t) info)))))
  4625. ;; Find next element in document keywords.
  4626. (should
  4627. (eq 'verbatim
  4628. (org-test-with-parsed-data "#+TITLE: a =verb="
  4629. (org-element-type
  4630. (org-export-get-next-element
  4631. (org-element-map
  4632. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4633. ;; Find next element in parsed affiliated keywords.
  4634. (should
  4635. (eq 'verbatim
  4636. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  4637. (org-element-type
  4638. (org-export-get-next-element
  4639. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4640. ;; With optional argument N, return a list containing all the
  4641. ;; following elements.
  4642. (should
  4643. (equal
  4644. '(bold code underline)
  4645. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4646. (mapcar 'car
  4647. (org-export-get-next-element
  4648. (org-element-map tree 'italic 'identity info t) info t)))))
  4649. ;; When N is a positive integer, return a list containing up to
  4650. ;; N following elements.
  4651. (should
  4652. (equal
  4653. '(bold code)
  4654. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4655. (mapcar 'car
  4656. (org-export-get-next-element
  4657. (org-element-map tree 'italic 'identity info t) info 2))))))
  4658. (ert-deftest test-org-export/get-previous-element ()
  4659. "Test `org-export-get-previous-element' specifications."
  4660. ;; Standard test.
  4661. (should
  4662. (equal "a "
  4663. (org-test-with-parsed-data "* Headline\na *b*"
  4664. (org-export-get-previous-element
  4665. (org-element-map tree 'bold 'identity info t) info))))
  4666. ;; Return nil when no previous element.
  4667. (should-not
  4668. (org-test-with-parsed-data "* Headline\n*a* b"
  4669. (org-export-get-previous-element
  4670. (org-element-map tree 'bold 'identity info t) info)))
  4671. ;; Non-exportable elements are ignored.
  4672. (should-not
  4673. (let ((org-export-with-timestamps nil))
  4674. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  4675. (org-export-get-previous-element
  4676. (org-element-map tree 'entity 'identity info t) info))))
  4677. ;; Find previous element in secondary strings.
  4678. (should
  4679. (eq 'verbatim
  4680. (org-test-with-parsed-data "* =verb= a"
  4681. (org-element-type
  4682. (org-export-get-previous-element
  4683. (org-element-map tree 'plain-text 'identity info t) info)))))
  4684. (should
  4685. (eq 'verbatim
  4686. (org-test-with-parsed-data "* =verb= /italic/"
  4687. (org-element-type
  4688. (org-export-get-previous-element
  4689. (org-element-map tree 'italic 'identity info t) info)))))
  4690. ;; Find previous element in document keywords.
  4691. (should
  4692. (eq 'verbatim
  4693. (org-test-with-parsed-data "#+TITLE: =verb= a"
  4694. (org-element-type
  4695. (org-export-get-previous-element
  4696. (org-element-map
  4697. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4698. ;; Find previous element in parsed affiliated keywords.
  4699. (should
  4700. (eq 'verbatim
  4701. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  4702. (org-element-type
  4703. (org-export-get-previous-element
  4704. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4705. ;; With optional argument N, return a list containing up to
  4706. ;; N previous elements.
  4707. (should
  4708. (equal '(underline italic bold)
  4709. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4710. (mapcar 'car
  4711. (org-export-get-previous-element
  4712. (org-element-map tree 'code 'identity info t) info t)))))
  4713. ;; When N is a positive integer, return a list containing up to
  4714. ;; N previous elements.
  4715. (should
  4716. (equal '(italic bold)
  4717. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4718. (mapcar 'car
  4719. (org-export-get-previous-element
  4720. (org-element-map tree 'code 'identity info t) info 2))))))
  4721. (provide 'test-ox)
  4722. ;;; test-org-export.el end here