ox-odt.el 158 KB

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