test-ox.el 145 KB

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