test-ox.el 142 KB

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