test-ox.el 165 KB

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