ox-odt.el 158 KB

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