ox-odt.el 158 KB

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