org-e-odt.el 135 KB

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