test-ox.el 120 KB

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