test-ox.el 168 KB

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