test-ox.el 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369
  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/user-ignore-list ()
  1050. "Test if `:ignore-list' accepts user input."
  1051. (let ((backend (org-test-default-backend)))
  1052. (setf (org-export-backend-transcoders backend)
  1053. (cons '(template . (lambda (body i)
  1054. (format "BEGIN\n%sEND" body)))
  1055. (org-export-backend-transcoders backend)))
  1056. (org-test-with-temp-text "Text"
  1057. (should (equal (org-export-as backend nil nil 'body-only)
  1058. "Text\n"))
  1059. (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
  1060. (should
  1061. (equal
  1062. "* Head1\n"
  1063. (let ((org-export-filter-parse-tree-functions
  1064. '((lambda (data backend info)
  1065. ;; Ignore headlines with the word "note" in their title.
  1066. (org-element-map data 'headline
  1067. (lambda (headline)
  1068. (when (string-match "\\<note\\>"
  1069. (org-element-property :raw-value
  1070. headline))
  1071. (org-export-ignore-element headline info)))
  1072. info)
  1073. data))))
  1074. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  1075. (org-export-as (org-test-default-backend)))))))
  1076. (ert-deftest test-org-export/before-processing-hook ()
  1077. "Test `org-export-before-processing-hook'."
  1078. (should
  1079. (equal
  1080. "#+MACRO: mac val\nTest\n"
  1081. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1082. (let ((org-export-before-processing-hook
  1083. '((lambda (backend)
  1084. (while (re-search-forward "{{{" nil t)
  1085. (let ((object (org-element-context)))
  1086. (when (eq (org-element-type object) 'macro)
  1087. (delete-region
  1088. (org-element-property :begin object)
  1089. (org-element-property :end object)))))))))
  1090. (org-export-as (org-test-default-backend)))))))
  1091. (ert-deftest test-org-export/before-parsing-hook ()
  1092. "Test `org-export-before-parsing-hook'."
  1093. (should
  1094. (equal "Body 1\nBody 2\n"
  1095. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1096. (let ((org-export-before-parsing-hook
  1097. '((lambda (backend)
  1098. (goto-char (point-min))
  1099. (while (re-search-forward org-outline-regexp-bol nil t)
  1100. (delete-region
  1101. (point-at-bol) (progn (forward-line) (point))))))))
  1102. (org-export-as (org-test-default-backend)))))))
  1103. ;;; Affiliated Keywords
  1104. (ert-deftest test-org-export/read-attribute ()
  1105. "Test `org-export-read-attribute' specifications."
  1106. ;; Standard test.
  1107. (should
  1108. (equal
  1109. (org-export-read-attribute
  1110. :attr_html
  1111. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1112. (org-element-at-point)))
  1113. '(:a "1" :b "2")))
  1114. ;; Return nil on empty attribute.
  1115. (should-not
  1116. (org-export-read-attribute
  1117. :attr_html
  1118. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1119. ;; Return nil on "nil" string.
  1120. (should
  1121. (equal '(:a nil :b nil)
  1122. (org-export-read-attribute
  1123. :attr_html
  1124. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1125. (org-element-at-point)))))
  1126. ;; Return nil on empty string.
  1127. (should
  1128. (equal '(:a nil :b nil)
  1129. (org-export-read-attribute
  1130. :attr_html
  1131. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1132. (org-element-at-point)))))
  1133. ;; Return empty string when value is "".
  1134. (should
  1135. (equal '(:a "")
  1136. (org-export-read-attribute
  1137. :attr_html
  1138. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1139. (org-element-at-point)))))
  1140. ;; Return \"\" when value is """".
  1141. (should
  1142. (equal '(:a "\"\"")
  1143. (org-export-read-attribute
  1144. :attr_html
  1145. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1146. (org-element-at-point)))))
  1147. ;; Ignore text before first property.
  1148. (should-not
  1149. (member "ignore"
  1150. (org-export-read-attribute
  1151. :attr_html
  1152. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1153. (org-element-at-point))))))
  1154. (ert-deftest test-org-export/get-caption ()
  1155. "Test `org-export-get-caption' specifications."
  1156. ;; Without optional argument, return long caption
  1157. (should
  1158. (equal
  1159. '("l")
  1160. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1161. (org-export-get-caption (org-element-at-point)))))
  1162. ;; With optional argument, return short caption.
  1163. (should
  1164. (equal
  1165. '("s")
  1166. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1167. (org-export-get-caption (org-element-at-point) t))))
  1168. ;; Multiple lines are separated by white spaces.
  1169. (should
  1170. (equal
  1171. '("a" " " "b")
  1172. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1173. (org-export-get-caption (org-element-at-point))))))
  1174. ;;; Back-End Tools
  1175. (ert-deftest test-org-export/define-backend ()
  1176. "Test back-end definition and accessors."
  1177. ;; Translate table.
  1178. (should
  1179. (equal '((headline . my-headline-test))
  1180. (let (org-export--registered-backends)
  1181. (org-export-define-backend 'test '((headline . my-headline-test)))
  1182. (org-export-get-all-transcoders 'test))))
  1183. ;; Filters.
  1184. (should
  1185. (equal '((:filter-headline . my-filter))
  1186. (let (org-export--registered-backends)
  1187. (org-export-define-backend 'test
  1188. '((headline . my-headline-test))
  1189. :filters-alist '((:filter-headline . my-filter)))
  1190. (org-export-backend-filters (org-export-get-backend 'test)))))
  1191. ;; Options.
  1192. (should
  1193. (equal '((:prop value))
  1194. (let (org-export--registered-backends)
  1195. (org-export-define-backend 'test
  1196. '((headline . my-headline-test))
  1197. :options-alist '((:prop value)))
  1198. (org-export-backend-options (org-export-get-backend 'test)))))
  1199. ;; Menu.
  1200. (should
  1201. (equal '(?k "Test Export" test)
  1202. (let (org-export--registered-backends)
  1203. (org-export-define-backend 'test
  1204. '((headline . my-headline-test))
  1205. :menu-entry '(?k "Test Export" test))
  1206. (org-export-backend-menu (org-export-get-backend 'test)))))
  1207. ;; Export Blocks.
  1208. (should
  1209. (equal '(("TEST" . org-element-export-block-parser))
  1210. (let (org-export--registered-backends org-element-block-name-alist)
  1211. (org-export-define-backend 'test
  1212. '((headline . my-headline-test))
  1213. :export-block '("test"))
  1214. org-element-block-name-alist))))
  1215. (ert-deftest test-org-export/define-derived-backend ()
  1216. "Test `org-export-define-derived-backend' specifications."
  1217. ;; Error when parent back-end is not defined.
  1218. (should-error
  1219. (let (org-export--registered-backends)
  1220. (org-export-define-derived-backend 'test 'parent)))
  1221. ;; Append translation table to parent's.
  1222. (should
  1223. (equal '((:headline . test) (:headline . parent))
  1224. (let (org-export--registered-backends)
  1225. (org-export-define-backend 'parent '((:headline . parent)))
  1226. (org-export-define-derived-backend 'test 'parent
  1227. :translate-alist '((:headline . test)))
  1228. (org-export-get-all-transcoders 'test))))
  1229. ;; Options defined in the new back have priority over those defined
  1230. ;; in parent.
  1231. (should
  1232. (eq 'test
  1233. (let (org-export--registered-backends)
  1234. (org-export-define-backend 'parent
  1235. '((:headline . parent))
  1236. :options-alist '((:a nil nil 'parent)))
  1237. (org-export-define-derived-backend 'test 'parent
  1238. :options-alist '((:a nil nil 'test)))
  1239. (plist-get (org-export--get-global-options
  1240. (org-export-get-backend 'test))
  1241. :a)))))
  1242. (ert-deftest test-org-export/derived-backend-p ()
  1243. "Test `org-export-derived-backend-p' specifications."
  1244. ;; Non-nil with direct match.
  1245. (should
  1246. (let (org-export--registered-backends)
  1247. (org-export-define-backend 'test '((headline . test)))
  1248. (org-export-derived-backend-p 'test 'test)))
  1249. (should
  1250. (let (org-export--registered-backends)
  1251. (org-export-define-backend 'test '((headline . test)))
  1252. (org-export-define-derived-backend 'test2 'test)
  1253. (org-export-derived-backend-p 'test2 'test2)))
  1254. ;; Non-nil with a direct parent.
  1255. (should
  1256. (let (org-export--registered-backends)
  1257. (org-export-define-backend 'test '((headline . test)))
  1258. (org-export-define-derived-backend 'test2 'test)
  1259. (org-export-derived-backend-p 'test2 'test)))
  1260. ;; Non-nil with an indirect parent.
  1261. (should
  1262. (let (org-export--registered-backends)
  1263. (org-export-define-backend 'test '((headline . test)))
  1264. (org-export-define-derived-backend 'test2 'test)
  1265. (org-export-define-derived-backend 'test3 'test2)
  1266. (org-export-derived-backend-p 'test3 'test)))
  1267. ;; Nil otherwise.
  1268. (should-not
  1269. (let (org-export--registered-backends)
  1270. (org-export-define-backend 'test '((headline . test)))
  1271. (org-export-define-backend 'test2 '((headline . test2)))
  1272. (org-export-derived-backend-p 'test2 'test)))
  1273. (should-not
  1274. (let (org-export--registered-backends)
  1275. (org-export-define-backend 'test '((headline . test)))
  1276. (org-export-define-backend 'test2 '((headline . test2)))
  1277. (org-export-define-derived-backend 'test3 'test2)
  1278. (org-export-derived-backend-p 'test3 'test))))
  1279. (ert-deftest test-org-export/get-all-transcoders ()
  1280. "Test `org-export-get-all-transcoders' specifications."
  1281. ;; Return nil when back-end cannot be found.
  1282. (should-not (org-export-get-all-transcoders nil))
  1283. ;; Same as `org-export-transcoders' if no parent.
  1284. (should
  1285. (equal '((headline . ignore))
  1286. (org-export-get-all-transcoders
  1287. (org-export-create-backend
  1288. :transcoders '((headline . ignore))))))
  1289. ;; But inherit from all ancestors whenever possible.
  1290. (should
  1291. (equal '((section . ignore) (headline . ignore))
  1292. (let (org-export--registered-backends)
  1293. (org-export-define-backend 'b1 '((headline . ignore)))
  1294. (org-export-get-all-transcoders
  1295. (org-export-create-backend
  1296. :parent 'b1 :transcoders '((section . ignore)))))))
  1297. (should
  1298. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1299. (let (org-export--registered-backends)
  1300. (org-export-define-backend 'b1 '((headline . ignore)))
  1301. (org-export-define-derived-backend 'b2 'b1
  1302. :translate-alist '((section . ignore)))
  1303. (org-export-get-all-transcoders
  1304. (org-export-create-backend
  1305. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1306. ;; Back-end transcoders overrule inherited ones.
  1307. (should
  1308. (eq 'b
  1309. (let (org-export--registered-backends)
  1310. (org-export-define-backend 'b1 '((headline . a)))
  1311. (cdr (assq 'headline
  1312. (org-export-get-all-transcoders
  1313. (org-export-create-backend
  1314. :parent 'b1 :transcoders '((headline . b))))))))))
  1315. (ert-deftest test-org-export/get-all-options ()
  1316. "Test `org-export-get-all-options' specifications."
  1317. ;; Return nil when back-end cannot be found.
  1318. (should-not (org-export-get-all-options nil))
  1319. ;; Same as `org-export-options' if no parent.
  1320. (should
  1321. (equal '((headline . ignore))
  1322. (org-export-get-all-options
  1323. (org-export-create-backend
  1324. :options '((headline . ignore))))))
  1325. ;; But inherit from all ancestors whenever possible.
  1326. (should
  1327. (equal '((:key2 value2) (:key1 value1))
  1328. (let (org-export--registered-backends)
  1329. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1330. (org-export-get-all-options
  1331. (org-export-create-backend
  1332. :parent 'b1 :options '((:key2 value2)))))))
  1333. (should
  1334. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1335. (let (org-export--registered-backends)
  1336. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1337. (org-export-define-derived-backend 'b2 'b1
  1338. :options-alist '((:key2 value2)))
  1339. (org-export-get-all-options
  1340. (org-export-create-backend
  1341. :parent 'b2 :options '((:key3 value3)))))))
  1342. ;; Back-end options overrule inherited ones.
  1343. (should
  1344. (eq 'b
  1345. (let (org-export--registered-backends)
  1346. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1347. (cdr (assq :key1
  1348. (org-export-get-all-options
  1349. (org-export-create-backend
  1350. :parent 'b1 :options '((:key1 . b))))))))))
  1351. (ert-deftest test-org-export/get-all-filters ()
  1352. "Test `org-export-get-all-filters' specifications."
  1353. ;; Return nil when back-end cannot be found.
  1354. (should-not (org-export-get-all-filters nil))
  1355. ;; Same as `org-export-filters' if no parent.
  1356. (should
  1357. (equal '((:filter-headline . ignore))
  1358. (org-export-get-all-filters
  1359. (org-export-create-backend
  1360. :filters '((:filter-headline . ignore))))))
  1361. ;; But inherit from all ancestors whenever possible.
  1362. (should
  1363. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1364. (let (org-export--registered-backends)
  1365. (org-export-define-backend 'b1
  1366. nil :filters-alist '((:filter-headline . ignore)))
  1367. (org-export-get-all-filters
  1368. (org-export-create-backend
  1369. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1370. (should
  1371. (equal '((:filter-paragraph . ignore)
  1372. (:filter-section . ignore)
  1373. (:filter-headline . ignore))
  1374. (let (org-export--registered-backends)
  1375. (org-export-define-backend 'b1
  1376. nil :filters-alist '((:filter-headline . ignore)))
  1377. (org-export-define-derived-backend 'b2 'b1
  1378. :filters-alist '((:filter-section . ignore)))
  1379. (org-export-get-all-filters
  1380. (org-export-create-backend
  1381. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1382. ;; Back-end filters overrule inherited ones.
  1383. (should
  1384. (eq 'b
  1385. (let (org-export--registered-backends)
  1386. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1387. (cdr (assq :filter-headline
  1388. (org-export-get-all-filters
  1389. (org-export-create-backend
  1390. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1391. (ert-deftest test-org-export/with-backend ()
  1392. "Test `org-export-with-backend' definition."
  1393. ;; Error when calling an undefined back-end
  1394. (should-error (org-export-with-backend nil "Test"))
  1395. ;; Error when called back-end doesn't have an appropriate
  1396. ;; transcoder.
  1397. (should-error
  1398. (org-export-with-backend
  1399. (org-export-create-backend :transcoders '((headline . ignore)))
  1400. "Test"))
  1401. ;; Otherwise, export using correct transcoder
  1402. (should
  1403. (equal "Success"
  1404. (let (org-export--registered-backends)
  1405. (org-export-define-backend 'test
  1406. '((plain-text . (lambda (text contents info) "Failure"))))
  1407. (org-export-define-backend 'test2
  1408. '((plain-text . (lambda (text contents info) "Success"))))
  1409. (org-export-with-backend 'test2 "Test"))))
  1410. ;; Provide correct back-end if transcoder needs to use recursive
  1411. ;; calls anyway.
  1412. (should
  1413. (equal "Success\n"
  1414. (let ((test-back-end
  1415. (org-export-create-backend
  1416. :transcoders
  1417. '((headline . (lambda (headline contents info)
  1418. (org-export-data
  1419. (org-element-property :title headline)
  1420. info)))
  1421. (plain-text . (lambda (text info) "Success"))))))
  1422. (org-export-string-as
  1423. "* Test"
  1424. (org-export-create-backend
  1425. :transcoders
  1426. '((headline . (lambda (headline contents info)
  1427. (org-export-with-backend
  1428. test-back-end headline contents info))))))))))
  1429. (ert-deftest test-org-export/data-with-backend ()
  1430. "Test `org-export-data-with-backend' specifications."
  1431. ;; Error when calling an undefined back-end.
  1432. (should-error (org-export-data-with-backend nil "nil" nil))
  1433. ;; Otherwise, export data recursively, using correct back-end.
  1434. (should
  1435. (equal
  1436. "Success!"
  1437. (org-export-data-with-backend
  1438. '(bold nil "Test")
  1439. (org-export-create-backend
  1440. :transcoders
  1441. '((plain-text . (lambda (text info) "Success"))
  1442. (bold . (lambda (bold contents info) (concat contents "!")))))
  1443. '(:with-emphasize t)))))
  1444. ;;; Export Snippets
  1445. (ert-deftest test-org-export/export-snippet ()
  1446. "Test export snippets transcoding."
  1447. ;; Standard test.
  1448. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1449. (let ((backend (org-test-default-backend)))
  1450. (setf (org-export-backend-name backend) 'test)
  1451. (setf (org-export-backend-transcoders backend)
  1452. (cons (cons 'export-snippet
  1453. (lambda (snippet contents info)
  1454. (when (eq (org-export-snippet-backend snippet) 'test)
  1455. (org-element-property :value snippet))))
  1456. (org-export-backend-transcoders backend)))
  1457. (let ((org-export-snippet-translation-alist nil))
  1458. (should (equal (org-export-as backend) "A\n")))
  1459. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1460. (should (equal (org-export-as backend) "AB\n")))))
  1461. ;; Ignored export snippets do not remove any blank.
  1462. (should
  1463. (equal "begin end\n"
  1464. (org-test-with-parsed-data "begin @@test:A@@ end"
  1465. (org-export-data-with-backend
  1466. tree
  1467. (org-export-create-backend
  1468. :transcoders
  1469. '((paragraph . (lambda (paragraph contents info) contents))
  1470. (section . (lambda (section contents info) contents))))
  1471. info)))))
  1472. ;;; Footnotes
  1473. (ert-deftest test-org-export/footnotes ()
  1474. "Test footnotes specifications."
  1475. (let ((org-footnote-section nil)
  1476. (org-export-with-footnotes t))
  1477. ;; 1. Read every type of footnote.
  1478. (should
  1479. (equal
  1480. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1481. (org-test-with-parsed-data
  1482. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1483. (org-element-map tree 'footnote-reference
  1484. (lambda (ref)
  1485. (let ((def (org-export-get-footnote-definition ref info)))
  1486. (cons (org-export-get-footnote-number ref info)
  1487. (if (eq (org-element-property :type ref) 'inline) (car def)
  1488. (car (org-element-contents
  1489. (car (org-element-contents def))))))))
  1490. info))))
  1491. ;; 2. Test nested footnotes order.
  1492. (should
  1493. (equal
  1494. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  1495. (org-test-with-parsed-data
  1496. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1497. (org-element-map tree 'footnote-reference
  1498. (lambda (ref)
  1499. (when (org-export-footnote-first-reference-p ref info)
  1500. (cons (org-export-get-footnote-number ref info)
  1501. (org-element-property :label ref))))
  1502. info))))
  1503. ;; 3. Test nested footnote in invisible definitions.
  1504. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1505. ;; Hide definitions.
  1506. (narrow-to-region (point) (point-at-eol))
  1507. (let* ((tree (org-element-parse-buffer))
  1508. (info (org-combine-plists
  1509. `(:parse-tree ,tree)
  1510. (org-export-collect-tree-properties
  1511. tree (org-export-get-environment)))))
  1512. ;; Both footnotes should be seen.
  1513. (should
  1514. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  1515. ;; 4. Test footnotes definitions collection.
  1516. (should
  1517. (= 4
  1518. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1519. \[fn:2] B [fn:3] [fn::D].
  1520. \[fn:3] C."
  1521. (length (org-export-collect-footnote-definitions tree info)))))
  1522. ;; 5. Test export of footnotes defined outside parsing scope.
  1523. (should
  1524. (equal
  1525. "ParagraphOut of scope\n"
  1526. (org-test-with-temp-text "[fn:1] Out of scope
  1527. * Title
  1528. Paragraph[fn:1]"
  1529. (let ((backend (org-test-default-backend)))
  1530. (setf (org-export-backend-transcoders backend)
  1531. (cons (cons 'footnote-reference
  1532. (lambda (fn contents info)
  1533. (org-element-interpret-data
  1534. (org-export-get-footnote-definition fn info))))
  1535. (org-export-backend-transcoders backend)))
  1536. (forward-line)
  1537. (org-export-as backend 'subtree)))))
  1538. ;; 6. Footnotes without a definition should throw an error.
  1539. (should-error
  1540. (org-test-with-parsed-data "Text[fn:1]"
  1541. (org-export-get-footnote-definition
  1542. (org-element-map tree 'footnote-reference 'identity info t) info)))
  1543. ;; 7. Footnote section should be ignored in TOC and in headlines
  1544. ;; numbering.
  1545. (should
  1546. (= 1 (let ((org-footnote-section "Footnotes"))
  1547. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1548. (org-export-collect-headlines info))))))
  1549. (should
  1550. (equal '(2)
  1551. (let ((org-footnote-section "Footnotes"))
  1552. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1553. (org-element-map tree 'headline
  1554. (lambda (hl)
  1555. (when (equal (org-element-property :raw-value hl) "H2")
  1556. (org-export-get-headline-number hl info)))
  1557. info t)))))))
  1558. ;;; Headlines and Inlinetasks
  1559. (ert-deftest test-org-export/get-relative-level ()
  1560. "Test `org-export-get-relative-level' specifications."
  1561. ;; Standard test.
  1562. (should
  1563. (equal '(1 2)
  1564. (let ((org-odd-levels-only nil))
  1565. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1566. (org-element-map tree 'headline
  1567. (lambda (h) (org-export-get-relative-level h info))
  1568. info)))))
  1569. ;; Missing levels
  1570. (should
  1571. (equal '(1 3)
  1572. (let ((org-odd-levels-only nil))
  1573. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1574. (org-element-map tree 'headline
  1575. (lambda (h) (org-export-get-relative-level h info))
  1576. info))))))
  1577. (ert-deftest test-org-export/low-level-p ()
  1578. "Test `org-export-low-level-p' specifications."
  1579. (should
  1580. (equal
  1581. '(no yes)
  1582. (let ((org-odd-levels-only nil))
  1583. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1584. (org-element-map tree 'headline
  1585. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1586. (plist-put info :headline-levels 1)))))))
  1587. (ert-deftest test-org-export/get-headline-number ()
  1588. "Test `org-export-get-headline-number' specifications."
  1589. ;; Standard test.
  1590. (should
  1591. (equal
  1592. '((1) (1 1))
  1593. (let ((org-odd-levels-only nil))
  1594. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1595. (org-element-map tree 'headline
  1596. (lambda (h) (org-export-get-headline-number h info))
  1597. info)))))
  1598. ;; Missing levels are replaced with 0.
  1599. (should
  1600. (equal
  1601. '((1) (1 0 1))
  1602. (let ((org-odd-levels-only nil))
  1603. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1604. (org-element-map tree 'headline
  1605. (lambda (h) (org-export-get-headline-number h info))
  1606. info))))))
  1607. (ert-deftest test-org-export/numbered-headline-p ()
  1608. "Test `org-export-numbered-headline-p' specifications."
  1609. ;; If `:section-numbers' is nil, never number headlines.
  1610. (should-not
  1611. (org-test-with-parsed-data "* Headline"
  1612. (org-element-map tree 'headline
  1613. (lambda (h) (org-export-numbered-headline-p h info))
  1614. (plist-put info :section-numbers nil))))
  1615. ;; If `:section-numbers' is a number, only number headlines with
  1616. ;; a level greater that it.
  1617. (should
  1618. (equal
  1619. '(yes no)
  1620. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1621. (org-element-map tree 'headline
  1622. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1623. (plist-put info :section-numbers 1)))))
  1624. ;; Otherwise, headlines are always numbered.
  1625. (should
  1626. (org-test-with-parsed-data "* Headline"
  1627. (org-element-map tree 'headline
  1628. (lambda (h) (org-export-numbered-headline-p h info))
  1629. (plist-put info :section-numbers t)))))
  1630. (ert-deftest test-org-export/org-export-get-headline-id ()
  1631. "Test `org-export-get-headline-id' specifications."
  1632. ;; Numbered headlines have IDs akin to "sec-N".
  1633. (should
  1634. (equal "sec-1"
  1635. (org-test-with-parsed-data "* H"
  1636. (org-export-get-headline-id
  1637. (org-element-map tree 'headline #'identity info t)
  1638. info))))
  1639. ;; The ID of numbered headlines reflect the hierarchy.
  1640. (should
  1641. (equal "sec-1-1"
  1642. (org-test-with-parsed-data "* H1\n** H2"
  1643. (org-export-get-headline-id
  1644. (org-element-map tree 'headline
  1645. (lambda (h)
  1646. (and (equal "H2" (org-element-property :raw-value h)) h))
  1647. info t)
  1648. info))))
  1649. ;; Unnumbered headlines have IDs akin to "unnumbered-N".
  1650. (should
  1651. (equal "unnumbered-1"
  1652. (org-test-with-parsed-data
  1653. "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  1654. (org-export-get-headline-id
  1655. (org-element-map tree 'headline #'identity info t)
  1656. info))))
  1657. ;; The ID of Unnumbered headlines do not reflect the hierarchy.
  1658. (should
  1659. (equal "unnumbered-2"
  1660. (org-test-with-parsed-data
  1661. "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
  1662. (org-export-get-headline-id
  1663. (org-element-map tree 'headline
  1664. (lambda (h)
  1665. (and (equal "H2" (org-element-property :raw-value h)) h))
  1666. info t)
  1667. info))))
  1668. ;; When #+OPTIONS: num:nil all headlines are unnumbered.
  1669. (should
  1670. (equal "unnumbered-1"
  1671. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  1672. (org-export-get-headline-id
  1673. (org-element-map tree 'headline 'identity info t)
  1674. info))))
  1675. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  1676. ;; ancestors disables numbering.
  1677. (should
  1678. (org-test-with-parsed-data
  1679. "* H
  1680. :PROPERTIES:
  1681. :UNNUMBERED: t
  1682. :END:
  1683. ** H2
  1684. :PROPERTIES:
  1685. :UNNUMBERED: nil
  1686. :END:
  1687. *** H3"
  1688. (org-every
  1689. (lambda (h) (not (org-export-numbered-headline-p h info)))
  1690. (org-element-map tree 'headline #'identity info)))))
  1691. (ert-deftest test-org-export/number-to-roman ()
  1692. "Test `org-export-number-to-roman' specifications."
  1693. ;; If number is negative, return it as a string.
  1694. (should (equal (org-export-number-to-roman -1) "-1"))
  1695. ;; Otherwise, return it as a roman number.
  1696. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1697. (ert-deftest test-org-export/get-optional-title ()
  1698. "Test `org-export-get-alt-title' specifications."
  1699. ;; If ALT_TITLE property is defined, use it.
  1700. (should
  1701. (equal '("opt")
  1702. (org-test-with-parsed-data
  1703. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1704. (org-export-get-alt-title
  1705. (org-element-map tree 'headline 'identity info t)
  1706. info))))
  1707. ;; Otherwise, fall-back to regular title.
  1708. (should
  1709. (equal '("Headline")
  1710. (org-test-with-parsed-data "* Headline"
  1711. (org-export-get-alt-title
  1712. (org-element-map tree 'headline 'identity info t)
  1713. info)))))
  1714. (ert-deftest test-org-export/get-tags ()
  1715. "Test `org-export-get-tags' specifications."
  1716. (let ((org-export-exclude-tags '("noexport"))
  1717. (org-export-select-tags '("export")))
  1718. ;; Standard test: tags which are not a select tag, an exclude tag,
  1719. ;; or specified as optional argument shouldn't be ignored.
  1720. (should
  1721. (org-test-with-parsed-data "* Headline :tag:"
  1722. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1723. info)))
  1724. ;; Exclude tags are removed.
  1725. (should-not
  1726. (org-test-with-parsed-data "* Headline :noexport:"
  1727. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1728. info)))
  1729. ;; Select tags are removed.
  1730. (should-not
  1731. (org-test-with-parsed-data "* Headline :export:"
  1732. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1733. info)))
  1734. (should
  1735. (equal
  1736. '("tag")
  1737. (org-test-with-parsed-data "* Headline :tag:export:"
  1738. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1739. info))))
  1740. ;; Tags provided in the optional argument are also ignored.
  1741. (should-not
  1742. (org-test-with-parsed-data "* Headline :ignore:"
  1743. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1744. info '("ignore"))))
  1745. ;; Allow tag inheritance.
  1746. (should
  1747. (equal
  1748. '(("tag") ("tag"))
  1749. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1750. (org-element-map tree 'headline
  1751. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1752. ;; Tag inheritance checks FILETAGS keywords.
  1753. (should
  1754. (equal
  1755. '(("a" "b" "tag"))
  1756. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1757. (org-element-map tree 'headline
  1758. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1759. (ert-deftest test-org-export/get-node-property ()
  1760. "Test`org-export-get-node-property' specifications."
  1761. ;; Standard test.
  1762. (should
  1763. (equal "value"
  1764. (org-test-with-parsed-data "* Headline
  1765. :PROPERTIES:
  1766. :prop: value
  1767. :END:"
  1768. (org-export-get-node-property
  1769. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1770. ;; Test inheritance.
  1771. (should
  1772. (equal "value"
  1773. (org-test-with-parsed-data "* Parent
  1774. :PROPERTIES:
  1775. :prop: value
  1776. :END:
  1777. ** Headline
  1778. Paragraph"
  1779. (org-export-get-node-property
  1780. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1781. ;; Cannot return a value before the first headline.
  1782. (should-not
  1783. (org-test-with-parsed-data "Paragraph
  1784. * Headline
  1785. :PROPERTIES:
  1786. :prop: value
  1787. :END:"
  1788. (org-export-get-node-property
  1789. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1790. (ert-deftest test-org-export/get-category ()
  1791. "Test `org-export-get-category' specifications."
  1792. ;; Standard test.
  1793. (should
  1794. (equal "value"
  1795. (org-test-with-parsed-data "* Headline
  1796. :PROPERTIES:
  1797. :CATEGORY: value
  1798. :END:"
  1799. (org-export-get-category
  1800. (org-element-map tree 'headline 'identity nil t) info))))
  1801. ;; Test inheritance from a parent headline.
  1802. (should
  1803. (equal '("value" "value")
  1804. (org-test-with-parsed-data "* Headline1
  1805. :PROPERTIES:
  1806. :CATEGORY: value
  1807. :END:
  1808. ** Headline2"
  1809. (org-element-map tree 'headline
  1810. (lambda (hl) (org-export-get-category hl info)) info))))
  1811. ;; Test inheritance from #+CATEGORY keyword
  1812. (should
  1813. (equal "value"
  1814. (org-test-with-parsed-data "#+CATEGORY: value
  1815. * Headline"
  1816. (org-export-get-category
  1817. (org-element-map tree 'headline 'identity nil t) info))))
  1818. ;; Test inheritance from file name.
  1819. (should
  1820. (equal "test"
  1821. (org-test-with-parsed-data "* Headline"
  1822. (let ((info (plist-put info :input-file "~/test.org")))
  1823. (org-export-get-category
  1824. (org-element-map tree 'headline 'identity nil t) info)))))
  1825. ;; Fall-back value.
  1826. (should
  1827. (equal "???"
  1828. (org-test-with-parsed-data "* Headline"
  1829. (org-export-get-category
  1830. (org-element-map tree 'headline 'identity nil t) info)))))
  1831. (ert-deftest test-org-export/first-sibling-p ()
  1832. "Test `org-export-first-sibling-p' specifications."
  1833. ;; Standard test.
  1834. (should
  1835. (equal
  1836. '(yes yes no)
  1837. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  1838. (org-element-map tree 'headline
  1839. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1840. info))))
  1841. (should
  1842. (equal '(yes no)
  1843. (org-test-with-parsed-data "- item\n\n para"
  1844. (org-element-map tree 'paragraph
  1845. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1846. info))))
  1847. ;; Ignore sections for headlines.
  1848. (should
  1849. (equal '(yes yes)
  1850. (org-test-with-parsed-data "* H\nSection\n** H 2"
  1851. (org-element-map tree 'headline
  1852. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1853. info))))
  1854. ;; Ignore headlines not exported.
  1855. (should
  1856. (equal
  1857. '(yes)
  1858. (let ((org-export-exclude-tags '("ignore")))
  1859. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1860. (org-element-map tree 'headline
  1861. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1862. info))))))
  1863. (ert-deftest test-org-export/last-sibling-p ()
  1864. "Test `org-export-last-sibling-p' specifications."
  1865. ;; Standard test.
  1866. (should
  1867. (equal
  1868. '(yes no yes)
  1869. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1870. (org-element-map tree 'headline
  1871. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1872. info))))
  1873. (should
  1874. (equal '(no yes)
  1875. (org-test-with-parsed-data "- item\n\n para"
  1876. (org-element-map tree 'paragraph
  1877. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1878. info))))
  1879. ;; Ignore headlines not exported.
  1880. (should
  1881. (equal
  1882. '(yes)
  1883. (let ((org-export-exclude-tags '("ignore")))
  1884. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1885. (org-element-map tree 'headline
  1886. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1887. info))))))
  1888. (ert-deftest test-org-export/handle-inlinetasks ()
  1889. "Test inlinetask export."
  1890. ;; Inlinetask with an exclude tag.
  1891. (when (featurep 'org-inlinetask)
  1892. (should
  1893. (equal
  1894. ""
  1895. (let ((org-inlinetask-min-level 3))
  1896. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1897. (org-export-as (org-test-default-backend)
  1898. nil nil nil '(:exclude-tags ("noexp")))))))
  1899. ;; Inlinetask with an include tag.
  1900. (should
  1901. (equal
  1902. "* H2\n*** Inline :exp:\n"
  1903. (let ((org-inlinetask-min-level 3)
  1904. (org-tags-column 0))
  1905. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1906. (org-export-as (org-test-default-backend)
  1907. nil nil nil '(:select-tags ("exp")))))))
  1908. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1909. (should
  1910. (equal ""
  1911. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1912. (org-inlinetask-min-level 3))
  1913. (org-test-with-temp-text "*** TODO Inline"
  1914. (org-export-as (org-test-default-backend)
  1915. nil nil nil '(:with-tasks nil))))))))
  1916. ;;; Keywords
  1917. (ert-deftest test-org-export/get-date ()
  1918. "Test `org-export-get-date' specifications."
  1919. ;; Return a properly formatted string when
  1920. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  1921. ;; consists in a single timestamp.
  1922. (should
  1923. (equal "29 03 2012"
  1924. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1925. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1926. (org-export-get-date info)))))
  1927. ;; Return a secondary string otherwise.
  1928. (should-not
  1929. (stringp
  1930. (let ((org-export-date-timestamp-format nil))
  1931. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1932. (org-export-get-date info)))))
  1933. (should
  1934. (equal '("Date")
  1935. (org-test-with-parsed-data "#+DATE: Date"
  1936. (org-export-get-date info))))
  1937. ;; Optional argument has precedence over
  1938. ;; `org-export-date-timestamp-format'.
  1939. (should
  1940. (equal "29 03"
  1941. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1942. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1943. (org-export-get-date info "%d %m"))))))
  1944. ;;; Links
  1945. (ert-deftest test-org-export/custom-protocol-maybe ()
  1946. "Test `org-export-custom-protocol-maybe' specifications."
  1947. (should
  1948. (string-match
  1949. "success"
  1950. (let ((org-link-types (copy-sequence org-link-types)))
  1951. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  1952. (org-export-string-as
  1953. "[[foo:path]]"
  1954. (org-export-create-backend
  1955. :name 'test
  1956. :transcoders '((section . (lambda (s c i) c))
  1957. (paragraph . (lambda (p c i) c))
  1958. (link . (lambda (l c i)
  1959. (or (org-export-custom-protocol-maybe l c i)
  1960. "failure")))))))))
  1961. (should-not
  1962. (string-match
  1963. "success"
  1964. (let ((org-link-types (copy-sequence org-link-types)))
  1965. (org-add-link-type
  1966. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  1967. (org-export-string-as
  1968. "[[foo:path]]"
  1969. (org-export-create-backend
  1970. :name 'no-test
  1971. :transcoders '((section . (lambda (s c i) c))
  1972. (paragraph . (lambda (p c i) c))
  1973. (link . (lambda (l c i)
  1974. (or (org-export-custom-protocol-maybe l c i)
  1975. "failure")))))))))
  1976. ;; Ignore anonymous back-ends.
  1977. (should-not
  1978. (string-match
  1979. "success"
  1980. (let ((org-link-types (copy-sequence org-link-types)))
  1981. (org-add-link-type
  1982. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  1983. (org-export-string-as
  1984. "[[foo:path]]"
  1985. (org-export-create-backend
  1986. :transcoders '((section . (lambda (s c i) c))
  1987. (paragraph . (lambda (p c i) c))
  1988. (link . (lambda (l c i)
  1989. (or (org-export-custom-protocol-maybe l c i)
  1990. "failure"))))))))))
  1991. (ert-deftest test-org-export/get-coderef-format ()
  1992. "Test `org-export-get-coderef-format' specifications."
  1993. ;; A link without description returns "%s"
  1994. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1995. "%s"))
  1996. ;; Return "%s" when path is matched within description.
  1997. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1998. "desc %s"))
  1999. ;; Otherwise return description.
  2000. (should (equal (org-export-get-coderef-format "path" "desc")
  2001. "desc")))
  2002. (ert-deftest test-org-export/inline-image-p ()
  2003. "Test `org-export-inline-image-p' specifications."
  2004. (should
  2005. (org-export-inline-image-p
  2006. (org-test-with-temp-text "[[#id]]"
  2007. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2008. '(("custom-id" . "id")))))
  2009. (ert-deftest test-org-export/fuzzy-link ()
  2010. "Test fuzzy links specifications."
  2011. ;; Link to an headline should return headline's number.
  2012. (org-test-with-parsed-data
  2013. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2014. (should
  2015. ;; Note: Headline's number is in fact a list of numbers.
  2016. (equal '(2)
  2017. (org-element-map tree 'link
  2018. (lambda (link)
  2019. (org-export-get-ordinal
  2020. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2021. ;; Link to a target in an item should return item's number.
  2022. (org-test-with-parsed-data
  2023. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2024. (should
  2025. ;; Note: Item's number is in fact a list of numbers.
  2026. (equal '(1 2)
  2027. (org-element-map tree 'link
  2028. (lambda (link)
  2029. (org-export-get-ordinal
  2030. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2031. ;; Link to a target in a footnote should return footnote's number.
  2032. (org-test-with-parsed-data "
  2033. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2034. (should
  2035. (equal '(2 3)
  2036. (org-element-map tree 'link
  2037. (lambda (link)
  2038. (org-export-get-ordinal
  2039. (org-export-resolve-fuzzy-link link info) info)) info))))
  2040. ;; Link to a named element should return sequence number of that
  2041. ;; element.
  2042. (org-test-with-parsed-data
  2043. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2044. (should
  2045. (= 2
  2046. (org-element-map tree 'link
  2047. (lambda (link)
  2048. (org-export-get-ordinal
  2049. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2050. ;; Link to a target not within an item, a table, a footnote
  2051. ;; reference or definition should return section number.
  2052. (org-test-with-parsed-data
  2053. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2054. (should
  2055. (equal '(2)
  2056. (org-element-map tree 'link
  2057. (lambda (link)
  2058. (org-export-get-ordinal
  2059. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2060. ;; Space are not significant when matching a fuzzy link.
  2061. (should
  2062. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2063. (org-element-map tree 'link
  2064. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2065. info t)))
  2066. ;; Statistics cookies are ignored for headline match.
  2067. (should
  2068. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2069. (org-element-map tree 'link
  2070. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2071. info t)))
  2072. (should
  2073. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2074. (org-element-map tree 'link
  2075. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2076. info t)))
  2077. ;; Headline match is position dependent.
  2078. (should-not
  2079. (apply
  2080. 'eq
  2081. (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
  2082. (org-element-map tree 'link
  2083. (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
  2084. (ert-deftest test-org-export/resolve-coderef ()
  2085. "Test `org-export-resolve-coderef' specifications."
  2086. (let ((org-coderef-label-format "(ref:%s)"))
  2087. ;; 1. A link to a "-n -k -r" block returns line number.
  2088. (org-test-with-parsed-data
  2089. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2090. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2091. (org-test-with-parsed-data
  2092. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2093. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2094. ;; 2. A link to a "-n -r" block returns line number.
  2095. (org-test-with-parsed-data
  2096. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2097. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2098. (org-test-with-parsed-data
  2099. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2100. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2101. ;; 3. A link to a "-n" block returns coderef.
  2102. (org-test-with-parsed-data
  2103. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2104. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2105. (org-test-with-parsed-data
  2106. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2107. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2108. ;; 4. A link to a "-r" block returns line number.
  2109. (org-test-with-parsed-data
  2110. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2111. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2112. (org-test-with-parsed-data
  2113. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2114. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2115. ;; 5. A link to a block without a switch returns coderef.
  2116. (org-test-with-parsed-data
  2117. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2118. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2119. (org-test-with-parsed-data
  2120. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2121. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2122. ;; 6. Correctly handle continued line numbers. A "+n" switch
  2123. ;; should resume numbering from previous block with numbered
  2124. ;; lines, ignoring blocks not numbering lines in the process.
  2125. ;; A "-n" switch resets count.
  2126. (org-test-with-parsed-data "
  2127. #+BEGIN_EXAMPLE -n
  2128. Text.
  2129. #+END_EXAMPLE
  2130. #+BEGIN_SRC emacs-lisp
  2131. \(- 1 1)
  2132. #+END_SRC
  2133. #+BEGIN_SRC emacs-lisp +n -r
  2134. \(+ 1 1) (ref:addition)
  2135. #+END_SRC
  2136. #+BEGIN_EXAMPLE -n -r
  2137. Another text. (ref:text)
  2138. #+END_EXAMPLE"
  2139. (should (= (org-export-resolve-coderef "addition" info) 2))
  2140. (should (= (org-export-resolve-coderef "text" info) 1)))
  2141. ;; 7. Recognize coderef with user-specified syntax.
  2142. (org-test-with-parsed-data
  2143. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2144. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  2145. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2146. "Test `org-export-resolve-fuzzy-link' specifications."
  2147. ;; Match target objects.
  2148. (should
  2149. (org-test-with-parsed-data "<<target>> [[target]]"
  2150. (org-export-resolve-fuzzy-link
  2151. (org-element-map tree 'link 'identity info t) info)))
  2152. ;; Match named elements.
  2153. (should
  2154. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2155. (org-export-resolve-fuzzy-link
  2156. (org-element-map tree 'link 'identity info t) info)))
  2157. ;; Match exact headline's name.
  2158. (should
  2159. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2160. (org-export-resolve-fuzzy-link
  2161. (org-element-map tree 'link 'identity info t) info)))
  2162. ;; Targets objects have priority over named elements and headline
  2163. ;; titles.
  2164. (should
  2165. (eq 'target
  2166. (org-test-with-parsed-data
  2167. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  2168. (org-element-type
  2169. (org-export-resolve-fuzzy-link
  2170. (org-element-map tree 'link 'identity info t) info)))))
  2171. ;; Named elements have priority over headline titles.
  2172. (should
  2173. (eq 'paragraph
  2174. (org-test-with-parsed-data
  2175. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2176. (org-element-type
  2177. (org-export-resolve-fuzzy-link
  2178. (org-element-map tree 'link 'identity info t) info)))))
  2179. ;; If link's path starts with a "*", only match headline titles,
  2180. ;; though.
  2181. (should
  2182. (eq 'headline
  2183. (org-test-with-parsed-data
  2184. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2185. (org-element-type
  2186. (org-export-resolve-fuzzy-link
  2187. (org-element-map tree 'link 'identity info t) info)))))
  2188. ;; Return nil if no match.
  2189. (should-not
  2190. (org-test-with-parsed-data "[[target]]"
  2191. (org-export-resolve-fuzzy-link
  2192. (org-element-map tree 'link 'identity info t) info)))
  2193. ;; Match fuzzy link even when before first headline.
  2194. (should
  2195. (eq 'headline
  2196. (org-test-with-parsed-data "[[hl]]\n* hl"
  2197. (org-element-type
  2198. (org-export-resolve-fuzzy-link
  2199. (org-element-map tree 'link 'identity info t) info))))))
  2200. (ert-deftest test-org-export/resolve-id-link ()
  2201. "Test `org-export-resolve-id-link' specifications."
  2202. ;; 1. Regular test for custom-id link.
  2203. (org-test-with-parsed-data "* Headline1
  2204. :PROPERTIES:
  2205. :CUSTOM_ID: test
  2206. :END:
  2207. * Headline 2
  2208. \[[#test]]"
  2209. (should
  2210. (org-export-resolve-id-link
  2211. (org-element-map tree 'link 'identity info t) info)))
  2212. ;; 2. Failing test for custom-id link.
  2213. (org-test-with-parsed-data "* Headline1
  2214. :PROPERTIES:
  2215. :CUSTOM_ID: test
  2216. :END:
  2217. * Headline 2
  2218. \[[#no-match]]"
  2219. (should-not
  2220. (org-export-resolve-id-link
  2221. (org-element-map tree 'link 'identity info t) info)))
  2222. ;; 3. Test for internal id target.
  2223. (org-test-with-parsed-data "* Headline1
  2224. :PROPERTIES:
  2225. :ID: aaaa
  2226. :END:
  2227. * Headline 2
  2228. \[[id:aaaa]]"
  2229. (should
  2230. (org-export-resolve-id-link
  2231. (org-element-map tree 'link 'identity info t) info)))
  2232. ;; 4. Test for external id target.
  2233. (org-test-with-parsed-data "[[id:aaaa]]"
  2234. (should
  2235. (org-export-resolve-id-link
  2236. (org-element-map tree 'link 'identity info t)
  2237. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  2238. (ert-deftest test-org-export/resolve-radio-link ()
  2239. "Test `org-export-resolve-radio-link' specifications."
  2240. ;; Standard test.
  2241. (should
  2242. (org-test-with-temp-text "<<<radio>>> 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 targets are case-insensitive.
  2250. (should
  2251. (org-test-with-temp-text "<<<RADIO>>> radio"
  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. ;; Radio target with objects.
  2259. (should
  2260. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2261. (org-update-radio-target-regexp)
  2262. (let* ((tree (org-element-parse-buffer))
  2263. (info `(:parse-tree ,tree)))
  2264. (org-export-resolve-radio-link
  2265. (org-element-map tree 'link 'identity info t)
  2266. info))))
  2267. ;; Radio target with objects at its beginning.
  2268. (should
  2269. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2270. (org-update-radio-target-regexp)
  2271. (let* ((tree (org-element-parse-buffer))
  2272. (info `(:parse-tree ,tree)))
  2273. (org-export-resolve-radio-link
  2274. (org-element-map tree 'link 'identity info t)
  2275. info))))
  2276. ;; Radio link next to an apostrophe.
  2277. (should
  2278. (org-test-with-temp-text "<<<radio>>> radio's"
  2279. (org-update-radio-target-regexp)
  2280. (let* ((tree (org-element-parse-buffer))
  2281. (info `(:parse-tree ,tree)))
  2282. (org-export-resolve-radio-link
  2283. (org-element-map tree 'link 'identity info t)
  2284. info))))
  2285. ;; Multiple radio targets.
  2286. (should
  2287. (equal '("radio1" "radio2")
  2288. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2289. (org-update-radio-target-regexp)
  2290. (let* ((tree (org-element-parse-buffer))
  2291. (info `(:parse-tree ,tree)))
  2292. (org-element-map tree 'link
  2293. (lambda (link)
  2294. (org-element-property
  2295. :value (org-export-resolve-radio-link link info)))
  2296. info)))))
  2297. ;; Radio target is whitespace insensitive.
  2298. (should
  2299. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2300. (org-update-radio-target-regexp)
  2301. (let* ((tree (org-element-parse-buffer))
  2302. (info `(:parse-tree ,tree)))
  2303. (org-element-map tree 'link
  2304. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2305. ;;; Src-block and example-block
  2306. (ert-deftest test-org-export/unravel-code ()
  2307. "Test `org-export-unravel-code' function."
  2308. ;; Code without reference.
  2309. (should
  2310. (equal '("(+ 1 1)\n")
  2311. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2312. (org-export-unravel-code (org-element-at-point)))))
  2313. ;; Code with reference.
  2314. (should
  2315. (equal '("(+ 1 1)\n" (1 . "test"))
  2316. (org-test-with-temp-text
  2317. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2318. (let ((org-coderef-label-format "(ref:%s)"))
  2319. (org-export-unravel-code (org-element-at-point))))))
  2320. ;; Code with user-defined reference.
  2321. (should
  2322. (equal
  2323. '("(+ 1 1)\n" (1 . "test"))
  2324. (org-test-with-temp-text
  2325. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2326. (let ((org-coderef-label-format "(ref:%s)"))
  2327. (org-export-unravel-code (org-element-at-point))))))
  2328. ;; Code references keys are relative to the current block.
  2329. (should
  2330. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2331. (org-test-with-temp-text "
  2332. #+BEGIN_EXAMPLE -n
  2333. \(+ 1 1)
  2334. #+END_EXAMPLE
  2335. #+BEGIN_EXAMPLE +n
  2336. \(+ 2 2)
  2337. \(+ 3 3) (ref:one)
  2338. #+END_EXAMPLE"
  2339. (goto-line 5)
  2340. (let ((org-coderef-label-format "(ref:%s)"))
  2341. (org-export-unravel-code (org-element-at-point)))))))
  2342. (ert-deftest test-org-export/format-code-default ()
  2343. "Test `org-export-format-code-default' specifications."
  2344. ;; Return the empty string when code is empty.
  2345. (should
  2346. (equal ""
  2347. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2348. (org-export-format-code-default
  2349. (org-element-map tree 'src-block 'identity info t) info))))
  2350. ;; Number lines, two whitespace characters before the actual loc.
  2351. (should
  2352. (equal "1 a\n2 b\n"
  2353. (org-test-with-parsed-data
  2354. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2355. (org-export-format-code-default
  2356. (org-element-map tree 'src-block 'identity info t) info))))
  2357. ;; Put references 6 whitespace characters after the widest line,
  2358. ;; wrapped within parenthesis.
  2359. (should
  2360. (equal "123 (a)\n1 (b)\n"
  2361. (let ((org-coderef-label-format "(ref:%s)"))
  2362. (org-test-with-parsed-data
  2363. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2364. (org-export-format-code-default
  2365. (org-element-map tree 'src-block 'identity info t) info))))))
  2366. ;;; Smart Quotes
  2367. (ert-deftest test-org-export/activate-smart-quotes ()
  2368. "Test `org-export-activate-smart-quotes' specifications."
  2369. ;; Opening double quotes: standard test.
  2370. (should
  2371. (equal
  2372. '("some &ldquo;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. ;; Opening quotes: at the beginning of a paragraph.
  2379. (should
  2380. (equal
  2381. '("&ldquo;begin")
  2382. (let ((org-export-default-language "en"))
  2383. (org-test-with-parsed-data "\"begin"
  2384. (org-element-map tree 'plain-text
  2385. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2386. info)))))
  2387. ;; Opening quotes: after an object.
  2388. (should
  2389. (equal
  2390. '("&ldquo;begin")
  2391. (let ((org-export-default-language "en"))
  2392. (org-test-with-parsed-data "=verb= \"begin"
  2393. (org-element-map tree 'plain-text
  2394. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2395. info)))))
  2396. ;; Closing quotes: standard test.
  2397. (should
  2398. (equal
  2399. '("some&rdquo; paragraph")
  2400. (let ((org-export-default-language "en"))
  2401. (org-test-with-parsed-data "some\" paragraph"
  2402. (org-element-map tree 'plain-text
  2403. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2404. info)))))
  2405. ;; Closing quotes: at the end of a paragraph.
  2406. (should
  2407. (equal
  2408. '("end&rdquo;")
  2409. (let ((org-export-default-language "en"))
  2410. (org-test-with-parsed-data "end\""
  2411. (org-element-map tree 'plain-text
  2412. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2413. info)))))
  2414. ;; Apostrophe: standard test.
  2415. (should
  2416. (equal
  2417. '("It shouldn&rsquo;t fail")
  2418. (let ((org-export-default-language "en"))
  2419. (org-test-with-parsed-data "It shouldn't fail"
  2420. (org-element-map tree 'plain-text
  2421. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2422. info)))))
  2423. ;; Apostrophe: before an object.
  2424. (should
  2425. (equal
  2426. '("a&rsquo;")
  2427. (let ((org-export-default-language "en"))
  2428. (org-test-with-parsed-data "a'=b="
  2429. (org-element-map tree 'plain-text
  2430. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2431. info)))))
  2432. ;; Apostrophe: after an object.
  2433. (should
  2434. (equal
  2435. '("&rsquo;s")
  2436. (let ((org-export-default-language "en"))
  2437. (org-test-with-parsed-data "=code='s"
  2438. (org-element-map tree 'plain-text
  2439. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2440. info)))))
  2441. ;; Special case: isolated quotes.
  2442. (should
  2443. (equal '("&ldquo;" "&rdquo;")
  2444. (let ((org-export-default-language "en"))
  2445. (org-test-with-parsed-data "\"$x$\""
  2446. (org-element-map tree 'plain-text
  2447. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2448. info)))))
  2449. ;; Smart quotes in secondary strings.
  2450. (should
  2451. (equal '("&ldquo;" "&rdquo;")
  2452. (let ((org-export-default-language "en"))
  2453. (org-test-with-parsed-data "* \"$x$\""
  2454. (org-element-map tree 'plain-text
  2455. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2456. info)))))
  2457. ;; Smart quotes in document keywords.
  2458. (should
  2459. (equal '("&ldquo;" "&rdquo;")
  2460. (let ((org-export-default-language "en"))
  2461. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2462. (org-element-map (plist-get info :title) 'plain-text
  2463. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2464. info)))))
  2465. ;; Smart quotes in parsed affiliated keywords.
  2466. (should
  2467. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2468. (let ((org-export-default-language "en"))
  2469. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  2470. (org-element-map tree 'plain-text
  2471. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2472. info nil nil t))))))
  2473. ;;; Tables
  2474. (ert-deftest test-org-export/special-column ()
  2475. "Test if the table's special column is properly recognized."
  2476. ;; 1. First column is special if it contains only a special marking
  2477. ;; characters or empty cells.
  2478. (org-test-with-temp-text "
  2479. | ! | 1 |
  2480. | | 2 |"
  2481. (should
  2482. (org-export-table-has-special-column-p
  2483. (org-element-map
  2484. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2485. ;; 2. If the column contains anything else, it isn't special.
  2486. (org-test-with-temp-text "
  2487. | ! | 1 |
  2488. | b | 2 |"
  2489. (should-not
  2490. (org-export-table-has-special-column-p
  2491. (org-element-map
  2492. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2493. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  2494. ;; and "!".
  2495. (org-test-with-temp-text "
  2496. | # | 1 |
  2497. | ^ | 2 |
  2498. | * | 3 |
  2499. | _ | 4 |
  2500. | / | 5 |
  2501. | $ | 6 |
  2502. | ! | 7 |"
  2503. (should
  2504. (org-export-table-has-special-column-p
  2505. (org-element-map
  2506. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2507. ;; 4. A first column with only empty cells isn't considered as
  2508. ;; special.
  2509. (org-test-with-temp-text "
  2510. | | 1 |
  2511. | | 2 |"
  2512. (should-not
  2513. (org-export-table-has-special-column-p
  2514. (org-element-map
  2515. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  2516. (ert-deftest test-org-export/table-row-is-special-p ()
  2517. "Test `org-export-table-row-is-special-p' specifications."
  2518. ;; 1. A row is special if it has a special marking character in the
  2519. ;; special column.
  2520. (org-test-with-parsed-data "| ! | 1 |"
  2521. (should
  2522. (org-export-table-row-is-special-p
  2523. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2524. ;; 2. A row is special when its first field is "/"
  2525. (org-test-with-parsed-data "
  2526. | / | 1 |
  2527. | a | b |"
  2528. (should
  2529. (org-export-table-row-is-special-p
  2530. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2531. ;; 3. A row only containing alignment cookies is also considered as
  2532. ;; special.
  2533. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  2534. (should
  2535. (org-export-table-row-is-special-p
  2536. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2537. ;; 4. Everything else isn't considered as special.
  2538. (org-test-with-parsed-data "| \alpha | | c |"
  2539. (should-not
  2540. (org-export-table-row-is-special-p
  2541. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2542. ;; 5. Table's rules are never considered as special rows.
  2543. (org-test-with-parsed-data "|---+---|"
  2544. (should-not
  2545. (org-export-table-row-is-special-p
  2546. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  2547. (ert-deftest test-org-export/has-header-p ()
  2548. "Test `org-export-table-has-header-p' specifications."
  2549. ;; 1. With an header.
  2550. (org-test-with-parsed-data "
  2551. | a | b |
  2552. |---+---|
  2553. | c | d |"
  2554. (should
  2555. (org-export-table-has-header-p
  2556. (org-element-map tree 'table 'identity info 'first-match)
  2557. info)))
  2558. ;; 2. Without an header.
  2559. (org-test-with-parsed-data "
  2560. | a | b |
  2561. | c | d |"
  2562. (should-not
  2563. (org-export-table-has-header-p
  2564. (org-element-map tree 'table 'identity info 'first-match)
  2565. info)))
  2566. ;; 3. Don't get fooled with starting and ending rules.
  2567. (org-test-with-parsed-data "
  2568. |---+---|
  2569. | a | b |
  2570. | c | d |
  2571. |---+---|"
  2572. (should-not
  2573. (org-export-table-has-header-p
  2574. (org-element-map tree 'table 'identity info 'first-match)
  2575. info))))
  2576. (ert-deftest test-org-export/table-row-group ()
  2577. "Test `org-export-table-row-group' specifications."
  2578. ;; 1. A rule creates a new group.
  2579. (should
  2580. (equal '(1 rule 2)
  2581. (org-test-with-parsed-data "
  2582. | a | b |
  2583. |---+---|
  2584. | 1 | 2 |"
  2585. (org-element-map tree 'table-row
  2586. (lambda (row)
  2587. (if (eq (org-element-property :type row) 'rule) 'rule
  2588. (org-export-table-row-group row info)))))))
  2589. ;; 2. Special rows are ignored in count.
  2590. (should
  2591. (equal
  2592. '(rule 1)
  2593. (org-test-with-parsed-data "
  2594. | / | < | > |
  2595. |---|---+---|
  2596. | | 1 | 2 |"
  2597. (org-element-map tree 'table-row
  2598. (lambda (row)
  2599. (if (eq (org-element-property :type row) 'rule) 'rule
  2600. (org-export-table-row-group row info)))
  2601. info))))
  2602. ;; 3. Double rules also are ignored in count.
  2603. (should
  2604. (equal '(1 rule rule 2)
  2605. (org-test-with-parsed-data "
  2606. | a | b |
  2607. |---+---|
  2608. |---+---|
  2609. | 1 | 2 |"
  2610. (org-element-map tree 'table-row
  2611. (lambda (row)
  2612. (if (eq (org-element-property :type row) 'rule) 'rule
  2613. (org-export-table-row-group row info))))))))
  2614. (ert-deftest test-org-export/table-row-number ()
  2615. "Test `org-export-table-row-number' specifications."
  2616. ;; Standard test. Number is 0-indexed.
  2617. (should
  2618. (equal '(0 1)
  2619. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  2620. (org-element-map tree 'table-row
  2621. (lambda (row) (org-export-table-row-number row info)) info))))
  2622. ;; Number ignores separators.
  2623. (should
  2624. (equal '(0 1)
  2625. (org-test-with-parsed-data "
  2626. | a | b | c |
  2627. |---+---+---|
  2628. | d | e | f |"
  2629. (org-element-map tree 'table-row
  2630. (lambda (row) (org-export-table-row-number row info)) info))))
  2631. ;; Number ignores special rows.
  2632. (should
  2633. (equal '(0 1)
  2634. (org-test-with-parsed-data "
  2635. | / | < | > |
  2636. | | b | c |
  2637. |---+-----+-----|
  2638. | | <c> | <c> |
  2639. | | e | f |"
  2640. (org-element-map tree 'table-row
  2641. (lambda (row) (org-export-table-row-number row info)) info)))))
  2642. (ert-deftest test-org-export/table-cell-width ()
  2643. "Test `org-export-table-cell-width' specifications."
  2644. ;; 1. Width is primarily determined by width cookies. If no cookie
  2645. ;; is found, cell's width is nil.
  2646. (org-test-with-parsed-data "
  2647. | / | <l> | <6> | <l7> |
  2648. | | a | b | c |"
  2649. (should
  2650. (equal
  2651. '(nil 6 7)
  2652. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2653. (org-element-map tree 'table-cell 'identity info)))))
  2654. ;; 2. The last width cookie has precedence.
  2655. (org-test-with-parsed-data "
  2656. | <6> |
  2657. | <7> |
  2658. | a |"
  2659. (should
  2660. (equal
  2661. '(7)
  2662. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2663. (org-element-map tree 'table-cell 'identity info)))))
  2664. ;; 3. Valid width cookies must have a specific row.
  2665. (org-test-with-parsed-data "| <6> | cell |"
  2666. (should
  2667. (equal
  2668. '(nil nil)
  2669. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2670. (org-element-map tree 'table-cell 'identity))))))
  2671. (ert-deftest test-org-export/table-cell-alignment ()
  2672. "Test `org-export-table-cell-alignment' specifications."
  2673. ;; 1. Alignment is primarily determined by alignment cookies.
  2674. (should
  2675. (equal '(left center right)
  2676. (let ((org-table-number-fraction 0.5)
  2677. (org-table-number-regexp "^[0-9]+$"))
  2678. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  2679. (mapcar (lambda (cell)
  2680. (org-export-table-cell-alignment cell info))
  2681. (org-element-map tree 'table-cell 'identity))))))
  2682. ;; 2. The last alignment cookie has precedence.
  2683. (should
  2684. (equal '(right right right)
  2685. (org-test-with-parsed-data "
  2686. | <l8> |
  2687. | cell |
  2688. | <r9> |"
  2689. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  2690. (org-element-map tree 'table-cell 'identity)))))
  2691. ;; 3. If there's no cookie, cell's contents determine alignment.
  2692. ;; A column mostly made of cells containing numbers will align
  2693. ;; its cells to the right.
  2694. (should
  2695. (equal '(right right right)
  2696. (let ((org-table-number-fraction 0.5)
  2697. (org-table-number-regexp "^[0-9]+$"))
  2698. (org-test-with-parsed-data "
  2699. | 123 |
  2700. | some text |
  2701. | 12345 |"
  2702. (mapcar (lambda (cell)
  2703. (org-export-table-cell-alignment cell info))
  2704. (org-element-map tree 'table-cell 'identity))))))
  2705. ;; 4. Otherwise, they will be aligned to the left.
  2706. (should
  2707. (equal '(left left left)
  2708. (org-test-with-parsed-data "
  2709. | text |
  2710. | some text |
  2711. | \alpha |"
  2712. (mapcar (lambda (cell)
  2713. (org-export-table-cell-alignment cell info))
  2714. (org-element-map tree 'table-cell 'identity info))))))
  2715. (ert-deftest test-org-export/table-cell-borders ()
  2716. "Test `org-export-table-cell-borders' specifications."
  2717. ;; 1. Recognize various column groups indicators.
  2718. (org-test-with-parsed-data "| / | < | > | <> |"
  2719. (should
  2720. (equal
  2721. '((right bottom top) (left bottom top) (right bottom top)
  2722. (right left bottom top))
  2723. (mapcar (lambda (cell)
  2724. (org-export-table-cell-borders cell info))
  2725. (org-element-map tree 'table-cell 'identity)))))
  2726. ;; 2. Accept shortcuts to define column groups.
  2727. (org-test-with-parsed-data "| / | < | < |"
  2728. (should
  2729. (equal
  2730. '((right bottom top) (right left bottom top) (left bottom top))
  2731. (mapcar (lambda (cell)
  2732. (org-export-table-cell-borders cell info))
  2733. (org-element-map tree 'table-cell 'identity)))))
  2734. ;; 3. A valid column groups row must start with a "/".
  2735. (org-test-with-parsed-data "
  2736. | | < |
  2737. | a | b |"
  2738. (should
  2739. (equal '((top) (top) (bottom) (bottom))
  2740. (mapcar (lambda (cell)
  2741. (org-export-table-cell-borders cell info))
  2742. (org-element-map tree 'table-cell 'identity)))))
  2743. ;; 4. Take table rules into consideration.
  2744. (org-test-with-parsed-data "
  2745. | 1 |
  2746. |---|
  2747. | 2 |"
  2748. (should
  2749. (equal '((below top) (bottom above))
  2750. (mapcar (lambda (cell)
  2751. (org-export-table-cell-borders cell info))
  2752. (org-element-map tree 'table-cell 'identity)))))
  2753. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  2754. ;; (resp. `bottom' and `below') borders. Any special row is
  2755. ;; ignored.
  2756. (org-test-with-parsed-data "
  2757. |---+----|
  2758. | / | |
  2759. | | 1 |
  2760. |---+----|"
  2761. (should
  2762. (equal '((bottom below top above))
  2763. (last
  2764. (mapcar (lambda (cell)
  2765. (org-export-table-cell-borders cell info))
  2766. (org-element-map tree 'table-cell 'identity)))))))
  2767. (ert-deftest test-org-export/table-dimensions ()
  2768. "Test `org-export-table-dimensions' specifications."
  2769. ;; 1. Standard test.
  2770. (org-test-with-parsed-data "
  2771. | 1 | 2 | 3 |
  2772. | 4 | 5 | 6 |"
  2773. (should
  2774. (equal '(2 . 3)
  2775. (org-export-table-dimensions
  2776. (org-element-map tree 'table 'identity info 'first-match) info))))
  2777. ;; 2. Ignore horizontal rules and special columns.
  2778. (org-test-with-parsed-data "
  2779. | / | < | > |
  2780. | 1 | 2 | 3 |
  2781. |---+---+---|
  2782. | 4 | 5 | 6 |"
  2783. (should
  2784. (equal '(2 . 3)
  2785. (org-export-table-dimensions
  2786. (org-element-map tree 'table 'identity info 'first-match) info)))))
  2787. (ert-deftest test-org-export/table-cell-address ()
  2788. "Test `org-export-table-cell-address' specifications."
  2789. ;; 1. Standard test: index is 0-based.
  2790. (org-test-with-parsed-data "| a | b |"
  2791. (should
  2792. (equal '((0 . 0) (0 . 1))
  2793. (org-element-map tree 'table-cell
  2794. (lambda (cell) (org-export-table-cell-address cell info))
  2795. info))))
  2796. ;; 2. Special column isn't counted, nor are special rows.
  2797. (org-test-with-parsed-data "
  2798. | / | <> |
  2799. | | c |"
  2800. (should
  2801. (equal '(0 . 0)
  2802. (org-export-table-cell-address
  2803. (car (last (org-element-map tree 'table-cell 'identity info)))
  2804. info))))
  2805. ;; 3. Tables rules do not count either.
  2806. (org-test-with-parsed-data "
  2807. | a |
  2808. |---|
  2809. | b |
  2810. |---|
  2811. | c |"
  2812. (should
  2813. (equal '(2 . 0)
  2814. (org-export-table-cell-address
  2815. (car (last (org-element-map tree 'table-cell 'identity info)))
  2816. info))))
  2817. ;; 4. Return nil for special cells.
  2818. (org-test-with-parsed-data "| / | a |"
  2819. (should-not
  2820. (org-export-table-cell-address
  2821. (org-element-map tree 'table-cell 'identity nil 'first-match)
  2822. info))))
  2823. (ert-deftest test-org-export/get-table-cell-at ()
  2824. "Test `org-export-get-table-cell-at' specifications."
  2825. ;; 1. Address ignores special columns, special rows and rules.
  2826. (org-test-with-parsed-data "
  2827. | / | <> |
  2828. | | a |
  2829. |---+----|
  2830. | | b |"
  2831. (should
  2832. (equal '("b")
  2833. (org-element-contents
  2834. (org-export-get-table-cell-at
  2835. '(1 . 0)
  2836. (org-element-map tree 'table 'identity info 'first-match)
  2837. info)))))
  2838. ;; 2. Return value for a non-existent address is nil.
  2839. (org-test-with-parsed-data "| a |"
  2840. (should-not
  2841. (org-export-get-table-cell-at
  2842. '(2 . 2)
  2843. (org-element-map tree 'table 'identity info 'first-match)
  2844. info)))
  2845. (org-test-with-parsed-data "| / |"
  2846. (should-not
  2847. (org-export-get-table-cell-at
  2848. '(0 . 0)
  2849. (org-element-map tree 'table 'identity info 'first-match)
  2850. info))))
  2851. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  2852. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  2853. ;; 1. A cell at a beginning of a row always starts a column group.
  2854. (org-test-with-parsed-data "| a |"
  2855. (should
  2856. (org-export-table-cell-starts-colgroup-p
  2857. (org-element-map tree 'table-cell 'identity info 'first-match)
  2858. info)))
  2859. ;; 2. Special column should be ignored when determining the
  2860. ;; beginning of the row.
  2861. (org-test-with-parsed-data "
  2862. | / | |
  2863. | | a |"
  2864. (should
  2865. (org-export-table-cell-starts-colgroup-p
  2866. (org-element-map tree 'table-cell 'identity info 'first-match)
  2867. info)))
  2868. ;; 2. Explicit column groups.
  2869. (org-test-with-parsed-data "
  2870. | / | | < |
  2871. | a | b | c |"
  2872. (should
  2873. (equal
  2874. '(yes no yes)
  2875. (org-element-map tree 'table-cell
  2876. (lambda (cell)
  2877. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2878. info)))))
  2879. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2880. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2881. ;; 1. A cell at the end of a row always ends a column group.
  2882. (org-test-with-parsed-data "| a |"
  2883. (should
  2884. (org-export-table-cell-ends-colgroup-p
  2885. (org-element-map tree 'table-cell 'identity info 'first-match)
  2886. info)))
  2887. ;; 2. Special column should be ignored when determining the
  2888. ;; beginning of the row.
  2889. (org-test-with-parsed-data "
  2890. | / | |
  2891. | | a |"
  2892. (should
  2893. (org-export-table-cell-ends-colgroup-p
  2894. (org-element-map tree 'table-cell 'identity info 'first-match)
  2895. info)))
  2896. ;; 3. Explicit column groups.
  2897. (org-test-with-parsed-data "
  2898. | / | < | |
  2899. | a | b | c |"
  2900. (should
  2901. (equal
  2902. '(yes no yes)
  2903. (org-element-map tree 'table-cell
  2904. (lambda (cell)
  2905. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2906. info)))))
  2907. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2908. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2909. ;; 1. A row at the beginning of a table always starts a row group.
  2910. ;; So does a row following a table rule.
  2911. (org-test-with-parsed-data "
  2912. | a |
  2913. |---|
  2914. | b |"
  2915. (should
  2916. (equal
  2917. '(yes no yes)
  2918. (org-element-map tree 'table-row
  2919. (lambda (row)
  2920. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2921. info))))
  2922. ;; 2. Special rows should be ignored when determining the beginning
  2923. ;; of the row.
  2924. (org-test-with-parsed-data "
  2925. | / | < |
  2926. | | a |
  2927. |---+---|
  2928. | / | < |
  2929. | | b |"
  2930. (should
  2931. (equal
  2932. '(yes no yes)
  2933. (org-element-map tree 'table-row
  2934. (lambda (row)
  2935. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2936. info)))))
  2937. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2938. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2939. ;; 1. A row at the end of a table always ends a row group. So does
  2940. ;; a row preceding a table rule.
  2941. (org-test-with-parsed-data "
  2942. | a |
  2943. |---|
  2944. | b |"
  2945. (should
  2946. (equal
  2947. '(yes no yes)
  2948. (org-element-map tree 'table-row
  2949. (lambda (row)
  2950. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2951. info))))
  2952. ;; 2. Special rows should be ignored when determining the beginning
  2953. ;; of the row.
  2954. (org-test-with-parsed-data "
  2955. | | a |
  2956. | / | < |
  2957. |---+---|
  2958. | | b |
  2959. | / | < |"
  2960. (should
  2961. (equal
  2962. '(yes no yes)
  2963. (org-element-map tree 'table-row
  2964. (lambda (row)
  2965. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2966. info)))))
  2967. (ert-deftest test-org-export/table-row-in-header-p ()
  2968. "Test `org-export-table-row-in-header-p' specifications."
  2969. ;; Standard test. Separators are always nil.
  2970. (should
  2971. (equal
  2972. '(yes no no)
  2973. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  2974. (org-element-map tree 'table-row
  2975. (lambda (row)
  2976. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  2977. ;; Nil when there is no header.
  2978. (should
  2979. (equal
  2980. '(no no)
  2981. (org-test-with-parsed-data "| a |\n| b |"
  2982. (org-element-map tree 'table-row
  2983. (lambda (row)
  2984. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  2985. (ert-deftest test-org-export/table-row-starts-header-p ()
  2986. "Test `org-export-table-row-starts-header-p' specifications."
  2987. ;; 1. Only the row starting the first row group starts the table
  2988. ;; header.
  2989. (org-test-with-parsed-data "
  2990. | a |
  2991. | b |
  2992. |---|
  2993. | c |"
  2994. (should
  2995. (equal
  2996. '(yes no no no)
  2997. (org-element-map tree 'table-row
  2998. (lambda (row)
  2999. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3000. info))))
  3001. ;; 2. A row cannot start an header if there's no header in the
  3002. ;; table.
  3003. (org-test-with-parsed-data "
  3004. | a |
  3005. |---|"
  3006. (should-not
  3007. (org-export-table-row-starts-header-p
  3008. (org-element-map tree 'table-row 'identity info 'first-match)
  3009. info))))
  3010. (ert-deftest test-org-export/table-row-ends-header-p ()
  3011. "Test `org-export-table-row-ends-header-p' specifications."
  3012. ;; 1. Only the row starting the first row group starts the table
  3013. ;; header.
  3014. (org-test-with-parsed-data "
  3015. | a |
  3016. | b |
  3017. |---|
  3018. | c |"
  3019. (should
  3020. (equal
  3021. '(no yes no no)
  3022. (org-element-map tree 'table-row
  3023. (lambda (row)
  3024. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3025. info))))
  3026. ;; 2. A row cannot start an header if there's no header in the
  3027. ;; table.
  3028. (org-test-with-parsed-data "
  3029. | a |
  3030. |---|"
  3031. (should-not
  3032. (org-export-table-row-ends-header-p
  3033. (org-element-map tree 'table-row 'identity info 'first-match)
  3034. info))))
  3035. ;;; Tables of Contents
  3036. (ert-deftest test-org-export/collect-headlines ()
  3037. "Test `org-export-collect-headlines' specifications."
  3038. ;; Standard test.
  3039. (should
  3040. (= 2
  3041. (length
  3042. (org-test-with-parsed-data "* H1\n** H2"
  3043. (org-export-collect-headlines info)))))
  3044. ;; Do not collect headlines below optional argument.
  3045. (should
  3046. (= 1
  3047. (length
  3048. (org-test-with-parsed-data "* H1\n** H2"
  3049. (org-export-collect-headlines info 1)))))
  3050. ;; Never collect headlines below maximum headline level.
  3051. (should
  3052. (= 1
  3053. (length
  3054. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3055. (org-export-collect-headlines info)))))
  3056. (should
  3057. (= 1
  3058. (length
  3059. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3060. (org-export-collect-headlines info 2))))))
  3061. ;;; Templates
  3062. (ert-deftest test-org-export/inner-template ()
  3063. "Test `inner-template' translator specifications."
  3064. (should
  3065. (equal "Success!"
  3066. (org-test-with-temp-text "* Headline"
  3067. (org-export-as
  3068. (org-export-create-backend
  3069. :transcoders
  3070. '((inner-template . (lambda (contents info) "Success!"))
  3071. (headline . (lambda (h c i) "Headline"))))))))
  3072. ;; Inner template is applied even in a "body-only" export.
  3073. (should
  3074. (equal "Success!"
  3075. (org-test-with-temp-text "* Headline"
  3076. (org-export-as
  3077. (org-export-create-backend
  3078. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3079. (headline . (lambda (h c i) "Headline"))))
  3080. nil nil 'body-only)))))
  3081. (ert-deftest test-org-export/template ()
  3082. "Test `template' translator specifications."
  3083. (should
  3084. (equal "Success!"
  3085. (org-test-with-temp-text "* Headline"
  3086. (org-export-as
  3087. (org-export-create-backend
  3088. :transcoders '((template . (lambda (contents info) "Success!"))
  3089. (headline . (lambda (h c i) "Headline"))))))))
  3090. ;; Template is not applied in a "body-only" export.
  3091. (should-not
  3092. (equal "Success!"
  3093. (org-test-with-temp-text "* Headline"
  3094. (org-export-as
  3095. (org-export-create-backend
  3096. :transcoders '((template . (lambda (contents info) "Success!"))
  3097. (headline . (lambda (h c i) "Headline"))))
  3098. nil nil 'body-only)))))
  3099. ;;; Topology
  3100. (ert-deftest test-org-export/get-next-element ()
  3101. "Test `org-export-get-next-element' specifications."
  3102. ;; Standard test.
  3103. (should
  3104. (equal "b"
  3105. (org-test-with-parsed-data "* Headline\n*a* b"
  3106. (org-export-get-next-element
  3107. (org-element-map tree 'bold 'identity info t) info))))
  3108. ;; Return nil when no previous element.
  3109. (should-not
  3110. (org-test-with-parsed-data "* Headline\na *b*"
  3111. (org-export-get-next-element
  3112. (org-element-map tree 'bold 'identity info t) info)))
  3113. ;; Non-exportable elements are ignored.
  3114. (should-not
  3115. (let ((org-export-with-timestamps nil))
  3116. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3117. (org-export-get-next-element
  3118. (org-element-map tree 'entity 'identity info t) info))))
  3119. ;; Find next element in secondary strings.
  3120. (should
  3121. (eq 'verbatim
  3122. (org-test-with-parsed-data "* a =verb="
  3123. (org-element-type
  3124. (org-export-get-next-element
  3125. (org-element-map tree 'plain-text 'identity info t) info)))))
  3126. (should
  3127. (eq 'verbatim
  3128. (org-test-with-parsed-data "* /italic/ =verb="
  3129. (org-element-type
  3130. (org-export-get-next-element
  3131. (org-element-map tree 'italic 'identity info t) info)))))
  3132. ;; Find next element in document keywords.
  3133. (should
  3134. (eq 'verbatim
  3135. (org-test-with-parsed-data "#+TITLE: a =verb="
  3136. (org-element-type
  3137. (org-export-get-next-element
  3138. (org-element-map
  3139. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3140. ;; Find next element in parsed affiliated keywords.
  3141. (should
  3142. (eq 'verbatim
  3143. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3144. (org-element-type
  3145. (org-export-get-next-element
  3146. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3147. ;; With optional argument N, return a list containing all the
  3148. ;; following elements.
  3149. (should
  3150. (equal
  3151. '(bold code underline)
  3152. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3153. (mapcar 'car
  3154. (org-export-get-next-element
  3155. (org-element-map tree 'italic 'identity info t) info t)))))
  3156. ;; When N is a positive integer, return a list containing up to
  3157. ;; N following elements.
  3158. (should
  3159. (equal
  3160. '(bold code)
  3161. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3162. (mapcar 'car
  3163. (org-export-get-next-element
  3164. (org-element-map tree 'italic 'identity info t) info 2))))))
  3165. (ert-deftest test-org-export/get-previous-element ()
  3166. "Test `org-export-get-previous-element' specifications."
  3167. ;; Standard test.
  3168. (should
  3169. (equal "a "
  3170. (org-test-with-parsed-data "* Headline\na *b*"
  3171. (org-export-get-previous-element
  3172. (org-element-map tree 'bold 'identity info t) info))))
  3173. ;; Return nil when no previous element.
  3174. (should-not
  3175. (org-test-with-parsed-data "* Headline\n*a* b"
  3176. (org-export-get-previous-element
  3177. (org-element-map tree 'bold 'identity info t) info)))
  3178. ;; Non-exportable elements are ignored.
  3179. (should-not
  3180. (let ((org-export-with-timestamps nil))
  3181. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3182. (org-export-get-previous-element
  3183. (org-element-map tree 'entity 'identity info t) info))))
  3184. ;; Find previous element in secondary strings.
  3185. (should
  3186. (eq 'verbatim
  3187. (org-test-with-parsed-data "* =verb= a"
  3188. (org-element-type
  3189. (org-export-get-previous-element
  3190. (org-element-map tree 'plain-text 'identity info t) info)))))
  3191. (should
  3192. (eq 'verbatim
  3193. (org-test-with-parsed-data "* =verb= /italic/"
  3194. (org-element-type
  3195. (org-export-get-previous-element
  3196. (org-element-map tree 'italic 'identity info t) info)))))
  3197. ;; Find previous element in document keywords.
  3198. (should
  3199. (eq 'verbatim
  3200. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3201. (org-element-type
  3202. (org-export-get-previous-element
  3203. (org-element-map
  3204. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3205. ;; Find previous element in parsed affiliated keywords.
  3206. (should
  3207. (eq 'verbatim
  3208. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3209. (org-element-type
  3210. (org-export-get-previous-element
  3211. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3212. ;; With optional argument N, return a list containing up to
  3213. ;; N previous elements.
  3214. (should
  3215. (equal '(underline italic bold)
  3216. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3217. (mapcar 'car
  3218. (org-export-get-previous-element
  3219. (org-element-map tree 'code 'identity info t) info t)))))
  3220. ;; When N is a positive integer, return a list containing up to
  3221. ;; N previous elements.
  3222. (should
  3223. (equal '(italic bold)
  3224. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3225. (mapcar 'car
  3226. (org-export-get-previous-element
  3227. (org-element-map tree 'code 'identity info t) info 2))))))
  3228. (provide 'test-ox)
  3229. ;;; test-org-export.el end here