test-ox.el 164 KB

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