org-e-odt.el 135 KB

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