test-ox.el 151 KB

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