org-e-odt.el 143 KB

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