test-ox.el 122 KB

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