test-ox.el 143 KB

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