test-ox.el 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  1. ;;; test-ox.el --- Tests for ox.el
  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. (unless (featurep 'ox)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defun org-test-default-backend ()
  19. "Return a default export back-end.
  20. This back-end simply returns parsed data as Org syntax."
  21. (org-export-create-backend
  22. :transcoders (let (transcode-table)
  23. (dolist (type (append org-element-all-elements
  24. org-element-all-objects)
  25. transcode-table)
  26. (push
  27. (cons type
  28. (lambda (obj contents info)
  29. (funcall
  30. (intern (format "org-element-%s-interpreter"
  31. type))
  32. obj contents)))
  33. transcode-table)))))
  34. (defmacro org-test-with-parsed-data (data &rest body)
  35. "Execute body with parsed data available.
  36. DATA is a string containing the data to be parsed. BODY is the
  37. body to execute. Parse tree is available under the `tree'
  38. variable, and communication channel under `info'."
  39. (declare (debug (form body)) (indent 1))
  40. `(org-test-with-temp-text ,data
  41. (org-export--delete-comments)
  42. (let* ((tree (org-element-parse-buffer))
  43. (info (org-export-get-environment)))
  44. (org-export--prune-tree tree info)
  45. (org-export--remove-uninterpreted-data tree info)
  46. (let ((info (org-combine-plists
  47. info (org-export-collect-tree-properties tree info))))
  48. ,@body))))
  49. ;;; Internal Tests
  50. (ert-deftest test-org-export/bind-keyword ()
  51. "Test reading #+BIND: keywords."
  52. ;; Test with `org-export-allow-bind-keywords' set to t.
  53. (should
  54. (org-test-with-temp-text "#+BIND: test-ox-var value"
  55. (let ((org-export-allow-bind-keywords t))
  56. (org-export-get-environment)
  57. (eq test-ox-var 'value))))
  58. ;; Test with `org-export-allow-bind-keywords' set to nil.
  59. (should-not
  60. (org-test-with-temp-text "#+BIND: test-ox-var value"
  61. (let ((org-export-allow-bind-keywords nil))
  62. (org-export-get-environment)
  63. (boundp 'test-ox-var))))
  64. ;; BIND keywords are case-insensitive.
  65. (should
  66. (org-test-with-temp-text "#+bind: test-ox-var value"
  67. (let ((org-export-allow-bind-keywords t))
  68. (org-export-get-environment)
  69. (eq test-ox-var 'value))))
  70. ;; Preserve order of BIND keywords.
  71. (should
  72. (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
  73. (let ((org-export-allow-bind-keywords t))
  74. (org-export-get-environment)
  75. (eq test-ox-var 2))))
  76. ;; Read BIND keywords in setup files.
  77. (should
  78. (org-test-with-temp-text
  79. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  80. (let ((org-export-allow-bind-keywords t))
  81. (org-export-get-environment)
  82. (eq variable 'value))))
  83. ;; Verify that bound variables are seen during export.
  84. (should
  85. (equal "Yes\n"
  86. (org-test-with-temp-text "#+BIND: test-ox-var value"
  87. (let ((org-export-allow-bind-keywords t))
  88. (org-export-as
  89. (org-export-create-backend
  90. :transcoders
  91. '((section . (lambda (s c i)
  92. (if (eq test-ox-var 'value) "Yes" "No")))))))))))
  93. (ert-deftest test-org-export/parse-option-keyword ()
  94. "Test reading all standard #+OPTIONS: items."
  95. (should
  96. (equal
  97. (org-export--parse-option-keyword
  98. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  99. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  100. stat:t title:t")
  101. '(:headline-levels
  102. 1 :section-numbers t :preserve-breaks t :time-stamp-file t
  103. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  104. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  105. :with-footnotes t :with-priority t :with-special-strings t
  106. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  107. :with-tasks t :with-timestamps t :with-todo-keywords t
  108. :with-inlinetasks nil :with-statistics-cookies t :with-title t)))
  109. ;; Test some special values.
  110. (should
  111. (equal
  112. (org-export--parse-option-keyword
  113. "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  114. '(:with-archived-trees
  115. headline :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  116. :with-tags not-in-toc :with-tasks todo :section-numbers 2
  117. :with-timestamps active)))
  118. ;; Test back-end specific values.
  119. (should
  120. (equal
  121. (org-export--parse-option-keyword
  122. "opt:t" (org-export-create-backend :options '((:option nil "opt"))))
  123. '(:option t))))
  124. (ert-deftest test-org-export/get-inbuffer-options ()
  125. "Test reading all standard export keywords."
  126. ;; Properties should follow buffer order.
  127. (should
  128. (equal
  129. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  130. (org-export--get-inbuffer-options))
  131. '(:language "fr" :creator "Me" :email "email")))
  132. ;; Test `space' behaviour.
  133. (should
  134. (equal
  135. (let ((back-end (org-export-create-backend
  136. :options '((:keyword "KEYWORD" nil nil space)))))
  137. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
  138. (org-export--get-inbuffer-options back-end)))
  139. '(:keyword "With spaces")))
  140. ;; Test `newline' behaviour.
  141. (should
  142. (equal
  143. (let ((back-end (org-export-create-backend
  144. :options '((:keyword "KEYWORD" nil nil newline)))))
  145. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
  146. (org-export--get-inbuffer-options back-end)))
  147. '(:keyword "With\ntwo lines")))
  148. ;; Test `split' behaviour.
  149. (should
  150. (equal
  151. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  152. (org-export--get-inbuffer-options))
  153. '(:select-tags ("a" "b"))))
  154. ;; Test `parse' behaviour. `parse' implies `space' but preserve
  155. ;; line breaks. Multi-line objects are allowed.
  156. (should
  157. (org-element-map
  158. (org-test-with-temp-text "#+TITLE: *bold*"
  159. (plist-get (org-export--get-inbuffer-options) :title))
  160. 'bold #'identity nil t))
  161. (should
  162. (equal
  163. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  164. (plist-get (org-export--get-inbuffer-options) :title))
  165. '("Some title with spaces")))
  166. (should
  167. (org-element-map
  168. (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
  169. (plist-get (org-export--get-inbuffer-options) :title))
  170. 'line-break #'identity nil t))
  171. (should
  172. (org-element-map
  173. (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
  174. (plist-get (org-export--get-inbuffer-options) :title))
  175. 'bold #'identity nil t))
  176. ;; Options set through SETUPFILE.
  177. (should
  178. (equal
  179. (org-test-with-temp-text
  180. (format "#+DESCRIPTION: l1
  181. #+LANGUAGE: es
  182. #+SELECT_TAGS: a
  183. #+TITLE: a
  184. #+SETUPFILE: \"%s/examples/setupfile.org\"
  185. #+LANGUAGE: fr
  186. #+SELECT_TAGS: c
  187. #+TITLE: c"
  188. org-test-dir)
  189. (org-export--get-inbuffer-options))
  190. '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
  191. ;; More than one property can refer to the same buffer keyword.
  192. (should
  193. (equal '(:k2 "value" :k1 "value")
  194. (let ((backend (org-export-create-backend
  195. :options '((:k1 "KEYWORD")
  196. (:k2 "KEYWORD")))))
  197. (org-test-with-temp-text "#+KEYWORD: value"
  198. (org-export--get-inbuffer-options backend)))))
  199. ;; Keywords in commented subtrees are ignored.
  200. (should-not
  201. (equal "Me"
  202. (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
  203. (plist-get info :author))))
  204. (should-not
  205. (equal "Mine"
  206. (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
  207. (plist-get info :email)))))
  208. (ert-deftest test-org-export/get-subtree-options ()
  209. "Test setting options from headline's properties."
  210. ;; EXPORT_TITLE.
  211. (should
  212. (equal '("Subtree Title")
  213. (org-test-with-temp-text "#+TITLE: Title
  214. * Headline<point>
  215. :PROPERTIES:
  216. :EXPORT_TITLE: Subtree Title
  217. :END:
  218. Paragraph"
  219. (plist-get (org-export-get-environment nil t) :title))))
  220. ;; EXPORT_OPTIONS.
  221. (should
  222. (= 2
  223. (org-test-with-temp-text "#+OPTIONS: H:1
  224. * Headline<point>
  225. :PROPERTIES:
  226. :EXPORT_OPTIONS: H:2
  227. :END:
  228. Paragraph"
  229. (plist-get (org-export-get-environment nil t) :headline-levels))))
  230. ;; EXPORT_DATE.
  231. (should
  232. (equal '("29-03-2012")
  233. (org-test-with-temp-text "#+DATE: today
  234. * Headline<point>
  235. :PROPERTIES:
  236. :EXPORT_DATE: 29-03-2012
  237. :END:
  238. Paragraph"
  239. (plist-get (org-export-get-environment nil t) :date))))
  240. ;; Properties with `split' behaviour are stored as a list of
  241. ;; strings.
  242. (should
  243. (equal '("a" "b")
  244. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  245. * Headline<point>
  246. :PROPERTIES:
  247. :EXPORT_EXCLUDE_TAGS: a b
  248. :END:
  249. Paragraph"
  250. (plist-get (org-export-get-environment nil t) :exclude-tags))))
  251. ;; Handle :PROPERTY+: syntax.
  252. (should
  253. (equal '("a" "b")
  254. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  255. * Headline<point>
  256. :PROPERTIES:
  257. :EXPORT_EXCLUDE_TAGS: a
  258. :EXPORT_EXCLUDE_TAGS+: b
  259. :END:
  260. Paragraph"
  261. (plist-get (org-export-get-environment nil t) :exclude-tags))))
  262. ;; Export properties are case-insensitive.
  263. (should
  264. (equal '("29-03-2012")
  265. (org-test-with-temp-text "* Headline
  266. :PROPERTIES:
  267. :EXPORT_Date: 29-03-2012
  268. :END:
  269. Paragraph"
  270. (plist-get (org-export-get-environment nil t) :date))))
  271. ;; Still grab correct options when section above is empty.
  272. (should
  273. (equal '("H1")
  274. (org-test-with-temp-text "* H1\n** H11\n** H12<point>"
  275. (plist-get (org-export-get-environment nil t) :title))))
  276. ;; More than one property can refer to the same node property.
  277. (should
  278. (equal '("1" "1")
  279. (org-test-with-temp-text
  280. "* H\n:PROPERTIES:\n:EXPORT_A: 1\n:END:\n<point>"
  281. (let* ((backend (org-export-create-backend
  282. :options '((:k1 "A")
  283. (:k2 "A"))))
  284. (options (org-export-get-environment backend t)))
  285. (list (plist-get options :k1) (plist-get options :k2)))))))
  286. (ert-deftest test-org-export/set-title ()
  287. "Test title setting."
  288. ;; Without TITLE keyword.
  289. (should
  290. (equal
  291. ""
  292. (let (org-export-filter-body-functions
  293. org-export-filter-final-output-functions)
  294. (org-test-with-temp-text "Test"
  295. (org-export-as
  296. (org-export-create-backend
  297. :transcoders
  298. '((template . (lambda (text info)
  299. (org-element-interpret-data
  300. (plist-get info :title)))))))))))
  301. ;; With a blank TITLE keyword.
  302. (should
  303. (equal
  304. ""
  305. (let (org-export-filter-body-functions
  306. org-export-filter-final-output-functions)
  307. (org-test-with-temp-text "#+TITLE:\nTest"
  308. (org-export-as
  309. (org-export-create-backend
  310. :transcoders
  311. '((template . (lambda (text info)
  312. (org-element-interpret-data
  313. (plist-get info :title)))))))))))
  314. ;; With a non-empty TITLE keyword.
  315. (should
  316. (equal
  317. "Title"
  318. (org-test-with-temp-text "#+TITLE: Title\nTest"
  319. (org-export-as
  320. (org-export-create-backend
  321. :transcoders
  322. '((template . (lambda (text info)
  323. (org-element-interpret-data
  324. (plist-get info :title))))))))))
  325. ;; When exporting a subtree, its heading becomes the headline of the
  326. ;; document...
  327. (should
  328. (equal
  329. "Headline"
  330. (org-test-with-temp-text "* Headline\nBody"
  331. (org-export-as
  332. (org-export-create-backend
  333. :transcoders
  334. '((template . (lambda (text info)
  335. (org-element-interpret-data
  336. (plist-get info :title))))))
  337. 'subtree))))
  338. ;; ... unless there is an EXPORT_TITLE property at the root of the
  339. ;; subtree.
  340. (should
  341. (equal
  342. "B"
  343. (org-test-with-temp-text
  344. "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
  345. (org-export-as
  346. (org-export-create-backend
  347. :transcoders
  348. '((template . (lambda (text info)
  349. (org-element-interpret-data
  350. (plist-get info :title))))))
  351. 'subtree)))))
  352. (ert-deftest test-org-export/handle-options ()
  353. "Test if export options have an impact on output."
  354. ;; Test exclude tags for headlines and inlinetasks.
  355. (should
  356. (equal ""
  357. (let (org-export-filter-body-functions
  358. org-export-filter-final-output-functions)
  359. (org-test-with-temp-text "* Head1 :noexp:"
  360. (org-export-as (org-test-default-backend)
  361. nil nil nil '(:exclude-tags ("noexp")))))))
  362. ;; Test include tags for headlines and inlinetasks.
  363. (should
  364. (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
  365. (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  366. (let ((org-tags-column 0))
  367. (org-export-as (org-test-default-backend)
  368. nil nil nil '(:select-tags ("exp")))))))
  369. ;; If there is an include tag, ignore the section before the first
  370. ;; headline, if any.
  371. (should
  372. (equal "* H1 :exp:\nBody\n"
  373. (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
  374. (let ((org-tags-column 0))
  375. (org-export-as (org-test-default-backend)
  376. nil nil nil '(:select-tags ("exp")))))))
  377. (should-not
  378. (equal "* H1 :exp:\n"
  379. (org-test-with-temp-text "* H1 :exp:\nBody"
  380. (let ((org-tags-column 0))
  381. (org-export-as (org-test-default-backend)
  382. nil nil nil '(:select-tags ("exp")))))))
  383. ;; Test mixing include tags and exclude tags.
  384. (should
  385. (string-match
  386. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  387. (org-test-with-temp-text "
  388. * Head1 :export:
  389. ** Sub-Head1 :noexport:
  390. ** Sub-Head2
  391. * Head2 :noexport:
  392. ** Sub-Head1 :export:"
  393. (org-export-as (org-test-default-backend) nil nil nil
  394. '(:select-tags ("export") :exclude-tags ("noexport"))))))
  395. ;; Ignore tasks.
  396. (should
  397. (equal ""
  398. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  399. org-export-filter-body-functions
  400. org-export-filter-final-output-functions)
  401. (org-test-with-temp-text "* TODO Head1"
  402. (org-export-as (org-test-default-backend)
  403. nil nil nil '(:with-tasks nil))))))
  404. (should
  405. (equal "* TODO Head1\n"
  406. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  407. (org-test-with-temp-text "* TODO Head1"
  408. (org-export-as (org-test-default-backend)
  409. nil nil nil '(:with-tasks t))))))
  410. ;; Archived tree.
  411. (should
  412. (equal ""
  413. (let (org-export-filter-body-functions
  414. org-export-filter-final-output-functions)
  415. (org-test-with-temp-text "* Head1 :archive:"
  416. (let ((org-archive-tag "archive"))
  417. (org-export-as (org-test-default-backend)
  418. nil nil nil '(:with-archived-trees nil)))))))
  419. (should
  420. (string-match
  421. "\\* Head1[ \t]+:archive:"
  422. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  423. (let ((org-archive-tag "archive"))
  424. (org-export-as (org-test-default-backend) nil nil nil
  425. '(:with-archived-trees headline))))))
  426. (should
  427. (string-match
  428. "\\`\\* Head1[ \t]+:archive:\n\\'"
  429. (org-test-with-temp-text "* Head1 :archive:"
  430. (let ((org-archive-tag "archive"))
  431. (org-export-as (org-test-default-backend)
  432. nil nil nil '(:with-archived-trees t))))))
  433. ;; Clocks.
  434. (should
  435. (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
  436. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  437. (org-export-as (org-test-default-backend)
  438. nil nil nil '(:with-clocks t)))))
  439. (should
  440. (equal ""
  441. (let (org-export-filter-body-functions
  442. org-export-filter-final-output-functions)
  443. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  444. (org-export-as (org-test-default-backend)
  445. nil nil nil '(:with-clocks nil))))))
  446. ;; Drawers.
  447. (should
  448. (equal ""
  449. (let (org-export-filter-body-functions
  450. org-export-filter-final-output-functions)
  451. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  452. (org-export-as (org-test-default-backend)
  453. nil nil nil '(:with-drawers nil))))))
  454. (should
  455. (equal ":TEST:\ncontents\n:END:\n"
  456. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  457. (org-export-as (org-test-default-backend)
  458. nil nil nil '(:with-drawers t)))))
  459. (should
  460. (equal ":FOO:\nkeep\n:END:\n"
  461. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  462. (org-export-as (org-test-default-backend)
  463. nil nil nil '(:with-drawers ("FOO"))))))
  464. (should
  465. (equal ":FOO:\nkeep\n:END:\n"
  466. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  467. (org-export-as (org-test-default-backend)
  468. nil nil nil '(:with-drawers (not "BAR"))))))
  469. ;; Fixed-width.
  470. (should
  471. (equal ": A\n"
  472. (org-test-with-temp-text ": A"
  473. (org-export-as (org-test-default-backend) nil nil nil
  474. '(:with-fixed-width t)))))
  475. (should
  476. (equal ""
  477. (let (org-export-filter-body-functions
  478. org-export-filter-final-output-functions)
  479. (org-test-with-temp-text ": A"
  480. (org-export-as (org-test-default-backend) nil nil nil
  481. '(:with-fixed-width nil))))))
  482. ;; Footnotes.
  483. (should
  484. (equal "Footnote?"
  485. (let ((org-footnote-section nil))
  486. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  487. (org-trim (org-export-as (org-test-default-backend)
  488. nil nil nil '(:with-footnotes nil)))))))
  489. (should
  490. (equal "Footnote?[fn:1]\n\n[fn:1] Def"
  491. (let ((org-footnote-section nil))
  492. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  493. (org-trim (org-export-as (org-test-default-backend)
  494. nil nil nil '(:with-footnotes t)))))))
  495. ;; Inlinetasks.
  496. (when (featurep 'org-inlinetask)
  497. (should
  498. (equal
  499. ""
  500. (let ((org-inlinetask-min-level 15)
  501. org-export-filter-body-functions
  502. org-export-filter-final-output-functions)
  503. (org-test-with-temp-text "*************** Task"
  504. (org-export-as (org-test-default-backend)
  505. nil nil nil '(:with-inlinetasks nil))))))
  506. (should
  507. (equal
  508. ""
  509. (let ((org-inlinetask-min-level 15)
  510. org-export-filter-body-functions
  511. org-export-filter-final-output-functions)
  512. (org-test-with-temp-text
  513. "*************** Task\nContents\n*************** END"
  514. (org-export-as (org-test-default-backend)
  515. nil nil nil '(:with-inlinetasks nil)))))))
  516. ;; Plannings.
  517. (should
  518. (string-match
  519. "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
  520. (let ((org-closed-string "CLOSED:"))
  521. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  522. (org-export-as (org-test-default-backend)
  523. nil nil nil '(:with-planning t))))))
  524. (should
  525. (equal "* H\n"
  526. (let ((org-closed-string "CLOSED:"))
  527. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  528. (org-export-as (org-test-default-backend)
  529. nil nil nil '(:with-planning nil))))))
  530. ;; Property Drawers.
  531. (should
  532. (equal "* H1\n"
  533. (org-test-with-temp-text
  534. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  535. (org-export-as (org-test-default-backend)
  536. nil nil nil '(:with-properties nil)))))
  537. (should
  538. (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
  539. (org-test-with-temp-text
  540. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  541. (org-export-as (org-test-default-backend)
  542. nil nil nil '(:with-properties t)))))
  543. (should
  544. (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
  545. (org-test-with-temp-text
  546. "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
  547. (org-export-as (org-test-default-backend)
  548. nil nil nil '(:with-properties ("B"))))))
  549. ;; Statistics cookies.
  550. (should
  551. (equal ""
  552. (let (org-export-filter-body-functions
  553. org-export-filter-final-output-functions)
  554. (org-trim
  555. (org-test-with-temp-text "[0/0]"
  556. (org-export-as (org-test-default-backend)
  557. nil nil nil '(:with-statistics-cookies nil)))))))
  558. ;; Tables.
  559. (should
  560. (equal "| A |\n"
  561. (org-test-with-temp-text "| A |"
  562. (org-export-as (org-test-default-backend) nil nil nil
  563. '(:with-tables t)))))
  564. (should
  565. (equal ""
  566. (let (org-export-filter-body-functions
  567. org-export-filter-final-output-functions)
  568. (org-test-with-temp-text "| A |"
  569. (org-export-as (org-test-default-backend) nil nil nil
  570. '(:with-tables nil)))))))
  571. (ert-deftest test-org-export/with-timestamps ()
  572. "Test `org-export-with-timestamps' specifications."
  573. ;; t value.
  574. (should
  575. (string-match
  576. "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
  577. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  578. (org-export-as (org-test-default-backend)
  579. nil nil nil '(:with-timestamps t)))))
  580. ;; nil value.
  581. (should
  582. (equal
  583. ""
  584. (let (org-export-filter-body-functions
  585. org-export-filter-final-output-functions)
  586. (org-trim
  587. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  588. (org-export-as (org-test-default-backend)
  589. nil nil nil '(:with-timestamps nil)))))))
  590. ;; `active' value.
  591. (should
  592. (string-match
  593. "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  594. (org-test-with-temp-text
  595. "<2012-03-29 Thu>[2012-03-29 Thu]
  596. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  597. (org-export-as (org-test-default-backend)
  598. nil nil nil '(:with-timestamps active)))))
  599. ;; `inactive' value.
  600. (should
  601. (string-match
  602. "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  603. (org-test-with-temp-text
  604. "<2012-03-29 Thu>[2012-03-29 Thu]
  605. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  606. (org-export-as (org-test-default-backend)
  607. nil nil nil '(:with-timestamps inactive))))))
  608. (ert-deftest test-org-export/comment-tree ()
  609. "Test if export process ignores commented trees."
  610. (should
  611. (equal ""
  612. (let (org-export-filter-body-functions
  613. org-export-filter-final-output-functions)
  614. (org-test-with-temp-text "* COMMENT Head1"
  615. (org-export-as (org-test-default-backend)))))))
  616. (ert-deftest test-org-export/uninterpreted ()
  617. "Test handling of uninterpreted elements."
  618. ;; Entities.
  619. (should
  620. (equal "dummy\n"
  621. (org-test-with-temp-text "\\alpha"
  622. (org-export-as
  623. (org-export-create-backend
  624. :transcoders '((entity . (lambda (e c i) "dummy"))
  625. (paragraph . (lambda (p c i) c))
  626. (section . (lambda (s c i) c))))
  627. nil nil nil '(:with-entities t)))))
  628. (should
  629. (equal "\\alpha\n"
  630. (org-test-with-temp-text "\\alpha"
  631. (org-export-as
  632. (org-export-create-backend
  633. :transcoders '((entity . (lambda (e c i) "dummy"))
  634. (paragraph . (lambda (p c i) c))
  635. (section . (lambda (s c i) c))))
  636. nil nil nil '(:with-entities nil)))))
  637. ;; Emphasis.
  638. (should
  639. (equal "dummy\n"
  640. (org-test-with-temp-text "*bold*"
  641. (org-export-as
  642. (org-export-create-backend
  643. :transcoders '((bold . (lambda (b c i) "dummy"))
  644. (paragraph . (lambda (p c i) c))
  645. (section . (lambda (s c i) c))))
  646. nil nil nil '(:with-emphasize t)))))
  647. (should
  648. (equal "*bold*\n"
  649. (org-test-with-temp-text "*bold*"
  650. (org-export-as
  651. (org-export-create-backend
  652. :transcoders '((bold . (lambda (b c i) "dummy"))
  653. (paragraph . (lambda (p c i) c))
  654. (section . (lambda (s c i) c))))
  655. nil nil nil '(:with-emphasize nil)))))
  656. ;; LaTeX environment.
  657. (should
  658. (equal "dummy\n"
  659. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  660. (org-export-as
  661. (org-export-create-backend
  662. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  663. (section . (lambda (s c i) c))))
  664. nil nil nil '(:with-latex t)))))
  665. (should
  666. (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
  667. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  668. (org-export-as
  669. (org-export-create-backend
  670. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  671. (section . (lambda (s c i) c))))
  672. nil nil nil '(:with-latex verbatim)))))
  673. ;; LaTeX fragment.
  674. (should
  675. (equal "dummy\n"
  676. (org-test-with-temp-text "$1$"
  677. (org-export-as
  678. (org-export-create-backend
  679. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  680. (paragraph . (lambda (p c i) c))
  681. (section . (lambda (s c i) c))))
  682. nil nil nil '(:with-latex t)))))
  683. (should
  684. (equal "$1$\n"
  685. (org-test-with-temp-text "$1$"
  686. (org-export-as
  687. (org-export-create-backend
  688. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  689. (paragraph . (lambda (p c i) c))
  690. (section . (lambda (s c i) c))))
  691. nil nil nil '(:with-latex verbatim)))))
  692. ;; Sub/superscript.
  693. (should
  694. (equal "adummy\n"
  695. (org-test-with-temp-text "a_b"
  696. (org-export-as
  697. (org-export-create-backend
  698. :transcoders '((subscript . (lambda (s c i) "dummy"))
  699. (paragraph . (lambda (p c i) c))
  700. (section . (lambda (s c i) c))))
  701. nil nil nil '(:with-sub-superscript t)))))
  702. (should
  703. (equal "a_b\n"
  704. (org-test-with-temp-text "a_b"
  705. (org-export-as
  706. (org-export-create-backend
  707. :transcoders '((subscript . (lambda (s c i) "dummy"))
  708. (paragraph . (lambda (p c i) c))
  709. (section . (lambda (s c i) c))))
  710. nil nil nil '(:with-sub-superscript nil)))))
  711. (should
  712. (equal "a_b\n"
  713. (org-test-with-temp-text "a_b"
  714. (org-export-as
  715. (org-export-create-backend
  716. :transcoders '((subscript . (lambda (s c i) "dummy"))
  717. (paragraph . (lambda (p c i) c))
  718. (section . (lambda (s c i) c))))
  719. nil nil nil '(:with-sub-superscript {})))))
  720. (should
  721. (equal "adummy\n"
  722. (org-test-with-temp-text "a_{b}"
  723. (org-export-as
  724. (org-export-create-backend
  725. :transcoders '((subscript . (lambda (s c i) "dummy"))
  726. (paragraph . (lambda (p c i) c))
  727. (section . (lambda (s c i) c))))
  728. nil nil nil '(:with-sub-superscript {})))))
  729. ;; Also handle uninterpreted objects in title.
  730. (should
  731. (equal "a_b"
  732. (org-test-with-temp-text "#+TITLE: a_b"
  733. (org-export-as
  734. (org-export-create-backend
  735. :transcoders
  736. '((subscript . (lambda (s c i) "dummy"))
  737. (template . (lambda (c i) (org-export-data
  738. (plist-get i :title) i)))
  739. (section . (lambda (s c i) c))))
  740. nil nil nil '(:with-sub-superscript nil)))))
  741. ;; Handle uninterpreted objects in captions.
  742. (should
  743. (equal "adummy\n"
  744. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  745. (org-export-as
  746. (org-export-create-backend
  747. :transcoders
  748. '((subscript . (lambda (s c i) "dummy"))
  749. (paragraph . (lambda (p c i)
  750. (org-export-data (org-export-get-caption p) i)))
  751. (section . (lambda (s c i) c))))
  752. nil nil nil '(:with-sub-superscript t)))))
  753. (should
  754. (equal "a_b\n"
  755. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  756. (org-export-as
  757. (org-export-create-backend
  758. :transcoders
  759. '((subscript . (lambda (s c i) "dummy"))
  760. (paragraph . (lambda (p c i)
  761. (org-export-data (org-export-get-caption p) i)))
  762. (section . (lambda (s c i) c))))
  763. nil nil nil '(:with-sub-superscript nil)))))
  764. ;; Special case: multiples uninterpreted objects in a row.
  765. (should
  766. (equal "a_b_c_d\n"
  767. (org-test-with-temp-text "a_b_c_d"
  768. (org-export-as
  769. (org-export-create-backend
  770. :transcoders '((subscript . (lambda (s c i) "dummy"))
  771. (paragraph . (lambda (p c i) c))
  772. (section . (lambda (s c i) c))))
  773. nil nil nil '(:with-sub-superscript {}))))))
  774. (ert-deftest test-org-export/export-scope ()
  775. "Test all export scopes."
  776. (org-test-with-temp-text "
  777. * Head1
  778. ** Head2
  779. text
  780. *** Head3"
  781. ;; Subtree.
  782. (forward-line 3)
  783. (should (equal (org-export-as (org-test-default-backend) 'subtree)
  784. "text\n*** Head3\n"))
  785. ;; Visible.
  786. (goto-char (point-min))
  787. (forward-line)
  788. (org-cycle)
  789. (should (equal (org-export-as (org-test-default-backend) nil 'visible)
  790. "* Head1\n"))
  791. ;; Region.
  792. (goto-char (point-min))
  793. (forward-line 3)
  794. (transient-mark-mode 1)
  795. (push-mark (point) t t)
  796. (goto-char (point-at-eol))
  797. (should (equal (org-export-as (org-test-default-backend)) "text\n")))
  798. ;; Subtree with a code block calling another block outside.
  799. (let ((org-export-babel-evaluate t))
  800. (should
  801. (equal ": 3\n"
  802. (org-test-with-temp-text "
  803. * Head1
  804. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  805. <<test>>
  806. #+END_SRC
  807. * Head2
  808. #+NAME: test
  809. #+BEGIN_SRC emacs-lisp
  810. \(+ 1 2)
  811. #+END_SRC"
  812. (forward-line 1)
  813. (org-export-as (org-test-default-backend) 'subtree)))))
  814. ;; Body only.
  815. (let ((backend (org-test-default-backend)))
  816. (setf (org-export-backend-transcoders backend)
  817. (cons '(template . (lambda (body i)
  818. (format "BEGIN\n%sEND" body)))
  819. (org-export-backend-transcoders backend)))
  820. (org-test-with-temp-text "Text"
  821. (should (equal (org-export-as backend nil nil 'body-only)
  822. "Text\n"))
  823. (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
  824. (ert-deftest test-org-export/output-file-name ()
  825. "Test `org-export-output-file-name' specifications."
  826. ;; Export from a file: name is built from original file name.
  827. (should
  828. (org-test-with-temp-text-in-file "Test"
  829. (equal (concat (file-name-as-directory ".")
  830. (file-name-nondirectory
  831. (file-name-sans-extension (buffer-file-name))))
  832. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  833. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  834. (should
  835. (org-test-with-temp-text-in-file
  836. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  837. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  838. ;; From a buffer not associated to a file, too.
  839. (should
  840. (org-test-with-temp-text
  841. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  842. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  843. ;; When provided name is absolute, preserve it.
  844. (should
  845. (org-test-with-temp-text
  846. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  847. (expand-file-name "test"))
  848. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  849. ;; When PUB-DIR argument is provided, use it.
  850. (should
  851. (org-test-with-temp-text-in-file "Test"
  852. (equal (file-name-directory
  853. (org-export-output-file-name ".ext" nil "dir/"))
  854. "dir/")))
  855. ;; When returned name would overwrite original file, add EXTENSION
  856. ;; another time.
  857. (should
  858. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  859. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  860. (ert-deftest test-org-export/expand-include ()
  861. "Test file inclusion in an Org buffer."
  862. ;; Error when file isn't specified.
  863. (should-error
  864. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  865. (org-export-expand-include-keyword)))
  866. ;; Full insertion with recursive inclusion.
  867. (should
  868. (equal
  869. (with-temp-buffer
  870. (insert-file
  871. (expand-file-name "examples/include.org" org-test-dir))
  872. (replace-regexp-in-string
  873. (regexp-quote "#+INCLUDE: \"include2.org\"")
  874. "Success!" (buffer-string)))
  875. (org-test-with-temp-text
  876. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  877. (org-export-expand-include-keyword)
  878. (buffer-string))))
  879. ;; Localized insertion.
  880. (org-test-with-temp-text
  881. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  882. org-test-dir)
  883. (org-export-expand-include-keyword)
  884. (should (equal (buffer-string)
  885. "Small Org file with an include keyword.\n")))
  886. ;; Insertion with constraints on headlines level.
  887. (should
  888. (equal
  889. "* Top heading\n** Heading\nbody\n"
  890. (org-test-with-temp-text
  891. (format
  892. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  893. org-test-dir)
  894. (org-export-expand-include-keyword)
  895. (buffer-string))))
  896. (should
  897. (equal
  898. "* Top heading\n* Heading\nbody\n"
  899. (org-test-with-temp-text
  900. (format
  901. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  902. org-test-dir)
  903. (org-export-expand-include-keyword)
  904. (buffer-string))))
  905. ;; Inclusion within an example block.
  906. (should
  907. (equal
  908. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  909. (org-test-with-temp-text
  910. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  911. org-test-dir)
  912. (org-export-expand-include-keyword)
  913. (buffer-string))))
  914. ;; Inclusion within a src-block.
  915. (should
  916. (equal
  917. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  918. (org-test-with-temp-text
  919. (format
  920. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  921. org-test-dir)
  922. (org-export-expand-include-keyword)
  923. (buffer-string))))
  924. ;; Inclusion within an html export-block.
  925. (should
  926. (equal
  927. "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
  928. (org-test-with-temp-text
  929. (format
  930. "#+INCLUDE: \"%s/examples/include.html\" HTML"
  931. org-test-dir)
  932. (org-export-expand-include-keyword)
  933. (buffer-string))))
  934. ;; Inclusion within an center paragraph
  935. (should
  936. (equal
  937. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  938. (org-test-with-temp-text
  939. (format
  940. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  941. org-test-dir)
  942. (org-export-expand-include-keyword)
  943. (buffer-string))))
  944. ;; Footnotes labels are local to each included file.
  945. (should
  946. (= 6
  947. (length
  948. (delete-dups
  949. (let ((contents "
  950. Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:anonymous footnote].
  951. \[fn:1] Footnote 1
  952. \[fn:test] Footnote \"test\""))
  953. (org-test-with-temp-text-in-file contents
  954. (let ((file1 (buffer-file-name)))
  955. (org-test-with-temp-text-in-file contents
  956. (let ((file2 (buffer-file-name)))
  957. (org-test-with-temp-text
  958. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  959. file1 file2)
  960. (org-export-expand-include-keyword)
  961. (org-element-map (org-element-parse-buffer)
  962. 'footnote-reference
  963. (lambda (r) (org-element-property :label r)))))))))))))
  964. ;; Footnotes labels are not local to each include keyword.
  965. (should
  966. (= 4
  967. (length
  968. (delete-dups
  969. (let ((contents "
  970. Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
  971. \[fn:1] Footnote 1
  972. \[2] Footnote 2
  973. \[fn:test] Footnote \"test\""))
  974. (org-test-with-temp-text-in-file contents
  975. (let ((file (buffer-file-name)))
  976. (org-test-with-temp-text
  977. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  978. (org-export-expand-include-keyword)
  979. (org-element-map (org-element-parse-buffer)
  980. 'footnote-reference
  981. (lambda (ref) (org-element-property :label ref)))))))))))
  982. ;; Footnotes are supported by :lines-like elements and unnecessary
  983. ;; footnotes are dropped.
  984. (should
  985. (= 4
  986. (length
  987. (delete-dups
  988. (let ((contents "
  989. * foo
  990. Footnotes[fn:1]
  991. * bar
  992. Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
  993. \[fn:1] Footnote 1
  994. \[fn:2] Footnote 1
  995. * Footnotes
  996. \[fn:test] Footnote \"test\"
  997. \[3] Footnote 3
  998. "))
  999. (org-test-with-temp-text-in-file contents
  1000. (let ((file (buffer-file-name)))
  1001. (org-test-with-temp-text
  1002. (format "#+INCLUDE: \"%s::*bar\"
  1003. " file)
  1004. (org-export-expand-include-keyword)
  1005. (org-element-map (org-element-parse-buffer)
  1006. 'footnote-definition
  1007. (lambda (ref) (org-element-property :label ref)))))))))))
  1008. ;; If only-contents is non-nil only include contents of element.
  1009. (should
  1010. (equal
  1011. "body\n"
  1012. (org-test-with-temp-text
  1013. (concat
  1014. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
  1015. ":only-contents t")
  1016. (org-export-expand-include-keyword)
  1017. (buffer-string))))
  1018. ;; Headings can be included via CUSTOM_ID.
  1019. (should
  1020. (org-test-with-temp-text
  1021. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1022. (org-export-expand-include-keyword)
  1023. (goto-char (point-min))
  1024. (looking-at "* Another heading")))
  1025. ;; Named objects can be included.
  1026. (should
  1027. (equal
  1028. "| 1 |\n"
  1029. (org-test-with-temp-text
  1030. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
  1031. (org-export-expand-include-keyword)
  1032. (buffer-string))))
  1033. ;; Including non-existing elements should result in an error.
  1034. (should-error
  1035. (org-test-with-temp-text
  1036. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
  1037. (org-export-expand-include-keyword)))
  1038. ;; Lines work relatively to an included element.
  1039. (should
  1040. (equal
  1041. "2\n3\n"
  1042. (org-test-with-temp-text
  1043. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
  1044. (org-export-expand-include-keyword)
  1045. (buffer-string))))
  1046. ;; Properties should be dropped from headlines.
  1047. (should
  1048. (equal
  1049. (org-test-with-temp-text
  1050. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
  1051. (org-export-expand-include-keyword)
  1052. (buffer-string))
  1053. (org-test-with-temp-text
  1054. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1055. (org-export-expand-include-keyword)
  1056. (buffer-string))))
  1057. ;; Properties should be dropped, drawers should not be.
  1058. (should
  1059. (equal
  1060. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1061. (org-test-with-temp-text
  1062. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
  1063. (org-export-expand-include-keyword)
  1064. (buffer-string))))
  1065. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1066. (should
  1067. (org-every (lambda (level) (zerop (1- level)))
  1068. (org-test-with-temp-text
  1069. (concat
  1070. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
  1071. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
  1072. (org-export-expand-include-keyword)
  1073. (org-element-map (org-element-parse-buffer) 'headline
  1074. (lambda (head) (org-element-property :level head))))))
  1075. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1076. (should-not
  1077. (equal
  1078. (org-test-with-temp-text
  1079. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
  1080. (org-export-expand-include-keyword)
  1081. (buffer-string))
  1082. (org-test-with-temp-text
  1083. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
  1084. (org-export-expand-include-keyword)
  1085. (buffer-string))))
  1086. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1087. (should
  1088. (org-test-with-temp-text-in-file
  1089. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1090. (narrow-to-region (point) (point-max))
  1091. (org-export-expand-include-keyword)
  1092. (eq 1 (org-current-level))))
  1093. ;; If :minlevel is present do not alter it.
  1094. (should
  1095. (org-test-with-temp-text
  1096. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
  1097. (narrow-to-region (point) (point-max))
  1098. (org-export-expand-include-keyword)
  1099. (eq 3 (org-current-level)))))
  1100. (ert-deftest test-org-export/expand-macro ()
  1101. "Test macro expansion in an Org buffer."
  1102. ;; Standard macro expansion.
  1103. (should
  1104. (equal "#+MACRO: macro1 value\nvalue\n"
  1105. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1106. (org-export-as (org-test-default-backend)))))
  1107. ;; Allow macro in parsed keywords and associated properties.
  1108. ;; Standard macro expansion.
  1109. (should
  1110. (string-match
  1111. "#\\+K: value"
  1112. (let ((backend (org-export-create-backend
  1113. :parent 'org
  1114. :options '((:k "K" nil nil parse)))))
  1115. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1116. (org-export-as backend)))))
  1117. (should
  1118. (string-match
  1119. ":EXPORT_K: v"
  1120. (let ((backend (org-export-create-backend
  1121. :parent 'org
  1122. :options '((:k "K" nil nil parse)))))
  1123. (org-test-with-temp-text
  1124. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1125. (org-export-as backend nil nil nil '(:with-properties t))))))
  1126. ;; Expand specific macros.
  1127. (should
  1128. (equal "me 2012-03-29 me@here Title\n"
  1129. (org-test-with-temp-text
  1130. "
  1131. #+TITLE: Title
  1132. #+DATE: 2012-03-29
  1133. #+AUTHOR: me
  1134. #+EMAIL: me@here
  1135. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1136. (let ((output (org-export-as (org-test-default-backend))))
  1137. (substring output (string-match ".*\n\\'" output))))))
  1138. ;; Expand specific macros when property contained a regular macro
  1139. ;; already.
  1140. (should
  1141. (equal "value\n"
  1142. (org-test-with-temp-text "
  1143. #+MACRO: macro1 value
  1144. #+TITLE: {{{macro1}}}
  1145. {{{title}}}"
  1146. (let ((output (org-export-as (org-test-default-backend))))
  1147. (substring output (string-match ".*\n\\'" output))))))
  1148. ;; Expand macros with templates in included files.
  1149. (should
  1150. (equal "success\n"
  1151. (org-test-with-temp-text
  1152. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1153. {{{included-macro}}}" org-test-dir)
  1154. (let ((output (org-export-as (org-test-default-backend))))
  1155. (substring output (string-match ".*\n\\'" output))))))
  1156. ;; Date macro takes a optional formatting argument
  1157. (should
  1158. (equal "09-02-15\n"
  1159. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1160. (org-export-as (org-test-default-backend)))))
  1161. ;; Only single timestamps are formatted
  1162. (should
  1163. (equal "<2015-02x-09>\n"
  1164. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1165. (org-export-as (org-test-default-backend)))))
  1166. ;; Throw an error when a macro definition is missing.
  1167. (should-error
  1168. (org-test-with-temp-text "{{{missing}}}"
  1169. (org-export-as (org-test-default-backend))))
  1170. ;; Macros defined in commented subtrees are ignored.
  1171. (should-error
  1172. (org-test-with-temp-text
  1173. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1174. (org-export-as (org-test-default-backend))))
  1175. (should-error
  1176. (org-test-with-temp-text
  1177. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1178. (org-export-as (org-test-default-backend)))))
  1179. (ert-deftest test-org-export/before-processing-hook ()
  1180. "Test `org-export-before-processing-hook'."
  1181. (should
  1182. (equal
  1183. "#+MACRO: mac val\nTest\n"
  1184. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1185. (let ((org-export-before-processing-hook
  1186. '((lambda (backend)
  1187. (while (re-search-forward "{{{" nil t)
  1188. (let ((object (org-element-context)))
  1189. (when (eq (org-element-type object) 'macro)
  1190. (delete-region
  1191. (org-element-property :begin object)
  1192. (org-element-property :end object)))))))))
  1193. (org-export-as (org-test-default-backend)))))))
  1194. (ert-deftest test-org-export/before-parsing-hook ()
  1195. "Test `org-export-before-parsing-hook'."
  1196. (should
  1197. (equal "Body 1\nBody 2\n"
  1198. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1199. (let ((org-export-before-parsing-hook
  1200. '((lambda (backend)
  1201. (goto-char (point-min))
  1202. (while (re-search-forward org-outline-regexp-bol nil t)
  1203. (delete-region
  1204. (point-at-bol) (progn (forward-line) (point))))))))
  1205. (org-export-as (org-test-default-backend)))))))
  1206. ;;; Affiliated Keywords
  1207. (ert-deftest test-org-export/read-attribute ()
  1208. "Test `org-export-read-attribute' specifications."
  1209. ;; Standard test.
  1210. (should
  1211. (equal
  1212. (org-export-read-attribute
  1213. :attr_html
  1214. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1215. (org-element-at-point)))
  1216. '(:a "1" :b "2")))
  1217. ;; Return nil on empty attribute.
  1218. (should-not
  1219. (org-export-read-attribute
  1220. :attr_html
  1221. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1222. ;; Return nil on "nil" string.
  1223. (should
  1224. (equal '(:a nil :b nil)
  1225. (org-export-read-attribute
  1226. :attr_html
  1227. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1228. (org-element-at-point)))))
  1229. ;; Return nil on empty string.
  1230. (should
  1231. (equal '(:a nil :b nil)
  1232. (org-export-read-attribute
  1233. :attr_html
  1234. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1235. (org-element-at-point)))))
  1236. ;; Return empty string when value is "".
  1237. (should
  1238. (equal '(:a "")
  1239. (org-export-read-attribute
  1240. :attr_html
  1241. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1242. (org-element-at-point)))))
  1243. ;; Return \"\" when value is """".
  1244. (should
  1245. (equal '(:a "\"\"")
  1246. (org-export-read-attribute
  1247. :attr_html
  1248. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1249. (org-element-at-point)))))
  1250. ;; Ignore text before first property.
  1251. (should-not
  1252. (member "ignore"
  1253. (org-export-read-attribute
  1254. :attr_html
  1255. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1256. (org-element-at-point))))))
  1257. (ert-deftest test-org-export/get-caption ()
  1258. "Test `org-export-get-caption' specifications."
  1259. ;; Without optional argument, return long caption
  1260. (should
  1261. (equal
  1262. '("l")
  1263. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1264. (org-export-get-caption (org-element-at-point)))))
  1265. ;; With optional argument, return short caption.
  1266. (should
  1267. (equal
  1268. '("s")
  1269. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1270. (org-export-get-caption (org-element-at-point) t))))
  1271. ;; Multiple lines are separated by white spaces.
  1272. (should
  1273. (equal
  1274. '("a" " " "b")
  1275. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1276. (org-export-get-caption (org-element-at-point))))))
  1277. ;;; Back-End Tools
  1278. (ert-deftest test-org-export/define-backend ()
  1279. "Test back-end definition and accessors."
  1280. ;; Translate table.
  1281. (should
  1282. (equal '((headline . my-headline-test))
  1283. (let (org-export-registered-backends)
  1284. (org-export-define-backend 'test '((headline . my-headline-test)))
  1285. (org-export-get-all-transcoders 'test))))
  1286. ;; Filters.
  1287. (should
  1288. (equal '((:filter-headline . my-filter))
  1289. (let (org-export-registered-backends)
  1290. (org-export-define-backend 'test
  1291. '((headline . my-headline-test))
  1292. :filters-alist '((:filter-headline . my-filter)))
  1293. (org-export-backend-filters (org-export-get-backend 'test)))))
  1294. ;; Options.
  1295. (should
  1296. (equal '((:prop value))
  1297. (let (org-export-registered-backends)
  1298. (org-export-define-backend 'test
  1299. '((headline . my-headline-test))
  1300. :options-alist '((:prop value)))
  1301. (org-export-backend-options (org-export-get-backend 'test)))))
  1302. ;; Menu.
  1303. (should
  1304. (equal '(?k "Test Export" test)
  1305. (let (org-export-registered-backends)
  1306. (org-export-define-backend 'test
  1307. '((headline . my-headline-test))
  1308. :menu-entry '(?k "Test Export" test))
  1309. (org-export-backend-menu (org-export-get-backend 'test)))))
  1310. ;; Export Blocks.
  1311. (should
  1312. (equal '(("TEST" . org-element-export-block-parser))
  1313. (let (org-export-registered-backends org-element-block-name-alist)
  1314. (org-export-define-backend 'test
  1315. '((headline . my-headline-test))
  1316. :export-block '("test"))
  1317. org-element-block-name-alist))))
  1318. (ert-deftest test-org-export/define-derived-backend ()
  1319. "Test `org-export-define-derived-backend' specifications."
  1320. ;; Error when parent back-end is not defined.
  1321. (should-error
  1322. (let (org-export-registered-backends)
  1323. (org-export-define-derived-backend 'test 'parent)))
  1324. ;; Append translation table to parent's.
  1325. (should
  1326. (equal '((:headline . test) (:headline . parent))
  1327. (let (org-export-registered-backends)
  1328. (org-export-define-backend 'parent '((:headline . parent)))
  1329. (org-export-define-derived-backend 'test 'parent
  1330. :translate-alist '((:headline . test)))
  1331. (org-export-get-all-transcoders 'test))))
  1332. ;; Options defined in the new back have priority over those defined
  1333. ;; in parent.
  1334. (should
  1335. (eq 'test
  1336. (let (org-export-registered-backends)
  1337. (org-export-define-backend 'parent
  1338. '((:headline . parent))
  1339. :options-alist '((:a nil nil 'parent)))
  1340. (org-export-define-derived-backend 'test 'parent
  1341. :options-alist '((:a nil nil 'test)))
  1342. (plist-get (org-export--get-global-options
  1343. (org-export-get-backend 'test))
  1344. :a)))))
  1345. (ert-deftest test-org-export/derived-backend-p ()
  1346. "Test `org-export-derived-backend-p' specifications."
  1347. ;; Non-nil with direct match.
  1348. (should
  1349. (let (org-export-registered-backends)
  1350. (org-export-define-backend 'test '((headline . test)))
  1351. (org-export-derived-backend-p 'test 'test)))
  1352. (should
  1353. (let (org-export-registered-backends)
  1354. (org-export-define-backend 'test '((headline . test)))
  1355. (org-export-define-derived-backend 'test2 'test)
  1356. (org-export-derived-backend-p 'test2 'test2)))
  1357. ;; Non-nil with a direct parent.
  1358. (should
  1359. (let (org-export-registered-backends)
  1360. (org-export-define-backend 'test '((headline . test)))
  1361. (org-export-define-derived-backend 'test2 'test)
  1362. (org-export-derived-backend-p 'test2 'test)))
  1363. ;; Non-nil with an indirect parent.
  1364. (should
  1365. (let (org-export-registered-backends)
  1366. (org-export-define-backend 'test '((headline . test)))
  1367. (org-export-define-derived-backend 'test2 'test)
  1368. (org-export-define-derived-backend 'test3 'test2)
  1369. (org-export-derived-backend-p 'test3 'test)))
  1370. ;; Nil otherwise.
  1371. (should-not
  1372. (let (org-export-registered-backends)
  1373. (org-export-define-backend 'test '((headline . test)))
  1374. (org-export-define-backend 'test2 '((headline . test2)))
  1375. (org-export-derived-backend-p 'test2 'test)))
  1376. (should-not
  1377. (let (org-export-registered-backends)
  1378. (org-export-define-backend 'test '((headline . test)))
  1379. (org-export-define-backend 'test2 '((headline . test2)))
  1380. (org-export-define-derived-backend 'test3 'test2)
  1381. (org-export-derived-backend-p 'test3 'test))))
  1382. (ert-deftest test-org-export/get-all-transcoders ()
  1383. "Test `org-export-get-all-transcoders' specifications."
  1384. ;; Return nil when back-end cannot be found.
  1385. (should-not (org-export-get-all-transcoders nil))
  1386. ;; Same as `org-export-transcoders' if no parent.
  1387. (should
  1388. (equal '((headline . ignore))
  1389. (org-export-get-all-transcoders
  1390. (org-export-create-backend
  1391. :transcoders '((headline . ignore))))))
  1392. ;; But inherit from all ancestors whenever possible.
  1393. (should
  1394. (equal '((section . ignore) (headline . ignore))
  1395. (let (org-export-registered-backends)
  1396. (org-export-define-backend 'b1 '((headline . ignore)))
  1397. (org-export-get-all-transcoders
  1398. (org-export-create-backend
  1399. :parent 'b1 :transcoders '((section . ignore)))))))
  1400. (should
  1401. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1402. (let (org-export-registered-backends)
  1403. (org-export-define-backend 'b1 '((headline . ignore)))
  1404. (org-export-define-derived-backend 'b2 'b1
  1405. :translate-alist '((section . ignore)))
  1406. (org-export-get-all-transcoders
  1407. (org-export-create-backend
  1408. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1409. ;; Back-end transcoders overrule inherited ones.
  1410. (should
  1411. (eq 'b
  1412. (let (org-export-registered-backends)
  1413. (org-export-define-backend 'b1 '((headline . a)))
  1414. (cdr (assq 'headline
  1415. (org-export-get-all-transcoders
  1416. (org-export-create-backend
  1417. :parent 'b1 :transcoders '((headline . b))))))))))
  1418. (ert-deftest test-org-export/get-all-options ()
  1419. "Test `org-export-get-all-options' specifications."
  1420. ;; Return nil when back-end cannot be found.
  1421. (should-not (org-export-get-all-options nil))
  1422. ;; Same as `org-export-options' if no parent.
  1423. (should
  1424. (equal '((headline . ignore))
  1425. (org-export-get-all-options
  1426. (org-export-create-backend
  1427. :options '((headline . ignore))))))
  1428. ;; But inherit from all ancestors whenever possible.
  1429. (should
  1430. (equal '((:key2 value2) (:key1 value1))
  1431. (let (org-export-registered-backends)
  1432. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1433. (org-export-get-all-options
  1434. (org-export-create-backend
  1435. :parent 'b1 :options '((:key2 value2)))))))
  1436. (should
  1437. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1438. (let (org-export-registered-backends)
  1439. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1440. (org-export-define-derived-backend 'b2 'b1
  1441. :options-alist '((:key2 value2)))
  1442. (org-export-get-all-options
  1443. (org-export-create-backend
  1444. :parent 'b2 :options '((:key3 value3)))))))
  1445. ;; Back-end options overrule inherited ones.
  1446. (should
  1447. (eq 'b
  1448. (let (org-export-registered-backends)
  1449. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1450. (cdr (assq :key1
  1451. (org-export-get-all-options
  1452. (org-export-create-backend
  1453. :parent 'b1 :options '((:key1 . b))))))))))
  1454. (ert-deftest test-org-export/get-all-filters ()
  1455. "Test `org-export-get-all-filters' specifications."
  1456. ;; Return nil when back-end cannot be found.
  1457. (should-not (org-export-get-all-filters nil))
  1458. ;; Same as `org-export-filters' if no parent.
  1459. (should
  1460. (equal '((:filter-headline . ignore))
  1461. (org-export-get-all-filters
  1462. (org-export-create-backend
  1463. :filters '((:filter-headline . ignore))))))
  1464. ;; But inherit from all ancestors whenever possible.
  1465. (should
  1466. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1467. (let (org-export-registered-backends)
  1468. (org-export-define-backend 'b1
  1469. nil :filters-alist '((:filter-headline . ignore)))
  1470. (org-export-get-all-filters
  1471. (org-export-create-backend
  1472. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1473. (should
  1474. (equal '((:filter-paragraph . ignore)
  1475. (:filter-section . ignore)
  1476. (:filter-headline . ignore))
  1477. (let (org-export-registered-backends)
  1478. (org-export-define-backend 'b1
  1479. nil :filters-alist '((:filter-headline . ignore)))
  1480. (org-export-define-derived-backend 'b2 'b1
  1481. :filters-alist '((:filter-section . ignore)))
  1482. (org-export-get-all-filters
  1483. (org-export-create-backend
  1484. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1485. ;; Back-end filters overrule inherited ones.
  1486. (should
  1487. (eq 'b
  1488. (let (org-export-registered-backends)
  1489. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1490. (cdr (assq :filter-headline
  1491. (org-export-get-all-filters
  1492. (org-export-create-backend
  1493. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1494. (ert-deftest test-org-export/with-backend ()
  1495. "Test `org-export-with-backend' definition."
  1496. ;; Error when calling an undefined back-end
  1497. (should-error (org-export-with-backend nil "Test"))
  1498. ;; Error when called back-end doesn't have an appropriate
  1499. ;; transcoder.
  1500. (should-error
  1501. (org-export-with-backend
  1502. (org-export-create-backend :transcoders '((headline . ignore)))
  1503. "Test"))
  1504. ;; Otherwise, export using correct transcoder
  1505. (should
  1506. (equal "Success"
  1507. (let (org-export-registered-backends)
  1508. (org-export-define-backend 'test
  1509. '((plain-text . (lambda (text contents info) "Failure"))))
  1510. (org-export-define-backend 'test2
  1511. '((plain-text . (lambda (text contents info) "Success"))))
  1512. (org-export-with-backend 'test2 "Test"))))
  1513. ;; Provide correct back-end if transcoder needs to use recursive
  1514. ;; calls anyway.
  1515. (should
  1516. (equal "Success\n"
  1517. (let ((test-back-end
  1518. (org-export-create-backend
  1519. :transcoders
  1520. '((headline . (lambda (headline contents info)
  1521. (org-export-data
  1522. (org-element-property :title headline)
  1523. info)))
  1524. (plain-text . (lambda (text info) "Success"))))))
  1525. (org-export-string-as
  1526. "* Test"
  1527. (org-export-create-backend
  1528. :transcoders
  1529. '((headline . (lambda (headline contents info)
  1530. (org-export-with-backend
  1531. test-back-end headline contents info))))))))))
  1532. (ert-deftest test-org-export/data-with-backend ()
  1533. "Test `org-export-data-with-backend' specifications."
  1534. ;; Error when calling an undefined back-end.
  1535. (should-error (org-export-data-with-backend nil "nil" nil))
  1536. ;; Otherwise, export data recursively, using correct back-end.
  1537. (should
  1538. (equal
  1539. "Success!"
  1540. (org-export-data-with-backend
  1541. '(bold nil "Test")
  1542. (org-export-create-backend
  1543. :transcoders
  1544. '((plain-text . (lambda (text info) "Success"))
  1545. (bold . (lambda (bold contents info) (concat contents "!")))))
  1546. '(:with-emphasize t)))))
  1547. ;;; Export Snippets
  1548. (ert-deftest test-org-export/export-snippet ()
  1549. "Test export snippets transcoding."
  1550. ;; Standard test.
  1551. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1552. (let ((backend (org-test-default-backend)))
  1553. (setf (org-export-backend-name backend) 'test)
  1554. (setf (org-export-backend-transcoders backend)
  1555. (cons (cons 'export-snippet
  1556. (lambda (snippet contents info)
  1557. (when (eq (org-export-snippet-backend snippet) 'test)
  1558. (org-element-property :value snippet))))
  1559. (org-export-backend-transcoders backend)))
  1560. (let ((org-export-snippet-translation-alist nil))
  1561. (should (equal (org-export-as backend) "A\n")))
  1562. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1563. (should (equal (org-export-as backend) "AB\n")))))
  1564. ;; Ignored export snippets do not remove any blank.
  1565. (should
  1566. (equal "begin end\n"
  1567. (org-test-with-parsed-data "begin @@test:A@@ end"
  1568. (org-export-data-with-backend
  1569. tree
  1570. (org-export-create-backend
  1571. :transcoders
  1572. '((paragraph . (lambda (paragraph contents info) contents))
  1573. (section . (lambda (section contents info) contents))))
  1574. info)))))
  1575. ;;; Footnotes
  1576. (ert-deftest test-org-export/footnote-first-reference-p ()
  1577. "Test `org-export-footnote-first-reference-p' specifications."
  1578. (should
  1579. (equal
  1580. '(t nil)
  1581. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1582. (let (result)
  1583. (org-export-as
  1584. (org-export-create-backend
  1585. :transcoders
  1586. `(,(cons 'footnote-reference
  1587. (lambda (f c i)
  1588. (push (org-export-footnote-first-reference-p f info)
  1589. result)
  1590. ""))
  1591. (section . (lambda (s c i) c))
  1592. (paragraph . (lambda (p c i) c))))
  1593. nil nil nil '(:with-footnotes t))
  1594. (nreverse result)))))
  1595. ;; Limit check to DATA, when non-nil.
  1596. (should
  1597. (equal
  1598. '(nil t)
  1599. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1600. (let (result)
  1601. (org-element-map tree 'footnote-reference
  1602. (lambda (ref)
  1603. (push
  1604. (org-export-footnote-first-reference-p
  1605. ref info (org-element-map tree 'headline #'identity info t))
  1606. result))
  1607. info)
  1608. (nreverse result)))))
  1609. (should
  1610. (equal
  1611. '(t nil)
  1612. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1613. (let (result)
  1614. (org-element-map tree 'footnote-reference
  1615. (lambda (ref)
  1616. (push (org-export-footnote-first-reference-p ref info) result))
  1617. info)
  1618. (nreverse result)))))
  1619. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1620. ;; in the main body of the document. Otherwise, enter footnote
  1621. ;; definitions when they are encountered.
  1622. (should
  1623. (equal
  1624. '(t nil)
  1625. (org-test-with-temp-text
  1626. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1627. (let (result)
  1628. (org-export-as
  1629. (org-export-create-backend
  1630. :transcoders
  1631. `(,(cons 'footnote-reference
  1632. (lambda (f c i)
  1633. (when (org-element-lineage f '(drawer))
  1634. (push (org-export-footnote-first-reference-p f info nil)
  1635. result))
  1636. ""))
  1637. (drawer . (lambda (d c i) c))
  1638. (footnote-definition . (lambda (d c i) c))
  1639. (section . (lambda (s c i) c))
  1640. (paragraph . (lambda (p c i) c))))
  1641. nil nil nil '(:with-footnotes t))
  1642. (nreverse result)))))
  1643. (should
  1644. (equal
  1645. '(t t)
  1646. (org-test-with-temp-text
  1647. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1648. (let (result)
  1649. (org-export-as
  1650. (org-export-create-backend
  1651. :transcoders
  1652. `(,(cons 'footnote-reference
  1653. (lambda (f c i)
  1654. (when (org-element-lineage f '(drawer))
  1655. (push (org-export-footnote-first-reference-p f info nil t)
  1656. result))
  1657. ""))
  1658. (drawer . (lambda (d c i) c))
  1659. (footnote-definition . (lambda (d c i) c))
  1660. (section . (lambda (s c i) c))
  1661. (paragraph . (lambda (p c i) c))))
  1662. nil nil nil '(:with-footnotes t))
  1663. (nreverse result))))))
  1664. (ert-deftest test-org-export/get-footnote-number ()
  1665. "Test `org-export-get-footnote-number' specifications."
  1666. (should
  1667. (equal '(1 2 1)
  1668. (org-test-with-parsed-data
  1669. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1670. (org-element-map tree 'footnote-reference
  1671. (lambda (ref) (org-export-get-footnote-number ref info))
  1672. info))))
  1673. ;; Anonymous footnotes all get a new number.
  1674. (should
  1675. (equal '(1 2)
  1676. (org-test-with-parsed-data
  1677. "Text[fn::anon1][fn::anon2]"
  1678. (org-element-map tree 'footnote-reference
  1679. (lambda (ref) (org-export-get-footnote-number ref info))
  1680. info))))
  1681. ;; Test nested footnotes order.
  1682. (should
  1683. (equal
  1684. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
  1685. (org-test-with-parsed-data
  1686. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1687. (org-element-map tree 'footnote-reference
  1688. (lambda (ref)
  1689. (cons (org-export-get-footnote-number ref info)
  1690. (org-element-property :label ref)))
  1691. info))))
  1692. ;; Limit number to provided DATA, when non-nil.
  1693. (should
  1694. (equal
  1695. '(1)
  1696. (org-test-with-parsed-data
  1697. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1698. (org-element-map tree 'footnote-reference
  1699. (lambda (ref)
  1700. (org-export-get-footnote-number
  1701. ref info (org-element-map tree 'headline #'identity info t)))
  1702. info))))
  1703. (should
  1704. (equal
  1705. '(1 2)
  1706. (org-test-with-parsed-data
  1707. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1708. (org-element-map tree 'footnote-reference
  1709. (lambda (ref) (org-export-get-footnote-number ref info))
  1710. info))))
  1711. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1712. ;; then footnote definitions.
  1713. (should
  1714. (equal
  1715. '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
  1716. (org-test-with-parsed-data
  1717. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1718. (org-element-map tree 'footnote-reference
  1719. (lambda (ref)
  1720. (cons (org-element-property :label ref)
  1721. (org-export-get-footnote-number ref info nil t)))
  1722. info))))
  1723. (should
  1724. (equal
  1725. '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
  1726. (org-test-with-parsed-data
  1727. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1728. (org-element-map tree 'footnote-reference
  1729. (lambda (ref)
  1730. (cons (org-element-property :label ref)
  1731. (org-export-get-footnote-number ref info nil)))
  1732. info)))))
  1733. (ert-deftest test-org-export/collect-footnote-definitions ()
  1734. "Test `org-export-collect-footnote-definitions' specifications."
  1735. (should
  1736. (= 4
  1737. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1738. \[fn:2] B [fn:3] [fn::D].
  1739. \[fn:3] C."
  1740. (length (org-export-collect-footnote-definitions info)))))
  1741. ;; Limit number to provided DATA, when non-nil.
  1742. (should
  1743. (equal
  1744. '((1 "fn:2"))
  1745. (org-test-with-parsed-data
  1746. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1747. (mapcar #'butlast
  1748. (org-export-collect-footnote-definitions
  1749. info (org-element-map tree 'headline #'identity info t))))))
  1750. (should
  1751. (equal
  1752. '((1 "fn:1") (2 "fn:2"))
  1753. (org-test-with-parsed-data
  1754. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1755. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  1756. ;; With optional argument BODY-FIRST, first check body, then
  1757. ;; footnote definitions.
  1758. (should
  1759. (equal '("fn:1" "fn:3" "fn:2" nil)
  1760. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1761. \[fn:2] B [fn:3] [fn::D].
  1762. \[fn:3] C."
  1763. (mapcar (lambda (e) (nth 1 e))
  1764. (org-export-collect-footnote-definitions info nil t)))))
  1765. (should-not
  1766. (equal '("fn:1" "fn:3" "fn:2" nil)
  1767. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1768. \[fn:2] B [fn:3] [fn::D].
  1769. \[fn:3] C."
  1770. (mapcar (lambda (e) (nth 1 e))
  1771. (org-export-collect-footnote-definitions info))))))
  1772. (ert-deftest test-org-export/footnotes ()
  1773. "Miscellaneous tests on footnotes."
  1774. (let ((org-footnote-section nil)
  1775. (org-export-with-footnotes t))
  1776. ;; Read every type of footnote.
  1777. (should
  1778. (equal
  1779. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1780. (org-test-with-parsed-data
  1781. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1782. (org-element-map tree 'footnote-reference
  1783. (lambda (ref)
  1784. (let ((def (org-export-get-footnote-definition ref info)))
  1785. (cons (org-export-get-footnote-number ref info)
  1786. (if (eq (org-element-property :type ref) 'inline) (car def)
  1787. (car (org-element-contents
  1788. (car (org-element-contents def))))))))
  1789. info))))
  1790. ;; Test nested footnote in invisible definitions.
  1791. (should
  1792. (= 2
  1793. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1794. (narrow-to-region (point) (line-end-position))
  1795. (catch 'exit
  1796. (org-export-as
  1797. (org-export-create-backend
  1798. :transcoders
  1799. '((section
  1800. .
  1801. (lambda (s c i)
  1802. (throw 'exit (length
  1803. (org-export-collect-footnote-definitions
  1804. i))))))))))))
  1805. ;; Test export of footnotes defined outside parsing scope.
  1806. (should
  1807. (equal
  1808. "ParagraphOut of scope\n"
  1809. (org-test-with-temp-text "[fn:1] Out of scope
  1810. * Title
  1811. <point>Paragraph[fn:1]"
  1812. (let ((backend (org-test-default-backend)))
  1813. (setf (org-export-backend-transcoders backend)
  1814. (append
  1815. (list (cons 'footnote-reference
  1816. (lambda (fn contents info)
  1817. (org-element-interpret-data
  1818. (org-export-get-footnote-definition fn info))))
  1819. (cons 'footnote-definition #'ignore)
  1820. (cons 'headline #'ignore))
  1821. (org-export-backend-transcoders backend)))
  1822. (org-export-as backend 'subtree)))))
  1823. ;; Footnotes without a definition should throw an error.
  1824. (should-error
  1825. (org-test-with-parsed-data "Text[fn:1]"
  1826. (org-export-get-footnote-definition
  1827. (org-element-map tree 'footnote-reference #'identity info t) info)))
  1828. ;; Footnote section should be ignored in TOC and in headlines
  1829. ;; numbering.
  1830. (should
  1831. (= 1 (let ((org-footnote-section "Footnotes"))
  1832. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1833. (org-export-collect-headlines info))))))
  1834. (should
  1835. (equal '(2)
  1836. (let ((org-footnote-section "Footnotes"))
  1837. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1838. (org-element-map tree 'headline
  1839. (lambda (hl)
  1840. (when (equal (org-element-property :raw-value hl) "H2")
  1841. (org-export-get-headline-number hl info)))
  1842. info t)))))))
  1843. ;;; Headlines and Inlinetasks
  1844. (ert-deftest test-org-export/get-relative-level ()
  1845. "Test `org-export-get-relative-level' specifications."
  1846. ;; Standard test.
  1847. (should
  1848. (equal '(1 2)
  1849. (let ((org-odd-levels-only nil))
  1850. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1851. (org-element-map tree 'headline
  1852. (lambda (h) (org-export-get-relative-level h info))
  1853. info)))))
  1854. ;; Missing levels
  1855. (should
  1856. (equal '(1 3)
  1857. (let ((org-odd-levels-only nil))
  1858. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1859. (org-element-map tree 'headline
  1860. (lambda (h) (org-export-get-relative-level h info))
  1861. info))))))
  1862. (ert-deftest test-org-export/low-level-p ()
  1863. "Test `org-export-low-level-p' specifications."
  1864. (should
  1865. (equal
  1866. '(no yes)
  1867. (let ((org-odd-levels-only nil))
  1868. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1869. (org-element-map tree 'headline
  1870. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1871. (plist-put info :headline-levels 1)))))))
  1872. (ert-deftest test-org-export/get-headline-number ()
  1873. "Test `org-export-get-headline-number' specifications."
  1874. ;; Standard test.
  1875. (should
  1876. (equal
  1877. '((1) (1 1))
  1878. (let ((org-odd-levels-only nil))
  1879. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1880. (org-element-map tree 'headline
  1881. (lambda (h) (org-export-get-headline-number h info))
  1882. info)))))
  1883. ;; Missing levels are replaced with 0.
  1884. (should
  1885. (equal
  1886. '((1) (1 0 1))
  1887. (let ((org-odd-levels-only nil))
  1888. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1889. (org-element-map tree 'headline
  1890. (lambda (h) (org-export-get-headline-number h info))
  1891. info))))))
  1892. (ert-deftest test-org-export/numbered-headline-p ()
  1893. "Test `org-export-numbered-headline-p' specifications."
  1894. ;; If `:section-numbers' is nil, never number headlines.
  1895. (should-not
  1896. (org-test-with-parsed-data "* Headline"
  1897. (org-element-map tree 'headline
  1898. (lambda (h) (org-export-numbered-headline-p h info))
  1899. (plist-put info :section-numbers nil))))
  1900. ;; If `:section-numbers' is a number, only number headlines with
  1901. ;; a level greater that it.
  1902. (should
  1903. (equal
  1904. '(yes no)
  1905. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1906. (org-element-map tree 'headline
  1907. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1908. (plist-put info :section-numbers 1)))))
  1909. ;; Otherwise, headlines are always numbered.
  1910. (should
  1911. (org-test-with-parsed-data "* Headline"
  1912. (org-element-map tree 'headline
  1913. (lambda (h) (org-export-numbered-headline-p h info))
  1914. (plist-put info :section-numbers t))))
  1915. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  1916. (should-not
  1917. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  1918. (org-export-numbered-headline-p
  1919. (org-element-map tree 'headline 'identity info t)
  1920. info)))
  1921. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  1922. (should-not
  1923. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  1924. (org-export-numbered-headline-p
  1925. (org-element-map tree 'headline #'identity info t)
  1926. info)))
  1927. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  1928. ;; ancestors disables numbering.
  1929. (should
  1930. (org-test-with-parsed-data
  1931. "* H
  1932. :PROPERTIES:
  1933. :UNNUMBERED: t
  1934. :END:
  1935. ** H2
  1936. :PROPERTIES:
  1937. :UNNUMBERED: nil
  1938. :END:
  1939. *** H3"
  1940. (org-every
  1941. (lambda (h) (not (org-export-numbered-headline-p h info)))
  1942. (org-element-map tree 'headline #'identity info)))))
  1943. (ert-deftest test-org-export/number-to-roman ()
  1944. "Test `org-export-number-to-roman' specifications."
  1945. ;; If number is negative, return it as a string.
  1946. (should (equal (org-export-number-to-roman -1) "-1"))
  1947. ;; Otherwise, return it as a roman number.
  1948. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1949. (ert-deftest test-org-export/get-optional-title ()
  1950. "Test `org-export-get-alt-title' specifications."
  1951. ;; If ALT_TITLE property is defined, use it.
  1952. (should
  1953. (equal '("opt")
  1954. (org-test-with-parsed-data
  1955. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1956. (org-export-get-alt-title
  1957. (org-element-map tree 'headline 'identity info t)
  1958. info))))
  1959. ;; Otherwise, fall-back to regular title.
  1960. (should
  1961. (equal '("Headline")
  1962. (org-test-with-parsed-data "* Headline"
  1963. (org-export-get-alt-title
  1964. (org-element-map tree 'headline 'identity info t)
  1965. info)))))
  1966. (ert-deftest test-org-export/get-tags ()
  1967. "Test `org-export-get-tags' specifications."
  1968. (let ((org-export-exclude-tags '("noexport"))
  1969. (org-export-select-tags '("export")))
  1970. ;; Standard test: tags which are not a select tag, an exclude tag,
  1971. ;; or specified as optional argument shouldn't be ignored.
  1972. (should
  1973. (org-test-with-parsed-data "* Headline :tag:"
  1974. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1975. info)))
  1976. ;; Exclude tags are removed.
  1977. (should-not
  1978. (org-test-with-parsed-data "* Headline :noexport:"
  1979. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1980. info)))
  1981. ;; Select tags are removed.
  1982. (should-not
  1983. (org-test-with-parsed-data "* Headline :export:"
  1984. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1985. info)))
  1986. (should
  1987. (equal
  1988. '("tag")
  1989. (org-test-with-parsed-data "* Headline :tag:export:"
  1990. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1991. info))))
  1992. ;; Tags provided in the optional argument are also ignored.
  1993. (should-not
  1994. (org-test-with-parsed-data "* Headline :ignore:"
  1995. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1996. info '("ignore"))))
  1997. ;; Allow tag inheritance.
  1998. (should
  1999. (equal
  2000. '(("tag") ("tag"))
  2001. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2002. (org-element-map tree 'headline
  2003. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2004. ;; Tag inheritance checks FILETAGS keywords.
  2005. (should
  2006. (equal
  2007. '(("a" "b" "tag"))
  2008. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2009. (org-element-map tree 'headline
  2010. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  2011. (ert-deftest test-org-export/get-node-property ()
  2012. "Test`org-export-get-node-property' specifications."
  2013. ;; Standard test.
  2014. (should
  2015. (equal "value"
  2016. (org-test-with-parsed-data "* Headline
  2017. :PROPERTIES:
  2018. :prop: value
  2019. :END:"
  2020. (org-export-get-node-property
  2021. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2022. ;; Test inheritance.
  2023. (should
  2024. (equal "value"
  2025. (org-test-with-parsed-data "* Parent
  2026. :PROPERTIES:
  2027. :prop: value
  2028. :END:
  2029. ** Headline
  2030. Paragraph"
  2031. (org-export-get-node-property
  2032. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2033. ;; Cannot return a value before the first headline.
  2034. (should-not
  2035. (org-test-with-parsed-data "Paragraph
  2036. * Headline
  2037. :PROPERTIES:
  2038. :prop: value
  2039. :END:"
  2040. (org-export-get-node-property
  2041. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2042. (ert-deftest test-org-export/get-category ()
  2043. "Test `org-export-get-category' specifications."
  2044. ;; Standard test.
  2045. (should
  2046. (equal "value"
  2047. (org-test-with-parsed-data "* Headline
  2048. :PROPERTIES:
  2049. :CATEGORY: value
  2050. :END:"
  2051. (org-export-get-category
  2052. (org-element-map tree 'headline 'identity nil t) info))))
  2053. ;; Test inheritance from a parent headline.
  2054. (should
  2055. (equal '("value" "value")
  2056. (org-test-with-parsed-data "* Headline1
  2057. :PROPERTIES:
  2058. :CATEGORY: value
  2059. :END:
  2060. ** Headline2"
  2061. (org-element-map tree 'headline
  2062. (lambda (hl) (org-export-get-category hl info)) info))))
  2063. ;; Test inheritance from #+CATEGORY keyword
  2064. (should
  2065. (equal "value"
  2066. (org-test-with-parsed-data "#+CATEGORY: value
  2067. * Headline"
  2068. (org-export-get-category
  2069. (org-element-map tree 'headline 'identity nil t) info))))
  2070. ;; Test inheritance from file name.
  2071. (should
  2072. (equal "test"
  2073. (org-test-with-parsed-data "* Headline"
  2074. (let ((info (plist-put info :input-file "~/test.org")))
  2075. (org-export-get-category
  2076. (org-element-map tree 'headline 'identity nil t) info)))))
  2077. ;; Fall-back value.
  2078. (should
  2079. (equal "???"
  2080. (org-test-with-parsed-data "* Headline"
  2081. (org-export-get-category
  2082. (org-element-map tree 'headline 'identity nil t) info)))))
  2083. (ert-deftest test-org-export/first-sibling-p ()
  2084. "Test `org-export-first-sibling-p' specifications."
  2085. ;; Standard test.
  2086. (should
  2087. (equal
  2088. '(yes yes no)
  2089. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2090. (org-element-map tree 'headline
  2091. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2092. info))))
  2093. (should
  2094. (equal '(yes no)
  2095. (org-test-with-parsed-data "- item\n\n para"
  2096. (org-element-map tree 'paragraph
  2097. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2098. info))))
  2099. ;; Ignore sections for headlines.
  2100. (should
  2101. (equal '(yes yes)
  2102. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2103. (org-element-map tree 'headline
  2104. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2105. info))))
  2106. ;; Ignore headlines not exported.
  2107. (should
  2108. (equal
  2109. '(yes)
  2110. (let ((org-export-exclude-tags '("ignore")))
  2111. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2112. (org-element-map tree 'headline
  2113. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2114. info))))))
  2115. (ert-deftest test-org-export/last-sibling-p ()
  2116. "Test `org-export-last-sibling-p' specifications."
  2117. ;; Standard test.
  2118. (should
  2119. (equal
  2120. '(yes no yes)
  2121. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2122. (org-element-map tree 'headline
  2123. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2124. info))))
  2125. (should
  2126. (equal '(no yes)
  2127. (org-test-with-parsed-data "- item\n\n para"
  2128. (org-element-map tree 'paragraph
  2129. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2130. info))))
  2131. ;; Ignore headlines not exported.
  2132. (should
  2133. (equal
  2134. '(yes)
  2135. (let ((org-export-exclude-tags '("ignore")))
  2136. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2137. (org-element-map tree 'headline
  2138. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2139. info))))))
  2140. (ert-deftest test-org-export/handle-inlinetasks ()
  2141. "Test inlinetask export."
  2142. ;; Inlinetask with an exclude tag.
  2143. (when (featurep 'org-inlinetask)
  2144. (should
  2145. (equal
  2146. ""
  2147. (let ((org-inlinetask-min-level 3)
  2148. org-export-filter-body-functions
  2149. org-export-filter-final-output-functions)
  2150. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2151. (org-export-as (org-test-default-backend)
  2152. nil nil nil '(:exclude-tags ("noexp")))))))
  2153. ;; Inlinetask with an include tag.
  2154. (should
  2155. (equal
  2156. "* H2\n*** Inline :exp:\n"
  2157. (let ((org-inlinetask-min-level 3)
  2158. (org-tags-column 0))
  2159. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2160. (org-export-as (org-test-default-backend)
  2161. nil nil nil '(:select-tags ("exp")))))))
  2162. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2163. (should
  2164. (equal ""
  2165. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2166. (org-inlinetask-min-level 3)
  2167. org-export-filter-body-functions
  2168. org-export-filter-final-output-functions)
  2169. (org-test-with-temp-text "*** TODO Inline"
  2170. (org-export-as (org-test-default-backend)
  2171. nil nil nil '(:with-tasks nil))))))))
  2172. ;;; Keywords
  2173. (ert-deftest test-org-export/get-date ()
  2174. "Test `org-export-get-date' specifications."
  2175. ;; Return a properly formatted string when
  2176. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2177. ;; consists in a single timestamp.
  2178. (should
  2179. (equal "29 03 2012"
  2180. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2181. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2182. (org-export-get-date info)))))
  2183. ;; Return a secondary string otherwise.
  2184. (should-not
  2185. (stringp
  2186. (let ((org-export-date-timestamp-format nil))
  2187. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2188. (org-export-get-date info)))))
  2189. (should
  2190. (equal '("Date")
  2191. (org-test-with-parsed-data "#+DATE: Date"
  2192. (org-export-get-date info))))
  2193. ;; Optional argument has precedence over
  2194. ;; `org-export-date-timestamp-format'.
  2195. (should
  2196. (equal "29 03"
  2197. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2198. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2199. (org-export-get-date info "%d %m"))))))
  2200. ;;; Links
  2201. (ert-deftest test-org-export/custom-protocol-maybe ()
  2202. "Test `org-export-custom-protocol-maybe' specifications."
  2203. (should
  2204. (string-match
  2205. "success"
  2206. (let ((org-link-types (copy-sequence org-link-types)))
  2207. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  2208. (org-export-string-as
  2209. "[[foo:path]]"
  2210. (org-export-create-backend
  2211. :name 'test
  2212. :transcoders
  2213. '((section . (lambda (s c i) c))
  2214. (paragraph . (lambda (p c i) c))
  2215. (link . (lambda (l c i)
  2216. (or (org-export-custom-protocol-maybe l c 'test)
  2217. "failure")))))))))
  2218. (should-not
  2219. (string-match
  2220. "success"
  2221. (let ((org-link-types (copy-sequence org-link-types)))
  2222. (org-add-link-type
  2223. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2224. (org-export-string-as
  2225. "[[foo:path]]"
  2226. (org-export-create-backend
  2227. :name 'no-test
  2228. :transcoders
  2229. '((section . (lambda (s c i) c))
  2230. (paragraph . (lambda (p c i) c))
  2231. (link . (lambda (l c i)
  2232. (or (org-export-custom-protocol-maybe l c 'no-test)
  2233. "failure")))))))))
  2234. ;; Ignore anonymous back-ends.
  2235. (should-not
  2236. (string-match
  2237. "success"
  2238. (let ((org-link-types (copy-sequence org-link-types)))
  2239. (org-add-link-type
  2240. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2241. (org-export-string-as
  2242. "[[foo:path]]"
  2243. (org-export-create-backend
  2244. :transcoders
  2245. '((section . (lambda (s c i) c))
  2246. (paragraph . (lambda (p c i) c))
  2247. (link . (lambda (l c i)
  2248. (or (org-export-custom-protocol-maybe l c nil)
  2249. "failure"))))))))))
  2250. (ert-deftest test-org-export/get-coderef-format ()
  2251. "Test `org-export-get-coderef-format' specifications."
  2252. ;; A link without description returns "%s"
  2253. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2254. "%s"))
  2255. ;; Return "%s" when path is matched within description.
  2256. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2257. "desc %s"))
  2258. ;; Otherwise return description.
  2259. (should (equal (org-export-get-coderef-format "path" "desc")
  2260. "desc")))
  2261. (ert-deftest test-org-export/inline-image-p ()
  2262. "Test `org-export-inline-image-p' specifications."
  2263. (should
  2264. (org-export-inline-image-p
  2265. (org-test-with-temp-text "[[#id]]"
  2266. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2267. '(("custom-id" . "id")))))
  2268. (ert-deftest test-org-export/fuzzy-link ()
  2269. "Test fuzzy links specifications."
  2270. ;; Link to an headline should return headline's number.
  2271. (org-test-with-parsed-data
  2272. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2273. (should
  2274. ;; Note: Headline's number is in fact a list of numbers.
  2275. (equal '(2)
  2276. (org-element-map tree 'link
  2277. (lambda (link)
  2278. (org-export-get-ordinal
  2279. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2280. ;; Link to a target in an item should return item's number.
  2281. (org-test-with-parsed-data
  2282. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2283. (should
  2284. ;; Note: Item's number is in fact a list of numbers.
  2285. (equal '(1 2)
  2286. (org-element-map tree 'link
  2287. (lambda (link)
  2288. (org-export-get-ordinal
  2289. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2290. ;; Link to a target in a footnote should return footnote's number.
  2291. (org-test-with-parsed-data "
  2292. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2293. (should
  2294. (equal '(2 3)
  2295. (org-element-map tree 'link
  2296. (lambda (link)
  2297. (org-export-get-ordinal
  2298. (org-export-resolve-fuzzy-link link info) info)) info))))
  2299. ;; Link to a named element should return sequence number of that
  2300. ;; element.
  2301. (org-test-with-parsed-data
  2302. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2303. (should
  2304. (= 2
  2305. (org-element-map tree 'link
  2306. (lambda (link)
  2307. (org-export-get-ordinal
  2308. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2309. ;; Link to a target not within an item, a table, a footnote
  2310. ;; reference or definition should return section number.
  2311. (org-test-with-parsed-data
  2312. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2313. (should
  2314. (equal '(2)
  2315. (org-element-map tree 'link
  2316. (lambda (link)
  2317. (org-export-get-ordinal
  2318. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2319. ;; Space are not significant when matching a fuzzy link.
  2320. (should
  2321. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2322. (org-element-map tree 'link
  2323. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2324. info t)))
  2325. ;; Statistics cookies are ignored for headline match.
  2326. (should
  2327. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2328. (org-element-map tree 'link
  2329. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2330. info t)))
  2331. (should
  2332. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2333. (org-element-map tree 'link
  2334. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2335. info t))))
  2336. (ert-deftest test-org-export/resolve-coderef ()
  2337. "Test `org-export-resolve-coderef' specifications."
  2338. (let ((org-coderef-label-format "(ref:%s)"))
  2339. ;; A link to a "-n -k -r" block returns line number.
  2340. (should
  2341. (= 1
  2342. (org-test-with-parsed-data
  2343. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2344. (org-export-resolve-coderef "coderef" info))))
  2345. (should
  2346. (= 1
  2347. (org-test-with-parsed-data
  2348. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2349. (org-export-resolve-coderef "coderef" info))))
  2350. ;; A link to a "-n -r" block returns line number.
  2351. (should
  2352. (= 1
  2353. (org-test-with-parsed-data
  2354. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2355. (org-export-resolve-coderef "coderef" info))))
  2356. (should
  2357. (= 1
  2358. (org-test-with-parsed-data
  2359. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2360. (org-export-resolve-coderef "coderef" info))))
  2361. ;; A link to a "-n" block returns coderef.
  2362. (should
  2363. (equal "coderef"
  2364. (org-test-with-parsed-data
  2365. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2366. (org-export-resolve-coderef "coderef" info))))
  2367. (should
  2368. (equal "coderef"
  2369. (org-test-with-parsed-data
  2370. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2371. (org-export-resolve-coderef "coderef" info))))
  2372. ;; A link to a "-r" block returns line number.
  2373. (should
  2374. (= 1
  2375. (org-test-with-parsed-data
  2376. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2377. (org-export-resolve-coderef "coderef" info))))
  2378. (should
  2379. (= 1
  2380. (org-test-with-parsed-data
  2381. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2382. (org-export-resolve-coderef "coderef" info))))
  2383. ;; A link to a block without a switch returns coderef.
  2384. (should
  2385. (equal "coderef"
  2386. (org-test-with-parsed-data
  2387. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2388. (org-export-resolve-coderef "coderef" info))))
  2389. (org-test-with-parsed-data
  2390. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2391. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2392. ;; Correctly handle continued line numbers. A "+n" switch should
  2393. ;; resume numbering from previous block with numbered lines,
  2394. ;; ignoring blocks not numbering lines in the process. A "-n"
  2395. ;; switch resets count.
  2396. (should
  2397. (equal '(2 1)
  2398. (org-test-with-parsed-data "
  2399. #+BEGIN_EXAMPLE -n
  2400. Text.
  2401. #+END_EXAMPLE
  2402. #+BEGIN_SRC emacs-lisp
  2403. \(- 1 1)
  2404. #+END_SRC
  2405. #+BEGIN_SRC emacs-lisp +n -r
  2406. \(+ 1 1) (ref:addition)
  2407. #+END_SRC
  2408. #+BEGIN_EXAMPLE -n -r
  2409. Another text. (ref:text)
  2410. #+END_EXAMPLE"
  2411. (list (org-export-resolve-coderef "addition" info)
  2412. (org-export-resolve-coderef "text" info)))))
  2413. ;; Recognize coderef with user-specified syntax.
  2414. (should
  2415. (equal "text"
  2416. (org-test-with-parsed-data
  2417. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2418. (org-export-resolve-coderef "text" info))))
  2419. ;; Unresolved coderefs throw an error.
  2420. (should-error
  2421. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2422. (org-export-resolve-coderef "unknown" info)))))
  2423. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2424. "Test `org-export-resolve-fuzzy-link' specifications."
  2425. ;; Match target objects.
  2426. (should
  2427. (org-test-with-parsed-data "<<target>> [[target]]"
  2428. (org-export-resolve-fuzzy-link
  2429. (org-element-map tree 'link 'identity info t) info)))
  2430. ;; Match named elements.
  2431. (should
  2432. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2433. (org-export-resolve-fuzzy-link
  2434. (org-element-map tree 'link 'identity info t) info)))
  2435. ;; Match exact headline's name.
  2436. (should
  2437. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2438. (org-export-resolve-fuzzy-link
  2439. (org-element-map tree 'link 'identity info t) info)))
  2440. ;; Targets objects have priority over named elements and headline
  2441. ;; titles.
  2442. (should
  2443. (eq 'target
  2444. (org-test-with-parsed-data
  2445. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  2446. (org-element-type
  2447. (org-export-resolve-fuzzy-link
  2448. (org-element-map tree 'link 'identity info t) info)))))
  2449. ;; Named elements have priority over headline titles.
  2450. (should
  2451. (eq 'paragraph
  2452. (org-test-with-parsed-data
  2453. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2454. (org-element-type
  2455. (org-export-resolve-fuzzy-link
  2456. (org-element-map tree 'link 'identity info t) info)))))
  2457. ;; If link's path starts with a "*", only match headline titles,
  2458. ;; though.
  2459. (should
  2460. (eq 'headline
  2461. (org-test-with-parsed-data
  2462. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2463. (org-element-type
  2464. (org-export-resolve-fuzzy-link
  2465. (org-element-map tree 'link 'identity info t) info)))))
  2466. ;; Error if no match.
  2467. (should-error
  2468. (org-test-with-parsed-data "[[target]]"
  2469. (org-export-resolve-fuzzy-link
  2470. (org-element-map tree 'link 'identity info t) info)))
  2471. ;; Match fuzzy link even when before first headline.
  2472. (should
  2473. (eq 'headline
  2474. (org-test-with-parsed-data "[[hl]]\n* hl"
  2475. (org-element-type
  2476. (org-export-resolve-fuzzy-link
  2477. (org-element-map tree 'link 'identity info t) info)))))
  2478. ;; Handle url-encoded fuzzy links.
  2479. (should
  2480. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  2481. (org-export-resolve-fuzzy-link
  2482. (org-element-map tree 'link #'identity info t) info))))
  2483. (ert-deftest test-org-export/resolve-id-link ()
  2484. "Test `org-export-resolve-id-link' specifications."
  2485. ;; Regular test for custom-id link.
  2486. (should
  2487. (equal '("Headline1")
  2488. (org-test-with-parsed-data "* Headline1
  2489. :PROPERTIES:
  2490. :CUSTOM_ID: test
  2491. :END:
  2492. * Headline 2
  2493. \[[#test]]"
  2494. (org-element-property
  2495. :title
  2496. (org-export-resolve-id-link
  2497. (org-element-map tree 'link 'identity info t) info)))))
  2498. ;; Throw an error on failing searches.
  2499. (should-error
  2500. (org-test-with-parsed-data "* Headline1
  2501. :PROPERTIES:
  2502. :CUSTOM_ID: test
  2503. :END:
  2504. * Headline 2
  2505. \[[#no-match]]"
  2506. (org-export-resolve-id-link
  2507. (org-element-map tree 'link 'identity info t) info)))
  2508. ;; Test for internal id target.
  2509. (should
  2510. (equal '("Headline1")
  2511. (org-test-with-parsed-data "* Headline1
  2512. :PROPERTIES:
  2513. :ID: aaaa
  2514. :END:
  2515. * Headline 2
  2516. \[[id:aaaa]]"
  2517. (org-element-property
  2518. :title
  2519. (org-export-resolve-id-link
  2520. (org-element-map tree 'link 'identity info t) info)))))
  2521. ;; Test for external id target.
  2522. (should
  2523. (equal
  2524. "external-file"
  2525. (org-test-with-parsed-data "[[id:aaaa]]"
  2526. (org-export-resolve-id-link
  2527. (org-element-map tree 'link 'identity info t)
  2528. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  2529. (ert-deftest test-org-export/resolve-radio-link ()
  2530. "Test `org-export-resolve-radio-link' specifications."
  2531. ;; Standard test.
  2532. (should
  2533. (org-test-with-temp-text "<<<radio>>> radio"
  2534. (org-update-radio-target-regexp)
  2535. (let* ((tree (org-element-parse-buffer))
  2536. (info `(:parse-tree ,tree)))
  2537. (org-export-resolve-radio-link
  2538. (org-element-map tree 'link 'identity info t)
  2539. info))))
  2540. ;; Radio targets are case-insensitive.
  2541. (should
  2542. (org-test-with-temp-text "<<<RADIO>>> radio"
  2543. (org-update-radio-target-regexp)
  2544. (let* ((tree (org-element-parse-buffer))
  2545. (info `(:parse-tree ,tree)))
  2546. (org-export-resolve-radio-link
  2547. (org-element-map tree 'link 'identity info t)
  2548. info))))
  2549. ;; Radio target with objects.
  2550. (should
  2551. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2552. (org-update-radio-target-regexp)
  2553. (let* ((tree (org-element-parse-buffer))
  2554. (info `(:parse-tree ,tree)))
  2555. (org-export-resolve-radio-link
  2556. (org-element-map tree 'link 'identity info t)
  2557. info))))
  2558. ;; Radio target with objects at its beginning.
  2559. (should
  2560. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2561. (org-update-radio-target-regexp)
  2562. (let* ((tree (org-element-parse-buffer))
  2563. (info `(:parse-tree ,tree)))
  2564. (org-export-resolve-radio-link
  2565. (org-element-map tree 'link 'identity info t)
  2566. info))))
  2567. ;; Radio link next to an apostrophe.
  2568. (should
  2569. (org-test-with-temp-text "<<<radio>>> radio's"
  2570. (org-update-radio-target-regexp)
  2571. (let* ((tree (org-element-parse-buffer))
  2572. (info `(:parse-tree ,tree)))
  2573. (org-export-resolve-radio-link
  2574. (org-element-map tree 'link 'identity info t)
  2575. info))))
  2576. ;; Multiple radio targets.
  2577. (should
  2578. (equal '("radio1" "radio2")
  2579. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2580. (org-update-radio-target-regexp)
  2581. (let* ((tree (org-element-parse-buffer))
  2582. (info `(:parse-tree ,tree)))
  2583. (org-element-map tree 'link
  2584. (lambda (link)
  2585. (org-element-property
  2586. :value (org-export-resolve-radio-link link info)))
  2587. info)))))
  2588. ;; Radio target is whitespace insensitive.
  2589. (should
  2590. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2591. (org-update-radio-target-regexp)
  2592. (let* ((tree (org-element-parse-buffer))
  2593. (info `(:parse-tree ,tree)))
  2594. (org-element-map tree 'link
  2595. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2596. (ert-deftest test-org-export/file-uri ()
  2597. "Test `org-export-file-uri' specifications."
  2598. ;; Preserve relative filenames.
  2599. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  2600. ;; Local files start with "file:///"
  2601. (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
  2602. ;; Remote files start with "file://"
  2603. (should (equal "file://myself@some.where:papers/last.pdf"
  2604. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  2605. ;; Expand filename starting with "~".
  2606. (should (equal (org-export-file-uri "~/file.org")
  2607. (concat "file://" (expand-file-name "~/file.org")))))
  2608. ;;; Src-block and example-block
  2609. (ert-deftest test-org-export/unravel-code ()
  2610. "Test `org-export-unravel-code' function."
  2611. ;; Code without reference.
  2612. (should
  2613. (equal '("(+ 1 1)\n")
  2614. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2615. (org-export-unravel-code (org-element-at-point)))))
  2616. ;; Code with reference.
  2617. (should
  2618. (equal '("(+ 1 1)\n" (1 . "test"))
  2619. (org-test-with-temp-text
  2620. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2621. (let ((org-coderef-label-format "(ref:%s)"))
  2622. (org-export-unravel-code (org-element-at-point))))))
  2623. ;; Code with user-defined reference.
  2624. (should
  2625. (equal
  2626. '("(+ 1 1)\n" (1 . "test"))
  2627. (org-test-with-temp-text
  2628. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2629. (let ((org-coderef-label-format "(ref:%s)"))
  2630. (org-export-unravel-code (org-element-at-point))))))
  2631. ;; Code references keys are relative to the current block.
  2632. (should
  2633. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2634. (org-test-with-temp-text "
  2635. #+BEGIN_EXAMPLE -n
  2636. \(+ 1 1)
  2637. #+END_EXAMPLE
  2638. #+BEGIN_EXAMPLE +n
  2639. \(+ 2 2)
  2640. \(+ 3 3) (ref:one)
  2641. #+END_EXAMPLE"
  2642. (goto-line 5)
  2643. (let ((org-coderef-label-format "(ref:%s)"))
  2644. (org-export-unravel-code (org-element-at-point)))))))
  2645. (ert-deftest test-org-export/format-code-default ()
  2646. "Test `org-export-format-code-default' specifications."
  2647. ;; Return the empty string when code is empty.
  2648. (should
  2649. (equal ""
  2650. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2651. (org-export-format-code-default
  2652. (org-element-map tree 'src-block 'identity info t) info))))
  2653. ;; Number lines, two whitespace characters before the actual loc.
  2654. (should
  2655. (equal "1 a\n2 b\n"
  2656. (org-test-with-parsed-data
  2657. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2658. (org-export-format-code-default
  2659. (org-element-map tree 'src-block 'identity info t) info))))
  2660. ;; Put references 6 whitespace characters after the widest line,
  2661. ;; wrapped within parenthesis.
  2662. (should
  2663. (equal "123 (a)\n1 (b)\n"
  2664. (let ((org-coderef-label-format "(ref:%s)"))
  2665. (org-test-with-parsed-data
  2666. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2667. (org-export-format-code-default
  2668. (org-element-map tree 'src-block 'identity info t) info))))))
  2669. ;;; Smart Quotes
  2670. (ert-deftest test-org-export/activate-smart-quotes ()
  2671. "Test `org-export-activate-smart-quotes' specifications."
  2672. ;; Double quotes: standard test.
  2673. (should
  2674. (equal
  2675. '("some &ldquo;quoted&rdquo; text")
  2676. (let ((org-export-default-language "en"))
  2677. (org-test-with-parsed-data "some \"quoted\" text"
  2678. (org-element-map tree 'plain-text
  2679. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2680. info)))))
  2681. ;; Opening quotes: at the beginning of a paragraph.
  2682. (should
  2683. (equal
  2684. '("&ldquo;begin")
  2685. (let ((org-export-default-language "en"))
  2686. (org-test-with-parsed-data "\"begin"
  2687. (org-element-map tree 'plain-text
  2688. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2689. info)))))
  2690. ;; Opening quotes: after an object.
  2691. (should
  2692. (equal
  2693. '("&ldquo;quoted&rdquo; text")
  2694. (let ((org-export-default-language "en"))
  2695. (org-test-with-parsed-data "=verb= \"quoted\" text"
  2696. (org-element-map tree 'plain-text
  2697. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2698. info)))))
  2699. ;; Closing quotes: at the end of a paragraph.
  2700. (should
  2701. (equal
  2702. '("Quoted &ldquo;text&rdquo;")
  2703. (let ((org-export-default-language "en"))
  2704. (org-test-with-parsed-data "Quoted \"text\""
  2705. (org-element-map tree 'plain-text
  2706. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2707. info)))))
  2708. ;; Inner quotes: standard test.
  2709. (should
  2710. (equal '("« outer « inner » outer »")
  2711. (let ((org-export-default-language "fr"))
  2712. (org-test-with-parsed-data "\"outer 'inner' outer\""
  2713. (org-element-map tree 'plain-text
  2714. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2715. info)))))
  2716. ;; Inner quotes: close to special symbols.
  2717. (should
  2718. (equal '("« outer (« inner ») outer »")
  2719. (let ((org-export-default-language "fr"))
  2720. (org-test-with-parsed-data "\"outer ('inner') outer\""
  2721. (org-element-map tree 'plain-text
  2722. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2723. info)))))
  2724. (should
  2725. (equal '("« « inner » »")
  2726. (let ((org-export-default-language "fr"))
  2727. (org-test-with-parsed-data "\"'inner'\""
  2728. (org-element-map tree 'plain-text
  2729. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2730. info)))))
  2731. ;; Apostrophe: standard test.
  2732. (should
  2733. (equal '("It « shouldn’t » fail")
  2734. (let ((org-export-default-language "fr"))
  2735. (org-test-with-parsed-data "It \"shouldn't\" fail"
  2736. (org-element-map tree 'plain-text
  2737. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2738. info)))))
  2739. (should
  2740. (equal '("It shouldn’t fail")
  2741. (let ((org-export-default-language "fr"))
  2742. (org-test-with-parsed-data "It shouldn't fail"
  2743. (org-element-map tree 'plain-text
  2744. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2745. info)))))
  2746. ;; Apostrophe: before an object.
  2747. (should
  2748. (equal
  2749. '("« a’" " »")
  2750. (let ((org-export-default-language "fr"))
  2751. (org-test-with-parsed-data "\"a'=b=\""
  2752. (org-element-map tree 'plain-text
  2753. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2754. info)))))
  2755. ;; Apostrophe: after an object.
  2756. (should
  2757. (equal '("« " "’s »")
  2758. (let ((org-export-default-language "fr"))
  2759. (org-test-with-parsed-data "\"=code='s\""
  2760. (org-element-map tree 'plain-text
  2761. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2762. info)))))
  2763. ;; Special case: isolated quotes.
  2764. (should
  2765. (equal '("&ldquo;" "&rdquo;")
  2766. (let ((org-export-default-language "en"))
  2767. (org-test-with-parsed-data "\"$x$\""
  2768. (org-element-map tree 'plain-text
  2769. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2770. info)))))
  2771. ;; Smart quotes in secondary strings.
  2772. (should
  2773. (equal '("&ldquo;" "&rdquo;")
  2774. (let ((org-export-default-language "en"))
  2775. (org-test-with-parsed-data "* \"$x$\""
  2776. (org-element-map tree 'plain-text
  2777. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2778. info)))))
  2779. ;; Smart quotes in document keywords.
  2780. (should
  2781. (equal '("&ldquo;" "&rdquo;")
  2782. (let ((org-export-default-language "en"))
  2783. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2784. (org-element-map (plist-get info :title) 'plain-text
  2785. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2786. info)))))
  2787. ;; Smart quotes in parsed affiliated keywords.
  2788. (should
  2789. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2790. (let ((org-export-default-language "en"))
  2791. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  2792. (org-element-map tree 'plain-text
  2793. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2794. info nil nil t))))))
  2795. ;;; Tables
  2796. (ert-deftest test-org-export/special-column ()
  2797. "Test if the table's special column is properly recognized."
  2798. ;; 1. First column is special if it contains only a special marking
  2799. ;; characters or empty cells.
  2800. (org-test-with-temp-text "
  2801. | ! | 1 |
  2802. | | 2 |"
  2803. (should
  2804. (org-export-table-has-special-column-p
  2805. (org-element-map
  2806. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2807. ;; 2. If the column contains anything else, it isn't special.
  2808. (org-test-with-temp-text "
  2809. | ! | 1 |
  2810. | b | 2 |"
  2811. (should-not
  2812. (org-export-table-has-special-column-p
  2813. (org-element-map
  2814. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2815. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  2816. ;; and "!".
  2817. (org-test-with-temp-text "
  2818. | # | 1 |
  2819. | ^ | 2 |
  2820. | * | 3 |
  2821. | _ | 4 |
  2822. | / | 5 |
  2823. | $ | 6 |
  2824. | ! | 7 |"
  2825. (should
  2826. (org-export-table-has-special-column-p
  2827. (org-element-map
  2828. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2829. ;; 4. A first column with only empty cells isn't considered as
  2830. ;; special.
  2831. (org-test-with-temp-text "
  2832. | | 1 |
  2833. | | 2 |"
  2834. (should-not
  2835. (org-export-table-has-special-column-p
  2836. (org-element-map
  2837. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  2838. (ert-deftest test-org-export/table-row-is-special-p ()
  2839. "Test `org-export-table-row-is-special-p' specifications."
  2840. ;; 1. A row is special if it has a special marking character in the
  2841. ;; special column.
  2842. (org-test-with-parsed-data "| ! | 1 |"
  2843. (should
  2844. (org-export-table-row-is-special-p
  2845. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2846. ;; 2. A row is special when its first field is "/"
  2847. (org-test-with-parsed-data "
  2848. | / | 1 |
  2849. | a | b |"
  2850. (should
  2851. (org-export-table-row-is-special-p
  2852. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2853. ;; 3. A row only containing alignment cookies is also considered as
  2854. ;; special.
  2855. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  2856. (should
  2857. (org-export-table-row-is-special-p
  2858. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2859. ;; 4. Everything else isn't considered as special.
  2860. (org-test-with-parsed-data "| \alpha | | c |"
  2861. (should-not
  2862. (org-export-table-row-is-special-p
  2863. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2864. ;; 5. Table's rules are never considered as special rows.
  2865. (org-test-with-parsed-data "|---+---|"
  2866. (should-not
  2867. (org-export-table-row-is-special-p
  2868. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  2869. (ert-deftest test-org-export/has-header-p ()
  2870. "Test `org-export-table-has-header-p' specifications."
  2871. ;; 1. With an header.
  2872. (org-test-with-parsed-data "
  2873. | a | b |
  2874. |---+---|
  2875. | c | d |"
  2876. (should
  2877. (org-export-table-has-header-p
  2878. (org-element-map tree 'table 'identity info 'first-match)
  2879. info)))
  2880. ;; 2. Without an header.
  2881. (org-test-with-parsed-data "
  2882. | a | b |
  2883. | c | d |"
  2884. (should-not
  2885. (org-export-table-has-header-p
  2886. (org-element-map tree 'table 'identity info 'first-match)
  2887. info)))
  2888. ;; 3. Don't get fooled with starting and ending rules.
  2889. (org-test-with-parsed-data "
  2890. |---+---|
  2891. | a | b |
  2892. | c | d |
  2893. |---+---|"
  2894. (should-not
  2895. (org-export-table-has-header-p
  2896. (org-element-map tree 'table 'identity info 'first-match)
  2897. info))))
  2898. (ert-deftest test-org-export/table-row-group ()
  2899. "Test `org-export-table-row-group' specifications."
  2900. ;; 1. A rule creates a new group.
  2901. (should
  2902. (equal '(1 rule 2)
  2903. (org-test-with-parsed-data "
  2904. | a | b |
  2905. |---+---|
  2906. | 1 | 2 |"
  2907. (org-element-map tree 'table-row
  2908. (lambda (row)
  2909. (if (eq (org-element-property :type row) 'rule) 'rule
  2910. (org-export-table-row-group row info)))))))
  2911. ;; 2. Special rows are ignored in count.
  2912. (should
  2913. (equal
  2914. '(rule 1)
  2915. (org-test-with-parsed-data "
  2916. | / | < | > |
  2917. |---|---+---|
  2918. | | 1 | 2 |"
  2919. (org-element-map tree 'table-row
  2920. (lambda (row)
  2921. (if (eq (org-element-property :type row) 'rule) 'rule
  2922. (org-export-table-row-group row info)))
  2923. info))))
  2924. ;; 3. Double rules also are ignored in count.
  2925. (should
  2926. (equal '(1 rule rule 2)
  2927. (org-test-with-parsed-data "
  2928. | a | b |
  2929. |---+---|
  2930. |---+---|
  2931. | 1 | 2 |"
  2932. (org-element-map tree 'table-row
  2933. (lambda (row)
  2934. (if (eq (org-element-property :type row) 'rule) 'rule
  2935. (org-export-table-row-group row info))))))))
  2936. (ert-deftest test-org-export/table-row-number ()
  2937. "Test `org-export-table-row-number' specifications."
  2938. ;; Standard test. Number is 0-indexed.
  2939. (should
  2940. (equal '(0 1)
  2941. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  2942. (org-element-map tree 'table-row
  2943. (lambda (row) (org-export-table-row-number row info)) info))))
  2944. ;; Number ignores separators.
  2945. (should
  2946. (equal '(0 1)
  2947. (org-test-with-parsed-data "
  2948. | a | b | c |
  2949. |---+---+---|
  2950. | d | e | f |"
  2951. (org-element-map tree 'table-row
  2952. (lambda (row) (org-export-table-row-number row info)) info))))
  2953. ;; Number ignores special rows.
  2954. (should
  2955. (equal '(0 1)
  2956. (org-test-with-parsed-data "
  2957. | / | < | > |
  2958. | | b | c |
  2959. |---+-----+-----|
  2960. | | <c> | <c> |
  2961. | | e | f |"
  2962. (org-element-map tree 'table-row
  2963. (lambda (row) (org-export-table-row-number row info)) info)))))
  2964. (ert-deftest test-org-export/table-cell-width ()
  2965. "Test `org-export-table-cell-width' specifications."
  2966. ;; 1. Width is primarily determined by width cookies. If no cookie
  2967. ;; is found, cell's width is nil.
  2968. (org-test-with-parsed-data "
  2969. | / | <l> | <6> | <l7> |
  2970. | | a | b | c |"
  2971. (should
  2972. (equal
  2973. '(nil 6 7)
  2974. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2975. (org-element-map tree 'table-cell 'identity info)))))
  2976. ;; 2. The last width cookie has precedence.
  2977. (org-test-with-parsed-data "
  2978. | <6> |
  2979. | <7> |
  2980. | a |"
  2981. (should
  2982. (equal
  2983. '(7)
  2984. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2985. (org-element-map tree 'table-cell 'identity info)))))
  2986. ;; 3. Valid width cookies must have a specific row.
  2987. (org-test-with-parsed-data "| <6> | cell |"
  2988. (should
  2989. (equal
  2990. '(nil nil)
  2991. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2992. (org-element-map tree 'table-cell 'identity))))))
  2993. (ert-deftest test-org-export/table-cell-alignment ()
  2994. "Test `org-export-table-cell-alignment' specifications."
  2995. ;; 1. Alignment is primarily determined by alignment cookies.
  2996. (should
  2997. (equal '(left center right)
  2998. (let ((org-table-number-fraction 0.5)
  2999. (org-table-number-regexp "^[0-9]+$"))
  3000. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3001. (mapcar (lambda (cell)
  3002. (org-export-table-cell-alignment cell info))
  3003. (org-element-map tree 'table-cell 'identity))))))
  3004. ;; 2. The last alignment cookie has precedence.
  3005. (should
  3006. (equal '(right right right)
  3007. (org-test-with-parsed-data "
  3008. | <l8> |
  3009. | cell |
  3010. | <r9> |"
  3011. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3012. (org-element-map tree 'table-cell 'identity)))))
  3013. ;; 3. If there's no cookie, cell's contents determine alignment.
  3014. ;; A column mostly made of cells containing numbers will align
  3015. ;; its cells to the right.
  3016. (should
  3017. (equal '(right right right)
  3018. (let ((org-table-number-fraction 0.5)
  3019. (org-table-number-regexp "^[0-9]+$"))
  3020. (org-test-with-parsed-data "
  3021. | 123 |
  3022. | some text |
  3023. | 12345 |"
  3024. (mapcar (lambda (cell)
  3025. (org-export-table-cell-alignment cell info))
  3026. (org-element-map tree 'table-cell 'identity))))))
  3027. ;; 4. Otherwise, they will be aligned to the left.
  3028. (should
  3029. (equal '(left left left)
  3030. (org-test-with-parsed-data "
  3031. | text |
  3032. | some text |
  3033. | \alpha |"
  3034. (mapcar (lambda (cell)
  3035. (org-export-table-cell-alignment cell info))
  3036. (org-element-map tree 'table-cell 'identity info))))))
  3037. (ert-deftest test-org-export/table-cell-borders ()
  3038. "Test `org-export-table-cell-borders' specifications."
  3039. ;; 1. Recognize various column groups indicators.
  3040. (org-test-with-parsed-data "| / | < | > | <> |"
  3041. (should
  3042. (equal
  3043. '((right bottom top) (left bottom top) (right bottom top)
  3044. (right left bottom top))
  3045. (mapcar (lambda (cell)
  3046. (org-export-table-cell-borders cell info))
  3047. (org-element-map tree 'table-cell 'identity)))))
  3048. ;; 2. Accept shortcuts to define column groups.
  3049. (org-test-with-parsed-data "| / | < | < |"
  3050. (should
  3051. (equal
  3052. '((right bottom top) (right left bottom top) (left bottom top))
  3053. (mapcar (lambda (cell)
  3054. (org-export-table-cell-borders cell info))
  3055. (org-element-map tree 'table-cell 'identity)))))
  3056. ;; 3. A valid column groups row must start with a "/".
  3057. (org-test-with-parsed-data "
  3058. | | < |
  3059. | a | b |"
  3060. (should
  3061. (equal '((top) (top) (bottom) (bottom))
  3062. (mapcar (lambda (cell)
  3063. (org-export-table-cell-borders cell info))
  3064. (org-element-map tree 'table-cell 'identity)))))
  3065. ;; 4. Take table rules into consideration.
  3066. (org-test-with-parsed-data "
  3067. | 1 |
  3068. |---|
  3069. | 2 |"
  3070. (should
  3071. (equal '((below top) (bottom above))
  3072. (mapcar (lambda (cell)
  3073. (org-export-table-cell-borders cell info))
  3074. (org-element-map tree 'table-cell 'identity)))))
  3075. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3076. ;; (resp. `bottom' and `below') borders. Any special row is
  3077. ;; ignored.
  3078. (org-test-with-parsed-data "
  3079. |---+----|
  3080. | / | |
  3081. | | 1 |
  3082. |---+----|"
  3083. (should
  3084. (equal '((bottom below top above))
  3085. (last
  3086. (mapcar (lambda (cell)
  3087. (org-export-table-cell-borders cell info))
  3088. (org-element-map tree 'table-cell 'identity)))))))
  3089. (ert-deftest test-org-export/table-dimensions ()
  3090. "Test `org-export-table-dimensions' specifications."
  3091. ;; 1. Standard test.
  3092. (org-test-with-parsed-data "
  3093. | 1 | 2 | 3 |
  3094. | 4 | 5 | 6 |"
  3095. (should
  3096. (equal '(2 . 3)
  3097. (org-export-table-dimensions
  3098. (org-element-map tree 'table 'identity info 'first-match) info))))
  3099. ;; 2. Ignore horizontal rules and special columns.
  3100. (org-test-with-parsed-data "
  3101. | / | < | > |
  3102. | 1 | 2 | 3 |
  3103. |---+---+---|
  3104. | 4 | 5 | 6 |"
  3105. (should
  3106. (equal '(2 . 3)
  3107. (org-export-table-dimensions
  3108. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3109. (ert-deftest test-org-export/table-cell-address ()
  3110. "Test `org-export-table-cell-address' specifications."
  3111. ;; 1. Standard test: index is 0-based.
  3112. (org-test-with-parsed-data "| a | b |"
  3113. (should
  3114. (equal '((0 . 0) (0 . 1))
  3115. (org-element-map tree 'table-cell
  3116. (lambda (cell) (org-export-table-cell-address cell info))
  3117. info))))
  3118. ;; 2. Special column isn't counted, nor are special rows.
  3119. (org-test-with-parsed-data "
  3120. | / | <> |
  3121. | | c |"
  3122. (should
  3123. (equal '(0 . 0)
  3124. (org-export-table-cell-address
  3125. (car (last (org-element-map tree 'table-cell 'identity info)))
  3126. info))))
  3127. ;; 3. Tables rules do not count either.
  3128. (org-test-with-parsed-data "
  3129. | a |
  3130. |---|
  3131. | b |
  3132. |---|
  3133. | c |"
  3134. (should
  3135. (equal '(2 . 0)
  3136. (org-export-table-cell-address
  3137. (car (last (org-element-map tree 'table-cell 'identity info)))
  3138. info))))
  3139. ;; 4. Return nil for special cells.
  3140. (org-test-with-parsed-data "| / | a |"
  3141. (should-not
  3142. (org-export-table-cell-address
  3143. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3144. info))))
  3145. (ert-deftest test-org-export/get-table-cell-at ()
  3146. "Test `org-export-get-table-cell-at' specifications."
  3147. ;; 1. Address ignores special columns, special rows and rules.
  3148. (org-test-with-parsed-data "
  3149. | / | <> |
  3150. | | a |
  3151. |---+----|
  3152. | | b |"
  3153. (should
  3154. (equal '("b")
  3155. (org-element-contents
  3156. (org-export-get-table-cell-at
  3157. '(1 . 0)
  3158. (org-element-map tree 'table 'identity info 'first-match)
  3159. info)))))
  3160. ;; 2. Return value for a non-existent address is nil.
  3161. (org-test-with-parsed-data "| a |"
  3162. (should-not
  3163. (org-export-get-table-cell-at
  3164. '(2 . 2)
  3165. (org-element-map tree 'table 'identity info 'first-match)
  3166. info)))
  3167. (org-test-with-parsed-data "| / |"
  3168. (should-not
  3169. (org-export-get-table-cell-at
  3170. '(0 . 0)
  3171. (org-element-map tree 'table 'identity info 'first-match)
  3172. info))))
  3173. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3174. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3175. ;; 1. A cell at a beginning of a row always starts a column group.
  3176. (org-test-with-parsed-data "| a |"
  3177. (should
  3178. (org-export-table-cell-starts-colgroup-p
  3179. (org-element-map tree 'table-cell 'identity info 'first-match)
  3180. info)))
  3181. ;; 2. Special column should be ignored when determining the
  3182. ;; beginning of the row.
  3183. (org-test-with-parsed-data "
  3184. | / | |
  3185. | | a |"
  3186. (should
  3187. (org-export-table-cell-starts-colgroup-p
  3188. (org-element-map tree 'table-cell 'identity info 'first-match)
  3189. info)))
  3190. ;; 2. Explicit column groups.
  3191. (org-test-with-parsed-data "
  3192. | / | | < |
  3193. | a | b | c |"
  3194. (should
  3195. (equal
  3196. '(yes no yes)
  3197. (org-element-map tree 'table-cell
  3198. (lambda (cell)
  3199. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3200. info)))))
  3201. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3202. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3203. ;; 1. A cell at the end of a row always ends a column group.
  3204. (org-test-with-parsed-data "| a |"
  3205. (should
  3206. (org-export-table-cell-ends-colgroup-p
  3207. (org-element-map tree 'table-cell 'identity info 'first-match)
  3208. info)))
  3209. ;; 2. Special column should be ignored when determining the
  3210. ;; beginning of the row.
  3211. (org-test-with-parsed-data "
  3212. | / | |
  3213. | | a |"
  3214. (should
  3215. (org-export-table-cell-ends-colgroup-p
  3216. (org-element-map tree 'table-cell 'identity info 'first-match)
  3217. info)))
  3218. ;; 3. Explicit column groups.
  3219. (org-test-with-parsed-data "
  3220. | / | < | |
  3221. | a | b | c |"
  3222. (should
  3223. (equal
  3224. '(yes no yes)
  3225. (org-element-map tree 'table-cell
  3226. (lambda (cell)
  3227. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3228. info)))))
  3229. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3230. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3231. ;; 1. A row at the beginning of a table always starts a row group.
  3232. ;; So does a row following a table rule.
  3233. (org-test-with-parsed-data "
  3234. | a |
  3235. |---|
  3236. | b |"
  3237. (should
  3238. (equal
  3239. '(yes no yes)
  3240. (org-element-map tree 'table-row
  3241. (lambda (row)
  3242. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3243. info))))
  3244. ;; 2. Special rows should be ignored when determining the beginning
  3245. ;; of the row.
  3246. (org-test-with-parsed-data "
  3247. | / | < |
  3248. | | a |
  3249. |---+---|
  3250. | / | < |
  3251. | | b |"
  3252. (should
  3253. (equal
  3254. '(yes no yes)
  3255. (org-element-map tree 'table-row
  3256. (lambda (row)
  3257. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3258. info)))))
  3259. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3260. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3261. ;; 1. A row at the end of a table always ends a row group. So does
  3262. ;; a row preceding a table rule.
  3263. (org-test-with-parsed-data "
  3264. | a |
  3265. |---|
  3266. | b |"
  3267. (should
  3268. (equal
  3269. '(yes no yes)
  3270. (org-element-map tree 'table-row
  3271. (lambda (row)
  3272. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3273. info))))
  3274. ;; 2. Special rows should be ignored when determining the beginning
  3275. ;; of the row.
  3276. (org-test-with-parsed-data "
  3277. | | a |
  3278. | / | < |
  3279. |---+---|
  3280. | | b |
  3281. | / | < |"
  3282. (should
  3283. (equal
  3284. '(yes no yes)
  3285. (org-element-map tree 'table-row
  3286. (lambda (row)
  3287. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3288. info)))))
  3289. (ert-deftest test-org-export/table-row-in-header-p ()
  3290. "Test `org-export-table-row-in-header-p' specifications."
  3291. ;; Standard test. Separators are always nil.
  3292. (should
  3293. (equal
  3294. '(yes no no)
  3295. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3296. (org-element-map tree 'table-row
  3297. (lambda (row)
  3298. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  3299. ;; Nil when there is no header.
  3300. (should
  3301. (equal
  3302. '(no no)
  3303. (org-test-with-parsed-data "| a |\n| b |"
  3304. (org-element-map tree 'table-row
  3305. (lambda (row)
  3306. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3307. (ert-deftest test-org-export/table-row-starts-header-p ()
  3308. "Test `org-export-table-row-starts-header-p' specifications."
  3309. ;; 1. Only the row starting the first row group starts the table
  3310. ;; header.
  3311. (org-test-with-parsed-data "
  3312. | a |
  3313. | b |
  3314. |---|
  3315. | c |"
  3316. (should
  3317. (equal
  3318. '(yes no no no)
  3319. (org-element-map tree 'table-row
  3320. (lambda (row)
  3321. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3322. info))))
  3323. ;; 2. A row cannot start an header if there's no header in the
  3324. ;; table.
  3325. (org-test-with-parsed-data "
  3326. | a |
  3327. |---|"
  3328. (should-not
  3329. (org-export-table-row-starts-header-p
  3330. (org-element-map tree 'table-row 'identity info 'first-match)
  3331. info))))
  3332. (ert-deftest test-org-export/table-row-ends-header-p ()
  3333. "Test `org-export-table-row-ends-header-p' specifications."
  3334. ;; 1. Only the row starting the first row group starts the table
  3335. ;; header.
  3336. (org-test-with-parsed-data "
  3337. | a |
  3338. | b |
  3339. |---|
  3340. | c |"
  3341. (should
  3342. (equal
  3343. '(no yes no no)
  3344. (org-element-map tree 'table-row
  3345. (lambda (row)
  3346. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3347. info))))
  3348. ;; 2. A row cannot start an header if there's no header in the
  3349. ;; table.
  3350. (org-test-with-parsed-data "
  3351. | a |
  3352. |---|"
  3353. (should-not
  3354. (org-export-table-row-ends-header-p
  3355. (org-element-map tree 'table-row 'identity info 'first-match)
  3356. info))))
  3357. ;;; Tables of Contents
  3358. (ert-deftest test-org-export/collect-headlines ()
  3359. "Test `org-export-collect-headlines' specifications."
  3360. ;; Standard test.
  3361. (should
  3362. (= 2
  3363. (length
  3364. (org-test-with-parsed-data "* H1\n** H2"
  3365. (org-export-collect-headlines info)))))
  3366. ;; Do not collect headlines below optional argument.
  3367. (should
  3368. (= 1
  3369. (length
  3370. (org-test-with-parsed-data "* H1\n** H2"
  3371. (org-export-collect-headlines info 1)))))
  3372. ;; Never collect headlines below maximum headline level.
  3373. (should
  3374. (= 1
  3375. (length
  3376. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3377. (org-export-collect-headlines info)))))
  3378. (should
  3379. (= 1
  3380. (length
  3381. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3382. (org-export-collect-headlines info 2)))))
  3383. ;; Collect headlines locally.
  3384. (should
  3385. (= 2
  3386. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  3387. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3388. (length (org-export-collect-headlines info nil scope))))))
  3389. ;; When collecting locally, optional level is relative.
  3390. (should
  3391. (= 1
  3392. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  3393. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3394. (length (org-export-collect-headlines info 1 scope)))))))
  3395. ;;; Templates
  3396. (ert-deftest test-org-export/inner-template ()
  3397. "Test `inner-template' translator specifications."
  3398. (should
  3399. (equal "Success!"
  3400. (org-test-with-temp-text "* Headline"
  3401. (org-export-as
  3402. (org-export-create-backend
  3403. :transcoders
  3404. '((inner-template . (lambda (contents info) "Success!"))
  3405. (headline . (lambda (h c i) "Headline"))))))))
  3406. ;; Inner template is applied even in a "body-only" export.
  3407. (should
  3408. (equal "Success!"
  3409. (org-test-with-temp-text "* Headline"
  3410. (org-export-as
  3411. (org-export-create-backend
  3412. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3413. (headline . (lambda (h c i) "Headline"))))
  3414. nil nil 'body-only)))))
  3415. (ert-deftest test-org-export/template ()
  3416. "Test `template' translator specifications."
  3417. (should
  3418. (equal "Success!"
  3419. (org-test-with-temp-text "* Headline"
  3420. (org-export-as
  3421. (org-export-create-backend
  3422. :transcoders '((template . (lambda (contents info) "Success!"))
  3423. (headline . (lambda (h c i) "Headline"))))))))
  3424. ;; Template is not applied in a "body-only" export.
  3425. (should-not
  3426. (equal "Success!"
  3427. (org-test-with-temp-text "* Headline"
  3428. (org-export-as
  3429. (org-export-create-backend
  3430. :transcoders '((template . (lambda (contents info) "Success!"))
  3431. (headline . (lambda (h c i) "Headline"))))
  3432. nil nil 'body-only)))))
  3433. ;;; Topology
  3434. (ert-deftest test-org-export/get-next-element ()
  3435. "Test `org-export-get-next-element' specifications."
  3436. ;; Standard test.
  3437. (should
  3438. (equal "b"
  3439. (org-test-with-parsed-data "* Headline\n*a* b"
  3440. (org-export-get-next-element
  3441. (org-element-map tree 'bold 'identity info t) info))))
  3442. ;; Return nil when no previous element.
  3443. (should-not
  3444. (org-test-with-parsed-data "* Headline\na *b*"
  3445. (org-export-get-next-element
  3446. (org-element-map tree 'bold 'identity info t) info)))
  3447. ;; Non-exportable elements are ignored.
  3448. (should-not
  3449. (let ((org-export-with-timestamps nil))
  3450. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3451. (org-export-get-next-element
  3452. (org-element-map tree 'entity 'identity info t) info))))
  3453. ;; Find next element in secondary strings.
  3454. (should
  3455. (eq 'verbatim
  3456. (org-test-with-parsed-data "* a =verb="
  3457. (org-element-type
  3458. (org-export-get-next-element
  3459. (org-element-map tree 'plain-text 'identity info t) info)))))
  3460. (should
  3461. (eq 'verbatim
  3462. (org-test-with-parsed-data "* /italic/ =verb="
  3463. (org-element-type
  3464. (org-export-get-next-element
  3465. (org-element-map tree 'italic 'identity info t) info)))))
  3466. ;; Find next element in document keywords.
  3467. (should
  3468. (eq 'verbatim
  3469. (org-test-with-parsed-data "#+TITLE: a =verb="
  3470. (org-element-type
  3471. (org-export-get-next-element
  3472. (org-element-map
  3473. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3474. ;; Find next element in parsed affiliated keywords.
  3475. (should
  3476. (eq 'verbatim
  3477. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3478. (org-element-type
  3479. (org-export-get-next-element
  3480. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3481. ;; With optional argument N, return a list containing all the
  3482. ;; following elements.
  3483. (should
  3484. (equal
  3485. '(bold code underline)
  3486. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3487. (mapcar 'car
  3488. (org-export-get-next-element
  3489. (org-element-map tree 'italic 'identity info t) info t)))))
  3490. ;; When N is a positive integer, return a list containing up to
  3491. ;; N following elements.
  3492. (should
  3493. (equal
  3494. '(bold code)
  3495. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3496. (mapcar 'car
  3497. (org-export-get-next-element
  3498. (org-element-map tree 'italic 'identity info t) info 2))))))
  3499. (ert-deftest test-org-export/get-previous-element ()
  3500. "Test `org-export-get-previous-element' specifications."
  3501. ;; Standard test.
  3502. (should
  3503. (equal "a "
  3504. (org-test-with-parsed-data "* Headline\na *b*"
  3505. (org-export-get-previous-element
  3506. (org-element-map tree 'bold 'identity info t) info))))
  3507. ;; Return nil when no previous element.
  3508. (should-not
  3509. (org-test-with-parsed-data "* Headline\n*a* b"
  3510. (org-export-get-previous-element
  3511. (org-element-map tree 'bold 'identity info t) info)))
  3512. ;; Non-exportable elements are ignored.
  3513. (should-not
  3514. (let ((org-export-with-timestamps nil))
  3515. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3516. (org-export-get-previous-element
  3517. (org-element-map tree 'entity 'identity info t) info))))
  3518. ;; Find previous element in secondary strings.
  3519. (should
  3520. (eq 'verbatim
  3521. (org-test-with-parsed-data "* =verb= a"
  3522. (org-element-type
  3523. (org-export-get-previous-element
  3524. (org-element-map tree 'plain-text 'identity info t) info)))))
  3525. (should
  3526. (eq 'verbatim
  3527. (org-test-with-parsed-data "* =verb= /italic/"
  3528. (org-element-type
  3529. (org-export-get-previous-element
  3530. (org-element-map tree 'italic 'identity info t) info)))))
  3531. ;; Find previous element in document keywords.
  3532. (should
  3533. (eq 'verbatim
  3534. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3535. (org-element-type
  3536. (org-export-get-previous-element
  3537. (org-element-map
  3538. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3539. ;; Find previous element in parsed affiliated keywords.
  3540. (should
  3541. (eq 'verbatim
  3542. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3543. (org-element-type
  3544. (org-export-get-previous-element
  3545. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3546. ;; With optional argument N, return a list containing up to
  3547. ;; N previous elements.
  3548. (should
  3549. (equal '(underline italic bold)
  3550. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3551. (mapcar 'car
  3552. (org-export-get-previous-element
  3553. (org-element-map tree 'code 'identity info t) info t)))))
  3554. ;; When N is a positive integer, return a list containing up to
  3555. ;; N previous elements.
  3556. (should
  3557. (equal '(italic bold)
  3558. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3559. (mapcar 'car
  3560. (org-export-get-previous-element
  3561. (org-element-map tree 'code 'identity info t) info 2))))))
  3562. (provide 'test-ox)
  3563. ;;; test-org-export.el end here