org-e-odt.el 141 KB

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