test-ox.el 168 KB

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