org-e-odt.el 143 KB

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