test-ox.el 120 KB

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