ox-odt.el 157 KB

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