test-ox.el 165 KB

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