ox-odt.el 158 KB

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