test-ox.el 147 KB

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