test-ox.el 165 KB

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