test-ox.el 111 KB

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