test-ox.el 169 KB

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