ox-odt.el 158 KB

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