test-ox.el 140 KB

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