ox-odt.el 159 KB

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