ox-odt.el 158 KB

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