test-ox.el 168 KB

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