org-e-odt.el 140 KB

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