org-e-odt.el 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006
  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 ((section (assq 'section (org-element-contents headline))))
  1626. (assq 'table (and section (org-element-contents section))))))
  1627. (format "\n<text:list-item>\n%s\n%s"
  1628. (concat
  1629. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1630. "Text_20_body"
  1631. (concat extra-targets anchored-title))
  1632. contents)
  1633. (if headline-has-table-p
  1634. "</text:list-header>"
  1635. "</text:list-item>")))
  1636. (and (org-export-last-sibling-p headline info)
  1637. "</text:list>")))
  1638. ;; Case 3. Standard headline. Export it as a section.
  1639. (t
  1640. (concat
  1641. (format
  1642. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
  1643. (format "Heading_20_%s" level)
  1644. level
  1645. (concat extra-targets anchored-title))
  1646. contents))))))
  1647. ;;;; Horizontal Rule
  1648. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1649. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1650. CONTENTS is nil. INFO is a plist holding contextual information."
  1651. (org-e-odt--wrap-label
  1652. horizontal-rule
  1653. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1654. "Horizontal_20_Line" "")))
  1655. ;;;; Inline Babel Call
  1656. ;; Inline Babel Calls are ignored.
  1657. ;;;; Inline Src Block
  1658. (defun org-e-odt--find-verb-separator (s)
  1659. "Return a character not used in string S.
  1660. This is used to choose a separator for constructs like \\verb."
  1661. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1662. (loop for c across ll
  1663. when (not (string-match (regexp-quote (char-to-string c)) s))
  1664. return (char-to-string c))))
  1665. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1666. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1667. CONTENTS holds the contents of the item. INFO is a plist holding
  1668. contextual information."
  1669. (let* ((org-lang (org-element-property :language inline-src-block))
  1670. (code (org-element-property :value inline-src-block))
  1671. (separator (org-e-odt--find-verb-separator code)))
  1672. (error "FIXME")))
  1673. ;;;; Inlinetask
  1674. (defun org-e-odt-inlinetask (inlinetask contents info)
  1675. "Transcode an INLINETASK element from Org to ODT.
  1676. CONTENTS holds the contents of the block. INFO is a plist
  1677. holding contextual information."
  1678. (cond
  1679. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1680. ;; with appropriate arguments.
  1681. ((functionp org-e-odt-format-inlinetask-function)
  1682. (let ((format-function
  1683. (function*
  1684. (lambda (todo todo-type priority text tags
  1685. &key contents &allow-other-keys)
  1686. (funcall org-e-odt-format-inlinetask-function
  1687. todo todo-type priority text tags contents)))))
  1688. (org-e-odt-format-headline--wrap
  1689. inlinetask info format-function :contents contents)))
  1690. ;; Otherwise, use a default template.
  1691. (t (org-e-odt--wrap-label
  1692. inlinetask
  1693. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1694. "Text_20_body"
  1695. (org-e-odt--textbox
  1696. (concat
  1697. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1698. "OrgInlineTaskHeading"
  1699. (org-e-odt-format-headline--wrap
  1700. inlinetask info))
  1701. contents)
  1702. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  1703. ;;;; Italic
  1704. (defun org-e-odt-italic (italic contents info)
  1705. "Transcode ITALIC from Org to ODT.
  1706. CONTENTS is the text with italic markup. INFO is a plist holding
  1707. contextual information."
  1708. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1709. "Emphasis" contents))
  1710. ;;;; Item
  1711. (defun org-e-odt-item (item contents info)
  1712. "Transcode an ITEM element from Org to ODT.
  1713. CONTENTS holds the contents of the item. INFO is a plist holding
  1714. contextual information."
  1715. (let* ((plain-list (org-export-get-parent item))
  1716. (type (org-element-property :type plain-list))
  1717. (counter (org-element-property :counter item))
  1718. (tag (let ((tag (org-element-property :tag item)))
  1719. (and tag
  1720. (concat (org-e-odt--checkbox item)
  1721. (org-export-data tag info))))))
  1722. (case type
  1723. ((ordered unordered descriptive-1 descriptive-2)
  1724. (format "\n<text:list-item>\n%s\n%s"
  1725. contents
  1726. (let* ((--element-has-a-table-p
  1727. (function
  1728. (lambda (element info)
  1729. (loop for el in (org-element-contents element)
  1730. thereis (eq (org-element-type el) 'table))))))
  1731. (cond
  1732. ((funcall --element-has-a-table-p item info)
  1733. "</text:list-header>")
  1734. (t "</text:list-item>")))))
  1735. (t (error "Unknown list type: %S" type)))))
  1736. ;;;; Keyword
  1737. (defun org-e-odt-keyword (keyword contents info)
  1738. "Transcode a KEYWORD element from Org to ODT.
  1739. CONTENTS is nil. INFO is a plist holding contextual information."
  1740. (let ((key (org-element-property :key keyword))
  1741. (value (org-element-property :value keyword)))
  1742. (cond
  1743. ((string= key "ODT") value)
  1744. ((string= key "INDEX") (format "\\index{%s}" value))
  1745. ((string= key "TARGET") nil ; FIXME
  1746. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  1747. )
  1748. ((string= key "toc")
  1749. (let ((value (downcase value)))
  1750. (cond
  1751. ((string-match "\\<headlines\\>" value)
  1752. (let ((depth (or (and (string-match "[0-9]+" value)
  1753. (string-to-number (match-string 0 value)))
  1754. (plist-get info :with-toc))))
  1755. (when (wholenump depth) (org-e-odt-toc depth info))))
  1756. ((string= "tables" value) "FIXME")
  1757. ((string= "figures" value) "FIXME")
  1758. ((string= "listings" value)
  1759. (cond
  1760. ;; At the moment, src blocks with a caption are wrapped
  1761. ;; into a figure environment.
  1762. (t "FIXME")))))))))
  1763. ;;;; Latex Environment
  1764. (eval-after-load 'org-odt
  1765. '(ad-deactivate 'org-format-latex-as-mathml))
  1766. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1767. ;; (after org-e-odt-protect-latex-fragment activate)
  1768. ;; "Encode LaTeX fragment as XML.
  1769. ;; Do this when translation to MathML fails."
  1770. ;; (when (or (not (> (length ad-return-value) 0))
  1771. ;; (get-text-property 0 'org-protected ad-return-value))
  1772. ;; (setq ad-return-value
  1773. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1774. ;; 'org-protected t))))
  1775. (defun org-e-odt-format-latex (latex-frag processing-type info)
  1776. (let* ((prefix (case processing-type
  1777. (dvipng "ltxpng/")
  1778. (mathml "ltxmathml/")))
  1779. (input-file (plist-get info :input-file))
  1780. (cache-subdir
  1781. (concat prefix (file-name-sans-extension
  1782. (file-name-nondirectory input-file))))
  1783. (cache-dir (file-name-directory input-file))
  1784. (display-msg (case processing-type
  1785. (dvipng "Creating LaTeX Image...")
  1786. (mathml "Creating MathML snippet..."))))
  1787. (with-temp-buffer
  1788. (insert latex-frag)
  1789. (org-format-latex cache-subdir cache-dir nil display-msg
  1790. nil nil processing-type)
  1791. (buffer-string))))
  1792. (defun org-e-odt-latex-environment (latex-environment contents info)
  1793. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1794. CONTENTS is nil. INFO is a plist holding contextual information."
  1795. (org-e-odt--wrap-label
  1796. latex-environment
  1797. (let* ((latex-frag
  1798. (org-remove-indentation
  1799. (org-element-property :value latex-environment)))
  1800. (processing-type (plist-get info :LaTeX-fragments))
  1801. (caption (org-element-property :caption latex-environment))
  1802. (short-caption (and (cdr caption)
  1803. (org-export-data (cdr caption) info)))
  1804. (caption (and (car caption) (org-export-data (car caption) info)))
  1805. (label (org-element-property :name latex-environment))
  1806. (attr nil) ; FIXME
  1807. (label (org-element-property :name latex-environment)))
  1808. (when (memq processing-type '(t mathjax))
  1809. (unless (and (fboundp 'org-format-latex-mathml-available-p)
  1810. (org-format-latex-mathml-available-p))
  1811. (message "LaTeX to MathML converter not available. Trying dvinpng...")
  1812. (setq processing-type 'dvipng)))
  1813. (when (eq processing-type 'dvipng)
  1814. (unless (and (org-check-external-command "latex" "" t)
  1815. (org-check-external-command "dvipng" "" t))
  1816. (message "LaTeX to PNG converter not available. Using verbatim.")
  1817. (setq processing-type 'verbatim)))
  1818. (case processing-type
  1819. ((t mathjax)
  1820. (org-e-odt-format-formula latex-environment info))
  1821. (dvipng
  1822. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1823. "Text_20_body"
  1824. (org-e-odt-link--inline-image latex-environment info)))
  1825. (t (org-e-odt-do-format-code latex-frag))))))
  1826. ;;;; Latex Fragment
  1827. ;; (when latex-frag ; FIXME
  1828. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1829. ;; :description latex-frag)))
  1830. ;; handle verbatim
  1831. ;; provide descriptions
  1832. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1833. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1834. CONTENTS is nil. INFO is a plist holding contextual information."
  1835. (let* ((latex-frag (org-element-property :value latex-fragment))
  1836. (processing-type (plist-get info :LaTeX-fragments)))
  1837. (cond
  1838. ((member processing-type '(t mathjax))
  1839. (org-e-odt-format-formula latex-fragment info))
  1840. ((eq processing-type 'dvipng)
  1841. (org-e-odt-link--inline-image latex-fragment info))
  1842. (t (org-e-odt-encode-plain-text latex-frag t)))))
  1843. ;;;; Line Break
  1844. (defun org-e-odt-line-break (line-break contents info)
  1845. "Transcode a LINE-BREAK object from Org to ODT.
  1846. CONTENTS is nil. INFO is a plist holding contextual information."
  1847. "<text:line-break/>\n")
  1848. ;;;; Link
  1849. ;;;; Links :: Generic
  1850. ;; (org-lparse-link-description-is-image
  1851. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  1852. ;; href desc))
  1853. ;;;; Links :: Label references
  1854. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  1855. (let* ((--numbered-parent-headline-at-<=-n
  1856. (function
  1857. (lambda (element n info)
  1858. (loop for x in (org-export-get-genealogy element)
  1859. thereis (and (eq (org-element-type x) 'headline)
  1860. (<= (org-export-get-relative-level x info) n)
  1861. (org-export-numbered-headline-p x info)
  1862. x)))))
  1863. (--enumerate
  1864. (function
  1865. (lambda (element scope info &optional predicate)
  1866. (let ((counter 0))
  1867. (org-element-map
  1868. (or scope (plist-get info :parse-tree))
  1869. (org-element-type element)
  1870. (lambda (el)
  1871. (and (or (not predicate) (funcall predicate el info))
  1872. (incf counter)
  1873. (eq element el)
  1874. counter))
  1875. info 'first-match)))))
  1876. (scope (funcall --numbered-parent-headline-at-<=-n
  1877. element (or n org-e-odt-display-outline-level) info))
  1878. (ordinal (funcall --enumerate element scope info predicate))
  1879. (tag
  1880. (concat
  1881. ;; Section number.
  1882. (and scope
  1883. (mapconcat 'number-to-string
  1884. (org-export-get-headline-number scope info) "."))
  1885. ;; Separator.
  1886. (and scope ".")
  1887. ;; Ordinal.
  1888. (number-to-string ordinal))))
  1889. tag))
  1890. (defun org-e-odt-format-label (element info op)
  1891. (let* ((caption-from
  1892. (case (org-element-type element)
  1893. (link (org-export-get-parent-element element))
  1894. (t element)))
  1895. ;; get label and caption.
  1896. (label (org-element-property :name caption-from))
  1897. (caption (org-element-property :caption caption-from))
  1898. (short-caption (cdr caption))
  1899. ;; transcode captions.
  1900. (caption (and (car caption) (org-export-data (car caption) info)))
  1901. (short-caption (and short-caption
  1902. (org-export-data short-caption info))))
  1903. (when (or label caption)
  1904. (let* ((default-category
  1905. (cond
  1906. ((eq (org-element-type element) 'table)
  1907. "__Table__")
  1908. ((org-e-odt-standalone-image-p element info)
  1909. "__Figure__")
  1910. ((member (org-element-type element)
  1911. '(latex-environment latex-fragment))
  1912. (let ((processing-type (plist-get info :LaTeX-fragments)))
  1913. (cond
  1914. ((eq processing-type 'dvipng) "__DvipngImage__")
  1915. ((eq processing-type 'mathjax) "__MathFormula__")
  1916. ((eq processing-type 't) "__MathFormula__")
  1917. (t (error "Handle LaTeX:verbatim")))))
  1918. ((eq (org-element-type element) 'src-block)
  1919. "__Listing__")
  1920. (t (error "Handle enumeration of %S" element))))
  1921. (predicate
  1922. (cond
  1923. ((member (org-element-type element)
  1924. '(table latex-environment src-block))
  1925. nil)
  1926. ((org-e-odt-standalone-image-p element info)
  1927. 'org-e-odt-standalone-image-p)
  1928. (t (error "Handle enumeration of %S" element))))
  1929. (seqno (org-e-odt-enumerate-element
  1930. element info predicate)) ; FIXME
  1931. ;; handle label props.
  1932. (label-props (assoc default-category org-e-odt-category-map-alist))
  1933. ;; identify opendocument counter
  1934. (counter (nth 1 label-props))
  1935. ;; identify label style
  1936. (label-style (nth 2 label-props))
  1937. ;; retrieve localized category sting
  1938. (category (org-export-translate (nth 3 label-props) :utf-8 info)))
  1939. (case op
  1940. (definition
  1941. ;; assign an internal label, if user has not provided one
  1942. (setq label (or label (format "%s-%s" default-category seqno)))
  1943. (setq label (org-export-solidify-link-text label))
  1944. (cons
  1945. (format-spec
  1946. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1947. `((?e . ,category)
  1948. (?n . ,(format
  1949. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1950. label counter counter seqno))
  1951. (?c . ,(or caption ""))))
  1952. short-caption))
  1953. (reference
  1954. (assert label)
  1955. (setq label (org-export-solidify-link-text label))
  1956. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1957. (fmt1 (car fmt))
  1958. (fmt2 (cadr fmt)))
  1959. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1960. fmt1 label (format-spec fmt2 `((?e . ,category)
  1961. (?n . ,seqno))))))
  1962. (t (error "Unknow %S on label" op)))))))
  1963. ;;;; Links :: Math formula
  1964. (defun org-e-odt-format-formula (element info)
  1965. (let* ((src (cond
  1966. ((eq (org-element-type element) 'link) ; FIXME
  1967. (let* ((type (org-element-property :type element))
  1968. (raw-path (org-element-property :path element)))
  1969. (cond
  1970. ((file-name-absolute-p raw-path)
  1971. (expand-file-name raw-path))
  1972. (t raw-path))))
  1973. ((member (org-element-type element)
  1974. '(latex-fragment latex-environment))
  1975. (let* ((latex-frag (org-remove-indentation
  1976. (org-element-property :value element)))
  1977. (formula-link (org-e-odt-format-latex
  1978. latex-frag 'mathml info)))
  1979. (and formula-link
  1980. (string-match "file:\\([^]]*\\)" formula-link)
  1981. (match-string 1 formula-link))))
  1982. (t (error "what is this?"))))
  1983. (full-src (if (file-name-absolute-p src) src
  1984. (expand-file-name src (file-name-directory
  1985. (plist-get info :input-file)))))
  1986. (caption-from
  1987. (case (org-element-type element)
  1988. (link (org-export-get-parent-element element))
  1989. (t element)))
  1990. (captions (org-e-odt-format-label caption-from info 'definition))
  1991. (caption (car captions))
  1992. (href
  1993. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  1994. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  1995. (file-name-directory (org-e-odt-copy-formula-file full-src))))
  1996. (embed-as (if caption 'paragraph 'character))
  1997. width height)
  1998. (cond
  1999. ((eq embed-as 'character)
  2000. (org-e-odt-format-entity "InlineFormula" href width height))
  2001. (t
  2002. (let* ((equation (org-e-odt-format-entity
  2003. "CaptionedDisplayFormula" href width height captions))
  2004. (label
  2005. (let* ((org-e-odt-category-map-alist
  2006. '(("__Table__" "Table" "value")
  2007. ("__Figure__" "Illustration" "value")
  2008. ("__MathFormula__" "Text" "math-label")
  2009. ("__DvipngImage__" "Equation" "value")
  2010. ("__Listing__" "Listing" "value"))))
  2011. (car (org-e-odt-format-label caption-from info 'definition))))
  2012. (formula-tree
  2013. (org-element-adopt-elements
  2014. (list 'table '(:type org :attr_odt (":style \"OrgEquation\"")))
  2015. (org-element-adopt-elements
  2016. (list 'table-row '(:type standard))
  2017. (list 'table-cell nil "<c8>") (list 'table-cell nil "<c1>"))
  2018. (org-element-adopt-elements
  2019. (list 'table-row '(:type standard))
  2020. (org-element-adopt-elements
  2021. (list 'table-cell nil)
  2022. (list 'export-block (list :type "ODT" :value equation)))
  2023. (org-element-adopt-elements
  2024. (list 'table-cell nil)
  2025. (list 'export-block (list :type "ODT" :value label))))))
  2026. (formula-info
  2027. (org-export-collect-tree-properties
  2028. formula-tree (org-export-get-environment 'e-odt))))
  2029. (org-export-data formula-tree formula-info))))))
  2030. (defun org-e-odt-copy-formula-file (src-file)
  2031. "Returns the internal name of the file"
  2032. (let* ((target-dir (format "Formula-%04d/"
  2033. (incf org-e-odt-embedded-formulas-count)))
  2034. (target-file (concat target-dir "content.xml")))
  2035. ;; Create a directory for holding formula file. Also enter it in
  2036. ;; to manifest.
  2037. (make-directory (concat org-e-odt-zip-dir target-dir))
  2038. (org-e-odt-create-manifest-file-entry
  2039. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2040. ;; Copy over the formula file from user directory to zip
  2041. ;; directory.
  2042. (message "Embedding %s as %s ..." src-file target-file)
  2043. (let ((case-fold-search nil))
  2044. (cond
  2045. ;; Case 1: Mathml.
  2046. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  2047. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  2048. ;; Case 2: OpenDocument formula.
  2049. ((string-match "\\.odf\\'" src-file)
  2050. (org-e-odt--zip-extract src-file "content.xml"
  2051. (concat org-e-odt-zip-dir target-dir)))
  2052. (t (error "%s is not a formula file" src-file))))
  2053. ;; Enter the formula file in to manifest.
  2054. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  2055. target-file))
  2056. ;;;; Targets
  2057. (defun org-e-odt-format-entity (entity href width height &optional
  2058. captions user-frame-params)
  2059. (let* ((caption (car captions)) (short-caption (cdr captions))
  2060. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  2061. default-frame-params frame-params
  2062. (--merge-frame-params
  2063. (function
  2064. (lambda (default-frame-params user-frame-params)
  2065. (if (not user-frame-params) default-frame-params
  2066. (assert (= (length default-frame-params) 3))
  2067. (assert (= (length user-frame-params) 3))
  2068. (loop for user-frame-param in user-frame-params
  2069. for default-frame-param in default-frame-params
  2070. collect (or user-frame-param default-frame-param)))))))
  2071. (cond
  2072. ((not caption)
  2073. (setq default-frame-params (nth 2 entity-style))
  2074. (setq frame-params (funcall --merge-frame-params
  2075. default-frame-params user-frame-params))
  2076. (apply 'org-e-odt--frame href width height frame-params))
  2077. (t
  2078. (setq default-frame-params (nth 3 entity-style))
  2079. (setq frame-params (funcall --merge-frame-params
  2080. default-frame-params user-frame-params))
  2081. (apply 'org-e-odt--textbox
  2082. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2083. "Illustration"
  2084. (concat
  2085. (apply 'org-e-odt--frame href width height
  2086. (let ((entity-style-1 (copy-sequence
  2087. (nth 2 entity-style))))
  2088. (setcar (cdr entity-style-1)
  2089. (concat
  2090. (cadr entity-style-1)
  2091. (and short-caption
  2092. (format " draw:name=\"%s\" "
  2093. short-caption))))
  2094. entity-style-1))
  2095. caption))
  2096. width height frame-params)))))
  2097. (defun org-e-odt-standalone-image-p (element info)
  2098. "Test if ELEMENT is a standalone image for the purpose ODT export.
  2099. INFO is a plist holding contextual information.
  2100. Return non-nil, if ELEMENT is of type paragraph and it's sole
  2101. content, save for whitespaces, is a link that qualifies as an
  2102. inline image.
  2103. Return non-nil, if ELEMENT is of type link and it's containing
  2104. paragraph has no other content save for leading and trailing
  2105. whitespaces.
  2106. Return nil, otherwise.
  2107. Bind `org-e-odt-standalone-image-predicate' to constrain
  2108. paragraph further. For example, to check for only captioned
  2109. standalone images, do the following.
  2110. \(setq org-e-odt-standalone-image-predicate
  2111. \(lambda \(paragraph\)
  2112. \(org-element-property :caption paragraph\)\)\)
  2113. "
  2114. (let ((--standalone-image-predicate
  2115. (function (lambda (paragraph)
  2116. (or (org-element-property :caption paragraph)
  2117. (org-element-property :name paragraph)))))
  2118. (paragraph (case (org-element-type element)
  2119. (paragraph element)
  2120. (link (and (org-export-inline-image-p
  2121. element org-e-odt-inline-image-rules)
  2122. (org-export-get-parent element)))
  2123. (t nil))))
  2124. (when paragraph
  2125. (assert (eq (org-element-type paragraph) 'paragraph))
  2126. (when (funcall --standalone-image-predicate paragraph)
  2127. (let ((contents (org-element-contents paragraph)))
  2128. (loop for x in contents
  2129. with inline-image-count = 0
  2130. always (cond
  2131. ((eq (org-element-type x) 'plain-text)
  2132. (not (org-string-nw-p x)))
  2133. ((eq (org-element-type x) 'link)
  2134. (when (org-export-inline-image-p
  2135. x org-e-odt-inline-image-rules)
  2136. (= (incf inline-image-count) 1)))
  2137. (t nil))))))))
  2138. (defun org-e-odt-get-previous-elements (blob info)
  2139. (let ((parent (org-export-get-parent blob)))
  2140. (cdr (memq blob (reverse (org-element-contents parent))))))
  2141. (defun org-e-odt-resolve-numbered-paragraph (element info)
  2142. (when (eq (org-element-type element) 'item)
  2143. (let ((el element) ordinal)
  2144. (while (eq (org-element-type el) 'item)
  2145. (push (1+ (length (org-e-odt-get-previous-elements el info))) ordinal)
  2146. (setq el (org-export-get-parent (org-export-get-parent el))))
  2147. ordinal)))
  2148. (defun org-e-odt-link (link desc info)
  2149. "Transcode a LINK object from Org to ODT.
  2150. DESC is the description part of the link, or the empty string.
  2151. INFO is a plist holding contextual information. See
  2152. `org-export-data'."
  2153. (let* ((type (org-element-property :type link))
  2154. (raw-path (org-element-property :path link))
  2155. ;; Ensure DESC really exists, or set it to nil.
  2156. (desc (and (not (string= desc "")) desc))
  2157. (imagep (org-export-inline-image-p
  2158. link org-e-odt-inline-image-rules))
  2159. (path (cond
  2160. ((member type '("http" "https" "ftp" "mailto"))
  2161. (concat type ":" raw-path))
  2162. ((string= type "file")
  2163. (if (file-name-absolute-p raw-path)
  2164. (concat "file://" (expand-file-name raw-path))
  2165. (concat "file://" raw-path)))
  2166. (t raw-path)))
  2167. protocol)
  2168. (cond
  2169. ;; Image file.
  2170. ((and (not desc) (org-export-inline-image-p
  2171. link org-e-odt-inline-image-rules))
  2172. (org-e-odt-link--inline-image link info))
  2173. ;; Radio target: Transcode target's contents and use them as
  2174. ;; link's description.
  2175. ((string= type "radio")
  2176. (let ((destination (org-export-resolve-radio-link link info)))
  2177. (when destination
  2178. (let ((desc (org-export-data (org-element-contents destination) info))
  2179. (href (org-export-solidify-link-text path)))
  2180. (format
  2181. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2182. href desc)))))
  2183. ;; Links pointing to an headline: Find destination and build
  2184. ;; appropriate referencing command.
  2185. ((member type '("custom-id" "fuzzy" "id"))
  2186. (let ((destination (if (string= type "fuzzy")
  2187. (org-export-resolve-fuzzy-link link info)
  2188. (org-export-resolve-id-link link info))))
  2189. (case (org-element-type destination)
  2190. ;; Fuzzy link points nowhere.
  2191. ('nil
  2192. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2193. "Emphasis" (or desc (org-export-data
  2194. (org-element-property
  2195. :raw-link link) info))))
  2196. ;; Fuzzy link points to an invisible target.
  2197. (keyword nil)
  2198. ;; LINK points to an headline. Check if LINK should display
  2199. ;; section numbers.
  2200. (headline
  2201. (let* ((headline-no (org-export-get-headline-number destination info))
  2202. (label (format "sec-%s" (mapconcat 'number-to-string
  2203. headline-no "-"))))
  2204. (cond
  2205. ;; Case 1: Headline is numbered and LINK has no
  2206. ;; description or LINK's description matches headline's
  2207. ;; title. Display section number.
  2208. ((and (org-export-numbered-headline-p destination info)
  2209. (or (not desc) (string= desc (org-element-property
  2210. :raw-value destination))))
  2211. (format
  2212. "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2213. label (mapconcat 'number-to-string headline-no ".")))
  2214. ;; Case 2: Either the headline is un-numbered or
  2215. ;; LINK has a custom description. Display LINK's
  2216. ;; description or headline's title.
  2217. (t
  2218. (let ((desc (or desc (org-export-data
  2219. (org-element-property :title destination)
  2220. info))))
  2221. (format
  2222. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2223. label desc))))))
  2224. ;; Fuzzy link points to a target. Do as above.
  2225. (target
  2226. ;; Identify nearest meaningful container
  2227. (let ((container
  2228. (loop for parent in (org-export-get-genealogy destination)
  2229. when
  2230. (memq
  2231. (org-element-type parent)
  2232. '(footnote-definition footnote-reference headline item
  2233. table))
  2234. return parent)))
  2235. ;; There is a meaningful container
  2236. (when container
  2237. (case (org-element-type container)
  2238. ;; Container is item
  2239. (item
  2240. (format
  2241. "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2242. (org-solidify-link-text path)
  2243. (mapconcat 'number-to-string
  2244. (org-e-odt-resolve-numbered-paragraph
  2245. container info) ".")))))))
  2246. (otherwise
  2247. ;; (unless desc
  2248. ;; (setq number (cond
  2249. ;; ((org-e-odt-standalone-image-p destination info)
  2250. ;; (org-export-get-ordinal
  2251. ;; (assoc 'link (org-element-contents destination))
  2252. ;; info 'link 'org-e-odt-standalone-image-p))
  2253. ;; (t (org-export-get-ordinal destination info))))
  2254. ;; (setq desc (when number
  2255. ;; (if (atom number) (number-to-string number)
  2256. ;; (mapconcat 'number-to-string number ".")))))
  2257. (let ((label-reference
  2258. (org-e-odt-format-label destination info 'reference)))
  2259. (assert label-reference)
  2260. label-reference)))))
  2261. ;; Coderef: replace link with the reference name or the
  2262. ;; equivalent line number.
  2263. ((string= type "coderef")
  2264. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2265. (href (concat "coderef-" path)))
  2266. (format
  2267. (org-export-get-coderef-format path desc)
  2268. (format
  2269. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2270. href line-no))))
  2271. ;; Link type is handled by a special function.
  2272. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2273. (funcall protocol (org-link-unescape path) desc 'odt))
  2274. ;; External link with a description part.
  2275. ((and path desc)
  2276. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2277. path desc))
  2278. ;; External link without a description part.
  2279. (path
  2280. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2281. path path))
  2282. ;; No path, only description. Try to do something useful.
  2283. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2284. "Emphasis" desc)))))
  2285. ;;;; Paragraph
  2286. (defun org-e-odt-paragraph (paragraph contents info)
  2287. "Transcode a PARAGRAPH element from Org to ODT.
  2288. CONTENTS is the contents of the paragraph, as a string. INFO is
  2289. the plist used as a communication channel."
  2290. (let* ((parent (org-export-get-parent paragraph))
  2291. (parent-type (org-element-type parent))
  2292. (style (case parent-type
  2293. (quote-block "Quotations")
  2294. (center-block "OrgCenter")
  2295. (footnote-definition "Footnote")
  2296. (t (or (org-element-property :style paragraph)
  2297. "Text_20_body")))))
  2298. ;; If this paragraph is a leading paragraph in an item and the
  2299. ;; item has a checkbox, splice the checkbox and paragraph contents
  2300. ;; together.
  2301. (when (and (eq (org-element-type parent) 'item)
  2302. (eq paragraph (car (org-element-contents parent))))
  2303. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2304. (assert style)
  2305. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2306. ;;;; Plain List
  2307. (defun org-e-odt-plain-list (plain-list contents info)
  2308. "Transcode a PLAIN-LIST element from Org to ODT.
  2309. CONTENTS is the contents of the list. INFO is a plist holding
  2310. contextual information."
  2311. (org-e-odt--wrap-label
  2312. plain-list
  2313. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2314. ;; Choose style based on list type.
  2315. (case (org-element-property :type plain-list)
  2316. (ordered "OrgNumberedList")
  2317. (unordered "OrgBulletedList")
  2318. (descriptive-1 "OrgDescriptionList")
  2319. (descriptive-2 "OrgDescriptionList"))
  2320. ;; If top-level list, re-start numbering. Otherwise,
  2321. ;; continue numbering.
  2322. (format "text:continue-numbering=\"%s\""
  2323. (let* ((parent (org-export-get-parent plain-list)))
  2324. (if (and parent (eq (org-element-type parent) 'item))
  2325. "true" "false")))
  2326. contents)))
  2327. ;;;; Plain Text
  2328. (defun org-e-odt-fill-tabs-and-spaces (line)
  2329. (replace-regexp-in-string
  2330. "\\([\t]\\|\\([ ]+\\)\\)"
  2331. (lambda (s)
  2332. (cond
  2333. ((string= s "\t") "<text:tab/>")
  2334. (t (let ((n (length s)))
  2335. (cond
  2336. ((= n 1) " ")
  2337. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2338. (t ""))))))
  2339. line))
  2340. (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
  2341. (mapc
  2342. (lambda (pair)
  2343. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2344. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2345. (if no-whitespace-filling text
  2346. (org-e-odt-fill-tabs-and-spaces text)))
  2347. (defun org-e-odt--quotation-marks (text info)
  2348. "Export quotation marks depending on language conventions.
  2349. TEXT is a string containing quotation marks to be replaced. INFO
  2350. is a plist used as a communication channel."
  2351. (mapc (lambda(l)
  2352. (let ((start 0))
  2353. (while (setq start (string-match (car l) text start))
  2354. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2355. (setq text (replace-match new-quote t t text))))))
  2356. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2357. ;; Falls back on English.
  2358. (assoc "en" org-e-odt-quotes))))
  2359. text)
  2360. (defun org-e-odt-plain-text (text info)
  2361. "Transcode a TEXT string from Org to ODT.
  2362. TEXT is the string to transcode. INFO is a plist holding
  2363. contextual information."
  2364. ;; Protect &, < and >.
  2365. (setq text (org-e-odt-encode-plain-text text t))
  2366. ;; Handle quotation marks
  2367. (setq text (org-e-odt--quotation-marks text info))
  2368. ;; Convert special strings.
  2369. (when (plist-get info :with-special-strings)
  2370. (mapc
  2371. (lambda (pair)
  2372. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  2373. org-e-odt-special-string-regexps))
  2374. ;; Handle break preservation if required.
  2375. (when (plist-get info :preserve-breaks)
  2376. (setq text (replace-regexp-in-string
  2377. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  2378. ;; Return value.
  2379. text)
  2380. ;;;; Planning
  2381. (defun org-e-odt-planning (planning contents info)
  2382. "Transcode a PLANNING element from Org to ODT.
  2383. CONTENTS is nil. INFO is a plist used as a communication
  2384. channel."
  2385. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2386. "OrgTimestampWrapper"
  2387. (concat
  2388. (let ((closed (org-element-property :closed planning)))
  2389. (when closed
  2390. (concat
  2391. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2392. "OrgTimestampKeyword" org-closed-string)
  2393. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2394. "OrgTimestamp" (org-translate-time closed)))))
  2395. (let ((deadline (org-element-property :deadline planning)))
  2396. (when deadline
  2397. (concat
  2398. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2399. "OrgTimestampKeyword" org-deadline-string)
  2400. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2401. "OrgTimestamp" (org-translate-time deadline)))))
  2402. (let ((scheduled (org-element-property :scheduled planning)))
  2403. (when scheduled
  2404. (concat
  2405. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2406. "OrgTimestampKeyword" org-scheduled-string)
  2407. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2408. "OrgTimestamp" (org-translate-time scheduled))))))))
  2409. ;;;; Property Drawer
  2410. (defun org-e-odt-property-drawer (property-drawer contents info)
  2411. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2412. CONTENTS is nil. INFO is a plist holding contextual
  2413. information."
  2414. ;; The property drawer isn't exported but we want separating blank
  2415. ;; lines nonetheless.
  2416. "")
  2417. ;;;; Quote Block
  2418. (defun org-e-odt-quote-block (quote-block contents info)
  2419. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2420. CONTENTS holds the contents of the block. INFO is a plist
  2421. holding contextual information."
  2422. (org-e-odt--wrap-label quote-block contents))
  2423. ;;;; Quote Section
  2424. (defun org-e-odt-quote-section (quote-section contents info)
  2425. "Transcode a QUOTE-SECTION element from Org to ODT.
  2426. CONTENTS is nil. INFO is a plist holding contextual information."
  2427. (let ((value (org-remove-indentation
  2428. (org-element-property :value quote-section))))
  2429. (when value (org-e-odt-do-format-code value))))
  2430. ;;;; Section
  2431. (defun org-e-odt-format-section (text style &optional name)
  2432. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2433. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2434. style
  2435. (format "text:name=\"%s\"" (or name default-name))
  2436. text)))
  2437. (defun org-e-odt-section (section contents info) ; FIXME
  2438. "Transcode a SECTION element from Org to ODT.
  2439. CONTENTS holds the contents of the section. INFO is a plist
  2440. holding contextual information."
  2441. contents)
  2442. ;;;; Radio Target
  2443. (defun org-e-odt-radio-target (radio-target text info)
  2444. "Transcode a RADIO-TARGET object from Org to ODT.
  2445. TEXT is the text of the target. INFO is a plist holding
  2446. contextual information."
  2447. (org-e-odt--target
  2448. text (org-export-solidify-link-text
  2449. (org-element-property :value radio-target))))
  2450. ;;;; Special Block
  2451. (defun org-e-odt-special-block (special-block contents info)
  2452. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2453. CONTENTS holds the contents of the block. INFO is a plist
  2454. holding contextual information."
  2455. (let ((type (downcase (org-element-property :type special-block)))
  2456. (attributes (org-export-read-attribute :attr_odt special-block)))
  2457. (org-e-odt--wrap-label
  2458. special-block
  2459. (cond
  2460. ;; Annotation.
  2461. ((string= type "annotation")
  2462. (let ((author (or (plist-get attributes :author)
  2463. (let ((author (plist-get info :author)))
  2464. (and author (org-export-data author info)))))
  2465. (date (or (plist-get attributes :date)
  2466. (plist-get info :date))))
  2467. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2468. "Text_20_body"
  2469. (format "<office:annotation>\n%s\n</office:annotation>"
  2470. (concat
  2471. (and author
  2472. (format "<dc:creator>%s</dc:creator>" author))
  2473. (and date
  2474. (format "<dc:date>%s</dc:date>"
  2475. (org-e-odt--date date)))
  2476. contents)))))
  2477. ;; Textbox.
  2478. ((string= type "textbox")
  2479. (let ((width (plist-get attributes :width))
  2480. (height (plist-get attributes :height))
  2481. (style (plist-get attributes :style))
  2482. (extra (plist-get attributes :extra))
  2483. (anchor (plist-get attributes :anchor)))
  2484. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2485. "Text_20_body" (org-e-odt--textbox contents width height
  2486. style extra anchor))))
  2487. (t contents)))))
  2488. ;;;; Src Block
  2489. (defun org-e-odt-hfy-face-to-css (fn)
  2490. "Create custom style for face FN.
  2491. When FN is the default face, use it's foreground and background
  2492. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2493. use it's color attribute to create a character style whose name
  2494. is obtained from FN. Currently all attributes of FN other than
  2495. color are ignored.
  2496. The style name for a face FN is derived using the following
  2497. operations on the face name in that order - de-dash, CamelCase
  2498. and prefix with \"OrgSrc\". For example,
  2499. `font-lock-function-name-face' is associated with
  2500. \"OrgSrcFontLockFunctionNameFace\"."
  2501. (let* ((css-list (hfy-face-to-style fn))
  2502. (style-name ((lambda (fn)
  2503. (concat "OrgSrc"
  2504. (mapconcat
  2505. 'capitalize (split-string
  2506. (hfy-face-or-def-to-name fn) "-")
  2507. ""))) fn))
  2508. (color-val (cdr (assoc "color" css-list)))
  2509. (background-color-val (cdr (assoc "background" css-list)))
  2510. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2511. (cond
  2512. ((eq fn 'default)
  2513. (format org-e-odt-src-block-paragraph-format
  2514. background-color-val color-val))
  2515. (t
  2516. (format
  2517. "
  2518. <style:style style:name=\"%s\" style:family=\"text\">
  2519. <style:text-properties fo:color=\"%s\"/>
  2520. </style:style>" style-name color-val))))))
  2521. (cons style-name style)))
  2522. (defun org-e-odt-htmlfontify-string (line)
  2523. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2524. (hfy-html-quote-map '(("\"" "&quot;")
  2525. ("<" "&lt;")
  2526. ("&" "&amp;")
  2527. (">" "&gt;")
  2528. (" " "<text:s/>")
  2529. (" " "<text:tab/>")))
  2530. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2531. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  2532. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2533. 'body-text-only))
  2534. (hfy-begin-span-handler
  2535. (lambda (style text-block text-id text-begins-block-p)
  2536. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2537. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2538. (with-no-warnings (htmlfontify-string line))))
  2539. (defun org-e-odt-do-format-code
  2540. (code &optional lang refs retain-labels num-start)
  2541. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2542. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2543. (code-lines (org-split-string code "\n"))
  2544. (code-length (length code-lines))
  2545. (use-htmlfontify-p (and (functionp lang-mode)
  2546. org-e-odt-fontify-srcblocks
  2547. (require 'htmlfontify nil t)
  2548. (fboundp 'htmlfontify-string)))
  2549. (code (if (not use-htmlfontify-p) code
  2550. (with-temp-buffer
  2551. (insert code)
  2552. (funcall lang-mode)
  2553. (font-lock-fontify-buffer)
  2554. (buffer-string))))
  2555. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2556. 'org-e-odt-encode-plain-text))
  2557. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2558. "OrgFixedWidthBlock"))
  2559. (i 0))
  2560. (assert (= code-length (length (org-split-string code "\n"))))
  2561. (setq code
  2562. (org-export-format-code
  2563. code
  2564. (lambda (loc line-num ref)
  2565. (setq par-style
  2566. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2567. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2568. (setq loc (funcall fontifier loc))
  2569. (when ref
  2570. (setq loc (org-e-odt--target loc (concat "coderef-" ref))))
  2571. (assert par-style)
  2572. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2573. par-style loc))
  2574. (if (not line-num) loc
  2575. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2576. num-start refs))
  2577. (cond
  2578. ((not num-start) code)
  2579. ((= num-start 0)
  2580. (format
  2581. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2582. " text:continue-numbering=\"false\"" code))
  2583. (t
  2584. (format
  2585. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2586. " text:continue-numbering=\"true\"" code)))))
  2587. (defun org-e-odt-format-code (element info)
  2588. (let* ((lang (org-element-property :language element))
  2589. ;; Extract code and references.
  2590. (code-info (org-export-unravel-code element))
  2591. (code (car code-info))
  2592. (refs (cdr code-info))
  2593. ;; Does the src block contain labels?
  2594. (retain-labels (org-element-property :retain-labels element))
  2595. ;; Does it have line numbers?
  2596. (num-start (case (org-element-property :number-lines element)
  2597. (continued (org-export-get-loc element info))
  2598. (new 0))))
  2599. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2600. (defun org-e-odt-src-block (src-block contents info)
  2601. "Transcode a SRC-BLOCK element from Org to ODT.
  2602. CONTENTS holds the contents of the item. INFO is a plist holding
  2603. contextual information."
  2604. (let* ((lang (org-element-property :language src-block))
  2605. (caption (org-element-property :caption src-block))
  2606. (short-caption (and (cdr caption)
  2607. (org-export-data (cdr caption) info)))
  2608. (caption (and (car caption) (org-export-data (car caption) info)))
  2609. (label (org-element-property :name src-block))
  2610. (attributes (org-export-read-attribute :attr_odt src-block)))
  2611. ;; FIXME: Handle caption
  2612. ;; caption-str (when caption)
  2613. ;; (main (org-export-data (car caption) info))
  2614. ;; (secondary (org-export-data (cdr caption) info))
  2615. ;; (caption-str (org-e-odt--caption/label-string caption label info))
  2616. (let* ((captions (org-e-odt-format-label src-block info 'definition))
  2617. (caption (car captions)) (short-caption (cdr captions)))
  2618. (concat
  2619. (and caption
  2620. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2621. "Listing" caption))
  2622. (let ((--src-block (org-e-odt-format-code src-block info)))
  2623. (if (not (plist-get attributes :textbox)) --src-block
  2624. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2625. "Text_20_body"
  2626. (org-e-odt--textbox --src-block nil nil nil))))))))
  2627. ;;;; Statistics Cookie
  2628. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2629. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2630. CONTENTS is nil. INFO is a plist holding contextual information."
  2631. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2632. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2633. "OrgCode" cookie-value)))
  2634. ;;;; Strike-Through
  2635. (defun org-e-odt-strike-through (strike-through contents info)
  2636. "Transcode STRIKE-THROUGH from Org to ODT.
  2637. CONTENTS is the text with strike-through markup. INFO is a plist
  2638. holding contextual information."
  2639. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2640. "Strikethrough" contents))
  2641. ;;;; Subscript
  2642. (defun org-e-odt-subscript (subscript contents info)
  2643. "Transcode a SUBSCRIPT object from Org to ODT.
  2644. CONTENTS is the contents of the object. INFO is a plist holding
  2645. contextual information."
  2646. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2647. "OrgSubscript" contents))
  2648. ;;;; Superscript
  2649. (defun org-e-odt-superscript (superscript contents info)
  2650. "Transcode a SUPERSCRIPT object from Org to ODT.
  2651. CONTENTS is the contents of the object. INFO is a plist holding
  2652. contextual information."
  2653. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2654. "OrgSuperscript" contents))
  2655. ;;;; Table Cell
  2656. (defun org-e-odt-table-style-spec (element info)
  2657. (let* ((table (org-export-get-parent-table element))
  2658. (table-attributes (org-export-read-attribute :attr_odt table))
  2659. (table-style (plist-get table-attributes :style)))
  2660. (assoc table-style org-e-odt-table-styles)))
  2661. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2662. "Retrieve styles applicable to a table cell.
  2663. R and C are (zero-based) row and column numbers of the table
  2664. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2665. applicable to the current table. It is `nil' if the table is not
  2666. associated with any style attributes.
  2667. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2668. When STYLE-SPEC is nil, style the table cell the conventional way
  2669. - choose cell borders based on row and column groupings and
  2670. choose paragraph alignment based on `org-col-cookies' text
  2671. property. See also
  2672. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2673. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2674. styles congruent with the ODF-1.2 specification."
  2675. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2676. (r (car table-cell-address)) (c (cdr table-cell-address))
  2677. (style-spec (org-e-odt-table-style-spec table-cell info))
  2678. (table-dimensions (org-export-table-dimensions
  2679. (org-export-get-parent-table table-cell)
  2680. info)))
  2681. (when style-spec
  2682. ;; LibreOffice - particularly the Writer - honors neither table
  2683. ;; templates nor custom table-cell styles. Inorder to retain
  2684. ;; inter-operability with LibreOffice, only automatic styles are
  2685. ;; used for styling of table-cells. The current implementation is
  2686. ;; congruent with ODF-1.2 specification and hence is
  2687. ;; future-compatible.
  2688. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2689. ;; which recognizes as many as 16 different cell types - is much
  2690. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2691. ;; by hand.
  2692. (let* ((template-name (nth 1 style-spec))
  2693. (cell-style-selectors (nth 2 style-spec))
  2694. (cell-type
  2695. (cond
  2696. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2697. (= c 0)) "FirstColumn")
  2698. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2699. (= (1+ c) (cdr table-dimensions)))
  2700. "LastColumn")
  2701. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2702. (= r 0)) "FirstRow")
  2703. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2704. (= (1+ r) (car table-dimensions)))
  2705. "LastRow")
  2706. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2707. (= (% r 2) 1)) "EvenRow")
  2708. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2709. (= (% r 2) 0)) "OddRow")
  2710. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2711. (= (% c 2) 1)) "EvenColumn")
  2712. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2713. (= (% c 2) 0)) "OddColumn")
  2714. (t ""))))
  2715. (concat template-name cell-type)))))
  2716. (defun org-e-odt-table-cell (table-cell contents info)
  2717. "Transcode a TABLE-CELL element from Org to ODT.
  2718. CONTENTS is nil. INFO is a plist used as a communication
  2719. channel."
  2720. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2721. (r (car table-cell-address))
  2722. (c (cdr table-cell-address))
  2723. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2724. (table-row (org-export-get-parent table-cell))
  2725. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2726. table-cell info))
  2727. (paragraph-style
  2728. (or
  2729. (and custom-style-prefix
  2730. (format "%sTableParagraph" custom-style-prefix))
  2731. (concat
  2732. (cond
  2733. ((and (= 1 (org-export-table-row-group table-row info))
  2734. (org-export-table-has-header-p
  2735. (org-export-get-parent-table table-row) info))
  2736. "OrgTableHeading")
  2737. ((let* ((table (org-export-get-parent-table table-cell))
  2738. (table-attrs (org-export-read-attribute :attr_odt table))
  2739. (table-header-columns (plist-get table-attrs
  2740. :header-columns)))
  2741. (<= c (cond ((wholenump table-header-columns)
  2742. (- table-header-columns 1))
  2743. (table-header-columns 0)
  2744. (t -1))))
  2745. "OrgTableHeading")
  2746. (t "OrgTableContents"))
  2747. (capitalize (symbol-name (org-export-table-cell-alignment
  2748. table-cell info))))))
  2749. (cell-style-name
  2750. (or
  2751. (and custom-style-prefix (format "%sTableCell"
  2752. custom-style-prefix))
  2753. (concat
  2754. "OrgTblCell"
  2755. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2756. (zerop r)) "T")
  2757. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2758. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2759. (not (zerop c)) ) "L"))))
  2760. (cell-attributes
  2761. (concat
  2762. (format " table:style-name=\"%s\"" cell-style-name)
  2763. (and (> horiz-span 0)
  2764. (format " table:number-columns-spanned=\"%d\""
  2765. (1+ horiz-span))))))
  2766. (unless contents (setq contents ""))
  2767. (concat
  2768. (assert paragraph-style)
  2769. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2770. cell-attributes
  2771. (let ((table-cell-contents (org-element-contents table-cell)))
  2772. (if (memq (org-element-type (car table-cell-contents))
  2773. org-element-all-elements)
  2774. contents
  2775. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2776. paragraph-style contents))))
  2777. (let (s)
  2778. (dotimes (i horiz-span s)
  2779. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2780. "\n")))
  2781. ;;;; Table Row
  2782. (defun org-e-odt-table-row (table-row contents info)
  2783. "Transcode a TABLE-ROW element from Org to ODT.
  2784. CONTENTS is the contents of the row. INFO is a plist used as a
  2785. communication channel."
  2786. ;; Rules are ignored since table separators are deduced from
  2787. ;; borders of the current row.
  2788. (when (eq (org-element-property :type table-row) 'standard)
  2789. (let* ((rowgroup-tags
  2790. (if (and (= 1 (org-export-table-row-group table-row info))
  2791. (org-export-table-has-header-p
  2792. (org-export-get-parent-table table-row) info))
  2793. ;; If the row belongs to the first rowgroup and the
  2794. ;; table has more than one row groups, then this row
  2795. ;; belongs to the header row group.
  2796. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2797. ;; Otherwise, it belongs to non-header row group.
  2798. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2799. (concat
  2800. ;; Does this row begin a rowgroup?
  2801. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2802. (car rowgroup-tags))
  2803. ;; Actual table row
  2804. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2805. ;; Does this row end a rowgroup?
  2806. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2807. (cdr rowgroup-tags))))))
  2808. ;;;; Table
  2809. (defun org-e-odt-table-first-row-data-cells (table info)
  2810. (let ((table-row
  2811. (org-element-map
  2812. table 'table-row
  2813. (lambda (row)
  2814. (unless (eq (org-element-property :type row) 'rule) row))
  2815. info 'first-match))
  2816. (special-column-p (org-export-table-has-special-column-p table)))
  2817. (if (not special-column-p) (org-element-contents table-row)
  2818. (cdr (org-element-contents table-row)))))
  2819. (defun org-e-odt--table (table contents info)
  2820. "Transcode a TABLE element from Org to ODT.
  2821. CONTENTS is the contents of the table. INFO is a plist holding
  2822. contextual information."
  2823. (case (org-element-property :type table)
  2824. ;; Case 1: table.el doesn't support export to OD format. Strip
  2825. ;; such tables from export.
  2826. (table.el
  2827. (prog1 nil
  2828. (message
  2829. (concat
  2830. "(org-e-odt): Found table.el-type table in the source Org file."
  2831. " table.el doesn't support export to ODT format."
  2832. " Stripping the table from export."))))
  2833. ;; Case 2: Native Org tables.
  2834. (otherwise
  2835. (let* ((captions (org-e-odt-format-label table info 'definition))
  2836. (caption (car captions)) (short-caption (cdr captions))
  2837. (attributes (org-export-read-attribute :attr_odt table))
  2838. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2839. (table-column-specs
  2840. (function
  2841. (lambda (table info)
  2842. (let* ((table-style (or custom-table-style "OrgTable"))
  2843. (column-style (format "%sColumn" table-style)))
  2844. (mapconcat
  2845. (lambda (table-cell)
  2846. (let ((width (1+ (or (org-export-table-cell-width
  2847. table-cell info) 0)))
  2848. (s (format
  2849. "\n<table:table-column table:style-name=\"%s\"/>"
  2850. column-style))
  2851. out)
  2852. (dotimes (i width out) (setq out (concat s out)))))
  2853. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2854. (concat
  2855. ;; caption.
  2856. (when caption
  2857. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2858. "Table" caption))
  2859. ;; begin table.
  2860. (let* ((automatic-name
  2861. (org-e-odt-add-automatic-style "Table" attributes)))
  2862. (format
  2863. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2864. (or short-caption (car automatic-name))
  2865. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2866. ;; column specification.
  2867. (funcall table-column-specs table info)
  2868. ;; actual contents.
  2869. "\n" contents
  2870. ;; end table.
  2871. "</table:table>")))))
  2872. (defun org-e-odt-table (table contents info)
  2873. "Transcode a TABLE element from Org to ODT.
  2874. CONTENTS is the contents of the table. INFO is a plist holding
  2875. contextual information.
  2876. Use `org-e-odt--table' to typeset the table. Handle details
  2877. pertaining to indentation here."
  2878. (let* ((--get-previous-elements
  2879. (function
  2880. (lambda (blob info)
  2881. (let ((parent (org-export-get-parent blob)))
  2882. (cdr (memq blob (reverse (org-element-contents parent))))))))
  2883. (--element-preceded-by-table-p
  2884. (function
  2885. (lambda (element info)
  2886. (loop for el in (funcall --get-previous-elements element info)
  2887. thereis (eq (org-element-type el) 'table)))))
  2888. (--walk-list-genealogy-and-collect-tags
  2889. (function
  2890. (lambda (table info)
  2891. (let* ((genealogy (org-export-get-genealogy table))
  2892. (list-genealogy
  2893. (when (eq (org-element-type (car genealogy)) 'item)
  2894. (loop for el in genealogy
  2895. when (memq (org-element-type el)
  2896. '(item plain-list))
  2897. collect el)))
  2898. (llh-genealogy
  2899. (apply 'nconc
  2900. (loop for el in genealogy
  2901. when (and (eq (org-element-type el) 'headline)
  2902. (org-export-low-level-p el info))
  2903. collect
  2904. (list el
  2905. (assq 'headline
  2906. (org-element-contents
  2907. (org-export-get-parent el)))))))
  2908. parent-list)
  2909. (nconc
  2910. ;; Handle list genealogy.
  2911. (loop for el in list-genealogy collect
  2912. (case (org-element-type el)
  2913. (plain-list
  2914. (setq parent-list el)
  2915. (cons "</text:list>"
  2916. (format "\n<text:list text:style-name=\"%s\" %s>"
  2917. (case (org-element-property :type el)
  2918. (ordered "OrgNumberedList")
  2919. (unordered "OrgBulletedList")
  2920. (descriptive-1 "OrgDescriptionList")
  2921. (descriptive-2 "OrgDescriptionList"))
  2922. "text:continue-numbering=\"true\"")))
  2923. (item
  2924. (cond
  2925. ((not parent-list)
  2926. (if (funcall --element-preceded-by-table-p table info)
  2927. '("</text:list-header>" . "<text:list-header>")
  2928. '("</text:list-item>" . "<text:list-header>")))
  2929. ((funcall --element-preceded-by-table-p
  2930. parent-list info)
  2931. '("</text:list-header>" . "<text:list-header>"))
  2932. (t '("</text:list-item>" . "<text:list-item>"))))))
  2933. ;; Handle low-level headlines.
  2934. (loop for el in llh-genealogy
  2935. with step = 'item collect
  2936. (case step
  2937. (plain-list
  2938. (setq step 'item) ; Flip-flop
  2939. (setq parent-list el)
  2940. (cons "</text:list>"
  2941. (format "\n<text:list text:style-name=\"%s\" %s>"
  2942. (if (org-export-numbered-headline-p
  2943. el info)
  2944. "OrgNumberedList"
  2945. "OrgBulletedList")
  2946. "text:continue-numbering=\"true\"")))
  2947. (item
  2948. (setq step 'plain-list) ; Flip-flop
  2949. (cond
  2950. ((not parent-list)
  2951. (if (funcall --element-preceded-by-table-p table info)
  2952. '("</text:list-header>" . "<text:list-header>")
  2953. '("</text:list-item>" . "<text:list-header>")))
  2954. ((let ((section? (org-export-get-previous-element
  2955. parent-list info)))
  2956. (and section?
  2957. (eq (org-element-type section?) 'section)
  2958. (assq 'table (org-element-contents section?))))
  2959. '("</text:list-header>" . "<text:list-header>"))
  2960. (t
  2961. '("</text:list-item>" . "<text:list-item>")))))))))))
  2962. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2963. table info)))
  2964. ;; OpenDocument schema does not permit table to occur within a
  2965. ;; list item.
  2966. ;; One solution - the easiest and lightweight, in terms of
  2967. ;; implementation - is to put the table in an indented text box
  2968. ;; and make the text box part of the list-item. Unfortunately if
  2969. ;; the table is big and spans multiple pages, the text box could
  2970. ;; overflow. In this case, the following attribute will come
  2971. ;; handy.
  2972. ;; ,---- From OpenDocument-v1.1.pdf
  2973. ;; | 15.27.28 Overflow behavior
  2974. ;; |
  2975. ;; | For text boxes contained within text document, the
  2976. ;; | style:overflow-behavior property specifies the behavior of text
  2977. ;; | boxes where the containing text does not fit into the text
  2978. ;; | box.
  2979. ;; |
  2980. ;; | If the attribute's value is clip, the text that does not fit
  2981. ;; | into the text box is not displayed.
  2982. ;; |
  2983. ;; | If the attribute value is auto-create-new-frame, a new frame
  2984. ;; | will be created on the next page, with the same position and
  2985. ;; | dimensions of the original frame.
  2986. ;; |
  2987. ;; | If the style:overflow-behavior property's value is
  2988. ;; | auto-create-new-frame and the text box has a minimum width or
  2989. ;; | height specified, then the text box will grow until the page
  2990. ;; | bounds are reached before a new frame is created.
  2991. ;; `----
  2992. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  2993. ;; auto-create-new-frame property and always resorts to clipping
  2994. ;; the text box. This results in table being truncated.
  2995. ;; So we solve the problem the hard (and fun) way using list
  2996. ;; continuations.
  2997. ;; The problem only becomes more interesting if you take in to
  2998. ;; account the following facts:
  2999. ;;
  3000. ;; - Description lists are simulated as plain lists.
  3001. ;; - Low-level headlines can be listified.
  3002. ;; - In Org-mode, a table can occur not only as a regular list
  3003. ;; item, but also within description lists and low-level
  3004. ;; headlines.
  3005. ;; See `org-e-odt-translate-description-lists' and
  3006. ;; `org-e-odt-translate-low-level-headlines' for how this is
  3007. ;; tackled.
  3008. (concat "\n"
  3009. ;; Discontinue the list.
  3010. (mapconcat 'car close-open-tags "\n")
  3011. ;; Put the table in an indented section.
  3012. (let* ((table (org-e-odt--table table contents info))
  3013. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3014. (style (format "OrgIndentedSection-Level-%d" level)))
  3015. (when table (org-e-odt-format-section table style)))
  3016. ;; Continue the list.
  3017. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3018. ;;;; Target
  3019. (defun org-e-odt-target (target contents info)
  3020. "Transcode a TARGET object from Org to ODT.
  3021. CONTENTS is nil. INFO is a plist holding contextual
  3022. information."
  3023. (let ((value (org-element-property :value target)))
  3024. (org-e-odt--target "" (org-export-solidify-link-text value))))
  3025. ;;;; Timestamp
  3026. (defun org-e-odt-timestamp (timestamp contents info)
  3027. "Transcode a TIMESTAMP object from Org to ODT.
  3028. CONTENTS is nil. INFO is a plist used as a communication
  3029. channel."
  3030. (let ((timestamp-1 (org-element-property :value timestamp))
  3031. (timestamp-2 (org-element-property :range-end timestamp)))
  3032. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3033. "OrgTimestampWrapper"
  3034. (concat
  3035. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3036. "OrgTimestamp" (org-translate-time timestamp-1))
  3037. (and timestamp-2
  3038. "&#x2013;"
  3039. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3040. "OrgTimestamp" (org-translate-time timestamp-2)))))))
  3041. ;;;; Underline
  3042. (defun org-e-odt-underline (underline contents info)
  3043. "Transcode UNDERLINE from Org to ODT.
  3044. CONTENTS is the text with underline markup. INFO is a plist
  3045. holding contextual information."
  3046. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3047. "Underline" contents))
  3048. ;;;; Verbatim
  3049. (defun org-e-odt-verbatim (verbatim contents info)
  3050. "Transcode a VERBATIM object from Org to ODT.
  3051. CONTENTS is nil. INFO is a plist used as a communication
  3052. channel."
  3053. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3054. "OrgCode" (org-element-property :value verbatim)))
  3055. ;;;; Verse Block
  3056. (defun org-e-odt-verse-block (verse-block contents info)
  3057. "Transcode a VERSE-BLOCK element from Org to ODT.
  3058. CONTENTS is verse block contents. INFO is a plist holding
  3059. contextual information."
  3060. ;; Add line breaks to each line of verse.
  3061. (setq contents (replace-regexp-in-string
  3062. "\\(<text:line-break/>\\)?[ \t]*\n"
  3063. "<text:line-break/>" contents))
  3064. ;; Replace tabs and spaces.
  3065. (setq contents (org-e-odt-fill-tabs-and-spaces contents))
  3066. ;; Surround it in a verse environment.
  3067. (org-e-odt--wrap-label
  3068. verse-block
  3069. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3070. "OrgVerse" contents)))
  3071. ;;; Filters
  3072. ;;;; Description lists
  3073. ;; This translator is necessary to handle indented tables in a uniform
  3074. ;; manner. See comment in `org-e-odt--table'.
  3075. (add-to-list 'org-export-filter-parse-tree-functions
  3076. 'org-e-odt--translate-description-lists)
  3077. (defun org-e-odt--translate-description-lists (tree backend info)
  3078. ;; OpenDocument has no notion of a description list. So simulate it
  3079. ;; using plain lists. Description lists in the exported document
  3080. ;; are typeset in the same manner as they are in a typical HTML
  3081. ;; document.
  3082. ;;
  3083. ;; Specifically, a description list like this:
  3084. ;;
  3085. ;; ,----
  3086. ;; | - term-1 :: definition-1
  3087. ;; | - term-2 :: definition-2
  3088. ;; `----
  3089. ;;
  3090. ;; gets translated in to the following form:
  3091. ;;
  3092. ;; ,----
  3093. ;; | - term-1
  3094. ;; | - definition-1
  3095. ;; | - term-2
  3096. ;; | - definition-2
  3097. ;; `----
  3098. ;;
  3099. ;; Further effect is achieved by fixing the OD styles as below:
  3100. ;;
  3101. ;; 1. Set the :type property of the simulated lists to
  3102. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3103. ;; that has *no* bullets whatsoever.
  3104. ;;
  3105. ;; 2. The paragraph containing the definition term is styled to be
  3106. ;; in bold.
  3107. ;;
  3108. (when (eq backend 'e-odt)
  3109. (org-element-map
  3110. tree 'plain-list
  3111. (lambda (el)
  3112. (when (equal (org-element-property :type el) 'descriptive)
  3113. (org-element-set-element
  3114. el
  3115. (apply 'org-element-adopt-elements
  3116. (list 'plain-list (list :type 'descriptive-1))
  3117. (mapcar
  3118. (lambda (item)
  3119. (org-element-adopt-elements
  3120. (list 'item (list :checkbox (org-element-property
  3121. :checkbox item)))
  3122. (list 'paragraph (list :style "Text_20_body_20_bold")
  3123. (or (org-element-property :tag item) "(no term)"))
  3124. (org-element-adopt-elements
  3125. (list 'plain-list (list :type 'descriptive-2))
  3126. (apply 'org-element-adopt-elements
  3127. (list 'item nil)
  3128. (org-element-contents item)))))
  3129. (org-element-contents el)))))
  3130. nil)
  3131. info))
  3132. tree)
  3133. ;;;; List tables
  3134. ;; Lists that are marked with attribute `:list-table' are called as
  3135. ;; list tables. They will be rendered as a table within the exported
  3136. ;; document.
  3137. ;; Consider an example. The following list table
  3138. ;;
  3139. ;; #+attr_odt :list-table t
  3140. ;; - Row 1
  3141. ;; - 1.1
  3142. ;; - 1.2
  3143. ;; - 1.3
  3144. ;; - Row 2
  3145. ;; - 2.1
  3146. ;; - 2.2
  3147. ;; - 2.3
  3148. ;;
  3149. ;; will be exported as though it were an Org table like the one show
  3150. ;; below.
  3151. ;;
  3152. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3153. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3154. ;;
  3155. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3156. ;; a unique row in the exported document. So if an exported table
  3157. ;; needs to contain a single paragraph (with copious text) it needs to
  3158. ;; be typed up in a single line. Editing such long lines using the
  3159. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3160. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3161. ;;
  3162. ;; A LIST-TABLE circumvents above problems.
  3163. ;;
  3164. ;; Note that in the example above the list items could be paragraphs
  3165. ;; themselves and the list can be arbitrarily deep.
  3166. ;;
  3167. ;; Inspired by following thread:
  3168. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
  3169. ;; Translate lists to tables
  3170. (add-to-list 'org-export-filter-parse-tree-functions
  3171. 'org-e-odt--translate-list-tables)
  3172. (defun org-e-odt--translate-list-tables (tree backend info)
  3173. (when (eq backend 'e-odt)
  3174. (org-element-map
  3175. tree 'plain-list
  3176. (lambda (plain-list-1)
  3177. (when (org-export-read-attribute :attr_odt plain-list-1 :list-table)
  3178. (org-element-set-element
  3179. plain-list-1
  3180. (apply 'org-element-adopt-elements
  3181. (list 'table nil)
  3182. (org-element-map
  3183. plain-list-1
  3184. 'item
  3185. (lambda (level-1-item)
  3186. (apply 'org-element-adopt-elements
  3187. (list 'table-row (list :type 'standard))
  3188. (org-element-adopt-elements
  3189. (list 'table-cell nil)
  3190. (car (org-element-contents level-1-item)))
  3191. (let ((plain-list-2 (assq 'plain-list level-1-item)))
  3192. (org-element-map
  3193. plain-list-2
  3194. 'item
  3195. (lambda (item)
  3196. (apply 'org-element-adopt-elements
  3197. (list 'table-cell nil)
  3198. (org-element-contents item)))
  3199. info nil 'item))))
  3200. info nil 'item))))
  3201. nil)
  3202. info))
  3203. tree)
  3204. ;;; Interactive functions
  3205. (defun org-e-odt-create-manifest-file-entry (&rest args)
  3206. (push args org-e-odt-manifest-file-entries))
  3207. (defun org-e-odt-write-manifest-file ()
  3208. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  3209. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  3210. (with-current-buffer
  3211. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3212. (find-file-noselect manifest-file t))
  3213. (insert
  3214. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3215. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3216. (mapc
  3217. (lambda (file-entry)
  3218. (let* ((version (nth 2 file-entry))
  3219. (extra (if (not version) ""
  3220. (format " manifest:version=\"%s\"" version))))
  3221. (insert
  3222. (format org-e-odt-manifest-file-entry-tag
  3223. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3224. org-e-odt-manifest-file-entries)
  3225. (insert "\n</manifest:manifest>"))))
  3226. (defmacro org-e-odt--export-wrap (out-file &rest body)
  3227. `(let* ((--out-file ,out-file)
  3228. (out-file-type (file-name-extension --out-file))
  3229. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3230. "meta.xml" "styles.xml"))
  3231. ;; Initialize workarea. All files that end up in the
  3232. ;; exported get created here.
  3233. (org-e-odt-zip-dir (file-name-as-directory
  3234. (make-temp-file (format "%s-" out-file-type) t)))
  3235. (org-e-odt-manifest-file-entries nil)
  3236. (--cleanup-xml-buffers
  3237. (function
  3238. (lambda nil
  3239. ;; Kill all XML buffers.
  3240. (mapc (lambda (file)
  3241. (let ((buf (get-file-buffer
  3242. (concat org-e-odt-zip-dir file))))
  3243. (when buf
  3244. (set-buffer-modified-p nil)
  3245. (kill-buffer buf))))
  3246. org-e-odt-xml-files)
  3247. ;; Delete temporary directory and also other embedded
  3248. ;; files that get copied there.
  3249. (delete-directory org-e-odt-zip-dir t)))))
  3250. (org-condition-case-unless-debug err
  3251. (progn
  3252. (unless (executable-find "zip")
  3253. ;; Not at all OSes ship with zip by default
  3254. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3255. ;; Do export. This creates a bunch of xml files ready to be
  3256. ;; saved and zipped.
  3257. (progn ,@body)
  3258. ;; Create a manifest entry for content.xml.
  3259. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  3260. ;; Write mimetype file
  3261. (let* ((mimetypes
  3262. '(("odt" . "application/vnd.oasis.opendocument.text")
  3263. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3264. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3265. (unless mimetype
  3266. (error "Unknown OpenDocument backend %S" out-file-type))
  3267. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  3268. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3269. ;; Write out the manifest entries before zipping
  3270. (org-e-odt-write-manifest-file)
  3271. ;; Save all XML files.
  3272. (mapc (lambda (file)
  3273. (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
  3274. (when buf
  3275. (with-current-buffer buf
  3276. ;; Prettify output if needed.
  3277. (when org-e-odt-prettify-xml
  3278. (indent-region (point-min) (point-max)))
  3279. (save-buffer 0)))))
  3280. org-e-odt-xml-files)
  3281. ;; Run zip.
  3282. (let* ((target --out-file)
  3283. (target-name (file-name-nondirectory target))
  3284. (target-dir (file-name-directory target))
  3285. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3286. ("zip" "-rmTq" ,target-name "."))))
  3287. ;; If a file with same name as the desired output file
  3288. ;; exists, remove it.
  3289. (when (file-exists-p target)
  3290. (delete-file target))
  3291. ;; Zip up the xml files.
  3292. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3293. (message "Creating ODT file...")
  3294. ;; Switch temporarily to content.xml. This way Zip
  3295. ;; process will inherit `org-e-odt-zip-dir' as the current
  3296. ;; directory.
  3297. (with-current-buffer
  3298. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3299. (mapc
  3300. (lambda (cmd)
  3301. (message "Running %s" (mapconcat 'identity cmd " "))
  3302. (setq err-string
  3303. (with-output-to-string
  3304. (setq exitcode
  3305. (apply 'call-process (car cmd)
  3306. nil standard-output nil (cdr cmd)))))
  3307. (or (zerop exitcode)
  3308. (error (concat "Unable to create OpenDocument file."
  3309. (format " Zip failed with error (%s)"
  3310. err-string)))))
  3311. cmds)
  3312. ;; Zip file is now in the rightful place.
  3313. (rename-file target-name target)))
  3314. (message "Created %s" target)
  3315. ;; Cleanup work directory and work files.
  3316. (funcall --cleanup-xml-buffers)
  3317. ;; Open the OpenDocument file in archive-mode for
  3318. ;; examination.
  3319. (find-file-noselect target t)
  3320. ;; Return exported file.
  3321. (cond
  3322. ;; Case 1: Conversion desired on exported file. Run the
  3323. ;; converter on the OpenDocument file. Return the
  3324. ;; converted file.
  3325. (org-e-odt-preferred-output-format
  3326. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3327. target))
  3328. ;; Case 2: No further conversion. Return exported
  3329. ;; OpenDocument file.
  3330. (t target))))
  3331. (error
  3332. ;; Cleanup work directory and work files.
  3333. (funcall --cleanup-xml-buffers)
  3334. (message "OpenDocument export failed: %s"
  3335. (error-message-string err))))))
  3336. ;;;; Export to OpenDocument formula
  3337. ;;;###autoload
  3338. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3339. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3340. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3341. MathML. When invoked as an interactive command, use
  3342. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3343. region. If no LaTeX fragments are found, prompt for it. Push
  3344. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3345. non-nil."
  3346. (interactive
  3347. `(,(let (frag)
  3348. (setq frag (and (setq frag (and (region-active-p)
  3349. (buffer-substring (region-beginning)
  3350. (region-end))))
  3351. (loop for e in org-latex-regexps
  3352. thereis (when (string-match (nth 1 e) frag)
  3353. (match-string (nth 2 e) frag)))))
  3354. (read-string "LaTeX Fragment: " frag nil frag))
  3355. ,(let ((odf-filename (expand-file-name
  3356. (concat
  3357. (file-name-sans-extension
  3358. (or (file-name-nondirectory buffer-file-name)))
  3359. "." "odf")
  3360. (file-name-directory buffer-file-name))))
  3361. (read-file-name "ODF filename: " nil odf-filename nil
  3362. (file-name-nondirectory odf-filename)))))
  3363. (let ((filename (or odf-file
  3364. (expand-file-name
  3365. (concat
  3366. (file-name-sans-extension
  3367. (or (file-name-nondirectory buffer-file-name)))
  3368. "." "odf")
  3369. (file-name-directory buffer-file-name)))))
  3370. (org-e-odt--export-wrap
  3371. filename
  3372. (let* ((buffer (progn
  3373. (require 'nxml-mode)
  3374. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3375. (find-file-noselect (concat org-e-odt-zip-dir
  3376. "content.xml") t))))
  3377. (coding-system-for-write 'utf-8)
  3378. (save-buffer-coding-system 'utf-8))
  3379. (set-buffer buffer)
  3380. (set-buffer-file-coding-system coding-system-for-write)
  3381. (let ((mathml (org-create-math-formula latex-frag)))
  3382. (unless mathml (error "No Math formula created"))
  3383. (insert mathml)
  3384. ;; Add MathML to kill ring, if needed.
  3385. (when org-export-copy-to-kill-ring
  3386. (org-kill-new (buffer-string))))))))
  3387. ;;;###autoload
  3388. (defun org-e-odt-export-as-odf-and-open ()
  3389. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3390. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3391. formula file."
  3392. (interactive)
  3393. (org-open-file (call-interactively 'org-e-odt-export-as-odf) 'system))
  3394. ;;;; Export to OpenDocument Text
  3395. ;;;###autoload
  3396. (defun org-e-odt-export-to-odt
  3397. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3398. "Export current buffer to a HTML file.
  3399. If narrowing is active in the current buffer, only export its
  3400. narrowed part.
  3401. If a region is active, export that region.
  3402. When optional argument SUBTREEP is non-nil, export the sub-tree
  3403. at point, extracting information from the headline properties
  3404. first.
  3405. When optional argument VISIBLE-ONLY is non-nil, don't export
  3406. contents of hidden elements.
  3407. When optional argument BODY-ONLY is non-nil, only write code
  3408. between \"\\begin{document}\" and \"\\end{document}\".
  3409. EXT-PLIST, when provided, is a property list with external
  3410. parameters overriding Org default settings, but still inferior to
  3411. file-local settings.
  3412. When optional argument PUB-DIR is set, use it as the publishing
  3413. directory.
  3414. Return output file's name."
  3415. (interactive)
  3416. (org-e-odt--export-wrap
  3417. (org-export-output-file-name ".odt" subtreep pub-dir)
  3418. (let* ((org-e-odt-embedded-images-count 0)
  3419. (org-e-odt-embedded-formulas-count 0)
  3420. (org-e-odt-automatic-styles nil)
  3421. (org-e-odt-object-counters nil)
  3422. ;; Let `htmlfontify' know that we are interested in collecting
  3423. ;; styles.
  3424. (hfy-user-sheet-assoc nil))
  3425. ;; Initialize content.xml and kick-off the export process.
  3426. (let ((out-buf (progn
  3427. (require 'nxml-mode)
  3428. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3429. (find-file-noselect
  3430. (concat org-e-odt-zip-dir "content.xml") t)))))
  3431. (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
  3432. ;;;; Convert between OpenDocument and other formats
  3433. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3434. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3435. (catch 'done
  3436. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3437. (dolist (e reachable-formats)
  3438. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3439. (when out-fmt-spec
  3440. (throw 'done (cons (car e) out-fmt-spec))))))))
  3441. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3442. "Workhorse routine for `org-e-odt-convert'."
  3443. (require 'browse-url)
  3444. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3445. (dummy (or (file-readable-p in-file)
  3446. (error "Cannot read %s" in-file)))
  3447. (in-fmt (file-name-extension in-file))
  3448. (out-fmt (or out-fmt (error "Output format unspecified")))
  3449. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3450. (error "Cannot convert from %s format to %s format?"
  3451. in-fmt out-fmt)))
  3452. (convert-process (car how))
  3453. (out-file (concat (file-name-sans-extension in-file) "."
  3454. (nth 1 (or (cdr how) out-fmt))))
  3455. (extra-options (or (nth 2 (cdr how)) ""))
  3456. (out-dir (file-name-directory in-file))
  3457. (cmd (format-spec convert-process
  3458. `((?i . ,(shell-quote-argument in-file))
  3459. (?I . ,(browse-url-file-url in-file))
  3460. (?f . ,out-fmt)
  3461. (?o . ,out-file)
  3462. (?O . ,(browse-url-file-url out-file))
  3463. (?d . , (shell-quote-argument out-dir))
  3464. (?D . ,(browse-url-file-url out-dir))
  3465. (?x . ,extra-options)))))
  3466. (when (file-exists-p out-file)
  3467. (delete-file out-file))
  3468. (message "Executing %s" cmd)
  3469. (let ((cmd-output (shell-command-to-string cmd)))
  3470. (message "%s" cmd-output))
  3471. (cond
  3472. ((file-exists-p out-file)
  3473. (message "Exported to %s" out-file)
  3474. (when prefix-arg
  3475. (message "Opening %s..." out-file)
  3476. (org-open-file out-file 'system))
  3477. out-file)
  3478. (t
  3479. (message "Export to %s failed" out-file)
  3480. nil))))
  3481. (defun org-e-odt-do-reachable-formats (in-fmt)
  3482. "Return verbose info about formats to which IN-FMT can be converted.
  3483. Return a list where each element is of the
  3484. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3485. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3486. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3487. (let* ((converter
  3488. (and org-e-odt-convert-process
  3489. (cadr (assoc-string org-e-odt-convert-process
  3490. org-e-odt-convert-processes t))))
  3491. (capabilities
  3492. (and org-e-odt-convert-process
  3493. (cadr (assoc-string org-e-odt-convert-process
  3494. org-e-odt-convert-processes t))
  3495. org-e-odt-convert-capabilities))
  3496. reachable-formats)
  3497. (when converter
  3498. (dolist (c capabilities)
  3499. (when (member in-fmt (nth 1 c))
  3500. (push (cons converter (nth 2 c)) reachable-formats))))
  3501. reachable-formats))
  3502. (defun org-e-odt-reachable-formats (in-fmt)
  3503. "Return list of formats to which IN-FMT can be converted.
  3504. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3505. (let (l)
  3506. (mapc (lambda (e) (add-to-list 'l e))
  3507. (apply 'append (mapcar
  3508. (lambda (e) (mapcar 'car (cdr e)))
  3509. (org-e-odt-do-reachable-formats in-fmt))))
  3510. l))
  3511. (defun org-e-odt-convert-read-params ()
  3512. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3513. This is a helper routine for interactive use."
  3514. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3515. (in-file (read-file-name "File to be converted: "
  3516. nil buffer-file-name t))
  3517. (in-fmt (file-name-extension in-file))
  3518. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3519. (out-fmt
  3520. (or (and out-fmt-choices
  3521. (funcall input "Output format: "
  3522. out-fmt-choices nil nil nil))
  3523. (error
  3524. "No known converter or no known output formats for %s files"
  3525. in-fmt))))
  3526. (list in-file out-fmt)))
  3527. ;;;###autoload
  3528. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3529. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3530. IN-FILE is the file to be converted. If unspecified, it defaults
  3531. to variable `buffer-file-name'. OUT-FMT is the desired output
  3532. format. Use `org-e-odt-convert-process' as the converter.
  3533. If PREFIX-ARG is non-nil then the newly converted file is opened
  3534. using `org-open-file'."
  3535. (interactive
  3536. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3537. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3538. ;;; Library Initializations
  3539. (mapc
  3540. (lambda (desc)
  3541. ;; Let Emacs open all OpenDocument files in archive mode
  3542. (add-to-list 'auto-mode-alist
  3543. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3544. org-e-odt-file-extensions)
  3545. (provide 'org-e-odt)
  3546. ;;; org-e-odt.el ends here