test-ox.el 126 KB

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