test-ox.el 126 KB

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