test-ox.el 146 KB

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