test-ox.el 122 KB

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