test-ox.el 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653
  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. ;; Inline source blocks generate {{{results}}} macros. Evaluate
  1405. ;; those.
  1406. (should
  1407. (equal "=2=\n"
  1408. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  1409. (let ((org-export-use-babel t)
  1410. (org-babel-inline-result-wrap "=%s="))
  1411. (org-export-as (org-test-default-backend)))))))
  1412. (ert-deftest test-org-export/before-processing-hook ()
  1413. "Test `org-export-before-processing-hook'."
  1414. (should
  1415. (equal
  1416. "#+macro: mac val\nTest\n"
  1417. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1418. (let ((org-export-before-processing-hook
  1419. '((lambda (backend)
  1420. (while (re-search-forward "{{{" nil t)
  1421. (let ((object (org-element-context)))
  1422. (when (eq (org-element-type object) 'macro)
  1423. (delete-region
  1424. (org-element-property :begin object)
  1425. (org-element-property :end object)))))))))
  1426. (org-export-as (org-test-default-backend)))))))
  1427. (ert-deftest test-org-export/before-parsing-hook ()
  1428. "Test `org-export-before-parsing-hook'."
  1429. (should
  1430. (equal "Body 1\nBody 2\n"
  1431. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1432. (let ((org-export-before-parsing-hook
  1433. '((lambda (backend)
  1434. (goto-char (point-min))
  1435. (while (re-search-forward org-outline-regexp-bol nil t)
  1436. (delete-region
  1437. (point-at-bol) (progn (forward-line) (point))))))))
  1438. (org-export-as (org-test-default-backend)))))))
  1439. ;;; Affiliated Keywords
  1440. (ert-deftest test-org-export/read-attribute ()
  1441. "Test `org-export-read-attribute' specifications."
  1442. ;; Standard test.
  1443. (should
  1444. (equal
  1445. (org-export-read-attribute
  1446. :attr_html
  1447. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1448. (org-element-at-point)))
  1449. '(:a "1" :b "2")))
  1450. ;; Return nil on empty attribute.
  1451. (should-not
  1452. (org-export-read-attribute
  1453. :attr_html
  1454. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1455. ;; Return nil on "nil" string.
  1456. (should
  1457. (equal '(:a nil :b nil)
  1458. (org-export-read-attribute
  1459. :attr_html
  1460. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1461. (org-element-at-point)))))
  1462. ;; Return nil on empty string.
  1463. (should
  1464. (equal '(:a nil :b nil)
  1465. (org-export-read-attribute
  1466. :attr_html
  1467. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1468. (org-element-at-point)))))
  1469. ;; Return empty string when value is "".
  1470. (should
  1471. (equal '(:a "")
  1472. (org-export-read-attribute
  1473. :attr_html
  1474. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1475. (org-element-at-point)))))
  1476. ;; Return \"\" when value is """".
  1477. (should
  1478. (equal '(:a "\"\"")
  1479. (org-export-read-attribute
  1480. :attr_html
  1481. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1482. (org-element-at-point)))))
  1483. ;; Ignore text before first property.
  1484. (should-not
  1485. (member "ignore"
  1486. (org-export-read-attribute
  1487. :attr_html
  1488. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1489. (org-element-at-point))))))
  1490. (ert-deftest test-org-export/get-caption ()
  1491. "Test `org-export-get-caption' specifications."
  1492. ;; Without optional argument, return long caption
  1493. (should
  1494. (equal
  1495. '("l")
  1496. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1497. (org-export-get-caption (org-element-at-point)))))
  1498. ;; With optional argument, return short caption.
  1499. (should
  1500. (equal
  1501. '("s")
  1502. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1503. (org-export-get-caption (org-element-at-point) t))))
  1504. ;; Multiple lines are separated by white spaces.
  1505. (should
  1506. (equal
  1507. '("a" " " "b")
  1508. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1509. (org-export-get-caption (org-element-at-point))))))
  1510. ;;; Back-End Tools
  1511. (ert-deftest test-org-export/define-backend ()
  1512. "Test back-end definition and accessors."
  1513. ;; Translate table.
  1514. (should
  1515. (equal '((headline . my-headline-test))
  1516. (let (org-export-registered-backends)
  1517. (org-export-define-backend 'test '((headline . my-headline-test)))
  1518. (org-export-get-all-transcoders 'test))))
  1519. ;; Filters.
  1520. (should
  1521. (equal '((:filter-headline . my-filter))
  1522. (let (org-export-registered-backends)
  1523. (org-export-define-backend 'test
  1524. '((headline . my-headline-test))
  1525. :filters-alist '((:filter-headline . my-filter)))
  1526. (org-export-backend-filters (org-export-get-backend 'test)))))
  1527. ;; Options.
  1528. (should
  1529. (equal '((:prop value))
  1530. (let (org-export-registered-backends)
  1531. (org-export-define-backend 'test
  1532. '((headline . my-headline-test))
  1533. :options-alist '((:prop value)))
  1534. (org-export-backend-options (org-export-get-backend 'test)))))
  1535. ;; Menu.
  1536. (should
  1537. (equal '(?k "Test Export" test)
  1538. (let (org-export-registered-backends)
  1539. (org-export-define-backend 'test
  1540. '((headline . my-headline-test))
  1541. :menu-entry '(?k "Test Export" test))
  1542. (org-export-backend-menu (org-export-get-backend 'test))))))
  1543. (ert-deftest test-org-export/define-derived-backend ()
  1544. "Test `org-export-define-derived-backend' specifications."
  1545. ;; Error when parent back-end is not defined.
  1546. (should-error
  1547. (let (org-export-registered-backends)
  1548. (org-export-define-derived-backend 'test 'parent)))
  1549. ;; Append translation table to parent's.
  1550. (should
  1551. (equal '((:headline . test) (:headline . parent))
  1552. (let (org-export-registered-backends)
  1553. (org-export-define-backend 'parent '((:headline . parent)))
  1554. (org-export-define-derived-backend 'test 'parent
  1555. :translate-alist '((:headline . test)))
  1556. (org-export-get-all-transcoders 'test))))
  1557. ;; Options defined in the new back have priority over those defined
  1558. ;; in parent.
  1559. (should
  1560. (eq 'test
  1561. (let (org-export-registered-backends)
  1562. (org-export-define-backend 'parent
  1563. '((:headline . parent))
  1564. :options-alist '((:a nil nil 'parent)))
  1565. (org-export-define-derived-backend 'test 'parent
  1566. :options-alist '((:a nil nil 'test)))
  1567. (plist-get (org-export--get-global-options
  1568. (org-export-get-backend 'test))
  1569. :a)))))
  1570. (ert-deftest test-org-export/derived-backend-p ()
  1571. "Test `org-export-derived-backend-p' specifications."
  1572. ;; Non-nil with direct match.
  1573. (should
  1574. (let (org-export-registered-backends)
  1575. (org-export-define-backend 'test '((headline . test)))
  1576. (org-export-derived-backend-p 'test 'test)))
  1577. (should
  1578. (let (org-export-registered-backends)
  1579. (org-export-define-backend 'test '((headline . test)))
  1580. (org-export-define-derived-backend 'test2 'test)
  1581. (org-export-derived-backend-p 'test2 'test2)))
  1582. ;; Non-nil with a direct parent.
  1583. (should
  1584. (let (org-export-registered-backends)
  1585. (org-export-define-backend 'test '((headline . test)))
  1586. (org-export-define-derived-backend 'test2 'test)
  1587. (org-export-derived-backend-p 'test2 'test)))
  1588. ;; Non-nil with an indirect parent.
  1589. (should
  1590. (let (org-export-registered-backends)
  1591. (org-export-define-backend 'test '((headline . test)))
  1592. (org-export-define-derived-backend 'test2 'test)
  1593. (org-export-define-derived-backend 'test3 'test2)
  1594. (org-export-derived-backend-p 'test3 'test)))
  1595. ;; Nil otherwise.
  1596. (should-not
  1597. (let (org-export-registered-backends)
  1598. (org-export-define-backend 'test '((headline . test)))
  1599. (org-export-define-backend 'test2 '((headline . test2)))
  1600. (org-export-derived-backend-p 'test2 'test)))
  1601. (should-not
  1602. (let (org-export-registered-backends)
  1603. (org-export-define-backend 'test '((headline . test)))
  1604. (org-export-define-backend 'test2 '((headline . test2)))
  1605. (org-export-define-derived-backend 'test3 'test2)
  1606. (org-export-derived-backend-p 'test3 'test))))
  1607. (ert-deftest test-org-export/get-all-transcoders ()
  1608. "Test `org-export-get-all-transcoders' specifications."
  1609. ;; Return nil when back-end cannot be found.
  1610. (should-not (org-export-get-all-transcoders nil))
  1611. ;; Same as `org-export-transcoders' if no parent.
  1612. (should
  1613. (equal '((headline . ignore))
  1614. (org-export-get-all-transcoders
  1615. (org-export-create-backend
  1616. :transcoders '((headline . ignore))))))
  1617. ;; But inherit from all ancestors whenever possible.
  1618. (should
  1619. (equal '((section . ignore) (headline . ignore))
  1620. (let (org-export-registered-backends)
  1621. (org-export-define-backend 'b1 '((headline . ignore)))
  1622. (org-export-get-all-transcoders
  1623. (org-export-create-backend
  1624. :parent 'b1 :transcoders '((section . ignore)))))))
  1625. (should
  1626. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1627. (let (org-export-registered-backends)
  1628. (org-export-define-backend 'b1 '((headline . ignore)))
  1629. (org-export-define-derived-backend 'b2 'b1
  1630. :translate-alist '((section . ignore)))
  1631. (org-export-get-all-transcoders
  1632. (org-export-create-backend
  1633. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1634. ;; Back-end transcoders overrule inherited ones.
  1635. (should
  1636. (eq 'b
  1637. (let (org-export-registered-backends)
  1638. (org-export-define-backend 'b1 '((headline . a)))
  1639. (cdr (assq 'headline
  1640. (org-export-get-all-transcoders
  1641. (org-export-create-backend
  1642. :parent 'b1 :transcoders '((headline . b))))))))))
  1643. (ert-deftest test-org-export/get-all-options ()
  1644. "Test `org-export-get-all-options' specifications."
  1645. ;; Return nil when back-end cannot be found.
  1646. (should-not (org-export-get-all-options nil))
  1647. ;; Same as `org-export-options' if no parent.
  1648. (should
  1649. (equal '((headline . ignore))
  1650. (org-export-get-all-options
  1651. (org-export-create-backend
  1652. :options '((headline . ignore))))))
  1653. ;; But inherit from all ancestors whenever possible.
  1654. (should
  1655. (equal '((:key2 value2) (:key1 value1))
  1656. (let (org-export-registered-backends)
  1657. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1658. (org-export-get-all-options
  1659. (org-export-create-backend
  1660. :parent 'b1 :options '((:key2 value2)))))))
  1661. (should
  1662. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1663. (let (org-export-registered-backends)
  1664. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1665. (org-export-define-derived-backend 'b2 'b1
  1666. :options-alist '((:key2 value2)))
  1667. (org-export-get-all-options
  1668. (org-export-create-backend
  1669. :parent 'b2 :options '((:key3 value3)))))))
  1670. ;; Back-end options overrule inherited ones.
  1671. (should
  1672. (eq 'b
  1673. (let (org-export-registered-backends)
  1674. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1675. (cdr (assq :key1
  1676. (org-export-get-all-options
  1677. (org-export-create-backend
  1678. :parent 'b1 :options '((:key1 . b))))))))))
  1679. (ert-deftest test-org-export/get-all-filters ()
  1680. "Test `org-export-get-all-filters' specifications."
  1681. ;; Return nil when back-end cannot be found.
  1682. (should-not (org-export-get-all-filters nil))
  1683. ;; Same as `org-export-filters' if no parent.
  1684. (should
  1685. (equal '((:filter-headline . ignore))
  1686. (org-export-get-all-filters
  1687. (org-export-create-backend
  1688. :filters '((:filter-headline . ignore))))))
  1689. ;; But inherit from all ancestors whenever possible.
  1690. (should
  1691. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1692. (let (org-export-registered-backends)
  1693. (org-export-define-backend 'b1
  1694. nil :filters-alist '((:filter-headline . ignore)))
  1695. (org-export-get-all-filters
  1696. (org-export-create-backend
  1697. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1698. (should
  1699. (equal '((:filter-paragraph . ignore)
  1700. (:filter-section . ignore)
  1701. (:filter-headline . ignore))
  1702. (let (org-export-registered-backends)
  1703. (org-export-define-backend 'b1
  1704. nil :filters-alist '((:filter-headline . ignore)))
  1705. (org-export-define-derived-backend 'b2 'b1
  1706. :filters-alist '((:filter-section . ignore)))
  1707. (org-export-get-all-filters
  1708. (org-export-create-backend
  1709. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1710. ;; Back-end filters overrule inherited ones.
  1711. (should
  1712. (eq 'b
  1713. (let (org-export-registered-backends)
  1714. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1715. (cdr (assq :filter-headline
  1716. (org-export-get-all-filters
  1717. (org-export-create-backend
  1718. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1719. (ert-deftest test-org-export/with-backend ()
  1720. "Test `org-export-with-backend' definition."
  1721. ;; Error when calling an undefined back-end
  1722. (should-error (org-export-with-backend nil "Test"))
  1723. ;; Error when called back-end doesn't have an appropriate
  1724. ;; transcoder.
  1725. (should-error
  1726. (org-export-with-backend
  1727. (org-export-create-backend :transcoders '((headline . ignore)))
  1728. "Test"))
  1729. ;; Otherwise, export using correct transcoder
  1730. (should
  1731. (equal "Success"
  1732. (let (org-export-registered-backends)
  1733. (org-export-define-backend 'test
  1734. '((verbatim . (lambda (text contents info) "Failure"))))
  1735. (org-export-define-backend 'test2
  1736. '((verbatim . (lambda (text contents info) "Success"))))
  1737. (org-export-with-backend 'test2 '(verbatim (:value "=Test="))))))
  1738. ;; Corner case: plain-text transcoders have a different arity.
  1739. (should
  1740. (equal "Success"
  1741. (org-export-with-backend
  1742. (org-export-create-backend
  1743. :transcoders '((plain-text . (lambda (text info) "Success"))))
  1744. "Test")))
  1745. ;; Provide correct back-end if transcoder needs to use recursive
  1746. ;; calls.
  1747. (should
  1748. (equal "Success\n"
  1749. (let ((test-back-end
  1750. (org-export-create-backend
  1751. :transcoders
  1752. (list (cons 'headline
  1753. (lambda (headline contents info)
  1754. (org-export-data
  1755. (org-element-property :title headline)
  1756. info)))
  1757. (cons 'plain-text (lambda (text info) "Success"))))))
  1758. (org-export-string-as
  1759. "* Test"
  1760. (org-export-create-backend
  1761. :transcoders
  1762. (list (cons 'headline
  1763. (lambda (headline contents info)
  1764. (org-export-with-backend
  1765. test-back-end headline contents info))))))))))
  1766. (ert-deftest test-org-export/data-with-backend ()
  1767. "Test `org-export-data-with-backend' specifications."
  1768. ;; Error when calling an undefined back-end.
  1769. (should-error (org-export-data-with-backend nil "nil" nil))
  1770. ;; Otherwise, export data recursively, using correct back-end.
  1771. (should
  1772. (equal
  1773. "Success!"
  1774. (org-export-data-with-backend
  1775. '(bold nil "Test")
  1776. (org-export-create-backend
  1777. :transcoders
  1778. '((plain-text . (lambda (text info) "Success"))
  1779. (bold . (lambda (bold contents info) (concat contents "!")))))
  1780. '(:with-emphasize t)))))
  1781. ;;; Comments
  1782. (ert-deftest test-org-export/comments ()
  1783. "Test comments handling during export.
  1784. In particular, structure of the document mustn't be altered after
  1785. comments removal."
  1786. (should
  1787. (equal "Para1\n\nPara2\n"
  1788. (org-test-with-temp-text "
  1789. Para1
  1790. # Comment
  1791. # Comment
  1792. Para2"
  1793. (org-export-as (org-test-default-backend)))))
  1794. (should
  1795. (equal "Para1\n\nPara2\n"
  1796. (org-test-with-temp-text "
  1797. Para1
  1798. # Comment
  1799. Para2"
  1800. (org-export-as (org-test-default-backend)))))
  1801. (should
  1802. (equal "[fn:1] Para1\n\n\nPara2\n"
  1803. (org-test-with-temp-text "
  1804. \[fn:1] Para1
  1805. # Inside definition
  1806. # Outside 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. # Inside definition
  1815. Para2"
  1816. (org-export-as (org-test-default-backend)))))
  1817. (should
  1818. (equal "[fn:1] Para1\n\nPara2\n"
  1819. (org-test-with-temp-text "
  1820. \[fn:1] Para1
  1821. # Inside definition
  1822. Para2"
  1823. (org-export-as (org-test-default-backend)))))
  1824. (should
  1825. (equal "[fn:1] Para1\n\nPara2\n"
  1826. (org-test-with-temp-text "
  1827. \[fn:1] Para1
  1828. # Inside definition
  1829. Para2"
  1830. (org-export-as (org-test-default-backend)))))
  1831. (should
  1832. (equal "- item 1\n\n- item 2\n"
  1833. (org-test-with-temp-text "
  1834. - item 1
  1835. # Comment
  1836. - item 2"
  1837. (org-export-as (org-test-default-backend))))))
  1838. ;;; Export blocks
  1839. (ert-deftest test-org-export/export-block ()
  1840. "Test export blocks transcoding."
  1841. (should
  1842. (equal "Success!\n"
  1843. (org-test-with-temp-text
  1844. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1845. (org-export-as
  1846. (org-export-create-backend
  1847. :transcoders '((export-block . (lambda (b _c _i)
  1848. (org-element-property :value b)))
  1849. (section . (lambda (_s c _i) c))))))))
  1850. (should
  1851. (equal "Success!\n"
  1852. (org-test-with-temp-text
  1853. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1854. (org-export-as
  1855. (org-export-create-backend
  1856. :transcoders
  1857. (list
  1858. (cons 'export-block
  1859. (lambda (b _c _i)
  1860. (and (equal (org-element-property :type b) "BACKEND")
  1861. (org-element-property :value b))))
  1862. (cons 'section (lambda (_s c _i) c)))))))))
  1863. ;;; Export Snippets
  1864. (ert-deftest test-org-export/export-snippet ()
  1865. "Test export snippets transcoding."
  1866. ;; Standard test.
  1867. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1868. (let ((backend (org-test-default-backend)))
  1869. (setf (org-export-backend-name backend) 'test)
  1870. (setf (org-export-backend-transcoders backend)
  1871. (cons (cons 'export-snippet
  1872. (lambda (snippet contents info)
  1873. (when (eq (org-export-snippet-backend snippet) 'test)
  1874. (org-element-property :value snippet))))
  1875. (org-export-backend-transcoders backend)))
  1876. (let ((org-export-snippet-translation-alist nil))
  1877. (should (equal (org-export-as backend) "A\n")))
  1878. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1879. (should (equal (org-export-as backend) "AB\n")))))
  1880. ;; Ignored export snippets do not remove any blank.
  1881. (should
  1882. (equal "begin end\n"
  1883. (org-test-with-parsed-data "begin @@test:A@@ end"
  1884. (org-export-data-with-backend
  1885. tree
  1886. (org-export-create-backend
  1887. :transcoders
  1888. '((paragraph . (lambda (paragraph contents info) contents))
  1889. (section . (lambda (section contents info) contents))))
  1890. info)))))
  1891. ;;; Filters
  1892. (ert-deftest test-org-export/filter-apply-functions ()
  1893. "Test `org-export-filter-apply-functions' specifications."
  1894. ;; Functions are applied in order and return values are reduced.
  1895. (should
  1896. (equal "210"
  1897. (org-export-filter-apply-functions
  1898. (list (lambda (value &rest _) (concat "1" value))
  1899. (lambda (value &rest _) (concat "2" value)))
  1900. "0" nil)))
  1901. ;; Functions returning nil are skipped.
  1902. (should
  1903. (equal "20"
  1904. (org-export-filter-apply-functions
  1905. (list #'ignore (lambda (value &rest _) (concat "2" value)))
  1906. "0" nil)))
  1907. ;; If all functions are skipped, return the initial value.
  1908. (should
  1909. (equal "0"
  1910. (org-export-filter-apply-functions (list #'ignore) "0" nil)))
  1911. ;; If any function returns the empty string, final value is the
  1912. ;; empty string.
  1913. (should
  1914. (equal ""
  1915. (org-export-filter-apply-functions
  1916. (list (lambda (value &rest _) "")
  1917. (lambda (value &rest _) (concat "2" value)))
  1918. "0" nil)))
  1919. ;; Any function returning the empty string short-circuits the
  1920. ;; process.
  1921. (should
  1922. (org-export-filter-apply-functions
  1923. (list (lambda (value &rest _) "")
  1924. (lambda (value &rest _) (error "This shouldn't happen")))
  1925. "0" nil)))
  1926. ;;; Footnotes
  1927. (ert-deftest test-org-export/footnote-first-reference-p ()
  1928. "Test `org-export-footnote-first-reference-p' specifications."
  1929. (should
  1930. (equal
  1931. '(t nil)
  1932. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1933. (let (result)
  1934. (org-export-as
  1935. (org-export-create-backend
  1936. :transcoders
  1937. `(,(cons 'footnote-reference
  1938. (lambda (f c i)
  1939. (push (org-export-footnote-first-reference-p f i)
  1940. result)
  1941. ""))
  1942. (section . (lambda (s c i) c))
  1943. (paragraph . (lambda (p c i) c))))
  1944. nil nil nil '(:with-footnotes t))
  1945. (nreverse result)))))
  1946. ;; Limit check to DATA, when non-nil.
  1947. (should
  1948. (equal
  1949. '(nil t)
  1950. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1951. (let (result)
  1952. (org-element-map tree 'footnote-reference
  1953. (lambda (ref)
  1954. (push
  1955. (org-export-footnote-first-reference-p
  1956. ref info (org-element-map tree 'headline #'identity info t))
  1957. result))
  1958. info)
  1959. (nreverse result)))))
  1960. (should
  1961. (equal
  1962. '(t nil)
  1963. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1964. (let (result)
  1965. (org-element-map tree 'footnote-reference
  1966. (lambda (ref)
  1967. (push (org-export-footnote-first-reference-p ref info) result))
  1968. info)
  1969. (nreverse result)))))
  1970. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1971. ;; in the main body of the document. Otherwise, enter footnote
  1972. ;; definitions when they are encountered.
  1973. (should
  1974. (equal
  1975. '(t nil)
  1976. (org-test-with-temp-text
  1977. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1978. (let (result)
  1979. (org-export-as
  1980. (org-export-create-backend
  1981. :transcoders
  1982. `(,(cons 'footnote-reference
  1983. (lambda (f c i)
  1984. (when (org-element-lineage f '(drawer))
  1985. (push (org-export-footnote-first-reference-p f i nil)
  1986. result))
  1987. ""))
  1988. (drawer . (lambda (d c i) c))
  1989. (footnote-definition . (lambda (d c i) c))
  1990. (section . (lambda (s c i) c))
  1991. (paragraph . (lambda (p c i) c))))
  1992. nil nil nil '(:with-footnotes t))
  1993. (nreverse result)))))
  1994. (should
  1995. (equal
  1996. '(t t)
  1997. (org-test-with-temp-text
  1998. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1999. (let (result)
  2000. (org-export-as
  2001. (org-export-create-backend
  2002. :transcoders
  2003. `(,(cons 'footnote-reference
  2004. (lambda (f c i)
  2005. (when (org-element-lineage f '(drawer))
  2006. (push (org-export-footnote-first-reference-p f i nil t)
  2007. result))
  2008. ""))
  2009. (drawer . (lambda (d c i) c))
  2010. (footnote-definition . (lambda (d c i) c))
  2011. (section . (lambda (s c i) c))
  2012. (paragraph . (lambda (p c i) c))))
  2013. nil nil nil '(:with-footnotes t))
  2014. (nreverse result))))))
  2015. (ert-deftest test-org-export/get-footnote-number ()
  2016. "Test `org-export-get-footnote-number' specifications."
  2017. (should
  2018. (equal '(1 2 1)
  2019. (org-test-with-parsed-data
  2020. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  2021. (org-element-map tree 'footnote-reference
  2022. (lambda (ref) (org-export-get-footnote-number ref info))
  2023. info))))
  2024. ;; Anonymous footnotes all get a new number.
  2025. (should
  2026. (equal '(1 2)
  2027. (org-test-with-parsed-data
  2028. "Text[fn::anon1][fn::anon2]"
  2029. (org-element-map tree 'footnote-reference
  2030. (lambda (ref) (org-export-get-footnote-number ref info))
  2031. info))))
  2032. ;; Test nested footnotes order.
  2033. (should
  2034. (equal
  2035. '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
  2036. (org-test-with-parsed-data
  2037. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  2038. (org-element-map tree 'footnote-reference
  2039. (lambda (ref)
  2040. (cons (org-export-get-footnote-number ref info)
  2041. (org-element-property :label ref)))
  2042. info))))
  2043. ;; Limit number to provided DATA, when non-nil.
  2044. (should
  2045. (equal
  2046. '(1)
  2047. (org-test-with-parsed-data
  2048. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2049. (org-element-map tree 'footnote-reference
  2050. (lambda (ref)
  2051. (org-export-get-footnote-number
  2052. ref info (org-element-map tree 'headline #'identity info t)))
  2053. info))))
  2054. (should
  2055. (equal
  2056. '(1 2)
  2057. (org-test-with-parsed-data
  2058. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  2059. (org-element-map tree 'footnote-reference
  2060. (lambda (ref) (org-export-get-footnote-number ref info))
  2061. info))))
  2062. ;; With a non-nil BODY-FIRST optional argument, first check body,
  2063. ;; then footnote definitions.
  2064. (should
  2065. (equal
  2066. '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
  2067. (org-test-with-parsed-data
  2068. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2069. (org-element-map tree 'footnote-reference
  2070. (lambda (ref)
  2071. (cons (org-element-property :label ref)
  2072. (org-export-get-footnote-number ref info nil t)))
  2073. info))))
  2074. (should
  2075. (equal
  2076. '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
  2077. (org-test-with-parsed-data
  2078. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2079. (org-element-map tree 'footnote-reference
  2080. (lambda (ref)
  2081. (cons (org-element-property :label ref)
  2082. (org-export-get-footnote-number ref info nil)))
  2083. info)))))
  2084. (ert-deftest test-org-export/get-footnote-definition ()
  2085. "Test `org-export-get-footnote-definition' specifications."
  2086. ;; Standard test.
  2087. (should
  2088. (equal "A\n"
  2089. (org-element-interpret-data
  2090. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  2091. (org-export-get-footnote-definition
  2092. (org-element-map tree 'footnote-reference #'identity nil t)
  2093. info)))))
  2094. ;; Raise an error if no definition is found.
  2095. (should-error
  2096. (org-test-with-parsed-data "Text[fn:1]"
  2097. (org-export-get-footnote-definition
  2098. (org-element-map tree 'footnote-reference #'identity nil t)
  2099. info)))
  2100. ;; Find inline definitions.
  2101. (should
  2102. (equal "A"
  2103. (org-element-interpret-data
  2104. (org-test-with-parsed-data "Text[fn:1:A]"
  2105. (org-export-get-footnote-definition
  2106. (org-element-map tree 'footnote-reference #'identity nil t)
  2107. info)))))
  2108. ;; Find anonymous definitions.
  2109. (should
  2110. (equal "A"
  2111. (org-element-interpret-data
  2112. (org-test-with-parsed-data "Text[fn::A]"
  2113. (org-export-get-footnote-definition
  2114. (org-element-map tree 'footnote-reference #'identity nil t)
  2115. info)))))
  2116. ;; Find empty definitions.
  2117. (should
  2118. (equal ""
  2119. (org-element-interpret-data
  2120. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  2121. (org-export-get-footnote-definition
  2122. (org-element-map tree 'footnote-reference #'identity nil t)
  2123. info)))))
  2124. (should
  2125. (equal ""
  2126. (org-element-interpret-data
  2127. (org-test-with-parsed-data "Text[fn:1:]"
  2128. (org-export-get-footnote-definition
  2129. (org-element-map tree 'footnote-reference #'identity nil t)
  2130. info)))))
  2131. (should
  2132. (equal ""
  2133. (org-element-interpret-data
  2134. (org-test-with-parsed-data "Text[fn::]"
  2135. (org-export-get-footnote-definition
  2136. (org-element-map tree 'footnote-reference #'identity nil t)
  2137. info))))))
  2138. (ert-deftest test-org-export/collect-footnote-definitions ()
  2139. "Test `org-export-collect-footnote-definitions' specifications."
  2140. (should
  2141. (= 4
  2142. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2143. \[fn:2] B [fn:3] [fn::D].
  2144. \[fn:3] C."
  2145. (length (org-export-collect-footnote-definitions info)))))
  2146. ;; Limit number to provided DATA, when non-nil.
  2147. (should
  2148. (equal
  2149. '((1 "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
  2153. (org-export-collect-footnote-definitions
  2154. info (org-element-map tree 'headline #'identity info t))))))
  2155. (should
  2156. (equal
  2157. '((1 "1") (2 "2"))
  2158. (org-test-with-parsed-data
  2159. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2160. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  2161. ;; With optional argument BODY-FIRST, first check body, then
  2162. ;; footnote definitions.
  2163. (should
  2164. (equal '("1" "3" "2" nil)
  2165. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2166. \[fn:2] B [fn:3] [fn::D].
  2167. \[fn:3] C."
  2168. (mapcar (lambda (e) (nth 1 e))
  2169. (org-export-collect-footnote-definitions info nil t)))))
  2170. (should-not
  2171. (equal '("1" "3" "2" nil)
  2172. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2173. \[fn:2] B [fn:3] [fn::D].
  2174. \[fn:3] C."
  2175. (mapcar (lambda (e) (nth 1 e))
  2176. (org-export-collect-footnote-definitions info))))))
  2177. (ert-deftest test-org-export/footnotes ()
  2178. "Miscellaneous tests on footnotes."
  2179. (let ((org-footnote-section nil)
  2180. (org-export-with-footnotes t))
  2181. ;; Read every type of footnote.
  2182. (should
  2183. (equal
  2184. '((1 . "A\n") (2 . "C") (3 . "D"))
  2185. (org-test-with-parsed-data
  2186. "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
  2187. (org-element-map tree 'footnote-reference
  2188. (lambda (ref)
  2189. (let ((def (org-export-get-footnote-definition ref info)))
  2190. (cons (org-export-get-footnote-number ref info)
  2191. (if (eq (org-element-property :type ref) 'inline) (car def)
  2192. (car (org-element-contents
  2193. (car (org-element-contents def))))))))
  2194. info))))
  2195. ;; Export nested footnote in invisible definitions.
  2196. (should
  2197. (= 2
  2198. (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
  2199. (narrow-to-region (point) (line-end-position))
  2200. (catch 'exit
  2201. (org-export-as
  2202. (org-export-create-backend
  2203. :transcoders
  2204. '((section
  2205. .
  2206. (lambda (s c i)
  2207. (throw 'exit (length
  2208. (org-export-collect-footnote-definitions
  2209. i))))))))))))
  2210. ;; Export footnotes defined outside parsing scope.
  2211. (should
  2212. (string-match
  2213. "Out of scope"
  2214. (org-test-with-temp-text "[fn:1] Out of scope
  2215. * Title
  2216. <point>Paragraph[fn:1]"
  2217. (org-export-as (org-test-default-backend) 'subtree))))
  2218. (should
  2219. (string-match
  2220. "Out of scope"
  2221. (org-test-with-temp-text "[fn:1] Out of scope
  2222. * Title
  2223. <point>Paragraph[fn:1]"
  2224. (narrow-to-region (point) (point-max))
  2225. (org-export-as (org-test-default-backend)))))
  2226. ;; Export nested footnotes defined outside parsing scope.
  2227. (should
  2228. (string-match
  2229. "Very out of scope"
  2230. (org-test-with-temp-text "
  2231. \[fn:1] Out of scope[fn:2]
  2232. \[fn:2] Very out of scope
  2233. * Title
  2234. <point>Paragraph[fn:1]"
  2235. (org-export-as (org-test-default-backend) 'subtree))))
  2236. (should
  2237. (string-match
  2238. "Very out of scope"
  2239. (org-test-with-temp-text "
  2240. \[fn:1] Out of scope[fn:2]
  2241. \[fn:2] Very out of scope
  2242. * Title
  2243. <point>Paragraph[fn:1]"
  2244. (narrow-to-region (point) (point-max))
  2245. (org-export-as (org-test-default-backend)))))
  2246. (should
  2247. (string-match
  2248. "D2"
  2249. (org-test-with-temp-text "
  2250. \[fn:1] Out of scope[fn:2:D2]
  2251. * Title
  2252. <point>Paragraph[fn:1]"
  2253. (narrow-to-region (point) (point-max))
  2254. (org-export-as (org-test-default-backend)))))
  2255. ;; Export footnotes in pruned parts of tree.
  2256. (should
  2257. (string-match
  2258. "Definition"
  2259. (let ((org-export-exclude-tags '("noexport")))
  2260. (org-test-with-temp-text
  2261. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  2262. (org-export-as (org-test-default-backend))))))
  2263. (should
  2264. (string-match
  2265. "Definition"
  2266. (let ((org-export-select-tags '("export")))
  2267. (org-test-with-temp-text
  2268. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2269. (org-export-as (org-test-default-backend))))))
  2270. ;; Export nested footnotes in pruned parts of tree.
  2271. (should
  2272. (string-match
  2273. "D2"
  2274. (let ((org-export-exclude-tags '("noexport")))
  2275. (org-test-with-temp-text
  2276. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2277. (org-export-as (org-test-default-backend))))))
  2278. (should
  2279. (string-match
  2280. "D2"
  2281. (let ((org-export-select-tags '("export")))
  2282. (org-test-with-temp-text
  2283. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2284. (org-export-as (org-test-default-backend))))))
  2285. ;; Handle uninterpreted data in pruned footnote definitions.
  2286. (should-not
  2287. (string-match
  2288. "|"
  2289. (let ((org-export-with-tables nil))
  2290. (org-test-with-temp-text
  2291. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2292. (org-export-as (org-test-default-backend))))))
  2293. ;; Footnotes without a definition should throw an error.
  2294. (should-error
  2295. (org-test-with-parsed-data "Text[fn:1]"
  2296. (org-export-get-footnote-definition
  2297. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2298. ;; Footnote section should be ignored in TOC and in headlines
  2299. ;; numbering.
  2300. (should
  2301. (= 1 (let ((org-footnote-section "Footnotes"))
  2302. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2303. (org-export-collect-headlines info))))))
  2304. (should
  2305. (equal '(2)
  2306. (let ((org-footnote-section "Footnotes"))
  2307. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2308. (org-element-map tree 'headline
  2309. (lambda (hl)
  2310. (when (equal (org-element-property :raw-value hl) "H2")
  2311. (org-export-get-headline-number hl info)))
  2312. info t)))))))
  2313. ;;; Headlines and Inlinetasks
  2314. (ert-deftest test-org-export/get-relative-level ()
  2315. "Test `org-export-get-relative-level' specifications."
  2316. ;; Standard test.
  2317. (should
  2318. (equal '(1 2)
  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. ;; Missing levels
  2325. (should
  2326. (equal '(1 3)
  2327. (let ((org-odd-levels-only nil))
  2328. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2329. (org-element-map tree 'headline
  2330. (lambda (h) (org-export-get-relative-level h info))
  2331. info))))))
  2332. (ert-deftest test-org-export/low-level-p ()
  2333. "Test `org-export-low-level-p' specifications."
  2334. (should
  2335. (equal
  2336. '(no yes)
  2337. (let ((org-odd-levels-only nil))
  2338. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2339. (org-element-map tree 'headline
  2340. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2341. (plist-put info :headline-levels 1)))))))
  2342. (ert-deftest test-org-export/get-headline-number ()
  2343. "Test `org-export-get-headline-number' specifications."
  2344. ;; Standard test.
  2345. (should
  2346. (equal
  2347. '((1) (1 1))
  2348. (let ((org-odd-levels-only nil))
  2349. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2350. (org-element-map tree 'headline
  2351. (lambda (h) (org-export-get-headline-number h info))
  2352. info)))))
  2353. ;; Missing levels are replaced with 0.
  2354. (should
  2355. (equal
  2356. '((1) (1 0 1))
  2357. (let ((org-odd-levels-only nil))
  2358. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2359. (org-element-map tree 'headline
  2360. (lambda (h) (org-export-get-headline-number h info))
  2361. info))))))
  2362. (ert-deftest test-org-export/numbered-headline-p ()
  2363. "Test `org-export-numbered-headline-p' specifications."
  2364. ;; If `:section-numbers' is nil, never number headlines.
  2365. (should-not
  2366. (org-test-with-parsed-data "* Headline"
  2367. (org-element-map tree 'headline
  2368. (lambda (h) (org-export-numbered-headline-p h info))
  2369. (plist-put info :section-numbers nil))))
  2370. ;; If `:section-numbers' is a number, only number headlines with
  2371. ;; a level greater that it.
  2372. (should
  2373. (equal
  2374. '(yes no)
  2375. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2376. (org-element-map tree 'headline
  2377. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2378. (plist-put info :section-numbers 1)))))
  2379. ;; Otherwise, headlines are always numbered.
  2380. (should
  2381. (org-test-with-parsed-data "* Headline"
  2382. (org-element-map tree 'headline
  2383. (lambda (h) (org-export-numbered-headline-p h info))
  2384. (plist-put info :section-numbers t))))
  2385. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2386. (should-not
  2387. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2388. (org-export-numbered-headline-p
  2389. (org-element-map tree 'headline 'identity info t)
  2390. info)))
  2391. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2392. (should-not
  2393. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2394. (org-export-numbered-headline-p
  2395. (org-element-map tree 'headline #'identity info t)
  2396. info)))
  2397. ;; UNNUMBERED is inherited.
  2398. (should
  2399. (equal '(unnumbered numbered unnumbered)
  2400. (org-test-with-parsed-data
  2401. "* H
  2402. :PROPERTIES:
  2403. :UNNUMBERED: t
  2404. :END:
  2405. ** H2
  2406. :PROPERTIES:
  2407. :UNNUMBERED: nil
  2408. :END:
  2409. ** H3"
  2410. (org-element-map tree 'headline
  2411. (lambda (h)
  2412. (if (org-export-numbered-headline-p h info) 'numbered
  2413. 'unnumbered))
  2414. info)))))
  2415. (ert-deftest test-org-export/number-to-roman ()
  2416. "Test `org-export-number-to-roman' specifications."
  2417. ;; If number is negative, return it as a string.
  2418. (should (equal (org-export-number-to-roman -1) "-1"))
  2419. ;; Otherwise, return it as a roman number.
  2420. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2421. (ert-deftest test-org-export/get-optional-title ()
  2422. "Test `org-export-get-alt-title' specifications."
  2423. ;; If ALT_TITLE property is defined, use it.
  2424. (should
  2425. (equal '("opt")
  2426. (org-test-with-parsed-data
  2427. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2428. (org-export-get-alt-title
  2429. (org-element-map tree 'headline 'identity info t)
  2430. info))))
  2431. ;; Otherwise, fall-back to regular title.
  2432. (should
  2433. (equal '("Headline")
  2434. (org-test-with-parsed-data "* Headline"
  2435. (org-export-get-alt-title
  2436. (org-element-map tree 'headline 'identity info t)
  2437. info)))))
  2438. (ert-deftest test-org-export/get-tags ()
  2439. "Test `org-export-get-tags' specifications."
  2440. ;; Standard test: tags which are not a select tag, an exclude tag,
  2441. ;; or specified as optional argument shouldn't be ignored.
  2442. (should
  2443. (org-test-with-parsed-data "* Headline :tag:"
  2444. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2445. info)))
  2446. ;; Tags provided in the optional argument are ignored.
  2447. (should-not
  2448. (org-test-with-parsed-data "* Headline :ignore:"
  2449. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2450. info '("ignore"))))
  2451. ;; Allow tag inheritance.
  2452. (should
  2453. (equal
  2454. '(("tag") ("tag"))
  2455. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2456. (org-element-map tree 'headline
  2457. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2458. ;; Tag inheritance checks FILETAGS keywords.
  2459. (should
  2460. (equal
  2461. '(("a" "b" "tag"))
  2462. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2463. (org-element-map tree 'headline
  2464. (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
  2465. (ert-deftest test-org-export/get-node-property ()
  2466. "Test`org-export-get-node-property' specifications."
  2467. ;; Standard test.
  2468. (should
  2469. (equal "value"
  2470. (org-test-with-parsed-data "* Headline
  2471. :PROPERTIES:
  2472. :prop: value
  2473. :END:"
  2474. (org-export-get-node-property
  2475. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2476. ;; Test inheritance.
  2477. (should
  2478. (equal "value"
  2479. (org-test-with-parsed-data "* Parent
  2480. :PROPERTIES:
  2481. :prop: value
  2482. :END:
  2483. ** Headline
  2484. Paragraph"
  2485. (org-export-get-node-property
  2486. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2487. ;; Cannot return a value before the first headline.
  2488. (should-not
  2489. (org-test-with-parsed-data "Paragraph
  2490. * Headline
  2491. :PROPERTIES:
  2492. :prop: value
  2493. :END:"
  2494. (org-export-get-node-property
  2495. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2496. (ert-deftest test-org-export/get-category ()
  2497. "Test `org-export-get-category' specifications."
  2498. ;; Standard test.
  2499. (should
  2500. (equal "value"
  2501. (org-test-with-parsed-data "* Headline
  2502. :PROPERTIES:
  2503. :CATEGORY: value
  2504. :END:"
  2505. (org-export-get-category
  2506. (org-element-map tree 'headline 'identity nil t) info))))
  2507. ;; Test inheritance from a parent headline.
  2508. (should
  2509. (equal '("value" "value")
  2510. (org-test-with-parsed-data "* Headline1
  2511. :PROPERTIES:
  2512. :CATEGORY: value
  2513. :END:
  2514. ** Headline2"
  2515. (org-element-map tree 'headline
  2516. (lambda (hl) (org-export-get-category hl info)) info))))
  2517. ;; Test inheritance from #+CATEGORY keyword
  2518. (should
  2519. (equal "value"
  2520. (org-test-with-parsed-data "#+CATEGORY: value
  2521. * Headline"
  2522. (org-export-get-category
  2523. (org-element-map tree 'headline 'identity nil t) info))))
  2524. ;; Test inheritance from file name.
  2525. (should
  2526. (equal "test"
  2527. (org-test-with-parsed-data "* Headline"
  2528. (let ((info (plist-put info :input-file "~/test.org")))
  2529. (org-export-get-category
  2530. (org-element-map tree 'headline 'identity nil t) info)))))
  2531. ;; Fall-back value.
  2532. (should
  2533. (equal "???"
  2534. (org-test-with-parsed-data "* Headline"
  2535. (org-export-get-category
  2536. (org-element-map tree 'headline 'identity nil t) info)))))
  2537. (ert-deftest test-org-export/first-sibling-p ()
  2538. "Test `org-export-first-sibling-p' specifications."
  2539. ;; Standard test.
  2540. (should
  2541. (equal
  2542. '(yes yes no)
  2543. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2544. (org-element-map tree 'headline
  2545. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2546. info))))
  2547. (should
  2548. (equal '(yes no)
  2549. (org-test-with-parsed-data "- item\n\n para"
  2550. (org-element-map tree 'paragraph
  2551. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2552. info))))
  2553. ;; Ignore sections for headlines.
  2554. (should
  2555. (equal '(yes yes)
  2556. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2557. (org-element-map tree 'headline
  2558. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2559. info))))
  2560. ;; Ignore headlines not exported.
  2561. (should
  2562. (equal
  2563. '(yes)
  2564. (let ((org-export-exclude-tags '("ignore")))
  2565. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2566. (org-element-map tree 'headline
  2567. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2568. info))))))
  2569. (ert-deftest test-org-export/last-sibling-p ()
  2570. "Test `org-export-last-sibling-p' specifications."
  2571. ;; Standard test.
  2572. (should
  2573. (equal
  2574. '(yes no yes)
  2575. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2576. (org-element-map tree 'headline
  2577. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2578. info))))
  2579. (should
  2580. (equal '(no yes)
  2581. (org-test-with-parsed-data "- item\n\n para"
  2582. (org-element-map tree 'paragraph
  2583. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2584. info))))
  2585. ;; Ignore headlines not exported.
  2586. (should
  2587. (equal
  2588. '(yes)
  2589. (let ((org-export-exclude-tags '("ignore")))
  2590. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2591. (org-element-map tree 'headline
  2592. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2593. info)))))
  2594. ;; Handle gracefully discontinuous headings.
  2595. (should
  2596. (equal '(yes yes)
  2597. (org-test-with-parsed-data "** S\n* H"
  2598. (org-element-map tree 'headline
  2599. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no)))))))
  2600. (ert-deftest test-org-export/handle-inlinetasks ()
  2601. "Test inlinetask export."
  2602. ;; Inlinetask with an exclude tag.
  2603. (when (featurep 'org-inlinetask)
  2604. (should
  2605. (equal
  2606. ""
  2607. (let ((org-inlinetask-min-level 3)
  2608. org-export-filter-body-functions
  2609. org-export-filter-final-output-functions)
  2610. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2611. (org-export-as (org-test-default-backend)
  2612. nil nil nil '(:exclude-tags ("noexp")))))))
  2613. ;; Inlinetask with an include tag.
  2614. (should
  2615. (equal
  2616. "* H2\n*** Inline :exp:\n"
  2617. (let ((org-inlinetask-min-level 3)
  2618. (org-tags-column 0))
  2619. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2620. (org-export-as (org-test-default-backend)
  2621. nil nil nil '(:select-tags ("exp")))))))
  2622. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2623. (should
  2624. (equal ""
  2625. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2626. (org-inlinetask-min-level 3)
  2627. org-export-filter-body-functions
  2628. org-export-filter-final-output-functions)
  2629. (org-test-with-temp-text "*** TODO Inline"
  2630. (org-export-as (org-test-default-backend)
  2631. nil nil nil '(:with-tasks nil))))))))
  2632. ;;; Keywords
  2633. (ert-deftest test-org-export/get-date ()
  2634. "Test `org-export-get-date' specifications."
  2635. ;; Return a properly formatted string when
  2636. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2637. ;; consists in a single timestamp.
  2638. (should
  2639. (equal "29 03 2012"
  2640. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2641. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2642. (org-export-get-date info)))))
  2643. ;; Return a secondary string otherwise.
  2644. (should-not
  2645. (stringp
  2646. (let ((org-export-date-timestamp-format nil))
  2647. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2648. (org-export-get-date info)))))
  2649. (should
  2650. (equal '("Date")
  2651. (org-test-with-parsed-data "#+DATE: Date"
  2652. (org-export-get-date info))))
  2653. ;; Optional argument has precedence over
  2654. ;; `org-export-date-timestamp-format'.
  2655. (should
  2656. (equal "29 03"
  2657. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2658. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2659. (org-export-get-date info "%d %m"))))))
  2660. ;;; Links
  2661. (ert-deftest test-org-export/custom-protocol-maybe ()
  2662. "Test `org-export-custom-protocol-maybe' specifications."
  2663. (should
  2664. (string-match
  2665. "success"
  2666. (progn
  2667. (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
  2668. (org-export-string-as
  2669. "[[foo:path]]"
  2670. (org-export-create-backend
  2671. :name 'test
  2672. :transcoders
  2673. '((section . (lambda (s c i) c))
  2674. (paragraph . (lambda (p c i) c))
  2675. (link . (lambda (l c i)
  2676. (or (org-export-custom-protocol-maybe l c 'test)
  2677. "failure")))))))))
  2678. (should-not
  2679. (string-match
  2680. "success"
  2681. (progn
  2682. (org-link-set-parameters
  2683. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2684. (org-export-string-as
  2685. "[[foo:path]]"
  2686. (org-export-create-backend
  2687. :name 'no-test
  2688. :transcoders
  2689. '((section . (lambda (s c i) c))
  2690. (paragraph . (lambda (p c i) c))
  2691. (link . (lambda (l c i)
  2692. (or (org-export-custom-protocol-maybe l c 'no-test)
  2693. "failure")))))))))
  2694. ;; Ignore anonymous back-ends.
  2695. (should-not
  2696. (string-match
  2697. "success"
  2698. (progn
  2699. (org-link-set-parameters
  2700. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2701. (org-export-string-as
  2702. "[[foo:path]]"
  2703. (org-export-create-backend
  2704. :transcoders
  2705. '((section . (lambda (s c i) c))
  2706. (paragraph . (lambda (p c i) c))
  2707. (link . (lambda (l c i)
  2708. (or (org-export-custom-protocol-maybe l c nil)
  2709. "failure"))))))))))
  2710. (ert-deftest test-org-export/get-coderef-format ()
  2711. "Test `org-export-get-coderef-format' specifications."
  2712. ;; A link without description returns "%s"
  2713. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2714. "%s"))
  2715. ;; Return "%s" when path is matched within description.
  2716. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2717. "desc %s"))
  2718. ;; Otherwise return description.
  2719. (should (equal (org-export-get-coderef-format "path" "desc")
  2720. "desc")))
  2721. (ert-deftest test-org-export/inline-image-p ()
  2722. "Test `org-export-inline-image-p' specifications."
  2723. (should
  2724. (org-export-inline-image-p
  2725. (org-test-with-temp-text "[[#id]]"
  2726. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2727. '(("custom-id" . "id")))))
  2728. (ert-deftest test-org-export/insert-image-links ()
  2729. "Test `org-export-insert-image-links' specifications."
  2730. (should-not
  2731. (member "file"
  2732. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2733. (org-element-map tree 'link
  2734. (lambda (l) (org-element-property :type l))))))
  2735. (should
  2736. (member "file"
  2737. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2738. (org-element-map (org-export-insert-image-links tree info) 'link
  2739. (lambda (l) (org-element-property :type l))))))
  2740. ;; Properly set `:parent' property when replace contents with image
  2741. ;; link.
  2742. (should
  2743. (memq 'link
  2744. (org-test-with-parsed-data "[[https://orgmode.org][file:image.png]]"
  2745. (org-element-map (org-export-insert-image-links tree info) 'link
  2746. (lambda (l)
  2747. (org-element-type (org-element-property :parent l)))))))
  2748. ;; With optional argument RULES, recognize different links as
  2749. ;; images.
  2750. (should-not
  2751. (member "file"
  2752. (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
  2753. (org-element-map (org-export-insert-image-links tree info) 'link
  2754. (lambda (l) (org-element-property :type l))))))
  2755. (should
  2756. (member "file"
  2757. (org-test-with-parsed-data "[[https://orgmode.org][file:image.xxx]]"
  2758. (org-element-map
  2759. (org-export-insert-image-links tree info '(("file" . "xxx")))
  2760. 'link
  2761. (lambda (l) (org-element-property :type l)))))))
  2762. (ert-deftest test-org-export/fuzzy-link ()
  2763. "Test fuzzy links specifications."
  2764. ;; Link to an headline should return headline's number.
  2765. (should
  2766. ;; Note: Headline's number is in fact a list of numbers.
  2767. (equal '(2)
  2768. (org-test-with-parsed-data
  2769. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2770. (org-element-map tree 'link
  2771. (lambda (link)
  2772. (org-export-get-ordinal
  2773. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2774. ;; Link to a target in an item should return item's number.
  2775. (should
  2776. ;; Note: Item's number is in fact a list of numbers.
  2777. (equal '(1 2)
  2778. (org-test-with-parsed-data
  2779. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2780. (org-element-map tree 'link
  2781. (lambda (link)
  2782. (org-export-get-ordinal
  2783. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2784. ;; Link to a target in a footnote should return footnote's number.
  2785. (should
  2786. (equal '(2 3)
  2787. (org-test-with-parsed-data "
  2788. Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
  2789. \[fn:1] A
  2790. \[fn:2] <<test>>B"
  2791. (org-element-map tree 'link
  2792. (lambda (link)
  2793. (org-export-get-ordinal
  2794. (org-export-resolve-fuzzy-link link info) info)) info))))
  2795. ;; Link to a named element should return sequence number of that
  2796. ;; element.
  2797. (should
  2798. (= 2
  2799. (org-test-with-parsed-data
  2800. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2801. (org-element-map tree 'link
  2802. (lambda (link)
  2803. (org-export-get-ordinal
  2804. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2805. ;; Link to a target not within an item, a table, a footnote
  2806. ;; reference or definition should return section number.
  2807. (should
  2808. (equal '(2)
  2809. (org-test-with-parsed-data
  2810. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2811. (org-element-map tree 'link
  2812. (lambda (link)
  2813. (org-export-get-ordinal
  2814. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2815. ;; Space are not significant when matching a fuzzy link.
  2816. (should
  2817. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2818. (org-element-map tree 'link
  2819. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2820. info t)))
  2821. ;; Statistics cookies are ignored for headline match.
  2822. (should
  2823. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2824. (org-element-map tree 'link
  2825. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2826. info t)))
  2827. (should
  2828. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2829. (org-element-map tree 'link
  2830. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2831. info t))))
  2832. (defun test-org-gen-loc-list(text type)
  2833. (org-test-with-parsed-data text
  2834. (org-element-map tree type
  2835. (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
  2836. (ert-deftest test-org-export/get-loc ()
  2837. "Test `org-export-get-loc' specifications."
  2838. (should
  2839. ;; "-n" resets line number.
  2840. (equal '(0)
  2841. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
  2842. 'example-block)))
  2843. ;; The first "+n" has 0 lines before it
  2844. (should
  2845. (equal '(0)
  2846. (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
  2847. 'example-block)))
  2848. ;; "-n 10" resets line number but has "9 lines" before it.
  2849. (should
  2850. (equal '(9)
  2851. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
  2852. 'example-block)))
  2853. ;; -n10 with two lines then +n 15
  2854. (should
  2855. (equal '(9 25)
  2856. (test-org-gen-loc-list "
  2857. #+BEGIN_EXAMPLE -n 10
  2858. Text_10
  2859. Second line(11)
  2860. #+END_EXAMPLE
  2861. #+BEGIN_EXAMPLE +n 15
  2862. Text line (11 + 15)
  2863. #+END_EXAMPLE"
  2864. 'example-block)))
  2865. (should
  2866. (equal '(9 19 0)
  2867. (test-org-gen-loc-list "
  2868. #+BEGIN_EXAMPLE -n 10
  2869. Text
  2870. #+END_EXAMPLE
  2871. #+BEGIN_EXAMPLE +n 10
  2872. Text
  2873. #+END_EXAMPLE
  2874. #+BEGIN_EXAMPLE -n
  2875. Text
  2876. #+END_EXAMPLE"
  2877. 'example-block)))
  2878. ;; an Example Block without -n does not add to the line count.
  2879. (should
  2880. (equal '(9 no-loc 19)
  2881. (test-org-gen-loc-list "
  2882. #+BEGIN_EXAMPLE -n 10
  2883. Text
  2884. #+END_EXAMPLE
  2885. #+BEGIN_EXAMPLE
  2886. Text
  2887. #+END_EXAMPLE
  2888. #+BEGIN_EXAMPLE +n 10
  2889. Text
  2890. #+END_EXAMPLE"
  2891. 'example-block)))
  2892. ;; "-n" resets line number.
  2893. (should
  2894. (equal
  2895. '(0)
  2896. (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
  2897. 'src-block)))
  2898. ;; The first "+n" has 0 lines before it.
  2899. (should
  2900. (equal '(0)
  2901. (test-org-gen-loc-list
  2902. "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
  2903. 'src-block)))
  2904. ;; "-n 10" resets line number but has "9 lines" before it.
  2905. (should
  2906. (equal '(9)
  2907. (test-org-gen-loc-list
  2908. "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
  2909. 'src-block)))
  2910. (should
  2911. (equal '(9 25)
  2912. (test-org-gen-loc-list "
  2913. #+BEGIN_SRC emacs-lisp -n 10
  2914. (- 10 1)
  2915. (+ (- 10 1) 1)
  2916. #+END_SRC
  2917. #+BEGIN_SRC emacs-lisp +n 15
  2918. (+ (- 10 1) 2 (- 15 1))
  2919. #+END_SRC"
  2920. 'src-block)))
  2921. (should
  2922. (equal '(9 19 0)
  2923. (test-org-gen-loc-list "
  2924. #+BEGIN_SRC emacs-lisp -n 10
  2925. (- 10 1)
  2926. #+END_SRC
  2927. #+BEGIN_SRC emacs-lisp +n 10
  2928. (+ (- 10 1) 1 (- 10 1))
  2929. #+END_SRC
  2930. #+BEGIN_SRC emacs-lisp -n
  2931. (- 1 1)
  2932. #+END_SRC"
  2933. 'src-block)))
  2934. ;; A SRC Block without -n does not add to the line count.
  2935. (should
  2936. (equal '(9 no-loc 19)
  2937. (test-org-gen-loc-list
  2938. "#+BEGIN_SRC emacs-lisp -n 10
  2939. (+ (-10 1) 1)
  2940. #+END_SRC
  2941. #+BEGIN_SRC emacs-lisp
  2942. (+ 2 2)
  2943. #+END_SRC
  2944. #+BEGIN_SRC emacs-lisp +n 10
  2945. (+ (- 10 1) 1 (- 10 1))
  2946. #+END_SRC"
  2947. 'src-block))))
  2948. (ert-deftest test-org-export/resolve-coderef ()
  2949. "Test `org-export-resolve-coderef' specifications."
  2950. (let ((org-coderef-label-format "(ref:%s)"))
  2951. ;; A link to a "-n -k -r" block returns line number.
  2952. (should
  2953. (= 1
  2954. (org-test-with-parsed-data
  2955. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2956. (org-export-resolve-coderef "coderef" info))))
  2957. (should
  2958. (= 10
  2959. (org-test-with-parsed-data
  2960. "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2961. (org-export-resolve-coderef "coderef" info))))
  2962. (should
  2963. (= 135
  2964. (org-test-with-parsed-data
  2965. "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
  2966. #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2967. (org-export-resolve-coderef "coderef" info))))
  2968. (should
  2969. (= 1
  2970. (org-test-with-parsed-data
  2971. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2972. (org-export-resolve-coderef "coderef" info))))
  2973. (should
  2974. (= 10
  2975. (org-test-with-parsed-data
  2976. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2977. (org-export-resolve-coderef "coderef" info))))
  2978. (should
  2979. (= 135
  2980. (org-test-with-parsed-data
  2981. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
  2982. #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2983. (org-export-resolve-coderef "coderef" info))))
  2984. ;; A link to a "-n -r" block returns line number.
  2985. (should
  2986. (= 1
  2987. (org-test-with-parsed-data
  2988. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2989. (org-export-resolve-coderef "coderef" info))))
  2990. (should
  2991. (= 10
  2992. (org-test-with-parsed-data
  2993. "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2994. (org-export-resolve-coderef "coderef" info))))
  2995. (should
  2996. (= 135
  2997. (org-test-with-parsed-data
  2998. "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
  2999. #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3000. (org-export-resolve-coderef "coderef" info))))
  3001. (should
  3002. (= 1
  3003. (org-test-with-parsed-data
  3004. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3005. (org-export-resolve-coderef "coderef" info))))
  3006. (should
  3007. (= 10
  3008. (org-test-with-parsed-data
  3009. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3010. (org-export-resolve-coderef "coderef" info))))
  3011. (should
  3012. (= 135
  3013. (org-test-with-parsed-data
  3014. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
  3015. #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3016. (org-export-resolve-coderef "coderef" info))))
  3017. ;; A link to a "-n" block returns coderef.
  3018. (should
  3019. (equal "coderef"
  3020. (org-test-with-parsed-data
  3021. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3022. (org-export-resolve-coderef "coderef" info))))
  3023. (should
  3024. (equal "coderef"
  3025. (org-test-with-parsed-data
  3026. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  3027. (org-export-resolve-coderef "coderef" info))))
  3028. ;; A link to a "-r" block returns line number.
  3029. (should
  3030. (= 1
  3031. (org-test-with-parsed-data
  3032. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3033. (org-export-resolve-coderef "coderef" info))))
  3034. (should
  3035. (= 1
  3036. (org-test-with-parsed-data
  3037. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  3038. (org-export-resolve-coderef "coderef" info))))
  3039. ;; A link to a block without a switch returns coderef.
  3040. (should
  3041. (equal "coderef"
  3042. (org-test-with-parsed-data
  3043. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  3044. (org-export-resolve-coderef "coderef" info))))
  3045. (org-test-with-parsed-data
  3046. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  3047. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  3048. ;; Correctly handle continued line numbers. A "+n" switch should
  3049. ;; resume numbering from previous block with numbered lines,
  3050. ;; ignoring blocks not numbering lines in the process. A "-n"
  3051. ;; switch resets count.
  3052. (should
  3053. (equal '(2 1)
  3054. (org-test-with-parsed-data "
  3055. #+BEGIN_EXAMPLE -n
  3056. Text.
  3057. #+END_EXAMPLE
  3058. #+BEGIN_SRC emacs-lisp
  3059. \(- 1 1)
  3060. #+END_SRC
  3061. #+BEGIN_SRC emacs-lisp +n -r
  3062. \(+ 1 1) (ref:addition)
  3063. #+END_SRC
  3064. #+BEGIN_EXAMPLE -n -r
  3065. Another text. (ref:text)
  3066. #+END_EXAMPLE"
  3067. (list (org-export-resolve-coderef "addition" info)
  3068. (org-export-resolve-coderef "text" info)))))
  3069. ;; Recognize coderef with user-specified syntax.
  3070. (should
  3071. (equal
  3072. "text"
  3073. (org-test-with-parsed-data
  3074. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  3075. (org-export-resolve-coderef "text" info))))
  3076. ;; Unresolved coderefs raise a `org-link-broken' signal.
  3077. (should
  3078. (condition-case nil
  3079. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3080. (org-export-resolve-coderef "unknown" info))
  3081. (org-link-broken t)))))
  3082. (ert-deftest test-org-export/resolve-fuzzy-link ()
  3083. "Test `org-export-resolve-fuzzy-link' specifications."
  3084. ;; Match target objects.
  3085. (should
  3086. (org-test-with-parsed-data "<<target>> [[target]]"
  3087. (org-export-resolve-fuzzy-link
  3088. (org-element-map tree 'link 'identity info t) info)))
  3089. ;; Match named elements.
  3090. (should
  3091. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  3092. (org-export-resolve-fuzzy-link
  3093. (org-element-map tree 'link 'identity info t) info)))
  3094. ;; Match exact headline's name.
  3095. (should
  3096. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  3097. (org-export-resolve-fuzzy-link
  3098. (org-element-map tree 'link 'identity info t) info)))
  3099. ;; Targets objects have priority over headline titles.
  3100. (should
  3101. (eq 'target
  3102. (org-test-with-parsed-data "* target\n<<target>>[[target]]"
  3103. (org-element-type
  3104. (org-export-resolve-fuzzy-link
  3105. (org-element-map tree 'link 'identity info t) info)))))
  3106. ;; Named elements have priority over headline titles.
  3107. (should
  3108. (eq 'paragraph
  3109. (org-test-with-parsed-data
  3110. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  3111. (org-element-type
  3112. (org-export-resolve-fuzzy-link
  3113. (org-element-map tree 'link 'identity info t) info)))))
  3114. ;; If link's path starts with a "*", only match headline titles,
  3115. ;; though.
  3116. (should
  3117. (eq 'headline
  3118. (org-test-with-parsed-data
  3119. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  3120. (org-element-type
  3121. (org-export-resolve-fuzzy-link
  3122. (org-element-map tree 'link 'identity info t) info)))))
  3123. ;; Raise a `org-link-broken' signal if no match.
  3124. (should
  3125. (org-test-with-parsed-data "[[target]]"
  3126. (condition-case nil
  3127. (org-export-resolve-fuzzy-link
  3128. (org-element-map tree 'link #'identity info t) info)
  3129. (org-link-broken t))))
  3130. ;; Match fuzzy link even when before first headline.
  3131. (should
  3132. (eq 'headline
  3133. (org-test-with-parsed-data "[[hl]]\n* hl"
  3134. (org-element-type
  3135. (org-export-resolve-fuzzy-link
  3136. (org-element-map tree 'link 'identity info t) info)))))
  3137. ;; Handle url-encoded fuzzy links.
  3138. (should
  3139. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  3140. (org-export-resolve-fuzzy-link
  3141. (org-element-map tree 'link #'identity info t) info))))
  3142. (ert-deftest test-org-export/resolve-id-link ()
  3143. "Test `org-export-resolve-id-link' specifications."
  3144. ;; Regular test for custom-id link.
  3145. (should
  3146. (equal '("Headline1")
  3147. (org-test-with-parsed-data "* Headline1
  3148. :PROPERTIES:
  3149. :CUSTOM_ID: test
  3150. :END:
  3151. * Headline 2
  3152. \[[#test]]"
  3153. (org-element-property
  3154. :title
  3155. (org-export-resolve-id-link
  3156. (org-element-map tree 'link 'identity info t) info)))))
  3157. ;; Raise a `org-link-broken' signal on failing searches.
  3158. (should
  3159. (org-test-with-parsed-data "* Headline1
  3160. :PROPERTIES:
  3161. :CUSTOM_ID: test
  3162. :END:
  3163. * Headline 2
  3164. \[[#no-match]]"
  3165. (condition-case nil
  3166. (org-export-resolve-id-link
  3167. (org-element-map tree 'link #'identity info t) info)
  3168. (org-link-broken t))))
  3169. ;; Test for internal id target.
  3170. (should
  3171. (equal '("Headline1")
  3172. (org-test-with-parsed-data "* Headline1
  3173. :PROPERTIES:
  3174. :ID: aaaa
  3175. :END:
  3176. * Headline 2
  3177. \[[id:aaaa]]"
  3178. (org-element-property
  3179. :title
  3180. (org-export-resolve-id-link
  3181. (org-element-map tree 'link 'identity info t) info)))))
  3182. ;; Test for external id target.
  3183. (should
  3184. (equal
  3185. "external-file"
  3186. (org-test-with-parsed-data "[[id:aaaa]]"
  3187. (org-export-resolve-id-link
  3188. (org-element-map tree 'link 'identity info t)
  3189. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  3190. (ert-deftest test-org-export/resolve-radio-link ()
  3191. "Test `org-export-resolve-radio-link' specifications."
  3192. ;; Standard test.
  3193. (should
  3194. (org-test-with-temp-text "<<<radio>>> radio"
  3195. (org-update-radio-target-regexp)
  3196. (let* ((tree (org-element-parse-buffer))
  3197. (info `(:parse-tree ,tree)))
  3198. (org-export-resolve-radio-link
  3199. (org-element-map tree 'link 'identity info t)
  3200. info))))
  3201. ;; Radio targets are case-insensitive.
  3202. (should
  3203. (org-test-with-temp-text "<<<RADIO>>> radio"
  3204. (org-update-radio-target-regexp)
  3205. (let* ((tree (org-element-parse-buffer))
  3206. (info `(:parse-tree ,tree)))
  3207. (org-export-resolve-radio-link
  3208. (org-element-map tree 'link 'identity info t)
  3209. info))))
  3210. ;; Radio target with objects.
  3211. (should
  3212. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  3213. (org-update-radio-target-regexp)
  3214. (let* ((tree (org-element-parse-buffer))
  3215. (info `(:parse-tree ,tree)))
  3216. (org-export-resolve-radio-link
  3217. (org-element-map tree 'link 'identity info t)
  3218. info))))
  3219. ;; Radio target with objects at its beginning.
  3220. (should
  3221. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  3222. (org-update-radio-target-regexp)
  3223. (let* ((tree (org-element-parse-buffer))
  3224. (info `(:parse-tree ,tree)))
  3225. (org-export-resolve-radio-link
  3226. (org-element-map tree 'link 'identity info t)
  3227. info))))
  3228. ;; Radio link next to an apostrophe.
  3229. (should
  3230. (org-test-with-temp-text "<<<radio>>> radio's"
  3231. (org-update-radio-target-regexp)
  3232. (let* ((tree (org-element-parse-buffer))
  3233. (info `(:parse-tree ,tree)))
  3234. (org-export-resolve-radio-link
  3235. (org-element-map tree 'link 'identity info t)
  3236. info))))
  3237. ;; Multiple radio targets.
  3238. (should
  3239. (equal '("radio1" "radio2")
  3240. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  3241. (org-update-radio-target-regexp)
  3242. (let* ((tree (org-element-parse-buffer))
  3243. (info `(:parse-tree ,tree)))
  3244. (org-element-map tree 'link
  3245. (lambda (link)
  3246. (org-element-property
  3247. :value (org-export-resolve-radio-link link info)))
  3248. info)))))
  3249. ;; Radio target is whitespace insensitive.
  3250. (should
  3251. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  3252. (org-update-radio-target-regexp)
  3253. (let* ((tree (org-element-parse-buffer))
  3254. (info `(:parse-tree ,tree)))
  3255. (org-element-map tree 'link
  3256. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  3257. (ert-deftest test-org-export/file-uri ()
  3258. "Test `org-export-file-uri' specifications."
  3259. ;; Preserve relative filenames.
  3260. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  3261. ;; Local files start with "file://"
  3262. (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
  3263. (org-export-file-uri "/local.org")))
  3264. ;; Remote files start with "file://"
  3265. (should (equal "file://ssh:myself@some.where:papers/last.pdf"
  3266. (org-export-file-uri "/ssh:myself@some.where:papers/last.pdf")))
  3267. (should (equal "file://localhost/etc/fstab"
  3268. (org-export-file-uri "//localhost/etc/fstab")))
  3269. ;; Expand filename starting with "~".
  3270. (should (equal (org-export-file-uri "~/file.org")
  3271. (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
  3272. (ert-deftest test-org-export/get-reference ()
  3273. "Test `org-export-get-reference' specifications."
  3274. (should
  3275. (org-test-with-parsed-data "* Headline"
  3276. (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
  3277. info)))
  3278. ;; For a given element always return the same reference.
  3279. (should
  3280. (org-test-with-parsed-data "* Headline"
  3281. (let ((headline (org-element-map tree 'headline #'identity nil t)))
  3282. (equal (org-export-get-reference headline info)
  3283. (org-export-get-reference headline info)))))
  3284. ;; References get through local export back-ends.
  3285. (should
  3286. (org-test-with-parsed-data "* Headline"
  3287. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3288. (backend
  3289. (org-export-create-backend
  3290. :transcoders
  3291. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3292. (equal (org-trim (org-export-data-with-backend headline backend info))
  3293. (org-export-get-reference headline info)))))
  3294. (should
  3295. (org-test-with-parsed-data "* Headline"
  3296. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3297. (backend
  3298. (org-export-create-backend
  3299. :transcoders
  3300. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3301. (equal (org-export-with-backend backend headline nil info)
  3302. (org-export-get-reference headline info)))))
  3303. ;; Use search cells defined in `:crossrefs'. However, handle
  3304. ;; duplicate search cells.
  3305. (should
  3306. (equal "org0000001"
  3307. (org-test-with-parsed-data "* Headline"
  3308. (let* ((headline (org-element-map tree 'headline #'identity nil t))
  3309. (search-cell (car (org-export-search-cells headline))))
  3310. (setq info
  3311. (plist-put info :crossrefs (list (cons search-cell 1))))
  3312. (org-export-get-reference headline info)))))
  3313. (should-not
  3314. (equal '("org0000001" "org0000001")
  3315. (org-test-with-parsed-data "* H\n** H"
  3316. (org-element-map tree 'headline
  3317. (lambda (h)
  3318. (let* ((search-cell (car (org-export-search-cells h)))
  3319. (info (plist-put info :crossrefs
  3320. (list (cons search-cell 1)))))
  3321. (org-export-get-reference h info))))))))
  3322. ;;; Pseudo objects and pseudo elements
  3323. (ert-deftest test-org-export/pseudo-elements ()
  3324. "Test exporting pseudo-elements."
  3325. ;; Handle blank lines after pseudo-elements. In particular, do not
  3326. ;; replace them with white spaces.
  3327. (should
  3328. (equal "contents\n\nparagraph\n"
  3329. (let ((backend (org-export-create-backend
  3330. :transcoders
  3331. '((pseudo-element . (lambda (_p c _i) c))
  3332. (paragraph . (lambda (_p c _i) c))
  3333. (plain-text . (lambda (c _i) c)))))
  3334. (element '(pseudo-element (:post-blank 1) "contents"))
  3335. (paragraph '(paragraph nil "paragraph"))
  3336. (data '(org-data nil)))
  3337. (org-element-adopt-elements data element paragraph)
  3338. (org-export-data-with-backend data backend nil)))))
  3339. (ert-deftest test-org-export/pseudo-objects ()
  3340. "Test exporting pseudo-objects."
  3341. ;; Handle blank spaces after pseudo-objects. In particular, do not
  3342. ;; replace them with newlines.
  3343. (should
  3344. (equal "begin x end\n"
  3345. (let ((backend (org-export-create-backend
  3346. :transcoders
  3347. '((pseudo-object . (lambda (_p c _i) c))
  3348. (paragraph . (lambda (_p c _i) c))
  3349. (plain-text . (lambda (c _i) c)))))
  3350. (object '(pseudo-object (:post-blank 1) "x"))
  3351. (paragraph '(paragraph nil)))
  3352. (org-element-adopt-elements paragraph "begin " object "end")
  3353. (org-export-data-with-backend paragraph backend nil)))))
  3354. ;;; Src-block and example-block
  3355. (ert-deftest test-org-export/unravel-code ()
  3356. "Test `org-export-unravel-code' function."
  3357. ;; Code without reference.
  3358. (should
  3359. (equal '("(+ 1 1)")
  3360. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3361. (org-export-unravel-code (org-element-at-point)))))
  3362. ;; Code with reference.
  3363. (should
  3364. (equal '("(+ 1 1)" (1 . "test"))
  3365. (org-test-with-temp-text
  3366. "#+BEGIN_EXAMPLE\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 with user-defined reference.
  3370. (should
  3371. (equal
  3372. '("(+ 1 1)" (1 . "test"))
  3373. (org-test-with-temp-text
  3374. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  3375. (let ((org-coderef-label-format "(ref:%s)"))
  3376. (org-export-unravel-code (org-element-at-point))))))
  3377. ;; Code references keys are relative to the current block.
  3378. (should
  3379. (equal '("(+ 2 2)\n(+ 3 3)" (2 . "one"))
  3380. (org-test-with-temp-text "
  3381. #+BEGIN_EXAMPLE -n
  3382. \(+ 1 1)
  3383. #+END_EXAMPLE
  3384. #+BEGIN_EXAMPLE +n
  3385. \(+ 2 2)
  3386. \(+ 3 3) (ref:one)
  3387. #+END_EXAMPLE"
  3388. (goto-line 5)
  3389. (let ((org-coderef-label-format "(ref:%s)"))
  3390. (org-export-unravel-code (org-element-at-point)))))))
  3391. (ert-deftest test-org-export/format-code-default ()
  3392. "Test `org-export-format-code-default' specifications."
  3393. ;; Preserve blank lines, even when code is empty.
  3394. (should
  3395. (equal "\n\n"
  3396. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  3397. (org-export-format-code-default
  3398. (org-element-map tree 'src-block #'identity info t) info))))
  3399. ;; Likewise, preserve leading and trailing blank lines in the code.
  3400. (should
  3401. (equal "\n(+ 1 1)\n"
  3402. (org-test-with-parsed-data
  3403. "#+BEGIN_SRC emacs-lisp\n\n(+ 1 1)\n#+END_SRC"
  3404. (org-export-format-code-default
  3405. (org-element-map tree 'src-block #'identity info t) info))))
  3406. (should
  3407. (equal "(+ 1 1)\n\n"
  3408. (org-test-with-parsed-data
  3409. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n\n#+END_SRC"
  3410. (org-export-format-code-default
  3411. (org-element-map tree 'src-block #'identity info t) info))))
  3412. ;; Number lines, two whitespace characters before the actual loc.
  3413. (should
  3414. (equal "1 a\n2 b\n"
  3415. (org-test-with-parsed-data
  3416. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  3417. (org-export-format-code-default
  3418. (org-element-map tree 'src-block #'identity info t) info))))
  3419. ;; Numbering includes blank lines.
  3420. (should
  3421. (equal "1 \n2 a\n3 \n4 b\n5 \n"
  3422. (org-test-with-parsed-data
  3423. "#+BEGIN_SRC emacs-lisp +n\n\na\n\nb\n\n#+END_SRC"
  3424. (org-export-format-code-default
  3425. (org-element-map tree 'src-block #'identity info t) info))))
  3426. ;; Put references 6 whitespace characters after the widest line,
  3427. ;; wrapped within parenthesis.
  3428. (should
  3429. (equal "123 (a)\n1 (b)\n"
  3430. (let ((org-coderef-label-format "(ref:%s)"))
  3431. (org-test-with-parsed-data
  3432. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  3433. (org-export-format-code-default
  3434. (org-element-map tree 'src-block #'identity info t) info))))))
  3435. ;;; Smart Quotes
  3436. (ert-deftest test-org-export/activate-smart-quotes ()
  3437. "Test `org-export-activate-smart-quotes' specifications."
  3438. ;; Double quotes: standard test.
  3439. (should
  3440. (equal
  3441. '("some &ldquo;quoted&rdquo; text")
  3442. (let ((org-export-default-language "en"))
  3443. (org-test-with-parsed-data "some \"quoted\" text"
  3444. (org-element-map tree 'plain-text
  3445. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3446. info)))))
  3447. ;; Opening quotes: at the beginning of a paragraph.
  3448. (should
  3449. (equal
  3450. '("&ldquo;begin")
  3451. (let ((org-export-default-language "en"))
  3452. (org-test-with-parsed-data "\"begin"
  3453. (org-element-map tree 'plain-text
  3454. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3455. info)))))
  3456. ;; Opening quotes: after an object.
  3457. (should
  3458. (equal
  3459. '("&ldquo;quoted&rdquo; text")
  3460. (let ((org-export-default-language "en"))
  3461. (org-test-with-parsed-data "=verb= \"quoted\" text"
  3462. (org-element-map tree 'plain-text
  3463. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3464. info)))))
  3465. ;; Closing quotes: at the end of a paragraph.
  3466. (should
  3467. (equal
  3468. '("Quoted &ldquo;text&rdquo;")
  3469. (let ((org-export-default-language "en"))
  3470. (org-test-with-parsed-data "Quoted \"text\""
  3471. (org-element-map tree 'plain-text
  3472. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3473. info)))))
  3474. ;; Inner quotes: standard test.
  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. ;; Inner quotes: close to special symbols.
  3483. (should
  3484. (equal '("« outer (« inner ») outer »")
  3485. (let ((org-export-default-language "fr"))
  3486. (org-test-with-parsed-data "\"outer ('inner') outer\""
  3487. (org-element-map tree 'plain-text
  3488. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3489. info)))))
  3490. (should
  3491. (equal '("« « inner » »")
  3492. (let ((org-export-default-language "fr"))
  3493. (org-test-with-parsed-data "\"'inner'\""
  3494. (org-element-map tree 'plain-text
  3495. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3496. info)))))
  3497. ;; Apostrophe: standard test.
  3498. (should
  3499. (equal '("It « shouldn’t » fail")
  3500. (let ((org-export-default-language "fr"))
  3501. (org-test-with-parsed-data "It \"shouldn't\" fail"
  3502. (org-element-map tree 'plain-text
  3503. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3504. info)))))
  3505. (should
  3506. (equal '("It shouldn’t fail")
  3507. (let ((org-export-default-language "fr"))
  3508. (org-test-with-parsed-data "It shouldn't fail"
  3509. (org-element-map tree 'plain-text
  3510. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3511. info)))))
  3512. ;; Apostrophe: before an object.
  3513. (should
  3514. (equal
  3515. '("« a’" " »")
  3516. (let ((org-export-default-language "fr"))
  3517. (org-test-with-parsed-data "\"a'=b=\""
  3518. (org-element-map tree 'plain-text
  3519. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3520. info)))))
  3521. ;; Apostrophe: after an object.
  3522. (should
  3523. (equal '("« " "’s »")
  3524. (let ((org-export-default-language "fr"))
  3525. (org-test-with-parsed-data "\"=code='s\""
  3526. (org-element-map tree 'plain-text
  3527. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3528. info)))))
  3529. ;; Special case: isolated quotes.
  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 secondary strings.
  3538. (should
  3539. (equal '("&ldquo;" "&rdquo;")
  3540. (let ((org-export-default-language "en"))
  3541. (org-test-with-parsed-data "* \"$x$\""
  3542. (org-element-map tree 'plain-text
  3543. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3544. info)))))
  3545. ;; Smart quotes in document keywords.
  3546. (should
  3547. (equal '("&ldquo;" "&rdquo;")
  3548. (let ((org-export-default-language "en"))
  3549. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  3550. (org-element-map (plist-get info :title) 'plain-text
  3551. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3552. info)))))
  3553. ;; Smart quotes in parsed affiliated keywords.
  3554. (should
  3555. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  3556. (let ((org-export-default-language "en"))
  3557. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3558. (org-element-map tree 'plain-text
  3559. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3560. info nil nil t)))))
  3561. ;; Smart quotes within objects.
  3562. (should
  3563. (equal '("&ldquo;foo&rdquo;")
  3564. (let ((org-export-default-language "en"))
  3565. (org-test-with-parsed-data "| \"foo\" |"
  3566. (org-element-map tree 'plain-text
  3567. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3568. info nil nil t)))))
  3569. ;; FIXME: Test failing non-interactively.
  3570. ;;
  3571. ;; (should
  3572. ;; (equal '("&ldquo;foo&rdquo;")
  3573. ;; (let ((org-export-default-language "en"))
  3574. ;; (org-test-with-parsed-data "*\"foo\"*"
  3575. ;; (org-element-map tree 'plain-text
  3576. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3577. ;; info nil nil t)))))
  3578. )
  3579. ;;; Tables
  3580. (ert-deftest test-org-export/special-column ()
  3581. "Test if the table's special column is properly recognized."
  3582. ;; 1. First column is special if it contains only a special marking
  3583. ;; characters or empty cells.
  3584. (org-test-with-temp-text "
  3585. | ! | 1 |
  3586. | | 2 |"
  3587. (should
  3588. (org-export-table-has-special-column-p
  3589. (org-element-map
  3590. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3591. ;; 2. If the column contains anything else, it isn't special.
  3592. (org-test-with-temp-text "
  3593. | ! | 1 |
  3594. | b | 2 |"
  3595. (should-not
  3596. (org-export-table-has-special-column-p
  3597. (org-element-map
  3598. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3599. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3600. ;; and "!".
  3601. (org-test-with-temp-text "
  3602. | # | 1 |
  3603. | ^ | 2 |
  3604. | * | 3 |
  3605. | _ | 4 |
  3606. | / | 5 |
  3607. | $ | 6 |
  3608. | ! | 7 |"
  3609. (should
  3610. (org-export-table-has-special-column-p
  3611. (org-element-map
  3612. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3613. ;; 4. A first column with only empty cells isn't considered as
  3614. ;; special.
  3615. (org-test-with-temp-text "
  3616. | | 1 |
  3617. | | 2 |"
  3618. (should-not
  3619. (org-export-table-has-special-column-p
  3620. (org-element-map
  3621. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3622. (ert-deftest test-org-export/table-row-is-special-p ()
  3623. "Test `org-export-table-row-is-special-p' specifications."
  3624. ;; 1. A row is special if it has a special marking character in the
  3625. ;; special column.
  3626. (org-test-with-parsed-data "| ! | 1 |"
  3627. (should
  3628. (org-export-table-row-is-special-p
  3629. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3630. ;; 2. A row is special when its first field is "/"
  3631. (org-test-with-parsed-data "
  3632. | / | 1 |
  3633. | a | b |"
  3634. (should
  3635. (org-export-table-row-is-special-p
  3636. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3637. ;; 3. A row only containing alignment cookies is also considered as
  3638. ;; special.
  3639. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3640. (should
  3641. (org-export-table-row-is-special-p
  3642. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3643. ;; 4. Everything else isn't considered as special.
  3644. (org-test-with-parsed-data "| \alpha | | c |"
  3645. (should-not
  3646. (org-export-table-row-is-special-p
  3647. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3648. ;; 5. Table's rules are never considered as special rows.
  3649. (org-test-with-parsed-data "|---+---|"
  3650. (should-not
  3651. (org-export-table-row-is-special-p
  3652. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3653. (ert-deftest test-org-export/has-header-p ()
  3654. "Test `org-export-table-has-header-p' specifications."
  3655. ;; With an header.
  3656. (should
  3657. (org-test-with-parsed-data "
  3658. | a | b |
  3659. |---+---|
  3660. | c | d |"
  3661. (org-export-table-has-header-p
  3662. (org-element-map tree 'table 'identity info 'first-match)
  3663. info)))
  3664. ;; Without an header.
  3665. (should-not
  3666. (org-test-with-parsed-data "
  3667. | a | b |
  3668. | c | d |"
  3669. (org-export-table-has-header-p
  3670. (org-element-map tree 'table 'identity info 'first-match)
  3671. info)))
  3672. ;; Don't get fooled with starting and ending rules.
  3673. (should-not
  3674. (org-test-with-parsed-data "
  3675. |---+---|
  3676. | a | b |
  3677. | c | d |
  3678. |---+---|"
  3679. (org-export-table-has-header-p
  3680. (org-element-map tree 'table 'identity info 'first-match)
  3681. info))))
  3682. (ert-deftest test-org-export/table-row-group ()
  3683. "Test `org-export-table-row-group' specifications."
  3684. ;; A rule creates a new group.
  3685. (should
  3686. (equal '(1 rule 2)
  3687. (org-test-with-parsed-data "
  3688. | a | b |
  3689. |---+---|
  3690. | 1 | 2 |"
  3691. (org-element-map tree 'table-row
  3692. (lambda (row)
  3693. (if (eq (org-element-property :type row) 'rule) 'rule
  3694. (org-export-table-row-group row info)))))))
  3695. ;; Special rows are ignored in count.
  3696. (should
  3697. (equal
  3698. '(rule 1)
  3699. (org-test-with-parsed-data "
  3700. | / | < | > |
  3701. |---|---+---|
  3702. | | 1 | 2 |"
  3703. (org-element-map tree 'table-row
  3704. (lambda (row)
  3705. (if (eq (org-element-property :type row) 'rule) 'rule
  3706. (org-export-table-row-group row info)))
  3707. info))))
  3708. ;; Double rules also are ignored in count.
  3709. (should
  3710. (equal '(1 rule rule 2)
  3711. (org-test-with-parsed-data "
  3712. | a | b |
  3713. |---+---|
  3714. |---+---|
  3715. | 1 | 2 |"
  3716. (org-element-map tree 'table-row
  3717. (lambda (row)
  3718. (if (eq (org-element-property :type row) 'rule) 'rule
  3719. (org-export-table-row-group row info))))))))
  3720. (ert-deftest test-org-export/table-row-number ()
  3721. "Test `org-export-table-row-number' specifications."
  3722. ;; Standard test. Number is 0-indexed.
  3723. (should
  3724. (equal '(0 1)
  3725. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3726. (org-element-map tree 'table-row
  3727. (lambda (row) (org-export-table-row-number row info)) info))))
  3728. ;; Number ignores separators.
  3729. (should
  3730. (equal '(0 1)
  3731. (org-test-with-parsed-data "
  3732. | a | b | c |
  3733. |---+---+---|
  3734. | d | e | f |"
  3735. (org-element-map tree 'table-row
  3736. (lambda (row) (org-export-table-row-number row info)) info))))
  3737. ;; Number ignores special rows.
  3738. (should
  3739. (equal '(0 1)
  3740. (org-test-with-parsed-data "
  3741. | / | < | > |
  3742. | | b | c |
  3743. |---+-----+-----|
  3744. | | <c> | <c> |
  3745. | | e | f |"
  3746. (org-element-map tree 'table-row
  3747. (lambda (row) (org-export-table-row-number row info)) info)))))
  3748. (ert-deftest test-org-export/table-cell-width ()
  3749. "Test `org-export-table-cell-width' specifications."
  3750. ;; 1. Width is primarily determined by width cookies. If no cookie
  3751. ;; is found, cell's width is nil.
  3752. (org-test-with-parsed-data "
  3753. | / | <l> | <6> | <l7> |
  3754. | | a | b | c |"
  3755. (should
  3756. (equal
  3757. '(nil 6 7)
  3758. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3759. (org-element-map tree 'table-cell 'identity info)))))
  3760. ;; 2. The last width cookie has precedence.
  3761. (org-test-with-parsed-data "
  3762. | <6> |
  3763. | <7> |
  3764. | a |"
  3765. (should
  3766. (equal
  3767. '(7)
  3768. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3769. (org-element-map tree 'table-cell 'identity info)))))
  3770. ;; 3. Valid width cookies must have a specific row.
  3771. (org-test-with-parsed-data "| <6> | cell |"
  3772. (should
  3773. (equal
  3774. '(nil nil)
  3775. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3776. (org-element-map tree 'table-cell 'identity))))))
  3777. (ert-deftest test-org-export/table-cell-alignment ()
  3778. "Test `org-export-table-cell-alignment' specifications."
  3779. ;; 1. Alignment is primarily determined by alignment cookies.
  3780. (should
  3781. (equal '(left center right)
  3782. (let ((org-table-number-fraction 0.5)
  3783. (org-table-number-regexp "^[0-9]+$"))
  3784. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3785. (mapcar (lambda (cell)
  3786. (org-export-table-cell-alignment cell info))
  3787. (org-element-map tree 'table-cell 'identity))))))
  3788. ;; 2. The last alignment cookie has precedence.
  3789. (should
  3790. (equal '(right right right)
  3791. (org-test-with-parsed-data "
  3792. | <l8> |
  3793. | cell |
  3794. | <r9> |"
  3795. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3796. (org-element-map tree 'table-cell 'identity)))))
  3797. ;; 3. If there's no cookie, cell's contents determine alignment.
  3798. ;; A column mostly made of cells containing numbers will align
  3799. ;; its cells to the right.
  3800. (should
  3801. (equal '(right right right)
  3802. (let ((org-table-number-fraction 0.5)
  3803. (org-table-number-regexp "^[0-9]+$"))
  3804. (org-test-with-parsed-data "
  3805. | 123 |
  3806. | some text |
  3807. | 12345 |"
  3808. (mapcar (lambda (cell)
  3809. (org-export-table-cell-alignment cell info))
  3810. (org-element-map tree 'table-cell 'identity))))))
  3811. ;; 4. Otherwise, they will be aligned to the left.
  3812. (should
  3813. (equal '(left left left)
  3814. (org-test-with-parsed-data "
  3815. | text |
  3816. | some text |
  3817. | \alpha |"
  3818. (mapcar (lambda (cell)
  3819. (org-export-table-cell-alignment cell info))
  3820. (org-element-map tree 'table-cell 'identity info))))))
  3821. (ert-deftest test-org-export/table-cell-borders ()
  3822. "Test `org-export-table-cell-borders' specifications."
  3823. ;; 1. Recognize various column groups indicators.
  3824. (org-test-with-parsed-data "| / | < | > | <> |"
  3825. (should
  3826. (equal
  3827. '((right bottom top) (left bottom top) (right bottom top)
  3828. (right left bottom top))
  3829. (mapcar (lambda (cell)
  3830. (org-export-table-cell-borders cell info))
  3831. (org-element-map tree 'table-cell 'identity)))))
  3832. ;; 2. Accept shortcuts to define column groups.
  3833. (org-test-with-parsed-data "| / | < | < |"
  3834. (should
  3835. (equal
  3836. '((right bottom top) (right left bottom top) (left bottom top))
  3837. (mapcar (lambda (cell)
  3838. (org-export-table-cell-borders cell info))
  3839. (org-element-map tree 'table-cell 'identity)))))
  3840. ;; 3. A valid column groups row must start with a "/".
  3841. (org-test-with-parsed-data "
  3842. | | < |
  3843. | a | b |"
  3844. (should
  3845. (equal '((top) (top) (bottom) (bottom))
  3846. (mapcar (lambda (cell)
  3847. (org-export-table-cell-borders cell info))
  3848. (org-element-map tree 'table-cell 'identity)))))
  3849. ;; 4. Take table rules into consideration.
  3850. (org-test-with-parsed-data "
  3851. | 1 |
  3852. |---|
  3853. | 2 |"
  3854. (should
  3855. (equal '((below top) (bottom above))
  3856. (mapcar (lambda (cell)
  3857. (org-export-table-cell-borders cell info))
  3858. (org-element-map tree 'table-cell 'identity)))))
  3859. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3860. ;; (resp. `bottom' and `below') borders. Any special row is
  3861. ;; ignored.
  3862. (org-test-with-parsed-data "
  3863. |---+----|
  3864. | / | |
  3865. | | 1 |
  3866. |---+----|"
  3867. (should
  3868. (equal '((bottom below top above))
  3869. (last
  3870. (mapcar (lambda (cell)
  3871. (org-export-table-cell-borders cell info))
  3872. (org-element-map tree 'table-cell 'identity)))))))
  3873. (ert-deftest test-org-export/table-dimensions ()
  3874. "Test `org-export-table-dimensions' specifications."
  3875. ;; 1. Standard test.
  3876. (org-test-with-parsed-data "
  3877. | 1 | 2 | 3 |
  3878. | 4 | 5 | 6 |"
  3879. (should
  3880. (equal '(2 . 3)
  3881. (org-export-table-dimensions
  3882. (org-element-map tree 'table 'identity info 'first-match) info))))
  3883. ;; 2. Ignore horizontal rules and special columns.
  3884. (org-test-with-parsed-data "
  3885. | / | < | > |
  3886. | 1 | 2 | 3 |
  3887. |---+---+---|
  3888. | 4 | 5 | 6 |"
  3889. (should
  3890. (equal '(2 . 3)
  3891. (org-export-table-dimensions
  3892. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3893. (ert-deftest test-org-export/table-cell-address ()
  3894. "Test `org-export-table-cell-address' specifications."
  3895. ;; 1. Standard test: index is 0-based.
  3896. (org-test-with-parsed-data "| a | b |"
  3897. (should
  3898. (equal '((0 . 0) (0 . 1))
  3899. (org-element-map tree 'table-cell
  3900. (lambda (cell) (org-export-table-cell-address cell info))
  3901. info))))
  3902. ;; 2. Special column isn't counted, nor are special rows.
  3903. (org-test-with-parsed-data "
  3904. | / | <> |
  3905. | | c |"
  3906. (should
  3907. (equal '(0 . 0)
  3908. (org-export-table-cell-address
  3909. (car (last (org-element-map tree 'table-cell 'identity info)))
  3910. info))))
  3911. ;; 3. Tables rules do not count either.
  3912. (org-test-with-parsed-data "
  3913. | a |
  3914. |---|
  3915. | b |
  3916. |---|
  3917. | c |"
  3918. (should
  3919. (equal '(2 . 0)
  3920. (org-export-table-cell-address
  3921. (car (last (org-element-map tree 'table-cell 'identity info)))
  3922. info))))
  3923. ;; 4. Return nil for special cells.
  3924. (org-test-with-parsed-data "| / | a |"
  3925. (should-not
  3926. (org-export-table-cell-address
  3927. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3928. info))))
  3929. (ert-deftest test-org-export/get-table-cell-at ()
  3930. "Test `org-export-get-table-cell-at' specifications."
  3931. ;; 1. Address ignores special columns, special rows and rules.
  3932. (org-test-with-parsed-data "
  3933. | / | <> |
  3934. | | a |
  3935. |---+----|
  3936. | | b |"
  3937. (should
  3938. (equal '("b")
  3939. (org-element-contents
  3940. (org-export-get-table-cell-at
  3941. '(1 . 0)
  3942. (org-element-map tree 'table 'identity info 'first-match)
  3943. info)))))
  3944. ;; 2. Return value for a non-existent address is nil.
  3945. (org-test-with-parsed-data "| a |"
  3946. (should-not
  3947. (org-export-get-table-cell-at
  3948. '(2 . 2)
  3949. (org-element-map tree 'table 'identity info 'first-match)
  3950. info)))
  3951. (org-test-with-parsed-data "| / |"
  3952. (should-not
  3953. (org-export-get-table-cell-at
  3954. '(0 . 0)
  3955. (org-element-map tree 'table 'identity info 'first-match)
  3956. info))))
  3957. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3958. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3959. ;; 1. A cell at a beginning of a row always starts a column group.
  3960. (org-test-with-parsed-data "| a |"
  3961. (should
  3962. (org-export-table-cell-starts-colgroup-p
  3963. (org-element-map tree 'table-cell 'identity info 'first-match)
  3964. info)))
  3965. ;; 2. Special column should be ignored when determining the
  3966. ;; beginning of the row.
  3967. (org-test-with-parsed-data "
  3968. | / | |
  3969. | | a |"
  3970. (should
  3971. (org-export-table-cell-starts-colgroup-p
  3972. (org-element-map tree 'table-cell 'identity info 'first-match)
  3973. info)))
  3974. ;; 2. Explicit column groups.
  3975. (org-test-with-parsed-data "
  3976. | / | | < |
  3977. | a | b | c |"
  3978. (should
  3979. (equal
  3980. '(yes no yes)
  3981. (org-element-map tree 'table-cell
  3982. (lambda (cell)
  3983. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3984. info)))))
  3985. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3986. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3987. ;; 1. A cell at the end of a row always ends a column group.
  3988. (org-test-with-parsed-data "| a |"
  3989. (should
  3990. (org-export-table-cell-ends-colgroup-p
  3991. (org-element-map tree 'table-cell 'identity info 'first-match)
  3992. info)))
  3993. ;; 2. Special column should be ignored when determining the
  3994. ;; beginning of the row.
  3995. (org-test-with-parsed-data "
  3996. | / | |
  3997. | | a |"
  3998. (should
  3999. (org-export-table-cell-ends-colgroup-p
  4000. (org-element-map tree 'table-cell 'identity info 'first-match)
  4001. info)))
  4002. ;; 3. Explicit column groups.
  4003. (org-test-with-parsed-data "
  4004. | / | < | |
  4005. | a | b | c |"
  4006. (should
  4007. (equal
  4008. '(yes no yes)
  4009. (org-element-map tree 'table-cell
  4010. (lambda (cell)
  4011. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  4012. info)))))
  4013. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  4014. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  4015. ;; 1. A row at the beginning of a table always starts a row group.
  4016. ;; So does a row following a table rule.
  4017. (org-test-with-parsed-data "
  4018. | a |
  4019. |---|
  4020. | b |"
  4021. (should
  4022. (equal
  4023. '(yes no yes)
  4024. (org-element-map tree 'table-row
  4025. (lambda (row)
  4026. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  4027. info))))
  4028. ;; 2. Special rows should be ignored when determining the beginning
  4029. ;; of the row.
  4030. (org-test-with-parsed-data "
  4031. | / | < |
  4032. | | a |
  4033. |---+---|
  4034. | / | < |
  4035. | | b |"
  4036. (should
  4037. (equal
  4038. '(yes no yes)
  4039. (org-element-map tree 'table-row
  4040. (lambda (row)
  4041. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  4042. info)))))
  4043. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  4044. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  4045. ;; 1. A row at the end of a table always ends a row group. So does
  4046. ;; a row preceding a table rule.
  4047. (org-test-with-parsed-data "
  4048. | a |
  4049. |---|
  4050. | b |"
  4051. (should
  4052. (equal
  4053. '(yes no yes)
  4054. (org-element-map tree 'table-row
  4055. (lambda (row)
  4056. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  4057. info))))
  4058. ;; 2. Special rows should be ignored when determining the beginning
  4059. ;; of the row.
  4060. (org-test-with-parsed-data "
  4061. | | a |
  4062. | / | < |
  4063. |---+---|
  4064. | | b |
  4065. | / | < |"
  4066. (should
  4067. (equal
  4068. '(yes no yes)
  4069. (org-element-map tree 'table-row
  4070. (lambda (row)
  4071. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  4072. info)))))
  4073. (ert-deftest test-org-export/table-row-in-header-p ()
  4074. "Test `org-export-table-row-in-header-p' specifications."
  4075. ;; Standard test. Separators are always nil.
  4076. (should
  4077. (equal
  4078. '(yes no no)
  4079. (org-test-with-parsed-data "| a |\n|---|\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. ;; Nil when there is no header.
  4084. (should
  4085. (equal
  4086. '(no no)
  4087. (org-test-with-parsed-data "| a |\n| b |"
  4088. (org-element-map tree 'table-row
  4089. (lambda (row)
  4090. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  4091. (ert-deftest test-org-export/table-row-starts-header-p ()
  4092. "Test `org-export-table-row-starts-header-p' specifications."
  4093. ;; 1. Only the row starting the first row group starts the table
  4094. ;; header.
  4095. (org-test-with-parsed-data "
  4096. | a |
  4097. | b |
  4098. |---|
  4099. | c |"
  4100. (should
  4101. (equal
  4102. '(yes no no no)
  4103. (org-element-map tree 'table-row
  4104. (lambda (row)
  4105. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  4106. info))))
  4107. ;; 2. A row cannot start an header if there's no header in the
  4108. ;; table.
  4109. (org-test-with-parsed-data "
  4110. | a |
  4111. |---|"
  4112. (should-not
  4113. (org-export-table-row-starts-header-p
  4114. (org-element-map tree 'table-row 'identity info 'first-match)
  4115. info))))
  4116. (ert-deftest test-org-export/table-row-ends-header-p ()
  4117. "Test `org-export-table-row-ends-header-p' specifications."
  4118. ;; 1. Only the row starting the first row group starts the table
  4119. ;; header.
  4120. (org-test-with-parsed-data "
  4121. | a |
  4122. | b |
  4123. |---|
  4124. | c |"
  4125. (should
  4126. (equal
  4127. '(no yes no no)
  4128. (org-element-map tree 'table-row
  4129. (lambda (row)
  4130. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  4131. info))))
  4132. ;; 2. A row cannot start an header if there's no header in the
  4133. ;; table.
  4134. (org-test-with-parsed-data "
  4135. | a |
  4136. |---|"
  4137. (should-not
  4138. (org-export-table-row-ends-header-p
  4139. (org-element-map tree 'table-row 'identity info 'first-match)
  4140. info))))
  4141. ;;; Tables of Contents
  4142. (ert-deftest test-org-export/collect-headlines ()
  4143. "Test `org-export-collect-headlines' specifications."
  4144. ;; Standard test.
  4145. (should
  4146. (equal '("H1" "H2")
  4147. (org-test-with-parsed-data "* H1\n** H2"
  4148. (mapcar (lambda (h) (org-element-property :raw-value h))
  4149. (org-export-collect-headlines info)))))
  4150. ;; Do not collect headlines below optional argument.
  4151. (should
  4152. (equal '("H1")
  4153. (org-test-with-parsed-data "* H1\n** H2"
  4154. (mapcar (lambda (h) (org-element-property :raw-value h))
  4155. (org-export-collect-headlines info 1)))))
  4156. ;; Never collect headlines below maximum headline level.
  4157. (should
  4158. (equal '("H1")
  4159. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4160. (mapcar (lambda (h) (org-element-property :raw-value h))
  4161. (org-export-collect-headlines info)))))
  4162. (should
  4163. (equal '("H1")
  4164. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4165. (mapcar (lambda (h) (org-element-property :raw-value h))
  4166. (org-export-collect-headlines info 2)))))
  4167. ;; Do not collect footnote section.
  4168. (should
  4169. (equal '("H1")
  4170. (let ((org-footnote-section "Footnotes"))
  4171. (org-test-with-parsed-data "* H1\n** Footnotes"
  4172. (mapcar (lambda (h) (org-element-property :raw-value h))
  4173. (org-export-collect-headlines info))))))
  4174. ;; Do not collect headlines with UNNUMBERED property set to "notoc".
  4175. ;; Headlines with another value for the property are still
  4176. ;; collected. UNNUMBERED property is inherited.
  4177. (should
  4178. (equal '("H1")
  4179. (org-test-with-parsed-data
  4180. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
  4181. (mapcar (lambda (h) (org-element-property :raw-value h))
  4182. (org-export-collect-headlines info)))))
  4183. (should-not
  4184. (org-test-with-parsed-data
  4185. "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
  4186. (mapcar (lambda (h) (org-element-property :raw-value h))
  4187. (org-export-collect-headlines info))))
  4188. (should
  4189. (equal '("H1" "H2")
  4190. (org-test-with-parsed-data
  4191. "* H1\n* H2\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  4192. (mapcar (lambda (h) (org-element-property :raw-value h))
  4193. (org-export-collect-headlines info)))))
  4194. ;; Collect headlines locally.
  4195. (should
  4196. (equal '("H2" "H3")
  4197. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  4198. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4199. (mapcar (lambda (h) (org-element-property :raw-value h))
  4200. (org-export-collect-headlines info nil scope))))))
  4201. ;; When collecting locally, optional level is relative.
  4202. (should
  4203. (equal '("H2")
  4204. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  4205. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4206. (mapcar (lambda (h) (org-element-property :raw-value h))
  4207. (org-export-collect-headlines info 1 scope)))))))
  4208. (ert-deftest test-org-export/excluded-from-toc-p ()
  4209. "Test `org-export-excluded-from-toc-p' specifications."
  4210. ;; By default, headlines are not excluded.
  4211. (should-not
  4212. (org-test-with-parsed-data "* H1"
  4213. (org-element-map tree 'headline
  4214. (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  4215. ;; Exclude according to a maximum level.
  4216. (should
  4217. (equal '(in out)
  4218. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4219. (org-element-map tree 'headline
  4220. (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
  4221. info))))
  4222. ;; Exclude according to UNNUMBERED property.
  4223. (should
  4224. (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
  4225. (org-element-map tree 'headline
  4226. (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
  4227. ;; UNNUMBERED property is inherited, so is "notoc" value.
  4228. (should
  4229. (equal '(out out)
  4230. (org-test-with-parsed-data
  4231. "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:\n** H2"
  4232. (org-element-map tree 'headline
  4233. (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
  4234. info)))))
  4235. (ert-deftest test-org-export/toc-entry-backend ()
  4236. "Test `org-export-toc-entry-backend' specifications."
  4237. ;; Ignore targets.
  4238. (should
  4239. (equal "H \n"
  4240. (org-test-with-temp-text "* H <<target>>"
  4241. (let (org-export-registered-backends)
  4242. (org-export-define-backend 'test
  4243. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4244. (org-element-property :title h)
  4245. (org-export-toc-entry-backend 'test)
  4246. i)))))
  4247. (org-export-as 'test)))))
  4248. ;; Ignore footnote references.
  4249. (should
  4250. (equal "H \n"
  4251. (org-test-with-temp-text "[fn:1] Definition\n* H [fn:1]"
  4252. (let (org-export-registered-backends)
  4253. (org-export-define-backend 'test
  4254. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4255. (org-element-property :title h)
  4256. (org-export-toc-entry-backend 'test)
  4257. i)))))
  4258. (org-export-as 'test)))))
  4259. ;; Replace plain links with contents, or with path.
  4260. (should
  4261. (equal "H Org mode\n"
  4262. (org-test-with-temp-text "* H [[https://orgmode.org][Org mode]]"
  4263. (let (org-export-registered-backends)
  4264. (org-export-define-backend 'test
  4265. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4266. (org-element-property :title h)
  4267. (org-export-toc-entry-backend 'test)
  4268. i)))))
  4269. (org-export-as 'test)))))
  4270. (should
  4271. (equal "H https://orgmode.org\n"
  4272. (org-test-with-temp-text "* H [[https://orgmode.org]]"
  4273. (let (org-export-registered-backends)
  4274. (org-export-define-backend 'test
  4275. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4276. (org-element-property :title h)
  4277. (org-export-toc-entry-backend 'test)
  4278. i)))))
  4279. (org-export-as 'test)))))
  4280. ;; Replace radio targets with contents.
  4281. (should
  4282. (equal "H radio\n"
  4283. (org-test-with-temp-text "* H <<<radio>>>"
  4284. (let (org-export-registered-backends)
  4285. (org-export-define-backend 'test
  4286. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4287. (org-element-property :title h)
  4288. (org-export-toc-entry-backend 'test)
  4289. i)))))
  4290. (org-export-as 'test)))))
  4291. ;; With optional argument TRANSCODERS, specify other
  4292. ;; transformations.
  4293. (should
  4294. (equal "H bold\n"
  4295. (org-test-with-temp-text "* H *bold*"
  4296. (let (org-export-registered-backends)
  4297. (org-export-define-backend 'test
  4298. '((headline . (lambda (h _c i) (org-export-data-with-backend
  4299. (org-element-property :title h)
  4300. (org-export-toc-entry-backend 'test
  4301. '(bold . (lambda (_b c _i) c)))
  4302. i)))))
  4303. (org-export-as 'test))))))
  4304. ;;; Templates
  4305. (ert-deftest test-org-export/inner-template ()
  4306. "Test `inner-template' translator specifications."
  4307. (should
  4308. (equal "Success!"
  4309. (org-test-with-temp-text "* Headline"
  4310. (org-export-as
  4311. (org-export-create-backend
  4312. :transcoders
  4313. '((inner-template . (lambda (contents info) "Success!"))
  4314. (headline . (lambda (h c i) "Headline"))))))))
  4315. ;; Inner template is applied even in a "body-only" export.
  4316. (should
  4317. (equal "Success!"
  4318. (org-test-with-temp-text "* Headline"
  4319. (org-export-as
  4320. (org-export-create-backend
  4321. :transcoders '((inner-template . (lambda (c i) "Success!"))
  4322. (headline . (lambda (h c i) "Headline"))))
  4323. nil nil 'body-only)))))
  4324. (ert-deftest test-org-export/template ()
  4325. "Test `template' translator specifications."
  4326. (should
  4327. (equal "Success!"
  4328. (org-test-with-temp-text "* Headline"
  4329. (org-export-as
  4330. (org-export-create-backend
  4331. :transcoders '((template . (lambda (contents info) "Success!"))
  4332. (headline . (lambda (h c i) "Headline"))))))))
  4333. ;; Template is not applied in a "body-only" export.
  4334. (should-not
  4335. (equal "Success!"
  4336. (org-test-with-temp-text "* Headline"
  4337. (org-export-as
  4338. (org-export-create-backend
  4339. :transcoders '((template . (lambda (contents info) "Success!"))
  4340. (headline . (lambda (h c i) "Headline"))))
  4341. nil nil 'body-only)))))
  4342. ;;; Topology
  4343. (ert-deftest test-org-export/get-next-element ()
  4344. "Test `org-export-get-next-element' specifications."
  4345. ;; Standard test.
  4346. (should
  4347. (equal "b"
  4348. (org-test-with-parsed-data "* Headline\n*a* b"
  4349. (org-export-get-next-element
  4350. (org-element-map tree 'bold 'identity info t) info))))
  4351. ;; Return nil when no previous element.
  4352. (should-not
  4353. (org-test-with-parsed-data "* Headline\na *b*"
  4354. (org-export-get-next-element
  4355. (org-element-map tree 'bold 'identity info t) info)))
  4356. ;; Non-exportable elements are ignored.
  4357. (should-not
  4358. (let ((org-export-with-timestamps nil))
  4359. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  4360. (org-export-get-next-element
  4361. (org-element-map tree 'entity 'identity info t) info))))
  4362. ;; Find next element in secondary strings.
  4363. (should
  4364. (eq 'verbatim
  4365. (org-test-with-parsed-data "* a =verb="
  4366. (org-element-type
  4367. (org-export-get-next-element
  4368. (org-element-map tree 'plain-text 'identity info t) info)))))
  4369. (should
  4370. (eq 'verbatim
  4371. (org-test-with-parsed-data "* /italic/ =verb="
  4372. (org-element-type
  4373. (org-export-get-next-element
  4374. (org-element-map tree 'italic 'identity info t) info)))))
  4375. ;; Find next element in document keywords.
  4376. (should
  4377. (eq 'verbatim
  4378. (org-test-with-parsed-data "#+TITLE: a =verb="
  4379. (org-element-type
  4380. (org-export-get-next-element
  4381. (org-element-map
  4382. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4383. ;; Find next element in parsed affiliated keywords.
  4384. (should
  4385. (eq 'verbatim
  4386. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  4387. (org-element-type
  4388. (org-export-get-next-element
  4389. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4390. ;; With optional argument N, return a list containing all the
  4391. ;; following elements.
  4392. (should
  4393. (equal
  4394. '(bold code underline)
  4395. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4396. (mapcar 'car
  4397. (org-export-get-next-element
  4398. (org-element-map tree 'italic 'identity info t) info t)))))
  4399. ;; When N is a positive integer, return a list containing up to
  4400. ;; N following elements.
  4401. (should
  4402. (equal
  4403. '(bold code)
  4404. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4405. (mapcar 'car
  4406. (org-export-get-next-element
  4407. (org-element-map tree 'italic 'identity info t) info 2))))))
  4408. (ert-deftest test-org-export/get-previous-element ()
  4409. "Test `org-export-get-previous-element' specifications."
  4410. ;; Standard test.
  4411. (should
  4412. (equal "a "
  4413. (org-test-with-parsed-data "* Headline\na *b*"
  4414. (org-export-get-previous-element
  4415. (org-element-map tree 'bold 'identity info t) info))))
  4416. ;; Return nil when no previous element.
  4417. (should-not
  4418. (org-test-with-parsed-data "* Headline\n*a* b"
  4419. (org-export-get-previous-element
  4420. (org-element-map tree 'bold 'identity info t) info)))
  4421. ;; Non-exportable elements are ignored.
  4422. (should-not
  4423. (let ((org-export-with-timestamps nil))
  4424. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  4425. (org-export-get-previous-element
  4426. (org-element-map tree 'entity 'identity info t) info))))
  4427. ;; Find previous element in secondary strings.
  4428. (should
  4429. (eq 'verbatim
  4430. (org-test-with-parsed-data "* =verb= a"
  4431. (org-element-type
  4432. (org-export-get-previous-element
  4433. (org-element-map tree 'plain-text 'identity info t) info)))))
  4434. (should
  4435. (eq 'verbatim
  4436. (org-test-with-parsed-data "* =verb= /italic/"
  4437. (org-element-type
  4438. (org-export-get-previous-element
  4439. (org-element-map tree 'italic 'identity info t) info)))))
  4440. ;; Find previous element in document keywords.
  4441. (should
  4442. (eq 'verbatim
  4443. (org-test-with-parsed-data "#+TITLE: =verb= a"
  4444. (org-element-type
  4445. (org-export-get-previous-element
  4446. (org-element-map
  4447. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4448. ;; Find previous element in parsed affiliated keywords.
  4449. (should
  4450. (eq 'verbatim
  4451. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  4452. (org-element-type
  4453. (org-export-get-previous-element
  4454. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4455. ;; With optional argument N, return a list containing up to
  4456. ;; N previous elements.
  4457. (should
  4458. (equal '(underline italic bold)
  4459. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4460. (mapcar 'car
  4461. (org-export-get-previous-element
  4462. (org-element-map tree 'code 'identity info t) info t)))))
  4463. ;; When N is a positive integer, return a list containing up to
  4464. ;; N previous elements.
  4465. (should
  4466. (equal '(italic bold)
  4467. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4468. (mapcar 'car
  4469. (org-export-get-previous-element
  4470. (org-element-map tree 'code 'identity info t) info 2))))))
  4471. (provide 'test-ox)
  4472. ;;; test-org-export.el end here