test-ox.el 149 KB

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