ox-odt.el 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405
  1. ;;; ox-odt.el --- OpenDocument Text Exporter for Org Mode
  2. ;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl)
  21. (require 'table nil 'noerror))
  22. (require 'format-spec)
  23. (require 'ox)
  24. (require 'org-compat)
  25. ;;; Define Back-End
  26. (org-export-define-backend 'odt
  27. '((bold . org-odt-bold)
  28. (center-block . org-odt-center-block)
  29. (clock . org-odt-clock)
  30. (code . org-odt-code)
  31. (drawer . org-odt-drawer)
  32. (dynamic-block . org-odt-dynamic-block)
  33. (entity . org-odt-entity)
  34. (example-block . org-odt-example-block)
  35. (export-block . org-odt-export-block)
  36. (export-snippet . org-odt-export-snippet)
  37. (fixed-width . org-odt-fixed-width)
  38. (footnote-definition . org-odt-footnote-definition)
  39. (footnote-reference . org-odt-footnote-reference)
  40. (headline . org-odt-headline)
  41. (horizontal-rule . org-odt-horizontal-rule)
  42. (inline-src-block . org-odt-inline-src-block)
  43. (inlinetask . org-odt-inlinetask)
  44. (italic . org-odt-italic)
  45. (item . org-odt-item)
  46. (keyword . org-odt-keyword)
  47. (latex-environment . org-odt-latex-environment)
  48. (latex-fragment . org-odt-latex-fragment)
  49. (line-break . org-odt-line-break)
  50. (link . org-odt-link)
  51. (node-property . org-odt-node-property)
  52. (paragraph . org-odt-paragraph)
  53. (plain-list . org-odt-plain-list)
  54. (plain-text . org-odt-plain-text)
  55. (planning . org-odt-planning)
  56. (property-drawer . org-odt-property-drawer)
  57. (quote-block . org-odt-quote-block)
  58. (radio-target . org-odt-radio-target)
  59. (section . org-odt-section)
  60. (special-block . org-odt-special-block)
  61. (src-block . org-odt-src-block)
  62. (statistics-cookie . org-odt-statistics-cookie)
  63. (strike-through . org-odt-strike-through)
  64. (subscript . org-odt-subscript)
  65. (superscript . org-odt-superscript)
  66. (table . org-odt-table)
  67. (table-cell . org-odt-table-cell)
  68. (table-row . org-odt-table-row)
  69. (target . org-odt-target)
  70. (template . org-odt-template)
  71. (timestamp . org-odt-timestamp)
  72. (underline . org-odt-underline)
  73. (verbatim . org-odt-verbatim)
  74. (verse-block . org-odt-verse-block))
  75. :export-block "ODT"
  76. :filters-alist '((:filter-parse-tree
  77. . (org-odt--translate-latex-fragments
  78. org-odt--translate-description-lists
  79. org-odt--translate-list-tables)))
  80. :menu-entry
  81. '(?o "Export to ODT"
  82. ((?o "As ODT file" org-odt-export-to-odt)
  83. (?O "As ODT file and open"
  84. (lambda (a s v b)
  85. (if a (org-odt-export-to-odt t s v)
  86. (org-open-file (org-odt-export-to-odt nil s v) 'system))))))
  87. :options-alist
  88. '((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  89. ;; Other variables.
  90. (:odt-content-template-file nil nil org-odt-content-template-file)
  91. (:odt-display-outline-level nil nil org-odt-display-outline-level)
  92. (:odt-fontify-srcblocks nil nil org-odt-fontify-srcblocks)
  93. (:odt-format-drawer-function nil nil org-odt-format-drawer-function)
  94. (:odt-format-headline-function nil nil org-odt-format-headline-function)
  95. (:odt-format-inlinetask-function nil nil org-odt-format-inlinetask-function)
  96. (:odt-inline-formula-rules nil nil org-odt-inline-formula-rules)
  97. (:odt-inline-image-rules nil nil org-odt-inline-image-rules)
  98. (:odt-pixels-per-inch nil nil org-odt-pixels-per-inch)
  99. (:odt-styles-file nil nil org-odt-styles-file)
  100. (:odt-table-styles nil nil org-odt-table-styles)
  101. (:odt-use-date-fields nil nil org-odt-use-date-fields)
  102. ;; Redefine regular option.
  103. (:with-latex nil "tex" org-odt-with-latex)))
  104. ;;; Dependencies
  105. ;;; Hooks
  106. ;;; Function Declarations
  107. (declare-function hfy-face-to-style "htmlfontify" (fn))
  108. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  109. (declare-function archive-zip-extract "arc-mode" (archive name))
  110. (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
  111. (declare-function browse-url-file-url "browse-url" (file))
  112. ;;; Internal Variables
  113. (defconst org-odt-lib-dir
  114. (file-name-directory (or load-file-name (buffer-file-name)))
  115. "Location of ODT exporter.
  116. Use this to infer values of `org-odt-styles-dir' and
  117. `org-odt-schema-dir'.")
  118. (defvar org-odt-data-dir
  119. (expand-file-name "../../etc/" org-odt-lib-dir)
  120. "Data directory for ODT exporter.
  121. Use this to infer values of `org-odt-styles-dir' and
  122. `org-odt-schema-dir'.")
  123. (defconst org-odt-special-string-regexps
  124. '(("\\\\-" . "&#x00ad;\\1") ; shy
  125. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  126. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  127. ("\\.\\.\\." . "&#x2026;")) ; hellip
  128. "Regular expressions for special string conversion.")
  129. (defconst org-odt-schema-dir-list
  130. (list
  131. (and org-odt-data-dir
  132. (expand-file-name "./schema/" org-odt-data-dir)) ; bail out
  133. (eval-when-compile
  134. (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
  135. (expand-file-name "./schema/" org-odt-data-dir))))
  136. "List of directories to search for OpenDocument schema files.
  137. Use this list to set the default value of
  138. `org-odt-schema-dir'. The entries in this list are
  139. populated heuristically based on the values of `org-odt-lib-dir'
  140. and `org-odt-data-dir'.")
  141. (defconst org-odt-styles-dir-list
  142. (list
  143. (and org-odt-data-dir
  144. (expand-file-name "./styles/" org-odt-data-dir)) ; bail out
  145. (eval-when-compile
  146. (and (boundp 'org-odt-data-dir) org-odt-data-dir ; see make install
  147. (expand-file-name "./styles/" org-odt-data-dir)))
  148. (expand-file-name "../../etc/styles/" org-odt-lib-dir) ; git
  149. (expand-file-name "./etc/styles/" org-odt-lib-dir) ; elpa
  150. (expand-file-name "./org/" data-directory) ; system
  151. )
  152. "List of directories to search for OpenDocument styles files.
  153. See `org-odt-styles-dir'. The entries in this list are populated
  154. heuristically based on the values of `org-odt-lib-dir' and
  155. `org-odt-data-dir'.")
  156. (defconst org-odt-styles-dir
  157. (let* ((styles-dir
  158. (catch 'styles-dir
  159. (message "Debug (ox-odt): Searching for OpenDocument styles files...")
  160. (mapc (lambda (styles-dir)
  161. (when styles-dir
  162. (message "Debug (ox-odt): Trying %s..." styles-dir)
  163. (when (and (file-readable-p
  164. (expand-file-name
  165. "OrgOdtContentTemplate.xml" styles-dir))
  166. (file-readable-p
  167. (expand-file-name
  168. "OrgOdtStyles.xml" styles-dir)))
  169. (message "Debug (ox-odt): Using styles under %s"
  170. styles-dir)
  171. (throw 'styles-dir styles-dir))))
  172. org-odt-styles-dir-list)
  173. nil)))
  174. (unless styles-dir
  175. (error "Error (ox-odt): Cannot find factory styles files, aborting"))
  176. styles-dir)
  177. "Directory that holds auxiliary XML files used by the ODT exporter.
  178. This directory contains the following XML files -
  179. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
  180. XML files are used as the default values of
  181. `org-odt-styles-file' and `org-odt-content-template-file'.
  182. The default value of this variable varies depending on the
  183. version of org in use and is initialized from
  184. `org-odt-styles-dir-list'. Note that the user could be using org
  185. from one of: org's own private git repository, GNU ELPA tar or
  186. standard Emacs.")
  187. (defconst org-odt-bookmark-prefix "OrgXref.")
  188. (defconst org-odt-manifest-file-entry-tag
  189. "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  190. (defconst org-odt-file-extensions
  191. '(("odt" . "OpenDocument Text")
  192. ("ott" . "OpenDocument Text Template")
  193. ("odm" . "OpenDocument Master Document")
  194. ("ods" . "OpenDocument Spreadsheet")
  195. ("ots" . "OpenDocument Spreadsheet Template")
  196. ("odg" . "OpenDocument Drawing (Graphics)")
  197. ("otg" . "OpenDocument Drawing Template")
  198. ("odp" . "OpenDocument Presentation")
  199. ("otp" . "OpenDocument Presentation Template")
  200. ("odi" . "OpenDocument Image")
  201. ("odf" . "OpenDocument Formula")
  202. ("odc" . "OpenDocument Chart")))
  203. (defconst org-odt-table-style-format
  204. "
  205. <style:style style:name=\"%s\" style:family=\"table\">
  206. <style:table-properties style:rel-width=\"%s%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  207. </style:style>
  208. "
  209. "Template for auto-generated Table styles.")
  210. (defvar org-odt-automatic-styles '()
  211. "Registry of automatic styles for various OBJECT-TYPEs.
  212. The variable has the following form:
  213. \(\(OBJECT-TYPE-A
  214. \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
  215. \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
  216. \(OBJECT-TYPE-B
  217. \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
  218. \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
  219. ...\).
  220. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  221. OBJECT-PROPS is (typically) a plist created by passing
  222. \"#+ATTR_ODT: \" option to `org-odt-parse-block-attributes'.
  223. Use `org-odt-add-automatic-style' to add update this variable.'")
  224. (defvar org-odt-object-counters nil
  225. "Running counters for various OBJECT-TYPEs.
  226. Use this to generate automatic names and style-names. See
  227. `org-odt-add-automatic-style'.")
  228. (defvar org-odt-src-block-paragraph-format
  229. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  230. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  231. <style:background-image/>
  232. </style:paragraph-properties>
  233. <style:text-properties fo:color=\"%s\"/>
  234. </style:style>"
  235. "Custom paragraph style for colorized source and example blocks.
  236. This style is much the same as that of \"OrgFixedWidthBlock\"
  237. except that the foreground and background colors are set
  238. according to the default face identified by the `htmlfontify'.")
  239. (defvar hfy-optimisations)
  240. (defvar org-odt-embedded-formulas-count 0)
  241. (defvar org-odt-embedded-images-count 0)
  242. (defvar org-odt-image-size-probe-method
  243. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  244. '(emacs fixed))
  245. "Ordered list of methods for determining image sizes.")
  246. (defvar org-odt-default-image-sizes-alist
  247. '(("as-char" . (5 . 0.4))
  248. ("paragraph" . (5 . 5)))
  249. "Hardcoded image dimensions one for each of the anchor
  250. methods.")
  251. ;; A4 page size is 21.0 by 29.7 cms
  252. ;; The default page settings has 2cm margin on each of the sides. So
  253. ;; the effective text area is 17.0 by 25.7 cm
  254. (defvar org-odt-max-image-size '(17.0 . 20.0)
  255. "Limiting dimensions for an embedded image.")
  256. (defconst org-odt-label-styles
  257. '(("math-formula" "%c" "text" "(%n)")
  258. ("math-label" "(%n)" "text" "(%n)")
  259. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  260. ("value" "%e %n: %c" "value" "%n"))
  261. "Specify how labels are applied and referenced.
  262. This is an alist where each element is of the form:
  263. \(STYLE-NAME ATTACH-FMT REF-MODE REF-FMT)
  264. ATTACH-FMT controls how labels and captions are attached to an
  265. entity. It may contain following specifiers - %e and %c. %e is
  266. replaced with the CATEGORY-NAME. %n is replaced with
  267. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  268. with CAPTION.
  269. REF-MODE and REF-FMT controls how label references are generated.
  270. The following XML is generated for a label reference -
  271. \"<text:sequence-ref text:reference-format=\"REF-MODE\" ...>
  272. REF-FMT </text:sequence-ref>\". REF-FMT may contain following
  273. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  274. %n is replaced with SEQNO.
  275. See also `org-odt-format-label'.")
  276. (defvar org-odt-category-map-alist
  277. '(("__Table__" "Table" "value" "Table" org-odt--enumerable-p)
  278. ("__Figure__" "Illustration" "value" "Figure" org-odt--enumerable-image-p)
  279. ("__MathFormula__" "Text" "math-formula" "Equation" org-odt--enumerable-formula-p)
  280. ("__DvipngImage__" "Equation" "value" "Equation" org-odt--enumerable-latex-image-p)
  281. ("__Listing__" "Listing" "value" "Listing" org-odt--enumerable-p))
  282. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  283. This is a list where each entry is of the form:
  284. \(CATEGORY-HANDLE OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE)
  285. CATEGORY_HANDLE identifies the captionable entity in question.
  286. OD-VARIABLE is the OpenDocument sequence counter associated with
  287. the entity. These counters are declared within
  288. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  289. `org-odt-content-template-file'.
  290. LABEL-STYLE is a key into `org-odt-label-styles' and specifies
  291. how a given entity should be captioned and referenced.
  292. CATEGORY-NAME is used for qualifying captions on export.
  293. ENUMERATOR-PREDICATE is used for assigning a sequence number to
  294. the entity. See `org-odt--enumerate'.")
  295. (defvar org-odt-manifest-file-entries nil)
  296. (defvar hfy-user-sheet-assoc)
  297. (defvar org-odt-zip-dir nil
  298. "Temporary work directory for OpenDocument exporter.")
  299. ;;; User Configuration Variables
  300. (defgroup org-export-odt nil
  301. "Options for exporting Org mode files to ODT."
  302. :tag "Org Export ODT"
  303. :group 'org-export)
  304. ;;;; Debugging
  305. (defcustom org-odt-prettify-xml nil
  306. "Specify whether or not the xml output should be prettified.
  307. When this option is turned on, `indent-region' is run on all
  308. component xml buffers before they are saved. Turn this off for
  309. regular use. Turn this on if you need to examine the xml
  310. visually."
  311. :group 'org-export-odt
  312. :version "24.1"
  313. :type 'boolean)
  314. ;;;; Document schema
  315. (require 'rng-loc)
  316. (defcustom org-odt-schema-dir
  317. (let* ((schema-dir
  318. (catch 'schema-dir
  319. (message "Debug (ox-odt): Searching for OpenDocument schema files...")
  320. (mapc
  321. (lambda (schema-dir)
  322. (when schema-dir
  323. (message "Debug (ox-odt): Trying %s..." schema-dir)
  324. (when (and (file-expand-wildcards
  325. (expand-file-name "od-manifest-schema*.rnc"
  326. schema-dir))
  327. (file-expand-wildcards
  328. (expand-file-name "od-schema*.rnc"
  329. schema-dir))
  330. (file-readable-p
  331. (expand-file-name "schemas.xml" schema-dir)))
  332. (message "Debug (ox-odt): Using schema files under %s"
  333. schema-dir)
  334. (throw 'schema-dir schema-dir))))
  335. org-odt-schema-dir-list)
  336. (message "Debug (ox-odt): No OpenDocument schema files installed")
  337. nil)))
  338. schema-dir)
  339. "Directory that contains OpenDocument schema files.
  340. This directory contains:
  341. 1. rnc files for OpenDocument schema
  342. 2. a \"schemas.xml\" file that specifies locating rules needed
  343. for auto validation of OpenDocument XML files.
  344. Use the customize interface to set this variable. This ensures
  345. that `rng-schema-locating-files' is updated and auto-validation
  346. of OpenDocument XML takes place based on the value
  347. `rng-nxml-auto-validate-flag'.
  348. The default value of this variable varies depending on the
  349. version of org in use and is initialized from
  350. `org-odt-schema-dir-list'. The OASIS schema files are available
  351. only in the org's private git repository. It is *not* bundled
  352. with GNU ELPA tar or standard Emacs distribution."
  353. :type '(choice
  354. (const :tag "Not set" nil)
  355. (directory :tag "Schema directory"))
  356. :group 'org-export-odt
  357. :version "24.1"
  358. :set
  359. (lambda (var value)
  360. "Set `org-odt-schema-dir'.
  361. Also add it to `rng-schema-locating-files'."
  362. (let ((schema-dir value))
  363. (set var
  364. (if (and
  365. (file-expand-wildcards
  366. (expand-file-name "od-manifest-schema*.rnc" schema-dir))
  367. (file-expand-wildcards
  368. (expand-file-name "od-schema*.rnc" schema-dir))
  369. (file-readable-p
  370. (expand-file-name "schemas.xml" schema-dir)))
  371. schema-dir
  372. (when value
  373. (message "Error (ox-odt): %s has no OpenDocument schema files"
  374. value))
  375. nil)))
  376. (when org-odt-schema-dir
  377. (eval-after-load 'rng-loc
  378. '(add-to-list 'rng-schema-locating-files
  379. (expand-file-name "schemas.xml"
  380. org-odt-schema-dir))))))
  381. ;;;; Document styles
  382. (defcustom org-odt-content-template-file nil
  383. "Template file for \"content.xml\".
  384. The exporter embeds the exported content just before
  385. \"</office:text>\" element.
  386. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  387. under `org-odt-styles-dir' is used."
  388. :type '(choice (const nil)
  389. (file))
  390. :group 'org-export-odt
  391. :version "24.3")
  392. (defcustom org-odt-styles-file nil
  393. "Default styles file for use with ODT export.
  394. Valid values are one of:
  395. 1. nil
  396. 2. path to a styles.xml file
  397. 3. path to a *.odt or a *.ott file
  398. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  399. ...))
  400. In case of option 1, an in-built styles.xml is used. See
  401. `org-odt-styles-dir' for more information.
  402. In case of option 3, the specified file is unzipped and the
  403. styles.xml embedded therein is used.
  404. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  405. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  406. generated odt file. Use relative path for specifying the
  407. FILE-MEMBERS. styles.xml must be specified as one of the
  408. FILE-MEMBERS.
  409. Use options 1, 2 or 3 only if styles.xml alone suffices for
  410. achieving the desired formatting. Use option 4, if the styles.xml
  411. references additional files like header and footer images for
  412. achieving the desired formatting.
  413. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  414. a per-file basis. For example,
  415. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  416. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  417. :group 'org-export-odt
  418. :version "24.1"
  419. :type
  420. '(choice
  421. (const :tag "Factory settings" nil)
  422. (file :must-match t :tag "styles.xml")
  423. (file :must-match t :tag "ODT or OTT file")
  424. (list :tag "ODT or OTT file + Members"
  425. (file :must-match t :tag "ODF Text or Text Template file")
  426. (cons :tag "Members"
  427. (file :tag " Member" "styles.xml")
  428. (repeat (file :tag "Member"))))))
  429. (defcustom org-odt-display-outline-level 2
  430. "Outline levels considered for enumerating captioned entities."
  431. :group 'org-export-odt
  432. :version "24.4"
  433. :package-version '(Org . "8.0")
  434. :type 'integer)
  435. ;;;; Document conversion
  436. (defcustom org-odt-convert-processes
  437. '(("LibreOffice"
  438. "soffice --headless --convert-to %f%x --outdir %d %i")
  439. ("unoconv"
  440. "unoconv -f %f -o %d %i"))
  441. "Specify a list of document converters and their usage.
  442. The converters in this list are offered as choices while
  443. customizing `org-odt-convert-process'.
  444. This variable is a list where each element is of the
  445. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  446. of the converter. CONVERTER-CMD is the shell command for the
  447. converter and can contain format specifiers. These format
  448. specifiers are interpreted as below:
  449. %i input file name in full
  450. %I input file name as a URL
  451. %f format of the output file
  452. %o output file name in full
  453. %O output file name as a URL
  454. %d output dir in full
  455. %D output dir as a URL.
  456. %x extra options as set in `org-odt-convert-capabilities'."
  457. :group 'org-export-odt
  458. :version "24.1"
  459. :type
  460. '(choice
  461. (const :tag "None" nil)
  462. (alist :tag "Converters"
  463. :key-type (string :tag "Converter Name")
  464. :value-type (group (string :tag "Command line")))))
  465. (defcustom org-odt-convert-process "LibreOffice"
  466. "Use this converter to convert from \"odt\" format to other formats.
  467. During customization, the list of converter names are populated
  468. from `org-odt-convert-processes'."
  469. :group 'org-export-odt
  470. :version "24.1"
  471. :type '(choice :convert-widget
  472. (lambda (w)
  473. (apply 'widget-convert (widget-type w)
  474. (eval (car (widget-get w :args)))))
  475. `((const :tag "None" nil)
  476. ,@(mapcar (lambda (c)
  477. `(const :tag ,(car c) ,(car c)))
  478. org-odt-convert-processes))))
  479. (defcustom org-odt-convert-capabilities
  480. '(("Text"
  481. ("odt" "ott" "doc" "rtf" "docx")
  482. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  483. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  484. ("Web"
  485. ("html")
  486. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  487. ("Spreadsheet"
  488. ("ods" "ots" "xls" "csv" "xlsx")
  489. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  490. ("xls" "xls") ("xlsx" "xlsx")))
  491. ("Presentation"
  492. ("odp" "otp" "ppt" "pptx")
  493. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  494. ("pptx" "pptx") ("odg" "odg"))))
  495. "Specify input and output formats of `org-odt-convert-process'.
  496. More correctly, specify the set of input and output formats that
  497. the user is actually interested in.
  498. This variable is an alist where each element is of the
  499. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  500. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  501. alist where each element is of the form (OUTPUT-FMT
  502. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  503. The variable is interpreted as follows:
  504. `org-odt-convert-process' can take any document that is in
  505. INPUT-FMT-LIST and produce any document that is in the
  506. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  507. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  508. serves dual purposes:
  509. - It is used for populating completion candidates during
  510. `org-odt-convert' commands.
  511. - It is used as the value of \"%f\" specifier in
  512. `org-odt-convert-process'.
  513. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  514. `org-odt-convert-process'.
  515. DOCUMENT-CLASS is used to group a set of file formats in
  516. INPUT-FMT-LIST in to a single class.
  517. Note that this variable inherently captures how LibreOffice based
  518. converters work. LibreOffice maps documents of various formats
  519. to classes like Text, Web, Spreadsheet, Presentation etc and
  520. allow document of a given class (irrespective of its source
  521. format) to be converted to any of the export formats associated
  522. with that class.
  523. See default setting of this variable for an typical
  524. configuration."
  525. :group 'org-export-odt
  526. :version "24.1"
  527. :type
  528. '(choice
  529. (const :tag "None" nil)
  530. (alist :tag "Capabilities"
  531. :key-type (string :tag "Document Class")
  532. :value-type
  533. (group (repeat :tag "Input formats" (string :tag "Input format"))
  534. (alist :tag "Output formats"
  535. :key-type (string :tag "Output format")
  536. :value-type
  537. (group (string :tag "Output file extension")
  538. (choice
  539. (const :tag "None" nil)
  540. (string :tag "Extra options"))))))))
  541. (defcustom org-odt-preferred-output-format nil
  542. "Automatically post-process to this format after exporting to \"odt\".
  543. Command `org-odt-export-to-odt' exports first to \"odt\" format
  544. and then uses `org-odt-convert-process' to convert the
  545. resulting document to this format. During customization of this
  546. variable, the list of valid values are populated based on
  547. `org-odt-convert-capabilities'.
  548. You can set this option on per-file basis using file local
  549. values. See Info node `(emacs) File Variables'."
  550. :group 'org-export-odt
  551. :version "24.1"
  552. :type '(choice :convert-widget
  553. (lambda (w)
  554. (apply 'widget-convert (widget-type w)
  555. (eval (car (widget-get w :args)))))
  556. `((const :tag "None" nil)
  557. ,@(mapcar (lambda (c)
  558. `(const :tag ,c ,c))
  559. (org-odt-reachable-formats "odt")))))
  560. ;;;###autoload
  561. (put 'org-odt-preferred-output-format 'safe-local-variable 'stringp)
  562. ;;;; Drawers
  563. (defcustom org-odt-format-drawer-function
  564. (lambda (name contents) contents)
  565. "Function called to format a drawer in ODT code.
  566. The function must accept two parameters:
  567. NAME the drawer name, like \"LOGBOOK\"
  568. CONTENTS the contents of the drawer.
  569. The function should return the string to be exported.
  570. The default value simply returns the value of CONTENTS."
  571. :group 'org-export-odt
  572. :version "24.4"
  573. :package-version '(Org . "8.3")
  574. :type 'function)
  575. ;;;; Headline
  576. (defcustom org-odt-format-headline-function
  577. 'org-odt-format-headline-default-function
  578. "Function to format headline text.
  579. This function will be called with 5 arguments:
  580. TODO the todo keyword \(string or nil\).
  581. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  582. PRIORITY the priority of the headline \(integer or nil\)
  583. TEXT the main headline text \(string\).
  584. TAGS the tags string, separated with colons \(string or nil\).
  585. The function result will be used as headline text."
  586. :group 'org-export-odt
  587. :version "25.1"
  588. :package-version '(Org . "8.3")
  589. :type 'function)
  590. ;;;; Inlinetasks
  591. (defcustom org-odt-format-inlinetask-function
  592. 'org-odt-format-inlinetask-default-function
  593. "Function called to format an inlinetask in ODT code.
  594. The function must accept six parameters:
  595. TODO the todo keyword, as a string
  596. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  597. PRIORITY the inlinetask priority, as a string
  598. NAME the inlinetask name, as a string.
  599. TAGS the inlinetask tags, as a string.
  600. CONTENTS the contents of the inlinetask, as a string.
  601. The function should return the string to be exported."
  602. :group 'org-export-odt
  603. :version "25.1"
  604. :package-version '(Org . "8.3")
  605. :type 'function)
  606. ;;;; LaTeX
  607. (defcustom org-odt-with-latex org-export-with-latex
  608. "Non-nil means process LaTeX math snippets.
  609. When set, the exporter will process LaTeX environments and
  610. fragments.
  611. This option can also be set with the +OPTIONS line,
  612. e.g. \"tex:mathjax\". Allowed values are:
  613. nil Ignore math snippets.
  614. `verbatim' Keep everything in verbatim
  615. `dvipng' Process the LaTeX fragments to images. This will also
  616. include processing of non-math environments.
  617. `imagemagick' Convert the LaTeX fragments to pdf files and use
  618. imagemagick to convert pdf files to png files.
  619. `mathjax' Do MathJax preprocessing and arrange for MathJax.js to
  620. be loaded.
  621. t Synonym for `mathjax'."
  622. :group 'org-export-odt
  623. :version "24.4"
  624. :package-version '(Org . "8.0")
  625. :type '(choice
  626. (const :tag "Do not process math in any way" nil)
  627. (const :tag "Use dvipng to make images" dvipng)
  628. (const :tag "Use imagemagick to make images" imagemagick)
  629. (const :tag "Use MathJax to display math" mathjax)
  630. (const :tag "Leave math verbatim" verbatim)))
  631. ;;;; Links
  632. (defcustom org-odt-inline-formula-rules
  633. '(("file" . "\\.\\(mathml\\|mml\\|odf\\)\\'"))
  634. "Rules characterizing formula files that can be inlined into ODT.
  635. A rule consists in an association whose key is the type of link
  636. to consider, and value is a regexp that will be matched against
  637. link's path."
  638. :group 'org-export-odt
  639. :version "24.4"
  640. :package-version '(Org . "8.0")
  641. :type '(alist :key-type (string :tag "Type")
  642. :value-type (regexp :tag "Path")))
  643. (defcustom org-odt-inline-image-rules
  644. '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
  645. "Rules characterizing image files that can be inlined into ODT.
  646. A rule consists in an association whose key is the type of link
  647. to consider, and value is a regexp that will be matched against
  648. link's path."
  649. :group 'org-export-odt
  650. :version "24.4"
  651. :package-version '(Org . "8.0")
  652. :type '(alist :key-type (string :tag "Type")
  653. :value-type (regexp :tag "Path")))
  654. (defcustom org-odt-pixels-per-inch 96.0
  655. "Scaling factor for converting images pixels to inches.
  656. Use this for sizing of embedded images. See Info node `(org)
  657. Images in ODT export' for more information."
  658. :type 'float
  659. :group 'org-export-odt
  660. :version "24.4"
  661. :package-version '(Org . "8.1"))
  662. ;;;; Src Block
  663. (defcustom org-odt-create-custom-styles-for-srcblocks t
  664. "Whether custom styles for colorized source blocks be automatically created.
  665. When this option is turned on, the exporter creates custom styles
  666. for source blocks based on the advice of `htmlfontify'. Creation
  667. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  668. When this option is turned off exporter does not create such
  669. styles.
  670. Use the latter option if you do not want the custom styles to be
  671. based on your current display settings. It is necessary that the
  672. styles.xml already contains needed styles for colorizing to work.
  673. This variable is effective only if `org-odt-fontify-srcblocks' is
  674. turned on."
  675. :group 'org-export-odt
  676. :version "24.1"
  677. :type 'boolean)
  678. (defcustom org-odt-fontify-srcblocks t
  679. "Specify whether or not source blocks need to be fontified.
  680. Turn this option on if you want to colorize the source code
  681. blocks in the exported file. For colorization to work, you need
  682. to make available an enhanced version of `htmlfontify' library."
  683. :type 'boolean
  684. :group 'org-export-odt
  685. :version "24.1")
  686. ;;;; Table
  687. (defcustom org-odt-table-styles
  688. '(("OrgEquation" "OrgEquation"
  689. ((use-first-column-styles . t)
  690. (use-last-column-styles . t)))
  691. ("TableWithHeaderRowAndColumn" "Custom"
  692. ((use-first-row-styles . t)
  693. (use-first-column-styles . t)))
  694. ("TableWithFirstRowandLastRow" "Custom"
  695. ((use-first-row-styles . t)
  696. (use-last-row-styles . t)))
  697. ("GriddedTable" "Custom" nil))
  698. "Specify how Table Styles should be derived from a Table Template.
  699. This is a list where each element is of the
  700. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  701. TABLE-STYLE-NAME is the style associated with the table through
  702. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  703. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  704. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  705. below) that is included in `org-odt-content-template-file'.
  706. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  707. \"TableCell\"
  708. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  709. \"TableParagraph\"
  710. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  711. \"FirstRow\" | \"LastRow\" |
  712. \"EvenRow\" | \"OddRow\" |
  713. \"EvenColumn\" | \"OddColumn\" | \"\"
  714. where \"+\" above denotes string concatenation.
  715. TABLE-CELL-OPTIONS is an alist where each element is of the
  716. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  717. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  718. `use-last-row-styles' |
  719. `use-first-column-styles' |
  720. `use-last-column-styles' |
  721. `use-banding-rows-styles' |
  722. `use-banding-columns-styles' |
  723. `use-first-row-styles'
  724. ON-OR-OFF := `t' | `nil'
  725. For example, with the following configuration
  726. \(setq org-odt-table-styles
  727. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  728. \(\(use-first-row-styles . t\)
  729. \(use-first-column-styles . t\)\)\)
  730. \(\"TableWithHeaderColumns\" \"Custom\"
  731. \(\(use-first-column-styles . t\)\)\)\)\)
  732. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  733. style will use the following table-cell styles -
  734. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  735. \"CustomTableCell\" and the following paragraph styles
  736. \"CustomFirstRowTableParagraph\",
  737. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  738. as appropriate.
  739. 2. A table associated with \"TableWithHeaderColumns\" style will
  740. use the following table-cell styles -
  741. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  742. following paragraph styles
  743. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  744. as appropriate..
  745. Note that TABLE-TEMPLATE-NAME corresponds to the
  746. \"<table:table-template>\" elements contained within
  747. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  748. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  749. \"table:template-name\" and \"table:use-first-row-styles\" etc
  750. attributes of \"<table:table>\" element. Refer ODF-1.2
  751. specification for more information. Also consult the
  752. implementation filed under `org-odt-get-table-cell-styles'.
  753. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  754. formatting of numbered display equations. Do not delete this
  755. style from the list."
  756. :group 'org-export-odt
  757. :version "24.1"
  758. :type '(choice
  759. (const :tag "None" nil)
  760. (repeat :tag "Table Styles"
  761. (list :tag "Table Style Specification"
  762. (string :tag "Table Style Name")
  763. (string :tag "Table Template Name")
  764. (alist :options (use-first-row-styles
  765. use-last-row-styles
  766. use-first-column-styles
  767. use-last-column-styles
  768. use-banding-rows-styles
  769. use-banding-columns-styles)
  770. :key-type symbol
  771. :value-type (const :tag "True" t))))))
  772. ;;;; Timestamps
  773. (defcustom org-odt-use-date-fields nil
  774. "Non-nil, if timestamps should be exported as date fields.
  775. When nil, export timestamps as plain text.
  776. When non-nil, map `org-time-stamp-custom-formats' to a pair of
  777. OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
  778. respectively. A timestamp with no time component is formatted
  779. with style \"OrgDate1\" while one with explicit hour and minutes
  780. is formatted with style \"OrgDate2\".
  781. This feature is experimental. Most (but not all) of the common
  782. %-specifiers in `format-time-string' are supported.
  783. Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
  784. formatted as canonical Org timestamps. For finer control, avoid
  785. these %-specifiers.
  786. Textual specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
  787. etc., are displayed by the application in the default language
  788. and country specified in `org-odt-styles-file'. Note that the
  789. default styles file uses language \"en\" and country \"GB\". You
  790. can localize the week day and month strings in the exported
  791. document by setting the default language and country either using
  792. the application UI or through a custom styles file.
  793. See `org-odt--build-date-styles' for implementation details."
  794. :group 'org-export-odt
  795. :version "24.4"
  796. :package-version '(Org . "8.0")
  797. :type 'boolean)
  798. ;;; Internal functions
  799. ;;;; Date
  800. (defun org-odt--format-timestamp (timestamp &optional end iso-date-p)
  801. (let* ((format-timestamp
  802. (lambda (timestamp format &optional end utc)
  803. (if timestamp
  804. (org-timestamp-format timestamp format end utc)
  805. (format-time-string format nil utc))))
  806. (has-time-p (or (not timestamp)
  807. (org-timestamp-has-time-p timestamp)))
  808. (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
  809. "%Y-%m-%dT%H:%M:%S")))
  810. (funcall format-timestamp timestamp format end))))
  811. (if iso-date-p iso-date
  812. (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
  813. ;; LibreOffice does not care about end goes as content
  814. ;; within the "<text:date>...</text:date>" field. The
  815. ;; displayed date is automagically corrected to match the
  816. ;; format requested by "style:data-style-name" attribute. So
  817. ;; don't bother about formatting the date contents to be
  818. ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
  819. ;; simple Org-style date should suffice.
  820. (date (let* ((formats
  821. (if org-display-custom-times
  822. (cons (substring
  823. (car org-time-stamp-custom-formats) 1 -1)
  824. (substring
  825. (cdr org-time-stamp-custom-formats) 1 -1))
  826. '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
  827. (format (if has-time-p (cdr formats) (car formats))))
  828. (funcall format-timestamp timestamp format end)))
  829. (repeater (let ((repeater-type (org-element-property
  830. :repeater-type timestamp))
  831. (repeater-value (org-element-property
  832. :repeater-value timestamp))
  833. (repeater-unit (org-element-property
  834. :repeater-unit timestamp)))
  835. (concat
  836. (case repeater-type
  837. (catchup "++") (restart ".+") (cumulate "+"))
  838. (when repeater-value
  839. (number-to-string repeater-value))
  840. (case repeater-unit
  841. (hour "h") (day "d") (week "w") (month "m")
  842. (year "y"))))))
  843. (concat
  844. (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
  845. iso-date style date)
  846. (and (not (string= repeater "")) " ")
  847. repeater)))))
  848. ;;;; Frame
  849. (defun org-odt--frame (text width height style &optional extra
  850. anchor-type &rest title-and-desc)
  851. (let ((frame-attrs
  852. (concat
  853. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  854. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  855. extra
  856. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph"))
  857. (format " draw:name=\"%s\""
  858. (car (org-odt-add-automatic-style "Frame"))))))
  859. (format
  860. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  861. style frame-attrs
  862. (concat text
  863. (let ((title (car title-and-desc))
  864. (desc (cadr title-and-desc)))
  865. (concat (when title
  866. (format "<svg:title>%s</svg:title>"
  867. (org-odt--encode-plain-text title t)))
  868. (when desc
  869. (format "<svg:desc>%s</svg:desc>"
  870. (org-odt--encode-plain-text desc t)))))))))
  871. ;;;; Library wrappers
  872. (defun org-odt--zip-extract (archive members target)
  873. (when (atom members) (setq members (list members)))
  874. (mapc (lambda (member)
  875. (require 'arc-mode)
  876. (let* ((--quote-file-name
  877. ;; This is shamelessly stolen from `archive-zip-extract'.
  878. (lambda (name)
  879. (if (or (not (memq system-type '(windows-nt ms-dos)))
  880. (and (boundp 'w32-quote-process-args)
  881. (null w32-quote-process-args)))
  882. (shell-quote-argument name)
  883. name)))
  884. (target (funcall --quote-file-name target))
  885. (archive (expand-file-name archive))
  886. (archive-zip-extract
  887. (list "unzip" "-qq" "-o" "-d" target))
  888. exit-code command-output)
  889. (setq command-output
  890. (with-temp-buffer
  891. (setq exit-code (archive-zip-extract archive member))
  892. (buffer-string)))
  893. (unless (zerop exit-code)
  894. (message command-output)
  895. (error "Extraction failed"))))
  896. members))
  897. ;;;; Target
  898. (defun org-odt--target (text id)
  899. (if (not id) text
  900. (concat
  901. (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
  902. (format "\n<text:bookmark text:name=\"%s\"/>" id) text
  903. (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
  904. ;;;; Textbox
  905. (defun org-odt--textbox (text width height style &optional
  906. extra anchor-type)
  907. (org-odt--frame
  908. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  909. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  910. (and (not width)
  911. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  912. text)
  913. width nil style extra anchor-type))
  914. ;;;; Table of Contents
  915. (defun org-odt-begin-toc (index-title depth)
  916. (concat
  917. (format "
  918. <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
  919. <text:table-of-content-source text:outline-level=\"%d\">
  920. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  921. " depth index-title)
  922. (let ((levels (number-sequence 1 10)))
  923. (mapconcat
  924. (lambda (level)
  925. (format
  926. "
  927. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  928. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  929. <text:index-entry-chapter/>
  930. <text:index-entry-text/>
  931. <text:index-entry-link-end/>
  932. </text:table-of-content-entry-template>
  933. " level level)) levels ""))
  934. (format "
  935. </text:table-of-content-source>
  936. <text:index-body>
  937. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  938. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  939. </text:index-title>
  940. " index-title)))
  941. (defun org-odt-end-toc ()
  942. (format "
  943. </text:index-body>
  944. </text:table-of-content>
  945. "))
  946. (defun* org-odt-format-toc-headline
  947. (todo todo-type priority text tags
  948. &key level section-number headline-label &allow-other-keys)
  949. (setq text
  950. (concat
  951. ;; Section number.
  952. (and section-number (concat section-number ". "))
  953. ;; Todo.
  954. (when todo
  955. (let ((style (if (member todo org-done-keywords)
  956. "OrgDone" "OrgTodo")))
  957. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  958. style todo)))
  959. (when priority
  960. (let* ((style (format "OrgPriority-%s" priority))
  961. (priority (format "[#%c]" priority)))
  962. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  963. style priority)))
  964. ;; Title.
  965. text
  966. ;; Tags.
  967. (when tags
  968. (concat
  969. (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
  970. "OrgTags"
  971. (mapconcat
  972. (lambda (tag)
  973. (format
  974. "<text:span text:style-name=\"%s\">%s</text:span>"
  975. "OrgTag" tag)) tags " : "))))))
  976. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  977. headline-label text))
  978. (defun org-odt-toc (depth info)
  979. (assert (wholenump depth))
  980. ;; When a headline is marked as a radio target, as in the example below:
  981. ;;
  982. ;; ** <<<Some Heading>>>
  983. ;; Some text.
  984. ;;
  985. ;; suppress generation of radio targets. i.e., Radio targets are to
  986. ;; be marked as targets within /document body/ and *not* within
  987. ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
  988. ;; and one in the document body.
  989. ;;
  990. ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
  991. ;; doesn't provide a way to disable memoization. So this doesn't
  992. ;; work.
  993. ;;
  994. ;; FIXME-2: Are there any other objects that need to be suppressed
  995. ;; within TOC?
  996. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  997. (headlines (org-export-collect-headlines
  998. info (and (wholenump depth) depth)))
  999. (backend (org-export-create-backend
  1000. :parent (org-export-backend-name
  1001. (plist-get info :back-end))
  1002. :transcoders (mapcar
  1003. (lambda (type) (cons type (lambda (d c i) c)))
  1004. (list 'radio-target)))))
  1005. (when headlines
  1006. (concat
  1007. (org-odt-begin-toc title depth)
  1008. (mapconcat
  1009. (lambda (headline)
  1010. (let* ((entry (org-odt-format-headline--wrap
  1011. headline backend info 'org-odt-format-toc-headline))
  1012. (level (org-export-get-relative-level headline info))
  1013. (style (format "Contents_20_%d" level)))
  1014. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1015. style entry)))
  1016. headlines "\n")
  1017. (org-odt-end-toc)))))
  1018. ;;;; Document styles
  1019. (defun org-odt-add-automatic-style (object-type &optional object-props)
  1020. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  1021. OBJECT-PROPS is (typically) a plist created by passing
  1022. \"#+ATTR_ODT: \" option of the object in question to
  1023. `org-odt-parse-block-attributes'.
  1024. Use `org-odt-object-counters' to generate an automatic
  1025. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  1026. new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
  1027. . STYLE-NAME)."
  1028. (assert (stringp object-type))
  1029. (let* ((object (intern object-type))
  1030. (seqvar object)
  1031. (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
  1032. (object-name (format "%s%d" object-type seqno)) style-name)
  1033. (setq org-odt-object-counters
  1034. (plist-put org-odt-object-counters seqvar seqno))
  1035. (when object-props
  1036. (setq style-name (format "Org%s" object-name))
  1037. (setq org-odt-automatic-styles
  1038. (plist-put org-odt-automatic-styles object
  1039. (append (list (list style-name object-props))
  1040. (plist-get org-odt-automatic-styles object)))))
  1041. (cons object-name style-name)))
  1042. ;;;; Checkbox
  1043. (defun org-odt--checkbox (item)
  1044. "Return check-box string associated to ITEM."
  1045. (let ((checkbox (org-element-property :checkbox item)))
  1046. (if (not checkbox) ""
  1047. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1048. "OrgCode" (case checkbox
  1049. (on "[&#x2713;] ") ; CHECK MARK
  1050. (off "[ ] ")
  1051. (trans "[-] "))))))
  1052. ;;; Template
  1053. (defun org-odt--build-date-styles (fmt style)
  1054. ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
  1055. ;; to modify the date fields. A date could be modified by
  1056. ;; offsetting in days. That's about it. Also, date and time may
  1057. ;; have to be emitted as two fields - a date field and a time field
  1058. ;; - separately.
  1059. ;; One can add Form Controls to date and time fields so that they
  1060. ;; can be easily modified. But then, the exported document will
  1061. ;; become tightly coupled with LibreOffice and may not function
  1062. ;; properly with other OpenDocument applications.
  1063. ;; I have a strange feeling that Date styles are a bit flaky at the
  1064. ;; moment.
  1065. ;; The feature is experimental.
  1066. (when (and fmt style)
  1067. (let* ((fmt-alist
  1068. '(("%A" . "<number:day-of-week number:style=\"long\"/>")
  1069. ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
  1070. ("%H" . "<number:hours number:style=\"long\"/>")
  1071. ("%M" . "<number:minutes number:style=\"long\"/>")
  1072. ("%S" . "<number:seconds number:style=\"long\"/>")
  1073. ("%V" . "<number:week-of-year/>")
  1074. ("%Y" . "<number:year number:style=\"long\"/>")
  1075. ("%a" . "<number:day-of-week number:style=\"short\"/>")
  1076. ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1077. ("%d" . "<number:day number:style=\"long\"/>")
  1078. ("%e" . "<number:day number:style=\"short\"/>")
  1079. ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1080. ("%k" . "<number:hours number:style=\"short\"/>")
  1081. ("%m" . "<number:month number:style=\"long\"/>")
  1082. ("%p" . "<number:am-pm/>")
  1083. ("%y" . "<number:year number:style=\"short\"/>")))
  1084. (case-fold-search nil)
  1085. (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
  1086. match rpl (start 0) (filler-beg 0) filler-end filler output)
  1087. (mapc
  1088. (lambda (pair)
  1089. (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
  1090. '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
  1091. ("%C" . "Y") ; replace century with year
  1092. ("%D" . "%m/%d/%y")
  1093. ("%G" . "Y") ; year corresponding to iso week
  1094. ("%I" . "%H") ; hour on a 12-hour clock
  1095. ("%R" . "%H:%M")
  1096. ("%T" . "%H:%M:%S")
  1097. ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
  1098. ("%Z" . "") ; time zone name
  1099. ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
  1100. ("%g" . "%y")
  1101. ("%X" . "%x" ) ; locale's pref. time format
  1102. ("%j" . "") ; day of the year
  1103. ("%l" . "%k") ; like %I blank-padded
  1104. ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
  1105. ("%n" . "<text:line-break/>")
  1106. ("%r" . "%I:%M:%S %p")
  1107. ("%t" . "<text:tab/>")
  1108. ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
  1109. ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
  1110. ("%z" . "") ; time zone in numeric form
  1111. ))
  1112. (while (string-match re fmt start)
  1113. (setq match (match-string 0 fmt))
  1114. (setq rpl (assoc-default match fmt-alist))
  1115. (setq start (match-end 0))
  1116. (setq filler-end (match-beginning 0))
  1117. (setq filler (substring fmt (prog1 filler-beg
  1118. (setq filler-beg (match-end 0)))
  1119. filler-end))
  1120. (setq filler (and (not (string= filler ""))
  1121. (format "<number:text>%s</number:text>"
  1122. (org-odt--encode-plain-text filler))))
  1123. (setq output (concat output "\n" filler "\n" rpl)))
  1124. (setq filler (substring fmt filler-beg))
  1125. (unless (string= filler "")
  1126. (setq output (concat output
  1127. (format "\n<number:text>%s</number:text>"
  1128. (org-odt--encode-plain-text filler)))))
  1129. (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
  1130. style
  1131. (concat " number:automatic-order=\"true\""
  1132. " number:format-source=\"fixed\"")
  1133. output ))))
  1134. (defun org-odt-template (contents info)
  1135. "Return complete document string after ODT conversion.
  1136. CONTENTS is the transcoded contents string. RAW-DATA is the
  1137. original parsed data. INFO is a plist holding export options."
  1138. ;; Write meta file.
  1139. (let ((title (org-export-data (plist-get info :title) info))
  1140. (author (let ((author (plist-get info :author)))
  1141. (if (not author) "" (org-export-data author info))))
  1142. (email (plist-get info :email))
  1143. (keywords (plist-get info :keywords))
  1144. (description (plist-get info :description)))
  1145. (write-region
  1146. (concat
  1147. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1148. <office:document-meta
  1149. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1150. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1151. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1152. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1153. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1154. office:version=\"1.2\">
  1155. <office:meta>\n"
  1156. (format "<dc:creator>%s</dc:creator>\n" author)
  1157. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1158. ;; Date, if required.
  1159. (when (plist-get info :with-date)
  1160. ;; Check if DATE is specified as an Org-timestamp. If yes,
  1161. ;; include it as meta information. Otherwise, just use
  1162. ;; today's date.
  1163. (let* ((date (let ((date (plist-get info :date)))
  1164. (and (not (cdr date))
  1165. (eq (org-element-type (car date)) 'timestamp)
  1166. (car date)))))
  1167. (let ((iso-date (org-odt--format-timestamp date nil 'iso-date)))
  1168. (concat
  1169. (format "<dc:date>%s</dc:date>\n" iso-date)
  1170. (format "<meta:creation-date>%s</meta:creation-date>\n"
  1171. iso-date)))))
  1172. (format "<meta:generator>%s</meta:generator>\n"
  1173. (let ((creator-info (plist-get info :with-creator)))
  1174. (if (or (not creator-info) (eq creator-info 'comment)) ""
  1175. (plist-get info :creator))))
  1176. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1177. (format "<dc:subject>%s</dc:subject>\n" description)
  1178. (format "<dc:title>%s</dc:title>\n" title)
  1179. "\n"
  1180. " </office:meta>\n" "</office:document-meta>")
  1181. nil (concat org-odt-zip-dir "meta.xml"))
  1182. ;; Add meta.xml in to manifest.
  1183. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1184. ;; Update styles file.
  1185. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1186. ;; Write styles file.
  1187. (let* ((styles-file (plist-get info :odt-styles-file))
  1188. (styles-file (and styles-file (read (org-trim styles-file))))
  1189. ;; Non-availability of styles.xml is not a critical
  1190. ;; error. For now, throw an error.
  1191. (styles-file (or styles-file
  1192. (plist-get info :odt-styles-file)
  1193. (expand-file-name "OrgOdtStyles.xml"
  1194. org-odt-styles-dir)
  1195. (error "org-odt: Missing styles file?"))))
  1196. (cond
  1197. ((listp styles-file)
  1198. (let ((archive (nth 0 styles-file))
  1199. (members (nth 1 styles-file)))
  1200. (org-odt--zip-extract archive members org-odt-zip-dir)
  1201. (mapc
  1202. (lambda (member)
  1203. (when (org-file-image-p member)
  1204. (let* ((image-type (file-name-extension member))
  1205. (media-type (format "image/%s" image-type)))
  1206. (org-odt-create-manifest-file-entry media-type member))))
  1207. members)))
  1208. ((and (stringp styles-file) (file-exists-p styles-file))
  1209. (let ((styles-file-type (file-name-extension styles-file)))
  1210. (cond
  1211. ((string= styles-file-type "xml")
  1212. (copy-file styles-file (concat org-odt-zip-dir "styles.xml") t))
  1213. ((member styles-file-type '("odt" "ott"))
  1214. (org-odt--zip-extract styles-file "styles.xml" org-odt-zip-dir)))))
  1215. (t
  1216. (error (format "Invalid specification of styles.xml file: %S"
  1217. (plist-get info :odt-styles-file)))))
  1218. ;; create a manifest entry for styles.xml
  1219. (org-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1220. ;; FIXME: Who is opening an empty styles.xml before this point?
  1221. (with-current-buffer
  1222. (find-file-noselect (concat org-odt-zip-dir "styles.xml") t)
  1223. (revert-buffer t t)
  1224. ;; Write custom styles for source blocks
  1225. ;; Save STYLES used for colorizing of source blocks.
  1226. ;; Update styles.xml with styles that were collected as part of
  1227. ;; `org-odt-hfy-face-to-css' callbacks.
  1228. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1229. hfy-user-sheet-assoc "")))
  1230. (when styles
  1231. (goto-char (point-min))
  1232. (when (re-search-forward "</office:styles>" nil t)
  1233. (goto-char (match-beginning 0))
  1234. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1235. ;; Update styles.xml - take care of outline numbering
  1236. ;; Don't make automatic backup of styles.xml file. This setting
  1237. ;; prevents the backed-up styles.xml file from being zipped in to
  1238. ;; odt file. This is more of a hackish fix. Better alternative
  1239. ;; would be to fix the zip command so that the output odt file
  1240. ;; includes only the needed files and excludes any auto-generated
  1241. ;; extra files like backups and auto-saves etc etc. Note that
  1242. ;; currently the zip command zips up the entire temp directory so
  1243. ;; that any auto-generated files created under the hood ends up in
  1244. ;; the resulting odt file.
  1245. (set (make-local-variable 'backup-inhibited) t)
  1246. ;; Outline numbering is retained only upto LEVEL.
  1247. ;; To disable outline numbering pass a LEVEL of 0.
  1248. (goto-char (point-min))
  1249. (let ((regex
  1250. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1251. (replacement
  1252. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1253. (while (re-search-forward regex nil t)
  1254. (unless (let ((sec-num (plist-get info :section-numbers))
  1255. (level (string-to-number (match-string 2))))
  1256. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1257. (replace-match replacement t nil))))
  1258. (save-buffer 0)))
  1259. ;; Update content.xml.
  1260. (let* ( ;; `org-display-custom-times' should be accessed right
  1261. ;; within the context of the Org buffer. So obtain its
  1262. ;; value before moving on to temp-buffer context down below.
  1263. (custom-time-fmts
  1264. (if org-display-custom-times
  1265. (cons (substring (car org-time-stamp-custom-formats) 1 -1)
  1266. (substring (cdr org-time-stamp-custom-formats) 1 -1))
  1267. '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
  1268. (with-temp-buffer
  1269. (insert-file-contents
  1270. (or (plist-get info :odt-content-template-file)
  1271. (expand-file-name "OrgOdtContentTemplate.xml"
  1272. org-odt-styles-dir)))
  1273. ;; Write automatic styles.
  1274. ;; - Position the cursor.
  1275. (goto-char (point-min))
  1276. (re-search-forward " </office:automatic-styles>" nil t)
  1277. (goto-char (match-beginning 0))
  1278. ;; - Dump automatic table styles.
  1279. (loop for (style-name props) in
  1280. (plist-get org-odt-automatic-styles 'Table) do
  1281. (when (setq props (or (plist-get props :rel-width) "96"))
  1282. (insert (format org-odt-table-style-format style-name props))))
  1283. ;; - Dump date-styles.
  1284. (when (plist-get info :odt-use-date-fields)
  1285. (insert (org-odt--build-date-styles (car custom-time-fmts)
  1286. "OrgDate1")
  1287. (org-odt--build-date-styles (cdr custom-time-fmts)
  1288. "OrgDate2")))
  1289. ;; Update display level.
  1290. ;; - Remove existing sequence decls. Also position the cursor.
  1291. (goto-char (point-min))
  1292. (when (re-search-forward "<text:sequence-decls" nil t)
  1293. (delete-region (match-beginning 0)
  1294. (re-search-forward "</text:sequence-decls>" nil nil)))
  1295. ;; Update sequence decls according to user preference.
  1296. (insert
  1297. (format
  1298. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1299. (mapconcat
  1300. (lambda (x)
  1301. (format
  1302. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1303. (plist-get info :odt-display-outline-level)
  1304. (nth 1 x)))
  1305. org-odt-category-map-alist "\n")))
  1306. ;; Position the cursor to document body.
  1307. (goto-char (point-min))
  1308. (re-search-forward "</office:text>" nil nil)
  1309. (goto-char (match-beginning 0))
  1310. ;; Preamble - Title, Author, Date etc.
  1311. (insert
  1312. (let* ((title (org-export-data (plist-get info :title) info))
  1313. (author (and (plist-get info :with-author)
  1314. (let ((auth (plist-get info :author)))
  1315. (and auth (org-export-data auth info)))))
  1316. (email (plist-get info :email))
  1317. ;; Switch on or off above vars based on user settings
  1318. (author (and (plist-get info :with-author) (or author email)))
  1319. (email (and (plist-get info :with-email) email)))
  1320. (concat
  1321. ;; Title.
  1322. (when (org-string-nw-p title)
  1323. (concat
  1324. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1325. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1326. ;; Separator.
  1327. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1328. (cond
  1329. ((and author (not email))
  1330. ;; Author only.
  1331. (concat
  1332. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1333. "OrgSubtitle"
  1334. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1335. ;; Separator.
  1336. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1337. ((and author email)
  1338. ;; Author and E-mail.
  1339. (concat
  1340. (format
  1341. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1342. "OrgSubtitle"
  1343. (format
  1344. "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1345. (concat "mailto:" email)
  1346. (format "<text:initial-creator>%s</text:initial-creator>" author)))
  1347. ;; Separator.
  1348. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1349. ;; Date, if required.
  1350. (when (plist-get info :with-date)
  1351. (let* ((date (plist-get info :date))
  1352. ;; Check if DATE is specified as a timestamp.
  1353. (timestamp (and (not (cdr date))
  1354. (eq (org-element-type (car date)) 'timestamp)
  1355. (car date))))
  1356. (concat
  1357. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1358. "OrgSubtitle"
  1359. (if (and (plist-get info :odt-use-date-fields) timestamp)
  1360. (org-odt--format-timestamp (car date))
  1361. (org-export-data (plist-get info :date) info)))
  1362. ;; Separator
  1363. "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
  1364. ;; Table of Contents
  1365. (let* ((with-toc (plist-get info :with-toc))
  1366. (depth (and with-toc (if (wholenump with-toc)
  1367. with-toc
  1368. (plist-get info :headline-levels)))))
  1369. (when depth (insert (or (org-odt-toc depth info) ""))))
  1370. ;; Contents.
  1371. (insert contents)
  1372. ;; Return contents.
  1373. (buffer-substring-no-properties (point-min) (point-max)))))
  1374. ;;; Transcode Functions
  1375. ;;;; Bold
  1376. (defun org-odt-bold (bold contents info)
  1377. "Transcode BOLD from Org to ODT.
  1378. CONTENTS is the text with bold markup. INFO is a plist holding
  1379. contextual information."
  1380. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1381. "Bold" contents))
  1382. ;;;; Center Block
  1383. (defun org-odt-center-block (center-block contents info)
  1384. "Transcode a CENTER-BLOCK element from Org to ODT.
  1385. CONTENTS holds the contents of the center block. INFO is a plist
  1386. holding contextual information."
  1387. contents)
  1388. ;;;; Clock
  1389. (defun org-odt-clock (clock contents info)
  1390. "Transcode a CLOCK element from Org to ODT.
  1391. CONTENTS is nil. INFO is a plist used as a communication
  1392. channel."
  1393. (let ((timestamp (org-element-property :value clock))
  1394. (duration (org-element-property :duration clock)))
  1395. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1396. (if (eq (org-element-type (org-export-get-next-element clock info))
  1397. 'clock) "OrgClock" "OrgClockLastLine")
  1398. (concat
  1399. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1400. "OrgClockKeyword" org-clock-string)
  1401. (org-odt-timestamp timestamp contents info)
  1402. (and duration (format " (%s)" duration))))))
  1403. ;;;; Code
  1404. (defun org-odt-code (code contents info)
  1405. "Transcode a CODE object from Org to ODT.
  1406. CONTENTS is nil. INFO is a plist used as a communication
  1407. channel."
  1408. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1409. "OrgCode" (org-odt--encode-plain-text
  1410. (org-element-property :value code))))
  1411. ;;;; Comment
  1412. ;; Comments are ignored.
  1413. ;;;; Comment Block
  1414. ;; Comment Blocks are ignored.
  1415. ;;;; Drawer
  1416. (defun org-odt-drawer (drawer contents info)
  1417. "Transcode a DRAWER element from Org to ODT.
  1418. CONTENTS holds the contents of the block. INFO is a plist
  1419. holding contextual information."
  1420. (let* ((name (org-element-property :drawer-name drawer))
  1421. (output (funcall (plist-get info :odt-format-drawer-function)
  1422. name contents)))
  1423. output))
  1424. ;;;; Dynamic Block
  1425. (defun org-odt-dynamic-block (dynamic-block contents info)
  1426. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1427. CONTENTS holds the contents of the block. INFO is a plist
  1428. holding contextual information. See `org-export-data'."
  1429. contents)
  1430. ;;;; Entity
  1431. (defun org-odt-entity (entity contents info)
  1432. "Transcode an ENTITY object from Org to ODT.
  1433. CONTENTS are the definition itself. INFO is a plist holding
  1434. contextual information."
  1435. (org-element-property :utf-8 entity))
  1436. ;;;; Example Block
  1437. (defun org-odt-example-block (example-block contents info)
  1438. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1439. CONTENTS is nil. INFO is a plist holding contextual information."
  1440. (org-odt-format-code example-block info))
  1441. ;;;; Export Snippet
  1442. (defun org-odt-export-snippet (export-snippet contents info)
  1443. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1444. CONTENTS is nil. INFO is a plist holding contextual information."
  1445. (when (eq (org-export-snippet-backend export-snippet) 'odt)
  1446. (org-element-property :value export-snippet)))
  1447. ;;;; Export Block
  1448. (defun org-odt-export-block (export-block contents info)
  1449. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1450. CONTENTS is nil. INFO is a plist holding contextual information."
  1451. (when (string= (org-element-property :type export-block) "ODT")
  1452. (org-remove-indentation (org-element-property :value export-block))))
  1453. ;;;; Fixed Width
  1454. (defun org-odt-fixed-width (fixed-width contents info)
  1455. "Transcode a FIXED-WIDTH element from Org to ODT.
  1456. CONTENTS is nil. INFO is a plist holding contextual information."
  1457. (org-odt-do-format-code (org-element-property :value fixed-width) info))
  1458. ;;;; Footnote Definition
  1459. ;; Footnote Definitions are ignored.
  1460. ;;;; Footnote Reference
  1461. (defun org-odt-footnote-reference (footnote-reference contents info)
  1462. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1463. CONTENTS is nil. INFO is a plist holding contextual information."
  1464. (let ((--format-footnote-definition
  1465. (function
  1466. (lambda (n def)
  1467. (setq n (format "%d" n))
  1468. (let ((id (concat "fn" n))
  1469. (note-class "footnote")
  1470. (par-style "Footnote"))
  1471. (format
  1472. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1473. id note-class
  1474. (concat
  1475. (format "<text:note-citation>%s</text:note-citation>" n)
  1476. (format "<text:note-body>%s</text:note-body>" def)))))))
  1477. (--format-footnote-reference
  1478. (function
  1479. (lambda (n)
  1480. (setq n (format "%d" n))
  1481. (let ((note-class "footnote")
  1482. (ref-format "text")
  1483. (ref-name (concat "fn" n)))
  1484. (format
  1485. "<text:span text:style-name=\"%s\">%s</text:span>"
  1486. "OrgSuperscript"
  1487. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1488. note-class ref-format ref-name n)))))))
  1489. (concat
  1490. ;; Insert separator between two footnotes in a row.
  1491. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1492. (and (eq (org-element-type prev) 'footnote-reference)
  1493. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1494. "OrgSuperscript" ",")))
  1495. ;; Transcode footnote reference.
  1496. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1497. (cond
  1498. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1499. (funcall --format-footnote-reference n))
  1500. ;; Inline definitions are secondary strings.
  1501. ;; Non-inline footnotes definitions are full Org data.
  1502. (t
  1503. (let* ((raw (org-export-get-footnote-definition
  1504. footnote-reference info))
  1505. (def
  1506. (let ((def (org-trim
  1507. (org-export-data-with-backend
  1508. raw
  1509. (org-export-create-backend
  1510. :parent 'odt
  1511. :transcoders
  1512. '((paragraph . (lambda (p c i)
  1513. (org-odt--format-paragraph
  1514. p c i
  1515. "Footnote"
  1516. "OrgFootnoteCenter"
  1517. "OrgFootnoteQuotations")))))
  1518. info))))
  1519. (if (eq (org-element-type raw) 'org-data) def
  1520. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1521. "Footnote" def)))))
  1522. (funcall --format-footnote-definition n def))))))))
  1523. ;;;; Headline
  1524. (defun org-odt-format-headline--wrap (headline backend info
  1525. &optional format-function
  1526. &rest extra-keys)
  1527. "Transcode a HEADLINE element using BACKEND.
  1528. INFO is a plist holding contextual information."
  1529. (setq backend (or backend (plist-get info :back-end)))
  1530. (let* ((level (+ (org-export-get-relative-level headline info)))
  1531. (headline-number (org-export-get-headline-number headline info))
  1532. (section-number (and (org-export-numbered-headline-p headline info)
  1533. (mapconcat 'number-to-string
  1534. headline-number ".")))
  1535. (todo (and (plist-get info :with-todo-keywords)
  1536. (let ((todo (org-element-property :todo-keyword headline)))
  1537. (and todo
  1538. (org-export-data-with-backend todo backend info)))))
  1539. (todo-type (and todo (org-element-property :todo-type headline)))
  1540. (priority (and (plist-get info :with-priority)
  1541. (org-element-property :priority headline)))
  1542. (text (org-export-data-with-backend
  1543. (org-element-property :title headline) backend info))
  1544. (tags (and (plist-get info :with-tags)
  1545. (org-export-get-tags headline info)))
  1546. (headline-label (org-export-get-headline-id headline info))
  1547. (format-function
  1548. (if (functionp format-function) format-function
  1549. (function*
  1550. (lambda (todo todo-type priority text tags
  1551. &key level section-number headline-label
  1552. &allow-other-keys)
  1553. (funcall (plist-get info :odt-format-headline-function)
  1554. todo todo-type priority text tags))))))
  1555. (apply format-function
  1556. todo todo-type priority text tags
  1557. :headline-label headline-label :level level
  1558. :section-number section-number extra-keys)))
  1559. (defun org-odt-headline (headline contents info)
  1560. "Transcode a HEADLINE element from Org to ODT.
  1561. CONTENTS holds the contents of the headline. INFO is a plist
  1562. holding contextual information."
  1563. ;; Case 1: This is a footnote section: ignore it.
  1564. (unless (org-element-property :footnote-section-p headline)
  1565. (let* ((text (org-export-data (org-element-property :title headline) info))
  1566. ;; Create the headline text.
  1567. (full-text (org-odt-format-headline--wrap headline nil info))
  1568. ;; Get level relative to current parsed data.
  1569. (level (org-export-get-relative-level headline info))
  1570. (numbered (org-export-numbered-headline-p headline info))
  1571. ;; Get canonical label for the headline.
  1572. (id (org-export-get-headline-id headline info))
  1573. ;; Get user-specified labels for the headline.
  1574. (extra-ids (list (org-element-property :CUSTOM_ID headline)
  1575. (org-element-property :ID headline)))
  1576. ;; Extra targets.
  1577. (extra-targets
  1578. (mapconcat (lambda (x)
  1579. (when x
  1580. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1581. (org-odt--target
  1582. "" (org-export-solidify-link-text x)))))
  1583. extra-ids ""))
  1584. ;; Title.
  1585. (anchored-title (org-odt--target full-text id)))
  1586. (cond
  1587. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1588. ;; Also export as items headlines for which no section
  1589. ;; format has been found.
  1590. ((org-export-low-level-p headline info)
  1591. ;; Build the real contents of the sub-tree.
  1592. (concat
  1593. (and (org-export-first-sibling-p headline info)
  1594. (format "\n<text:list text:style-name=\"%s\" %s>"
  1595. ;; Choose style based on list type.
  1596. (if numbered "OrgNumberedList" "OrgBulletedList")
  1597. ;; If top-level list, re-start numbering. Otherwise,
  1598. ;; continue numbering.
  1599. (format "text:continue-numbering=\"%s\""
  1600. (let* ((parent (org-export-get-parent-headline
  1601. headline)))
  1602. (if (and parent
  1603. (org-export-low-level-p parent info))
  1604. "true" "false")))))
  1605. (let ((headline-has-table-p
  1606. (let ((section (assq 'section (org-element-contents headline))))
  1607. (assq 'table (and section (org-element-contents section))))))
  1608. (format "\n<text:list-item>\n%s\n%s"
  1609. (concat
  1610. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1611. "Text_20_body"
  1612. (concat extra-targets anchored-title))
  1613. contents)
  1614. (if headline-has-table-p
  1615. "</text:list-header>"
  1616. "</text:list-item>")))
  1617. (and (org-export-last-sibling-p headline info)
  1618. "</text:list>")))
  1619. ;; Case 3. Standard headline. Export it as a section.
  1620. (t
  1621. (concat
  1622. (format
  1623. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\" text:is-list-header=\"%s\">%s</text:h>"
  1624. (format "Heading_20_%s%s"
  1625. level (if numbered "" "_unnumbered"))
  1626. level
  1627. (if numbered "false" "true")
  1628. (concat extra-targets anchored-title))
  1629. contents))))))
  1630. (defun org-odt-format-headline-default-function
  1631. (todo todo-type priority text tags)
  1632. "Default format function for a headline.
  1633. See `org-odt-format-headline-function' for details."
  1634. (concat
  1635. ;; Todo.
  1636. (when todo
  1637. (let ((style (if (eq todo-type 'done) "OrgDone" "OrgTodo")))
  1638. (format "<text:span text:style-name=\"%s\">%s</text:span> " style todo)))
  1639. (when priority
  1640. (let* ((style (format "OrgPriority-%s" priority))
  1641. (priority (format "[#%c]" priority)))
  1642. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  1643. style priority)))
  1644. ;; Title.
  1645. text
  1646. ;; Tags.
  1647. (when tags
  1648. (concat
  1649. "<text:tab/>"
  1650. (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
  1651. "OrgTags" (mapconcat
  1652. (lambda (tag)
  1653. (format
  1654. "<text:span text:style-name=\"%s\">%s</text:span>"
  1655. "OrgTag" tag)) tags " : "))))))
  1656. ;;;; Horizontal Rule
  1657. (defun org-odt-horizontal-rule (horizontal-rule contents info)
  1658. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1659. CONTENTS is nil. INFO is a plist holding contextual information."
  1660. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1661. "Horizontal_20_Line" ""))
  1662. ;;;; Inline Babel Call
  1663. ;; Inline Babel Calls are ignored.
  1664. ;;;; Inline Src Block
  1665. (defun org-odt--find-verb-separator (s)
  1666. "Return a character not used in string S.
  1667. This is used to choose a separator for constructs like \\verb."
  1668. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1669. (loop for c across ll
  1670. when (not (string-match (regexp-quote (char-to-string c)) s))
  1671. return (char-to-string c))))
  1672. (defun org-odt-inline-src-block (inline-src-block contents info)
  1673. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1674. CONTENTS holds the contents of the item. INFO is a plist holding
  1675. contextual information."
  1676. (let* ((org-lang (org-element-property :language inline-src-block))
  1677. (code (org-element-property :value inline-src-block))
  1678. (separator (org-odt--find-verb-separator code)))
  1679. (error "FIXME")))
  1680. ;;;; Inlinetask
  1681. (defun org-odt-inlinetask (inlinetask contents info)
  1682. "Transcode an INLINETASK element from Org to ODT.
  1683. CONTENTS holds the contents of the block. INFO is a plist
  1684. holding contextual information."
  1685. (let* ((todo
  1686. (and (plist-get info :with-todo-keywords)
  1687. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1688. (and todo (org-export-data todo info)))))
  1689. (todo-type (and todo (org-element-property :todo-type inlinetask)))
  1690. (priority (and (plist-get info :with-priority)
  1691. (org-element-property :priority inlinetask)))
  1692. (text (org-export-data (org-element-property :title inlinetask) info))
  1693. (tags (and (plist-get info :with-tags)
  1694. (org-export-get-tags inlinetask info))))
  1695. (funcall (plist-get info :odt-format-inlinetask-function)
  1696. todo todo-type priority text tags contents)))
  1697. (defun org-odt-format-inlinetask-default-function
  1698. (todo todo-type priority name tags contents)
  1699. "Default format function for a inlinetasks.
  1700. See `org-odt-format-inlinetask-function' for details."
  1701. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1702. "Text_20_body"
  1703. (org-odt--textbox
  1704. (concat
  1705. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1706. "OrgInlineTaskHeading"
  1707. (org-odt-format-headline-default-function
  1708. todo todo-type priority name tags))
  1709. contents)
  1710. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1711. ;;;; Italic
  1712. (defun org-odt-italic (italic contents info)
  1713. "Transcode ITALIC from Org to ODT.
  1714. CONTENTS is the text with italic markup. INFO is a plist holding
  1715. contextual information."
  1716. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1717. "Emphasis" contents))
  1718. ;;;; Item
  1719. (defun org-odt-item (item contents info)
  1720. "Transcode an ITEM element from Org to ODT.
  1721. CONTENTS holds the contents of the item. INFO is a plist holding
  1722. contextual information."
  1723. (let* ((plain-list (org-export-get-parent item))
  1724. (type (org-element-property :type plain-list))
  1725. (counter (org-element-property :counter item))
  1726. (tag (let ((tag (org-element-property :tag item)))
  1727. (and tag
  1728. (concat (org-odt--checkbox item)
  1729. (org-export-data tag info))))))
  1730. (case type
  1731. ((ordered unordered descriptive-1 descriptive-2)
  1732. (format "\n<text:list-item>\n%s\n%s"
  1733. contents
  1734. (let* ((--element-has-a-table-p
  1735. (function
  1736. (lambda (element info)
  1737. (loop for el in (org-element-contents element)
  1738. thereis (eq (org-element-type el) 'table))))))
  1739. (cond
  1740. ((funcall --element-has-a-table-p item info)
  1741. "</text:list-header>")
  1742. (t "</text:list-item>")))))
  1743. (t (error "Unknown list type: %S" type)))))
  1744. ;;;; Keyword
  1745. (defun org-odt-keyword (keyword contents info)
  1746. "Transcode a KEYWORD element from Org to ODT.
  1747. CONTENTS is nil. INFO is a plist holding contextual information."
  1748. (let ((key (org-element-property :key keyword))
  1749. (value (org-element-property :value keyword)))
  1750. (cond
  1751. ((string= key "ODT") value)
  1752. ((string= key "INDEX")
  1753. ;; FIXME
  1754. (ignore))
  1755. ((string= key "TOC")
  1756. (let ((value (downcase value)))
  1757. (cond
  1758. ((string-match "\\<headlines\\>" value)
  1759. (let ((depth (or (and (string-match "[0-9]+" value)
  1760. (string-to-number (match-string 0 value)))
  1761. (plist-get info :with-toc))))
  1762. (when (wholenump depth) (org-odt-toc depth info))))
  1763. ((member value '("tables" "figures" "listings"))
  1764. ;; FIXME
  1765. (ignore))))))))
  1766. ;;;; Latex Environment
  1767. ;; (eval-after-load 'ox-odt '(ad-deactivate 'org-format-latex-as-mathml))
  1768. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1769. ;; (after org-odt-protect-latex-fragment activate)
  1770. ;; "Encode LaTeX fragment as XML.
  1771. ;; Do this when translation to MathML fails."
  1772. ;; (unless (> (length ad-return-value) 0)
  1773. ;; (setq ad-return-value (org-odt--encode-plain-text (ad-get-arg 0)))))
  1774. (defun org-odt-latex-environment (latex-environment contents info)
  1775. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1776. CONTENTS is nil. INFO is a plist holding contextual information."
  1777. (let* ((latex-frag (org-remove-indentation
  1778. (org-element-property :value latex-environment))))
  1779. (org-odt-do-format-code latex-frag info)))
  1780. ;;;; Latex Fragment
  1781. ;; (when latex-frag ; FIXME
  1782. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1783. ;; :description latex-frag)))
  1784. ;; handle verbatim
  1785. ;; provide descriptions
  1786. (defun org-odt-latex-fragment (latex-fragment contents info)
  1787. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1788. CONTENTS is nil. INFO is a plist holding contextual information."
  1789. (let* ((latex-frag (org-element-property :value latex-fragment))
  1790. (processing-type (plist-get info :with-latex)))
  1791. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1792. "OrgCode" (org-odt--encode-plain-text latex-frag t))))
  1793. ;;;; Line Break
  1794. (defun org-odt-line-break (line-break contents info)
  1795. "Transcode a LINE-BREAK object from Org to ODT.
  1796. CONTENTS is nil. INFO is a plist holding contextual information."
  1797. "<text:line-break/>")
  1798. ;;;; Link
  1799. ;;;; Links :: Label references
  1800. (defun org-odt--enumerate (element info &optional predicate n)
  1801. (when predicate (assert (funcall predicate element info)))
  1802. (let* ((--numbered-parent-headline-at-<=-n
  1803. (function
  1804. (lambda (element n info)
  1805. (loop for x in (org-element-lineage element)
  1806. thereis (and (eq (org-element-type x) 'headline)
  1807. (<= (org-export-get-relative-level x info) n)
  1808. (org-export-numbered-headline-p x info)
  1809. x)))))
  1810. (--enumerate
  1811. (function
  1812. (lambda (element scope info &optional predicate)
  1813. (let ((counter 0))
  1814. (org-element-map (or scope (plist-get info :parse-tree))
  1815. (org-element-type element)
  1816. (lambda (el)
  1817. (and (or (not predicate) (funcall predicate el info))
  1818. (incf counter)
  1819. (eq element el)
  1820. counter))
  1821. info 'first-match)))))
  1822. (scope (funcall --numbered-parent-headline-at-<=-n
  1823. element
  1824. (or n (plist-get info :odt-display-outline-level))
  1825. info))
  1826. (ordinal (funcall --enumerate element scope info predicate))
  1827. (tag
  1828. (concat
  1829. ;; Section number.
  1830. (and scope
  1831. (mapconcat 'number-to-string
  1832. (org-export-get-headline-number scope info) "."))
  1833. ;; Separator.
  1834. (and scope ".")
  1835. ;; Ordinal.
  1836. (number-to-string ordinal))))
  1837. tag))
  1838. (defun org-odt-format-label (element info op)
  1839. "Return a label for ELEMENT.
  1840. ELEMENT is a `link', `table', `src-block' or `paragraph' type
  1841. element. INFO is a plist used as a communication channel. OP is
  1842. either `definition' or `reference', depending on the purpose of
  1843. the generated string.
  1844. Return value is a string if OP is set to `reference' or a cons
  1845. cell like CAPTION . SHORT-CAPTION) where CAPTION and
  1846. SHORT-CAPTION are strings."
  1847. (assert (memq (org-element-type element) '(link table src-block paragraph)))
  1848. (let* ((caption-from
  1849. (case (org-element-type element)
  1850. (link (org-export-get-parent-element element))
  1851. (t element)))
  1852. ;; Get label and caption.
  1853. (label (org-element-property :name caption-from))
  1854. (caption (org-export-get-caption caption-from))
  1855. (caption (and caption (org-export-data caption info)))
  1856. ;; FIXME: We don't use short-caption for now
  1857. (short-caption nil))
  1858. (when (or label caption)
  1859. (let* ((default-category
  1860. (case (org-element-type element)
  1861. (table "__Table__")
  1862. (src-block "__Listing__")
  1863. ((link paragraph)
  1864. (cond
  1865. ((org-odt--enumerable-latex-image-p element info)
  1866. "__DvipngImage__")
  1867. ((org-odt--enumerable-image-p element info)
  1868. "__Figure__")
  1869. ((org-odt--enumerable-formula-p element info)
  1870. "__MathFormula__")
  1871. (t (error "Don't know how to format label for link: %S"
  1872. element))))
  1873. (t (error "Don't know how to format label for element type: %s"
  1874. (org-element-type element)))))
  1875. seqno)
  1876. (assert default-category)
  1877. (destructuring-bind (counter label-style category predicate)
  1878. (assoc-default default-category org-odt-category-map-alist)
  1879. ;; Compute sequence number of the element.
  1880. (setq seqno (org-odt--enumerate element info predicate))
  1881. ;; Localize category string.
  1882. (setq category (org-export-translate category :utf-8 info))
  1883. (case op
  1884. ;; Case 1: Handle Label definition.
  1885. (definition
  1886. ;; Assign an internal label, if user has not provided one
  1887. (setq label (org-export-solidify-link-text
  1888. (or label (format "%s-%s" default-category seqno))))
  1889. (cons
  1890. (concat
  1891. ;; Sneak in a bookmark. The bookmark is used when the
  1892. ;; labeled element is referenced with a link that
  1893. ;; provides its own description.
  1894. (format "\n<text:bookmark text:name=\"%s\"/>" label)
  1895. ;; Label definition: Typically formatted as below:
  1896. ;; CATEGORY SEQ-NO: LONG CAPTION
  1897. ;; with translation for correct punctuation.
  1898. (format-spec
  1899. (org-export-translate
  1900. (cadr (assoc-string label-style org-odt-label-styles t))
  1901. :utf-8 info)
  1902. `((?e . ,category)
  1903. (?n . ,(format
  1904. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1905. label counter counter seqno))
  1906. (?c . ,(or caption "")))))
  1907. short-caption))
  1908. ;; Case 2: Handle Label reference.
  1909. (reference
  1910. (assert label)
  1911. (setq label (org-export-solidify-link-text label))
  1912. (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
  1913. (fmt1 (car fmt))
  1914. (fmt2 (cadr fmt)))
  1915. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1916. fmt1 label (format-spec fmt2 `((?e . ,category)
  1917. (?n . ,seqno))))))
  1918. (t (error "Unknown %S on label" op))))))))
  1919. ;;;; Links :: Inline Images
  1920. (defun org-odt--copy-image-file (path)
  1921. "Returns the internal name of the file"
  1922. (let* ((image-type (file-name-extension path))
  1923. (media-type (format "image/%s" image-type))
  1924. (target-dir "Images/")
  1925. (target-file
  1926. (format "%s%04d.%s" target-dir
  1927. (incf org-odt-embedded-images-count) image-type)))
  1928. (message "Embedding %s as %s..."
  1929. (substring-no-properties path) target-file)
  1930. (when (= 1 org-odt-embedded-images-count)
  1931. (make-directory (concat org-odt-zip-dir target-dir))
  1932. (org-odt-create-manifest-file-entry "" target-dir))
  1933. (copy-file path (concat org-odt-zip-dir target-file) 'overwrite)
  1934. (org-odt-create-manifest-file-entry media-type target-file)
  1935. target-file))
  1936. (defun org-odt--image-size
  1937. (file info &optional user-width user-height scale dpi embed-as)
  1938. (let* ((--pixels-to-cms
  1939. (function (lambda (pixels dpi)
  1940. (let ((cms-per-inch 2.54)
  1941. (inches (/ pixels dpi)))
  1942. (* cms-per-inch inches)))))
  1943. (--size-in-cms
  1944. (function
  1945. (lambda (size-in-pixels dpi)
  1946. (and size-in-pixels
  1947. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1948. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  1949. (dpi (or dpi (plist-get info :odt-pixels-per-inch)))
  1950. (anchor-type (or embed-as "paragraph"))
  1951. (user-width (and (not scale) user-width))
  1952. (user-height (and (not scale) user-height))
  1953. (size
  1954. (and
  1955. (not (and user-height user-width))
  1956. (or
  1957. ;; Use Imagemagick.
  1958. (and (executable-find "identify")
  1959. (let ((size-in-pixels
  1960. (let ((dim (shell-command-to-string
  1961. (format "identify -format \"%%w:%%h\" \"%s\""
  1962. file))))
  1963. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1964. (cons (string-to-number (match-string 1 dim))
  1965. (string-to-number (match-string 2 dim)))))))
  1966. (funcall --size-in-cms size-in-pixels dpi)))
  1967. ;; Use Emacs.
  1968. (let ((size-in-pixels
  1969. (ignore-errors ; Emacs could be in batch mode
  1970. (clear-image-cache)
  1971. (image-size (create-image file) 'pixels))))
  1972. (funcall --size-in-cms size-in-pixels dpi))
  1973. ;; Use hard-coded values.
  1974. (cdr (assoc-string anchor-type
  1975. org-odt-default-image-sizes-alist))
  1976. ;; Error out.
  1977. (error "Cannot determine image size, aborting"))))
  1978. (width (car size)) (height (cdr size)))
  1979. (cond
  1980. (scale
  1981. (setq width (* width scale) height (* height scale)))
  1982. ((and user-height user-width)
  1983. (setq width user-width height user-height))
  1984. (user-height
  1985. (setq width (* user-height (/ width height)) height user-height))
  1986. (user-width
  1987. (setq height (* user-width (/ height width)) width user-width))
  1988. (t (ignore)))
  1989. ;; ensure that an embedded image fits comfortably within a page
  1990. (let ((max-width (car org-odt-max-image-size))
  1991. (max-height (cdr org-odt-max-image-size)))
  1992. (when (or (> width max-width) (> height max-height))
  1993. (let* ((scale1 (/ max-width width))
  1994. (scale2 (/ max-height height))
  1995. (scale (min scale1 scale2)))
  1996. (setq width (* scale width) height (* scale height)))))
  1997. (cons width height)))
  1998. (defun org-odt-link--inline-image (element info)
  1999. "Return ODT code for an inline image.
  2000. LINK is the link pointing to the inline image. INFO is a plist
  2001. used as a communication channel."
  2002. (assert (eq (org-element-type element) 'link))
  2003. (let* ((src (let* ((type (org-element-property :type element))
  2004. (raw-path (org-element-property :path element)))
  2005. (cond ((member type '("http" "https"))
  2006. (concat type ":" raw-path))
  2007. ((file-name-absolute-p raw-path)
  2008. (expand-file-name raw-path))
  2009. (t raw-path))))
  2010. (src-expanded (if (file-name-absolute-p src) src
  2011. (expand-file-name src (file-name-directory
  2012. (plist-get info :input-file)))))
  2013. (href (format
  2014. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  2015. (org-odt--copy-image-file src-expanded)))
  2016. ;; Extract attributes from #+ATTR_ODT line.
  2017. (attr-from (case (org-element-type element)
  2018. (link (org-export-get-parent-element element))
  2019. (t element)))
  2020. ;; Convert attributes to a plist.
  2021. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  2022. ;; Handle `:anchor', `:style' and `:attributes' properties.
  2023. (user-frame-anchor
  2024. (car (assoc-string (plist-get attr-plist :anchor)
  2025. '(("as-char") ("paragraph") ("page")) t)))
  2026. (user-frame-style
  2027. (and user-frame-anchor (plist-get attr-plist :style)))
  2028. (user-frame-attrs
  2029. (and user-frame-anchor (plist-get attr-plist :attributes)))
  2030. (user-frame-params
  2031. (list user-frame-style user-frame-attrs user-frame-anchor))
  2032. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  2033. ;;
  2034. ;; Handle `:width', `:height' and `:scale' properties. Read
  2035. ;; them as numbers since we need them for computations.
  2036. (size (org-odt--image-size
  2037. src-expanded info
  2038. (let ((width (plist-get attr-plist :width)))
  2039. (and width (read width)))
  2040. (let ((length (plist-get attr-plist :length)))
  2041. (and length (read length)))
  2042. (let ((scale (plist-get attr-plist :scale)))
  2043. (and scale (read scale)))
  2044. nil ; embed-as
  2045. "paragraph" ; FIXME
  2046. ))
  2047. (width (car size)) (height (cdr size))
  2048. (standalone-link-p (org-odt--standalone-link-p element info))
  2049. (embed-as (if standalone-link-p "paragraph" "as-char"))
  2050. (captions (org-odt-format-label element info 'definition))
  2051. (caption (car captions)) (short-caption (cdr captions))
  2052. (entity (concat (and caption "Captioned") embed-as "Image"))
  2053. ;; Check if this link was created by LaTeX-to-PNG converter.
  2054. (replaces (org-element-property
  2055. :replaces (if (not standalone-link-p) element
  2056. (org-export-get-parent-element element))))
  2057. ;; If yes, note down the type of the element - LaTeX Fragment
  2058. ;; or LaTeX environment. It will go in to frame title.
  2059. (title (and replaces (capitalize
  2060. (symbol-name (org-element-type replaces)))))
  2061. ;; If yes, note down its contents. It will go in to frame
  2062. ;; description. This quite useful for debugging.
  2063. (desc (and replaces (org-element-property :value replaces))))
  2064. (org-odt--render-image/formula entity href width height
  2065. captions user-frame-params title desc)))
  2066. ;;;; Links :: Math formula
  2067. (defun org-odt-link--inline-formula (element info)
  2068. (let* ((src (let* ((type (org-element-property :type element))
  2069. (raw-path (org-element-property :path element)))
  2070. (cond
  2071. ((file-name-absolute-p raw-path)
  2072. (expand-file-name raw-path))
  2073. (t raw-path))))
  2074. (src-expanded (if (file-name-absolute-p src) src
  2075. (expand-file-name src (file-name-directory
  2076. (plist-get info :input-file)))))
  2077. (href
  2078. (format
  2079. "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  2080. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  2081. (file-name-directory (org-odt--copy-formula-file src-expanded))))
  2082. (standalone-link-p (org-odt--standalone-link-p element info))
  2083. (embed-as (if standalone-link-p 'paragraph 'character))
  2084. (captions (org-odt-format-label element info 'definition))
  2085. (caption (car captions)) (short-caption (cdr captions))
  2086. ;; Check if this link was created by LaTeX-to-MathML
  2087. ;; converter.
  2088. (replaces (org-element-property
  2089. :replaces (if (not standalone-link-p) element
  2090. (org-export-get-parent-element element))))
  2091. ;; If yes, note down the type of the element - LaTeX Fragment
  2092. ;; or LaTeX environment. It will go in to frame title.
  2093. (title (and replaces (capitalize
  2094. (symbol-name (org-element-type replaces)))))
  2095. ;; If yes, note down its contents. It will go in to frame
  2096. ;; description. This quite useful for debugging.
  2097. (desc (and replaces (org-element-property :value replaces)))
  2098. width height)
  2099. (cond
  2100. ((eq embed-as 'character)
  2101. (org-odt--render-image/formula "InlineFormula" href width height
  2102. nil nil title desc))
  2103. (t
  2104. (let* ((equation (org-odt--render-image/formula
  2105. "CaptionedDisplayFormula" href width height
  2106. captions nil title desc))
  2107. (label
  2108. (let* ((org-odt-category-map-alist
  2109. '(("__MathFormula__" "Text" "math-label" "Equation"
  2110. org-odt--enumerable-formula-p))))
  2111. (car (org-odt-format-label element info 'definition)))))
  2112. (concat equation "<text:tab/>" label))))))
  2113. (defun org-odt--copy-formula-file (src-file)
  2114. "Returns the internal name of the file"
  2115. (let* ((target-dir (format "Formula-%04d/"
  2116. (incf org-odt-embedded-formulas-count)))
  2117. (target-file (concat target-dir "content.xml")))
  2118. ;; Create a directory for holding formula file. Also enter it in
  2119. ;; to manifest.
  2120. (make-directory (concat org-odt-zip-dir target-dir))
  2121. (org-odt-create-manifest-file-entry
  2122. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2123. ;; Copy over the formula file from user directory to zip
  2124. ;; directory.
  2125. (message "Embedding %s as %s..." src-file target-file)
  2126. (let ((case-fold-search nil))
  2127. (cond
  2128. ;; Case 1: Mathml.
  2129. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  2130. (copy-file src-file (concat org-odt-zip-dir target-file) 'overwrite))
  2131. ;; Case 2: OpenDocument formula.
  2132. ((string-match "\\.odf\\'" src-file)
  2133. (org-odt--zip-extract src-file "content.xml"
  2134. (concat org-odt-zip-dir target-dir)))
  2135. (t (error "%s is not a formula file" src-file))))
  2136. ;; Enter the formula file in to manifest.
  2137. (org-odt-create-manifest-file-entry "text/xml" target-file)
  2138. target-file))
  2139. ;;;; Targets
  2140. (defun org-odt--render-image/formula (cfg-key href width height &optional
  2141. captions user-frame-params
  2142. &rest title-and-desc)
  2143. (let* ((frame-cfg-alist
  2144. ;; Each element of this alist is of the form (CFG-HANDLE
  2145. ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
  2146. ;; CFG-HANDLE is the key to the alist.
  2147. ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
  2148. ;; frame params for INNER-FRAME and OUTER-FRAME
  2149. ;; respectively. See below.
  2150. ;; Configurations that are meant to be applied to
  2151. ;; non-captioned image/formula specifies no
  2152. ;; OUTER-FRAME-PARAMS.
  2153. ;; TERMINOLOGY
  2154. ;; ===========
  2155. ;; INNER-FRAME :: Frame that directly surrounds an
  2156. ;; image/formula.
  2157. ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
  2158. ;; frame also contains the caption, if any.
  2159. ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
  2160. ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
  2161. ;; that these are the last three arguments
  2162. ;; to `org-odt--frame'.
  2163. ;; Note that an un-captioned image/formula requires just an
  2164. ;; INNER-FRAME, while a captioned image/formula requires
  2165. ;; both an INNER and an OUTER-FRAME.
  2166. '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
  2167. ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
  2168. ("PageImage" ("OrgPageImage" nil "page"))
  2169. ("CaptionedAs-CharImage"
  2170. ("OrgCaptionedImage"
  2171. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2172. ("OrgInlineImage" nil "as-char"))
  2173. ("CaptionedParagraphImage"
  2174. ("OrgCaptionedImage"
  2175. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2176. ("OrgImageCaptionFrame" nil "paragraph"))
  2177. ("CaptionedPageImage"
  2178. ("OrgCaptionedImage"
  2179. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2180. ("OrgPageImageCaptionFrame" nil "page"))
  2181. ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
  2182. ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
  2183. ("CaptionedDisplayFormula"
  2184. ("OrgCaptionedFormula" nil "paragraph")
  2185. ("OrgFormulaCaptionFrame" nil "paragraph"))))
  2186. (caption (car captions)) (short-caption (cdr captions))
  2187. ;; Retrieve inner and outer frame params, from configuration.
  2188. (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
  2189. (inner (nth 1 frame-cfg))
  2190. (outer (nth 2 frame-cfg))
  2191. ;; User-specified frame params (from #+ATTR_ODT spec)
  2192. (user user-frame-params)
  2193. (--merge-frame-params (function
  2194. (lambda (default user)
  2195. "Merge default and user frame params."
  2196. (if (not user) default
  2197. (assert (= (length default) 3))
  2198. (assert (= (length user) 3))
  2199. (loop for u in user
  2200. for d in default
  2201. collect (or u d)))))))
  2202. (cond
  2203. ;; Case 1: Image/Formula has no caption.
  2204. ;; There is only one frame, one that surrounds the image
  2205. ;; or formula.
  2206. ((not caption)
  2207. ;; Merge user frame params with that from configuration.
  2208. (setq inner (funcall --merge-frame-params inner user))
  2209. (apply 'org-odt--frame href width height
  2210. (append inner title-and-desc)))
  2211. ;; Case 2: Image/Formula is captioned or labeled.
  2212. ;; There are two frames: The inner one surrounds the
  2213. ;; image or formula. The outer one contains the
  2214. ;; caption/sequence number.
  2215. (t
  2216. ;; Merge user frame params with outer frame params.
  2217. (setq outer (funcall --merge-frame-params outer user))
  2218. ;; Short caption, if specified, goes as part of inner frame.
  2219. (setq inner (let ((frame-params (copy-sequence inner)))
  2220. (setcar (cdr frame-params)
  2221. (concat
  2222. (cadr frame-params)
  2223. (when short-caption
  2224. (format " draw:name=\"%s\" " short-caption))))
  2225. frame-params))
  2226. (apply 'org-odt--textbox
  2227. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2228. "Illustration"
  2229. (concat
  2230. (apply 'org-odt--frame href width height
  2231. (append inner title-and-desc))
  2232. caption))
  2233. width height outer)))))
  2234. (defun org-odt--enumerable-p (element info)
  2235. ;; Element should have a caption or label.
  2236. (or (org-element-property :caption element)
  2237. (org-element-property :name element)))
  2238. (defun org-odt--enumerable-image-p (element info)
  2239. (org-odt--standalone-link-p
  2240. element info
  2241. ;; Paragraph should have a caption or label. It SHOULD NOT be a
  2242. ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
  2243. ;; processing.)
  2244. (lambda (p)
  2245. (and (not (org-element-property :replaces p))
  2246. (or (org-element-property :caption p)
  2247. (org-element-property :name p))))
  2248. ;; Link should point to an image file.
  2249. (lambda (l)
  2250. (assert (eq (org-element-type l) 'link))
  2251. (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
  2252. (defun org-odt--enumerable-latex-image-p (element info)
  2253. (org-odt--standalone-link-p
  2254. element info
  2255. ;; Paragraph should have a caption or label. It SHOULD also be a
  2256. ;; replacement element. (i.e., It SHOULD be a result of LaTeX
  2257. ;; processing.)
  2258. (lambda (p)
  2259. (and (org-element-property :replaces p)
  2260. (or (org-element-property :caption p)
  2261. (org-element-property :name p))))
  2262. ;; Link should point to an image file.
  2263. (lambda (l)
  2264. (assert (eq (org-element-type l) 'link))
  2265. (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
  2266. (defun org-odt--enumerable-formula-p (element info)
  2267. (org-odt--standalone-link-p
  2268. element info
  2269. ;; Paragraph should have a caption or label.
  2270. (lambda (p)
  2271. (or (org-element-property :caption p)
  2272. (org-element-property :name p)))
  2273. ;; Link should point to a MathML or ODF file.
  2274. (lambda (l)
  2275. (assert (eq (org-element-type l) 'link))
  2276. (org-export-inline-image-p l (plist-get info :odt-inline-formula-rules)))))
  2277. (defun org-odt--standalone-link-p (element info &optional
  2278. paragraph-predicate
  2279. link-predicate)
  2280. "Test if ELEMENT is a standalone link for the purpose ODT export.
  2281. INFO is a plist holding contextual information.
  2282. Return non-nil, if ELEMENT is of type paragraph satisfying
  2283. PARAGRAPH-PREDICATE and its sole content, save for whitespaces,
  2284. is a link that satisfies LINK-PREDICATE.
  2285. Return non-nil, if ELEMENT is of type link satisfying
  2286. LINK-PREDICATE and its containing paragraph satisfies
  2287. PARAGRAPH-PREDICATE in addition to having no other content save for
  2288. leading and trailing whitespaces.
  2289. Return nil, otherwise."
  2290. (let ((p (case (org-element-type element)
  2291. (paragraph element)
  2292. (link (and (or (not link-predicate)
  2293. (funcall link-predicate element))
  2294. (org-export-get-parent element)))
  2295. (t nil))))
  2296. (when (and p (eq (org-element-type p) 'paragraph))
  2297. (when (or (not paragraph-predicate)
  2298. (funcall paragraph-predicate p))
  2299. (let ((contents (org-element-contents p)))
  2300. (loop for x in contents
  2301. with inline-image-count = 0
  2302. always (case (org-element-type x)
  2303. (plain-text
  2304. (not (org-string-nw-p x)))
  2305. (link
  2306. (and (or (not link-predicate)
  2307. (funcall link-predicate x))
  2308. (= (incf inline-image-count) 1)))
  2309. (t nil))))))))
  2310. (defun org-odt-link--infer-description (destination info)
  2311. ;; DESTINATION is a HEADLINE, a "<<target>>" or an element (like
  2312. ;; paragraph, verse-block etc) to which a "#+NAME: label" can be
  2313. ;; attached. Note that labels that are attached to captioned
  2314. ;; entities - inline images, math formulae and tables - get resolved
  2315. ;; as part of `org-odt-format-label' and `org-odt--enumerate'.
  2316. ;; Create a cross-reference to DESTINATION but make best-efforts to
  2317. ;; create a *meaningful* description. Check item numbers, section
  2318. ;; number and section title in that order.
  2319. ;; NOTE: Counterpart of `org-export-get-ordinal'.
  2320. ;; FIXME: Handle footnote-definition footnote-reference?
  2321. (let* ((genealogy (org-element-lineage destination))
  2322. (data (reverse genealogy))
  2323. (label (case (org-element-type destination)
  2324. (headline (org-export-get-headline-id destination info))
  2325. (target
  2326. (org-element-property :value destination))
  2327. (t (error "FIXME: Resolve %S" destination)))))
  2328. (or
  2329. (let* ( ;; Locate top-level list.
  2330. (top-level-list
  2331. (loop for x on data
  2332. when (eq (org-element-type (car x)) 'plain-list)
  2333. return x))
  2334. ;; Get list item nos.
  2335. (item-numbers
  2336. (loop for (plain-list item . rest) on top-level-list by #'cddr
  2337. until (not (eq (org-element-type plain-list) 'plain-list))
  2338. collect (when (eq (org-element-property :type
  2339. plain-list)
  2340. 'ordered)
  2341. (1+ (length (org-export-get-previous-element
  2342. item info t))))))
  2343. ;; Locate top-most listified headline.
  2344. (listified-headlines
  2345. (loop for x on data
  2346. when (and (eq (org-element-type (car x)) 'headline)
  2347. (org-export-low-level-p (car x) info))
  2348. return x))
  2349. ;; Get listified headline numbers.
  2350. (listified-headline-nos
  2351. (loop for el in listified-headlines
  2352. when (eq (org-element-type el) 'headline)
  2353. collect (when (org-export-numbered-headline-p el info)
  2354. (1+ (length (org-export-get-previous-element
  2355. el info t)))))))
  2356. ;; Combine item numbers from both the listified headlines and
  2357. ;; regular list items.
  2358. ;; Case 1: Check if all the parents of list item are numbered.
  2359. ;; If yes, link to the item proper.
  2360. (let ((item-numbers (append listified-headline-nos item-numbers)))
  2361. (when (and item-numbers (not (memq nil item-numbers)))
  2362. (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  2363. (org-export-solidify-link-text label)
  2364. (mapconcat (lambda (n) (if (not n) " "
  2365. (concat (number-to-string n) ".")))
  2366. item-numbers "")))))
  2367. ;; Case 2: Locate a regular and numbered headline in the
  2368. ;; hierarchy. Display its section number.
  2369. (let ((headline
  2370. (and
  2371. ;; Test if destination is a numbered headline.
  2372. (org-export-numbered-headline-p destination info)
  2373. (loop for el in (cons destination genealogy)
  2374. when (and (eq (org-element-type el) 'headline)
  2375. (not (org-export-low-level-p el info))
  2376. (org-export-numbered-headline-p el info))
  2377. return el))))
  2378. ;; We found one.
  2379. (when headline
  2380. (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2381. (org-export-solidify-link-text label)
  2382. (mapconcat 'number-to-string (org-export-get-headline-number
  2383. headline info) "."))))
  2384. ;; Case 4: Locate a regular headline in the hierarchy. Display
  2385. ;; its title.
  2386. (let ((headline (loop for el in (cons destination genealogy)
  2387. when (and (eq (org-element-type el) 'headline)
  2388. (not (org-export-low-level-p el info)))
  2389. return el)))
  2390. ;; We found one.
  2391. (when headline
  2392. (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2393. (org-export-solidify-link-text label)
  2394. (let ((title (org-element-property :title headline)))
  2395. (org-export-data title info)))))
  2396. (error "FIXME?"))))
  2397. (defun org-odt-link (link desc info)
  2398. "Transcode a LINK object from Org to ODT.
  2399. DESC is the description part of the link, or the empty string.
  2400. INFO is a plist holding contextual information. See
  2401. `org-export-data'."
  2402. (let* ((type (org-element-property :type link))
  2403. (raw-path (org-element-property :path link))
  2404. ;; Ensure DESC really exists, or set it to nil.
  2405. (desc (and (not (string= desc "")) desc))
  2406. (imagep (org-export-inline-image-p
  2407. link (plist-get info :odt-inline-image-rules)))
  2408. (path (cond
  2409. ((member type '("http" "https" "ftp" "mailto"))
  2410. (concat type ":" raw-path))
  2411. ((and (string= type "file") (file-name-absolute-p raw-path))
  2412. (concat "file:" raw-path))
  2413. (t raw-path)))
  2414. ;; Convert & to &amp; for correct XML representation
  2415. (path (replace-regexp-in-string "&" "&amp;" path))
  2416. protocol)
  2417. (cond
  2418. ;; Image file.
  2419. ((and (not desc) (org-export-inline-image-p
  2420. link (plist-get info :odt-inline-image-rules)))
  2421. (org-odt-link--inline-image link info))
  2422. ;; Formula file.
  2423. ((and (not desc) (org-export-inline-image-p
  2424. link (plist-get info :odt-inline-formula-rules)))
  2425. (org-odt-link--inline-formula link info))
  2426. ;; Radio target: Transcode target's contents and use them as
  2427. ;; link's description.
  2428. ((string= type "radio")
  2429. (let ((destination (org-export-resolve-radio-link link info)))
  2430. (if (not destination) desc
  2431. (format
  2432. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2433. (org-export-solidify-link-text
  2434. (org-element-property :value destination))
  2435. desc))))
  2436. ;; Links pointing to a headline: Find destination and build
  2437. ;; appropriate referencing command.
  2438. ((member type '("custom-id" "fuzzy" "id"))
  2439. (let ((destination (if (string= type "fuzzy")
  2440. (org-export-resolve-fuzzy-link link info)
  2441. (org-export-resolve-id-link link info))))
  2442. (case (org-element-type destination)
  2443. ;; Case 1: Fuzzy link points nowhere.
  2444. ('nil
  2445. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2446. "Emphasis"
  2447. (or desc
  2448. (org-export-data (org-element-property :raw-link link)
  2449. info))))
  2450. ;; Case 2: Fuzzy link points to a headline.
  2451. (headline
  2452. ;; If there's a description, create a hyperlink.
  2453. ;; Otherwise, try to provide a meaningful description.
  2454. (if (not desc) (org-odt-link--infer-description destination info)
  2455. (let ((label (or (and (string= type "custom-id")
  2456. (org-element-property :CUSTOM_ID destination))
  2457. (org-export-get-headline-id destination info))))
  2458. (format
  2459. "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2460. label desc))))
  2461. ;; Case 3: Fuzzy link points to a target.
  2462. (target
  2463. ;; If there's a description, create a hyperlink.
  2464. ;; Otherwise, try to provide a meaningful description.
  2465. (if (not desc) (org-odt-link--infer-description destination info)
  2466. (let ((label (org-element-property :value destination)))
  2467. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2468. (org-export-solidify-link-text label)
  2469. desc))))
  2470. ;; Case 4: Fuzzy link points to some element (e.g., an
  2471. ;; inline image, a math formula or a table).
  2472. (otherwise
  2473. (let ((label-reference
  2474. (ignore-errors (org-odt-format-label
  2475. destination info 'reference))))
  2476. (cond ((not label-reference)
  2477. (org-odt-link--infer-description destination info))
  2478. ;; LINK has no description. Create
  2479. ;; a cross-reference showing entity's sequence
  2480. ;; number.
  2481. ((not desc) label-reference)
  2482. ;; LINK has description. Insert a hyperlink with
  2483. ;; user-provided description.
  2484. (t
  2485. (let ((label (org-element-property :name destination)))
  2486. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2487. (org-export-solidify-link-text label)
  2488. desc)))))))))
  2489. ;; Coderef: replace link with the reference name or the
  2490. ;; equivalent line number.
  2491. ((string= type "coderef")
  2492. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2493. (href (concat "coderef-" path)))
  2494. (format
  2495. (org-export-get-coderef-format path desc)
  2496. (format
  2497. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2498. href line-no))))
  2499. ;; Link type is handled by a special function.
  2500. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2501. (funcall protocol (org-link-unescape path) desc 'odt))
  2502. ;; External link with a description part.
  2503. ((and path desc)
  2504. (let ((link-contents (org-element-contents link)))
  2505. ;; Check if description is a link to an inline image.
  2506. (if (and (not (cdr link-contents))
  2507. (let ((desc-element (car link-contents)))
  2508. (and (eq (org-element-type desc-element) 'link)
  2509. (org-export-inline-image-p
  2510. desc-element
  2511. (plist-get info :odt-inline-image-rules)))))
  2512. ;; Format link as a clickable image.
  2513. (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  2514. path desc)
  2515. ;; Otherwise, format it as a regular link.
  2516. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2517. path desc))))
  2518. ;; External link without a description part.
  2519. (path
  2520. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2521. path path))
  2522. ;; No path, only description. Try to do something useful.
  2523. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2524. "Emphasis" desc)))))
  2525. ;;;; Node Property
  2526. (defun org-odt-node-property (node-property contents info)
  2527. "Transcode a NODE-PROPERTY element from Org to ODT.
  2528. CONTENTS is nil. INFO is a plist holding contextual
  2529. information."
  2530. (org-odt--encode-plain-text
  2531. (format "%s:%s"
  2532. (org-element-property :key node-property)
  2533. (let ((value (org-element-property :value node-property)))
  2534. (if value (concat " " value) "")))))
  2535. ;;;; Paragraph
  2536. (defun org-odt--paragraph-style (paragraph)
  2537. "Return style of PARAGRAPH.
  2538. Style is a symbol among `quoted', `centered' and nil."
  2539. (let ((up paragraph))
  2540. (while (and (setq up (org-element-property :parent up))
  2541. (not (memq (org-element-type up)
  2542. '(center-block quote-block section)))))
  2543. (case (org-element-type up)
  2544. (center-block 'centered)
  2545. (quote-block 'quoted))))
  2546. (defun org-odt--format-paragraph (paragraph contents info default center quote)
  2547. "Format paragraph according to given styles.
  2548. PARAGRAPH is a paragraph type element. CONTENTS is the
  2549. transcoded contents of that paragraph, as a string. INFO is
  2550. a plist used as a communication channel. DEFAULT, CENTER and
  2551. QUOTE are, respectively, style to use when paragraph belongs to
  2552. no special environment, a center block, or a quote block."
  2553. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2554. (case (org-odt--paragraph-style paragraph)
  2555. (quoted quote)
  2556. (centered center)
  2557. (otherwise default))
  2558. ;; If PARAGRAPH is a leading paragraph in an item that has
  2559. ;; a checkbox, splice checkbox and paragraph contents
  2560. ;; together.
  2561. (concat (let ((parent (org-element-property :parent paragraph)))
  2562. (and (eq (org-element-type parent) 'item)
  2563. (not (org-export-get-previous-element paragraph info))
  2564. (org-odt--checkbox parent)))
  2565. contents)))
  2566. (defun org-odt-paragraph (paragraph contents info)
  2567. "Transcode a PARAGRAPH element from Org to ODT.
  2568. CONTENTS is the contents of the paragraph, as a string. INFO is
  2569. the plist used as a communication channel."
  2570. (org-odt--format-paragraph
  2571. paragraph contents info
  2572. (or (org-element-property :style paragraph) "Text_20_body")
  2573. "OrgCenter"
  2574. "Quotations"))
  2575. ;;;; Plain List
  2576. (defun org-odt-plain-list (plain-list contents info)
  2577. "Transcode a PLAIN-LIST element from Org to ODT.
  2578. CONTENTS is the contents of the list. INFO is a plist holding
  2579. contextual information."
  2580. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2581. ;; Choose style based on list type.
  2582. (case (org-element-property :type plain-list)
  2583. (ordered "OrgNumberedList")
  2584. (unordered "OrgBulletedList")
  2585. (descriptive-1 "OrgDescriptionList")
  2586. (descriptive-2 "OrgDescriptionList"))
  2587. ;; If top-level list, re-start numbering. Otherwise,
  2588. ;; continue numbering.
  2589. (format "text:continue-numbering=\"%s\""
  2590. (let* ((parent (org-export-get-parent plain-list)))
  2591. (if (and parent (eq (org-element-type parent) 'item))
  2592. "true" "false")))
  2593. contents))
  2594. ;;;; Plain Text
  2595. (defun org-odt--encode-tabs-and-spaces (line)
  2596. (replace-regexp-in-string
  2597. "\\([\t]\\|\\([ ]+\\)\\)"
  2598. (lambda (s)
  2599. (cond
  2600. ((string= s "\t") "<text:tab/>")
  2601. (t (let ((n (length s)))
  2602. (cond
  2603. ((= n 1) " ")
  2604. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2605. (t ""))))))
  2606. line))
  2607. (defun org-odt--encode-plain-text (text &optional no-whitespace-filling)
  2608. (mapc
  2609. (lambda (pair)
  2610. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2611. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2612. (if no-whitespace-filling text
  2613. (org-odt--encode-tabs-and-spaces text)))
  2614. (defun org-odt-plain-text (text info)
  2615. "Transcode a TEXT string from Org to ODT.
  2616. TEXT is the string to transcode. INFO is a plist holding
  2617. contextual information."
  2618. (let ((output text))
  2619. ;; Protect &, < and >.
  2620. (setq output (org-odt--encode-plain-text output t))
  2621. ;; Handle smart quotes. Be sure to provide original string since
  2622. ;; OUTPUT may have been modified.
  2623. (when (plist-get info :with-smart-quotes)
  2624. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  2625. ;; Convert special strings.
  2626. (when (plist-get info :with-special-strings)
  2627. (mapc
  2628. (lambda (pair)
  2629. (setq output
  2630. (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
  2631. org-odt-special-string-regexps))
  2632. ;; Handle break preservation if required.
  2633. (when (plist-get info :preserve-breaks)
  2634. (setq output (replace-regexp-in-string
  2635. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>" output t)))
  2636. ;; Return value.
  2637. output))
  2638. ;;;; Planning
  2639. (defun org-odt-planning (planning contents info)
  2640. "Transcode a PLANNING element from Org to ODT.
  2641. CONTENTS is nil. INFO is a plist used as a communication
  2642. channel."
  2643. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2644. "OrgPlanning"
  2645. (concat
  2646. (let ((closed (org-element-property :closed planning)))
  2647. (when closed
  2648. (concat
  2649. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2650. "OrgClosedKeyword" org-closed-string)
  2651. (org-odt-timestamp closed contents info))))
  2652. (let ((deadline (org-element-property :deadline planning)))
  2653. (when deadline
  2654. (concat
  2655. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2656. "OrgDeadlineKeyword" org-deadline-string)
  2657. (org-odt-timestamp deadline contents info))))
  2658. (let ((scheduled (org-element-property :scheduled planning)))
  2659. (when scheduled
  2660. (concat
  2661. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2662. "OrgScheduledKeyword" org-deadline-string)
  2663. (org-odt-timestamp scheduled contents info)))))))
  2664. ;;;; Property Drawer
  2665. (defun org-odt-property-drawer (property-drawer contents info)
  2666. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2667. CONTENTS holds the contents of the drawer. INFO is a plist
  2668. holding contextual information."
  2669. (and (org-string-nw-p contents)
  2670. (format "<text:p text:style-name=\"OrgFixedWidthBlock\">%s</text:p>"
  2671. contents)))
  2672. ;;;; Quote Block
  2673. (defun org-odt-quote-block (quote-block contents info)
  2674. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2675. CONTENTS holds the contents of the block. INFO is a plist
  2676. holding contextual information."
  2677. contents)
  2678. ;;;; Section
  2679. (defun org-odt-format-section (text style &optional name)
  2680. (let ((default-name (car (org-odt-add-automatic-style "Section"))))
  2681. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2682. style
  2683. (format "text:name=\"%s\"" (or name default-name))
  2684. text)))
  2685. (defun org-odt-section (section contents info) ; FIXME
  2686. "Transcode a SECTION element from Org to ODT.
  2687. CONTENTS holds the contents of the section. INFO is a plist
  2688. holding contextual information."
  2689. contents)
  2690. ;;;; Radio Target
  2691. (defun org-odt-radio-target (radio-target text info)
  2692. "Transcode a RADIO-TARGET object from Org to ODT.
  2693. TEXT is the text of the target. INFO is a plist holding
  2694. contextual information."
  2695. (org-odt--target
  2696. text (org-export-solidify-link-text
  2697. (org-element-property :value radio-target))))
  2698. ;;;; Special Block
  2699. (defun org-odt-special-block (special-block contents info)
  2700. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2701. CONTENTS holds the contents of the block. INFO is a plist
  2702. holding contextual information."
  2703. (let ((type (org-element-property :type special-block))
  2704. (attributes (org-export-read-attribute :attr_odt special-block)))
  2705. (cond
  2706. ;; Annotation.
  2707. ((string= type "annotation")
  2708. (let* ((author (or (plist-get attributes :author)
  2709. (let ((author (plist-get info :author)))
  2710. (and author (org-export-data author info)))))
  2711. (date (or (plist-get attributes :date)
  2712. ;; FIXME: Is `car' right thing to do below?
  2713. (car (plist-get info :date)))))
  2714. (format "\n<text:p>%s</text:p>"
  2715. (format "<office:annotation>\n%s\n</office:annotation>"
  2716. (concat
  2717. (and author
  2718. (format "<dc:creator>%s</dc:creator>" author))
  2719. (and date
  2720. (format "<dc:date>%s</dc:date>"
  2721. (org-odt--format-timestamp date nil 'iso-date)))
  2722. contents)))))
  2723. ;; Textbox.
  2724. ((string= type "textbox")
  2725. (let ((width (plist-get attributes :width))
  2726. (height (plist-get attributes :height))
  2727. (style (plist-get attributes :style))
  2728. (extra (plist-get attributes :extra))
  2729. (anchor (plist-get attributes :anchor)))
  2730. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2731. "Text_20_body" (org-odt--textbox contents width height
  2732. style extra anchor))))
  2733. (t contents))))
  2734. ;;;; Src Block
  2735. (defun org-odt-hfy-face-to-css (fn)
  2736. "Create custom style for face FN.
  2737. When FN is the default face, use its foreground and background
  2738. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2739. use its color attribute to create a character style whose name
  2740. is obtained from FN. Currently all attributes of FN other than
  2741. color are ignored.
  2742. The style name for a face FN is derived using the following
  2743. operations on the face name in that order - de-dash, CamelCase
  2744. and prefix with \"OrgSrc\". For example,
  2745. `font-lock-function-name-face' is associated with
  2746. \"OrgSrcFontLockFunctionNameFace\"."
  2747. (let* ((css-list (hfy-face-to-style fn))
  2748. (style-name (concat "OrgSrc"
  2749. (mapconcat
  2750. 'capitalize (split-string
  2751. (hfy-face-or-def-to-name fn) "-")
  2752. "")))
  2753. (color-val (cdr (assoc "color" css-list)))
  2754. (background-color-val (cdr (assoc "background" css-list)))
  2755. (style (and org-odt-create-custom-styles-for-srcblocks
  2756. (cond
  2757. ((eq fn 'default)
  2758. (format org-odt-src-block-paragraph-format
  2759. background-color-val color-val))
  2760. (t
  2761. (format
  2762. "
  2763. <style:style style:name=\"%s\" style:family=\"text\">
  2764. <style:text-properties fo:color=\"%s\"/>
  2765. </style:style>" style-name color-val))))))
  2766. (cons style-name style)))
  2767. (defun org-odt-htmlfontify-string (line)
  2768. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2769. (hfy-html-quote-map '(("\"" "&quot;")
  2770. ("<" "&lt;")
  2771. ("&" "&amp;")
  2772. (">" "&gt;")
  2773. (" " "<text:s/>")
  2774. (" " "<text:tab/>")))
  2775. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  2776. (hfy-optimisations-1 (copy-sequence hfy-optimisations))
  2777. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2778. 'body-text-only))
  2779. (hfy-begin-span-handler
  2780. (lambda (style text-block text-id text-begins-block-p)
  2781. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2782. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2783. (org-no-warnings (htmlfontify-string line))))
  2784. (defun org-odt-do-format-code
  2785. (code info &optional lang refs retain-labels num-start)
  2786. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2787. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2788. (code-lines (org-split-string code "\n"))
  2789. (code-length (length code-lines))
  2790. (use-htmlfontify-p (and (functionp lang-mode)
  2791. (plist-get info :odt-fontify-srcblocks)
  2792. (require 'htmlfontify nil t)
  2793. (fboundp 'htmlfontify-string)))
  2794. (code (if (not use-htmlfontify-p) code
  2795. (with-temp-buffer
  2796. (insert code)
  2797. (funcall lang-mode)
  2798. (font-lock-ensure)
  2799. (buffer-string))))
  2800. (fontifier (if use-htmlfontify-p 'org-odt-htmlfontify-string
  2801. 'org-odt--encode-plain-text))
  2802. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2803. "OrgFixedWidthBlock"))
  2804. (i 0))
  2805. (assert (= code-length (length (org-split-string code "\n"))))
  2806. (setq code
  2807. (org-export-format-code
  2808. code
  2809. (lambda (loc line-num ref)
  2810. (setq par-style
  2811. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2812. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2813. (setq loc (funcall fontifier loc))
  2814. (when ref
  2815. (setq loc (org-odt--target loc (concat "coderef-" ref))))
  2816. (assert par-style)
  2817. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2818. par-style loc))
  2819. (if (not line-num) loc
  2820. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2821. num-start refs))
  2822. (cond
  2823. ((not num-start) code)
  2824. ((= num-start 0)
  2825. (format
  2826. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2827. " text:continue-numbering=\"false\"" code))
  2828. (t
  2829. (format
  2830. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2831. " text:continue-numbering=\"true\"" code)))))
  2832. (defun org-odt-format-code (element info)
  2833. (let* ((lang (org-element-property :language element))
  2834. ;; Extract code and references.
  2835. (code-info (org-export-unravel-code element))
  2836. (code (car code-info))
  2837. (refs (cdr code-info))
  2838. ;; Does the src block contain labels?
  2839. (retain-labels (org-element-property :retain-labels element))
  2840. ;; Does it have line numbers?
  2841. (num-start (case (org-element-property :number-lines element)
  2842. (continued (org-export-get-loc element info))
  2843. (new 0))))
  2844. (org-odt-do-format-code code info lang refs retain-labels num-start)))
  2845. (defun org-odt-src-block (src-block contents info)
  2846. "Transcode a SRC-BLOCK element from Org to ODT.
  2847. CONTENTS holds the contents of the item. INFO is a plist holding
  2848. contextual information."
  2849. (let* ((lang (org-element-property :language src-block))
  2850. (attributes (org-export-read-attribute :attr_odt src-block))
  2851. (captions (org-odt-format-label src-block info 'definition))
  2852. (caption (car captions)) (short-caption (cdr captions)))
  2853. (concat
  2854. (and caption
  2855. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2856. "Listing" caption))
  2857. (let ((--src-block (org-odt-format-code src-block info)))
  2858. (if (not (plist-get attributes :textbox)) --src-block
  2859. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2860. "Text_20_body"
  2861. (org-odt--textbox --src-block nil nil nil)))))))
  2862. ;;;; Statistics Cookie
  2863. (defun org-odt-statistics-cookie (statistics-cookie contents info)
  2864. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2865. CONTENTS is nil. INFO is a plist holding contextual information."
  2866. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2867. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2868. "OrgCode" cookie-value)))
  2869. ;;;; Strike-Through
  2870. (defun org-odt-strike-through (strike-through contents info)
  2871. "Transcode STRIKE-THROUGH from Org to ODT.
  2872. CONTENTS is the text with strike-through markup. INFO is a plist
  2873. holding contextual information."
  2874. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2875. "Strikethrough" contents))
  2876. ;;;; Subscript
  2877. (defun org-odt-subscript (subscript contents info)
  2878. "Transcode a SUBSCRIPT object from Org to ODT.
  2879. CONTENTS is the contents of the object. INFO is a plist holding
  2880. contextual information."
  2881. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2882. "OrgSubscript" contents))
  2883. ;;;; Superscript
  2884. (defun org-odt-superscript (superscript contents info)
  2885. "Transcode a SUPERSCRIPT object from Org to ODT.
  2886. CONTENTS is the contents of the object. INFO is a plist holding
  2887. contextual information."
  2888. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2889. "OrgSuperscript" contents))
  2890. ;;;; Table Cell
  2891. (defun org-odt-table-style-spec (element info)
  2892. (let* ((table (org-export-get-parent-table element))
  2893. (table-attributes (org-export-read-attribute :attr_odt table))
  2894. (table-style (plist-get table-attributes :style)))
  2895. (assoc table-style (plist-get info :odt-table-styles))))
  2896. (defun org-odt-get-table-cell-styles (table-cell info)
  2897. "Retrieve styles applicable to a table cell.
  2898. R and C are (zero-based) row and column numbers of the table
  2899. cell. STYLE-SPEC is an entry in `org-odt-table-styles'
  2900. applicable to the current table. It is nil if the table is not
  2901. associated with any style attributes.
  2902. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2903. When STYLE-SPEC is nil, style the table cell the conventional way
  2904. - choose cell borders based on row and column groupings and
  2905. choose paragraph alignment based on `org-col-cookies' text
  2906. property. See also
  2907. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  2908. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2909. styles congruent with the ODF-1.2 specification."
  2910. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2911. (r (car table-cell-address)) (c (cdr table-cell-address))
  2912. (style-spec (org-odt-table-style-spec table-cell info))
  2913. (table-dimensions (org-export-table-dimensions
  2914. (org-export-get-parent-table table-cell)
  2915. info)))
  2916. (when style-spec
  2917. ;; LibreOffice - particularly the Writer - honors neither table
  2918. ;; templates nor custom table-cell styles. Inorder to retain
  2919. ;; inter-operability with LibreOffice, only automatic styles are
  2920. ;; used for styling of table-cells. The current implementation is
  2921. ;; congruent with ODF-1.2 specification and hence is
  2922. ;; future-compatible.
  2923. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2924. ;; which recognizes as many as 16 different cell types - is much
  2925. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2926. ;; by hand.
  2927. (let* ((template-name (nth 1 style-spec))
  2928. (cell-style-selectors (nth 2 style-spec))
  2929. (cell-type
  2930. (cond
  2931. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2932. (= c 0)) "FirstColumn")
  2933. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2934. (= (1+ c) (cdr table-dimensions)))
  2935. "LastColumn")
  2936. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2937. (= r 0)) "FirstRow")
  2938. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2939. (= (1+ r) (car table-dimensions)))
  2940. "LastRow")
  2941. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2942. (= (% r 2) 1)) "EvenRow")
  2943. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2944. (= (% r 2) 0)) "OddRow")
  2945. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2946. (= (% c 2) 1)) "EvenColumn")
  2947. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2948. (= (% c 2) 0)) "OddColumn")
  2949. (t ""))))
  2950. (concat template-name cell-type)))))
  2951. (defun org-odt-table-cell (table-cell contents info)
  2952. "Transcode a TABLE-CELL element from Org to ODT.
  2953. CONTENTS is nil. INFO is a plist used as a communication
  2954. channel."
  2955. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2956. (r (car table-cell-address))
  2957. (c (cdr table-cell-address))
  2958. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2959. (table-row (org-export-get-parent table-cell))
  2960. (custom-style-prefix (org-odt-get-table-cell-styles
  2961. table-cell info))
  2962. (paragraph-style
  2963. (or
  2964. (and custom-style-prefix
  2965. (format "%sTableParagraph" custom-style-prefix))
  2966. (concat
  2967. (cond
  2968. ((and (= 1 (org-export-table-row-group table-row info))
  2969. (org-export-table-has-header-p
  2970. (org-export-get-parent-table table-row) info))
  2971. "OrgTableHeading")
  2972. ((let* ((table (org-export-get-parent-table table-cell))
  2973. (table-attrs (org-export-read-attribute :attr_odt table))
  2974. (table-header-columns
  2975. (let ((cols (plist-get table-attrs :header-columns)))
  2976. (and cols (read cols)))))
  2977. (<= c (cond ((wholenump table-header-columns)
  2978. (- table-header-columns 1))
  2979. (table-header-columns 0)
  2980. (t -1))))
  2981. "OrgTableHeading")
  2982. (t "OrgTableContents"))
  2983. (capitalize (symbol-name (org-export-table-cell-alignment
  2984. table-cell info))))))
  2985. (cell-style-name
  2986. (or
  2987. (and custom-style-prefix (format "%sTableCell"
  2988. custom-style-prefix))
  2989. (concat
  2990. "OrgTblCell"
  2991. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2992. (zerop r)) "T")
  2993. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2994. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2995. (not (zerop c)) ) "L"))))
  2996. (cell-attributes
  2997. (concat
  2998. (format " table:style-name=\"%s\"" cell-style-name)
  2999. (and (> horiz-span 0)
  3000. (format " table:number-columns-spanned=\"%d\""
  3001. (1+ horiz-span))))))
  3002. (unless contents (setq contents ""))
  3003. (concat
  3004. (assert paragraph-style)
  3005. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  3006. cell-attributes
  3007. (let ((table-cell-contents (org-element-contents table-cell)))
  3008. (if (memq (org-element-type (car table-cell-contents))
  3009. org-element-all-elements)
  3010. contents
  3011. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3012. paragraph-style contents))))
  3013. (let (s)
  3014. (dotimes (i horiz-span s)
  3015. (setq s (concat s "\n<table:covered-table-cell/>"))))
  3016. "\n")))
  3017. ;;;; Table Row
  3018. (defun org-odt-table-row (table-row contents info)
  3019. "Transcode a TABLE-ROW element from Org to ODT.
  3020. CONTENTS is the contents of the row. INFO is a plist used as a
  3021. communication channel."
  3022. ;; Rules are ignored since table separators are deduced from
  3023. ;; borders of the current row.
  3024. (when (eq (org-element-property :type table-row) 'standard)
  3025. (let* ((rowgroup-tags
  3026. (if (and (= 1 (org-export-table-row-group table-row info))
  3027. (org-export-table-has-header-p
  3028. (org-export-get-parent-table table-row) info))
  3029. ;; If the row belongs to the first rowgroup and the
  3030. ;; table has more than one row groups, then this row
  3031. ;; belongs to the header row group.
  3032. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  3033. ;; Otherwise, it belongs to non-header row group.
  3034. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  3035. (concat
  3036. ;; Does this row begin a rowgroup?
  3037. (when (org-export-table-row-starts-rowgroup-p table-row info)
  3038. (car rowgroup-tags))
  3039. ;; Actual table row
  3040. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  3041. ;; Does this row end a rowgroup?
  3042. (when (org-export-table-row-ends-rowgroup-p table-row info)
  3043. (cdr rowgroup-tags))))))
  3044. ;;;; Table
  3045. (defun org-odt-table-first-row-data-cells (table info)
  3046. (let ((table-row
  3047. (org-element-map table 'table-row
  3048. (lambda (row)
  3049. (unless (eq (org-element-property :type row) 'rule) row))
  3050. info 'first-match))
  3051. (special-column-p (org-export-table-has-special-column-p table)))
  3052. (if (not special-column-p) (org-element-contents table-row)
  3053. (cdr (org-element-contents table-row)))))
  3054. (defun org-odt--table (table contents info)
  3055. "Transcode a TABLE element from Org to ODT.
  3056. CONTENTS is the contents of the table. INFO is a plist holding
  3057. contextual information."
  3058. (case (org-element-property :type table)
  3059. ;; Case 1: table.el doesn't support export to OD format. Strip
  3060. ;; such tables from export.
  3061. (table.el
  3062. (prog1 nil
  3063. (message
  3064. (concat
  3065. "(ox-odt): Found table.el-type table in the source Org file."
  3066. " table.el doesn't support export to ODT format."
  3067. " Stripping the table from export."))))
  3068. ;; Case 2: Native Org tables.
  3069. (otherwise
  3070. (let* ((captions (org-odt-format-label table info 'definition))
  3071. (caption (car captions)) (short-caption (cdr captions))
  3072. (attributes (org-export-read-attribute :attr_odt table))
  3073. (custom-table-style (nth 1 (org-odt-table-style-spec table info)))
  3074. (table-column-specs
  3075. (function
  3076. (lambda (table info)
  3077. (let* ((table-style (or custom-table-style "OrgTable"))
  3078. (column-style (format "%sColumn" table-style)))
  3079. (mapconcat
  3080. (lambda (table-cell)
  3081. (let ((width (1+ (or (org-export-table-cell-width
  3082. table-cell info) 0)))
  3083. (s (format
  3084. "\n<table:table-column table:style-name=\"%s\"/>"
  3085. column-style))
  3086. out)
  3087. (dotimes (i width out) (setq out (concat s out)))))
  3088. (org-odt-table-first-row-data-cells table info) "\n"))))))
  3089. (concat
  3090. ;; caption.
  3091. (when caption
  3092. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3093. "Table" caption))
  3094. ;; begin table.
  3095. (let* ((automatic-name
  3096. (org-odt-add-automatic-style "Table" attributes)))
  3097. (format
  3098. "\n<table:table table:style-name=\"%s\"%s>"
  3099. (or custom-table-style (cdr automatic-name) "OrgTable")
  3100. (concat (when short-caption
  3101. (format " table:name=\"%s\"" short-caption)))))
  3102. ;; column specification.
  3103. (funcall table-column-specs table info)
  3104. ;; actual contents.
  3105. "\n" contents
  3106. ;; end table.
  3107. "</table:table>")))))
  3108. (defun org-odt-table (table contents info)
  3109. "Transcode a TABLE element from Org to ODT.
  3110. CONTENTS is the contents of the table. INFO is a plist holding
  3111. contextual information.
  3112. Use `org-odt--table' to typeset the table. Handle details
  3113. pertaining to indentation here."
  3114. (let* ((--element-preceded-by-table-p
  3115. (function
  3116. (lambda (element info)
  3117. (loop for el in (org-export-get-previous-element element info t)
  3118. thereis (eq (org-element-type el) 'table)))))
  3119. (--walk-list-genealogy-and-collect-tags
  3120. (function
  3121. (lambda (table info)
  3122. (let* ((genealogy (org-element-lineage table))
  3123. (list-genealogy
  3124. (when (eq (org-element-type (car genealogy)) 'item)
  3125. (loop for el in genealogy
  3126. when (memq (org-element-type el)
  3127. '(item plain-list))
  3128. collect el)))
  3129. (llh-genealogy
  3130. (apply 'nconc
  3131. (loop for el in genealogy
  3132. when (and (eq (org-element-type el) 'headline)
  3133. (org-export-low-level-p el info))
  3134. collect
  3135. (list el
  3136. (assq 'headline
  3137. (org-element-contents
  3138. (org-export-get-parent el)))))))
  3139. parent-list)
  3140. (nconc
  3141. ;; Handle list genealogy.
  3142. (loop for el in list-genealogy collect
  3143. (case (org-element-type el)
  3144. (plain-list
  3145. (setq parent-list el)
  3146. (cons "</text:list>"
  3147. (format "\n<text:list text:style-name=\"%s\" %s>"
  3148. (case (org-element-property :type el)
  3149. (ordered "OrgNumberedList")
  3150. (unordered "OrgBulletedList")
  3151. (descriptive-1 "OrgDescriptionList")
  3152. (descriptive-2 "OrgDescriptionList"))
  3153. "text:continue-numbering=\"true\"")))
  3154. (item
  3155. (cond
  3156. ((not parent-list)
  3157. (if (funcall --element-preceded-by-table-p table info)
  3158. '("</text:list-header>" . "<text:list-header>")
  3159. '("</text:list-item>" . "<text:list-header>")))
  3160. ((funcall --element-preceded-by-table-p
  3161. parent-list info)
  3162. '("</text:list-header>" . "<text:list-header>"))
  3163. (t '("</text:list-item>" . "<text:list-item>"))))))
  3164. ;; Handle low-level headlines.
  3165. (loop for el in llh-genealogy
  3166. with step = 'item collect
  3167. (case step
  3168. (plain-list
  3169. (setq step 'item) ; Flip-flop
  3170. (setq parent-list el)
  3171. (cons "</text:list>"
  3172. (format "\n<text:list text:style-name=\"%s\" %s>"
  3173. (if (org-export-numbered-headline-p
  3174. el info)
  3175. "OrgNumberedList"
  3176. "OrgBulletedList")
  3177. "text:continue-numbering=\"true\"")))
  3178. (item
  3179. (setq step 'plain-list) ; Flip-flop
  3180. (cond
  3181. ((not parent-list)
  3182. (if (funcall --element-preceded-by-table-p table info)
  3183. '("</text:list-header>" . "<text:list-header>")
  3184. '("</text:list-item>" . "<text:list-header>")))
  3185. ((let ((section? (org-export-get-previous-element
  3186. parent-list info)))
  3187. (and section?
  3188. (eq (org-element-type section?) 'section)
  3189. (assq 'table (org-element-contents section?))))
  3190. '("</text:list-header>" . "<text:list-header>"))
  3191. (t
  3192. '("</text:list-item>" . "<text:list-item>")))))))))))
  3193. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  3194. table info)))
  3195. ;; OpenDocument schema does not permit table to occur within a
  3196. ;; list item.
  3197. ;; One solution - the easiest and lightweight, in terms of
  3198. ;; implementation - is to put the table in an indented text box
  3199. ;; and make the text box part of the list-item. Unfortunately if
  3200. ;; the table is big and spans multiple pages, the text box could
  3201. ;; overflow. In this case, the following attribute will come
  3202. ;; handy.
  3203. ;; ,---- From OpenDocument-v1.1.pdf
  3204. ;; | 15.27.28 Overflow behavior
  3205. ;; |
  3206. ;; | For text boxes contained within text document, the
  3207. ;; | style:overflow-behavior property specifies the behavior of text
  3208. ;; | boxes where the containing text does not fit into the text
  3209. ;; | box.
  3210. ;; |
  3211. ;; | If the attribute's value is clip, the text that does not fit
  3212. ;; | into the text box is not displayed.
  3213. ;; |
  3214. ;; | If the attribute value is auto-create-new-frame, a new frame
  3215. ;; | will be created on the next page, with the same position and
  3216. ;; | dimensions of the original frame.
  3217. ;; |
  3218. ;; | If the style:overflow-behavior property's value is
  3219. ;; | auto-create-new-frame and the text box has a minimum width or
  3220. ;; | height specified, then the text box will grow until the page
  3221. ;; | bounds are reached before a new frame is created.
  3222. ;; `----
  3223. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  3224. ;; auto-create-new-frame property and always resorts to clipping
  3225. ;; the text box. This results in table being truncated.
  3226. ;; So we solve the problem the hard (and fun) way using list
  3227. ;; continuations.
  3228. ;; The problem only becomes more interesting if you take in to
  3229. ;; account the following facts:
  3230. ;;
  3231. ;; - Description lists are simulated as plain lists.
  3232. ;; - Low-level headlines can be listified.
  3233. ;; - In Org-mode, a table can occur not only as a regular list
  3234. ;; item, but also within description lists and low-level
  3235. ;; headlines.
  3236. ;; See `org-odt-translate-description-lists' and
  3237. ;; `org-odt-translate-low-level-headlines' for how this is
  3238. ;; tackled.
  3239. (concat "\n"
  3240. ;; Discontinue the list.
  3241. (mapconcat 'car close-open-tags "\n")
  3242. ;; Put the table in an indented section.
  3243. (let* ((table (org-odt--table table contents info))
  3244. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3245. (style (format "OrgIndentedSection-Level-%d" level)))
  3246. (when table (org-odt-format-section table style)))
  3247. ;; Continue the list.
  3248. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3249. ;;;; Target
  3250. (defun org-odt-target (target contents info)
  3251. "Transcode a TARGET object from Org to ODT.
  3252. CONTENTS is nil. INFO is a plist holding contextual
  3253. information."
  3254. (let ((value (org-element-property :value target)))
  3255. (org-odt--target "" (org-export-solidify-link-text value))))
  3256. ;;;; Timestamp
  3257. (defun org-odt-timestamp (timestamp contents info)
  3258. "Transcode a TIMESTAMP object from Org to ODT.
  3259. CONTENTS is nil. INFO is a plist used as a communication
  3260. channel."
  3261. (let* ((raw-value (org-element-property :raw-value timestamp))
  3262. (type (org-element-property :type timestamp)))
  3263. (if (not (plist-get info :odt-use-date-fields))
  3264. (let ((value (org-odt-plain-text
  3265. (org-timestamp-translate timestamp) info)))
  3266. (case (org-element-property :type timestamp)
  3267. ((active active-range)
  3268. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3269. "OrgActiveTimestamp" value))
  3270. ((inactive inactive-range)
  3271. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3272. "OrgInactiveTimestamp" value))
  3273. (otherwise value)))
  3274. (case type
  3275. (active
  3276. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3277. "OrgActiveTimestamp"
  3278. (format "&lt;%s&gt;" (org-odt--format-timestamp timestamp))))
  3279. (inactive
  3280. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3281. "OrgInactiveTimestamp"
  3282. (format "[%s]" (org-odt--format-timestamp timestamp))))
  3283. (active-range
  3284. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3285. "OrgActiveTimestamp"
  3286. (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
  3287. (org-odt--format-timestamp timestamp)
  3288. (org-odt--format-timestamp timestamp 'end))))
  3289. (inactive-range
  3290. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3291. "OrgInactiveTimestamp"
  3292. (format "[%s]&#x2013;[%s]"
  3293. (org-odt--format-timestamp timestamp)
  3294. (org-odt--format-timestamp timestamp 'end))))
  3295. (otherwise
  3296. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3297. "OrgDiaryTimestamp"
  3298. (org-odt-plain-text (org-timestamp-translate timestamp)
  3299. info)))))))
  3300. ;;;; Underline
  3301. (defun org-odt-underline (underline contents info)
  3302. "Transcode UNDERLINE from Org to ODT.
  3303. CONTENTS is the text with underline markup. INFO is a plist
  3304. holding contextual information."
  3305. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3306. "Underline" contents))
  3307. ;;;; Verbatim
  3308. (defun org-odt-verbatim (verbatim contents info)
  3309. "Transcode a VERBATIM object from Org to ODT.
  3310. CONTENTS is nil. INFO is a plist used as a communication
  3311. channel."
  3312. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3313. "OrgCode" (org-odt--encode-plain-text
  3314. (org-element-property :value verbatim))))
  3315. ;;;; Verse Block
  3316. (defun org-odt-verse-block (verse-block contents info)
  3317. "Transcode a VERSE-BLOCK element from Org to ODT.
  3318. CONTENTS is verse block contents. INFO is a plist holding
  3319. contextual information."
  3320. ;; Add line breaks to each line of verse.
  3321. (setq contents (replace-regexp-in-string
  3322. "\\(<text:line-break/>\\)?[ \t]*\n"
  3323. "<text:line-break/>" contents))
  3324. ;; Replace tabs and spaces.
  3325. (setq contents (org-odt--encode-tabs-and-spaces contents))
  3326. ;; Surround it in a verse environment.
  3327. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3328. "OrgVerse" contents))
  3329. ;;; Filters
  3330. ;;;; LaTeX fragments
  3331. (defun org-odt--translate-latex-fragments (tree backend info)
  3332. (let ((processing-type (plist-get info :with-latex))
  3333. (count 0))
  3334. ;; Normalize processing-type to one of dvipng, mathml or verbatim.
  3335. ;; If the desired converter is not available, force verbatim
  3336. ;; processing.
  3337. (case processing-type
  3338. ((t mathml)
  3339. (if (and (fboundp 'org-format-latex-mathml-available-p)
  3340. (org-format-latex-mathml-available-p))
  3341. (setq processing-type 'mathml)
  3342. (message "LaTeX to MathML converter not available.")
  3343. (setq processing-type 'verbatim)))
  3344. ((dvipng imagemagick)
  3345. (unless (and (org-check-external-command "latex" "" t)
  3346. (org-check-external-command
  3347. (if (eq processing-type 'dvipng) "dvipng" "convert") "" t))
  3348. (message "LaTeX to PNG converter not available.")
  3349. (setq processing-type 'verbatim)))
  3350. (otherwise
  3351. (message "Unknown LaTeX option. Forcing verbatim.")
  3352. (setq processing-type 'verbatim)))
  3353. ;; Store normalized value for later use.
  3354. (when (plist-get info :with-latex)
  3355. (plist-put info :with-latex processing-type))
  3356. (message "Formatting LaTeX using %s" processing-type)
  3357. ;; Convert `latex-fragment's and `latex-environment's.
  3358. (when (memq processing-type '(mathml dvipng imagemagick))
  3359. (org-element-map tree '(latex-fragment latex-environment)
  3360. (lambda (latex-*)
  3361. (incf count)
  3362. (let* ((latex-frag (org-element-property :value latex-*))
  3363. (input-file (plist-get info :input-file))
  3364. (cache-dir (file-name-directory input-file))
  3365. (cache-subdir (concat
  3366. (case processing-type
  3367. ((dvipng imagemagick) "ltxpng/")
  3368. (mathml "ltxmathml/"))
  3369. (file-name-sans-extension
  3370. (file-name-nondirectory input-file))))
  3371. (display-msg
  3372. (case processing-type
  3373. ((dvipng imagemagick) (format "Creating LaTeX Image %d..." count))
  3374. (mathml (format "Creating MathML snippet %d..." count))))
  3375. ;; Get an Org-style link to PNG image or the MathML
  3376. ;; file.
  3377. (org-link
  3378. (let ((link (with-temp-buffer
  3379. (insert latex-frag)
  3380. (org-format-latex cache-subdir cache-dir
  3381. nil display-msg
  3382. nil processing-type)
  3383. (buffer-substring-no-properties
  3384. (point-min) (point-max)))))
  3385. (if (not (string-match "file:\\([^]]*\\)" link))
  3386. (prog1 nil (message "LaTeX Conversion failed."))
  3387. link))))
  3388. (when org-link
  3389. ;; Conversion succeeded. Parse above Org-style link to a
  3390. ;; `link' object.
  3391. (let* ((link (car (org-element-map (with-temp-buffer
  3392. (org-mode)
  3393. (insert org-link)
  3394. (org-element-parse-buffer))
  3395. 'link 'identity))))
  3396. ;; Orphan the link.
  3397. (org-element-put-property link :parent nil)
  3398. (let* (
  3399. (replacement
  3400. (case (org-element-type latex-*)
  3401. ;; Case 1: LaTeX environment.
  3402. ;; Mimic a "standalone image or formula" by
  3403. ;; enclosing the `link' in a `paragraph'.
  3404. ;; Copy over original attributes, captions to
  3405. ;; the enclosing paragraph.
  3406. (latex-environment
  3407. (org-element-adopt-elements
  3408. (list 'paragraph
  3409. (list :style "OrgFormula"
  3410. :name (org-element-property :name
  3411. latex-*)
  3412. :caption (org-element-property :caption
  3413. latex-*)))
  3414. link))
  3415. ;; Case 2: LaTeX fragment.
  3416. ;; No special action.
  3417. (latex-fragment link))))
  3418. ;; Note down the object that link replaces.
  3419. (org-element-put-property replacement :replaces
  3420. (list (org-element-type latex-*)
  3421. (list :value latex-frag)))
  3422. ;; Replace now.
  3423. (org-element-set-element latex-* replacement))))))
  3424. info)))
  3425. tree)
  3426. ;;;; Description lists
  3427. ;; This translator is necessary to handle indented tables in a uniform
  3428. ;; manner. See comment in `org-odt--table'.
  3429. (defun org-odt--translate-description-lists (tree backend info)
  3430. ;; OpenDocument has no notion of a description list. So simulate it
  3431. ;; using plain lists. Description lists in the exported document
  3432. ;; are typeset in the same manner as they are in a typical HTML
  3433. ;; document.
  3434. ;;
  3435. ;; Specifically, a description list like this:
  3436. ;;
  3437. ;; ,----
  3438. ;; | - term-1 :: definition-1
  3439. ;; | - term-2 :: definition-2
  3440. ;; `----
  3441. ;;
  3442. ;; gets translated in to the following form:
  3443. ;;
  3444. ;; ,----
  3445. ;; | - term-1
  3446. ;; | - definition-1
  3447. ;; | - term-2
  3448. ;; | - definition-2
  3449. ;; `----
  3450. ;;
  3451. ;; Further effect is achieved by fixing the OD styles as below:
  3452. ;;
  3453. ;; 1. Set the :type property of the simulated lists to
  3454. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3455. ;; that has *no* bullets whatsoever.
  3456. ;;
  3457. ;; 2. The paragraph containing the definition term is styled to be
  3458. ;; in bold.
  3459. ;;
  3460. (org-element-map tree 'plain-list
  3461. (lambda (el)
  3462. (when (equal (org-element-property :type el) 'descriptive)
  3463. (org-element-set-element
  3464. el
  3465. (apply 'org-element-adopt-elements
  3466. (list 'plain-list (list :type 'descriptive-1))
  3467. (mapcar
  3468. (lambda (item)
  3469. (org-element-adopt-elements
  3470. (list 'item (list :checkbox (org-element-property
  3471. :checkbox item)))
  3472. (list 'paragraph (list :style "Text_20_body_20_bold")
  3473. (or (org-element-property :tag item) "(no term)"))
  3474. (org-element-adopt-elements
  3475. (list 'plain-list (list :type 'descriptive-2))
  3476. (apply 'org-element-adopt-elements
  3477. (list 'item nil)
  3478. (org-element-contents item)))))
  3479. (org-element-contents el)))))
  3480. nil)
  3481. info)
  3482. tree)
  3483. ;;;; List tables
  3484. ;; Lists that are marked with attribute `:list-table' are called as
  3485. ;; list tables. They will be rendered as a table within the exported
  3486. ;; document.
  3487. ;; Consider an example. The following list table
  3488. ;;
  3489. ;; #+attr_odt :list-table t
  3490. ;; - Row 1
  3491. ;; - 1.1
  3492. ;; - 1.2
  3493. ;; - 1.3
  3494. ;; - Row 2
  3495. ;; - 2.1
  3496. ;; - 2.2
  3497. ;; - 2.3
  3498. ;;
  3499. ;; will be exported as though it were an Org table like the one show
  3500. ;; below.
  3501. ;;
  3502. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3503. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3504. ;;
  3505. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3506. ;; a unique row in the exported document. So if an exported table
  3507. ;; needs to contain a single paragraph (with copious text) it needs to
  3508. ;; be typed up in a single line. Editing such long lines using the
  3509. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3510. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3511. ;;
  3512. ;; A LIST-TABLE circumvents above problems.
  3513. ;;
  3514. ;; Note that in the example above the list items could be paragraphs
  3515. ;; themselves and the list can be arbitrarily deep.
  3516. ;;
  3517. ;; Inspired by following thread:
  3518. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
  3519. ;; Translate lists to tables
  3520. (defun org-odt--translate-list-tables (tree backend info)
  3521. (org-element-map tree 'plain-list
  3522. (lambda (l1-list)
  3523. (when (org-export-read-attribute :attr_odt l1-list :list-table)
  3524. ;; Replace list with table.
  3525. (org-element-set-element
  3526. l1-list
  3527. ;; Build replacement table.
  3528. (apply 'org-element-adopt-elements
  3529. (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
  3530. (org-element-map l1-list 'item
  3531. (lambda (l1-item)
  3532. (let* ((l1-item-contents (org-element-contents l1-item))
  3533. l1-item-leading-text l2-list)
  3534. ;; Remove Level-2 list from the Level-item. It
  3535. ;; will be subsequently attached as table-cells.
  3536. (let ((cur l1-item-contents) prev)
  3537. (while (and cur (not (eq (org-element-type (car cur))
  3538. 'plain-list)))
  3539. (setq prev cur)
  3540. (setq cur (cdr cur)))
  3541. (when prev
  3542. (setcdr prev nil)
  3543. (setq l2-list (car cur)))
  3544. (setq l1-item-leading-text l1-item-contents))
  3545. ;; Level-1 items start a table row.
  3546. (apply 'org-element-adopt-elements
  3547. (list 'table-row (list :type 'standard))
  3548. ;; Leading text of level-1 item define
  3549. ;; the first table-cell.
  3550. (apply 'org-element-adopt-elements
  3551. (list 'table-cell nil)
  3552. l1-item-leading-text)
  3553. ;; Level-2 items define subsequent
  3554. ;; table-cells of the row.
  3555. (org-element-map l2-list 'item
  3556. (lambda (l2-item)
  3557. (apply 'org-element-adopt-elements
  3558. (list 'table-cell nil)
  3559. (org-element-contents l2-item)))
  3560. info nil 'item))))
  3561. info nil 'item))))
  3562. nil)
  3563. info)
  3564. tree)
  3565. ;;; Interactive functions
  3566. (defun org-odt-create-manifest-file-entry (&rest args)
  3567. (push args org-odt-manifest-file-entries))
  3568. (defun org-odt-write-manifest-file ()
  3569. (make-directory (concat org-odt-zip-dir "META-INF"))
  3570. (let ((manifest-file (concat org-odt-zip-dir "META-INF/manifest.xml")))
  3571. (with-current-buffer
  3572. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3573. (find-file-noselect manifest-file t))
  3574. (insert
  3575. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3576. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3577. (mapc
  3578. (lambda (file-entry)
  3579. (let* ((version (nth 2 file-entry))
  3580. (extra (if (not version) ""
  3581. (format " manifest:version=\"%s\"" version))))
  3582. (insert
  3583. (format org-odt-manifest-file-entry-tag
  3584. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3585. org-odt-manifest-file-entries)
  3586. (insert "\n</manifest:manifest>"))))
  3587. (defmacro org-odt--export-wrap (out-file &rest body)
  3588. `(let* ((--out-file ,out-file)
  3589. (out-file-type (file-name-extension --out-file))
  3590. (org-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3591. "meta.xml" "styles.xml"))
  3592. ;; Initialize temporary workarea. All files that end up in
  3593. ;; the exported document get parked/created here.
  3594. (org-odt-zip-dir (file-name-as-directory
  3595. (make-temp-file (format "%s-" out-file-type) t)))
  3596. (org-odt-manifest-file-entries nil)
  3597. (--cleanup-xml-buffers
  3598. (function
  3599. (lambda nil
  3600. ;; Kill all XML buffers.
  3601. (mapc (lambda (file)
  3602. (let ((buf (find-buffer-visiting
  3603. (concat org-odt-zip-dir file))))
  3604. (when buf
  3605. (with-current-buffer buf
  3606. (set-buffer-modified-p nil)
  3607. (kill-buffer buf)))))
  3608. org-odt-xml-files)
  3609. ;; Delete temporary directory and also other embedded
  3610. ;; files that get copied there.
  3611. (delete-directory org-odt-zip-dir t)))))
  3612. (condition-case err
  3613. (progn
  3614. (unless (executable-find "zip")
  3615. ;; Not at all OSes ship with zip by default
  3616. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3617. ;; Do export. This creates a bunch of xml files ready to be
  3618. ;; saved and zipped.
  3619. (progn ,@body)
  3620. ;; Create a manifest entry for content.xml.
  3621. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  3622. ;; Write mimetype file
  3623. (let* ((mimetypes
  3624. '(("odt" . "application/vnd.oasis.opendocument.text")
  3625. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3626. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3627. (unless mimetype
  3628. (error "Unknown OpenDocument backend %S" out-file-type))
  3629. (write-region mimetype nil (concat org-odt-zip-dir "mimetype"))
  3630. (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3631. ;; Write out the manifest entries before zipping
  3632. (org-odt-write-manifest-file)
  3633. ;; Save all XML files.
  3634. (mapc (lambda (file)
  3635. (let ((buf (find-buffer-visiting
  3636. (concat org-odt-zip-dir file))))
  3637. (when buf
  3638. (with-current-buffer buf
  3639. ;; Prettify output if needed.
  3640. (when org-odt-prettify-xml
  3641. (indent-region (point-min) (point-max)))
  3642. (save-buffer 0)))))
  3643. org-odt-xml-files)
  3644. ;; Run zip.
  3645. (let* ((target --out-file)
  3646. (target-name (file-name-nondirectory target))
  3647. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3648. ("zip" "-rmTq" ,target-name "."))))
  3649. ;; If a file with same name as the desired output file
  3650. ;; exists, remove it.
  3651. (when (file-exists-p target)
  3652. (delete-file target))
  3653. ;; Zip up the xml files.
  3654. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3655. (message "Creating ODT file...")
  3656. ;; Switch temporarily to content.xml. This way Zip
  3657. ;; process will inherit `org-odt-zip-dir' as the current
  3658. ;; directory.
  3659. (with-current-buffer
  3660. (find-file-noselect (concat org-odt-zip-dir "content.xml") t)
  3661. (mapc
  3662. (lambda (cmd)
  3663. (message "Running %s" (mapconcat 'identity cmd " "))
  3664. (setq err-string
  3665. (with-output-to-string
  3666. (setq exitcode
  3667. (apply 'call-process (car cmd)
  3668. nil standard-output nil (cdr cmd)))))
  3669. (or (zerop exitcode)
  3670. (error (concat "Unable to create OpenDocument file."
  3671. (format " Zip failed with error (%s)"
  3672. err-string)))))
  3673. cmds)))
  3674. ;; Move the zip file from temporary work directory to
  3675. ;; user-mandated location.
  3676. (rename-file (concat org-odt-zip-dir target-name) target)
  3677. (message "Created %s" (expand-file-name target))
  3678. ;; Cleanup work directory and work files.
  3679. (funcall --cleanup-xml-buffers)
  3680. ;; Open the OpenDocument file in archive-mode for
  3681. ;; examination.
  3682. (find-file-noselect target t)
  3683. ;; Return exported file.
  3684. (cond
  3685. ;; Case 1: Conversion desired on exported file. Run the
  3686. ;; converter on the OpenDocument file. Return the
  3687. ;; converted file.
  3688. (org-odt-preferred-output-format
  3689. (or (org-odt-convert target org-odt-preferred-output-format)
  3690. target))
  3691. ;; Case 2: No further conversion. Return exported
  3692. ;; OpenDocument file.
  3693. (t target))))
  3694. (error
  3695. ;; Cleanup work directory and work files.
  3696. (funcall --cleanup-xml-buffers)
  3697. (message "OpenDocument export failed: %s"
  3698. (error-message-string err))))))
  3699. ;;;; Export to OpenDocument formula
  3700. ;;;###autoload
  3701. (defun org-odt-export-as-odf (latex-frag &optional odf-file)
  3702. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3703. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3704. MathML. When invoked as an interactive command, use
  3705. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3706. region. If no LaTeX fragments are found, prompt for it. Push
  3707. MathML source to kill ring depending on the value of
  3708. `org-export-copy-to-kill-ring'."
  3709. (interactive
  3710. `(,(let (frag)
  3711. (setq frag (and (setq frag (and (region-active-p)
  3712. (buffer-substring (region-beginning)
  3713. (region-end))))
  3714. (loop for e in org-latex-regexps
  3715. thereis (when (string-match (nth 1 e) frag)
  3716. (match-string (nth 2 e) frag)))))
  3717. (read-string "LaTeX Fragment: " frag nil frag))
  3718. ,(let ((odf-filename (expand-file-name
  3719. (concat
  3720. (file-name-sans-extension
  3721. (or (file-name-nondirectory buffer-file-name)))
  3722. "." "odf")
  3723. (file-name-directory buffer-file-name))))
  3724. (read-file-name "ODF filename: " nil odf-filename nil
  3725. (file-name-nondirectory odf-filename)))))
  3726. (let ((filename (or odf-file
  3727. (expand-file-name
  3728. (concat
  3729. (file-name-sans-extension
  3730. (or (file-name-nondirectory buffer-file-name)))
  3731. "." "odf")
  3732. (file-name-directory buffer-file-name)))))
  3733. (org-odt--export-wrap
  3734. filename
  3735. (let* ((buffer (progn
  3736. (require 'nxml-mode)
  3737. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3738. (find-file-noselect (concat org-odt-zip-dir
  3739. "content.xml") t))))
  3740. (coding-system-for-write 'utf-8)
  3741. (save-buffer-coding-system 'utf-8))
  3742. (set-buffer buffer)
  3743. (set-buffer-file-coding-system coding-system-for-write)
  3744. (let ((mathml (org-create-math-formula latex-frag)))
  3745. (unless mathml (error "No Math formula created"))
  3746. (insert mathml)
  3747. ;; Add MathML to kill ring, if needed.
  3748. (when (org-export--copy-to-kill-ring-p)
  3749. (org-kill-new (buffer-string))))))))
  3750. ;;;###autoload
  3751. (defun org-odt-export-as-odf-and-open ()
  3752. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3753. Use `org-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3754. formula file."
  3755. (interactive)
  3756. (org-open-file (call-interactively 'org-odt-export-as-odf) 'system))
  3757. ;;;; Export to OpenDocument Text
  3758. ;;;###autoload
  3759. (defun org-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
  3760. "Export current buffer to a ODT file.
  3761. If narrowing is active in the current buffer, only export its
  3762. narrowed part.
  3763. If a region is active, export that region.
  3764. A non-nil optional argument ASYNC means the process should happen
  3765. asynchronously. The resulting file should be accessible through
  3766. the `org-export-stack' interface.
  3767. When optional argument SUBTREEP is non-nil, export the sub-tree
  3768. at point, extracting information from the headline properties
  3769. first.
  3770. When optional argument VISIBLE-ONLY is non-nil, don't export
  3771. contents of hidden elements.
  3772. EXT-PLIST, when provided, is a property list with external
  3773. parameters overriding Org default settings, but still inferior to
  3774. file-local settings.
  3775. Return output file's name."
  3776. (interactive)
  3777. (let ((outfile (org-export-output-file-name ".odt" subtreep)))
  3778. (if async
  3779. (org-export-async-start (lambda (f) (org-export-add-to-stack f 'odt))
  3780. `(expand-file-name
  3781. (org-odt--export-wrap
  3782. ,outfile
  3783. (let* ((org-odt-embedded-images-count 0)
  3784. (org-odt-embedded-formulas-count 0)
  3785. (org-odt-automatic-styles nil)
  3786. (org-odt-object-counters nil)
  3787. ;; Let `htmlfontify' know that we are interested in
  3788. ;; collecting styles.
  3789. (hfy-user-sheet-assoc nil))
  3790. ;; Initialize content.xml and kick-off the export
  3791. ;; process.
  3792. (let ((out-buf
  3793. (progn
  3794. (require 'nxml-mode)
  3795. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3796. (find-file-noselect
  3797. (concat org-odt-zip-dir "content.xml") t))))
  3798. (output (org-export-as
  3799. 'odt ,subtreep ,visible-only nil ,ext-plist)))
  3800. (with-current-buffer out-buf
  3801. (erase-buffer)
  3802. (insert output)))))))
  3803. (org-odt--export-wrap
  3804. outfile
  3805. (let* ((org-odt-embedded-images-count 0)
  3806. (org-odt-embedded-formulas-count 0)
  3807. (org-odt-automatic-styles nil)
  3808. (org-odt-object-counters nil)
  3809. ;; Let `htmlfontify' know that we are interested in collecting
  3810. ;; styles.
  3811. (hfy-user-sheet-assoc nil))
  3812. ;; Initialize content.xml and kick-off the export process.
  3813. (let ((output (org-export-as 'odt subtreep visible-only nil ext-plist))
  3814. (out-buf (progn
  3815. (require 'nxml-mode)
  3816. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3817. (find-file-noselect
  3818. (concat org-odt-zip-dir "content.xml") t)))))
  3819. (with-current-buffer out-buf (erase-buffer) (insert output))))))))
  3820. ;;;; Convert between OpenDocument and other formats
  3821. (defun org-odt-reachable-p (in-fmt out-fmt)
  3822. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3823. (catch 'done
  3824. (let ((reachable-formats (org-odt-do-reachable-formats in-fmt)))
  3825. (dolist (e reachable-formats)
  3826. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3827. (when out-fmt-spec
  3828. (throw 'done (cons (car e) out-fmt-spec))))))))
  3829. (defun org-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3830. "Workhorse routine for `org-odt-convert'."
  3831. (require 'browse-url)
  3832. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3833. (dummy (or (file-readable-p in-file)
  3834. (error "Cannot read %s" in-file)))
  3835. (in-fmt (file-name-extension in-file))
  3836. (out-fmt (or out-fmt (error "Output format unspecified")))
  3837. (how (or (org-odt-reachable-p in-fmt out-fmt)
  3838. (error "Cannot convert from %s format to %s format?"
  3839. in-fmt out-fmt)))
  3840. (convert-process (car how))
  3841. (out-file (concat (file-name-sans-extension in-file) "."
  3842. (nth 1 (or (cdr how) out-fmt))))
  3843. (extra-options (or (nth 2 (cdr how)) ""))
  3844. (out-dir (file-name-directory in-file))
  3845. (cmd (format-spec convert-process
  3846. `((?i . ,(shell-quote-argument in-file))
  3847. (?I . ,(browse-url-file-url in-file))
  3848. (?f . ,out-fmt)
  3849. (?o . ,out-file)
  3850. (?O . ,(browse-url-file-url out-file))
  3851. (?d . , (shell-quote-argument out-dir))
  3852. (?D . ,(browse-url-file-url out-dir))
  3853. (?x . ,extra-options)))))
  3854. (when (file-exists-p out-file)
  3855. (delete-file out-file))
  3856. (message "Executing %s" cmd)
  3857. (let ((cmd-output (shell-command-to-string cmd)))
  3858. (message "%s" cmd-output))
  3859. (cond
  3860. ((file-exists-p out-file)
  3861. (message "Exported to %s" out-file)
  3862. (when prefix-arg
  3863. (message "Opening %s..." out-file)
  3864. (org-open-file out-file 'system))
  3865. out-file)
  3866. (t
  3867. (message "Export to %s failed" out-file)
  3868. nil))))
  3869. (defun org-odt-do-reachable-formats (in-fmt)
  3870. "Return verbose info about formats to which IN-FMT can be converted.
  3871. Return a list where each element is of the
  3872. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3873. `org-odt-convert-processes' for CONVERTER-PROCESS and see
  3874. `org-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3875. (let* ((converter
  3876. (and org-odt-convert-process
  3877. (cadr (assoc-string org-odt-convert-process
  3878. org-odt-convert-processes t))))
  3879. (capabilities
  3880. (and org-odt-convert-process
  3881. (cadr (assoc-string org-odt-convert-process
  3882. org-odt-convert-processes t))
  3883. org-odt-convert-capabilities))
  3884. reachable-formats)
  3885. (when converter
  3886. (dolist (c capabilities)
  3887. (when (member in-fmt (nth 1 c))
  3888. (push (cons converter (nth 2 c)) reachable-formats))))
  3889. reachable-formats))
  3890. (defun org-odt-reachable-formats (in-fmt)
  3891. "Return list of formats to which IN-FMT can be converted.
  3892. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3893. (let (l)
  3894. (mapc (lambda (e) (add-to-list 'l e))
  3895. (apply 'append (mapcar
  3896. (lambda (e) (mapcar 'car (cdr e)))
  3897. (org-odt-do-reachable-formats in-fmt))))
  3898. l))
  3899. (defun org-odt-convert-read-params ()
  3900. "Return IN-FILE and OUT-FMT params for `org-odt-do-convert'.
  3901. This is a helper routine for interactive use."
  3902. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3903. (in-file (read-file-name "File to be converted: "
  3904. nil buffer-file-name t))
  3905. (in-fmt (file-name-extension in-file))
  3906. (out-fmt-choices (org-odt-reachable-formats in-fmt))
  3907. (out-fmt
  3908. (or (and out-fmt-choices
  3909. (funcall input "Output format: "
  3910. out-fmt-choices nil nil nil))
  3911. (error
  3912. "No known converter or no known output formats for %s files"
  3913. in-fmt))))
  3914. (list in-file out-fmt)))
  3915. ;;;###autoload
  3916. (defun org-odt-convert (&optional in-file out-fmt prefix-arg)
  3917. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3918. IN-FILE is the file to be converted. If unspecified, it defaults
  3919. to variable `buffer-file-name'. OUT-FMT is the desired output
  3920. format. Use `org-odt-convert-process' as the converter.
  3921. If PREFIX-ARG is non-nil then the newly converted file is opened
  3922. using `org-open-file'."
  3923. (interactive
  3924. (append (org-odt-convert-read-params) current-prefix-arg))
  3925. (org-odt-do-convert in-file out-fmt prefix-arg))
  3926. ;;; Library Initializations
  3927. (mapc
  3928. (lambda (desc)
  3929. ;; Let Emacs open all OpenDocument files in archive mode
  3930. (add-to-list 'auto-mode-alist
  3931. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3932. org-odt-file-extensions)
  3933. (provide 'ox-odt)
  3934. ;; Local variables:
  3935. ;; generated-autoload-file: "org-loaddefs.el"
  3936. ;; End:
  3937. ;;; ox-odt.el ends here