test-ox.el 169 KB

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